1*2d543d20SAndroid Build Coastguard Worker /*
2*2d543d20SAndroid Build Coastguard Worker * Author: Karl MacMillan <[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-cond.h"
22*2d543d20SAndroid Build Coastguard Worker #include "parse_util.h"
23*2d543d20SAndroid Build Coastguard Worker #include "helpers.h"
24*2d543d20SAndroid Build Coastguard Worker
25*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/policydb.h>
26*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/link.h>
27*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/expand.h>
28*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/conditional.h>
29*2d543d20SAndroid Build Coastguard Worker
30*2d543d20SAndroid Build Coastguard Worker static policydb_t basemod;
31*2d543d20SAndroid Build Coastguard Worker static policydb_t base_expanded;
32*2d543d20SAndroid Build Coastguard Worker
cond_test_init(void)33*2d543d20SAndroid Build Coastguard Worker int cond_test_init(void)
34*2d543d20SAndroid Build Coastguard Worker {
35*2d543d20SAndroid Build Coastguard Worker if (policydb_init(&base_expanded)) {
36*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "out of memory!\n");
37*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&basemod);
38*2d543d20SAndroid Build Coastguard Worker return -1;
39*2d543d20SAndroid Build Coastguard Worker }
40*2d543d20SAndroid Build Coastguard Worker
41*2d543d20SAndroid Build Coastguard Worker if (test_load_policy(&basemod, POLICY_BASE, 1, "test-cond", "refpolicy-base.conf"))
42*2d543d20SAndroid Build Coastguard Worker goto cleanup;
43*2d543d20SAndroid Build Coastguard Worker
44*2d543d20SAndroid Build Coastguard Worker if (link_modules(NULL, &basemod, NULL, 0, 0)) {
45*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "link modules failed\n");
46*2d543d20SAndroid Build Coastguard Worker goto cleanup;
47*2d543d20SAndroid Build Coastguard Worker }
48*2d543d20SAndroid Build Coastguard Worker
49*2d543d20SAndroid Build Coastguard Worker if (expand_module(NULL, &basemod, &base_expanded, 0, 1)) {
50*2d543d20SAndroid Build Coastguard Worker fprintf(stderr, "expand module failed\n");
51*2d543d20SAndroid Build Coastguard Worker goto cleanup;
52*2d543d20SAndroid Build Coastguard Worker }
53*2d543d20SAndroid Build Coastguard Worker
54*2d543d20SAndroid Build Coastguard Worker return 0;
55*2d543d20SAndroid Build Coastguard Worker
56*2d543d20SAndroid Build Coastguard Worker cleanup:
57*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&basemod);
58*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&base_expanded);
59*2d543d20SAndroid Build Coastguard Worker return -1;
60*2d543d20SAndroid Build Coastguard Worker }
61*2d543d20SAndroid Build Coastguard Worker
cond_test_cleanup(void)62*2d543d20SAndroid Build Coastguard Worker int cond_test_cleanup(void)
63*2d543d20SAndroid Build Coastguard Worker {
64*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&basemod);
65*2d543d20SAndroid Build Coastguard Worker policydb_destroy(&base_expanded);
66*2d543d20SAndroid Build Coastguard Worker
67*2d543d20SAndroid Build Coastguard Worker return 0;
68*2d543d20SAndroid Build Coastguard Worker }
69*2d543d20SAndroid Build Coastguard Worker
test_cond_expr_equal(void)70*2d543d20SAndroid Build Coastguard Worker static void test_cond_expr_equal(void)
71*2d543d20SAndroid Build Coastguard Worker {
72*2d543d20SAndroid Build Coastguard Worker cond_node_t *a, *b;
73*2d543d20SAndroid Build Coastguard Worker
74*2d543d20SAndroid Build Coastguard Worker a = base_expanded.cond_list;
75*2d543d20SAndroid Build Coastguard Worker while (a) {
76*2d543d20SAndroid Build Coastguard Worker b = base_expanded.cond_list;
77*2d543d20SAndroid Build Coastguard Worker while (b) {
78*2d543d20SAndroid Build Coastguard Worker if (a == b) {
79*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(cond_expr_equal(a, b));
80*2d543d20SAndroid Build Coastguard Worker } else {
81*2d543d20SAndroid Build Coastguard Worker CU_ASSERT(cond_expr_equal(a, b) == 0);
82*2d543d20SAndroid Build Coastguard Worker }
83*2d543d20SAndroid Build Coastguard Worker b = b->next;
84*2d543d20SAndroid Build Coastguard Worker }
85*2d543d20SAndroid Build Coastguard Worker a = a->next;
86*2d543d20SAndroid Build Coastguard Worker }
87*2d543d20SAndroid Build Coastguard Worker }
88*2d543d20SAndroid Build Coastguard Worker
cond_add_tests(CU_pSuite suite)89*2d543d20SAndroid Build Coastguard Worker int cond_add_tests(CU_pSuite suite)
90*2d543d20SAndroid Build Coastguard Worker {
91*2d543d20SAndroid Build Coastguard Worker if (NULL == CU_add_test(suite, "cond_expr_equal", test_cond_expr_equal)) {
92*2d543d20SAndroid Build Coastguard Worker return CU_get_error();
93*2d543d20SAndroid Build Coastguard Worker }
94*2d543d20SAndroid Build Coastguard Worker return 0;
95*2d543d20SAndroid Build Coastguard Worker }
96