1 /*
2  * Copyright 2011 Tresys Technology, LLC. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *    1. Redistributions of source code must retain the above copyright notice,
8  *       this list of conditions and the following disclaimer.
9  *
10  *    2. Redistributions in binary form must reproduce the above copyright notice,
11  *       this list of conditions and the following disclaimer in the documentation
12  *       and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY TRESYS TECHNOLOGY, LLC ``AS IS'' AND ANY EXPRESS
15  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17  * EVENT SHALL TRESYS TECHNOLOGY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * The views and conclusions contained in the software and documentation are those
26  * of the authors and should not be interpreted as representing official policies,
27  * either expressed or implied, of Tresys Technology, LLC.
28  */
29 
30 #include "CuTest.h"
31 #include "CilTest.h"
32 
33 #include "../../src/cil_internal.h"
34 #include "../../src/cil_build_ast.h"
35 
test_cil_list_init(CuTest * tc)36 void test_cil_list_init(CuTest *tc) {
37 	struct cil_avrule *test_avrule = malloc(sizeof(*test_avrule));
38 
39 	cil_classpermset_init(&test_avrule->classpermset);
40 	cil_permset_init(&test_avrule->classpermset->permset);
41 
42 	cil_list_init(&test_avrule->classpermset->permset->perms_list_str);
43 	CuAssertPtrNotNull(tc, test_avrule->classpermset->permset->perms_list_str);
44 
45 	cil_destroy_avrule(test_avrule);
46 }
47 
test_cil_list_append_item(CuTest * tc)48 void test_cil_list_append_item(CuTest *tc) {
49         char *line[] = {"(", "mlsconstrain", "(", "file", "dir", ")", "(", "create", "relabelto", ")", "(", "eq", "12", "h2", ")", ")", NULL};
50 
51 	struct cil_tree *test_tree;
52         gen_test_tree(&test_tree, line);
53 
54 	struct cil_tree_node *test_ast_node;
55 	cil_tree_node_init(&test_ast_node);
56 
57 	struct cil_db *test_db;
58 	cil_db_init(&test_db);
59 
60 	test_ast_node->parent = test_db->ast->root;
61 	test_ast_node->line = 1;
62 
63 	struct cil_list *test_class_list;
64 	cil_list_init(&test_class_list);
65 
66 	struct cil_list_item *test_new_item;
67 	cil_list_item_init(&test_new_item);
68 
69 	test_new_item->flavor = CIL_CLASS;
70 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head;
71 
72 	int rc = cil_list_append_item(test_class_list, test_new_item);
73 	CuAssertIntEquals(tc, SEPOL_OK, rc);
74 }
75 
test_cil_list_append_item_append(CuTest * tc)76 void test_cil_list_append_item_append(CuTest *tc) {
77         char *line[] = {"(", "mlsconstrain", "(", "file", "dir", ")", "(", "create", "relabelto", ")", "(", "eq", "12", "h2", ")", ")", NULL};
78 
79 	struct cil_tree *test_tree;
80         gen_test_tree(&test_tree, line);
81 
82 	struct cil_tree_node *test_ast_node;
83 	cil_tree_node_init(&test_ast_node);
84 
85 	struct cil_db *test_db;
86 	cil_db_init(&test_db);
87 
88 	test_ast_node->parent = test_db->ast->root;
89 	test_ast_node->line = 1;
90 
91 	struct cil_list *test_class_list;
92 	cil_list_init(&test_class_list);
93 
94 	struct cil_list_item *test_new_item;
95 	cil_list_item_init(&test_new_item);
96 
97 	test_new_item->flavor = CIL_CLASS;
98 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head;
99 
100 	int rc = cil_list_append_item(test_class_list, test_new_item);
101 
102 	cil_list_item_init(&test_new_item);
103 
104 	test_new_item->flavor = CIL_CLASS;
105 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head->next;
106 
107 	int rc2 = cil_list_append_item(test_class_list, test_new_item);
108 	CuAssertIntEquals(tc, SEPOL_OK, rc);
109 	CuAssertIntEquals(tc, SEPOL_OK, rc2);
110 }
111 
test_cil_list_append_item_append_extra(CuTest * tc)112 void test_cil_list_append_item_append_extra(CuTest *tc) {
113         char *line[] = {"(", "mlsconstrain", "(", "file", "dir", "process", ")", "(", "create", "relabelto", ")", "(", "eq", "12", "h2", ")", ")", NULL};
114 
115 	struct cil_tree *test_tree;
116         gen_test_tree(&test_tree, line);
117 
118 	struct cil_tree_node *test_ast_node;
119 	cil_tree_node_init(&test_ast_node);
120 
121 	struct cil_db *test_db;
122 	cil_db_init(&test_db);
123 
124 	test_ast_node->parent = test_db->ast->root;
125 	test_ast_node->line = 1;
126 
127 	struct cil_list *test_class_list;
128 	cil_list_init(&test_class_list);
129 
130 	struct cil_list_item *test_new_item;
131 	cil_list_item_init(&test_new_item);
132 
133 	test_new_item->flavor = CIL_CLASS;
134 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head;
135 
136 	int rc = cil_list_append_item(test_class_list, test_new_item);
137 
138 	cil_list_item_init(&test_new_item);
139 	test_new_item->flavor = CIL_CLASS;
140 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head->next;
141 
142 	int rc2 = cil_list_append_item(test_class_list, test_new_item);
143 
144 	cil_list_item_init(&test_new_item);
145 	test_new_item->flavor = CIL_CLASS;
146 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head->next->next;
147 
148 	int rc3 = cil_list_append_item(test_class_list, test_new_item);
149 	CuAssertIntEquals(tc, SEPOL_OK, rc);
150 	CuAssertIntEquals(tc, SEPOL_OK, rc2);
151 	CuAssertIntEquals(tc, SEPOL_OK, rc3);
152 }
153 
test_cil_list_append_item_listnull_neg(CuTest * tc)154 void test_cil_list_append_item_listnull_neg(CuTest *tc) {
155         char *line[] = {"(", "mlsconstrain", "(", "file", "dir", ")", "(", "create", "relabelto", ")", "(", "eq", "12", "h2", ")", ")", NULL};
156 
157 	struct cil_tree *test_tree;
158         gen_test_tree(&test_tree, line);
159 
160 	struct cil_tree_node *test_ast_node;
161 	cil_tree_node_init(&test_ast_node);
162 
163 	struct cil_db *test_db;
164 	cil_db_init(&test_db);
165 
166 	test_ast_node->parent = test_db->ast->root;
167 	test_ast_node->line = 1;
168 
169 	struct cil_list *test_class_list = NULL;
170 
171 	struct cil_list_item *test_new_item;
172 	cil_list_item_init(&test_new_item);
173 
174 	test_new_item->flavor = CIL_CLASS;
175 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head;
176 
177 	int rc = cil_list_append_item(test_class_list, test_new_item);
178 	CuAssertIntEquals(tc, SEPOL_ERR, rc);
179 }
180 
test_cil_list_append_item_itemnull_neg(CuTest * tc)181 void test_cil_list_append_item_itemnull_neg(CuTest *tc) {
182         char *line[] = {"(", "mlsconstrain", "(", "file", "dir", ")", "(", "create", "relabelto", ")", "(", "eq", "12", "h2", ")", ")", NULL};
183 
184 	struct cil_tree *test_tree;
185         gen_test_tree(&test_tree, line);
186 
187 	struct cil_tree_node *test_ast_node;
188 	cil_tree_node_init(&test_ast_node);
189 
190 	struct cil_db *test_db;
191 	cil_db_init(&test_db);
192 
193 	test_ast_node->parent = test_db->ast->root;
194 	test_ast_node->line = 1;
195 
196 	struct cil_list *test_class_list;
197 	cil_list_init(&test_class_list);
198 
199 	struct cil_list_item *test_new_item = NULL;
200 
201 	int rc = cil_list_append_item(test_class_list, test_new_item);
202 	CuAssertIntEquals(tc, SEPOL_ERR, rc);
203 }
204 
test_cil_list_prepend_item(CuTest * tc)205 void test_cil_list_prepend_item(CuTest *tc) {
206         char *line[] = {"(", "mlsconstrain", "(", "file", "dir", ")", "(", "create", "relabelto", ")", "(", "eq", "12", "h2", ")", ")", NULL};
207 
208 	struct cil_tree *test_tree;
209         gen_test_tree(&test_tree, line);
210 
211 	struct cil_tree_node *test_ast_node;
212 	cil_tree_node_init(&test_ast_node);
213 
214 	struct cil_db *test_db;
215 	cil_db_init(&test_db);
216 
217 	test_ast_node->parent = test_db->ast->root;
218 	test_ast_node->line = 1;
219 
220 	struct cil_list *test_class_list;
221 	cil_list_init(&test_class_list);
222 
223 	struct cil_list_item *test_new_item;
224 	cil_list_item_init(&test_new_item);
225 
226 	test_new_item->flavor = CIL_CLASS;
227 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head;
228 
229 	int rc = cil_list_prepend_item(test_class_list, test_new_item);
230 	CuAssertIntEquals(tc, SEPOL_OK, rc);
231 }
232 
test_cil_list_prepend_item_prepend(CuTest * tc)233 void test_cil_list_prepend_item_prepend(CuTest *tc) {
234         char *line[] = {"(", "mlsconstrain", "(", "file", "dir", ")", "(", "create", "relabelto", ")", "(", "eq", "12", "h2", ")", ")", NULL};
235 
236 	struct cil_tree *test_tree;
237         gen_test_tree(&test_tree, line);
238 
239 	struct cil_tree_node *test_ast_node;
240 	cil_tree_node_init(&test_ast_node);
241 
242 	struct cil_db *test_db;
243 	cil_db_init(&test_db);
244 
245 	test_ast_node->parent = test_db->ast->root;
246 	test_ast_node->line = 1;
247 
248 	struct cil_list *test_class_list;
249 	cil_list_init(&test_class_list);
250 
251 	struct cil_list_item *test_new_item;
252 	cil_list_item_init(&test_new_item);
253 
254 	test_new_item->flavor = CIL_CLASS;
255 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head;
256 
257 	int rc = cil_list_prepend_item(test_class_list, test_new_item);
258 
259 	CuAssertIntEquals(tc, SEPOL_OK, rc);
260 }
261 
test_cil_list_prepend_item_prepend_neg(CuTest * tc)262 void test_cil_list_prepend_item_prepend_neg(CuTest *tc) {
263         char *line[] = {"(", "mlsconstrain", "(", "file", "dir", "process", ")", "(", "create", "relabelto", ")", "(", "eq", "12", "h2", ")", ")", NULL};
264 
265 	struct cil_tree *test_tree;
266         gen_test_tree(&test_tree, line);
267 
268 	struct cil_tree_node *test_ast_node;
269 	cil_tree_node_init(&test_ast_node);
270 
271 	struct cil_db *test_db;
272 	cil_db_init(&test_db);
273 
274 	test_ast_node->parent = test_db->ast->root;
275 	test_ast_node->line = 1;
276 
277 	struct cil_list *test_class_list;
278 	cil_list_init(&test_class_list);
279 
280 	struct cil_list_item *test_new_item;
281 	cil_list_item_init(&test_new_item);
282 
283 	test_new_item->flavor = CIL_CLASS;
284 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head;
285 
286 	struct cil_list_item *test_new_item_next;
287 	cil_list_item_init(&test_new_item_next);
288 	test_new_item->flavor = CIL_CLASS;
289 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head->next;
290 	test_new_item->next = test_new_item_next;
291 
292 	int rc = cil_list_prepend_item(test_class_list, test_new_item);
293 	CuAssertIntEquals(tc, SEPOL_ERR, rc);
294 }
295 
test_cil_list_prepend_item_listnull_neg(CuTest * tc)296 void test_cil_list_prepend_item_listnull_neg(CuTest *tc) {
297         char *line[] = {"(", "mlsconstrain", "(", "file", "dir", ")", "(", "create", "relabelto", ")", "(", "eq", "12", "h2", ")", ")", NULL};
298 
299 	struct cil_tree *test_tree;
300         gen_test_tree(&test_tree, line);
301 
302 	struct cil_tree_node *test_ast_node;
303 	cil_tree_node_init(&test_ast_node);
304 
305 	struct cil_db *test_db;
306 	cil_db_init(&test_db);
307 
308 	test_ast_node->parent = test_db->ast->root;
309 	test_ast_node->line = 1;
310 
311 	struct cil_list *test_class_list = NULL;
312 
313 	struct cil_list_item *test_new_item;
314 	cil_list_item_init(&test_new_item);
315 
316 	test_new_item->flavor = CIL_CLASS;
317 	test_new_item->data = test_tree->root->cl_head->cl_head->next->cl_head;
318 
319 	int rc = cil_list_prepend_item(test_class_list, test_new_item);
320 	CuAssertIntEquals(tc, SEPOL_ERR, rc);
321 }
322 
test_cil_list_prepend_item_itemnull_neg(CuTest * tc)323 void test_cil_list_prepend_item_itemnull_neg(CuTest *tc) {
324         char *line[] = {"(", "mlsconstrain", "(", "file", "dir", ")", "(", "create", "relabelto", ")", "(", "eq", "12", "h2", ")", ")", NULL};
325 
326 	struct cil_tree *test_tree;
327         gen_test_tree(&test_tree, line);
328 
329 	struct cil_tree_node *test_ast_node;
330 	cil_tree_node_init(&test_ast_node);
331 
332 	struct cil_db *test_db;
333 	cil_db_init(&test_db);
334 
335 	test_ast_node->parent = test_db->ast->root;
336 	test_ast_node->line = 1;
337 
338 	struct cil_list *test_class_list;
339 	cil_list_init(&test_class_list);
340 
341 	struct cil_list_item *test_new_item = NULL;
342 
343 	int rc = cil_list_prepend_item(test_class_list, test_new_item);
344 	CuAssertIntEquals(tc, SEPOL_ERR, rc);
345 }
346