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;
27 void __register_parser(struct parser *parser)
29 struct p_item* i = talloc(NULL, struct p_item);
32 list_add(&parsers, &i->list);
35 static void __attribute__((destructor)) __cleanup_parsers(void)
37 struct p_item *item, *tmp;
39 list_for_each_entry_safe(&parsers, item, tmp, list)
43 static struct discover_device *test_create_device_simple(
44 struct parser_test *test)
49 sprintf(name, "__test%d", dev_idx++);
51 return test_create_device(test, name);
54 struct discover_device *test_create_device(struct parser_test *test,
57 struct discover_device *dev;
59 dev = discover_device_create(test->handler, name);
61 dev->device->id = talloc_strdup(dev, name);
62 dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
63 dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
69 static struct discover_context *test_create_context(struct parser_test *test)
71 struct discover_context *ctx;
73 ctx = talloc_zero(test, struct discover_context);
76 list_init(&ctx->boot_options);
77 ctx->device = test_create_device_simple(test);
78 ctx->test_data = test;
79 device_handler_add_device(test->handler, ctx->device);
84 extern struct config *test_config_init(struct parser_test *test);
86 struct parser_test *test_init(void)
88 struct parser_test *test;
90 test = talloc_zero(NULL, struct parser_test);
91 test->config = test_config_init(test);
92 test->handler = device_handler_init(NULL, NULL, 0);
93 test->ctx = test_create_context(test);
98 void test_fini(struct parser_test *test)
100 device_handler_destroy(test->handler);
104 void __test_read_conf_data(struct parser_test *test,
105 const char *buf, size_t len)
107 test->conf.size = len;
108 test->conf.buf = talloc_memdup(test, buf, len);
111 void test_read_conf_file(struct parser_test *test, const char *filename)
117 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
119 fd = open(path, O_RDONLY);
121 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
123 rc = fstat(fd, &stat);
127 test->conf.size = stat.st_size;
128 test->conf.buf = talloc_array(test, char, test->conf.size + 1);
130 rc = read(fd, test->conf.buf, test->conf.size);
131 assert(rc == (ssize_t)test->conf.size);
133 *(char *)(test->conf.buf + test->conf.size) = '\0';
139 void test_set_conf_source(struct parser_test *test, const char *url)
141 test->ctx->conf_url = pb_url_parse(test, url);
142 assert(test->ctx->conf_url);
145 int test_run_parser(struct parser_test *test, const char *parser_name)
149 list_for_each_entry(&parsers, i, list) {
150 if (strcmp(i->parser->name, parser_name))
152 test->ctx->parser = i->parser;
153 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
156 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
159 bool resource_resolve(struct device_handler *handler, struct parser *parser,
160 struct resource *resource)
164 if (resource->resolved)
168 assert(parser->resolve_resource);
170 return parser->resolve_resource(handler, resource);
173 void boot_option_resolve(struct device_handler *handler,
174 struct discover_boot_option *opt)
176 resource_resolve(handler, opt->source, opt->boot_image);
177 resource_resolve(handler, opt->source, opt->initrd);
178 resource_resolve(handler, opt->source, opt->icon);
181 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
183 struct discover_boot_option *opt;
185 device_handler_add_device(test->handler, dev);
187 list_for_each_entry(&test->ctx->boot_options, opt, list)
188 boot_option_resolve(test->handler, opt);
191 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
194 struct discover_boot_option *opt;
197 list_for_each_entry(&ctx->boot_options, opt, list) {
207 void __check_boot_option_count(struct discover_context *ctx, int count,
208 const char *file, int line)
210 struct discover_boot_option *opt;
211 int defaults = 0, i = 0;
213 list_for_each_entry(&ctx->boot_options, opt, list) {
215 if (opt->option->is_default)
220 fprintf(stderr, "%s:%d: parser returned multiple default "
221 "options\n", file, line);
228 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
229 fprintf(stderr, "expected %d options, got %d:\n", count, i);
232 list_for_each_entry(&ctx->boot_options, opt, list)
233 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
239 void __check_args(struct discover_boot_option *opt, const char *args,
240 const char *file, int line)
244 if (!opt->option->boot_args && !args)
247 if (!opt->option->boot_args) {
248 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
249 fprintf(stderr, " no arguments parsed\n");
250 fprintf(stderr, " expected '%s'\n", args);
254 rc = strcmp(opt->option->boot_args, args);
256 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
257 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
258 fprintf(stderr, " expected '%s'\n", args);
263 void __check_name(struct discover_boot_option *opt, const char *name,
264 const char *file, int line)
268 rc = strcmp(opt->option->name, name);
270 fprintf(stderr, "%s:%d: name check failed\n", file, line);
271 fprintf(stderr, " got '%s'\n", opt->option->name);
272 fprintf(stderr, " expected '%s'\n", name);
277 void __check_is_default(struct discover_boot_option *opt,
278 const char *file, int line)
280 if (opt->option->is_default)
283 fprintf(stderr, "%s:%d: default check failed\n", file, line);
287 void __check_resolved_local_resource(struct resource *res,
288 struct discover_device *dev, const char *local_path,
289 const char *file, int line)
291 const char *exp_url, *got_url;
294 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
297 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
300 exp_url = talloc_asprintf(res, "file://%s%s",
301 dev->mount_path, local_path);
302 got_url = pb_url_to_string(res->url);
304 if (strcmp(got_url, exp_url)) {
305 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
306 fprintf(stderr, " got '%s'\n", got_url);
307 fprintf(stderr, " expected '%s'\n", exp_url);
312 void __check_resolved_url_resource(struct resource *res,
313 const char *url, const char *file, int line)
318 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
321 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
324 res_url = pb_url_to_string(res->url);
325 if (strcmp(url, res_url)) {
326 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
327 fprintf(stderr, " got '%s'\n", res_url);
328 fprintf(stderr, " expected '%s'\n", url);
332 void __check_unresolved_resource(struct resource *res,
333 const char *file, int line)
336 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
339 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
342 void __check_not_present_resource(struct resource *res,
343 const char *file, int line)
346 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);