1 /*
2 * Author: Joshua Brindle <jbrindle@tresys.com>
3 * Chad Sellers <csellers@tresys.com>
4 *
5 * Copyright (C) 2006 Tresys Technology, LLC
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "parse_util.h"
23 #include "helpers.h"
24 #include "test-common.h"
25
26 #include <sepol/policydb/policydb.h>
27 #include <sepol/policydb/link.h>
28
29 #include <CUnit/Basic.h>
30 #include <stdlib.h>
31
32 /* Tests for types:
33 * Test for each of these for
34 * - type in appropriate symtab (global and decl)
35 * - datum in the decl symtab has correct type bitmap (if attr)
36 * - primary is set correctly
37 * - scope datum has correct decl ids
38 * Tests:
39 * - type in base, no modules
40 * - type in base optional, no modules
41 * - type a in base, b in module
42 * - type a in base optional, b in module
43 * - type a in base, b in module optional
44 * - type a in base optional, b in module optional
45 * - attr in base, no modules
46 * - attr in base optional, no modules
47 * - attr a in base, b in module
48 * - attr a in base optional, b in module
49 * - attr a in base, b in module optional
50 * - attr a in base optional, b in module optional
51 * - attr a declared in base, added to in module
52 * - attr a declared in base, added to in module optional
53 * - attr a declared in base, added to in 2 modules
54 * - attr a declared in base, added to in 2 modules (optional and global)
55 * - attr a declared in base optional, added to in module
56 * - attr a declared in base optional, added to in module optional
57 * - attr a added to in base optional, declared in module
58 * - attr a added to in base optional, declared in module optional
59 * - attr a added to in base optional, declared in module, added to in other module
60 * - attr a added to in base optional, declared in module optional, added to in other module
61 * - attr a added to in base optional, declared in module , added to in other module optional
62 * - attr a added to in base optional, declared in module optional, added to in other module optional
63 * - alias in base of primary type in base, no modules
64 * - alias in base optional of primary type in base, no modules
65 * - alias in base optional of primary type in base optional
66 * - alias in module of primary type in base
67 * - alias in module optional of primary type in base
68 * - alias in module optional of primary type in base optional
69 * - alias in module of primary type in module
70 * - alias in module optional of primary type in module
71 * - alias in module optional of primary type in module optional
72 * - alias a in base, b in module, primary type in base
73 * - alias a in base, b in module, primary type in module
74 * - alias a in base optional, b in module, primary type in base
75 * - alias a in base optional, b in module, primary type in module
76 * - alias a in base, b in module optional, primary type in base
77 * - alias a in base, b in module optional, primary type in module
78 * - alias a in base optional, b in module optional, primary type in base
79 * - alias a in base optional, b in module optional, primary type in module
80 * - alias a in base, required in module, primary type in base
81 * - alias a in base, required in base optional, primary type in base
82 * - alias a in base, required in module optional, primary type in base
83 * - alias a in module, required in base optional, primary type in base
84 * - alias a in module, required in module optional, primary type in base
85 * - alias a in base optional, required in module, primary type in base
86 * - alias a in base optional, required in different base optional, primary type in base
87 * - alias a in base optional, required in module optional, primary type in base
88 * - alias a in module optional, required in base optional, primary type in base
89 * - alias a in module optional, required in module optional, primary type in base
90 * - alias a in module, required in base optional, primary type in module
91 * - alias a in module, required in module optional, primary type in module
92 * - alias a in base optional, required in module, primary type in module
93 * - alias a in base optional, required in different base optional, primary type in module
94 * - alias a in base optional, required in module optional, primary type in module
95 * - alias a in module optional, required in base optional, primary type in module
96 * - alias a in module optional, required in module optional, primary type in module
97 */
98
99 /* Don't pass in decls from global blocks since symbols aren't stored in their symtab */
test_type_datum(policydb_t * p,const char * id,unsigned int * decls,int len,unsigned int primary)100 static void test_type_datum(policydb_t * p, const char *id, unsigned int *decls, int len, unsigned int primary)
101 {
102 int i;
103 unsigned int value;
104 type_datum_t *type;
105
106 /* just test the type datums for each decl to see if it is what we expect */
107 type = hashtab_search(p->p_types.table, id);
108
109 CU_ASSERT_FATAL(type != NULL);
110 CU_ASSERT(type->primary == primary);
111 CU_ASSERT(type->flavor == TYPE_TYPE);
112
113 value = type->s.value;
114
115 for (i = 0; i < len; i++) {
116 type = hashtab_search(p->decl_val_to_struct[decls[i] - 1]->p_types.table, id);
117 CU_ASSERT_FATAL(type != NULL);
118 CU_ASSERT(type->primary == primary);
119 CU_ASSERT(type->flavor == TYPE_TYPE);
120 CU_ASSERT(type->s.value == value);
121 }
122
123 }
124
base_type_tests(policydb_t * base)125 void base_type_tests(policydb_t * base)
126 {
127 unsigned int decls[2];
128 const char *types[2];
129
130 /* These tests look at types in the base only, the desire is to ensure that
131 * types are not destroyed or otherwise removed during the link process.
132 * if this happens these tests won't work anyway since we are using types to
133 * mark blocks */
134
135 /**** test for g_b_type_1 in base and decl 1 (global) ****/
136 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
137 test_sym_presence(base, "g_b_type_1", SYM_TYPES, SCOPE_DECL, decls, 1);
138 test_type_datum(base, "g_b_type_1", NULL, 0, 1);
139 /* this attr is in the same decl as the type */
140 test_sym_presence(base, "g_b_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
141 types[0] = "g_b_type_1";
142 test_attr_types(base, "g_b_attr_1", NULL, types, 1);
143
144 /**** test for o1_b_type_1 in optional (decl 2) ****/
145 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b"))->decl_id;
146 test_sym_presence(base, "o1_b_type_1", SYM_TYPES, SCOPE_DECL, decls, 1);
147 test_type_datum(base, "o1_b_type_1", NULL, 0, 1);
148 /* this attr is in the same decl as the type */
149 test_sym_presence(base, "o1_b_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
150 types[0] = "o1_b_type_1";
151 test_attr_types(base, "o1_b_attr_1", base->decl_val_to_struct[decls[0] - 1], types, 1);
152
153 /* tests for aliases */
154 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
155 test_sym_presence(base, "g_b_alias_1", SYM_TYPES, SCOPE_DECL, decls, 1);
156 test_alias_datum(base, "g_b_alias_1", "g_b_type_3", 1, 0);
157 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o6_b"))->decl_id;
158 test_sym_presence(base, "g_b_alias_2", SYM_TYPES, SCOPE_DECL, decls, 1);
159 test_alias_datum(base, "g_b_alias_2", "g_b_type_3", 1, 0);
160
161 }
162
module_type_tests(policydb_t * base)163 void module_type_tests(policydb_t * base)
164 {
165 unsigned int decls[2];
166 const char *types[2];
167 avrule_decl_t *d;
168
169 /* These tests look at types that were copied from modules or attributes
170 * that were modified and declared in modules and base. These apply to
171 * declarations and modifications in and out of optionals. These tests
172 * should ensure that types and attributes are correctly copied from modules
173 * and that attribute type sets are correctly copied and mapped. */
174
175 /* note: scope for attributes is currently smashed if the attribute is declared
176 * somewhere so the scope test only looks at global, the type bitmap test looks
177 * at the appropriate decl symtab */
178
179 /* test for type in module 1 (global) */
180 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
181 test_sym_presence(base, "g_m1_type_1", SYM_TYPES, SCOPE_DECL, decls, 1);
182 test_type_datum(base, "g_m1_type_1", NULL, 0, 1);
183 /* attr has is in the same decl as the above type */
184 test_sym_presence(base, "g_m1_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
185 types[0] = "g_m1_type_1";
186 types[1] = "g_m1_type_2";
187 test_attr_types(base, "g_m1_attr_1", NULL, types, 2);
188
189 /* test for type in module 1 (optional) */
190 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_m1"))->decl_id;
191 test_sym_presence(base, "o1_m1_type_1", SYM_TYPES, SCOPE_DECL, decls, 1);
192 test_type_datum(base, "o1_m1_type_1", NULL, 0, 1);
193 /* attr has is in the same decl as the above type */
194 test_sym_presence(base, "o1_m1_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
195 types[0] = "o1_m1_type_2";
196 test_attr_types(base, "o1_m1_attr_1", base->decl_val_to_struct[decls[0] - 1], types, 1);
197
198 /* test for attr declared in base, added to in module (global).
199 * Since these are both global it'll be merged in the main symtab */
200 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
201 test_sym_presence(base, "g_b_attr_3", SYM_TYPES, SCOPE_DECL, decls, 1);
202 types[0] = "g_m1_type_3";
203 test_attr_types(base, "g_b_attr_3", NULL, types, 1);
204
205 /* test for attr declared in base, added to in module (optional). */
206 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
207 test_sym_presence(base, "g_b_attr_4", SYM_TYPES, SCOPE_DECL, decls, 1);
208
209 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_m1"))->decl_id;
210 types[0] = "o1_m1_type_3";
211 test_attr_types(base, "g_b_attr_4", base->decl_val_to_struct[decls[0] - 1], types, 1);
212
213 /* test for attr declared in base, added to in 2 modules (global). (merged in main symtab) */
214 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
215 test_sym_presence(base, "g_b_attr_5", SYM_TYPES, SCOPE_DECL, decls, 1);
216 types[0] = "g_m1_type_4";
217 types[1] = "g_m2_type_4";
218 test_attr_types(base, "g_b_attr_5", NULL, types, 2);
219
220 /* test for attr declared in base, added to in 2 modules (optional/global). */
221 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
222 test_sym_presence(base, "g_b_attr_6", SYM_TYPES, SCOPE_DECL, decls, 1);
223 /* module 2 was global to its type is in main symtab */
224 types[0] = "g_m2_type_5";
225 test_attr_types(base, "g_b_attr_6", NULL, types, 1);
226 d = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o3_m1"));
227 types[0] = "o3_m1_type_2";
228 test_attr_types(base, "g_b_attr_6", d, types, 1);
229
230 /* test for attr declared in base optional, added to in module (global). */
231 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o4_b"))->decl_id;
232 test_sym_presence(base, "o4_b_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
233 types[0] = "g_m1_type_5";
234 test_attr_types(base, "o4_b_attr_1", NULL, types, 1);
235
236 /* test for attr declared in base optional, added to in module (optional). */
237 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b"))->decl_id;
238 test_sym_presence(base, "o1_b_attr_2", SYM_TYPES, SCOPE_DECL, decls, 1);
239 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_m1");
240 types[0] = "o1_m1_type_5";
241 test_attr_types(base, "o1_b_attr_2", d, types, 1);
242
243 /* test for attr declared in module, added to in base optional */
244 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
245 test_sym_presence(base, "g_m1_attr_2", SYM_TYPES, SCOPE_DECL, decls, 1);
246 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b");
247 types[0] = "o1_b_type_2";
248 test_attr_types(base, "g_m1_attr_2", d, types, 1);
249
250 /* test for attr declared in module optional, added to in base optional */
251 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o3_m1"))->decl_id;
252 test_sym_presence(base, "o3_m1_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
253 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o4_b");
254 types[0] = "o4_b_type_1";
255 test_attr_types(base, "o3_m1_attr_1", d, types, 1);
256
257 /* attr a added to in base optional, declared/added to in module, added to in other module */
258 /* first the module declare/add and module 2 add (since its global it'll be in the main symtab */
259 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
260 test_sym_presence(base, "g_m1_attr_3", SYM_TYPES, SCOPE_DECL, decls, 1);
261 types[0] = "g_m1_type_6";
262 types[1] = "g_m2_type_3";
263 test_attr_types(base, "g_m1_attr_3", NULL, types, 2);
264 /* base add */
265 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o4_b");
266 types[0] = "o4_b_type_2";
267 test_attr_types(base, "g_m1_attr_3", d, types, 1);
268
269 /* attr a added to in base optional, declared/added in module optional, added to in other module */
270 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o3_m1");
271 decls[0] = d->decl_id;
272 test_sym_presence(base, "o3_m1_attr_2", SYM_TYPES, SCOPE_DECL, decls, 1);
273 types[0] = "o3_m1_type_3";
274 test_attr_types(base, "o3_m1_attr_2", d, types, 1);
275 /* module 2's type will be in the main symtab */
276 types[0] = "g_m2_type_6";
277 test_attr_types(base, "o3_m1_attr_2", NULL, types, 1);
278 /* base add */
279 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o2_b");
280 types[0] = "o2_b_type_1";
281 test_attr_types(base, "o3_m1_attr_2", d, types, 1);
282
283 /* attr a added to in base optional, declared/added in module , added to in other module optional */
284 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
285 test_sym_presence(base, "g_m1_attr_4", SYM_TYPES, SCOPE_DECL, decls, 1);
286 types[0] = "g_m1_type_7";
287 test_attr_types(base, "g_m1_attr_4", NULL, types, 1);
288 /* module 2 */
289 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o2_m2");
290 types[0] = "o2_m2_type_1";
291 test_attr_types(base, "g_m1_attr_4", d, types, 1);
292 /* base add */
293 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o5_b");
294 types[0] = "o5_b_type_1";
295 test_attr_types(base, "g_m1_attr_4", d, types, 1);
296
297 /* attr a added to in base optional, declared/added in module optional, added to in other module optional */
298 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o4_m1");
299 decls[0] = d->decl_id;
300 test_sym_presence(base, "o4_m1_attr_1", SYM_TYPES, SCOPE_DECL, decls, 1);
301 types[0] = "o4_m1_type_1";
302 test_attr_types(base, "o4_m1_attr_1", d, types, 1);
303 /* module 2 */
304 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o2_m2");
305 types[0] = "o2_m2_type_2";
306 test_attr_types(base, "o4_m1_attr_1", d, types, 1);
307 /* base add */
308 d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o5_b");
309 types[0] = "o5_b_type_2";
310 test_attr_types(base, "o4_m1_attr_1", d, types, 1);
311
312 /* tests for aliases */
313 decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
314 test_sym_presence(base, "g_m_alias_1", SYM_TYPES, SCOPE_DECL, decls, 1);
315 test_alias_datum(base, "g_m_alias_1", "g_b_type_3", 1, 0);
316
317 }
318