]> git.ozlabs.org Git - petitboot/blob - test/parser/utils.c
discover/grub2: Move parser-api definitions to parser.y
[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 struct p_item {
21         struct list_item list;
22         struct parser *parser;
23 };
24
25 STATIC_LIST(parsers);
26
27 void __register_parser(struct parser *parser)
28 {
29         struct p_item* i = talloc(NULL, struct p_item);
30
31         i->parser = parser;
32         list_add(&parsers, &i->list);
33 }
34
35 static void __attribute__((destructor)) __cleanup_parsers(void)
36 {
37         struct p_item *item, *tmp;
38
39         list_for_each_entry_safe(&parsers, item, tmp, list)
40                 talloc_free(item);
41 }
42
43 static struct discover_device *test_create_device_simple(
44                 struct parser_test *test)
45 {
46         static int dev_idx;
47         char name[10];
48
49         sprintf(name, "__test%d", dev_idx++);
50
51         return test_create_device(test, name);
52 }
53
54 struct discover_device *test_create_device(struct parser_test *test,
55                 const char *name)
56 {
57         struct discover_device *dev;
58
59         dev = discover_device_create(test->handler, name);
60
61         dev->device->id = talloc_strdup(dev, name);
62         dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
63         dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
64
65         return dev;
66 }
67
68 static struct discover_context *test_create_context(struct parser_test *test)
69 {
70         struct discover_context *ctx;
71
72         ctx = talloc_zero(test, struct discover_context);
73         assert(ctx);
74
75         list_init(&ctx->boot_options);
76         ctx->device = test_create_device_simple(test);
77         device_handler_add_device(test->handler, ctx->device);
78
79         return ctx;
80 }
81
82 extern struct config *test_config_init(struct parser_test *test);
83
84 struct parser_test *test_init(void)
85 {
86         struct parser_test *test;
87
88         test = talloc_zero(NULL, struct parser_test);
89         test->config = test_config_init(test);
90         test->handler = device_handler_init(NULL, NULL, 0);
91         test->ctx = test_create_context(test);
92
93         return test;
94 }
95
96 void test_fini(struct parser_test *test)
97 {
98         device_handler_destroy(test->handler);
99         talloc_free(test);
100 }
101
102 void __test_read_conf_data(struct parser_test *test,
103                 const char *buf, size_t len)
104 {
105         test->conf.size = len;
106         test->conf.buf = talloc_memdup(test, buf, len);
107 }
108
109 void test_read_conf_file(struct parser_test *test, const char *filename)
110 {
111         struct stat stat;
112         char *path;
113         int fd, rc;
114
115         path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
116
117         fd = open(path, O_RDONLY);
118         if (fd < 0)
119                 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
120
121         rc = fstat(fd, &stat);
122         assert(!rc);
123         (void)rc;
124
125         test->conf.size = stat.st_size;
126         test->conf.buf = talloc_array(test, char, test->conf.size + 1);
127
128         rc = read(fd, test->conf.buf, test->conf.size);
129         assert(rc == (ssize_t)test->conf.size);
130
131         *(char *)(test->conf.buf + test->conf.size) = '\0';
132
133         close(fd);
134         talloc_free(path);
135 }
136
137 void test_set_conf_source(struct parser_test *test, const char *url)
138 {
139         test->ctx->conf_url = pb_url_parse(test, url);
140         assert(test->ctx->conf_url);
141 }
142
143 int test_run_parser(struct parser_test *test, const char *parser_name)
144 {
145         struct p_item* i;
146
147         list_for_each_entry(&parsers, i, list) {
148                 if (strcmp(i->parser->name, parser_name))
149                         continue;
150                 test->ctx->parser = i->parser;
151                 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
152         }
153
154         errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
155 }
156
157 bool resource_resolve(struct device_handler *handler, struct parser *parser,
158                 struct resource *resource)
159 {
160         if (!resource)
161                 return true;
162         if (resource->resolved)
163                 return true;
164
165         assert(parser);
166         assert(parser->resolve_resource);
167
168         return parser->resolve_resource(handler, resource);
169 }
170
171 void boot_option_resolve(struct device_handler *handler,
172                 struct discover_boot_option *opt)
173 {
174         resource_resolve(handler, opt->source, opt->boot_image);
175         resource_resolve(handler, opt->source, opt->initrd);
176         resource_resolve(handler, opt->source, opt->icon);
177 }
178
179 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
180 {
181         struct discover_boot_option *opt;
182
183         device_handler_add_device(test->handler, dev);
184
185         list_for_each_entry(&test->ctx->boot_options, opt, list)
186                 boot_option_resolve(test->handler, opt);
187 }
188
189 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
190                 int idx)
191 {
192         struct discover_boot_option *opt;
193         int i = 0;
194
195         list_for_each_entry(&ctx->boot_options, opt, list) {
196                 if (i++ == idx)
197                         return opt;
198         }
199
200         assert(0);
201
202         return NULL;
203 }
204
205 void __check_boot_option_count(struct discover_context *ctx, int count,
206                 const char *file, int line)
207 {
208         struct discover_boot_option *opt;
209         int defaults = 0, i = 0;
210
211         list_for_each_entry(&ctx->boot_options, opt, list) {
212                 i++;
213                 if (opt->option->is_default)
214                         defaults++;
215         }
216
217         if (defaults > 1) {
218                 fprintf(stderr, "%s:%d: parser returned multiple default "
219                                 "options\n", file, line);
220                 exit(EXIT_FAILURE);
221         }
222
223         if (i == count)
224                 return;
225
226         fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
227         fprintf(stderr, "expected %d options, got %d:\n", count, i);
228
229         i = 1;
230         list_for_each_entry(&ctx->boot_options, opt, list)
231                 fprintf(stderr, "  %2d: %s [%s]\n", i++, opt->option->name,
232                                 opt->option->id);
233
234         exit(EXIT_FAILURE);
235 }
236
237 void __check_args(struct discover_boot_option *opt, const char *args,
238                 const char *file, int line)
239 {
240         int rc;
241
242         if (!opt->option->boot_args) {
243                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
244                 fprintf(stderr, "  no arguments parsed\n");
245                 fprintf(stderr, "  expected '%s'\n", args);
246                 exit(EXIT_FAILURE);
247         }
248
249         rc = strcmp(opt->option->boot_args, args);
250         if (rc) {
251                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
252                 fprintf(stderr, "  got      '%s'\n", opt->option->boot_args);
253                 fprintf(stderr, "  expected '%s'\n", args);
254                 exit(EXIT_FAILURE);
255         }
256 }
257
258 void __check_name(struct discover_boot_option *opt, const char *name,
259                 const char *file, int line)
260 {
261         int rc;
262
263         rc = strcmp(opt->option->name, name);
264         if (rc) {
265                 fprintf(stderr, "%s:%d: name check failed\n", file, line);
266                 fprintf(stderr, "  got      '%s'\n", opt->option->name);
267                 fprintf(stderr, "  expected '%s'\n", name);
268                 exit(EXIT_FAILURE);
269         }
270 }
271
272 void __check_is_default(struct discover_boot_option *opt,
273                 const char *file, int line)
274 {
275         if (opt->option->is_default)
276                 return;
277
278         fprintf(stderr, "%s:%d: default check failed\n", file, line);
279         exit(EXIT_FAILURE);
280 }
281
282 void __check_resolved_local_resource(struct resource *res,
283                 struct discover_device *dev, const char *local_path,
284                 const char *file, int line)
285 {
286         const char *exp_url, *got_url;
287
288         if (!res)
289                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
290
291         if (!res->resolved)
292                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
293                                 file, line);
294
295         exp_url = talloc_asprintf(res, "file://%s%s",
296                         dev->mount_path, local_path);
297         got_url = pb_url_to_string(res->url);
298
299         if (strcmp(got_url, exp_url)) {
300                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
301                 fprintf(stderr, "  got      '%s'\n", got_url);
302                 fprintf(stderr, "  expected '%s'\n", exp_url);
303                 exit(EXIT_FAILURE);
304         }
305 }
306
307 void __check_resolved_url_resource(struct resource *res,
308                 const char *url, const char *file, int line)
309 {
310         char *res_url;
311
312         if (!res)
313                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
314
315         if (!res->resolved)
316                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
317                                 file, line);
318
319         res_url = pb_url_to_string(res->url);
320         if (strcmp(url, res_url)) {
321                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
322                 fprintf(stderr, "  got      '%s'\n", res_url);
323                 fprintf(stderr, "  expected '%s'\n", url);
324                 exit(EXIT_FAILURE);
325         }
326 }
327 void __check_unresolved_resource(struct resource *res,
328                 const char *file, int line)
329 {
330         if (!res)
331                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
332
333         if (res->resolved)
334                 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
335 }
336
337 void __check_not_present_resource(struct resource *res,
338                 const char *file, int line)
339 {
340         if (res)
341                 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
342 }