xref: /aosp_15_r20/external/selinux/libsepol/tests/test-linker-cond-map.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
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-cond-map.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 #include <sepol/policydb/conditional.h>
29*2d543d20SAndroid Build Coastguard Worker 
30*2d543d20SAndroid Build Coastguard Worker #include <CUnit/Basic.h>
31*2d543d20SAndroid Build Coastguard Worker #include <stdlib.h>
32*2d543d20SAndroid Build Coastguard Worker 
33*2d543d20SAndroid Build Coastguard Worker /* Tests for conditionals
34*2d543d20SAndroid Build Coastguard Worker  * Test each cond/bool for these
35*2d543d20SAndroid Build Coastguard Worker  * - boolean copied correctly (state is correct)
36*2d543d20SAndroid Build Coastguard Worker  * - conditional expression is correct
37*2d543d20SAndroid Build Coastguard Worker  * Tests:
38*2d543d20SAndroid Build Coastguard Worker  * - single boolean in base
39*2d543d20SAndroid Build Coastguard Worker  * - single boolean in module
40*2d543d20SAndroid Build Coastguard Worker  * - single boolean in base optional
41*2d543d20SAndroid Build Coastguard Worker  * - single boolean in module optional
42*2d543d20SAndroid Build Coastguard Worker  * - 2 booleans in base
43*2d543d20SAndroid Build Coastguard Worker  * - 2 booleans in module
44*2d543d20SAndroid Build Coastguard Worker  * - 2 booleans in base optional
45*2d543d20SAndroid Build Coastguard Worker  * - 2 booleans in module optional
46*2d543d20SAndroid Build Coastguard Worker  * - 2 booleans, base and module
47*2d543d20SAndroid Build Coastguard Worker  * - 2 booleans, base optional and module
48*2d543d20SAndroid Build Coastguard Worker  * - 2 booleans, base optional and module optional
49*2d543d20SAndroid Build Coastguard Worker  * - 3 booleans, base, base optional, module
50*2d543d20SAndroid Build Coastguard Worker  * - 4 boolean, base, base optional, module, module optional
51*2d543d20SAndroid Build Coastguard Worker  */
52*2d543d20SAndroid Build Coastguard Worker 
53*2d543d20SAndroid Build Coastguard Worker typedef struct test_cond_expr {
54*2d543d20SAndroid Build Coastguard Worker 	const char *boolean;
55*2d543d20SAndroid Build Coastguard Worker 	uint32_t expr_type;
56*2d543d20SAndroid Build Coastguard Worker } test_cond_expr_t;
57*2d543d20SAndroid Build Coastguard Worker 
test_cond_expr_mapping(policydb_t * p,avrule_decl_t * d,test_cond_expr_t * bools,int len)58*2d543d20SAndroid Build Coastguard Worker static void test_cond_expr_mapping(policydb_t * p, avrule_decl_t * d, test_cond_expr_t * bools, int len)
59*2d543d20SAndroid Build Coastguard Worker {
60*2d543d20SAndroid Build Coastguard Worker 	int i;
61*2d543d20SAndroid Build Coastguard Worker 	cond_expr_t *expr;
62*2d543d20SAndroid Build Coastguard Worker 
63*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(d->cond_list != NULL);
64*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(d->cond_list->expr != NULL);
65*2d543d20SAndroid Build Coastguard Worker 
66*2d543d20SAndroid Build Coastguard Worker 	expr = d->cond_list->expr;
67*2d543d20SAndroid Build Coastguard Worker 
68*2d543d20SAndroid Build Coastguard Worker 	for (i = 0; i < len; i++) {
69*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT_FATAL(expr != NULL);
70*2d543d20SAndroid Build Coastguard Worker 
71*2d543d20SAndroid Build Coastguard Worker 		CU_ASSERT(expr->expr_type == bools[i].expr_type);
72*2d543d20SAndroid Build Coastguard Worker 		if (bools[i].boolean) {
73*2d543d20SAndroid Build Coastguard Worker 			CU_ASSERT(strcmp(p->sym_val_to_name[SYM_BOOLS][expr->boolean - 1], bools[i].boolean) == 0);
74*2d543d20SAndroid Build Coastguard Worker 		}
75*2d543d20SAndroid Build Coastguard Worker 		expr = expr->next;
76*2d543d20SAndroid Build Coastguard Worker 	}
77*2d543d20SAndroid Build Coastguard Worker }
78*2d543d20SAndroid Build Coastguard Worker 
test_bool_state(policydb_t * p,const char * boolean,int state)79*2d543d20SAndroid Build Coastguard Worker static void test_bool_state(policydb_t * p, const char *boolean, int state)
80*2d543d20SAndroid Build Coastguard Worker {
81*2d543d20SAndroid Build Coastguard Worker 	cond_bool_datum_t *b;
82*2d543d20SAndroid Build Coastguard Worker 
83*2d543d20SAndroid Build Coastguard Worker 	b = hashtab_search(p->p_bools.table, boolean);
84*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(b != NULL);
85*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(b->state == state);
86*2d543d20SAndroid Build Coastguard Worker }
87*2d543d20SAndroid Build Coastguard Worker 
base_cond_tests(policydb_t * base)88*2d543d20SAndroid Build Coastguard Worker void base_cond_tests(policydb_t * base)
89*2d543d20SAndroid Build Coastguard Worker {
90*2d543d20SAndroid Build Coastguard Worker 	avrule_decl_t *d;
91*2d543d20SAndroid Build Coastguard Worker 	unsigned int decls[1];
92*2d543d20SAndroid Build Coastguard Worker 	test_cond_expr_t bools[2];
93*2d543d20SAndroid Build Coastguard Worker 
94*2d543d20SAndroid Build Coastguard Worker 	/* these tests look at booleans and conditionals in the base only
95*2d543d20SAndroid Build Coastguard Worker 	 * to ensure that they aren't altered or removed during the link process */
96*2d543d20SAndroid Build Coastguard Worker 
97*2d543d20SAndroid Build Coastguard Worker 	/* bool existence and state, global scope */
98*2d543d20SAndroid Build Coastguard Worker 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b");
99*2d543d20SAndroid Build Coastguard Worker 	decls[0] = d->decl_id;
100*2d543d20SAndroid Build Coastguard Worker 	test_sym_presence(base, "g_b_bool_1", SYM_BOOLS, SCOPE_DECL, decls, 1);
101*2d543d20SAndroid Build Coastguard Worker 	test_bool_state(base, "g_b_bool_1", 0);
102*2d543d20SAndroid Build Coastguard Worker 	/* conditional expression mapped correctly */
103*2d543d20SAndroid Build Coastguard Worker 	bools[0].boolean = "g_b_bool_1";
104*2d543d20SAndroid Build Coastguard Worker 	bools[0].expr_type = COND_BOOL;
105*2d543d20SAndroid Build Coastguard Worker 	test_cond_expr_mapping(base, d, bools, 1);
106*2d543d20SAndroid Build Coastguard Worker 
107*2d543d20SAndroid Build Coastguard Worker 	/* bool existence and state, optional scope */
108*2d543d20SAndroid Build Coastguard Worker 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b");
109*2d543d20SAndroid Build Coastguard Worker 	decls[0] = d->decl_id;
110*2d543d20SAndroid Build Coastguard Worker 	test_sym_presence(base, "o1_b_bool_1", SYM_BOOLS, SCOPE_DECL, decls, 1);
111*2d543d20SAndroid Build Coastguard Worker 	test_bool_state(base, "o1_b_bool_1", 1);
112*2d543d20SAndroid Build Coastguard Worker 	/* conditional expression mapped correctly */
113*2d543d20SAndroid Build Coastguard Worker 	bools[0].boolean = "o1_b_bool_1";
114*2d543d20SAndroid Build Coastguard Worker 	bools[0].expr_type = COND_BOOL;
115*2d543d20SAndroid Build Coastguard Worker 	test_cond_expr_mapping(base, d, bools, 1);
116*2d543d20SAndroid Build Coastguard Worker 
117*2d543d20SAndroid Build Coastguard Worker }
118*2d543d20SAndroid Build Coastguard Worker 
module_cond_tests(policydb_t * base)119*2d543d20SAndroid Build Coastguard Worker void module_cond_tests(policydb_t * base)
120*2d543d20SAndroid Build Coastguard Worker {
121*2d543d20SAndroid Build Coastguard Worker 	avrule_decl_t *d;
122*2d543d20SAndroid Build Coastguard Worker 	unsigned int decls[1];
123*2d543d20SAndroid Build Coastguard Worker 	test_cond_expr_t bools[3];
124*2d543d20SAndroid Build Coastguard Worker 
125*2d543d20SAndroid Build Coastguard Worker 	/* bool existence and state, module 1 global scope */
126*2d543d20SAndroid Build Coastguard Worker 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1");
127*2d543d20SAndroid Build Coastguard Worker 	decls[0] = d->decl_id;
128*2d543d20SAndroid Build Coastguard Worker 	test_sym_presence(base, "g_m1_bool_1", SYM_BOOLS, SCOPE_DECL, decls, 1);
129*2d543d20SAndroid Build Coastguard Worker 	test_bool_state(base, "g_m1_bool_1", 1);
130*2d543d20SAndroid Build Coastguard Worker 	/* conditional expression mapped correctly */
131*2d543d20SAndroid Build Coastguard Worker 	bools[0].boolean = "g_m1_bool_1";
132*2d543d20SAndroid Build Coastguard Worker 	bools[0].expr_type = COND_BOOL;
133*2d543d20SAndroid Build Coastguard Worker 	test_cond_expr_mapping(base, d, bools, 1);
134*2d543d20SAndroid Build Coastguard Worker 
135*2d543d20SAndroid Build Coastguard Worker 	/* bool existence and state, module 1 optional scope */
136*2d543d20SAndroid Build Coastguard Worker 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_m1");
137*2d543d20SAndroid Build Coastguard Worker 	decls[0] = d->decl_id;
138*2d543d20SAndroid Build Coastguard Worker 	test_sym_presence(base, "o1_m1_bool_1", SYM_BOOLS, SCOPE_DECL, decls, 1);
139*2d543d20SAndroid Build Coastguard Worker 	test_bool_state(base, "o1_m1_bool_1", 0);
140*2d543d20SAndroid Build Coastguard Worker 	/* conditional expression mapped correctly */
141*2d543d20SAndroid Build Coastguard Worker 	bools[0].boolean = "o1_m1_bool_1";
142*2d543d20SAndroid Build Coastguard Worker 	bools[0].expr_type = COND_BOOL;
143*2d543d20SAndroid Build Coastguard Worker 	test_cond_expr_mapping(base, d, bools, 1);
144*2d543d20SAndroid Build Coastguard Worker 
145*2d543d20SAndroid Build Coastguard Worker 	/* bool existence and state, module 2 global scope */
146*2d543d20SAndroid Build Coastguard Worker 	d = test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m2");
147*2d543d20SAndroid Build Coastguard Worker 	decls[0] = d->decl_id;
148*2d543d20SAndroid Build Coastguard Worker 	test_sym_presence(base, "g_m2_bool_1", SYM_BOOLS, SCOPE_DECL, decls, 1);
149*2d543d20SAndroid Build Coastguard Worker 	test_sym_presence(base, "g_m2_bool_2", SYM_BOOLS, SCOPE_DECL, decls, 1);
150*2d543d20SAndroid Build Coastguard Worker 	test_bool_state(base, "g_m2_bool_1", 1);
151*2d543d20SAndroid Build Coastguard Worker 	test_bool_state(base, "g_m2_bool_2", 0);
152*2d543d20SAndroid Build Coastguard Worker 	/* conditional expression mapped correctly */
153*2d543d20SAndroid Build Coastguard Worker 	bools[0].boolean = "g_m2_bool_1";
154*2d543d20SAndroid Build Coastguard Worker 	bools[0].expr_type = COND_BOOL;
155*2d543d20SAndroid Build Coastguard Worker 	bools[1].boolean = "g_m2_bool_2";
156*2d543d20SAndroid Build Coastguard Worker 	bools[1].expr_type = COND_BOOL;
157*2d543d20SAndroid Build Coastguard Worker 	bools[2].boolean = NULL;
158*2d543d20SAndroid Build Coastguard Worker 	bools[2].expr_type = COND_AND;
159*2d543d20SAndroid Build Coastguard Worker 	test_cond_expr_mapping(base, d, bools, 3);
160*2d543d20SAndroid Build Coastguard Worker }
161