1
2
3
4
5
6
7#include <common.h>
8#include <blk.h>
9#include <dm.h>
10#include <fs.h>
11#include <sandbox_host.h>
12#include <asm/test.h>
13#include <dm/device-internal.h>
14#include <dm/test.h>
15#include <test/test.h>
16#include <test/ut.h>
17
18static const char filename[] = "2MB.ext2.img";
19static const char filename2[] = "1MB.fat32.img";
20
21
22static int dm_test_host(struct unit_test_state *uts)
23{
24 static char label[] = "test";
25 struct udevice *dev, *part, *chk, *blk;
26 struct host_sb_plat *plat;
27 struct blk_desc *desc;
28 ulong mem_start;
29 loff_t actwrite;
30
31 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_HOST, &dev));
32 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));
33
34 mem_start = ut_check_delta(0);
35 ut_assertok(host_create_device(label, true, &dev));
36
37
38 plat = dev_get_plat(dev);
39 ut_asserteq_str("test", plat->label);
40 ut_assert(label != plat->label);
41 ut_asserteq(0, plat->fd);
42
43
44 ut_assertok(host_attach_file(dev, filename));
45 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
46 ut_asserteq_ptr(chk, dev);
47
48 ut_asserteq_str(filename, plat->filename);
49 ut_assert(filename != plat->filename);
50 ut_assert(plat->fd != 0);
51
52
53 ut_assertok(blk_get_from_parent(dev, &blk));
54 ut_assertok(device_probe(blk));
55
56
57 ut_asserteq(-ENODEV, uclass_first_device_err(UCLASS_PARTITION, &part));
58
59
60 desc = dev_get_uclass_plat(blk);
61 ut_asserteq(true, desc->removable);
62 ut_assertok(fs_set_blk_dev_with_part(desc, 0));
63 ut_assertok(fs_write("/testing", 0, 0, 0x1000, &actwrite));
64
65 ut_assertok(host_detach_file(dev));
66 ut_asserteq(0, plat->fd);
67 ut_asserteq(-ENODEV, blk_get_from_parent(dev, &blk));
68 ut_assertok(device_unbind(dev));
69
70
71 ut_asserteq(0, ut_check_delta(mem_start));
72
73 return 0;
74}
75DM_TEST(dm_test_host, UT_TESTF_SCAN_FDT);
76
77
78static int dm_test_host_dup(struct unit_test_state *uts)
79{
80 static char label[] = "test";
81 struct udevice *dev, *chk;
82
83 ut_asserteq(0, uclass_id_count(UCLASS_HOST));
84 ut_assertok(host_create_device(label, true, &dev));
85
86
87 ut_assertok(host_attach_file(dev, filename));
88 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
89 ut_asserteq_ptr(chk, dev);
90 ut_asserteq(1, uclass_id_count(UCLASS_HOST));
91
92
93 ut_assertok(host_create_device(label, true, &dev));
94
95
96 ut_assertok(host_attach_file(dev, filename2));
97 ut_assertok(uclass_first_device_err(UCLASS_HOST, &chk));
98 ut_asserteq_ptr(chk, dev);
99
100
101 ut_asserteq(1, uclass_id_count(UCLASS_HOST));
102
103 return 0;
104}
105DM_TEST(dm_test_host_dup, UT_TESTF_SCAN_FDT);
106
107
108static int dm_test_cmd_host(struct unit_test_state *uts)
109{
110 struct udevice *dev, *blk;
111 struct blk_desc *desc;
112
113 console_record_reset();
114
115
116 ut_assertok(run_command("host info", 0));
117 ut_assert_nextline("dev blocks label path");
118 ut_assert_console_end();
119
120 ut_assertok(run_commandf("host bind -r test2 %s", filename));
121
122
123 ut_assertok(uclass_first_device_err(UCLASS_HOST, &dev));
124 ut_assertok(blk_get_from_parent(dev, &blk));
125 desc = dev_get_uclass_plat(blk);
126 ut_asserteq(true, desc->removable);
127
128 ut_assertok(run_command("host info", 0));
129 ut_assert_nextline("dev blocks label path");
130 ut_assert_nextline(" 0 4096 test2 2MB.ext2.img");
131 ut_assert_console_end();
132
133 ut_assertok(run_commandf("host bind fat %s", filename2));
134
135
136 ut_assertok(uclass_next_device_err(&dev));
137 ut_assertok(blk_get_from_parent(dev, &blk));
138 desc = dev_get_uclass_plat(blk);
139 ut_asserteq(false, desc->removable);
140
141 ut_assertok(run_command("host info", 0));
142 ut_assert_nextline("dev blocks label path");
143 ut_assert_nextline(" 0 4096 test2 2MB.ext2.img");
144 ut_assert_nextline(" 1 2048 fat 1MB.fat32.img");
145 ut_assert_console_end();
146
147 ut_asserteq(1, run_command("host info test", 0));
148 ut_assert_nextline("No such device 'test'");
149 ut_assert_console_end();
150
151 ut_assertok(run_command("host info fat", 0));
152 ut_assert_nextline("dev blocks label path");
153 ut_assert_nextline(" 1 2048 fat 1MB.fat32.img");
154 ut_assert_console_end();
155
156
157 ut_asserteq(1, run_command("host dev", 0));
158 ut_assert_nextline("No current host device");
159 ut_assert_console_end();
160
161 ut_asserteq(1, run_command("host dev missing", 0));
162 ut_assert_nextline("No such device 'missing'");
163 ut_assert_console_end();
164
165 ut_assertok(run_command("host dev fat", 0));
166 ut_assert_console_end();
167
168 ut_assertok(run_command("host dev", 0));
169 ut_assert_nextline("Current host device: 1: fat");
170 ut_assert_console_end();
171
172
173 ut_assertok(run_command("host dev 0", 0));
174 ut_assert_console_end();
175
176 ut_assertok(run_command("host dev", 0));
177 ut_assert_nextline("Current host device: 0: test2");
178 ut_assert_console_end();
179
180
181 ut_assertok(run_commandf("host unbind test2"));
182
183
184 ut_asserteq(1, run_command("host dev", 0));
185 ut_assert_nextline("No current host device");
186 ut_assert_console_end();
187
188 ut_assertok(run_command("host info", 0));
189 ut_assert_nextline("dev blocks label path");
190 ut_assert_nextline(" 1 2048 fat 1MB.fat32.img");
191 ut_assert_console_end();
192
193 return 0;
194}
195DM_TEST(dm_test_cmd_host, UT_TESTF_SCAN_FDT);
196