]> git.ozlabs.org Git - petitboot/blob - test/parser/utils.c
ui/ncurses: Implement non-boot-cancelling keys
[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 #include "event.h"
19 #include "platform.h"
20 #include "paths.h"
21 #include "parser-conf.h"
22
23 #include "parser-test.h"
24
25 struct p_item {
26         struct list_item list;
27         struct parser *parser;
28 };
29
30 struct test_file {
31         struct discover_device  *dev;
32         enum {
33                 TEST_FILE,
34                 TEST_DIR,
35         }                       type;
36         const char              *name;
37         void                    *data;
38         int                     size;
39         struct list_item        list;
40 };
41
42 STATIC_LIST(parsers);
43
44 void __register_parser(struct parser *parser)
45 {
46         struct p_item* i = talloc(NULL, struct p_item);
47
48         i->parser = parser;
49         list_add(&parsers, &i->list);
50 }
51
52 static void __attribute__((destructor)) __cleanup_parsers(void)
53 {
54         struct p_item *item, *tmp;
55
56         list_for_each_entry_safe(&parsers, item, tmp, list)
57                 talloc_free(item);
58 }
59
60 static struct discover_device *test_create_device_simple(
61                 struct parser_test *test)
62 {
63         static int dev_idx;
64         char name[10];
65
66         sprintf(name, "__test%d", dev_idx++);
67
68         return test_create_device(test, name);
69 }
70
71 struct discover_device *test_create_device(struct parser_test *test,
72                 const char *name)
73 {
74         struct discover_device *dev;
75
76         dev = discover_device_create(test->handler, NULL, name);
77
78         dev->device->id = talloc_strdup(dev, name);
79         dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
80         dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
81         dev->mounted = true;
82
83         return dev;
84 }
85
86 static struct discover_context *test_create_context(struct parser_test *test)
87 {
88         struct discover_context *ctx;
89
90         ctx = talloc_zero(test, struct discover_context);
91         assert(ctx);
92
93         list_init(&ctx->boot_options);
94         ctx->device = test_create_device_simple(test);
95         ctx->test_data = test;
96         ctx->handler = test->handler;
97         device_handler_add_device(test->handler, ctx->device);
98
99         return ctx;
100 }
101
102 /* define our own test platform */
103 static bool test_platform_probe(struct platform *p __attribute__((unused)),
104                 void *ctx __attribute__((unused)))
105 {
106         return true;
107 }
108
109 struct platform test_platform = {
110         .name = "test",
111         .probe = test_platform_probe,
112 };
113
114 register_platform(test_platform);
115
116 struct parser_test *test_init(void)
117 {
118         struct parser_test *test;
119
120         test = talloc_zero(NULL, struct parser_test);
121         platform_init(NULL);
122         test->handler = device_handler_init(NULL, NULL, 0);
123         test->ctx = test_create_context(test);
124         list_init(&test->files);
125
126         return test;
127 }
128
129 void test_fini(struct parser_test *test)
130 {
131         device_handler_destroy(test->handler);
132         talloc_free(test);
133         platform_fini();
134 }
135
136 void __test_read_conf_data(struct parser_test *test,
137                 struct discover_device *dev, const char *conf_file,
138                 const char *buf, size_t len)
139 {
140         test_add_file_data(test, dev, conf_file, buf, len);
141 }
142
143 void test_read_conf_file(struct parser_test *test, const char *filename,
144                 const char *conf_file)
145 {
146         struct stat stat;
147         size_t size;
148         char *path;
149         int fd, rc;
150         char *buf;
151
152         path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
153
154         fd = open(path, O_RDONLY);
155         if (fd < 0)
156                 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
157
158         rc = fstat(fd, &stat);
159         assert(!rc);
160         (void)rc;
161
162         size = stat.st_size;
163         buf = talloc_array(test, char, size + 1);
164
165         rc = read(fd, buf, size);
166         assert(rc == (ssize_t)size);
167
168         *(buf + size) = '\0';
169
170         close(fd);
171         talloc_free(path);
172
173         test_add_file_data(test, test->ctx->device, conf_file, buf, size);
174 }
175
176 void test_add_file_data(struct parser_test *test, struct discover_device *dev,
177                 const char *filename, const void *data, int size)
178 {
179         struct test_file *file;
180
181         file = talloc_zero(test, struct test_file);
182         file->type = TEST_FILE;
183         file->dev = dev;
184         file->name = filename;
185         file->data = talloc_memdup(test, data, size);
186         file->size = size;
187         list_add(&test->files, &file->list);
188 }
189
190 void test_add_dir(struct parser_test *test, struct discover_device *dev,
191                 const char *dirname)
192 {
193         struct test_file *file;
194
195         file = talloc_zero(test, struct test_file);
196         file->type = TEST_DIR;
197         file->dev = dev;
198         file->name = dirname;
199         /* Pick a non-zero size for directories so that "[ -s <dir
200          * path> ]" sees that the file has non-zero size. */
201         file->size = 1;
202         list_add(&test->files, &file->list);
203 }
204
205 void test_set_event_source(struct parser_test *test)
206 {
207         test->ctx->event = talloc_zero(test->ctx, struct event);
208 }
209
210 void test_set_event_param(struct event *event, const char *name,
211                 const char *value)
212 {
213         event_set_param(event, name, value);
214 }
215
216 void test_set_event_device(struct event *event, const char *dev)
217 {
218         event->device = talloc_strdup(event, dev);
219 }
220
221 int parser_request_file(struct discover_context *ctx,
222                 struct discover_device *dev, const char *filename,
223                 char **buf, int *len)
224 {
225         struct parser_test *test = ctx->test_data;
226         struct test_file *file;
227         char *tmp;
228
229         list_for_each_entry(&test->files, file, list) {
230                 if (file->dev != dev)
231                         continue;
232                 if (strcmp(file->name, filename))
233                         continue;
234                 if (file->type != TEST_FILE)
235                         continue;
236
237                 /* the read_file() interface always adds a trailing null
238                  * for string-safety; do the same here */
239                 tmp = talloc_array(test, char, file->size + 1);
240                 memcpy(tmp, file->data, file->size);
241                 tmp[file->size] = '\0';
242                 *buf = tmp;
243                 *len = file->size;
244                 return 0;
245         }
246
247         return -1;
248 }
249
250 int parser_stat_path(struct discover_context *ctx,
251                 struct discover_device *dev, const char *path,
252                 struct stat *statbuf)
253 {
254         struct parser_test *test = ctx->test_data;
255         struct test_file *file;
256
257         list_for_each_entry(&test->files, file, list) {
258                 if (file->dev != dev)
259                         continue;
260                 if (strcmp(file->name, path))
261                         continue;
262
263                 statbuf->st_size = (off_t)file->size;
264                 switch (file->type) {
265                 case TEST_FILE:
266                         statbuf->st_mode = S_IFREG;
267                         break;
268                 case TEST_DIR:
269                         statbuf->st_mode = S_IFDIR;
270                         break;
271                 default:
272                         fprintf(stderr, "%s: bad test file mode %d!", __func__,
273                                 file->type);
274                         exit(EXIT_FAILURE);
275                 }
276
277                 return 0;
278         }
279
280         return -1;
281 }
282
283 int parser_replace_file(struct discover_context *ctx,
284                 struct discover_device *dev, const char *filename,
285                 char *buf, int len)
286 {
287         struct parser_test *test = ctx->test_data;
288         struct test_file *f, *file = NULL;
289
290         list_for_each_entry(&test->files, f, list) {
291                 if (f->dev != dev)
292                         continue;
293                 if (strcmp(f->name, filename))
294                         continue;
295
296                 file = f;
297                 break;
298         }
299
300         if (!file) {
301                 file = talloc_zero(test, struct test_file);
302                 file->dev = dev;
303                 file->name = filename;
304                 list_add(&test->files, &file->list);
305         }
306
307         file->data = talloc_memdup(test, buf, len);
308         file->size = len;
309         return 0;
310 }
311
312 int parser_scandir(struct discover_context *ctx, const char *dirname,
313                    struct dirent ***files, int (*filter)(const struct dirent *)
314                    __attribute__((unused)),
315                    int (*comp)(const struct dirent **, const struct dirent **)
316                    __attribute__((unused)))
317 {
318         struct parser_test *test = ctx->test_data;
319         struct test_file *f;
320         char *filename;
321         struct dirent **dirents = NULL, **new_dirents;
322         int n = 0, namelen;
323
324         list_for_each_entry(&test->files, f, list) {
325                 if (f->dev != ctx->device)
326                         continue;
327
328                 if (strlen(f->name) <= strlen(dirname))
329                         continue;
330
331                 filename = strrchr(f->name, '/');
332                 if (!filename)
333                         continue;
334
335                 namelen = strlen(filename);
336
337                 if (strncmp(f->name, dirname, strlen(f->name) - namelen))
338                         continue;
339
340                 if (!dirents) {
341                         dirents = malloc(sizeof(struct dirent *));
342                 } else {
343                         new_dirents = realloc(dirents, sizeof(struct dirent *)
344                                               * (n + 1));
345                         if (!new_dirents)
346                                 goto err_cleanup;
347
348                         dirents = new_dirents;
349                 }
350
351                 dirents[n] = malloc(sizeof(struct dirent) + namelen + 1);
352
353                 if (!dirents[n])
354                         goto err_cleanup;
355
356                 strcpy(dirents[n]->d_name, filename + 1);
357                 n++;
358         }
359
360         *files = dirents;
361
362         return n;
363
364 err_cleanup:
365         do {
366                 free(dirents[n]);
367         } while (n-- > 0);
368
369         free(dirents);
370
371         return -1;
372 }
373
374 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
375                 load_url_complete async_cb, void *async_data,
376                 waiter_cb stdout_cb, void *stdout_data)
377 {
378         struct conf_context *conf = async_data;
379         struct parser_test *test = conf->dc->test_data;
380         struct load_url_result *result;
381         char tmp[] = "/tmp/pb-XXXXXX";
382         ssize_t rc = -1, sz = 0;
383         struct test_file *file;
384         int fd;
385
386         /* Ignore the stdout callback for tests */
387         (void)stdout_cb;
388         (void)stdout_data;
389
390         fd = mkstemp(tmp);
391
392         if (fd < 0)
393                 return NULL;
394
395         /* Some parsers will expect to need to read a file, so write the
396          * specified file to a temporary file */
397         list_for_each_entry(&test->files, file, list) {
398                 if (file->dev)
399                         continue;
400
401                 if (strcmp(file->name, url->full))
402                         continue;
403
404                 while (sz < file->size) {
405                         rc = write(fd, file->data, file->size);
406                         if (rc < 0) {
407                                 fprintf(stderr,
408                                         "Failed to write to tmpfile, %m\n");
409                                 break;
410                         }
411                         sz += rc;
412                 }
413                 break;
414         }
415
416         close(fd);
417
418         result = talloc_zero(ctx, struct load_url_result);
419         if (!result)
420                 return NULL;
421
422         result->local = talloc_strdup(result, tmp);
423         result->url = url;
424         if (rc < 0)
425                 result->status = LOAD_ERROR;
426         else
427                 result->status = result->local ? LOAD_OK : LOAD_ERROR;
428         result->cleanup_local = true;
429
430         async_cb(result, conf);
431
432         return result;
433 }
434
435 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
436                 char **buf, int *len)
437 {
438         struct parser_test *test = ctx->test_data;
439         struct test_file *file;
440         char *tmp;
441
442         list_for_each_entry(&test->files, file, list) {
443                 if (file->dev)
444                         continue;
445
446                 if (strcmp(file->name, url->full))
447                         continue;
448
449                 /* the read_file() interface always adds a trailing null
450                  * for string-safety; do the same here */
451                 tmp = talloc_array(test, char, file->size + 1);
452                 memcpy(tmp, file->data, file->size);
453                 tmp[file->size] = '\0';
454                 *buf = tmp;
455                 *len = file->size;
456                 return 0;
457         }
458
459         return -1;
460 }
461
462 int test_run_parser(struct parser_test *test, const char *parser_name)
463 {
464         struct p_item* i;
465
466         list_for_each_entry(&parsers, i, list) {
467                 if (strcmp(i->parser->name, parser_name))
468                         continue;
469                 test->ctx->parser = i->parser;
470                 return i->parser->parse(test->ctx);
471         }
472
473         errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
474 }
475
476 bool resource_resolve(struct device_handler *handler, struct parser *parser,
477                 struct resource *resource)
478 {
479         if (!resource)
480                 return true;
481         if (resource->resolved)
482                 return true;
483
484         assert(parser);
485         assert(parser->resolve_resource);
486
487         return parser->resolve_resource(handler, resource);
488 }
489
490 void boot_option_resolve(struct device_handler *handler,
491                 struct discover_boot_option *opt)
492 {
493         resource_resolve(handler, opt->source, opt->boot_image);
494         resource_resolve(handler, opt->source, opt->initrd);
495         resource_resolve(handler, opt->source, opt->icon);
496 }
497
498 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
499 {
500         struct discover_boot_option *opt;
501
502         device_handler_add_device(test->handler, dev);
503
504         list_for_each_entry(&test->ctx->boot_options, opt, list)
505                 boot_option_resolve(test->handler, opt);
506 }
507
508 void test_remove_device(struct parser_test *test, struct discover_device *dev)
509 {
510         struct discover_boot_option *opt, *tmp;
511
512         if (dev == test->ctx->device) {
513                 list_for_each_entry_safe(&test->ctx->boot_options,
514                                 opt, tmp, list) {
515                         list_remove(&opt->list);
516                         talloc_free(opt);
517                 }
518         }
519
520         device_handler_remove(test->handler, dev);
521 }
522
523 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
524                 int idx)
525 {
526         struct discover_boot_option *opt;
527         int i = 0;
528
529         list_for_each_entry(&ctx->boot_options, opt, list) {
530                 if (i++ == idx)
531                         return opt;
532         }
533
534         assert(0);
535
536         return NULL;
537 }
538
539 void __check_boot_option_count(struct discover_context *ctx, int count,
540                 const char *file, int line)
541 {
542         struct discover_boot_option *opt;
543         int defaults = 0, i = 0;
544
545         list_for_each_entry(&ctx->boot_options, opt, list) {
546                 i++;
547                 if (opt->option->is_default)
548                         defaults++;
549         }
550
551         if (defaults > 1) {
552                 fprintf(stderr, "%s:%d: parser returned multiple default "
553                                 "options\n", file, line);
554                 exit(EXIT_FAILURE);
555         }
556
557         if (i == count)
558                 return;
559
560         fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
561         fprintf(stderr, "expected %d options, got %d:\n", count, i);
562
563         i = 1;
564         list_for_each_entry(&ctx->boot_options, opt, list)
565                 fprintf(stderr, "  %2d: %s [%s]\n", i++, opt->option->name,
566                                 opt->option->id);
567
568         exit(EXIT_FAILURE);
569 }
570
571 void __check_args(struct discover_boot_option *opt, const char *args,
572                 const char *file, int line)
573 {
574         int rc;
575
576         if (!opt->option->boot_args && !args)
577                 return;
578
579         if (!opt->option->boot_args) {
580                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
581                 fprintf(stderr, "  no arguments parsed\n");
582                 fprintf(stderr, "  expected '%s'\n", args);
583                 exit(EXIT_FAILURE);
584         }
585
586         rc = strcmp(opt->option->boot_args, args);
587         if (rc) {
588                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
589                 fprintf(stderr, "  got      '%s'\n", opt->option->boot_args);
590                 fprintf(stderr, "  expected '%s'\n", args);
591                 exit(EXIT_FAILURE);
592         }
593 }
594
595 void __check_name(struct discover_boot_option *opt, const char *name,
596                 const char *file, int line)
597 {
598         int rc;
599
600         rc = strcmp(opt->option->name, name);
601         if (rc) {
602                 fprintf(stderr, "%s:%d: name check failed\n", file, line);
603                 fprintf(stderr, "  got      '%s'\n", opt->option->name);
604                 fprintf(stderr, "  expected '%s'\n", name);
605                 exit(EXIT_FAILURE);
606         }
607 }
608
609 void __check_is_default(struct discover_boot_option *opt,
610                 const char *file, int line)
611 {
612         if (opt->option->is_default)
613                 return;
614
615         fprintf(stderr, "%s:%d: default check failed\n", file, line);
616         exit(EXIT_FAILURE);
617 }
618
619 void __check_resolved_local_resource(struct resource *res,
620                 struct discover_device *dev, const char *local_path,
621                 const char *file, int line)
622 {
623         const char *exp_url, *got_url;
624
625         if (!res)
626                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
627
628         if (!res->resolved)
629                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
630                                 file, line);
631
632         exp_url = talloc_asprintf(res, "file://%s%s",
633                         dev->mount_path, local_path);
634         got_url = pb_url_to_string(res->url);
635
636         if (strcmp(got_url, exp_url)) {
637                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
638                 fprintf(stderr, "  got      '%s'\n", got_url);
639                 fprintf(stderr, "  expected '%s'\n", exp_url);
640                 exit(EXIT_FAILURE);
641         }
642 }
643
644 void __check_resolved_url_resource(struct resource *res,
645                 const char *url, const char *file, int line)
646 {
647         char *res_url;
648
649         if (!res)
650                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
651
652         if (!res->resolved)
653                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
654                                 file, line);
655
656         res_url = pb_url_to_string(res->url);
657         if (strcmp(url, res_url)) {
658                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
659                 fprintf(stderr, "  got      '%s'\n", res_url);
660                 fprintf(stderr, "  expected '%s'\n", url);
661                 exit(EXIT_FAILURE);
662         }
663 }
664 void __check_unresolved_resource(struct resource *res,
665                 const char *file, int line)
666 {
667         if (!res)
668                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
669
670         if (res->resolved)
671                 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
672 }
673
674 void __check_not_present_resource(struct resource *res,
675                 const char *file, int line)
676 {
677         if (res)
678                 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
679 }
680
681 static void dump_file_data(const void *buf, int len)
682 {
683         int i, j, hex_len = strlen("00 ");
684         const int row_len = 16;
685
686         for (i = 0; i < len; i += row_len) {
687                 char hbuf[row_len * hex_len + 1];
688                 char cbuf[row_len + strlen("|") + 1];
689
690                 for (j = 0; (j < row_len) && ((i+j) < len); j++) {
691                         char c = ((const char *)buf)[i + j];
692
693                         snprintf(hbuf + j * hex_len, hex_len + 1, "%02x ", c);
694
695                         if (!isprint(c))
696                                 c = '.';
697
698                         snprintf(cbuf + j, hex_len + 1, "%c", c);
699                 }
700
701                 strcat(cbuf, "|");
702
703                 fprintf(stderr, "%08x  %*s |%s\n", i,
704                                 0 - (int)sizeof(hbuf) + 1, hbuf, cbuf);
705         }
706 }
707
708 void __check_file_contents(struct parser_test *test,
709                 struct discover_device *dev, const char *filename,
710                 const char *buf, int len,
711                 const char *srcfile, int srcline)
712 {
713         struct test_file *f, *file = NULL;
714
715         list_for_each_entry(&test->files, f, list) {
716                 if (f->dev != dev)
717                         continue;
718                 if (strcmp(f->name, filename))
719                         continue;
720
721                 file = f;
722                 break;
723         }
724
725         if (!file)
726                 errx(EXIT_FAILURE, "%s:%d: File '%s' not found",
727                                 srcfile, srcline, filename);
728
729         if (file->size != len || memcmp(file->data, buf, len)) {
730                 fprintf(stderr, "%s:%d: File '%s' data/size mismatch\n",
731                                 srcfile, srcline, filename);
732                 fprintf(stderr, "Expected:\n");
733                 dump_file_data(buf, len);
734                 fprintf(stderr, "Got:\n");
735                 dump_file_data(file->data, file->size);
736                 exit(EXIT_FAILURE);
737         }
738 }