]> git.ozlabs.org Git - petitboot/blob - test/parser/utils.c
f6939828337cfde8aa3ce31b8f1687cea2542e34
[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 && !args)
243                 return;
244
245         if (!opt->option->boot_args) {
246                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
247                 fprintf(stderr, "  no arguments parsed\n");
248                 fprintf(stderr, "  expected '%s'\n", args);
249                 exit(EXIT_FAILURE);
250         }
251
252         rc = strcmp(opt->option->boot_args, args);
253         if (rc) {
254                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
255                 fprintf(stderr, "  got      '%s'\n", opt->option->boot_args);
256                 fprintf(stderr, "  expected '%s'\n", args);
257                 exit(EXIT_FAILURE);
258         }
259 }
260
261 void __check_name(struct discover_boot_option *opt, const char *name,
262                 const char *file, int line)
263 {
264         int rc;
265
266         rc = strcmp(opt->option->name, name);
267         if (rc) {
268                 fprintf(stderr, "%s:%d: name check failed\n", file, line);
269                 fprintf(stderr, "  got      '%s'\n", opt->option->name);
270                 fprintf(stderr, "  expected '%s'\n", name);
271                 exit(EXIT_FAILURE);
272         }
273 }
274
275 void __check_is_default(struct discover_boot_option *opt,
276                 const char *file, int line)
277 {
278         if (opt->option->is_default)
279                 return;
280
281         fprintf(stderr, "%s:%d: default check failed\n", file, line);
282         exit(EXIT_FAILURE);
283 }
284
285 void __check_resolved_local_resource(struct resource *res,
286                 struct discover_device *dev, const char *local_path,
287                 const char *file, int line)
288 {
289         const char *exp_url, *got_url;
290
291         if (!res)
292                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
293
294         if (!res->resolved)
295                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
296                                 file, line);
297
298         exp_url = talloc_asprintf(res, "file://%s%s",
299                         dev->mount_path, local_path);
300         got_url = pb_url_to_string(res->url);
301
302         if (strcmp(got_url, exp_url)) {
303                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
304                 fprintf(stderr, "  got      '%s'\n", got_url);
305                 fprintf(stderr, "  expected '%s'\n", exp_url);
306                 exit(EXIT_FAILURE);
307         }
308 }
309
310 void __check_resolved_url_resource(struct resource *res,
311                 const char *url, const char *file, int line)
312 {
313         char *res_url;
314
315         if (!res)
316                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
317
318         if (!res->resolved)
319                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
320                                 file, line);
321
322         res_url = pb_url_to_string(res->url);
323         if (strcmp(url, res_url)) {
324                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
325                 fprintf(stderr, "  got      '%s'\n", res_url);
326                 fprintf(stderr, "  expected '%s'\n", url);
327                 exit(EXIT_FAILURE);
328         }
329 }
330 void __check_unresolved_resource(struct resource *res,
331                 const char *file, int line)
332 {
333         if (!res)
334                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
335
336         if (res->resolved)
337                 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
338 }
339
340 void __check_not_present_resource(struct resource *res,
341                 const char *file, int line)
342 {
343         if (res)
344                 errx(EXIT_FAILURE, "%s:%d: Resource present", file, line);
345 }