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