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 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
147 struct discover_boot_option *opt;
150 list_for_each_entry(&ctx->boot_options, opt, list) {
160 void __check_boot_option_count(struct discover_context *ctx, int count,
161 const char *file, int line)
163 struct discover_boot_option *opt;
166 list_for_each_entry(&ctx->boot_options, opt, list)
172 fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
173 fprintf(stderr, "expected %d options, got %d:\n", count, i);
176 list_for_each_entry(&ctx->boot_options, opt, list)
177 fprintf(stderr, " %2d: %s [%s]\n", i++, opt->option->name,
183 void __check_args(struct discover_boot_option *opt, const char *args,
184 const char *file, int line)
188 if (!opt->option->boot_args) {
189 fprintf(stderr, "%s%d: arg check failed\n", file, line);
190 fprintf(stderr, " no arguments parsed\n");
191 fprintf(stderr, " expected '%s'\n", args);
195 rc = strcmp(opt->option->boot_args, args);
197 fprintf(stderr, "%s%d: arg check failed\n", file, line);
198 fprintf(stderr, " got '%s'\n", opt->option->boot_args);
199 fprintf(stderr, " expected '%s'\n", args);