11 #include <talloc/talloc.h>
12 #include <types/types.h>
15 #include "device-handler.h"
21 #include "parser-test.h"
24 struct list_item list;
25 struct parser *parser;
29 struct discover_device *dev;
37 struct list_item list;
42 void __register_parser(struct parser *parser)
44 struct p_item* i = talloc(NULL, struct p_item);
47 list_add(&parsers, &i->list);
50 static void __attribute__((destructor)) __cleanup_parsers(void)
52 struct p_item *item, *tmp;
54 list_for_each_entry_safe(&parsers, item, tmp, list)
58 static struct discover_device *test_create_device_simple(
59 struct parser_test *test)
64 sprintf(name, "__test%d", dev_idx++);
66 return test_create_device(test, name);
69 struct discover_device *test_create_device(struct parser_test *test,
72 struct discover_device *dev;
74 dev = discover_device_create(test->handler, name);
76 dev->device->id = talloc_strdup(dev, name);
77 dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
78 dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
84 static struct discover_context *test_create_context(struct parser_test *test)
86 struct discover_context *ctx;
88 ctx = talloc_zero(test, struct discover_context);
91 list_init(&ctx->boot_options);
92 ctx->device = test_create_device_simple(test);
93 ctx->test_data = test;
94 device_handler_add_device(test->handler, ctx->device);
99 /* define our own test platform */
100 static bool test_platform_probe(struct platform *p __attribute__((unused)),
101 void *ctx __attribute__((unused)))
106 struct platform test_platform = {
108 .probe = test_platform_probe,
111 register_platform(test_platform);
113 struct parser_test *test_init(void)
115 struct parser_test *test;
117 test = talloc_zero(NULL, struct parser_test);
119 test->handler = device_handler_init(NULL, NULL, 0);
120 test->ctx = test_create_context(test);
121 list_init(&test->files);
126 void test_fini(struct parser_test *test)
128 device_handler_destroy(test->handler);
133 void __test_read_conf_data(struct parser_test *test,
134 struct discover_device *dev, const char *conf_file,
135 const char *buf, size_t len)
137 test_add_file_data(test, dev, conf_file, buf, len);
140 void test_read_conf_file(struct parser_test *test, const char *filename,
141 const char *conf_file)
149 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
151 fd = open(path, O_RDONLY);
153 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
155 rc = fstat(fd, &stat);
160 buf = talloc_array(test, char, size + 1);
162 rc = read(fd, buf, size);
163 assert(rc == (ssize_t)size);
165 *(buf + size) = '\0';
170 test_add_file_data(test, test->ctx->device, conf_file, buf, size);
173 void test_add_file_data(struct parser_test *test, struct discover_device *dev,
174 const char *filename, const void *data, int size)
176 struct test_file *file;
178 file = talloc_zero(test, struct test_file);
179 file->type = TEST_FILE;
181 file->name = filename;
182 file->data = talloc_memdup(test, data, size);
184 list_add(&test->files, &file->list);
187 void test_add_dir(struct parser_test *test, struct discover_device *dev,
190 struct test_file *file;
192 file = talloc_zero(test, struct test_file);
193 file->type = TEST_DIR;
195 file->name = dirname;
196 list_add(&test->files, &file->list);
199 void test_set_event_source(struct parser_test *test)
201 test->ctx->event = talloc_zero(test->ctx, struct event);
204 void test_set_event_param(struct event *event, const char *name,
207 event_set_param(event, name, value);
210 void test_set_event_device(struct event *event, const char *dev)
212 event->device = talloc_strdup(event, dev);
215 int parser_request_file(struct discover_context *ctx,
216 struct discover_device *dev, const char *filename,
217 char **buf, int *len)
219 struct parser_test *test = ctx->test_data;
220 struct test_file *file;
223 list_for_each_entry(&test->files, file, list) {
224 if (file->dev != dev)
226 if (strcmp(file->name, filename))
228 if (file->type != TEST_FILE)
231 /* the read_file() interface always adds a trailing null
232 * for string-safety; do the same here */
233 tmp = talloc_array(test, char, file->size + 1);
234 memcpy(tmp, file->data, file->size);
235 tmp[file->size] = '\0';
244 int parser_check_dir(struct discover_context *ctx,
245 struct discover_device *dev, const char *dirname)
247 struct parser_test *test = ctx->test_data;
248 struct test_file *file;
250 printf("%s: %s\n", __func__, dirname);
252 list_for_each_entry(&test->files, file, list) {
253 if (file->dev != dev)
255 if (strcmp(file->name, dirname))
257 return file->type == TEST_DIR ? 0 : -1;
263 int parser_replace_file(struct discover_context *ctx,
264 struct discover_device *dev, const char *filename,
267 struct parser_test *test = ctx->test_data;
268 struct test_file *f, *file;
270 list_for_each_entry(&test->files, f, list) {
273 if (strcmp(f->name, filename))
281 file = talloc_zero(test, struct test_file);
283 file->name = filename;
284 list_add(&test->files, &file->list);
287 file->data = talloc_memdup(test, buf, len);
292 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
293 char **buf, int *len)
295 struct parser_test *test = ctx->test_data;
296 struct test_file *file;
299 list_for_each_entry(&test->files, file, list) {
303 if (strcmp(file->name, url->full))
306 /* the read_file() interface always adds a trailing null
307 * for string-safety; do the same here */
308 tmp = talloc_array(test, char, file->size + 1);
309 memcpy(tmp, file->data, file->size);
310 tmp[file->size] = '\0';
319 int test_run_parser(struct parser_test *test, const char *parser_name)
323 list_for_each_entry(&parsers, i, list) {
324 if (strcmp(i->parser->name, parser_name))
326 test->ctx->parser = i->parser;
327 return i->parser->parse(test->ctx);
330 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
333 bool resource_resolve(struct device_handler *handler, struct parser *parser,
334 struct resource *resource)
338 if (resource->resolved)
342 assert(parser->resolve_resource);
344 return parser->resolve_resource(handler, resource);
347 void boot_option_resolve(struct device_handler *handler,
348 struct discover_boot_option *opt)
350 resource_resolve(handler, opt->source, opt->boot_image);
351 resource_resolve(handler, opt->source, opt->initrd);
352 resource_resolve(handler, opt->source, opt->icon);
355 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
357 struct discover_boot_option *opt;
359 device_handler_add_device(test->handler, dev);
361 list_for_each_entry(&test->ctx->boot_options, opt, list)
362 boot_option_resolve(test->handler, opt);
365 void test_remove_device(struct parser_test *test, struct discover_device *dev)
367 struct discover_boot_option *opt, *tmp;
369 if (dev == test->ctx->device) {
370 list_for_each_entry_safe(&test->ctx->boot_options,
372 list_remove(&opt->list);
377 device_handler_remove(test->handler, dev);
380 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
383 struct discover_boot_option *opt;
386 list_for_each_entry(&ctx->boot_options, opt, list) {
396 void __check_boot_option_count(struct discover_context *ctx, int count,
397 const char *file, int line)
399 struct discover_boot_option *opt;
400 int defaults = 0, i = 0;
402 list_for_each_entry(&ctx->boot_options, opt, list) {
404 if (opt->option->is_default)
409 fprintf(stderr, "%s:%d: parser returned multiple default "
410 "options\n", file, line);
417 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
418 fprintf(stderr, "expected %d options, got %d:\n", count, i);
421 list_for_each_entry(&ctx->boot_options, opt, list)
422 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
428 void __check_args(struct discover_boot_option *opt, const char *args,
429 const char *file, int line)
433 if (!opt->option->boot_args && !args)
436 if (!opt->option->boot_args) {
437 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
438 fprintf(stderr, " no arguments parsed\n");
439 fprintf(stderr, " expected '%s'\n", args);
443 rc = strcmp(opt->option->boot_args, args);
445 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
446 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
447 fprintf(stderr, " expected '%s'\n", args);
452 void __check_name(struct discover_boot_option *opt, const char *name,
453 const char *file, int line)
457 rc = strcmp(opt->option->name, name);
459 fprintf(stderr, "%s:%d: name check failed\n", file, line);
460 fprintf(stderr, " got '%s'\n", opt->option->name);
461 fprintf(stderr, " expected '%s'\n", name);
466 void __check_is_default(struct discover_boot_option *opt,
467 const char *file, int line)
469 if (opt->option->is_default)
472 fprintf(stderr, "%s:%d: default check failed\n", file, line);
476 void __check_resolved_local_resource(struct resource *res,
477 struct discover_device *dev, const char *local_path,
478 const char *file, int line)
480 const char *exp_url, *got_url;
483 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
486 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
489 exp_url = talloc_asprintf(res, "file://%s%s",
490 dev->mount_path, local_path);
491 got_url = pb_url_to_string(res->url);
493 if (strcmp(got_url, exp_url)) {
494 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
495 fprintf(stderr, " got '%s'\n", got_url);
496 fprintf(stderr, " expected '%s'\n", exp_url);
501 void __check_resolved_url_resource(struct resource *res,
502 const char *url, const char *file, int line)
507 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
510 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
513 res_url = pb_url_to_string(res->url);
514 if (strcmp(url, res_url)) {
515 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
516 fprintf(stderr, " got '%s'\n", res_url);
517 fprintf(stderr, " expected '%s'\n", url);
521 void __check_unresolved_resource(struct resource *res,
522 const char *file, int line)
525 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
528 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
531 void __check_not_present_resource(struct resource *res,
532 const char *file, int line)
535 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
538 static void dump_file_data(const void *buf, int len)
540 int i, j, hex_len = strlen("00 ");
541 const int row_len = 16;
543 for (i = 0; i < len; i += row_len) {
544 char hbuf[row_len * hex_len + 1];
545 char cbuf[row_len + strlen("|") + 1];
547 for (j = 0; (j < row_len) && ((i+j) < len); j++) {
548 char c = ((const char *)buf)[i + j];
550 snprintf(hbuf + j * hex_len, hex_len + 1, "%02x ", c);
555 snprintf(cbuf + j, hex_len + 1, "%c", c);
560 fprintf(stderr, "%08x %*s |%s\n", i,
561 0 - (int)sizeof(hbuf) + 1, hbuf, cbuf);
565 void __check_file_contents(struct parser_test *test,
566 struct discover_device *dev, const char *filename,
567 const char *buf, int len,
568 const char *srcfile, int srcline)
570 struct test_file *f, *file = NULL;
572 list_for_each_entry(&test->files, f, list) {
575 if (strcmp(f->name, filename))
583 errx(EXIT_FAILURE, "%s:%d: File '%s' not found",
584 srcfile, srcline, filename);
586 if (file->size != len || memcmp(file->data, buf, len)) {
587 fprintf(stderr, "%s:%d: File '%s' data/size mismatch\n",
588 srcfile, srcline, filename);
589 fprintf(stderr, "Expected:\n");
590 dump_file_data(buf, len);
591 fprintf(stderr, "Got:\n");
592 dump_file_data(file->data, file->size);