10 #include <talloc/talloc.h>
11 #include <types/types.h>
14 #include "device-handler.h"
18 #include "parser-test.h"
21 struct list_item list;
22 struct parser *parser;
26 struct discover_device *dev;
30 struct list_item list;
35 void __register_parser(struct parser *parser)
37 struct p_item* i = talloc(NULL, struct p_item);
40 list_add(&parsers, &i->list);
43 static void __attribute__((destructor)) __cleanup_parsers(void)
45 struct p_item *item, *tmp;
47 list_for_each_entry_safe(&parsers, item, tmp, list)
51 static struct discover_device *test_create_device_simple(
52 struct parser_test *test)
57 sprintf(name, "__test%d", dev_idx++);
59 return test_create_device(test, name);
62 struct discover_device *test_create_device(struct parser_test *test,
65 struct discover_device *dev;
67 dev = discover_device_create(test->handler, name);
69 dev->device->id = talloc_strdup(dev, name);
70 dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
71 dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
77 static struct discover_context *test_create_context(struct parser_test *test)
79 struct discover_context *ctx;
81 ctx = talloc_zero(test, struct discover_context);
84 list_init(&ctx->boot_options);
85 ctx->device = test_create_device_simple(test);
86 ctx->test_data = test;
87 device_handler_add_device(test->handler, ctx->device);
92 extern struct config *test_config_init(struct parser_test *test);
94 struct parser_test *test_init(void)
96 struct parser_test *test;
98 test = talloc_zero(NULL, struct parser_test);
99 test->config = test_config_init(test);
100 test->handler = device_handler_init(NULL, NULL, 0);
101 test->ctx = test_create_context(test);
102 list_init(&test->files);
107 void test_fini(struct parser_test *test)
109 device_handler_destroy(test->handler);
113 void __test_read_conf_data(struct parser_test *test,
114 const char *buf, size_t len)
116 test->conf.size = len;
117 test->conf.buf = talloc_memdup(test, buf, len);
120 void test_read_conf_file(struct parser_test *test, const char *filename)
126 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
128 fd = open(path, O_RDONLY);
130 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
132 rc = fstat(fd, &stat);
136 test->conf.size = stat.st_size;
137 test->conf.buf = talloc_array(test, char, test->conf.size + 1);
139 rc = read(fd, test->conf.buf, test->conf.size);
140 assert(rc == (ssize_t)test->conf.size);
142 *(char *)(test->conf.buf + test->conf.size) = '\0';
148 void test_set_conf_source(struct parser_test *test, const char *url)
150 test->ctx->conf_url = pb_url_parse(test, url);
151 assert(test->ctx->conf_url);
154 void test_add_file_data(struct parser_test *test, struct discover_device *dev,
155 const char *filename, void *data, int size)
157 struct test_file *file;
159 file = talloc_zero(test, struct test_file);
161 file->name = filename;
164 list_add(&test->files, &file->list);
168 int parser_request_file(struct discover_context *ctx,
169 struct discover_device *dev, const char *filename,
170 char **buf, int *len)
172 struct parser_test *test = ctx->test_data;
173 struct test_file *file;
175 list_for_each_entry(&test->files, file, list) {
176 if (file->dev != dev)
178 if (strcmp(file->name, filename))
181 *buf = talloc_memdup(test, file->data, file->size);
189 int parser_replace_file(struct discover_context *ctx,
190 struct discover_device *dev, const char *filename,
193 struct parser_test *test = ctx->test_data;
194 struct test_file *f, *file;
196 list_for_each_entry(&test->files, f, list) {
199 if (strcmp(f->name, filename))
207 file = talloc_zero(test, struct test_file);
209 file->name = filename;
210 list_add(&test->files, &file->list);
212 talloc_free(file->data);
215 file->data = talloc_memdup(test, buf, len);
219 int test_run_parser(struct parser_test *test, const char *parser_name)
223 list_for_each_entry(&parsers, i, list) {
224 if (strcmp(i->parser->name, parser_name))
226 test->ctx->parser = i->parser;
227 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
230 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
233 bool resource_resolve(struct device_handler *handler, struct parser *parser,
234 struct resource *resource)
238 if (resource->resolved)
242 assert(parser->resolve_resource);
244 return parser->resolve_resource(handler, resource);
247 void boot_option_resolve(struct device_handler *handler,
248 struct discover_boot_option *opt)
250 resource_resolve(handler, opt->source, opt->boot_image);
251 resource_resolve(handler, opt->source, opt->initrd);
252 resource_resolve(handler, opt->source, opt->icon);
255 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
257 struct discover_boot_option *opt;
259 device_handler_add_device(test->handler, dev);
261 list_for_each_entry(&test->ctx->boot_options, opt, list)
262 boot_option_resolve(test->handler, opt);
265 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
268 struct discover_boot_option *opt;
271 list_for_each_entry(&ctx->boot_options, opt, list) {
281 void __check_boot_option_count(struct discover_context *ctx, int count,
282 const char *file, int line)
284 struct discover_boot_option *opt;
285 int defaults = 0, i = 0;
287 list_for_each_entry(&ctx->boot_options, opt, list) {
289 if (opt->option->is_default)
294 fprintf(stderr, "%s:%d: parser returned multiple default "
295 "options\n", file, line);
302 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
303 fprintf(stderr, "expected %d options, got %d:\n", count, i);
306 list_for_each_entry(&ctx->boot_options, opt, list)
307 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
313 void __check_args(struct discover_boot_option *opt, const char *args,
314 const char *file, int line)
318 if (!opt->option->boot_args && !args)
321 if (!opt->option->boot_args) {
322 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
323 fprintf(stderr, " no arguments parsed\n");
324 fprintf(stderr, " expected '%s'\n", args);
328 rc = strcmp(opt->option->boot_args, args);
330 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
331 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
332 fprintf(stderr, " expected '%s'\n", args);
337 void __check_name(struct discover_boot_option *opt, const char *name,
338 const char *file, int line)
342 rc = strcmp(opt->option->name, name);
344 fprintf(stderr, "%s:%d: name check failed\n", file, line);
345 fprintf(stderr, " got '%s'\n", opt->option->name);
346 fprintf(stderr, " expected '%s'\n", name);
351 void __check_is_default(struct discover_boot_option *opt,
352 const char *file, int line)
354 if (opt->option->is_default)
357 fprintf(stderr, "%s:%d: default check failed\n", file, line);
361 void __check_resolved_local_resource(struct resource *res,
362 struct discover_device *dev, const char *local_path,
363 const char *file, int line)
365 const char *exp_url, *got_url;
368 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
371 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
374 exp_url = talloc_asprintf(res, "file://%s%s",
375 dev->mount_path, local_path);
376 got_url = pb_url_to_string(res->url);
378 if (strcmp(got_url, exp_url)) {
379 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
380 fprintf(stderr, " got '%s'\n", got_url);
381 fprintf(stderr, " expected '%s'\n", exp_url);
386 void __check_resolved_url_resource(struct resource *res,
387 const char *url, const char *file, int line)
392 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
395 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
398 res_url = pb_url_to_string(res->url);
399 if (strcmp(url, res_url)) {
400 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
401 fprintf(stderr, " got '%s'\n", res_url);
402 fprintf(stderr, " expected '%s'\n", url);
406 void __check_unresolved_resource(struct resource *res,
407 const char *file, int line)
410 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
413 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
416 void __check_not_present_resource(struct resource *res,
417 const char *file, int line)
420 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);