]> git.ozlabs.org Git - petitboot/blob - test/parser/utils.c
discover/yaboot: fix device parsing
[petitboot] / test / parser / utils.c
1
2 #include <assert.h>
3 #include <err.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9
10 #include <talloc/talloc.h>
11 #include <types/types.h>
12 #include <url/url.h>
13
14 #include "device-handler.h"
15 #include "parser.h"
16 #include "resource.h"
17
18 #include "parser-test.h"
19
20 struct p_item {
21         struct list_item list;
22         struct parser *parser;
23 };
24
25 STATIC_LIST(parsers);
26
27 void __register_parser(struct parser *parser)
28 {
29         struct p_item* i = talloc(NULL, struct p_item);
30
31         i->parser = parser;
32         list_add(&parsers, &i->list);
33 }
34
35 static void __attribute__((destructor)) __cleanup_parsers(void)
36 {
37         struct p_item *item, *tmp;
38
39         list_for_each_entry_safe(&parsers, item, tmp, list)
40                 talloc_free(item);
41 }
42
43 static struct discover_device *test_create_device_simple(
44                 struct discover_context *ctx)
45 {
46         static int dev_idx;
47         char name[10];
48
49         sprintf(name, "__test%d", dev_idx++);
50
51         return test_create_device(ctx, name);
52 }
53
54 struct discover_device *test_create_device(struct discover_context *ctx,
55                 const char *name)
56 {
57         struct discover_device *dev;
58
59         dev = talloc_zero(ctx, struct discover_device);
60         dev->device = talloc_zero(dev, struct device);
61
62         list_init(&dev->boot_options);
63
64         dev->device->id = talloc_strdup(dev, name);
65         dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
66         dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
67
68         return dev;
69 }
70
71 static struct discover_context *test_create_context(struct parser_test *test)
72 {
73         struct discover_context *ctx;
74
75         ctx = talloc_zero(test, struct discover_context);
76         assert(ctx);
77
78         list_init(&ctx->boot_options);
79         ctx->device = test_create_device_simple(ctx);
80
81         return ctx;
82 }
83
84 struct parser_test *test_init(void)
85 {
86         struct parser_test *test;
87
88         test = talloc_zero(NULL, struct parser_test);
89         test->handler = device_handler_init(NULL, NULL, 0);
90         test->ctx = test_create_context(test);
91
92         return test;
93 }
94
95 void test_fini(struct parser_test *test)
96 {
97         device_handler_destroy(test->handler);
98         talloc_free(test);
99 }
100
101 void __test_read_conf_data(struct parser_test *test,
102                 const char *buf, size_t len)
103 {
104         test->conf.size = len;
105         test->conf.buf = talloc_memdup(test, buf, len);
106 }
107
108 void test_read_conf_file(struct parser_test *test, const char *filename)
109 {
110         struct stat stat;
111         char *path;
112         int fd, rc;
113
114         path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
115
116         fd = open(path, O_RDONLY);
117         if (fd < 0)
118                 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
119
120         rc = fstat(fd, &stat);
121         assert(!rc);
122         (void)rc;
123
124         test->conf.size = stat.st_size;
125         test->conf.buf = talloc_array(test, char, test->conf.size + 1);
126
127         rc = read(fd, test->conf.buf, test->conf.size);
128         assert(rc == (ssize_t)test->conf.size);
129
130         *(char *)(test->conf.buf + test->conf.size) = '\0';
131
132         close(fd);
133         talloc_free(path);
134 }
135
136 void test_set_conf_source(struct parser_test *test, const char *url)
137 {
138         test->ctx->conf_url = pb_url_parse(test, url);
139         assert(test->ctx->conf_url);
140 }
141
142 int test_run_parser(struct parser_test *test, const char *parser_name)
143 {
144         struct p_item* i;
145
146         list_for_each_entry(&parsers, i, list) {
147                 if (strcmp(i->parser->name, parser_name))
148                         continue;
149                 test->ctx->parser = i->parser;
150                 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
151         }
152
153         errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
154 }
155
156 bool resource_resolve(struct device_handler *handler, struct parser *parser,
157                 struct resource *resource)
158 {
159         if (!resource)
160                 return true;
161         if (resource->resolved)
162                 return true;
163
164         assert(parser);
165         assert(parser->resolve_resource);
166
167         return parser->resolve_resource(handler, resource);
168 }
169
170 void boot_option_resolve(struct device_handler *handler,
171                 struct discover_boot_option *opt)
172 {
173         resource_resolve(handler, opt->source, opt->boot_image);
174         resource_resolve(handler, opt->source, opt->initrd);
175         resource_resolve(handler, opt->source, opt->icon);
176 }
177
178 extern void device_handler_add_device(struct device_handler *handler,
179                 struct discover_device *dev);
180
181 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
182 {
183         struct discover_boot_option *opt;
184
185         device_handler_add_device(test->handler, dev);
186
187         list_for_each_entry(&test->ctx->boot_options, opt, list)
188                 boot_option_resolve(test->handler, opt);
189 }
190
191 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
192                 int idx)
193 {
194         struct discover_boot_option *opt;
195         int i = 0;
196
197         list_for_each_entry(&ctx->boot_options, opt, list) {
198                 if (i++ == idx)
199                         return opt;
200         }
201
202         assert(0);
203
204         return NULL;
205 }
206
207 void __check_boot_option_count(struct discover_context *ctx, int count,
208                 const char *file, int line)
209 {
210         struct discover_boot_option *opt;
211         int i = 0;
212
213         list_for_each_entry(&ctx->boot_options, opt, list)
214                 i++;
215
216         if (i == count)
217                 return;
218
219         fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
220         fprintf(stderr, "expected %d options, got %d:\n", count, i);
221
222         i = 1;
223         list_for_each_entry(&ctx->boot_options, opt, list)
224                 fprintf(stderr, "  %2d: %s [%s]\n", i++, opt->option->name,
225                                 opt->option->id);
226
227         exit(EXIT_FAILURE);
228 }
229
230 void __check_args(struct discover_boot_option *opt, const char *args,
231                 const char *file, int line)
232 {
233         int rc;
234
235         if (!opt->option->boot_args) {
236                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
237                 fprintf(stderr, "  no arguments parsed\n");
238                 fprintf(stderr, "  expected '%s'\n", args);
239                 exit(EXIT_FAILURE);
240         }
241
242         rc = strcmp(opt->option->boot_args, args);
243         if (rc) {
244                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
245                 fprintf(stderr, "  got      '%s'\n", opt->option->boot_args);
246                 fprintf(stderr, "  expected '%s'\n", args);
247                 exit(EXIT_FAILURE);
248         }
249 }
250
251 void __check_name(struct discover_boot_option *opt, const char *name,
252                 const char *file, int line)
253 {
254         int rc;
255
256         rc = strcmp(opt->option->name, name);
257         if (rc) {
258                 fprintf(stderr, "%s:%d: name check failed\n", file, line);
259                 fprintf(stderr, "  got      '%s'\n", opt->option->name);
260                 fprintf(stderr, "  expected '%s'\n", name);
261                 exit(EXIT_FAILURE);
262         }
263 }
264
265 void __check_resolved_local_resource(struct resource *res,
266                 struct discover_device *dev, const char *local_path,
267                 const char *file, int line)
268 {
269         const char *exp_url, *got_url;
270
271         if (!res)
272                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
273
274         if (!res->resolved)
275                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
276                                 file, line);
277
278         exp_url = talloc_asprintf(res, "file://%s%s",
279                         dev->mount_path, local_path);
280         got_url = pb_url_to_string(res->url);
281
282         if (strcmp(got_url, exp_url)) {
283                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
284                 fprintf(stderr, "  got      '%s'\n", got_url);
285                 fprintf(stderr, "  expected '%s'\n", exp_url);
286                 exit(EXIT_FAILURE);
287         }
288 }
289
290 void __check_resolved_url_resource(struct resource *res,
291                 const char *url, const char *file, int line)
292 {
293         char *res_url;
294
295         if (!res)
296                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
297
298         if (!res->resolved)
299                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
300                                 file, line);
301
302         res_url = pb_url_to_string(res->url);
303         if (strcmp(url, res_url)) {
304                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
305                 fprintf(stderr, "  got      '%s'\n", res_url);
306                 fprintf(stderr, "  expected '%s'\n", url);
307                 exit(EXIT_FAILURE);
308         }
309 }
310 void __check_unresolved_resource(struct resource *res,
311                 const char *file, int line)
312 {
313         if (!res)
314                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
315
316         if (res->resolved)
317                 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
318 }
319
320 void __check_not_present_resource(struct resource *res,
321                 const char *file, int line)
322 {
323         if (res)
324                 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
325 }