• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Author: Joshua Brindle <jbrindle@tresys.com>
3  *
4  * Copyright (C) 2006 Tresys Technology, LLC
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2.1 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *  Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this library; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20 
21 /* This is where the linker tests should go, including:
22  * - check role, type, bool, user, attr mapping
23  * - check for properly enabled optional
24  * - check for properly disabled optional
25  * - check for non-optional disabled blocks
26  * - properly add symbols declared in optionals
27  */
28 
29 #include "test-linker.h"
30 #include "parse_util.h"
31 #include "helpers.h"
32 #include "test-common.h"
33 #include "test-linker-roles.h"
34 #include "test-linker-types.h"
35 #include "test-linker-cond-map.h"
36 
37 #include <sepol/policydb/policydb.h>
38 #include <sepol/policydb/link.h>
39 #include <sepol/policydb/conditional.h>
40 #include <sepol/policydb/expand.h>
41 #include <limits.h>
42 #include <stdlib.h>
43 
44 #define NUM_MODS 2
45 #define NUM_POLICIES NUM_MODS+1
46 
47 #define BASEMOD NUM_MODS
48 const char *policies[NUM_POLICIES] = {
49 	"module1.conf",
50 	"module2.conf",
51 	"small-base.conf",
52 };
53 
54 static policydb_t basenomods;
55 static policydb_t linkedbase;
56 static policydb_t *modules[NUM_MODS];
57 extern int mls;
58 
linker_test_init(void)59 int linker_test_init(void)
60 {
61 	int i;
62 
63 	if (test_load_policy(&linkedbase, POLICY_BASE, mls, "test-linker", policies[BASEMOD]))
64 		return -1;
65 
66 	if (test_load_policy(&basenomods, POLICY_BASE, mls, "test-linker", policies[BASEMOD]))
67 		return -1;
68 
69 	for (i = 0; i < NUM_MODS; i++) {
70 
71 		modules[i] = calloc(1, sizeof(*modules[i]));
72 		if (!modules[i]) {
73 			fprintf(stderr, "out of memory!\n");
74 			return -1;
75 		}
76 
77 		if (test_load_policy(modules[i], POLICY_MOD, mls, "test-linker", policies[i]))
78 			return -1;
79 
80 	}
81 
82 	if (link_modules(NULL, &linkedbase, modules, NUM_MODS, 0)) {
83 		fprintf(stderr, "link modules failed\n");
84 		return -1;
85 	}
86 
87 	if (link_modules(NULL, &basenomods, NULL, 0, 0)) {
88 		fprintf(stderr, "link modules failed\n");
89 		return -1;
90 	}
91 
92 	return 0;
93 }
94 
linker_test_cleanup(void)95 int linker_test_cleanup(void)
96 {
97 	int i;
98 
99 	policydb_destroy(&basenomods);
100 	policydb_destroy(&linkedbase);
101 
102 	for (i = 0; i < NUM_MODS; i++) {
103 		policydb_destroy(modules[i]);
104 		free(modules[i]);
105 	}
106 	return 0;
107 }
108 
test_linker_indexes(void)109 static void test_linker_indexes(void)
110 {
111 	test_policydb_indexes(&linkedbase);
112 }
113 
test_linker_roles(void)114 static void test_linker_roles(void)
115 {
116 	base_role_tests(&basenomods);
117 	base_role_tests(&linkedbase);
118 	module_role_tests(&linkedbase);
119 }
120 
test_linker_types(void)121 static void test_linker_types(void)
122 {
123 	base_type_tests(&basenomods);
124 	base_type_tests(&linkedbase);
125 	module_type_tests(&linkedbase);
126 }
127 
test_linker_cond(void)128 static void test_linker_cond(void)
129 {
130 	base_cond_tests(&basenomods);
131 	base_cond_tests(&linkedbase);
132 	module_cond_tests(&linkedbase);
133 }
134 
linker_add_tests(CU_pSuite suite)135 int linker_add_tests(CU_pSuite suite)
136 {
137 	if (NULL == CU_add_test(suite, "linker_indexes", test_linker_indexes)) {
138 		CU_cleanup_registry();
139 		return CU_get_error();
140 	}
141 	if (NULL == CU_add_test(suite, "linker_types", test_linker_types)) {
142 		CU_cleanup_registry();
143 		return CU_get_error();
144 	}
145 	if (NULL == CU_add_test(suite, "linker_roles", test_linker_roles)) {
146 		CU_cleanup_registry();
147 		return CU_get_error();
148 	}
149 	if (NULL == CU_add_test(suite, "linker_cond", test_linker_cond)) {
150 		CU_cleanup_registry();
151 		return CU_get_error();
152 	}
153 	return 0;
154 }
155