11 #include <talloc/talloc.h>
12 #include <types/types.h>
15 #include "device-handler.h"
20 #include "parser-test.h"
23 struct list_item list;
24 struct parser *parser;
28 struct discover_device *dev;
36 struct list_item list;
41 void __register_parser(struct parser *parser)
43 struct p_item* i = talloc(NULL, struct p_item);
46 list_add(&parsers, &i->list);
49 static void __attribute__((destructor)) __cleanup_parsers(void)
51 struct p_item *item, *tmp;
53 list_for_each_entry_safe(&parsers, item, tmp, list)
57 static struct discover_device *test_create_device_simple(
58 struct parser_test *test)
63 sprintf(name, "__test%d", dev_idx++);
65 return test_create_device(test, name);
68 struct discover_device *test_create_device(struct parser_test *test,
71 struct discover_device *dev;
73 dev = discover_device_create(test->handler, name);
75 dev->device->id = talloc_strdup(dev, name);
76 dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
77 dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
83 static struct discover_context *test_create_context(struct parser_test *test)
85 struct discover_context *ctx;
87 ctx = talloc_zero(test, struct discover_context);
90 list_init(&ctx->boot_options);
91 ctx->device = test_create_device_simple(test);
92 ctx->test_data = test;
93 device_handler_add_device(test->handler, ctx->device);
98 extern struct config *test_config_init(struct parser_test *test);
100 struct parser_test *test_init(void)
102 struct parser_test *test;
104 test = talloc_zero(NULL, struct parser_test);
105 test->config = test_config_init(test);
106 test->handler = device_handler_init(NULL, NULL, 0);
107 test->ctx = test_create_context(test);
108 list_init(&test->files);
113 void test_fini(struct parser_test *test)
115 device_handler_destroy(test->handler);
119 void __test_read_conf_data(struct parser_test *test,
120 struct discover_device *dev, const char *conf_file,
121 const char *buf, size_t len)
123 test_add_file_data(test, dev, conf_file, buf, len);
126 void test_read_conf_file(struct parser_test *test, const char *filename,
127 const char *conf_file)
135 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
137 fd = open(path, O_RDONLY);
139 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
141 rc = fstat(fd, &stat);
146 buf = talloc_array(test, char, size + 1);
148 rc = read(fd, buf, size);
149 assert(rc == (ssize_t)size);
151 *(buf + size) = '\0';
156 test_add_file_data(test, test->ctx->device, conf_file, buf, size);
159 void test_add_file_data(struct parser_test *test, struct discover_device *dev,
160 const char *filename, const void *data, int size)
162 struct test_file *file;
164 file = talloc_zero(test, struct test_file);
165 file->type = TEST_FILE;
167 file->name = filename;
168 file->data = talloc_memdup(test, data, size);
170 list_add(&test->files, &file->list);
173 void test_add_dir(struct parser_test *test, struct discover_device *dev,
176 struct test_file *file;
178 file = talloc_zero(test, struct test_file);
179 file->type = TEST_DIR;
181 file->name = dirname;
182 list_add(&test->files, &file->list);
185 void test_set_event_source(struct parser_test *test)
187 test->ctx->event = talloc_zero(test->ctx, struct event);
190 void test_set_event_param(struct event *event, const char *name,
193 event_set_param(event, name, value);
196 int parser_request_file(struct discover_context *ctx,
197 struct discover_device *dev, const char *filename,
198 char **buf, int *len)
200 struct parser_test *test = ctx->test_data;
201 struct test_file *file;
204 list_for_each_entry(&test->files, file, list) {
205 if (file->dev != dev)
207 if (strcmp(file->name, filename))
209 if (file->type != TEST_FILE)
212 /* the read_file() interface always adds a trailing null
213 * for string-safety; do the same here */
214 tmp = talloc_array(test, char, file->size + 1);
215 memcpy(tmp, file->data, file->size);
216 tmp[file->size] = '\0';
225 int parser_check_dir(struct discover_context *ctx,
226 struct discover_device *dev, const char *dirname)
228 struct parser_test *test = ctx->test_data;
229 struct test_file *file;
231 printf("%s: %s\n", __func__, dirname);
233 list_for_each_entry(&test->files, file, list) {
234 if (file->dev != dev)
236 if (strcmp(file->name, dirname))
238 return file->type == TEST_DIR ? 0 : -1;
244 int parser_replace_file(struct discover_context *ctx,
245 struct discover_device *dev, const char *filename,
248 struct parser_test *test = ctx->test_data;
249 struct test_file *f, *file;
251 list_for_each_entry(&test->files, f, list) {
254 if (strcmp(f->name, filename))
262 file = talloc_zero(test, struct test_file);
264 file->name = filename;
265 list_add(&test->files, &file->list);
268 file->data = talloc_memdup(test, buf, len);
273 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
274 char **buf, int *len)
276 struct parser_test *test = ctx->test_data;
277 struct test_file *file;
280 list_for_each_entry(&test->files, file, list) {
284 if (strcmp(file->name, url->full))
287 /* the read_file() interface always adds a trailing null
288 * for string-safety; do the same here */
289 tmp = talloc_array(test, char, file->size + 1);
290 memcpy(tmp, file->data, file->size);
291 tmp[file->size] = '\0';
300 int test_run_parser(struct parser_test *test, const char *parser_name)
304 list_for_each_entry(&parsers, i, list) {
305 if (strcmp(i->parser->name, parser_name))
307 test->ctx->parser = i->parser;
308 return i->parser->parse(test->ctx);
311 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
314 bool resource_resolve(struct device_handler *handler, struct parser *parser,
315 struct resource *resource)
319 if (resource->resolved)
323 assert(parser->resolve_resource);
325 return parser->resolve_resource(handler, resource);
328 void boot_option_resolve(struct device_handler *handler,
329 struct discover_boot_option *opt)
331 resource_resolve(handler, opt->source, opt->boot_image);
332 resource_resolve(handler, opt->source, opt->initrd);
333 resource_resolve(handler, opt->source, opt->icon);
336 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
338 struct discover_boot_option *opt;
340 device_handler_add_device(test->handler, dev);
342 list_for_each_entry(&test->ctx->boot_options, opt, list)
343 boot_option_resolve(test->handler, opt);
346 void test_remove_device(struct parser_test *test, struct discover_device *dev)
348 struct discover_boot_option *opt, *tmp;
350 if (dev == test->ctx->device) {
351 list_for_each_entry_safe(&test->ctx->boot_options,
353 list_remove(&opt->list);
358 device_handler_remove(test->handler, dev);
361 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
364 struct discover_boot_option *opt;
367 list_for_each_entry(&ctx->boot_options, opt, list) {
377 void __check_boot_option_count(struct discover_context *ctx, int count,
378 const char *file, int line)
380 struct discover_boot_option *opt;
381 int defaults = 0, i = 0;
383 list_for_each_entry(&ctx->boot_options, opt, list) {
385 if (opt->option->is_default)
390 fprintf(stderr, "%s:%d: parser returned multiple default "
391 "options\n", file, line);
398 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
399 fprintf(stderr, "expected %d options, got %d:\n", count, i);
402 list_for_each_entry(&ctx->boot_options, opt, list)
403 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
409 void __check_args(struct discover_boot_option *opt, const char *args,
410 const char *file, int line)
414 if (!opt->option->boot_args && !args)
417 if (!opt->option->boot_args) {
418 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
419 fprintf(stderr, " no arguments parsed\n");
420 fprintf(stderr, " expected '%s'\n", args);
424 rc = strcmp(opt->option->boot_args, args);
426 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
427 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
428 fprintf(stderr, " expected '%s'\n", args);
433 void __check_name(struct discover_boot_option *opt, const char *name,
434 const char *file, int line)
438 rc = strcmp(opt->option->name, name);
440 fprintf(stderr, "%s:%d: name check failed\n", file, line);
441 fprintf(stderr, " got '%s'\n", opt->option->name);
442 fprintf(stderr, " expected '%s'\n", name);
447 void __check_is_default(struct discover_boot_option *opt,
448 const char *file, int line)
450 if (opt->option->is_default)
453 fprintf(stderr, "%s:%d: default check failed\n", file, line);
457 void __check_resolved_local_resource(struct resource *res,
458 struct discover_device *dev, const char *local_path,
459 const char *file, int line)
461 const char *exp_url, *got_url;
464 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
467 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
470 exp_url = talloc_asprintf(res, "file://%s%s",
471 dev->mount_path, local_path);
472 got_url = pb_url_to_string(res->url);
474 if (strcmp(got_url, exp_url)) {
475 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
476 fprintf(stderr, " got '%s'\n", got_url);
477 fprintf(stderr, " expected '%s'\n", exp_url);
482 void __check_resolved_url_resource(struct resource *res,
483 const char *url, const char *file, int line)
488 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
491 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
494 res_url = pb_url_to_string(res->url);
495 if (strcmp(url, res_url)) {
496 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
497 fprintf(stderr, " got '%s'\n", res_url);
498 fprintf(stderr, " expected '%s'\n", url);
502 void __check_unresolved_resource(struct resource *res,
503 const char *file, int line)
506 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
509 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
512 void __check_not_present_resource(struct resource *res,
513 const char *file, int line)
516 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
519 static void dump_file_data(const void *buf, int len)
521 int i, j, hex_len = strlen("00 ");
522 const int row_len = 16;
524 for (i = 0; i < len; i += row_len) {
525 char hbuf[row_len * hex_len + 1];
526 char cbuf[row_len + strlen("|") + 1];
528 for (j = 0; (j < row_len) && ((i+j) < len); j++) {
529 char c = ((const char *)buf)[i + j];
531 snprintf(hbuf + j * hex_len, hex_len + 1, "%02x ", c);
536 snprintf(cbuf + j, hex_len + 1, "%c", c);
541 fprintf(stderr, "%08x %*s |%s\n", i,
542 0 - (int)sizeof(hbuf) + 1, hbuf, cbuf);
546 void __check_file_contents(struct parser_test *test,
547 struct discover_device *dev, const char *filename,
548 const char *buf, int len,
549 const char *srcfile, int srcline)
551 struct test_file *f, *file = NULL;
553 list_for_each_entry(&test->files, f, list) {
556 if (strcmp(f->name, filename))
564 errx(EXIT_FAILURE, "%s:%d: File '%s' not found",
565 srcfile, srcline, filename);
567 if (file->size != len || memcmp(file->data, buf, len)) {
568 fprintf(stderr, "%s:%d: File '%s' data/size mismatch\n",
569 srcfile, srcline, filename);
570 fprintf(stderr, "Expected:\n");
571 dump_file_data(buf, len);
572 fprintf(stderr, "Got:\n");
573 dump_file_data(file->data, file->size);