]> git.ozlabs.org Git - petitboot/blob - test/parser/utils.c
083d916b082fd4cda6ba3af8a59b370ffa9d5b0c
[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 <ctype.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10
11 #include <talloc/talloc.h>
12 #include <types/types.h>
13 #include <url/url.h>
14
15 #include "device-handler.h"
16 #include "parser.h"
17 #include "resource.h"
18
19 #include "parser-test.h"
20
21 struct p_item {
22         struct list_item list;
23         struct parser *parser;
24 };
25
26 struct test_file {
27         struct discover_device  *dev;
28         const char              *name;
29         void                    *data;
30         int                     size;
31         struct list_item        list;
32 };
33
34 STATIC_LIST(parsers);
35
36 void __register_parser(struct parser *parser)
37 {
38         struct p_item* i = talloc(NULL, struct p_item);
39
40         i->parser = parser;
41         list_add(&parsers, &i->list);
42 }
43
44 static void __attribute__((destructor)) __cleanup_parsers(void)
45 {
46         struct p_item *item, *tmp;
47
48         list_for_each_entry_safe(&parsers, item, tmp, list)
49                 talloc_free(item);
50 }
51
52 static struct discover_device *test_create_device_simple(
53                 struct parser_test *test)
54 {
55         static int dev_idx;
56         char name[10];
57
58         sprintf(name, "__test%d", dev_idx++);
59
60         return test_create_device(test, name);
61 }
62
63 struct discover_device *test_create_device(struct parser_test *test,
64                 const char *name)
65 {
66         struct discover_device *dev;
67
68         dev = discover_device_create(test->handler, name);
69
70         dev->device->id = talloc_strdup(dev, name);
71         dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
72         dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
73         dev->mounted = true;
74
75         return dev;
76 }
77
78 static struct discover_context *test_create_context(struct parser_test *test)
79 {
80         struct discover_context *ctx;
81
82         ctx = talloc_zero(test, struct discover_context);
83         assert(ctx);
84
85         list_init(&ctx->boot_options);
86         ctx->device = test_create_device_simple(test);
87         ctx->test_data = test;
88         device_handler_add_device(test->handler, ctx->device);
89
90         return ctx;
91 }
92
93 extern struct config *test_config_init(struct parser_test *test);
94
95 struct parser_test *test_init(void)
96 {
97         struct parser_test *test;
98
99         test = talloc_zero(NULL, struct parser_test);
100         test->config = test_config_init(test);
101         test->handler = device_handler_init(NULL, NULL, 0);
102         test->ctx = test_create_context(test);
103         list_init(&test->files);
104
105         return test;
106 }
107
108 void test_fini(struct parser_test *test)
109 {
110         device_handler_destroy(test->handler);
111         talloc_free(test);
112 }
113
114 void __test_read_conf_data(struct parser_test *test,
115                 const char *buf, size_t len)
116 {
117         test->conf.size = len;
118         test->conf.buf = talloc_memdup(test, buf, len);
119 }
120
121 void test_read_conf_file(struct parser_test *test, const char *filename)
122 {
123         struct stat stat;
124         char *path;
125         int fd, rc;
126
127         path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
128
129         fd = open(path, O_RDONLY);
130         if (fd < 0)
131                 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
132
133         rc = fstat(fd, &stat);
134         assert(!rc);
135         (void)rc;
136
137         test->conf.size = stat.st_size;
138         test->conf.buf = talloc_array(test, char, test->conf.size + 1);
139
140         rc = read(fd, test->conf.buf, test->conf.size);
141         assert(rc == (ssize_t)test->conf.size);
142
143         *(char *)(test->conf.buf + test->conf.size) = '\0';
144
145         close(fd);
146         talloc_free(path);
147 }
148
149 void test_set_conf_source(struct parser_test *test, const char *url)
150 {
151         test->ctx->conf_url = pb_url_parse(test, url);
152         assert(test->ctx->conf_url);
153 }
154
155 void test_add_file_data(struct parser_test *test, struct discover_device *dev,
156                 const char *filename, const void *data, int size)
157 {
158         struct test_file *file;
159
160         file = talloc_zero(test, struct test_file);
161         file->dev = dev;
162         file->name = filename;
163         file->data = talloc_memdup(test, data, size);
164         file->size = size;
165         list_add(&test->files, &file->list);
166 }
167
168
169 int parser_request_file(struct discover_context *ctx,
170                 struct discover_device *dev, const char *filename,
171                 char **buf, int *len)
172 {
173         struct parser_test *test = ctx->test_data;
174         struct test_file *file;
175         char *tmp;
176
177         list_for_each_entry(&test->files, file, list) {
178                 if (file->dev != dev)
179                         continue;
180                 if (strcmp(file->name, filename))
181                         continue;
182
183                 /* the read_file() interface always adds a trailing null
184                  * for string-safety; do the same here */
185                 tmp = talloc_array(test, char, file->size + 1);
186                 memcpy(tmp, file->data, file->size);
187                 tmp[file->size] = '\0';
188                 *buf = tmp;
189                 *len = file->size;
190                 return 0;
191         }
192
193         return -1;
194 }
195
196 int parser_replace_file(struct discover_context *ctx,
197                 struct discover_device *dev, const char *filename,
198                 char *buf, int len)
199 {
200         struct parser_test *test = ctx->test_data;
201         struct test_file *f, *file;
202
203         list_for_each_entry(&test->files, f, list) {
204                 if (f->dev != dev)
205                         continue;
206                 if (strcmp(f->name, filename))
207                         continue;
208
209                 file = f;
210                 break;
211         }
212
213         if (!file) {
214                 file = talloc_zero(test, struct test_file);
215                 file->dev = dev;
216                 file->name = filename;
217                 list_add(&test->files, &file->list);
218         }
219
220         file->data = talloc_memdup(test, buf, len);
221         file->size = len;
222         return 0;
223 }
224 int test_run_parser(struct parser_test *test, const char *parser_name)
225 {
226         struct p_item* i;
227
228         list_for_each_entry(&parsers, i, list) {
229                 if (strcmp(i->parser->name, parser_name))
230                         continue;
231                 test->ctx->parser = i->parser;
232                 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
233         }
234
235         errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
236 }
237
238 bool resource_resolve(struct device_handler *handler, struct parser *parser,
239                 struct resource *resource)
240 {
241         if (!resource)
242                 return true;
243         if (resource->resolved)
244                 return true;
245
246         assert(parser);
247         assert(parser->resolve_resource);
248
249         return parser->resolve_resource(handler, resource);
250 }
251
252 void boot_option_resolve(struct device_handler *handler,
253                 struct discover_boot_option *opt)
254 {
255         resource_resolve(handler, opt->source, opt->boot_image);
256         resource_resolve(handler, opt->source, opt->initrd);
257         resource_resolve(handler, opt->source, opt->icon);
258 }
259
260 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
261 {
262         struct discover_boot_option *opt;
263
264         device_handler_add_device(test->handler, dev);
265
266         list_for_each_entry(&test->ctx->boot_options, opt, list)
267                 boot_option_resolve(test->handler, opt);
268 }
269
270 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
271                 int idx)
272 {
273         struct discover_boot_option *opt;
274         int i = 0;
275
276         list_for_each_entry(&ctx->boot_options, opt, list) {
277                 if (i++ == idx)
278                         return opt;
279         }
280
281         assert(0);
282
283         return NULL;
284 }
285
286 void __check_boot_option_count(struct discover_context *ctx, int count,
287                 const char *file, int line)
288 {
289         struct discover_boot_option *opt;
290         int defaults = 0, i = 0;
291
292         list_for_each_entry(&ctx->boot_options, opt, list) {
293                 i++;
294                 if (opt->option->is_default)
295                         defaults++;
296         }
297
298         if (defaults > 1) {
299                 fprintf(stderr, "%s:%d: parser returned multiple default "
300                                 "options\n", file, line);
301                 exit(EXIT_FAILURE);
302         }
303
304         if (i == count)
305                 return;
306
307         fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
308         fprintf(stderr, "expected %d options, got %d:\n", count, i);
309
310         i = 1;
311         list_for_each_entry(&ctx->boot_options, opt, list)
312                 fprintf(stderr, "  %2d: %s [%s]\n", i++, opt->option->name,
313                                 opt->option->id);
314
315         exit(EXIT_FAILURE);
316 }
317
318 void __check_args(struct discover_boot_option *opt, const char *args,
319                 const char *file, int line)
320 {
321         int rc;
322
323         if (!opt->option->boot_args && !args)
324                 return;
325
326         if (!opt->option->boot_args) {
327                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
328                 fprintf(stderr, "  no arguments parsed\n");
329                 fprintf(stderr, "  expected '%s'\n", args);
330                 exit(EXIT_FAILURE);
331         }
332
333         rc = strcmp(opt->option->boot_args, args);
334         if (rc) {
335                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
336                 fprintf(stderr, "  got      '%s'\n", opt->option->boot_args);
337                 fprintf(stderr, "  expected '%s'\n", args);
338                 exit(EXIT_FAILURE);
339         }
340 }
341
342 void __check_name(struct discover_boot_option *opt, const char *name,
343                 const char *file, int line)
344 {
345         int rc;
346
347         rc = strcmp(opt->option->name, name);
348         if (rc) {
349                 fprintf(stderr, "%s:%d: name check failed\n", file, line);
350                 fprintf(stderr, "  got      '%s'\n", opt->option->name);
351                 fprintf(stderr, "  expected '%s'\n", name);
352                 exit(EXIT_FAILURE);
353         }
354 }
355
356 void __check_is_default(struct discover_boot_option *opt,
357                 const char *file, int line)
358 {
359         if (opt->option->is_default)
360                 return;
361
362         fprintf(stderr, "%s:%d: default check failed\n", file, line);
363         exit(EXIT_FAILURE);
364 }
365
366 void __check_resolved_local_resource(struct resource *res,
367                 struct discover_device *dev, const char *local_path,
368                 const char *file, int line)
369 {
370         const char *exp_url, *got_url;
371
372         if (!res)
373                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
374
375         if (!res->resolved)
376                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
377                                 file, line);
378
379         exp_url = talloc_asprintf(res, "file://%s%s",
380                         dev->mount_path, local_path);
381         got_url = pb_url_to_string(res->url);
382
383         if (strcmp(got_url, exp_url)) {
384                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
385                 fprintf(stderr, "  got      '%s'\n", got_url);
386                 fprintf(stderr, "  expected '%s'\n", exp_url);
387                 exit(EXIT_FAILURE);
388         }
389 }
390
391 void __check_resolved_url_resource(struct resource *res,
392                 const char *url, const char *file, int line)
393 {
394         char *res_url;
395
396         if (!res)
397                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
398
399         if (!res->resolved)
400                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
401                                 file, line);
402
403         res_url = pb_url_to_string(res->url);
404         if (strcmp(url, res_url)) {
405                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
406                 fprintf(stderr, "  got      '%s'\n", res_url);
407                 fprintf(stderr, "  expected '%s'\n", url);
408                 exit(EXIT_FAILURE);
409         }
410 }
411 void __check_unresolved_resource(struct resource *res,
412                 const char *file, int line)
413 {
414         if (!res)
415                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
416
417         if (res->resolved)
418                 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
419 }
420
421 void __check_not_present_resource(struct resource *res,
422                 const char *file, int line)
423 {
424         if (res)
425                 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
426 }
427
428 static void dump_file_data(const void *buf, int len)
429 {
430         int i, j, hex_len = strlen("00 ");
431         const int row_len = 16;
432
433         for (i = 0; i < len; i += row_len) {
434                 char hbuf[row_len * hex_len + 1];
435                 char cbuf[row_len + strlen("|") + 1];
436
437                 for (j = 0; (j < row_len) && ((i+j) < len); j++) {
438                         char c = ((const char *)buf)[i + j];
439
440                         snprintf(hbuf + j * hex_len, hex_len + 1, "%02x ", c);
441
442                         if (!isprint(c))
443                                 c = '.';
444
445                         snprintf(cbuf + j, hex_len + 1, "%c", c);
446                 }
447
448                 strcat(cbuf, "|");
449
450                 fprintf(stderr, "%08x  %*s |%s\n", i,
451                                 0 - (int)sizeof(hbuf) + 1, hbuf, cbuf);
452         }
453 }
454
455 void __check_file_contents(struct parser_test *test,
456                 struct discover_device *dev, const char *filename,
457                 const char *buf, int len,
458                 const char *srcfile, int srcline)
459 {
460         struct test_file *f, *file = NULL;
461
462         list_for_each_entry(&test->files, f, list) {
463                 if (f->dev != dev)
464                         continue;
465                 if (strcmp(f->name, filename))
466                         continue;
467
468                 file = f;
469                 break;
470         }
471
472         if (!file)
473                 errx(EXIT_FAILURE, "%s:%d: File '%s' not found",
474                                 srcfile, srcline, filename);
475
476         if (file->size != len || memcmp(file->data, buf, len)) {
477                 fprintf(stderr, "%s:%d: File '%s' data/size mismatch\n",
478                                 srcfile, srcline, filename);
479                 fprintf(stderr, "Expected:\n");
480                 dump_file_data(buf, len);
481                 fprintf(stderr, "Got:\n");
482                 dump_file_data(file->data, file->size);
483                 exit(EXIT_FAILURE);
484         }
485 }