1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-3-Clause
3 * Copyright 2022 Google LLC
12 #include <sandbox_host.h>
14 #include <dm/device-internal.h>
16 #include <test/test.h>
19 /* Basic test of host interface */
20 static int dm_test_host(struct unit_test_state *uts)
22 static char label[] = "test";
23 struct udevice *dev, *part, *chk, *blk;
24 struct host_sb_plat *plat;
25 struct blk_desc *desc;
30 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_HOST, &dev));
31 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));
33 mem_start = ut_check_delta(0);
34 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
36 /* Check that the plat data has been allocated */
37 plat = dev_get_plat(dev);
38 ut_asserteq_str("test", plat->label);
39 ut_assert(label != plat->label);
40 ut_asserteq(0, plat->fd);
42 /* Attach a file created in test_ut_dm_init */
43 ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img"));
45 ut_assertok(host_attach_file(dev, fname));
46 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
47 ut_asserteq_ptr(chk, dev);
49 ut_asserteq_str(fname, plat->filename);
50 ut_assert(fname != plat->filename);
51 ut_assert(plat->fd != 0);
53 /* Get the block device */
54 ut_assertok(blk_get_from_parent(dev, &blk));
55 ut_assertok(device_probe(blk));
57 /* There should be no partition table in this device */
58 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));
60 /* Write to a file on the ext4 filesystem */
61 desc = dev_get_uclass_plat(blk);
62 ut_asserteq(true, desc->removable);
63 ut_assertok(fs_set_blk_dev_with_part(desc, 0));
64 ut_assertok(fs_write("/testing", 0, 0, 0x1000, &actwrite));
66 ut_assertok(host_detach_file(dev));
67 ut_asserteq(0, plat->fd);
68 ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
69 ut_assertok(device_unbind(dev));
71 /* check there were no memory leaks */
72 ut_asserteq(0, ut_check_delta(mem_start));
76 DM_TEST(dm_test_host, UT_TESTF_SCAN_FDT);
78 /* reusing the same label should work */
79 static int dm_test_host_dup(struct unit_test_state *uts)
81 static char label[] = "test";
82 struct udevice *dev, *chk;
85 ut_asserteq(0, uclass_id_count(UCLASS_HOST));
86 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
88 /* Attach a file created in test_ut_dm_init */
89 ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img"));
90 ut_assertok(host_attach_file(dev, fname));
91 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
92 ut_asserteq_ptr(chk, dev);
93 ut_asserteq(1, uclass_id_count(UCLASS_HOST));
95 /* Create another device with the same label (should remove old one) */
96 ut_assertok(host_create_device(label, true, DEFAULT_BLKSZ, &dev));
98 /* Attach a different file created in test_ut_dm_init */
99 ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img"));
100 ut_assertok(host_attach_file(dev, fname));
102 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
103 ut_asserteq_ptr(chk, dev);
105 /* Make sure there is still only one device */
106 ut_asserteq(1, uclass_id_count(UCLASS_HOST));
110 DM_TEST(dm_test_host_dup, UT_TESTF_SCAN_FDT);
112 /* Basic test of 'host' command */
113 static int dm_test_cmd_host(struct unit_test_state *uts)
115 struct udevice *dev, *blk;
116 struct blk_desc *desc;
119 console_record_reset();
121 /* first check 'host info' with binding */
122 ut_assertok(run_command("host info", 0));
123 ut_assert_nextline("dev blocks blksz label path");
124 ut_assert_console_end();
126 ut_assertok(os_persistent_file(fname, sizeof(fname), "2MB.ext2.img"));
127 ut_assertok(run_commandf("host bind -r test2 %s", fname));
129 /* Check the -r flag worked */
130 ut_assertok(uclass_first_device_err(UCLASS_HOST, &dev));
131 ut_assertok(blk_get_from_parent(dev, &blk));
132 desc = dev_get_uclass_plat(blk);
133 ut_asserteq(true, desc->removable);
135 ut_assertok(run_command("host info", 0));
136 ut_assert_nextline("dev blocks blksz label path");
137 ut_assert_nextlinen(" 0 4096 512 test2");
138 ut_assert_console_end();
140 ut_assertok(os_persistent_file(fname, sizeof(fname), "1MB.fat32.img"));
141 ut_assertok(run_commandf("host bind fat %s", fname));
143 /* Check it is not removable (no '-r') */
144 ut_assertok(uclass_next_device_err(&dev));
145 ut_assertok(blk_get_from_parent(dev, &blk));
146 desc = dev_get_uclass_plat(blk);
147 ut_asserteq(false, desc->removable);
149 ut_assertok(run_command("host info", 0));
150 ut_assert_nextline("dev blocks blksz label path");
151 ut_assert_nextlinen(" 0 4096 512 test2");
152 ut_assert_nextlinen(" 1 2048 512 fat");
153 ut_assert_console_end();
155 ut_asserteq(1, run_command("host info test", 0));
156 ut_assert_nextline("No such device 'test'");
157 ut_assert_console_end();
159 ut_assertok(run_command("host info fat", 0));
160 ut_assert_nextline("dev blocks blksz label path");
161 ut_assert_nextlinen(" 1 2048 512 fat");
162 ut_assert_console_end();
164 /* check 'host dev' */
165 ut_asserteq(1, run_command("host dev", 0));
166 ut_assert_nextline("No current host device");
167 ut_assert_console_end();
169 ut_asserteq(1, run_command("host dev missing", 0));
170 ut_assert_nextline("No such device 'missing'");
171 ut_assert_console_end();
173 ut_assertok(run_command("host dev fat", 0));
174 ut_assert_console_end();
176 ut_assertok(run_command("host dev", 0));
177 ut_assert_nextline("Current host device: 1: fat");
178 ut_assert_console_end();
180 /* Try a numerical label */
181 ut_assertok(run_command("host dev 0", 0));
182 ut_assert_console_end();
184 ut_assertok(run_command("host dev", 0));
185 ut_assert_nextline("Current host device: 0: test2");
186 ut_assert_console_end();
188 /* Remove one of the bindings */
189 ut_assertok(run_commandf("host unbind test2"));
191 /* There should now be no current device */
192 ut_asserteq(1, run_command("host dev", 0));
193 ut_assert_nextline("No current host device");
194 ut_assert_console_end();
196 ut_assertok(run_command("host info", 0));
197 ut_assert_nextline("dev blocks blksz label path");
198 ut_assert_nextlinen(" 1 2048 512 fat");
199 ut_assert_console_end();
203 DM_TEST(dm_test_cmd_host, UT_TESTF_SCAN_FDT);