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