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