11 #include <talloc/talloc.h>
12 #include <types/types.h>
15 #include "device-handler.h"
19 #include "parser-test.h"
22 struct list_item list;
23 struct parser *parser;
27 struct discover_device *dev;
31 struct list_item list;
36 void __register_parser(struct parser *parser)
38 struct p_item* i = talloc(NULL, struct p_item);
41 list_add(&parsers, &i->list);
44 static void __attribute__((destructor)) __cleanup_parsers(void)
46 struct p_item *item, *tmp;
48 list_for_each_entry_safe(&parsers, item, tmp, list)
52 static struct discover_device *test_create_device_simple(
53 struct parser_test *test)
58 sprintf(name, "__test%d", dev_idx++);
60 return test_create_device(test, name);
63 struct discover_device *test_create_device(struct parser_test *test,
66 struct discover_device *dev;
68 dev = discover_device_create(test->handler, name);
70 dev->device->id = talloc_strdup(dev, name);
71 dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
72 dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
78 static struct discover_context *test_create_context(struct parser_test *test)
80 struct discover_context *ctx;
82 ctx = talloc_zero(test, struct discover_context);
85 list_init(&ctx->boot_options);
86 ctx->device = test_create_device_simple(test);
87 ctx->test_data = test;
88 device_handler_add_device(test->handler, ctx->device);
93 extern struct config *test_config_init(struct parser_test *test);
95 struct parser_test *test_init(void)
97 struct parser_test *test;
99 test = talloc_zero(NULL, struct parser_test);
100 test->config = test_config_init(test);
101 test->handler = device_handler_init(NULL, NULL, 0);
102 test->ctx = test_create_context(test);
103 list_init(&test->files);
108 void test_fini(struct parser_test *test)
110 device_handler_destroy(test->handler);
114 void __test_read_conf_data(struct parser_test *test,
115 const char *buf, size_t len)
117 test->conf.size = len;
118 test->conf.buf = talloc_memdup(test, buf, len);
121 void test_read_conf_file(struct parser_test *test, const char *filename)
127 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
129 fd = open(path, O_RDONLY);
131 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
133 rc = fstat(fd, &stat);
137 test->conf.size = stat.st_size;
138 test->conf.buf = talloc_array(test, char, test->conf.size + 1);
140 rc = read(fd, test->conf.buf, test->conf.size);
141 assert(rc == (ssize_t)test->conf.size);
143 *(char *)(test->conf.buf + test->conf.size) = '\0';
149 void test_set_conf_source(struct parser_test *test, const char *url)
151 test->ctx->conf_url = pb_url_parse(test, url);
152 assert(test->ctx->conf_url);
155 void test_add_file_data(struct parser_test *test, struct discover_device *dev,
156 const char *filename, const void *data, int size)
158 struct test_file *file;
160 file = talloc_zero(test, struct test_file);
162 file->name = filename;
163 file->data = talloc_memdup(test, data, size);
165 list_add(&test->files, &file->list);
169 int parser_request_file(struct discover_context *ctx,
170 struct discover_device *dev, const char *filename,
171 char **buf, int *len)
173 struct parser_test *test = ctx->test_data;
174 struct test_file *file;
177 list_for_each_entry(&test->files, file, list) {
178 if (file->dev != dev)
180 if (strcmp(file->name, filename))
183 /* the read_file() interface always adds a trailing null
184 * for string-safety; do the same here */
185 tmp = talloc_array(test, char, file->size + 1);
186 memcpy(tmp, file->data, file->size);
187 tmp[file->size] = '\0';
196 int parser_replace_file(struct discover_context *ctx,
197 struct discover_device *dev, const char *filename,
200 struct parser_test *test = ctx->test_data;
201 struct test_file *f, *file;
203 list_for_each_entry(&test->files, f, list) {
206 if (strcmp(f->name, filename))
214 file = talloc_zero(test, struct test_file);
216 file->name = filename;
217 list_add(&test->files, &file->list);
220 file->data = talloc_memdup(test, buf, len);
224 int test_run_parser(struct parser_test *test, const char *parser_name)
228 list_for_each_entry(&parsers, i, list) {
229 if (strcmp(i->parser->name, parser_name))
231 test->ctx->parser = i->parser;
232 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
235 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
238 bool resource_resolve(struct device_handler *handler, struct parser *parser,
239 struct resource *resource)
243 if (resource->resolved)
247 assert(parser->resolve_resource);
249 return parser->resolve_resource(handler, resource);
252 void boot_option_resolve(struct device_handler *handler,
253 struct discover_boot_option *opt)
255 resource_resolve(handler, opt->source, opt->boot_image);
256 resource_resolve(handler, opt->source, opt->initrd);
257 resource_resolve(handler, opt->source, opt->icon);
260 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
262 struct discover_boot_option *opt;
264 device_handler_add_device(test->handler, dev);
266 list_for_each_entry(&test->ctx->boot_options, opt, list)
267 boot_option_resolve(test->handler, opt);
270 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
273 struct discover_boot_option *opt;
276 list_for_each_entry(&ctx->boot_options, opt, list) {
286 void __check_boot_option_count(struct discover_context *ctx, int count,
287 const char *file, int line)
289 struct discover_boot_option *opt;
290 int defaults = 0, i = 0;
292 list_for_each_entry(&ctx->boot_options, opt, list) {
294 if (opt->option->is_default)
299 fprintf(stderr, "%s:%d: parser returned multiple default "
300 "options\n", file, line);
307 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
308 fprintf(stderr, "expected %d options, got %d:\n", count, i);
311 list_for_each_entry(&ctx->boot_options, opt, list)
312 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
318 void __check_args(struct discover_boot_option *opt, const char *args,
319 const char *file, int line)
323 if (!opt->option->boot_args && !args)
326 if (!opt->option->boot_args) {
327 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
328 fprintf(stderr, " no arguments parsed\n");
329 fprintf(stderr, " expected '%s'\n", args);
333 rc = strcmp(opt->option->boot_args, args);
335 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
336 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
337 fprintf(stderr, " expected '%s'\n", args);
342 void __check_name(struct discover_boot_option *opt, const char *name,
343 const char *file, int line)
347 rc = strcmp(opt->option->name, name);
349 fprintf(stderr, "%s:%d: name check failed\n", file, line);
350 fprintf(stderr, " got '%s'\n", opt->option->name);
351 fprintf(stderr, " expected '%s'\n", name);
356 void __check_is_default(struct discover_boot_option *opt,
357 const char *file, int line)
359 if (opt->option->is_default)
362 fprintf(stderr, "%s:%d: default check failed\n", file, line);
366 void __check_resolved_local_resource(struct resource *res,
367 struct discover_device *dev, const char *local_path,
368 const char *file, int line)
370 const char *exp_url, *got_url;
373 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
376 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
379 exp_url = talloc_asprintf(res, "file://%s%s",
380 dev->mount_path, local_path);
381 got_url = pb_url_to_string(res->url);
383 if (strcmp(got_url, exp_url)) {
384 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
385 fprintf(stderr, " got '%s'\n", got_url);
386 fprintf(stderr, " expected '%s'\n", exp_url);
391 void __check_resolved_url_resource(struct resource *res,
392 const char *url, const char *file, int line)
397 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
400 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
403 res_url = pb_url_to_string(res->url);
404 if (strcmp(url, res_url)) {
405 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
406 fprintf(stderr, " got '%s'\n", res_url);
407 fprintf(stderr, " expected '%s'\n", url);
411 void __check_unresolved_resource(struct resource *res,
412 const char *file, int line)
415 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
418 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
421 void __check_not_present_resource(struct resource *res,
422 const char *file, int line)
425 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
428 static void dump_file_data(const void *buf, int len)
430 int i, j, hex_len = strlen("00 ");
431 const int row_len = 16;
433 for (i = 0; i < len; i += row_len) {
434 char hbuf[row_len * hex_len + 1];
435 char cbuf[row_len + strlen("|") + 1];
437 for (j = 0; (j < row_len) && ((i+j) < len); j++) {
438 char c = ((const char *)buf)[i + j];
440 snprintf(hbuf + j * hex_len, hex_len + 1, "%02x ", c);
445 snprintf(cbuf + j, hex_len + 1, "%c", c);
450 fprintf(stderr, "%08x %*s |%s\n", i,
451 0 - (int)sizeof(hbuf) + 1, hbuf, cbuf);
455 void __check_file_contents(struct parser_test *test,
456 struct discover_device *dev, const char *filename,
457 const char *buf, int len,
458 const char *srcfile, int srcline)
460 struct test_file *f, *file = NULL;
462 list_for_each_entry(&test->files, f, list) {
465 if (strcmp(f->name, filename))
473 errx(EXIT_FAILURE, "%s:%d: File '%s' not found",
474 srcfile, srcline, filename);
476 if (file->size != len || memcmp(file->data, buf, len)) {
477 fprintf(stderr, "%s:%d: File '%s' data/size mismatch\n",
478 srcfile, srcline, filename);
479 fprintf(stderr, "Expected:\n");
480 dump_file_data(buf, len);
481 fprintf(stderr, "Got:\n");
482 dump_file_data(file->data, file->size);