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 device_handler_add_device(test->handler, ctx->device);
83 extern struct config *test_config_init(struct parser_test *test);
85 struct parser_test *test_init(void)
87 struct parser_test *test;
89 test = talloc_zero(NULL, struct parser_test);
90 test->config = test_config_init(test);
91 test->handler = device_handler_init(NULL, NULL, 0);
92 test->ctx = test_create_context(test);
97 void test_fini(struct parser_test *test)
99 device_handler_destroy(test->handler);
103 void __test_read_conf_data(struct parser_test *test,
104 const char *buf, size_t len)
106 test->conf.size = len;
107 test->conf.buf = talloc_memdup(test, buf, len);
110 void test_read_conf_file(struct parser_test *test, const char *filename)
116 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
118 fd = open(path, O_RDONLY);
120 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
122 rc = fstat(fd, &stat);
126 test->conf.size = stat.st_size;
127 test->conf.buf = talloc_array(test, char, test->conf.size + 1);
129 rc = read(fd, test->conf.buf, test->conf.size);
130 assert(rc == (ssize_t)test->conf.size);
132 *(char *)(test->conf.buf + test->conf.size) = '\0';
138 void test_set_conf_source(struct parser_test *test, const char *url)
140 test->ctx->conf_url = pb_url_parse(test, url);
141 assert(test->ctx->conf_url);
144 int test_run_parser(struct parser_test *test, const char *parser_name)
148 list_for_each_entry(&parsers, i, list) {
149 if (strcmp(i->parser->name, parser_name))
151 test->ctx->parser = i->parser;
152 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
155 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
158 bool resource_resolve(struct device_handler *handler, struct parser *parser,
159 struct resource *resource)
163 if (resource->resolved)
167 assert(parser->resolve_resource);
169 return parser->resolve_resource(handler, resource);
172 void boot_option_resolve(struct device_handler *handler,
173 struct discover_boot_option *opt)
175 resource_resolve(handler, opt->source, opt->boot_image);
176 resource_resolve(handler, opt->source, opt->initrd);
177 resource_resolve(handler, opt->source, opt->icon);
180 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
182 struct discover_boot_option *opt;
184 device_handler_add_device(test->handler, dev);
186 list_for_each_entry(&test->ctx->boot_options, opt, list)
187 boot_option_resolve(test->handler, opt);
190 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
193 struct discover_boot_option *opt;
196 list_for_each_entry(&ctx->boot_options, opt, list) {
206 void __check_boot_option_count(struct discover_context *ctx, int count,
207 const char *file, int line)
209 struct discover_boot_option *opt;
210 int defaults = 0, i = 0;
212 list_for_each_entry(&ctx->boot_options, opt, list) {
214 if (opt->option->is_default)
219 fprintf(stderr, "%s:%d: parser returned multiple default "
220 "options\n", file, line);
227 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
228 fprintf(stderr, "expected %d options, got %d:\n", count, i);
231 list_for_each_entry(&ctx->boot_options, opt, list)
232 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
238 void __check_args(struct discover_boot_option *opt, const char *args,
239 const char *file, int line)
243 if (!opt->option->boot_args && !args)
246 if (!opt->option->boot_args) {
247 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
248 fprintf(stderr, " no arguments parsed\n");
249 fprintf(stderr, " expected '%s'\n", args);
253 rc = strcmp(opt->option->boot_args, args);
255 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
256 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
257 fprintf(stderr, " expected '%s'\n", args);
262 void __check_name(struct discover_boot_option *opt, const char *name,
263 const char *file, int line)
267 rc = strcmp(opt->option->name, name);
269 fprintf(stderr, "%s:%d: name check failed\n", file, line);
270 fprintf(stderr, " got '%s'\n", opt->option->name);
271 fprintf(stderr, " expected '%s'\n", name);
276 void __check_is_default(struct discover_boot_option *opt,
277 const char *file, int line)
279 if (opt->option->is_default)
282 fprintf(stderr, "%s:%d: default check failed\n", file, line);
286 void __check_resolved_local_resource(struct resource *res,
287 struct discover_device *dev, const char *local_path,
288 const char *file, int line)
290 const char *exp_url, *got_url;
293 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
296 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
299 exp_url = talloc_asprintf(res, "file://%s%s",
300 dev->mount_path, local_path);
301 got_url = pb_url_to_string(res->url);
303 if (strcmp(got_url, exp_url)) {
304 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
305 fprintf(stderr, " got '%s'\n", got_url);
306 fprintf(stderr, " expected '%s'\n", exp_url);
311 void __check_resolved_url_resource(struct resource *res,
312 const char *url, const char *file, int line)
317 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
320 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
323 res_url = pb_url_to_string(res->url);
324 if (strcmp(url, res_url)) {
325 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
326 fprintf(stderr, " got '%s'\n", res_url);
327 fprintf(stderr, " expected '%s'\n", url);
331 void __check_unresolved_resource(struct resource *res,
332 const char *file, int line)
335 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
338 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
341 void __check_not_present_resource(struct resource *res,
342 const char *file, int line)
345 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);