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;
32 struct list_item list;
37 void __register_parser(struct parser *parser)
39 struct p_item* i = talloc(NULL, struct p_item);
42 list_add(&parsers, &i->list);
45 static void __attribute__((destructor)) __cleanup_parsers(void)
47 struct p_item *item, *tmp;
49 list_for_each_entry_safe(&parsers, item, tmp, list)
53 static struct discover_device *test_create_device_simple(
54 struct parser_test *test)
59 sprintf(name, "__test%d", dev_idx++);
61 return test_create_device(test, name);
64 struct discover_device *test_create_device(struct parser_test *test,
67 struct discover_device *dev;
69 dev = discover_device_create(test->handler, name);
71 dev->device->id = talloc_strdup(dev, name);
72 dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
73 dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
79 static struct discover_context *test_create_context(struct parser_test *test)
81 struct discover_context *ctx;
83 ctx = talloc_zero(test, struct discover_context);
86 list_init(&ctx->boot_options);
87 ctx->device = test_create_device_simple(test);
88 ctx->test_data = test;
89 device_handler_add_device(test->handler, ctx->device);
94 extern struct config *test_config_init(struct parser_test *test);
96 struct parser_test *test_init(void)
98 struct parser_test *test;
100 test = talloc_zero(NULL, struct parser_test);
101 test->config = test_config_init(test);
102 test->handler = device_handler_init(NULL, NULL, 0);
103 test->ctx = test_create_context(test);
104 list_init(&test->files);
109 void test_fini(struct parser_test *test)
111 device_handler_destroy(test->handler);
115 void __test_read_conf_data(struct parser_test *test, const char *conf_file,
116 const char *buf, size_t len)
118 test_add_file_data(test, test->ctx->device, conf_file, buf, len);
121 void test_read_conf_file(struct parser_test *test, const char *filename,
122 const char *conf_file)
130 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
132 fd = open(path, O_RDONLY);
134 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
136 rc = fstat(fd, &stat);
141 buf = talloc_array(test, char, size + 1);
143 rc = read(fd, buf, size);
144 assert(rc == (ssize_t)size);
146 *(buf + size) = '\0';
151 test_add_file_data(test, test->ctx->device, conf_file, buf, size);
154 void test_add_file_data(struct parser_test *test, struct discover_device *dev,
155 const char *filename, const void *data, int size)
157 struct test_file *file;
159 file = talloc_zero(test, struct test_file);
161 file->name = filename;
162 file->data = talloc_memdup(test, data, size);
164 list_add(&test->files, &file->list);
167 void test_set_event_source(struct parser_test *test)
169 test->ctx->event = talloc_zero(test->ctx, struct event);
172 void test_set_event_param(struct event *event, const char *name,
175 event_set_param(event, name, value);
178 int parser_request_file(struct discover_context *ctx,
179 struct discover_device *dev, const char *filename,
180 char **buf, int *len)
182 struct parser_test *test = ctx->test_data;
183 struct test_file *file;
186 list_for_each_entry(&test->files, file, list) {
187 if (file->dev != dev)
189 if (strcmp(file->name, filename))
192 /* the read_file() interface always adds a trailing null
193 * for string-safety; do the same here */
194 tmp = talloc_array(test, char, file->size + 1);
195 memcpy(tmp, file->data, file->size);
196 tmp[file->size] = '\0';
205 int parser_replace_file(struct discover_context *ctx,
206 struct discover_device *dev, const char *filename,
209 struct parser_test *test = ctx->test_data;
210 struct test_file *f, *file;
212 list_for_each_entry(&test->files, f, list) {
215 if (strcmp(f->name, filename))
223 file = talloc_zero(test, struct test_file);
225 file->name = filename;
226 list_add(&test->files, &file->list);
229 file->data = talloc_memdup(test, buf, len);
234 int parser_request_url(struct discover_context *ctx, struct pb_url *url,
235 char **buf, int *len)
237 struct parser_test *test = ctx->test_data;
238 struct test_file *file;
241 list_for_each_entry(&test->files, file, list) {
242 if (strcmp(file->name, url->file))
245 /* the read_file() interface always adds a trailing null
246 * for string-safety; do the same here */
247 tmp = talloc_array(test, char, file->size + 1);
248 memcpy(tmp, file->data, file->size);
249 tmp[file->size] = '\0';
258 int test_run_parser(struct parser_test *test, const char *parser_name)
262 list_for_each_entry(&parsers, i, list) {
263 if (strcmp(i->parser->name, parser_name))
265 test->ctx->parser = i->parser;
266 return i->parser->parse(test->ctx);
269 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
272 bool resource_resolve(struct device_handler *handler, struct parser *parser,
273 struct resource *resource)
277 if (resource->resolved)
281 assert(parser->resolve_resource);
283 return parser->resolve_resource(handler, resource);
286 void boot_option_resolve(struct device_handler *handler,
287 struct discover_boot_option *opt)
289 resource_resolve(handler, opt->source, opt->boot_image);
290 resource_resolve(handler, opt->source, opt->initrd);
291 resource_resolve(handler, opt->source, opt->icon);
294 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
296 struct discover_boot_option *opt;
298 device_handler_add_device(test->handler, dev);
300 list_for_each_entry(&test->ctx->boot_options, opt, list)
301 boot_option_resolve(test->handler, opt);
304 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
307 struct discover_boot_option *opt;
310 list_for_each_entry(&ctx->boot_options, opt, list) {
320 void __check_boot_option_count(struct discover_context *ctx, int count,
321 const char *file, int line)
323 struct discover_boot_option *opt;
324 int defaults = 0, i = 0;
326 list_for_each_entry(&ctx->boot_options, opt, list) {
328 if (opt->option->is_default)
333 fprintf(stderr, "%s:%d: parser returned multiple default "
334 "options\n", file, line);
341 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
342 fprintf(stderr, "expected %d options, got %d:\n", count, i);
345 list_for_each_entry(&ctx->boot_options, opt, list)
346 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
352 void __check_args(struct discover_boot_option *opt, const char *args,
353 const char *file, int line)
357 if (!opt->option->boot_args && !args)
360 if (!opt->option->boot_args) {
361 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
362 fprintf(stderr, " no arguments parsed\n");
363 fprintf(stderr, " expected '%s'\n", args);
367 rc = strcmp(opt->option->boot_args, args);
369 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
370 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
371 fprintf(stderr, " expected '%s'\n", args);
376 void __check_name(struct discover_boot_option *opt, const char *name,
377 const char *file, int line)
381 rc = strcmp(opt->option->name, name);
383 fprintf(stderr, "%s:%d: name check failed\n", file, line);
384 fprintf(stderr, " got '%s'\n", opt->option->name);
385 fprintf(stderr, " expected '%s'\n", name);
390 void __check_is_default(struct discover_boot_option *opt,
391 const char *file, int line)
393 if (opt->option->is_default)
396 fprintf(stderr, "%s:%d: default check failed\n", file, line);
400 void __check_resolved_local_resource(struct resource *res,
401 struct discover_device *dev, const char *local_path,
402 const char *file, int line)
404 const char *exp_url, *got_url;
407 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
410 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
413 exp_url = talloc_asprintf(res, "file://%s%s",
414 dev->mount_path, local_path);
415 got_url = pb_url_to_string(res->url);
417 if (strcmp(got_url, exp_url)) {
418 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
419 fprintf(stderr, " got '%s'\n", got_url);
420 fprintf(stderr, " expected '%s'\n", exp_url);
425 void __check_resolved_url_resource(struct resource *res,
426 const char *url, const char *file, int line)
431 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
434 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
437 res_url = pb_url_to_string(res->url);
438 if (strcmp(url, res_url)) {
439 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
440 fprintf(stderr, " got '%s'\n", res_url);
441 fprintf(stderr, " expected '%s'\n", url);
445 void __check_unresolved_resource(struct resource *res,
446 const char *file, int line)
449 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
452 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
455 void __check_not_present_resource(struct resource *res,
456 const char *file, int line)
459 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
462 static void dump_file_data(const void *buf, int len)
464 int i, j, hex_len = strlen("00 ");
465 const int row_len = 16;
467 for (i = 0; i < len; i += row_len) {
468 char hbuf[row_len * hex_len + 1];
469 char cbuf[row_len + strlen("|") + 1];
471 for (j = 0; (j < row_len) && ((i+j) < len); j++) {
472 char c = ((const char *)buf)[i + j];
474 snprintf(hbuf + j * hex_len, hex_len + 1, "%02x ", c);
479 snprintf(cbuf + j, hex_len + 1, "%c", c);
484 fprintf(stderr, "%08x %*s |%s\n", i,
485 0 - (int)sizeof(hbuf) + 1, hbuf, cbuf);
489 void __check_file_contents(struct parser_test *test,
490 struct discover_device *dev, const char *filename,
491 const char *buf, int len,
492 const char *srcfile, int srcline)
494 struct test_file *f, *file = NULL;
496 list_for_each_entry(&test->files, f, list) {
499 if (strcmp(f->name, filename))
507 errx(EXIT_FAILURE, "%s:%d: File '%s' not found",
508 srcfile, srcline, filename);
510 if (file->size != len || memcmp(file->data, buf, len)) {
511 fprintf(stderr, "%s:%d: File '%s' data/size mismatch\n",
512 srcfile, srcline, filename);
513 fprintf(stderr, "Expected:\n");
514 dump_file_data(buf, len);
515 fprintf(stderr, "Got:\n");
516 dump_file_data(file->data, file->size);