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 <sepol/policydb/policydb.h>
31
32#include "CuTest.h"
33#include "test_cil.h"
34
35#include "../../src/cil_internal.h"
36#include "../../src/cil_tree.h"
37
38void test_cil_symtab_array_init(CuTest *tc) {
39	struct cil_db *test_new_db;
40	test_new_db = malloc(sizeof(*test_new_db));
41
42	cil_symtab_array_init(test_new_db->symtab, cil_sym_sizes[CIL_SYM_ARRAY_ROOT]);
43	CuAssertPtrNotNull(tc, test_new_db->symtab);
44
45	free(test_new_db);
46}
47
48void test_cil_db_init(CuTest *tc) {
49	struct cil_db *test_db;
50
51	cil_db_init(&test_db);
52
53	CuAssertPtrNotNull(tc, test_db->ast);
54	CuAssertPtrNotNull(tc, test_db->symtab);
55	CuAssertPtrNotNull(tc, test_db->symtab);
56}
57
58// TODO: Reach SEPOL_ERR return in cil_db_init ( currently can't produce a method to do so )
59
60void test_cil_get_symtab_block(CuTest *tc) {
61	symtab_t *symtab = NULL;
62
63	struct cil_tree_node *test_ast_node;
64	cil_tree_node_init(&test_ast_node);
65
66	struct cil_db *test_db;
67	cil_db_init(&test_db);
68
69	test_ast_node->parent = test_db->ast->root;
70	test_ast_node->parent->flavor = CIL_BLOCK;
71	test_ast_node->line = 1;
72
73	int rc = cil_get_symtab(test_db, test_ast_node->parent, &symtab, CIL_SYM_BLOCKS);
74	CuAssertIntEquals(tc, SEPOL_OK, rc);
75	CuAssertPtrNotNull(tc, symtab);
76}
77
78void test_cil_get_symtab_class(CuTest *tc) {
79	symtab_t *symtab = NULL;
80
81	struct cil_tree_node *test_ast_node;
82	cil_tree_node_init(&test_ast_node);
83
84	struct cil_db *test_db;
85	cil_db_init(&test_db);
86
87	test_ast_node->parent = test_db->ast->root;
88	test_ast_node->parent->flavor = CIL_CLASS;
89	test_ast_node->line = 1;
90
91	int rc = cil_get_symtab(test_db, test_ast_node->parent, &symtab, CIL_SYM_BLOCKS);
92	CuAssertIntEquals(tc, SEPOL_OK, rc);
93	CuAssertPtrNotNull(tc, symtab);
94}
95
96void test_cil_get_symtab_root(CuTest *tc) {
97	symtab_t *symtab = NULL;
98
99	struct cil_tree_node *test_ast_node;
100	cil_tree_node_init(&test_ast_node);
101
102	struct cil_db *test_db;
103	cil_db_init(&test_db);
104
105	test_ast_node->parent = test_db->ast->root;
106	test_ast_node->parent->flavor = CIL_ROOT;
107	test_ast_node->line = 1;
108
109	int rc = cil_get_symtab(test_db, test_ast_node->parent, &symtab, CIL_SYM_BLOCKS);
110	CuAssertIntEquals(tc, SEPOL_OK, rc);
111	CuAssertPtrNotNull(tc, symtab);
112}
113
114void test_cil_get_symtab_flavor_neg(CuTest *tc) {
115	symtab_t *symtab = NULL;
116
117	struct cil_tree_node *test_ast_node;
118	cil_tree_node_init(&test_ast_node);
119
120	struct cil_db *test_db;
121	cil_db_init(&test_db);
122
123	test_ast_node->parent = test_db->ast->root;
124	test_ast_node->parent->flavor = 1234567;
125	test_ast_node->line = 1;
126
127	int rc = cil_get_symtab(test_db, test_ast_node->parent, &symtab, CIL_SYM_BLOCKS);
128	CuAssertIntEquals(tc, SEPOL_ERR, rc);
129	CuAssertPtrEquals(tc, symtab, NULL);
130}
131
132void test_cil_get_symtab_null_neg(CuTest *tc) {
133	symtab_t *symtab = NULL;
134
135	struct cil_tree_node *test_ast_node;
136	cil_tree_node_init(&test_ast_node);
137
138	struct cil_db *test_db;
139	cil_db_init(&test_db);
140
141	test_ast_node->parent = NULL;
142	test_ast_node->line = 1;
143
144	int rc = cil_get_symtab(test_db, test_ast_node->parent, &symtab, CIL_SYM_BLOCKS);
145	CuAssertIntEquals(tc, SEPOL_ERR, rc);
146	CuAssertPtrEquals(tc, symtab, NULL);
147}
148
149void test_cil_get_symtab_node_null_neg(CuTest *tc) {
150	symtab_t *symtab = NULL;
151
152	struct cil_tree_node *test_ast_node = NULL;
153
154	struct cil_db *test_db;
155	cil_db_init(&test_db);
156
157	int rc = cil_get_symtab(test_db, test_ast_node, &symtab, CIL_SYM_BLOCKS);
158	CuAssertIntEquals(tc, SEPOL_ERR, rc);
159	CuAssertPtrEquals(tc, symtab, NULL);
160	CuAssertPtrEquals(tc, test_ast_node, NULL);
161}
162
163void test_cil_get_symtab_parent_null_neg(CuTest *tc) {
164	symtab_t *symtab = NULL;
165
166	struct cil_tree_node *test_ast_node;
167	cil_tree_node_init(&test_ast_node);
168
169	struct cil_db *test_db;
170	cil_db_init(&test_db);
171
172	test_ast_node->parent = NULL;
173	test_ast_node->line = 1;
174
175	int rc = cil_get_symtab(test_db, test_ast_node->parent, &symtab, CIL_SYM_BLOCKS);
176	CuAssertIntEquals(tc, SEPOL_ERR, rc);
177	CuAssertPtrEquals(tc, symtab, NULL);
178}
179
180