10 #include <talloc/talloc.h>
11 #include <types/types.h>
14 #include "device-handler.h"
18 #include "parser-test.h"
21 static struct parser **parsers;
23 void __register_parser(struct parser *parser)
25 parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1);
26 parsers[n_parsers] = parser;
30 static struct discover_device *test_create_device_simple(
31 struct discover_context *ctx)
36 sprintf(name, "__test%d", dev_idx++);
38 return test_create_device(ctx, name);
41 struct discover_device *test_create_device(struct discover_context *ctx,
44 struct discover_device *dev;
46 dev = talloc_zero(ctx, struct discover_device);
47 dev->device = talloc_zero(dev, struct device);
49 list_init(&dev->boot_options);
51 dev->device->id = talloc_strdup(dev, name);
52 dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
53 dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
58 static struct discover_context *test_create_context(struct parser_test *test)
60 struct discover_context *ctx;
62 ctx = talloc_zero(test, struct discover_context);
65 list_init(&ctx->boot_options);
66 ctx->device = test_create_device_simple(ctx);
71 struct parser_test *test_init(void)
73 struct parser_test *test;
75 test = talloc_zero(NULL, struct parser_test);
76 test->handler = device_handler_init(NULL, 0);
77 test->ctx = test_create_context(test);
82 void test_fini(struct parser_test *test)
84 device_handler_destroy(test->handler);
88 void __test_read_conf_data(struct parser_test *test,
89 const char *buf, size_t len)
91 test->conf.size = len;
92 test->conf.buf = talloc_memdup(test, buf, len);
95 void test_read_conf_file(struct parser_test *test, const char *filename)
101 path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
103 fd = open(path, O_RDONLY);
105 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
107 rc = fstat(fd, &stat);
111 test->conf.size = stat.st_size;
112 test->conf.buf = talloc_array(test, char, test->conf.size + 1);
114 rc = read(fd, test->conf.buf, test->conf.size);
115 assert(rc == (ssize_t)test->conf.size);
117 *(char *)(test->conf.buf + test->conf.size) = '\0';
123 int test_run_parser(struct parser_test *test, const char *parser_name)
125 struct parser *parser;
128 for (i = 0; i < n_parsers; i++) {
130 if (strcmp(parser->name, parser_name))
132 test->ctx->parser = parser;
133 rc = parser->parse(test->ctx, test->conf.buf, test->conf.size);
138 errx(EXIT_FAILURE, "%s: parser '%s' not found",
139 __func__, parser_name);
144 bool resource_resolve(struct device_handler *handler, struct parser *parser,
145 struct resource *resource)
149 if (resource->resolved)
153 assert(parser->resolve_resource);
155 return parser->resolve_resource(handler, resource);
158 void boot_option_resolve(struct device_handler *handler,
159 struct discover_boot_option *opt)
161 resource_resolve(handler, opt->source, opt->boot_image);
162 resource_resolve(handler, opt->source, opt->initrd);
163 resource_resolve(handler, opt->source, opt->icon);
166 extern void device_handler_add_device(struct device_handler *handler,
167 struct discover_device *dev);
169 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
171 struct discover_boot_option *opt;
173 device_handler_add_device(test->handler, dev);
175 list_for_each_entry(&test->ctx->boot_options, opt, list)
176 boot_option_resolve(test->handler, opt);
179 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
182 struct discover_boot_option *opt;
185 list_for_each_entry(&ctx->boot_options, opt, list) {
195 void __check_boot_option_count(struct discover_context *ctx, int count,
196 const char *file, int line)
198 struct discover_boot_option *opt;
201 list_for_each_entry(&ctx->boot_options, opt, list)
207 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
208 fprintf(stderr, "expected %d options, got %d:\n", count, i);
211 list_for_each_entry(&ctx->boot_options, opt, list)
212 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
218 void __check_args(struct discover_boot_option *opt, const char *args,
219 const char *file, int line)
223 if (!opt->option->boot_args) {
224 fprintf(stderr, "%s%d: arg check failed\n", file, line);
225 fprintf(stderr, " no arguments parsed\n");
226 fprintf(stderr, " expected '%s'\n", args);
230 rc = strcmp(opt->option->boot_args, args);
232 fprintf(stderr, "%s%d: arg check failed\n", file, line);
233 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
234 fprintf(stderr, " expected '%s'\n", args);
239 void __check_name(struct discover_boot_option *opt, const char *name,
240 const char *file, int line)
244 rc = strcmp(opt->option->name, name);
246 fprintf(stderr, "%s%d: name check failed\n", file, line);
247 fprintf(stderr, " got '%s'\n", opt->option->name);
248 fprintf(stderr, " expected '%s'\n", name);
253 void __check_resolved_local_resource(struct resource *res,
254 struct discover_device *dev, const char *local_path,
255 const char *file, int line)
257 const char *exp_url, *got_url;
260 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
263 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
266 exp_url = talloc_asprintf(res, "file://%s%s",
267 dev->mount_path, local_path);
268 got_url = pb_url_to_string(res->url);
270 if (strcmp(got_url, exp_url)) {
272 "%s:%d Resource mismatch: got %s, expected %s",
273 file, line, got_url, exp_url);
277 void __check_unresolved_resource(struct resource *res,
278 const char *file, int line)
281 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
284 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);