]> git.ozlabs.org Git - petitboot/blob - test/parser/utils.c
test/parser: Add check_boot_option_count helper & get_boot_option
[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_data(struct parser_test *test,
89                 const char *buf, size_t len)
90 {
91         test->conf.size = len;
92         test->conf.buf = talloc_memdup(test, buf, len);
93 }
94
95 void test_read_conf_file(struct parser_test *test, const char *filename)
96 {
97         struct stat stat;
98         char *path;
99         int fd, rc;
100
101         path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
102
103         fd = open(path, O_RDONLY);
104         if (fd < 0)
105                 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
106
107         rc = fstat(fd, &stat);
108         assert(!rc);
109         (void)rc;
110
111         test->conf.size = stat.st_size;
112         test->conf.buf = talloc_array(test, char, test->conf.size + 1);
113
114         rc = read(fd, test->conf.buf, test->conf.size);
115         assert(rc == (ssize_t)test->conf.size);
116
117         *(char *)(test->conf.buf + test->conf.size) = '\0';
118
119         close(fd);
120         talloc_free(path);
121 }
122
123 int test_run_parser(struct parser_test *test, const char *parser_name)
124 {
125         struct parser *parser;
126         int i, rc = 0;
127
128         for (i = 0; i < n_parsers; i++) {
129                 parser = parsers[i];
130                 if (strcmp(parser->name, parser_name))
131                         continue;
132                 test->ctx->parser = parser;
133                 rc = parser->parse(test->ctx, test->conf.buf, test->conf.size);
134                 break;
135         }
136
137         if (i == n_parsers)
138                 errx(EXIT_FAILURE, "%s: parser '%s' not found",
139                                 __func__, parser_name);
140
141         return rc;
142 }
143
144 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
145                 int idx)
146 {
147         struct discover_boot_option *opt;
148         int i = 0;
149
150         list_for_each_entry(&ctx->boot_options, opt, list) {
151                 if (i++ == idx)
152                         return opt;
153         }
154
155         assert(0);
156
157         return NULL;
158 }
159
160 void __check_boot_option_count(struct discover_context *ctx, int count,
161                 const char *file, int line)
162 {
163         struct discover_boot_option *opt;
164         int i = 0;
165
166         list_for_each_entry(&ctx->boot_options, opt, list)
167                 i++;
168
169         if (i == count)
170                 return;
171
172         fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
173         fprintf(stderr, "expected %d options, got %d:\n", count, i);
174
175         i = 1;
176         list_for_each_entry(&ctx->boot_options, opt, list)
177                 fprintf(stderr, "  %2d: %s [%s]\n", i++, opt->option->name,
178                                 opt->option->id);
179
180         exit(EXIT_FAILURE);
181 }