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