1 #include <test.h>
2
3 #include <var_expressions.h>
4
test_plain_variable_with_no_stuff_in_it(void)5 static void test_plain_variable_with_no_stuff_in_it(void)
6 {
7 VarRef *ref = VarRefParse("foo");
8 assert_false(ref->ns);
9 assert_false(ref->scope);
10 assert_string_equal("foo", ref->lval);
11 assert_int_equal(0, ref->num_indices);
12 assert_false(ref->indices);
13 VarRefDestroy(ref);
14 }
15
test_scoped(void)16 static void test_scoped(void)
17 {
18 VarRef *ref = VarRefParse("scope.lval");
19 assert_false(ref->ns);
20 assert_string_equal("scope", ref->scope);
21 assert_string_equal("lval", ref->lval);
22 assert_int_equal(0, ref->num_indices);
23 assert_false(ref->indices);
24 VarRefDestroy(ref);
25 }
26
test_full(void)27 static void test_full(void)
28 {
29 VarRef *ref = VarRefParse("ns:scope.lval");
30 assert_string_equal("ns", ref->ns);
31 assert_string_equal("scope", ref->scope);
32 assert_string_equal("lval", ref->lval);
33 assert_int_equal(0, ref->num_indices);
34 assert_false(ref->indices);
35 VarRefDestroy(ref);
36 }
37
test_dotted_array(void)38 static void test_dotted_array(void)
39 {
40 VarRef *ref = VarRefParse("ns:scope.lval[la.la]");
41 assert_string_equal("ns", ref->ns);
42 assert_string_equal("scope", ref->scope);
43 assert_string_equal("lval", ref->lval);
44 assert_int_equal(1, ref->num_indices);
45 assert_string_equal("la.la", ref->indices[0]);
46 VarRefDestroy(ref);
47 }
48
test_levels(void)49 static void test_levels(void)
50 {
51 VarRef *ref = VarRefParse("ns:scope.lval[x][y][z]");
52 assert_string_equal("ns", ref->ns);
53 assert_string_equal("scope", ref->scope);
54 assert_string_equal("lval", ref->lval);
55 assert_int_equal(3, ref->num_indices);
56 assert_string_equal("x", ref->indices[0]);
57 assert_string_equal("y", ref->indices[1]);
58 assert_string_equal("z", ref->indices[2]);
59 VarRefDestroy(ref);
60 }
61
test_unqualified_array(void)62 static void test_unqualified_array(void)
63 {
64 VarRef *ref = VarRefParse("lval[x]");
65 assert_false(ref->ns);
66 assert_false(ref->scope);
67 assert_string_equal("lval", ref->lval);
68 assert_int_equal(1, ref->num_indices);
69 assert_string_equal("x", ref->indices[0]);
70 VarRefDestroy(ref);
71 }
72
test_qualified_array(void)73 static void test_qualified_array(void)
74 {
75 VarRef *ref = VarRefParse("scope.lval[x]");
76 assert_false(ref->ns);
77 assert_string_equal("scope", ref->scope);
78 assert_string_equal("lval", ref->lval);
79 assert_int_equal(1, ref->num_indices);
80 assert_string_equal("x", ref->indices[0]);
81 VarRefDestroy(ref);
82 }
83
test_nested_array(void)84 static void test_nested_array(void)
85 {
86 VarRef *ref = VarRefParse("scope.lval[$(other[x])]");
87 assert_false(ref->ns);
88 assert_string_equal("scope", ref->scope);
89 assert_string_equal("lval", ref->lval);
90 assert_int_equal(1, ref->num_indices);
91 assert_string_equal("$(other[x])", ref->indices[0]);
92 VarRefDestroy(ref);
93 }
94
test_array_with_dot_colon_in_index(void)95 static void test_array_with_dot_colon_in_index(void)
96 {
97 VarRef *ref = VarRefParse("lval[x-x.x:x]");
98 assert_false(ref->ns);
99 assert_false(ref->scope);
100 assert_string_equal("lval", ref->lval);
101 assert_int_equal(1, ref->num_indices);
102 assert_string_equal("x-x.x:x", ref->indices[0]);
103 VarRefDestroy(ref);
104 }
105
test_special_scope(void)106 static void test_special_scope(void)
107 {
108 Policy *p = PolicyNew();
109 Bundle *bp = PolicyAppendBundle(p, "ns", "b", "agent", NULL, NULL);
110
111 {
112 VarRef *ref = VarRefParseFromBundle("c.lval", bp);
113 assert_string_equal("ns", ref->ns);
114 assert_string_equal("c", ref->scope);
115 assert_string_equal("lval", ref->lval);
116 VarRefDestroy(ref);
117 }
118
119 {
120 VarRef *ref = VarRefParseFromBundle("sys.lval", bp);
121 assert_false(ref->ns);
122 assert_string_equal("sys", ref->scope);
123 assert_string_equal("lval", ref->lval);
124 VarRefDestroy(ref);
125 }
126 PolicyDestroy(p);
127 }
128
CheckToStringQualified(const char * str,const char * expect)129 static void CheckToStringQualified(const char *str, const char *expect)
130 {
131 VarRef *ref = VarRefParse(str);
132 char *out = VarRefToString(ref, true);
133 assert_string_equal(expect, out);
134 free(out);
135 VarRefDestroy(ref);
136 }
137
test_to_string_qualified(void)138 static void test_to_string_qualified(void)
139 {
140 CheckToStringQualified("ns:scope.lval[x][y]", "ns:scope.lval[x][y]");
141 CheckToStringQualified("ns:scope.lval[x]", "ns:scope.lval[x]");
142 CheckToStringQualified("ns:scope.lval", "ns:scope.lval");
143 CheckToStringQualified("scope.lval", "default:scope.lval");
144 CheckToStringQualified("lval", "lval");
145 }
146
test_to_string_unqualified(void)147 static void test_to_string_unqualified(void)
148 {
149 {
150 VarRef *ref = VarRefParse("ns:scope.lval[x][y]");
151 char *out = VarRefToString(ref, false);
152 assert_string_equal("lval[x][y]", out);
153 free(out);
154 VarRefDestroy(ref);
155 }
156
157 {
158 VarRef *ref = VarRefParse("ns:scope.lval[x]");
159 char *out = VarRefToString(ref, false);
160 assert_string_equal("lval[x]", out);
161 free(out);
162 VarRefDestroy(ref);
163 }
164
165 {
166 VarRef *ref = VarRefParse("scope.lval");
167 char *out = VarRefToString(ref, false);
168 assert_string_equal("lval", out);
169 free(out);
170 VarRefDestroy(ref);
171 }
172
173 {
174 VarRef *ref = VarRefParse("lval");
175 char *out = VarRefToString(ref, false);
176 assert_string_equal("lval", out);
177 free(out);
178 VarRefDestroy(ref);
179 }
180 }
181
main()182 int main()
183 {
184 PRINT_TEST_BANNER();
185 const UnitTest tests[] =
186 {
187 unit_test(test_plain_variable_with_no_stuff_in_it),
188 unit_test(test_scoped),
189 unit_test(test_full),
190 unit_test(test_dotted_array),
191 unit_test(test_levels),
192 unit_test(test_unqualified_array),
193 unit_test(test_qualified_array),
194 unit_test(test_nested_array),
195 unit_test(test_array_with_dot_colon_in_index),
196 unit_test(test_special_scope),
197 unit_test(test_to_string_qualified),
198 unit_test(test_to_string_unqualified),
199 };
200
201 return run_tests(tests);
202 }
203