826963ee22a677be7c2c6befbc10dacb9bb1abb9
[petitboot] / test / parser / test-grub2-save-env.c
1
2 #include <string.h>
3
4 #include <array-size/array-size.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
79 static void run_env_test(struct parser_test *test, struct env_test *envtest)
80 {
81         const char *env_before, *env_after;
82
83         env_before = talloc_asprintf(test, "%s%s", envsig, envtest->env_before);
84         env_after  = talloc_asprintf(test, "%s%s", envsig, envtest->env_after);
85
86         test_add_file_data(test, test->ctx->device, "/boot/grub/grubenv",
87                         env_before, strlen(env_before));
88
89         __test_read_conf_data(test, "/boot/grub2/grub.cfg", envtest->script,
90                         strlen(envtest->script));
91
92         test_run_parser(test, "grub2");
93
94         check_file_contents(test, test->ctx->device, "/boot/grub/grubenv",
95                         env_after, strlen(env_after));
96 }
97
98 void run_test(struct parser_test *test)
99 {
100         struct env_test *env_test;
101         unsigned int i;
102
103         for (i = 0; i < ARRAY_SIZE(tests); i++) {
104                 env_test = &tests[i];
105                 printf("test %s: ", env_test->name);
106                 fflush(stdout);
107                 run_env_test(test, env_test);
108                 printf("OK\n");
109                 fflush(stdout);
110         }
111 }