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);