1*2d543d20SAndroid Build Coastguard Worker /*
2*2d543d20SAndroid Build Coastguard Worker * Author: Joshua Brindle <[email protected]>
3*2d543d20SAndroid Build Coastguard Worker *
4*2d543d20SAndroid Build Coastguard Worker * Copyright (C) 2006 Tresys Technology, LLC
5*2d543d20SAndroid Build Coastguard Worker *
6*2d543d20SAndroid Build Coastguard Worker * This library is free software; you can redistribute it and/or
7*2d543d20SAndroid Build Coastguard Worker * modify it under the terms of the GNU Lesser General Public
8*2d543d20SAndroid Build Coastguard Worker * License as published by the Free Software Foundation; either
9*2d543d20SAndroid Build Coastguard Worker * version 2.1 of the License, or (at your option) any later version.
10*2d543d20SAndroid Build Coastguard Worker *
11*2d543d20SAndroid Build Coastguard Worker * This library is distributed in the hope that it will be useful,
12*2d543d20SAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*2d543d20SAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14*2d543d20SAndroid Build Coastguard Worker * Lesser General Public License for more details.
15*2d543d20SAndroid Build Coastguard Worker *
16*2d543d20SAndroid Build Coastguard Worker * You should have received a copy of the GNU Lesser General Public
17*2d543d20SAndroid Build Coastguard Worker * License along with this library; if not, write to the Free Software
18*2d543d20SAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19*2d543d20SAndroid Build Coastguard Worker */
20*2d543d20SAndroid Build Coastguard Worker
21*2d543d20SAndroid Build Coastguard Worker #include "test-linker-roles.h"
22*2d543d20SAndroid Build Coastguard Worker #include "parse_util.h"
23*2d543d20SAndroid Build Coastguard Worker #include "helpers.h"
24*2d543d20SAndroid Build Coastguard Worker #include "test-common.h"
25*2d543d20SAndroid Build Coastguard Worker
26*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/policydb.h>
27*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/link.h>
28*2d543d20SAndroid Build Coastguard Worker
29*2d543d20SAndroid Build Coastguard Worker #include <CUnit/Basic.h>
30*2d543d20SAndroid Build Coastguard Worker #include <stdlib.h>
31*2d543d20SAndroid Build Coastguard Worker
32*2d543d20SAndroid Build Coastguard Worker /* Tests for roles:
33*2d543d20SAndroid Build Coastguard Worker * Test for each of these for
34*2d543d20SAndroid Build Coastguard Worker * - role in appropriate symtab (global and decl)
35*2d543d20SAndroid Build Coastguard Worker * - datum in the decl symtab has correct type_set
36*2d543d20SAndroid Build Coastguard Worker * - scope datum has correct decl ids
37*2d543d20SAndroid Build Coastguard Worker * - dominates bitmap is correct
38*2d543d20SAndroid Build Coastguard Worker * Tests:
39*2d543d20SAndroid Build Coastguard Worker * - role in base, no modules
40*2d543d20SAndroid Build Coastguard Worker * - role in base optional, no modules
41*2d543d20SAndroid Build Coastguard Worker * - role a in base, b in module
42*2d543d20SAndroid Build Coastguard Worker * - role a in base and module (additive)
43*2d543d20SAndroid Build Coastguard Worker * - role a in base and 2 module
44*2d543d20SAndroid Build Coastguard Worker * - role a in base optional, b in module
45*2d543d20SAndroid Build Coastguard Worker * - role a in base, b in module optional
46*2d543d20SAndroid Build Coastguard Worker * - role a in base optional, b in module optional
47*2d543d20SAndroid Build Coastguard Worker * - role a in base optional and module
48*2d543d20SAndroid Build Coastguard Worker * - role a in base and module optional
49*2d543d20SAndroid Build Coastguard Worker * - role a in base optional and module optional
50*2d543d20SAndroid Build Coastguard Worker * - role a in base optional and 2 modules
51*2d543d20SAndroid Build Coastguard Worker * - role a and b in base, b dom a, are types correct (TODO)
52*2d543d20SAndroid Build Coastguard Worker */
53*2d543d20SAndroid Build Coastguard Worker
54*2d543d20SAndroid Build Coastguard Worker /* this simply tests whether the passed in role only has its own
55*2d543d20SAndroid Build Coastguard Worker * value in its dominates ebitmap */
only_dominates_self(policydb_t * p,role_datum_t * role)56*2d543d20SAndroid Build Coastguard Worker static void only_dominates_self(policydb_t * p __attribute__ ((unused)), role_datum_t * role)
57*2d543d20SAndroid Build Coastguard Worker {
58*2d543d20SAndroid Build Coastguard Worker ebitmap_node_t *tnode;
59*2d543d20SAndroid Build Coastguard Worker unsigned int i;
60*2d543d20SAndroid Build Coastguard Worker int found = 0;
61*2d543d20SAndroid Build Coastguard Worker
62*2d543d20SAndroid Build Coastguard Worker ebitmap_for_each_positive_bit(&role->dominates, tnode, i) {
63*2d543d20SAndroid Build Coastguard Worker found++;
64*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(i == role->s.value - 1);
65*2d543d20SAndroid Build Coastguard Worker }
66*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(found == 1);
67*2d543d20SAndroid Build Coastguard Worker }
68*2d543d20SAndroid Build Coastguard Worker
base_role_tests(policydb_t * base)69*2d543d20SAndroid Build Coastguard Worker void base_role_tests(policydb_t * base)
70*2d543d20SAndroid Build Coastguard Worker {
71*2d543d20SAndroid Build Coastguard Worker avrule_decl_t *decl;
72*2d543d20SAndroid Build Coastguard Worker role_datum_t *role;
73*2d543d20SAndroid Build Coastguard Worker unsigned int decls[2];
74*2d543d20SAndroid Build Coastguard Worker const char *types[2];
75*2d543d20SAndroid Build Coastguard Worker
76*2d543d20SAndroid Build Coastguard Worker /* These tests look at roles in the base only, the desire is to ensure that
77*2d543d20SAndroid Build Coastguard Worker * roles are not destroyed or otherwise removed during the link process */
78*2d543d20SAndroid Build Coastguard Worker
79*2d543d20SAndroid Build Coastguard Worker /**** test for g_b_role_1 in base and decl 1 (global) ****/
80*2d543d20SAndroid Build Coastguard Worker decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
81*2d543d20SAndroid Build Coastguard Worker test_sym_presence(base, "g_b_role_1", SYM_ROLES, SCOPE_DECL, decls, 1);
82*2d543d20SAndroid Build Coastguard Worker /* make sure it has the correct type set (g_b_type_1, no negset, no flags) */
83*2d543d20SAndroid Build Coastguard Worker types[0] = "g_b_type_1";
84*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "g_b_role_1", NULL, types, 1, 0);
85*2d543d20SAndroid Build Coastguard Worker /* This role should only dominate itself */
86*2d543d20SAndroid Build Coastguard Worker only_dominates_self(base, role);
87*2d543d20SAndroid Build Coastguard Worker
88*2d543d20SAndroid Build Coastguard Worker /**** test for o1_b_role_1 in optional (decl 2) ****/
89*2d543d20SAndroid Build Coastguard Worker decl = test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b");
90*2d543d20SAndroid Build Coastguard Worker decls[0] = decl->decl_id;
91*2d543d20SAndroid Build Coastguard Worker test_sym_presence(base, "o1_b_role_1", SYM_ROLES, SCOPE_DECL, decls, 1);
92*2d543d20SAndroid Build Coastguard Worker /* make sure it has the correct type set (o1_b_type_1, no negset, no flags) */
93*2d543d20SAndroid Build Coastguard Worker types[0] = "o1_b_type_1";
94*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "o1_b_role_1", decl, types, 1, 0);
95*2d543d20SAndroid Build Coastguard Worker /* and only dominates itself */
96*2d543d20SAndroid Build Coastguard Worker only_dominates_self(base, role);
97*2d543d20SAndroid Build Coastguard Worker }
98*2d543d20SAndroid Build Coastguard Worker
module_role_tests(policydb_t * base)99*2d543d20SAndroid Build Coastguard Worker void module_role_tests(policydb_t * base)
100*2d543d20SAndroid Build Coastguard Worker {
101*2d543d20SAndroid Build Coastguard Worker role_datum_t *role;
102*2d543d20SAndroid Build Coastguard Worker avrule_decl_t *decl;
103*2d543d20SAndroid Build Coastguard Worker unsigned int decls[3];
104*2d543d20SAndroid Build Coastguard Worker const char *types[3];
105*2d543d20SAndroid Build Coastguard Worker
106*2d543d20SAndroid Build Coastguard Worker /* These tests are run when the base is linked with 2 modules,
107*2d543d20SAndroid Build Coastguard Worker * They should test whether the roles get copied correctly from the
108*2d543d20SAndroid Build Coastguard Worker * modules into the base */
109*2d543d20SAndroid Build Coastguard Worker
110*2d543d20SAndroid Build Coastguard Worker /**** test for role in module 1 (global) ****/
111*2d543d20SAndroid Build Coastguard Worker decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
112*2d543d20SAndroid Build Coastguard Worker test_sym_presence(base, "g_m1_role_1", SYM_ROLES, SCOPE_DECL, decls, 1);
113*2d543d20SAndroid Build Coastguard Worker /* make sure it has the correct type set (g_m1_type_1, no negset, no flags) */
114*2d543d20SAndroid Build Coastguard Worker types[0] = "g_m1_type_1";
115*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "g_m1_role_1", NULL, types, 1, 0);
116*2d543d20SAndroid Build Coastguard Worker /* and only dominates itself */
117*2d543d20SAndroid Build Coastguard Worker only_dominates_self(base, role);
118*2d543d20SAndroid Build Coastguard Worker
119*2d543d20SAndroid Build Coastguard Worker /**** test for role in module 1 (optional) ****/
120*2d543d20SAndroid Build Coastguard Worker decl = test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_m1");
121*2d543d20SAndroid Build Coastguard Worker decls[0] = decl->decl_id;
122*2d543d20SAndroid Build Coastguard Worker test_sym_presence(base, "o1_m1_role_1", SYM_ROLES, SCOPE_DECL, decls, 1);
123*2d543d20SAndroid Build Coastguard Worker /* make sure it has the correct type set (o1_m1_type_1, no negset, no flags) */
124*2d543d20SAndroid Build Coastguard Worker types[0] = "o1_m1_type_1";
125*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "o1_m1_role_1", decl, types, 1, 0);
126*2d543d20SAndroid Build Coastguard Worker /* and only dominates itself */
127*2d543d20SAndroid Build Coastguard Worker only_dominates_self(base, role);
128*2d543d20SAndroid Build Coastguard Worker
129*2d543d20SAndroid Build Coastguard Worker /* These test whether the type sets are copied to the right place and
130*2d543d20SAndroid Build Coastguard Worker * correctly unioned when they should be */
131*2d543d20SAndroid Build Coastguard Worker
132*2d543d20SAndroid Build Coastguard Worker /**** test for type added to base role in module 1 (global) ****/
133*2d543d20SAndroid Build Coastguard Worker decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
134*2d543d20SAndroid Build Coastguard Worker test_sym_presence(base, "g_b_role_2", SYM_ROLES, SCOPE_DECL, decls, 1);
135*2d543d20SAndroid Build Coastguard Worker /* make sure it has the correct type set (g_m1_type_1, no negset, no flags) */
136*2d543d20SAndroid Build Coastguard Worker types[0] = "g_b_type_2"; /* added in base when declared */
137*2d543d20SAndroid Build Coastguard Worker types[1] = "g_m1_type_1"; /* added in module */
138*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "g_b_role_2", NULL, types, 2, 0);
139*2d543d20SAndroid Build Coastguard Worker /* and only dominates itself */
140*2d543d20SAndroid Build Coastguard Worker only_dominates_self(base, role);
141*2d543d20SAndroid Build Coastguard Worker
142*2d543d20SAndroid Build Coastguard Worker /**** test for type added to base role in module 1 & 2 (global) ****/
143*2d543d20SAndroid Build Coastguard Worker decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
144*2d543d20SAndroid Build Coastguard Worker decls[1] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
145*2d543d20SAndroid Build Coastguard Worker decls[2] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m2"))->decl_id;
146*2d543d20SAndroid Build Coastguard Worker test_sym_presence(base, "g_b_role_3", SYM_ROLES, SCOPE_DECL, decls, 3);
147*2d543d20SAndroid Build Coastguard Worker /* make sure it has the correct type set (g_b_type_2, g_m1_type_2, g_m2_type_2, no negset, no flags) */
148*2d543d20SAndroid Build Coastguard Worker types[0] = "g_b_type_2"; /* added in base when declared */
149*2d543d20SAndroid Build Coastguard Worker types[1] = "g_m1_type_2"; /* added in module 1 */
150*2d543d20SAndroid Build Coastguard Worker types[2] = "g_m2_type_2"; /* added in module 2 */
151*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "g_b_role_3", NULL, types, 3, 0);
152*2d543d20SAndroid Build Coastguard Worker /* and only dominates itself */
153*2d543d20SAndroid Build Coastguard Worker only_dominates_self(base, role);
154*2d543d20SAndroid Build Coastguard Worker
155*2d543d20SAndroid Build Coastguard Worker /**** test for role in base optional and module 1 (additive) ****/
156*2d543d20SAndroid Build Coastguard Worker decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b"))->decl_id;
157*2d543d20SAndroid Build Coastguard Worker decls[1] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
158*2d543d20SAndroid Build Coastguard Worker test_sym_presence(base, "o1_b_role_2", SYM_ROLES, SCOPE_DECL, decls, 2);
159*2d543d20SAndroid Build Coastguard Worker /* this one will have 2 type sets, one in the global symtab and one in the base optional 1 */
160*2d543d20SAndroid Build Coastguard Worker types[0] = "g_m1_type_1";
161*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "o1_b_role_2", NULL, types, 1, 0);
162*2d543d20SAndroid Build Coastguard Worker types[0] = "o1_b_type_1";
163*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "o1_b_role_2", test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b"), types, 1, 0);
164*2d543d20SAndroid Build Coastguard Worker /* and only dominates itself */
165*2d543d20SAndroid Build Coastguard Worker only_dominates_self(base, role);
166*2d543d20SAndroid Build Coastguard Worker
167*2d543d20SAndroid Build Coastguard Worker /**** test for role in base and module 1 optional (additive) ****/
168*2d543d20SAndroid Build Coastguard Worker decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"))->decl_id;
169*2d543d20SAndroid Build Coastguard Worker decls[1] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o2_m1"))->decl_id;
170*2d543d20SAndroid Build Coastguard Worker test_sym_presence(base, "g_b_role_4", SYM_ROLES, SCOPE_DECL, decls, 2);
171*2d543d20SAndroid Build Coastguard Worker /* this one will have 2 type sets, one in the global symtab and one in the base optional 1 */
172*2d543d20SAndroid Build Coastguard Worker types[0] = "g_b_type_2";
173*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "g_b_role_4", NULL, types, 1, 0);
174*2d543d20SAndroid Build Coastguard Worker types[0] = "g_m1_type_2";
175*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "g_b_role_4", test_find_decl_by_sym(base, SYM_TYPES, "tag_o2_m1"), types, 1, 0);
176*2d543d20SAndroid Build Coastguard Worker /* and only dominates itself */
177*2d543d20SAndroid Build Coastguard Worker only_dominates_self(base, role);
178*2d543d20SAndroid Build Coastguard Worker
179*2d543d20SAndroid Build Coastguard Worker /**** test for role in base and module 1 optional (additive) ****/
180*2d543d20SAndroid Build Coastguard Worker decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o3_b"))->decl_id;
181*2d543d20SAndroid Build Coastguard Worker decls[1] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o3_m1"))->decl_id;
182*2d543d20SAndroid Build Coastguard Worker test_sym_presence(base, "o3_b_role_1", SYM_ROLES, SCOPE_DECL, decls, 2);
183*2d543d20SAndroid Build Coastguard Worker /* this one will have 2 type sets, one in the 3rd base optional and one in the 3rd module optional */
184*2d543d20SAndroid Build Coastguard Worker types[0] = "o3_b_type_1";
185*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "o3_b_role_1", test_find_decl_by_sym(base, SYM_TYPES, "tag_o3_b"), types, 1, 0);
186*2d543d20SAndroid Build Coastguard Worker types[0] = "o3_m1_type_1";
187*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "o3_b_role_1", test_find_decl_by_sym(base, SYM_TYPES, "tag_o3_m1"), types, 1, 0);
188*2d543d20SAndroid Build Coastguard Worker /* and only dominates itself */
189*2d543d20SAndroid Build Coastguard Worker only_dominates_self(base, role);
190*2d543d20SAndroid Build Coastguard Worker
191*2d543d20SAndroid Build Coastguard Worker /**** test for role in base and module 1 optional (additive) ****/
192*2d543d20SAndroid Build Coastguard Worker decls[0] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_o4_b"))->decl_id;
193*2d543d20SAndroid Build Coastguard Worker decls[1] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"))->decl_id;
194*2d543d20SAndroid Build Coastguard Worker decls[2] = (test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m2"))->decl_id;
195*2d543d20SAndroid Build Coastguard Worker test_sym_presence(base, "o4_b_role_1", SYM_ROLES, SCOPE_DECL, decls, 3);
196*2d543d20SAndroid Build Coastguard Worker /* this one will have 2 type sets, one in the global symtab (with both module types) and one in the 4th optional of base */
197*2d543d20SAndroid Build Coastguard Worker types[0] = "g_m1_type_1";
198*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "o4_b_role_1", test_find_decl_by_sym(base, SYM_TYPES, "tag_o4_b"), types, 1, 0);
199*2d543d20SAndroid Build Coastguard Worker types[0] = "g_m2_type_1";
200*2d543d20SAndroid Build Coastguard Worker types[1] = "g_m1_type_2";
201*2d543d20SAndroid Build Coastguard Worker role = test_role_type_set(base, "o4_b_role_1", NULL, types, 2, 0);
202*2d543d20SAndroid Build Coastguard Worker /* and only dominates itself */
203*2d543d20SAndroid Build Coastguard Worker only_dominates_self(base, role);
204*2d543d20SAndroid Build Coastguard Worker }
205