11 #include <talloc/talloc.h>
12 #include <types/types.h>
15 #include "device-handler.h"
21 #include "parser-conf.h"
23 #include "parser-test.h"
26 struct list_item list;
27 struct parser *parser;
31 struct discover_device *dev;
39 struct list_item list;
44 void __register_parser(struct parser *parser)
46 struct p_item* i = talloc(NULL, struct p_item);
49 list_add(&parsers, &i->list);
52 static void __attribute__((destructor)) __cleanup_parsers(void)
54 struct p_item *item, *tmp;
56 list_for_each_entry_safe(&parsers, item, tmp, list)
60 static struct discover_device *test_create_device_simple(
61 struct parser_test *test)
66 sprintf(name, "__test%d", dev_idx++);
68 return test_create_device(test, name);
71 struct discover_device *test_create_device(struct parser_test *test,
74 struct discover_device *dev;
76 dev = discover_device_create(test->handler, NULL, name);
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);
86 static struct discover_context *test_create_context(struct parser_test *test)
88 struct discover_context *ctx;
90 ctx = talloc_zero(test, struct discover_context);
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);
102 /* define our own test platform */
103 static bool test_platform_probe(struct platform *p __attribute__((unused)),
104 void *ctx __attribute__((unused)))
109 struct platform test_platform = {
111 .probe = test_platform_probe,
114 register_platform(test_platform);
116 struct parser_test *test_init(void)
118 struct parser_test *test;
120 test = talloc_zero(NULL, struct parser_test);
122 test->handler = device_handler_init(NULL, NULL, 0);
123 test->ctx = test_create_context(test);
124 list_init(&test->files);
129 void test_fini(struct parser_test *test)
131 device_handler_destroy(test->handler);
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)
140 test_add_file_data(test, dev, conf_file, buf, len);
143 void test_read_conf_file(struct parser_test *test, const char *filename,
144 const char *conf_file)
152 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
154 fd = open(path, O_RDONLY);
156 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
158 rc = fstat(fd, &stat);
163 buf = talloc_array(test, char, size + 1);
165 rc = read(fd, buf, size);
166 assert(rc == (ssize_t)size);
168 *(buf + size) = '\0';
173 test_add_file_data(test, test->ctx->device, conf_file, buf, size);
176 void test_add_file_data(struct parser_test *test, struct discover_device *dev,
177 const char *filename, const void *data, int size)
179 struct test_file *file;
181 file = talloc_zero(test, struct test_file);
182 file->type = TEST_FILE;
184 file->name = filename;
185 file->data = talloc_memdup(test, data, size);
187 list_add(&test->files, &file->list);
190 void test_add_dir(struct parser_test *test, struct discover_device *dev,
193 struct test_file *file;
195 file = talloc_zero(test, struct test_file);
196 file->type = TEST_DIR;
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. */
202 list_add(&test->files, &file->list);
205 void test_set_event_source(struct parser_test *test)
207 test->ctx->event = talloc_zero(test->ctx, struct event);
210 void test_set_event_param(struct event *event, const char *name,
213 event_set_param(event, name, value);
216 void test_set_event_device(struct event *event, const char *dev)
218 event->device = talloc_strdup(event, dev);
221 int parser_request_file(struct discover_context *ctx,
222 struct discover_device *dev, const char *filename,
223 char **buf, int *len)
225 struct parser_test *test = ctx->test_data;
226 struct test_file *file;
229 list_for_each_entry(&test->files, file, list) {
230 if (file->dev != dev)
232 if (strcmp(file->name, filename))
234 if (file->type != TEST_FILE)
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';
250 int parser_stat_path(struct discover_context *ctx,
251 struct discover_device *dev, const char *path,
252 struct stat *statbuf)
254 struct parser_test *test = ctx->test_data;
255 struct test_file *file;
257 list_for_each_entry(&test->files, file, list) {
258 if (file->dev != dev)
260 if (strcmp(file->name, path))
263 statbuf->st_size = (off_t)file->size;
264 switch (file->type) {
266 statbuf->st_mode = S_IFREG;
269 statbuf->st_mode = S_IFDIR;
272 fprintf(stderr, "%s: bad test file mode %d!", __func__,
283 int parser_replace_file(struct discover_context *ctx,
284 struct discover_device *dev, const char *filename,
287 struct parser_test *test = ctx->test_data;
288 struct test_file *f, *file = NULL;
290 list_for_each_entry(&test->files, f, list) {
293 if (strcmp(f->name, filename))
301 file = talloc_zero(test, struct test_file);
303 file->name = filename;
304 list_add(&test->files, &file->list);
307 file->data = talloc_memdup(test, buf, len);
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)))
318 struct parser_test *test = ctx->test_data;
321 struct dirent **dirents = NULL, **new_dirents;
324 list_for_each_entry(&test->files, f, list) {
325 if (f->dev != ctx->device)
328 filename = strrchr(f->name, '/');
332 namelen = strlen(filename);
334 if (strncmp(f->name, dirname, strlen(f->name) - namelen))
338 dirents = malloc(sizeof(struct dirent *));
340 new_dirents = realloc(dirents, sizeof(struct dirent *)
345 dirents = new_dirents;
348 dirents[n] = malloc(sizeof(struct dirent) + namelen + 1);
353 strcpy(dirents[n]->d_name, filename + 1);
371 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
372 load_url_complete async_cb, void *async_data,
373 waiter_cb stdout_cb, void *stdout_data)
375 struct conf_context *conf = async_data;
376 struct parser_test *test = conf->dc->test_data;
377 struct load_url_result *result;
378 char tmp[] = "/tmp/pb-XXXXXX";
379 ssize_t rc = -1, sz = 0;
380 struct test_file *file;
383 /* Ignore the stdout callback for tests */
392 /* Some parsers will expect to need to read a file, so write the
393 * specified file to a temporary file */
394 list_for_each_entry(&test->files, file, list) {
398 if (strcmp(file->name, url->full))
401 while (sz < file->size) {
402 rc = write(fd, file->data, file->size);
405 "Failed to write to tmpfile, %m\n");
415 result = talloc_zero(ctx, struct load_url_result);
419 result->local = talloc_strdup(result, tmp);
422 result->status = LOAD_ERROR;
424 result->status = result->local ? LOAD_OK : LOAD_ERROR;
425 result->cleanup_local = true;
427 async_cb(result, conf);
432 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
433 char **buf, int *len)
435 struct parser_test *test = ctx->test_data;
436 struct test_file *file;
439 list_for_each_entry(&test->files, file, list) {
443 if (strcmp(file->name, url->full))
446 /* the read_file() interface always adds a trailing null
447 * for string-safety; do the same here */
448 tmp = talloc_array(test, char, file->size + 1);
449 memcpy(tmp, file->data, file->size);
450 tmp[file->size] = '\0';
459 int test_run_parser(struct parser_test *test, const char *parser_name)
463 list_for_each_entry(&parsers, i, list) {
464 if (strcmp(i->parser->name, parser_name))
466 test->ctx->parser = i->parser;
467 return i->parser->parse(test->ctx);
470 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
473 bool resource_resolve(struct device_handler *handler, struct parser *parser,
474 struct resource *resource)
478 if (resource->resolved)
482 assert(parser->resolve_resource);
484 return parser->resolve_resource(handler, resource);
487 void boot_option_resolve(struct device_handler *handler,
488 struct discover_boot_option *opt)
490 resource_resolve(handler, opt->source, opt->boot_image);
491 resource_resolve(handler, opt->source, opt->initrd);
492 resource_resolve(handler, opt->source, opt->icon);
495 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
497 struct discover_boot_option *opt;
499 device_handler_add_device(test->handler, dev);
501 list_for_each_entry(&test->ctx->boot_options, opt, list)
502 boot_option_resolve(test->handler, opt);
505 void test_remove_device(struct parser_test *test, struct discover_device *dev)
507 struct discover_boot_option *opt, *tmp;
509 if (dev == test->ctx->device) {
510 list_for_each_entry_safe(&test->ctx->boot_options,
512 list_remove(&opt->list);
517 device_handler_remove(test->handler, dev);
520 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
523 struct discover_boot_option *opt;
526 list_for_each_entry(&ctx->boot_options, opt, list) {
536 void __check_boot_option_count(struct discover_context *ctx, int count,
537 const char *file, int line)
539 struct discover_boot_option *opt;
540 int defaults = 0, i = 0;
542 list_for_each_entry(&ctx->boot_options, opt, list) {
544 if (opt->option->is_default)
549 fprintf(stderr, "%s:%d: parser returned multiple default "
550 "options\n", file, line);
557 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
558 fprintf(stderr, "expected %d options, got %d:\n", count, i);
561 list_for_each_entry(&ctx->boot_options, opt, list)
562 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
568 void __check_args(struct discover_boot_option *opt, const char *args,
569 const char *file, int line)
573 if (!opt->option->boot_args && !args)
576 if (!opt->option->boot_args) {
577 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
578 fprintf(stderr, " no arguments parsed\n");
579 fprintf(stderr, " expected '%s'\n", args);
583 rc = strcmp(opt->option->boot_args, args);
585 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
586 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
587 fprintf(stderr, " expected '%s'\n", args);
592 void __check_name(struct discover_boot_option *opt, const char *name,
593 const char *file, int line)
597 rc = strcmp(opt->option->name, name);
599 fprintf(stderr, "%s:%d: name check failed\n", file, line);
600 fprintf(stderr, " got '%s'\n", opt->option->name);
601 fprintf(stderr, " expected '%s'\n", name);
606 void __check_is_default(struct discover_boot_option *opt,
607 const char *file, int line)
609 if (opt->option->is_default)
612 fprintf(stderr, "%s:%d: default check failed\n", file, line);
616 void __check_resolved_local_resource(struct resource *res,
617 struct discover_device *dev, const char *local_path,
618 const char *file, int line)
620 const char *exp_url, *got_url;
623 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
626 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
629 exp_url = talloc_asprintf(res, "file://%s%s",
630 dev->mount_path, local_path);
631 got_url = pb_url_to_string(res->url);
633 if (strcmp(got_url, exp_url)) {
634 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
635 fprintf(stderr, " got '%s'\n", got_url);
636 fprintf(stderr, " expected '%s'\n", exp_url);
641 void __check_resolved_url_resource(struct resource *res,
642 const char *url, const char *file, int line)
647 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
650 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
653 res_url = pb_url_to_string(res->url);
654 if (strcmp(url, res_url)) {
655 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
656 fprintf(stderr, " got '%s'\n", res_url);
657 fprintf(stderr, " expected '%s'\n", url);
661 void __check_unresolved_resource(struct resource *res,
662 const char *file, int line)
665 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
668 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
671 void __check_not_present_resource(struct resource *res,
672 const char *file, int line)
675 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
678 static void dump_file_data(const void *buf, int len)
680 int i, j, hex_len = strlen("00 ");
681 const int row_len = 16;
683 for (i = 0; i < len; i += row_len) {
684 char hbuf[row_len * hex_len + 1];
685 char cbuf[row_len + strlen("|") + 1];
687 for (j = 0; (j < row_len) && ((i+j) < len); j++) {
688 char c = ((const char *)buf)[i + j];
690 snprintf(hbuf + j * hex_len, hex_len + 1, "%02x ", c);
695 snprintf(cbuf + j, hex_len + 1, "%c", c);
700 fprintf(stderr, "%08x %*s |%s\n", i,
701 0 - (int)sizeof(hbuf) + 1, hbuf, cbuf);
705 void __check_file_contents(struct parser_test *test,
706 struct discover_device *dev, const char *filename,
707 const char *buf, int len,
708 const char *srcfile, int srcline)
710 struct test_file *f, *file = NULL;
712 list_for_each_entry(&test->files, f, list) {
715 if (strcmp(f->name, filename))
723 errx(EXIT_FAILURE, "%s:%d: File '%s' not found",
724 srcfile, srcline, filename);
726 if (file->size != len || memcmp(file->data, buf, len)) {
727 fprintf(stderr, "%s:%d: File '%s' data/size mismatch\n",
728 srcfile, srcline, filename);
729 fprintf(stderr, "Expected:\n");
730 dump_file_data(buf, len);
731 fprintf(stderr, "Got:\n");
732 dump_file_data(file->data, file->size);