]> git.ozlabs.org Git - petitboot/blob - test/parser/utils.c
16c415a1ebd7477f27baa2fcfbd96cf5362fab0a
[petitboot] / test / parser / utils.c
1
2 #include <assert.h>
3 #include <err.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9
10 #include <talloc/talloc.h>
11 #include <types/types.h>
12 #include <url/url.h>
13
14 #include "device-handler.h"
15 #include "parser.h"
16 #include "resource.h"
17
18 #include "parser-test.h"
19
20 static int n_parsers;
21 static struct parser **parsers;
22
23 void __register_parser(struct parser *parser)
24 {
25         parsers = talloc_realloc(NULL, parsers, struct parser *, n_parsers + 1);
26         parsers[n_parsers] = parser;
27         n_parsers++;
28 }
29
30 static struct discover_device *test_create_device_simple(
31                 struct discover_context *ctx)
32 {
33         static int dev_idx;
34         char name[10];
35
36         sprintf(name, "__test%d", dev_idx++);
37
38         return test_create_device(ctx, name);
39 }
40
41 struct discover_device *test_create_device(struct discover_context *ctx,
42                 const char *name)
43 {
44         struct discover_device *dev;
45
46         dev = talloc_zero(ctx, struct discover_device);
47         dev->device = talloc_zero(dev, struct device);
48
49         list_init(&dev->boot_options);
50
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);
54
55         return dev;
56 }
57
58 static struct discover_context *test_create_context(struct parser_test *test)
59 {
60         struct discover_context *ctx;
61
62         ctx = talloc_zero(test, struct discover_context);
63         assert(ctx);
64
65         list_init(&ctx->boot_options);
66         ctx->device = test_create_device_simple(ctx);
67
68         return ctx;
69 }
70
71 struct parser_test *test_init(void)
72 {
73         struct parser_test *test;
74
75         test = talloc_zero(NULL, struct parser_test);
76         test->handler = device_handler_init(NULL, 0);
77         test->ctx = test_create_context(test);
78
79         return test;
80 }
81
82 void test_fini(struct parser_test *test)
83 {
84         device_handler_destroy(test->handler);
85         talloc_free(test);
86 }
87
88 void test_read_conf_file(struct parser_test *test, const char *filename)
89 {
90         struct stat stat;
91         char *path;
92         int fd, rc;
93
94         path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
95
96         fd = open(path, O_RDONLY);
97         if (fd < 0)
98                 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
99
100         rc = fstat(fd, &stat);
101         assert(!rc);
102         (void)rc;
103
104         test->conf.size = stat.st_size;
105         test->conf.buf = talloc_array(test, char, test->conf.size + 1);
106
107         rc = read(fd, test->conf.buf, test->conf.size);
108         assert(rc == (ssize_t)test->conf.size);
109
110         *(char *)(test->conf.buf + test->conf.size) = '\0';
111
112         close(fd);
113         talloc_free(path);
114 }
115
116 int test_run_parser(struct parser_test *test, const char *parser_name)
117 {
118         struct parser *parser;
119         int i, rc = 0;
120
121         for (i = 0; i < n_parsers; i++) {
122                 parser = parsers[i];
123                 if (strcmp(parser->name, parser_name))
124                         continue;
125                 test->ctx->parser = parser;
126                 rc = parser->parse(test->ctx, test->conf.buf, test->conf.size);
127         }
128
129         return rc;
130 }