]> git.ozlabs.org Git - petitboot/blob - test/parser/utils.c
parser: Use list to hold parsers
[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 struct discover_device *test_create_device_simple(
36                 struct discover_context *ctx)
37 {
38         static int dev_idx;
39         char name[10];
40
41         sprintf(name, "__test%d", dev_idx++);
42
43         return test_create_device(ctx, name);
44 }
45
46 struct discover_device *test_create_device(struct discover_context *ctx,
47                 const char *name)
48 {
49         struct discover_device *dev;
50
51         dev = talloc_zero(ctx, struct discover_device);
52         dev->device = talloc_zero(dev, struct device);
53
54         list_init(&dev->boot_options);
55
56         dev->device->id = talloc_strdup(dev, name);
57         dev->device_path = talloc_asprintf(dev, "/dev/%s", name);
58         dev->mount_path = talloc_asprintf(dev, "/test/mount/%s", name);
59
60         return dev;
61 }
62
63 static struct discover_context *test_create_context(struct parser_test *test)
64 {
65         struct discover_context *ctx;
66
67         ctx = talloc_zero(test, struct discover_context);
68         assert(ctx);
69
70         list_init(&ctx->boot_options);
71         ctx->device = test_create_device_simple(ctx);
72
73         return ctx;
74 }
75
76 struct parser_test *test_init(void)
77 {
78         struct parser_test *test;
79
80         test = talloc_zero(NULL, struct parser_test);
81         test->handler = device_handler_init(NULL, 0);
82         test->ctx = test_create_context(test);
83
84         return test;
85 }
86
87 void test_fini(struct parser_test *test)
88 {
89         device_handler_destroy(test->handler);
90         talloc_free(test);
91 }
92
93 void __test_read_conf_data(struct parser_test *test,
94                 const char *buf, size_t len)
95 {
96         test->conf.size = len;
97         test->conf.buf = talloc_memdup(test, buf, len);
98 }
99
100 void test_read_conf_file(struct parser_test *test, const char *filename)
101 {
102         struct stat stat;
103         char *path;
104         int fd, rc;
105
106         path = talloc_asprintf(test, "%s/%s", TEST_CONF_BASE, filename);
107
108         fd = open(path, O_RDONLY);
109         if (fd < 0)
110                 err(EXIT_FAILURE, "Can't open test conf file %s\n", path);
111
112         rc = fstat(fd, &stat);
113         assert(!rc);
114         (void)rc;
115
116         test->conf.size = stat.st_size;
117         test->conf.buf = talloc_array(test, char, test->conf.size + 1);
118
119         rc = read(fd, test->conf.buf, test->conf.size);
120         assert(rc == (ssize_t)test->conf.size);
121
122         *(char *)(test->conf.buf + test->conf.size) = '\0';
123
124         close(fd);
125         talloc_free(path);
126 }
127
128 int test_run_parser(struct parser_test *test, const char *parser_name)
129 {
130         struct p_item* i;
131
132         list_for_each_entry(&parsers, i, list) {
133                 if (strcmp(i->parser->name, parser_name))
134                         continue;
135                 test->ctx->parser = i->parser;
136                 return i->parser->parse(test->ctx, test->conf.buf, test->conf.size);
137         }
138
139         errx(EXIT_FAILURE, "%s: parser '%s' not found", __func__, parser_name);
140 }
141
142 bool resource_resolve(struct device_handler *handler, struct parser *parser,
143                 struct resource *resource)
144 {
145         if (!resource)
146                 return true;
147         if (resource->resolved)
148                 return true;
149
150         assert(parser);
151         assert(parser->resolve_resource);
152
153         return parser->resolve_resource(handler, resource);
154 }
155
156 void boot_option_resolve(struct device_handler *handler,
157                 struct discover_boot_option *opt)
158 {
159         resource_resolve(handler, opt->source, opt->boot_image);
160         resource_resolve(handler, opt->source, opt->initrd);
161         resource_resolve(handler, opt->source, opt->icon);
162 }
163
164 extern void device_handler_add_device(struct device_handler *handler,
165                 struct discover_device *dev);
166
167 void test_hotplug_device(struct parser_test *test, struct discover_device *dev)
168 {
169         struct discover_boot_option *opt;
170
171         device_handler_add_device(test->handler, dev);
172
173         list_for_each_entry(&test->ctx->boot_options, opt, list)
174                 boot_option_resolve(test->handler, opt);
175 }
176
177 struct discover_boot_option *get_boot_option(struct discover_context *ctx,
178                 int idx)
179 {
180         struct discover_boot_option *opt;
181         int i = 0;
182
183         list_for_each_entry(&ctx->boot_options, opt, list) {
184                 if (i++ == idx)
185                         return opt;
186         }
187
188         assert(0);
189
190         return NULL;
191 }
192
193 void __check_boot_option_count(struct discover_context *ctx, int count,
194                 const char *file, int line)
195 {
196         struct discover_boot_option *opt;
197         int i = 0;
198
199         list_for_each_entry(&ctx->boot_options, opt, list)
200                 i++;
201
202         if (i == count)
203                 return;
204
205         fprintf(stderr, "%s:%d: boot option count check failed\n", file, line);
206         fprintf(stderr, "expected %d options, got %d:\n", count, i);
207
208         i = 1;
209         list_for_each_entry(&ctx->boot_options, opt, list)
210                 fprintf(stderr, "  %2d: %s [%s]\n", i++, opt->option->name,
211                                 opt->option->id);
212
213         exit(EXIT_FAILURE);
214 }
215
216 void __check_args(struct discover_boot_option *opt, const char *args,
217                 const char *file, int line)
218 {
219         int rc;
220
221         if (!opt->option->boot_args) {
222                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
223                 fprintf(stderr, "  no arguments parsed\n");
224                 fprintf(stderr, "  expected '%s'\n", args);
225                 exit(EXIT_FAILURE);
226         }
227
228         rc = strcmp(opt->option->boot_args, args);
229         if (rc) {
230                 fprintf(stderr, "%s:%d: arg check failed\n", file, line);
231                 fprintf(stderr, "  got      '%s'\n", opt->option->boot_args);
232                 fprintf(stderr, "  expected '%s'\n", args);
233                 exit(EXIT_FAILURE);
234         }
235 }
236
237 void __check_name(struct discover_boot_option *opt, const char *name,
238                 const char *file, int line)
239 {
240         int rc;
241
242         rc = strcmp(opt->option->name, name);
243         if (rc) {
244                 fprintf(stderr, "%s:%d: name check failed\n", file, line);
245                 fprintf(stderr, "  got      '%s'\n", opt->option->name);
246                 fprintf(stderr, "  expected '%s'\n", name);
247                 exit(EXIT_FAILURE);
248         }
249 }
250
251 void __check_resolved_local_resource(struct resource *res,
252                 struct discover_device *dev, const char *local_path,
253                 const char *file, int line)
254 {
255         const char *exp_url, *got_url;
256
257         if (!res)
258                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
259
260         if (!res->resolved)
261                 errx(EXIT_FAILURE, "%s:%d: Resource is not resolved",
262                                 file, line);
263
264         exp_url = talloc_asprintf(res, "file://%s%s",
265                         dev->mount_path, local_path);
266         got_url = pb_url_to_string(res->url);
267
268         if (strcmp(got_url, exp_url)) {
269                 fprintf(stderr, "%s:%d: Resource mismatch\n", file, line);
270                 fprintf(stderr, "  got      '%s'\n", got_url);
271                 fprintf(stderr, "  expected '%s'\n", exp_url);
272                 exit(EXIT_FAILURE);
273         }
274 }
275
276 void __check_unresolved_resource(struct resource *res,
277                 const char *file, int line)
278 {
279         if (!res)
280                 errx(EXIT_FAILURE, "%s:%d: No resource", file, line);
281
282         if (res->resolved)
283                 errx(EXIT_FAILURE, "%s:%d: Resource is resolved", file, line);
284 }