Update tests to support changes to pxe_parser
[petitboot] / test / parser / test-grub2-save-env.c
1
2 #include <string.h>
3
4 #include <util/util.h>
5 #include <talloc/talloc.h>
6
7 #include "parser-test.h"
8
9 static const char *envsig = "# GRUB Environment Block\n";
10
11 struct env_test {
12         const char *name;
13         const char *env_before;
14         const char *script;
15         const char *env_after;
16 } tests[] = {
17         {
18                 "init",
19                 "######",
20                 "a=xxx\nsave_env a\n",
21                 "a=xxx\n"
22         },
23         {
24                 "append",
25                 "q=q\nr=r\n######",
26                 "a=xxx\nsave_env a\n",
27                 "q=q\nr=r\na=xxx\n"
28         },
29         {
30                 "expand",
31                 "q=q\na=x\nr=r\n##",
32                 "a=xxx\nsave_env a\n",
33                 "q=q\na=xxx\nr=r\n",
34         },
35         {
36                 "reduce",
37                 "q=q\na=xxx\nr=r\n",
38                 "a=x\nsave_env a\n",
39                 "q=q\na=x\nr=r\n##",
40         },
41         {
42                 "invalid-insert",
43                 "q=q\n---\nr=r\n",
44                 "a=x\nsave_env a\n",
45                 "q=q\na=x\nr=r\n",
46         },
47         {
48                 "invalid-shift",
49                 "q=q\n--\nr=r\n#",
50                 "a=x\nsave_env a\n",
51                 "q=q\na=x\nr=r\n",
52         },
53         {
54                 "invalid-reduce",
55                 "q=q\n----\nr=r\n",
56                 "a=x\nsave_env a\n",
57                 "q=q\na=x\nr=r\n#",
58         },
59         {
60                 "dup-replace-first",
61                 "q=q\na=y\nr=r\na=z",
62                 "a=x\nsave_env a\n",
63                 "q=q\na=x\nr=r\na=z",
64         },
65         {
66                 "nospace-add",
67                 "q=q\nr=r\n###",
68                 "a=x\nsave_env a\n",
69                 "q=q\nr=r\n###",
70         },
71         {
72                 "nospace-replace",
73                 "q=q\na=x\nr=r\n#",
74                 "a=xxx\nsave_env a\n",
75                 "q=q\na=x\nr=r\n#",
76         },
77         {
78                 "unset-var",
79                 "##############",
80                 "save_env an_unset_var\n",
81                 "an_unset_var=\n"
82         }
83 };
84
85 static void run_env_test(struct parser_test *test, struct env_test *envtest)
86 {
87         const char *env_before, *env_after;
88
89         env_before = talloc_asprintf(test, "%s%s", envsig, envtest->env_before);
90         env_after  = talloc_asprintf(test, "%s%s", envsig, envtest->env_after);
91
92         test_add_file_data(test, test->ctx->device, "/boot/grub/grubenv",
93                         env_before, strlen(env_before));
94
95         __test_read_conf_data(test, test->ctx->device,
96                         "/boot/grub/grub.cfg", envtest->script,
97                         strlen(envtest->script));
98
99         test_run_parser(test, "grub2");
100
101         check_file_contents(test, test->ctx->device, "/boot/grub/grubenv",
102                         env_after, strlen(env_after));
103 }
104
105 void run_test(struct parser_test *test)
106 {
107         struct env_test *env_test;
108         unsigned int i;
109
110         for (i = 0; i < ARRAY_SIZE(tests); i++) {
111                 env_test = &tests[i];
112                 printf("test %s: ", env_test->name);
113                 fflush(stdout);
114                 run_env_test(test, env_test);
115                 printf("OK\n");
116                 fflush(stdout);
117         }
118 }