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 device_handler_add_device(test->handler, ctx->device);
101 /* define our own test platform */
102 static bool test_platform_probe(struct platform *p __attribute__((unused)),
103 void *ctx __attribute__((unused)))
108 struct platform test_platform = {
110 .probe = test_platform_probe,
113 register_platform(test_platform);
115 struct parser_test *test_init(void)
117 struct parser_test *test;
119 test = talloc_zero(NULL, struct parser_test);
121 test->handler = device_handler_init(NULL, NULL, 0);
122 test->ctx = test_create_context(test);
123 list_init(&test->files);
128 void test_fini(struct parser_test *test)
130 device_handler_destroy(test->handler);
135 void __test_read_conf_data(struct parser_test *test,
136 struct discover_device *dev, const char *conf_file,
137 const char *buf, size_t len)
139 test_add_file_data(test, dev, conf_file, buf, len);
142 void test_read_conf_file(struct parser_test *test, const char *filename,
143 const char *conf_file)
151 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
153 fd = open(path, O_RDONLY);
155 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
157 rc = fstat(fd, &stat);
162 buf = talloc_array(test, char, size + 1);
164 rc = read(fd, buf, size);
165 assert(rc == (ssize_t)size);
167 *(buf + size) = '\0';
172 test_add_file_data(test, test->ctx->device, conf_file, buf, size);
175 void test_add_file_data(struct parser_test *test, struct discover_device *dev,
176 const char *filename, const void *data, int size)
178 struct test_file *file;
180 file = talloc_zero(test, struct test_file);
181 file->type = TEST_FILE;
183 file->name = filename;
184 file->data = talloc_memdup(test, data, size);
186 list_add(&test->files, &file->list);
189 void test_add_dir(struct parser_test *test, struct discover_device *dev,
192 struct test_file *file;
194 file = talloc_zero(test, struct test_file);
195 file->type = TEST_DIR;
197 file->name = dirname;
198 /* Pick a non-zero size for directories so that "[ -s <dir
199 * path> ]" sees that the file has non-zero size. */
201 list_add(&test->files, &file->list);
204 void test_set_event_source(struct parser_test *test)
206 test->ctx->event = talloc_zero(test->ctx, struct event);
209 void test_set_event_param(struct event *event, const char *name,
212 event_set_param(event, name, value);
215 void test_set_event_device(struct event *event, const char *dev)
217 event->device = talloc_strdup(event, dev);
220 int parser_request_file(struct discover_context *ctx,
221 struct discover_device *dev, const char *filename,
222 char **buf, int *len)
224 struct parser_test *test = ctx->test_data;
225 struct test_file *file;
228 list_for_each_entry(&test->files, file, list) {
229 if (file->dev != dev)
231 if (strcmp(file->name, filename))
233 if (file->type != TEST_FILE)
236 /* the read_file() interface always adds a trailing null
237 * for string-safety; do the same here */
238 tmp = talloc_array(test, char, file->size + 1);
239 memcpy(tmp, file->data, file->size);
240 tmp[file->size] = '\0';
249 int parser_stat_path(struct discover_context *ctx,
250 struct discover_device *dev, const char *path,
251 struct stat *statbuf)
253 struct parser_test *test = ctx->test_data;
254 struct test_file *file;
256 list_for_each_entry(&test->files, file, list) {
257 if (file->dev != dev)
259 if (strcmp(file->name, path))
262 statbuf->st_size = (off_t)file->size;
263 switch (file->type) {
265 statbuf->st_mode = S_IFREG;
268 statbuf->st_mode = S_IFDIR;
271 fprintf(stderr, "%s: bad test file mode %d!", __func__,
282 int parser_replace_file(struct discover_context *ctx,
283 struct discover_device *dev, const char *filename,
286 struct parser_test *test = ctx->test_data;
287 struct test_file *f, *file;
289 list_for_each_entry(&test->files, f, list) {
292 if (strcmp(f->name, filename))
300 file = talloc_zero(test, struct test_file);
302 file->name = filename;
303 list_add(&test->files, &file->list);
306 file->data = talloc_memdup(test, buf, len);
311 struct load_url_result *load_url_async(void *ctx, struct pb_url *url,
312 load_url_complete async_cb, void *async_data)
314 struct conf_context *conf = async_data;
315 struct parser_test *test = conf->dc->test_data;
316 struct load_url_result *result;
317 char tmp[] = "/tmp/pb-XXXXXX";
318 ssize_t rc = -1, sz = 0;
319 struct test_file *file;
327 /* Some parsers will expect to need to read a file, so write the
328 * specified file to a temporary file */
329 list_for_each_entry(&test->files, file, list) {
333 if (strcmp(file->name, url->full))
336 while (sz < file->size) {
337 rc = write(fd, file->data, file->size);
340 "Failed to write to tmpfile, %m\n");
350 result = talloc_zero(ctx, struct load_url_result);
354 result->local = talloc_strdup(result, tmp);
356 result->status = LOAD_ERROR;
358 result->status = result->local ? LOAD_OK : LOAD_ERROR;
359 result->cleanup_local = true;
361 async_cb(result, conf);
366 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
367 char **buf, int *len)
369 struct parser_test *test = ctx->test_data;
370 struct test_file *file;
373 list_for_each_entry(&test->files, file, list) {
377 if (strcmp(file->name, url->full))
380 /* the read_file() interface always adds a trailing null
381 * for string-safety; do the same here */
382 tmp = talloc_array(test, char, file->size + 1);
383 memcpy(tmp, file->data, file->size);
384 tmp[file->size] = '\0';
393 int test_run_parser(struct parser_test *test, const char *parser_name)
397 list_for_each_entry(&parsers, i, list) {
398 if (strcmp(i->parser->name, parser_name))
400 test->ctx->parser = i->parser;
401 return i->parser->parse(test->ctx);
404 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
407 bool resource_resolve(struct device_handler *handler, struct parser *parser,
408 struct resource *resource)
412 if (resource->resolved)
416 assert(parser->resolve_resource);
418 return parser->resolve_resource(handler, resource);
421 void boot_option_resolve(struct device_handler *handler,
422 struct discover_boot_option *opt)
424 resource_resolve(handler, opt->source, opt->boot_image);
425 resource_resolve(handler, opt->source, opt->initrd);
426 resource_resolve(handler, opt->source, opt->icon);
429 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
431 struct discover_boot_option *opt;
433 device_handler_add_device(test->handler, dev);
435 list_for_each_entry(&test->ctx->boot_options, opt, list)
436 boot_option_resolve(test->handler, opt);
439 void test_remove_device(struct parser_test *test, struct discover_device *dev)
441 struct discover_boot_option *opt, *tmp;
443 if (dev == test->ctx->device) {
444 list_for_each_entry_safe(&test->ctx->boot_options,
446 list_remove(&opt->list);
451 device_handler_remove(test->handler, dev);
454 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
457 struct discover_boot_option *opt;
460 list_for_each_entry(&ctx->boot_options, opt, list) {
470 void __check_boot_option_count(struct discover_context *ctx, int count,
471 const char *file, int line)
473 struct discover_boot_option *opt;
474 int defaults = 0, i = 0;
476 list_for_each_entry(&ctx->boot_options, opt, list) {
478 if (opt->option->is_default)
483 fprintf(stderr, "%s:%d: parser returned multiple default "
484 "options\n", file, line);
491 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
492 fprintf(stderr, "expected %d options, got %d:\n", count, i);
495 list_for_each_entry(&ctx->boot_options, opt, list)
496 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
502 void __check_args(struct discover_boot_option *opt, const char *args,
503 const char *file, int line)
507 if (!opt->option->boot_args && !args)
510 if (!opt->option->boot_args) {
511 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
512 fprintf(stderr, " no arguments parsed\n");
513 fprintf(stderr, " expected '%s'\n", args);
517 rc = strcmp(opt->option->boot_args, args);
519 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
520 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
521 fprintf(stderr, " expected '%s'\n", args);
526 void __check_name(struct discover_boot_option *opt, const char *name,
527 const char *file, int line)
531 rc = strcmp(opt->option->name, name);
533 fprintf(stderr, "%s:%d: name check failed\n", file, line);
534 fprintf(stderr, " got '%s'\n", opt->option->name);
535 fprintf(stderr, " expected '%s'\n", name);
540 void __check_is_default(struct discover_boot_option *opt,
541 const char *file, int line)
543 if (opt->option->is_default)
546 fprintf(stderr, "%s:%d: default check failed\n", file, line);
550 void __check_resolved_local_resource(struct resource *res,
551 struct discover_device *dev, const char *local_path,
552 const char *file, int line)
554 const char *exp_url, *got_url;
557 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
560 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
563 exp_url = talloc_asprintf(res, "file://%s%s",
564 dev->mount_path, local_path);
565 got_url = pb_url_to_string(res->url);
567 if (strcmp(got_url, exp_url)) {
568 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
569 fprintf(stderr, " got '%s'\n", got_url);
570 fprintf(stderr, " expected '%s'\n", exp_url);
575 void __check_resolved_url_resource(struct resource *res,
576 const char *url, const char *file, int line)
581 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
584 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
587 res_url = pb_url_to_string(res->url);
588 if (strcmp(url, res_url)) {
589 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
590 fprintf(stderr, " got '%s'\n", res_url);
591 fprintf(stderr, " expected '%s'\n", url);
595 void __check_unresolved_resource(struct resource *res,
596 const char *file, int line)
599 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
602 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
605 void __check_not_present_resource(struct resource *res,
606 const char *file, int line)
609 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
612 static void dump_file_data(const void *buf, int len)
614 int i, j, hex_len = strlen("00 ");
615 const int row_len = 16;
617 for (i = 0; i < len; i += row_len) {
618 char hbuf[row_len * hex_len + 1];
619 char cbuf[row_len + strlen("|") + 1];
621 for (j = 0; (j < row_len) && ((i+j) < len); j++) {
622 char c = ((const char *)buf)[i + j];
624 snprintf(hbuf + j * hex_len, hex_len + 1, "%02x ", c);
629 snprintf(cbuf + j, hex_len + 1, "%c", c);
634 fprintf(stderr, "%08x %*s |%s\n", i,
635 0 - (int)sizeof(hbuf) + 1, hbuf, cbuf);
639 void __check_file_contents(struct parser_test *test,
640 struct discover_device *dev, const char *filename,
641 const char *buf, int len,
642 const char *srcfile, int srcline)
644 struct test_file *f, *file = NULL;
646 list_for_each_entry(&test->files, f, list) {
649 if (strcmp(f->name, filename))
657 errx(EXIT_FAILURE, "%s:%d: File '%s' not found",
658 srcfile, srcline, filename);
660 if (file->size != len || memcmp(file->data, buf, len)) {
661 fprintf(stderr, "%s:%d: File '%s' data/size mismatch\n",
662 srcfile, srcline, filename);
663 fprintf(stderr, "Expected:\n");
664 dump_file_data(buf, len);
665 fprintf(stderr, "Got:\n");
666 dump_file_data(file->data, file->size);