xref: /aosp_15_r20/external/selinux/libsepol/tests/test-linker.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 /* This is where the linker tests should go, including:
22*2d543d20SAndroid Build Coastguard Worker  * - check role, type, bool, user, attr mapping
23*2d543d20SAndroid Build Coastguard Worker  * - check for properly enabled optional
24*2d543d20SAndroid Build Coastguard Worker  * - check for properly disabled optional
25*2d543d20SAndroid Build Coastguard Worker  * - check for non-optional disabled blocks
26*2d543d20SAndroid Build Coastguard Worker  * - properly add symbols declared in optionals
27*2d543d20SAndroid Build Coastguard Worker  */
28*2d543d20SAndroid Build Coastguard Worker 
29*2d543d20SAndroid Build Coastguard Worker #include "test-linker.h"
30*2d543d20SAndroid Build Coastguard Worker #include "parse_util.h"
31*2d543d20SAndroid Build Coastguard Worker #include "helpers.h"
32*2d543d20SAndroid Build Coastguard Worker #include "test-common.h"
33*2d543d20SAndroid Build Coastguard Worker #include "test-linker-roles.h"
34*2d543d20SAndroid Build Coastguard Worker #include "test-linker-types.h"
35*2d543d20SAndroid Build Coastguard Worker #include "test-linker-cond-map.h"
36*2d543d20SAndroid Build Coastguard Worker 
37*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/policydb.h>
38*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/link.h>
39*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/conditional.h>
40*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/expand.h>
41*2d543d20SAndroid Build Coastguard Worker #include <limits.h>
42*2d543d20SAndroid Build Coastguard Worker #include <stdlib.h>
43*2d543d20SAndroid Build Coastguard Worker 
44*2d543d20SAndroid Build Coastguard Worker #define NUM_MODS 2
45*2d543d20SAndroid Build Coastguard Worker #define NUM_POLICIES NUM_MODS+1
46*2d543d20SAndroid Build Coastguard Worker 
47*2d543d20SAndroid Build Coastguard Worker #define BASEMOD NUM_MODS
48*2d543d20SAndroid Build Coastguard Worker const char *policies[NUM_POLICIES] = {
49*2d543d20SAndroid Build Coastguard Worker 	"module1.conf",
50*2d543d20SAndroid Build Coastguard Worker 	"module2.conf",
51*2d543d20SAndroid Build Coastguard Worker 	"small-base.conf",
52*2d543d20SAndroid Build Coastguard Worker };
53*2d543d20SAndroid Build Coastguard Worker 
54*2d543d20SAndroid Build Coastguard Worker static policydb_t basenomods;
55*2d543d20SAndroid Build Coastguard Worker static policydb_t linkedbase;
56*2d543d20SAndroid Build Coastguard Worker static policydb_t *modules[NUM_MODS];
57*2d543d20SAndroid Build Coastguard Worker extern int mls;
58*2d543d20SAndroid Build Coastguard Worker 
linker_test_init(void)59*2d543d20SAndroid Build Coastguard Worker int linker_test_init(void)
60*2d543d20SAndroid Build Coastguard Worker {
61*2d543d20SAndroid Build Coastguard Worker 	int i;
62*2d543d20SAndroid Build Coastguard Worker 
63*2d543d20SAndroid Build Coastguard Worker 	if (test_load_policy(&linkedbase, POLICY_BASE, mls, "test-linker", policies[BASEMOD]))
64*2d543d20SAndroid Build Coastguard Worker 		return -1;
65*2d543d20SAndroid Build Coastguard Worker 
66*2d543d20SAndroid Build Coastguard Worker 	if (test_load_policy(&basenomods, POLICY_BASE, mls, "test-linker", policies[BASEMOD]))
67*2d543d20SAndroid Build Coastguard Worker 		return -1;
68*2d543d20SAndroid Build Coastguard Worker 
69*2d543d20SAndroid Build Coastguard Worker 	for (i = 0; i < NUM_MODS; i++) {
70*2d543d20SAndroid Build Coastguard Worker 
71*2d543d20SAndroid Build Coastguard Worker 		modules[i] = calloc(1, sizeof(*modules[i]));
72*2d543d20SAndroid Build Coastguard Worker 		if (!modules[i]) {
73*2d543d20SAndroid Build Coastguard Worker 			fprintf(stderr, "out of memory!\n");
74*2d543d20SAndroid Build Coastguard Worker 			return -1;
75*2d543d20SAndroid Build Coastguard Worker 		}
76*2d543d20SAndroid Build Coastguard Worker 
77*2d543d20SAndroid Build Coastguard Worker 		if (test_load_policy(modules[i], POLICY_MOD, mls, "test-linker", policies[i]))
78*2d543d20SAndroid Build Coastguard Worker 			return -1;
79*2d543d20SAndroid Build Coastguard Worker 
80*2d543d20SAndroid Build Coastguard Worker 	}
81*2d543d20SAndroid Build Coastguard Worker 
82*2d543d20SAndroid Build Coastguard Worker 	if (link_modules(NULL, &linkedbase, modules, NUM_MODS, 0)) {
83*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "link modules failed\n");
84*2d543d20SAndroid Build Coastguard Worker 		return -1;
85*2d543d20SAndroid Build Coastguard Worker 	}
86*2d543d20SAndroid Build Coastguard Worker 
87*2d543d20SAndroid Build Coastguard Worker 	if (link_modules(NULL, &basenomods, NULL, 0, 0)) {
88*2d543d20SAndroid Build Coastguard Worker 		fprintf(stderr, "link modules failed\n");
89*2d543d20SAndroid Build Coastguard Worker 		return -1;
90*2d543d20SAndroid Build Coastguard Worker 	}
91*2d543d20SAndroid Build Coastguard Worker 
92*2d543d20SAndroid Build Coastguard Worker 	return 0;
93*2d543d20SAndroid Build Coastguard Worker }
94*2d543d20SAndroid Build Coastguard Worker 
linker_test_cleanup(void)95*2d543d20SAndroid Build Coastguard Worker int linker_test_cleanup(void)
96*2d543d20SAndroid Build Coastguard Worker {
97*2d543d20SAndroid Build Coastguard Worker 	int i;
98*2d543d20SAndroid Build Coastguard Worker 
99*2d543d20SAndroid Build Coastguard Worker 	policydb_destroy(&basenomods);
100*2d543d20SAndroid Build Coastguard Worker 	policydb_destroy(&linkedbase);
101*2d543d20SAndroid Build Coastguard Worker 
102*2d543d20SAndroid Build Coastguard Worker 	for (i = 0; i < NUM_MODS; i++) {
103*2d543d20SAndroid Build Coastguard Worker 		policydb_destroy(modules[i]);
104*2d543d20SAndroid Build Coastguard Worker 		free(modules[i]);
105*2d543d20SAndroid Build Coastguard Worker 	}
106*2d543d20SAndroid Build Coastguard Worker 	return 0;
107*2d543d20SAndroid Build Coastguard Worker }
108*2d543d20SAndroid Build Coastguard Worker 
test_linker_indexes(void)109*2d543d20SAndroid Build Coastguard Worker static void test_linker_indexes(void)
110*2d543d20SAndroid Build Coastguard Worker {
111*2d543d20SAndroid Build Coastguard Worker 	test_policydb_indexes(&linkedbase);
112*2d543d20SAndroid Build Coastguard Worker }
113*2d543d20SAndroid Build Coastguard Worker 
test_linker_roles(void)114*2d543d20SAndroid Build Coastguard Worker static void test_linker_roles(void)
115*2d543d20SAndroid Build Coastguard Worker {
116*2d543d20SAndroid Build Coastguard Worker 	base_role_tests(&basenomods);
117*2d543d20SAndroid Build Coastguard Worker 	base_role_tests(&linkedbase);
118*2d543d20SAndroid Build Coastguard Worker 	module_role_tests(&linkedbase);
119*2d543d20SAndroid Build Coastguard Worker }
120*2d543d20SAndroid Build Coastguard Worker 
test_linker_types(void)121*2d543d20SAndroid Build Coastguard Worker static void test_linker_types(void)
122*2d543d20SAndroid Build Coastguard Worker {
123*2d543d20SAndroid Build Coastguard Worker 	base_type_tests(&basenomods);
124*2d543d20SAndroid Build Coastguard Worker 	base_type_tests(&linkedbase);
125*2d543d20SAndroid Build Coastguard Worker 	module_type_tests(&linkedbase);
126*2d543d20SAndroid Build Coastguard Worker }
127*2d543d20SAndroid Build Coastguard Worker 
test_linker_cond(void)128*2d543d20SAndroid Build Coastguard Worker static void test_linker_cond(void)
129*2d543d20SAndroid Build Coastguard Worker {
130*2d543d20SAndroid Build Coastguard Worker 	base_cond_tests(&basenomods);
131*2d543d20SAndroid Build Coastguard Worker 	base_cond_tests(&linkedbase);
132*2d543d20SAndroid Build Coastguard Worker 	module_cond_tests(&linkedbase);
133*2d543d20SAndroid Build Coastguard Worker }
134*2d543d20SAndroid Build Coastguard Worker 
linker_add_tests(CU_pSuite suite)135*2d543d20SAndroid Build Coastguard Worker int linker_add_tests(CU_pSuite suite)
136*2d543d20SAndroid Build Coastguard Worker {
137*2d543d20SAndroid Build Coastguard Worker 	if (NULL == CU_add_test(suite, "linker_indexes", test_linker_indexes)) {
138*2d543d20SAndroid Build Coastguard Worker 		CU_cleanup_registry();
139*2d543d20SAndroid Build Coastguard Worker 		return CU_get_error();
140*2d543d20SAndroid Build Coastguard Worker 	}
141*2d543d20SAndroid Build Coastguard Worker 	if (NULL == CU_add_test(suite, "linker_types", test_linker_types)) {
142*2d543d20SAndroid Build Coastguard Worker 		CU_cleanup_registry();
143*2d543d20SAndroid Build Coastguard Worker 		return CU_get_error();
144*2d543d20SAndroid Build Coastguard Worker 	}
145*2d543d20SAndroid Build Coastguard Worker 	if (NULL == CU_add_test(suite, "linker_roles", test_linker_roles)) {
146*2d543d20SAndroid Build Coastguard Worker 		CU_cleanup_registry();
147*2d543d20SAndroid Build Coastguard Worker 		return CU_get_error();
148*2d543d20SAndroid Build Coastguard Worker 	}
149*2d543d20SAndroid Build Coastguard Worker 	if (NULL == CU_add_test(suite, "linker_cond", test_linker_cond)) {
150*2d543d20SAndroid Build Coastguard Worker 		CU_cleanup_registry();
151*2d543d20SAndroid Build Coastguard Worker 		return CU_get_error();
152*2d543d20SAndroid Build Coastguard Worker 	}
153*2d543d20SAndroid Build Coastguard Worker 	return 0;
154*2d543d20SAndroid Build Coastguard Worker }
155