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);
68 static struct discover_context *test_create_context(struct parser_test *test)
70 struct discover_context *ctx;
72 ctx = talloc_zero(test, struct discover_context);
75 list_init(&ctx->boot_options);
76 ctx->device = test_create_device_simple(test);
77 device_handler_add_device(test->handler, ctx->device);
82 extern struct config *test_config_init(struct parser_test *test);
84 struct parser_test *test_init(void)
86 struct parser_test *test;
88 test = talloc_zero(NULL, struct parser_test);
89 test->config = test_config_init(test);
90 test->handler = device_handler_init(NULL, NULL, 0);
91 test->ctx = test_create_context(test);
96 void test_fini(struct parser_test *test)
98 device_handler_destroy(test->handler);
102 void __test_read_conf_data(struct parser_test *test,
103 const char *buf, size_t len)
105 test->conf.size = len;
106 test->conf.buf = talloc_memdup(test, buf, len);
109 void test_read_conf_file(struct parser_test *test, const char *filename)
115 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
117 fd = open(path, O_RDONLY);
119 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
121 rc = fstat(fd, &stat);
125 test->conf.size = stat.st_size;
126 test->conf.buf = talloc_array(test, char, test->conf.size + 1);
128 rc = read(fd, test->conf.buf, test->conf.size);
129 assert(rc == (ssize_t)test->conf.size);
131 *(char *)(test->conf.buf + test->conf.size) = '\0';
137 void test_set_conf_source(struct parser_test *test, const char *url)
139 test->ctx->conf_url = pb_url_parse(test, url);
140 assert(test->ctx->conf_url);
143 int test_run_parser(struct parser_test *test, const char *parser_name)
147 list_for_each_entry(&parsers, i, list) {
148 if (strcmp(i->parser->name, parser_name))
150 test->ctx->parser = i->parser;
151 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
154 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
157 bool resource_resolve(struct device_handler *handler, struct parser *parser,
158 struct resource *resource)
162 if (resource->resolved)
166 assert(parser->resolve_resource);
168 return parser->resolve_resource(handler, resource);
171 void boot_option_resolve(struct device_handler *handler,
172 struct discover_boot_option *opt)
174 resource_resolve(handler, opt->source, opt->boot_image);
175 resource_resolve(handler, opt->source, opt->initrd);
176 resource_resolve(handler, opt->source, opt->icon);
179 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
181 struct discover_boot_option *opt;
183 device_handler_add_device(test->handler, dev);
185 list_for_each_entry(&test->ctx->boot_options, opt, list)
186 boot_option_resolve(test->handler, opt);
189 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
192 struct discover_boot_option *opt;
195 list_for_each_entry(&ctx->boot_options, opt, list) {
205 void __check_boot_option_count(struct discover_context *ctx, int count,
206 const char *file, int line)
208 struct discover_boot_option *opt;
209 int defaults = 0, i = 0;
211 list_for_each_entry(&ctx->boot_options, opt, list) {
213 if (opt->option->is_default)
218 fprintf(stderr, "%s:%d: parser returned multiple default "
219 "options\n", file, line);
226 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
227 fprintf(stderr, "expected %d options, got %d:\n", count, i);
230 list_for_each_entry(&ctx->boot_options, opt, list)
231 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
237 void __check_args(struct discover_boot_option *opt, const char *args,
238 const char *file, int line)
242 if (!opt->option->boot_args) {
243 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
244 fprintf(stderr, " no arguments parsed\n");
245 fprintf(stderr, " expected '%s'\n", args);
249 rc = strcmp(opt->option->boot_args, args);
251 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
252 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
253 fprintf(stderr, " expected '%s'\n", args);
258 void __check_name(struct discover_boot_option *opt, const char *name,
259 const char *file, int line)
263 rc = strcmp(opt->option->name, name);
265 fprintf(stderr, "%s:%d: name check failed\n", file, line);
266 fprintf(stderr, " got '%s'\n", opt->option->name);
267 fprintf(stderr, " expected '%s'\n", name);
272 void __check_is_default(struct discover_boot_option *opt,
273 const char *file, int line)
275 if (opt->option->is_default)
278 fprintf(stderr, "%s:%d: default check failed\n", file, line);
282 void __check_resolved_local_resource(struct resource *res,
283 struct discover_device *dev, const char *local_path,
284 const char *file, int line)
286 const char *exp_url, *got_url;
289 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
292 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
295 exp_url = talloc_asprintf(res, "file://%s%s",
296 dev->mount_path, local_path);
297 got_url = pb_url_to_string(res->url);
299 if (strcmp(got_url, exp_url)) {
300 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
301 fprintf(stderr, " got '%s'\n", got_url);
302 fprintf(stderr, " expected '%s'\n", exp_url);
307 void __check_resolved_url_resource(struct resource *res,
308 const char *url, const char *file, int line)
313 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
316 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
319 res_url = pb_url_to_string(res->url);
320 if (strcmp(url, res_url)) {
321 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
322 fprintf(stderr, " got '%s'\n", res_url);
323 fprintf(stderr, " expected '%s'\n", url);
327 void __check_unresolved_resource(struct resource *res,
328 const char *file, int line)
331 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
334 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
337 void __check_not_present_resource(struct resource *res,
338 const char *file, int line)
341 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);