16cd6a6acSopenharmony_ci/* 26cd6a6acSopenharmony_ci * Author: Joshua Brindle <jbrindle@tresys.com> 36cd6a6acSopenharmony_ci * 46cd6a6acSopenharmony_ci * Copyright (C) 2006 Tresys Technology, LLC 56cd6a6acSopenharmony_ci * 66cd6a6acSopenharmony_ci * This library is free software; you can redistribute it and/or 76cd6a6acSopenharmony_ci * modify it under the terms of the GNU Lesser General Public 86cd6a6acSopenharmony_ci * License as published by the Free Software Foundation; either 96cd6a6acSopenharmony_ci * version 2.1 of the License, or (at your option) any later version. 106cd6a6acSopenharmony_ci * 116cd6a6acSopenharmony_ci * This library is distributed in the hope that it will be useful, 126cd6a6acSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 136cd6a6acSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 146cd6a6acSopenharmony_ci * Lesser General Public License for more details. 156cd6a6acSopenharmony_ci * 166cd6a6acSopenharmony_ci * You should have received a copy of the GNU Lesser General Public 176cd6a6acSopenharmony_ci * License along with this library; if not, write to the Free Software 186cd6a6acSopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 196cd6a6acSopenharmony_ci */ 206cd6a6acSopenharmony_ci 216cd6a6acSopenharmony_ci#include "test-linker-cond-map.h" 226cd6a6acSopenharmony_ci#include "parse_util.h" 236cd6a6acSopenharmony_ci#include "helpers.h" 246cd6a6acSopenharmony_ci#include "test-common.h" 256cd6a6acSopenharmony_ci 266cd6a6acSopenharmony_ci#include <sepol/policydb/policydb.h> 276cd6a6acSopenharmony_ci#include <sepol/policydb/link.h> 286cd6a6acSopenharmony_ci#include <sepol/policydb/conditional.h> 296cd6a6acSopenharmony_ci 306cd6a6acSopenharmony_ci#include <CUnit/Basic.h> 316cd6a6acSopenharmony_ci#include <stdlib.h> 326cd6a6acSopenharmony_ci 336cd6a6acSopenharmony_ci/* Tests for conditionals 346cd6a6acSopenharmony_ci * Test each cond/bool for these 356cd6a6acSopenharmony_ci * - boolean copied correctly (state is correct) 366cd6a6acSopenharmony_ci * - conditional expression is correct 376cd6a6acSopenharmony_ci * Tests: 386cd6a6acSopenharmony_ci * - single boolean in base 396cd6a6acSopenharmony_ci * - single boolean in module 406cd6a6acSopenharmony_ci * - single boolean in base optional 416cd6a6acSopenharmony_ci * - single boolean in module optional 426cd6a6acSopenharmony_ci * - 2 booleans in base 436cd6a6acSopenharmony_ci * - 2 booleans in module 446cd6a6acSopenharmony_ci * - 2 booleans in base optional 456cd6a6acSopenharmony_ci * - 2 booleans in module optional 466cd6a6acSopenharmony_ci * - 2 booleans, base and module 476cd6a6acSopenharmony_ci * - 2 booleans, base optional and module 486cd6a6acSopenharmony_ci * - 2 booleans, base optional and module optional 496cd6a6acSopenharmony_ci * - 3 booleans, base, base optional, module 506cd6a6acSopenharmony_ci * - 4 boolean, base, base optional, module, module optional 516cd6a6acSopenharmony_ci */ 526cd6a6acSopenharmony_ci 536cd6a6acSopenharmony_citypedef struct test_cond_expr { 546cd6a6acSopenharmony_ci const char *bool; 556cd6a6acSopenharmony_ci uint32_t expr_type; 566cd6a6acSopenharmony_ci} test_cond_expr_t; 576cd6a6acSopenharmony_ci 586cd6a6acSopenharmony_cistatic void test_cond_expr_mapping(policydb_t * p, avrule_decl_t * d, test_cond_expr_t * bools, int len) 596cd6a6acSopenharmony_ci{ 606cd6a6acSopenharmony_ci int i; 616cd6a6acSopenharmony_ci cond_expr_t *expr; 626cd6a6acSopenharmony_ci 636cd6a6acSopenharmony_ci CU_ASSERT_FATAL(d->cond_list != NULL); 646cd6a6acSopenharmony_ci CU_ASSERT_FATAL(d->cond_list->expr != NULL); 656cd6a6acSopenharmony_ci 666cd6a6acSopenharmony_ci expr = d->cond_list->expr; 676cd6a6acSopenharmony_ci 686cd6a6acSopenharmony_ci for (i = 0; i < len; i++) { 696cd6a6acSopenharmony_ci CU_ASSERT_FATAL(expr != NULL); 706cd6a6acSopenharmony_ci 716cd6a6acSopenharmony_ci CU_ASSERT(expr->expr_type == bools[i].expr_type); 726cd6a6acSopenharmony_ci if (bools[i].bool) { 736cd6a6acSopenharmony_ci CU_ASSERT(strcmp(p->sym_val_to_name[SYM_BOOLS][expr->bool - 1], bools[i].bool) == 0); 746cd6a6acSopenharmony_ci } 756cd6a6acSopenharmony_ci expr = expr->next; 766cd6a6acSopenharmony_ci } 776cd6a6acSopenharmony_ci} 786cd6a6acSopenharmony_ci 796cd6a6acSopenharmony_cistatic void test_bool_state(policydb_t * p, const char *bool, int state) 806cd6a6acSopenharmony_ci{ 816cd6a6acSopenharmony_ci cond_bool_datum_t *b; 826cd6a6acSopenharmony_ci 836cd6a6acSopenharmony_ci b = hashtab_search(p->p_bools.table, bool); 846cd6a6acSopenharmony_ci CU_ASSERT_FATAL(b != NULL); 856cd6a6acSopenharmony_ci CU_ASSERT(b->state == state); 866cd6a6acSopenharmony_ci} 876cd6a6acSopenharmony_ci 886cd6a6acSopenharmony_civoid base_cond_tests(policydb_t * base) 896cd6a6acSopenharmony_ci{ 906cd6a6acSopenharmony_ci avrule_decl_t *d; 916cd6a6acSopenharmony_ci unsigned int decls[1]; 926cd6a6acSopenharmony_ci test_cond_expr_t bools[2]; 936cd6a6acSopenharmony_ci 946cd6a6acSopenharmony_ci /* these tests look at booleans and conditionals in the base only 956cd6a6acSopenharmony_ci * to ensure that they aren't altered or removed during the link process */ 966cd6a6acSopenharmony_ci 976cd6a6acSopenharmony_ci /* bool existence and state, global scope */ 986cd6a6acSopenharmony_ci d = test_find_decl_by_sym(base, SYM_TYPES, "tag_g_b"); 996cd6a6acSopenharmony_ci decls[0] = d->decl_id; 1006cd6a6acSopenharmony_ci test_sym_presence(base, "g_b_bool_1", SYM_BOOLS, SCOPE_DECL, decls, 1); 1016cd6a6acSopenharmony_ci test_bool_state(base, "g_b_bool_1", 0); 1026cd6a6acSopenharmony_ci /* conditional expression mapped correctly */ 1036cd6a6acSopenharmony_ci bools[0].bool = "g_b_bool_1"; 1046cd6a6acSopenharmony_ci bools[0].expr_type = COND_BOOL; 1056cd6a6acSopenharmony_ci test_cond_expr_mapping(base, d, bools, 1); 1066cd6a6acSopenharmony_ci 1076cd6a6acSopenharmony_ci /* bool existence and state, optional scope */ 1086cd6a6acSopenharmony_ci d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_b"); 1096cd6a6acSopenharmony_ci decls[0] = d->decl_id; 1106cd6a6acSopenharmony_ci test_sym_presence(base, "o1_b_bool_1", SYM_BOOLS, SCOPE_DECL, decls, 1); 1116cd6a6acSopenharmony_ci test_bool_state(base, "o1_b_bool_1", 1); 1126cd6a6acSopenharmony_ci /* conditional expression mapped correctly */ 1136cd6a6acSopenharmony_ci bools[0].bool = "o1_b_bool_1"; 1146cd6a6acSopenharmony_ci bools[0].expr_type = COND_BOOL; 1156cd6a6acSopenharmony_ci test_cond_expr_mapping(base, d, bools, 1); 1166cd6a6acSopenharmony_ci 1176cd6a6acSopenharmony_ci} 1186cd6a6acSopenharmony_ci 1196cd6a6acSopenharmony_civoid module_cond_tests(policydb_t * base) 1206cd6a6acSopenharmony_ci{ 1216cd6a6acSopenharmony_ci avrule_decl_t *d; 1226cd6a6acSopenharmony_ci unsigned int decls[1]; 1236cd6a6acSopenharmony_ci test_cond_expr_t bools[3]; 1246cd6a6acSopenharmony_ci 1256cd6a6acSopenharmony_ci /* bool existence and state, module 1 global scope */ 1266cd6a6acSopenharmony_ci d = test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m1"); 1276cd6a6acSopenharmony_ci decls[0] = d->decl_id; 1286cd6a6acSopenharmony_ci test_sym_presence(base, "g_m1_bool_1", SYM_BOOLS, SCOPE_DECL, decls, 1); 1296cd6a6acSopenharmony_ci test_bool_state(base, "g_m1_bool_1", 1); 1306cd6a6acSopenharmony_ci /* conditional expression mapped correctly */ 1316cd6a6acSopenharmony_ci bools[0].bool = "g_m1_bool_1"; 1326cd6a6acSopenharmony_ci bools[0].expr_type = COND_BOOL; 1336cd6a6acSopenharmony_ci test_cond_expr_mapping(base, d, bools, 1); 1346cd6a6acSopenharmony_ci 1356cd6a6acSopenharmony_ci /* bool existence and state, module 1 optional scope */ 1366cd6a6acSopenharmony_ci d = test_find_decl_by_sym(base, SYM_TYPES, "tag_o1_m1"); 1376cd6a6acSopenharmony_ci decls[0] = d->decl_id; 1386cd6a6acSopenharmony_ci test_sym_presence(base, "o1_m1_bool_1", SYM_BOOLS, SCOPE_DECL, decls, 1); 1396cd6a6acSopenharmony_ci test_bool_state(base, "o1_m1_bool_1", 0); 1406cd6a6acSopenharmony_ci /* conditional expression mapped correctly */ 1416cd6a6acSopenharmony_ci bools[0].bool = "o1_m1_bool_1"; 1426cd6a6acSopenharmony_ci bools[0].expr_type = COND_BOOL; 1436cd6a6acSopenharmony_ci test_cond_expr_mapping(base, d, bools, 1); 1446cd6a6acSopenharmony_ci 1456cd6a6acSopenharmony_ci /* bool existence and state, module 2 global scope */ 1466cd6a6acSopenharmony_ci d = test_find_decl_by_sym(base, SYM_TYPES, "tag_g_m2"); 1476cd6a6acSopenharmony_ci decls[0] = d->decl_id; 1486cd6a6acSopenharmony_ci test_sym_presence(base, "g_m2_bool_1", SYM_BOOLS, SCOPE_DECL, decls, 1); 1496cd6a6acSopenharmony_ci test_sym_presence(base, "g_m2_bool_2", SYM_BOOLS, SCOPE_DECL, decls, 1); 1506cd6a6acSopenharmony_ci test_bool_state(base, "g_m2_bool_1", 1); 1516cd6a6acSopenharmony_ci test_bool_state(base, "g_m2_bool_2", 0); 1526cd6a6acSopenharmony_ci /* conditional expression mapped correctly */ 1536cd6a6acSopenharmony_ci bools[0].bool = "g_m2_bool_1"; 1546cd6a6acSopenharmony_ci bools[0].expr_type = COND_BOOL; 1556cd6a6acSopenharmony_ci bools[1].bool = "g_m2_bool_2"; 1566cd6a6acSopenharmony_ci bools[1].expr_type = COND_BOOL; 1576cd6a6acSopenharmony_ci bools[2].bool = NULL; 1586cd6a6acSopenharmony_ci bools[2].expr_type = COND_AND; 1596cd6a6acSopenharmony_ci test_cond_expr_mapping(base, d, bools, 3); 1606cd6a6acSopenharmony_ci} 161