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 test_run_parser(struct parser_test *test, const char *parser_name)
193 list_for_each_entry(&parsers, i, list) {
194 if (strcmp(i->parser->name, parser_name))
196 test->ctx->parser = i->parser;
197 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
200 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
203 bool resource_resolve(struct device_handler *handler, struct parser *parser,
204 struct resource *resource)
208 if (resource->resolved)
212 assert(parser->resolve_resource);
214 return parser->resolve_resource(handler, resource);
217 void boot_option_resolve(struct device_handler *handler,
218 struct discover_boot_option *opt)
220 resource_resolve(handler, opt->source, opt->boot_image);
221 resource_resolve(handler, opt->source, opt->initrd);
222 resource_resolve(handler, opt->source, opt->icon);
225 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
227 struct discover_boot_option *opt;
229 device_handler_add_device(test->handler, dev);
231 list_for_each_entry(&test->ctx->boot_options, opt, list)
232 boot_option_resolve(test->handler, opt);
235 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
238 struct discover_boot_option *opt;
241 list_for_each_entry(&ctx->boot_options, opt, list) {
251 void __check_boot_option_count(struct discover_context *ctx, int count,
252 const char *file, int line)
254 struct discover_boot_option *opt;
255 int defaults = 0, i = 0;
257 list_for_each_entry(&ctx->boot_options, opt, list) {
259 if (opt->option->is_default)
264 fprintf(stderr, "%s:%d: parser returned multiple default "
265 "options\n", file, line);
272 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
273 fprintf(stderr, "expected %d options, got %d:\n", count, i);
276 list_for_each_entry(&ctx->boot_options, opt, list)
277 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
283 void __check_args(struct discover_boot_option *opt, const char *args,
284 const char *file, int line)
288 if (!opt->option->boot_args && !args)
291 if (!opt->option->boot_args) {
292 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
293 fprintf(stderr, " no arguments parsed\n");
294 fprintf(stderr, " expected '%s'\n", args);
298 rc = strcmp(opt->option->boot_args, args);
300 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
301 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
302 fprintf(stderr, " expected '%s'\n", args);
307 void __check_name(struct discover_boot_option *opt, const char *name,
308 const char *file, int line)
312 rc = strcmp(opt->option->name, name);
314 fprintf(stderr, "%s:%d: name check failed\n", file, line);
315 fprintf(stderr, " got '%s'\n", opt->option->name);
316 fprintf(stderr, " expected '%s'\n", name);
321 void __check_is_default(struct discover_boot_option *opt,
322 const char *file, int line)
324 if (opt->option->is_default)
327 fprintf(stderr, "%s:%d: default check failed\n", file, line);
331 void __check_resolved_local_resource(struct resource *res,
332 struct discover_device *dev, const char *local_path,
333 const char *file, int line)
335 const char *exp_url, *got_url;
338 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
341 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
344 exp_url = talloc_asprintf(res, "file://%s%s",
345 dev->mount_path, local_path);
346 got_url = pb_url_to_string(res->url);
348 if (strcmp(got_url, exp_url)) {
349 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
350 fprintf(stderr, " got '%s'\n", got_url);
351 fprintf(stderr, " expected '%s'\n", exp_url);
356 void __check_resolved_url_resource(struct resource *res,
357 const char *url, const char *file, int line)
362 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
365 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
368 res_url = pb_url_to_string(res->url);
369 if (strcmp(url, res_url)) {
370 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
371 fprintf(stderr, " got '%s'\n", res_url);
372 fprintf(stderr, " expected '%s'\n", url);
376 void __check_unresolved_resource(struct resource *res,
377 const char *file, int line)
380 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
383 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
386 void __check_not_present_resource(struct resource *res,
387 const char *file, int line)
390 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);