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 discover_context *ctx)
49 sprintf(name, "__test%d", dev_idx++);
51 return test_create_device(ctx, name);
54 struct discover_device *test_create_device(struct discover_context *ctx,
57 struct discover_device *dev;
59 dev = talloc_zero(ctx, struct discover_device);
60 dev->device = talloc_zero(dev, struct device);
62 list_init(&dev->boot_options);
64 dev->device->id = talloc_strdup(dev, name);
65 dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
66 dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
71 static struct discover_context *test_create_context(struct parser_test *test)
73 struct discover_context *ctx;
75 ctx = talloc_zero(test, struct discover_context);
78 list_init(&ctx->boot_options);
79 ctx->device = test_create_device_simple(ctx);
84 struct parser_test *test_init(void)
86 struct parser_test *test;
88 test = talloc_zero(NULL, struct parser_test);
89 test->handler = device_handler_init(NULL, NULL, 0);
90 test->ctx = test_create_context(test);
95 void test_fini(struct parser_test *test)
97 device_handler_destroy(test->handler);
101 void __test_read_conf_data(struct parser_test *test,
102 const char *buf, size_t len)
104 test->conf.size = len;
105 test->conf.buf = talloc_memdup(test, buf, len);
108 void test_read_conf_file(struct parser_test *test, const char *filename)
114 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
116 fd = open(path, O_RDONLY);
118 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
120 rc = fstat(fd, &stat);
124 test->conf.size = stat.st_size;
125 test->conf.buf = talloc_array(test, char, test->conf.size + 1);
127 rc = read(fd, test->conf.buf, test->conf.size);
128 assert(rc == (ssize_t)test->conf.size);
130 *(char *)(test->conf.buf + test->conf.size) = '\0';
136 int test_run_parser(struct parser_test *test, const char *parser_name)
140 list_for_each_entry(&parsers, i, list) {
141 if (strcmp(i->parser->name, parser_name))
143 test->ctx->parser = i->parser;
144 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
147 errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
150 bool resource_resolve(struct device_handler *handler, struct parser *parser,
151 struct resource *resource)
155 if (resource->resolved)
159 assert(parser->resolve_resource);
161 return parser->resolve_resource(handler, resource);
164 void boot_option_resolve(struct device_handler *handler,
165 struct discover_boot_option *opt)
167 resource_resolve(handler, opt->source, opt->boot_image);
168 resource_resolve(handler, opt->source, opt->initrd);
169 resource_resolve(handler, opt->source, opt->icon);
172 extern void device_handler_add_device(struct device_handler *handler,
173 struct discover_device *dev);
175 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
177 struct discover_boot_option *opt;
179 device_handler_add_device(test->handler, dev);
181 list_for_each_entry(&test->ctx->boot_options, opt, list)
182 boot_option_resolve(test->handler, opt);
185 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
188 struct discover_boot_option *opt;
191 list_for_each_entry(&ctx->boot_options, opt, list) {
201 void __check_boot_option_count(struct discover_context *ctx, int count,
202 const char *file, int line)
204 struct discover_boot_option *opt;
207 list_for_each_entry(&ctx->boot_options, opt, list)
213 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
214 fprintf(stderr, "expected %d options, got %d:\n", count, i);
217 list_for_each_entry(&ctx->boot_options, opt, list)
218 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
224 void __check_args(struct discover_boot_option *opt, const char *args,
225 const char *file, int line)
229 if (!opt->option->boot_args) {
230 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
231 fprintf(stderr, " no arguments parsed\n");
232 fprintf(stderr, " expected '%s'\n", args);
236 rc = strcmp(opt->option->boot_args, args);
238 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
239 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
240 fprintf(stderr, " expected '%s'\n", args);
245 void __check_name(struct discover_boot_option *opt, const char *name,
246 const char *file, int line)
250 rc = strcmp(opt->option->name, name);
252 fprintf(stderr, "%s:%d: name check failed\n", file, line);
253 fprintf(stderr, " got '%s'\n", opt->option->name);
254 fprintf(stderr, " expected '%s'\n", name);
259 void __check_resolved_local_resource(struct resource *res,
260 struct discover_device *dev, const char *local_path,
261 const char *file, int line)
263 const char *exp_url, *got_url;
266 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
269 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
272 exp_url = talloc_asprintf(res, "file://%s%s",
273 dev->mount_path, local_path);
274 got_url = pb_url_to_string(res->url);
276 if (strcmp(got_url, exp_url)) {
277 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
278 fprintf(stderr, " got '%s'\n", got_url);
279 fprintf(stderr, " expected '%s'\n", exp_url);
284 void __check_unresolved_resource(struct resource *res,
285 const char *file, int line)
288 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
291 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
294 void __check_not_present_resource(struct resource *res,
295 const char *file, int line)
298 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);