]> git.ozlabs.org Git - petitboot/blob - test/parser/parser-test.c
cd6e62f54c065f1d47cc3eb96c70edc0663ed52e
[petitboot] / test / parser / parser-test.c
1 #define _GNU_SOURCE
2
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8
9 #include <log/log.h>
10 #include <types/types.h>
11 #include <talloc/talloc.h>
12
13 #include "discover/device-handler.h"
14 #include "discover/parser.h"
15 #include "discover/parser-utils.h"
16 #include "discover/paths.h"
17
18 static FILE *testf;
19
20 struct device *discover_context_device(struct discover_context *ctx)
21 {
22         return ctx->device->device;
23 }
24
25 struct discover_boot_option *discover_boot_option_create(
26                 struct discover_context *ctx,
27                 struct discover_device *dev)
28 {
29         struct discover_boot_option *opt;
30
31         opt = talloc_zero(ctx, struct discover_boot_option);
32         opt->option = talloc(opt, struct boot_option);
33         opt->device = dev;
34
35         return opt;
36 }
37
38 void discover_context_add_boot_option(struct discover_context *ctx,
39                 struct discover_boot_option *boot_option)
40 {
41         struct boot_option *opt = boot_option->option;
42
43         fprintf(testf, "%s: %s\n", __func__, ctx->device->device->id);
44         fprintf(testf, " id     '%s'\n", opt->id);
45         fprintf(testf, " name   '%s'\n", opt->name);
46         fprintf(testf, " descr  '%s'\n", opt->description);
47         fprintf(testf, " icon   '%s'\n", opt->icon_file);
48         fprintf(testf, " image  '%s'\n", opt->boot_image_file);
49         fprintf(testf, " initrd '%s'\n", opt->initrd_file);
50         fprintf(testf, " args   '%s'\n", opt->boot_args);
51         fflush(testf);
52 }
53
54 const char *generic_icon_file(
55         enum generic_icon_type __attribute__((unused)) type)
56 {
57         return "tester.png";
58 }
59
60 enum generic_icon_type guess_device_type(
61         struct discover_context __attribute__((unused)) *ctx)
62 {
63         return ICON_TYPE_UNKNOWN;
64 }
65
66 int main(int argc, char **argv)
67 {
68         struct discover_context *ctx;
69
70         if (argc != 3) {
71                 fprintf(stderr, "usage: %s <basedir> <devname>\n", argv[0]);
72                 return EXIT_FAILURE;
73         }
74
75         /* Default to test on stdout, pb_log on stderr. */
76
77         testf = stdout;
78
79         pb_log_set_stream(stderr);
80         pb_log_always_flush(1);
81         pb_log("--- parser-test ---\n");
82
83         ctx = talloc_zero(NULL, struct discover_context);
84
85         ctx->device = talloc_zero(ctx, struct discover_device);
86         ctx->device->device = talloc_zero(ctx->device, struct device);
87         ctx->device->device_path = talloc_asprintf(ctx, "%s/%s",
88                                                         argv[1], argv[2]);
89         ctx->device->device->id = talloc_strdup(ctx->device->device, argv[2]);
90
91         iterate_parsers(ctx);
92
93         pb_log("--- end ---\n");
94
95         return EXIT_SUCCESS;
96 }