1 /*
2  * Author: Joshua Brindle <jbrindle@tresys.com>
3  *         Chad Sellers <csellers@tresys.com>
4  *
5  * Copyright (C) 2006 Tresys Technology, LLC
6  *
7  *  This library is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU Lesser General Public
9  *  License as published by the Free Software Foundation; either
10  *  version 2.1 of the License, or (at your option) any later version.
11  *
12  *  This library is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this library; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 /* This has helper functions that are common between tests */
23 
24 #include "helpers.h"
25 #include "parse_util.h"
26 
27 #include <sepol/policydb/expand.h>
28 #include <sepol/policydb/avrule_block.h>
29 
30 #include <CUnit/Basic.h>
31 
32 #include <stdlib.h>
33 #include <limits.h>
34 
test_load_policy(policydb_t * p,int policy_type,int mls,const char * test_name,const char * policy_name)35 int test_load_policy(policydb_t * p, int policy_type, int mls, const char *test_name, const char *policy_name)
36 {
37 	char filename[PATH_MAX];
38 
39 	if (mls) {
40 		if (snprintf(filename, PATH_MAX, "policies/%s/%s.mls", test_name, policy_name) < 0) {
41 			return -1;
42 		}
43 	} else {
44 		if (snprintf(filename, PATH_MAX, "policies/%s/%s.std", test_name, policy_name) < 0) {
45 			return -1;
46 		}
47 	}
48 
49 	if (policydb_init(p)) {
50 		fprintf(stderr, "Out of memory");
51 		return -1;
52 	}
53 
54 	p->policy_type = policy_type;
55 	p->mls = mls;
56 
57 	if (read_source_policy(p, filename, test_name)) {
58 		fprintf(stderr, "failed to read policy %s\n", filename);
59 		policydb_destroy(p);
60 		return -1;
61 	}
62 
63 	return 0;
64 }
65 
test_find_decl_by_sym(policydb_t * p,int symtab,const char * sym)66 avrule_decl_t *test_find_decl_by_sym(policydb_t * p, int symtab, const char *sym)
67 {
68 	scope_datum_t *scope = (scope_datum_t *) hashtab_search(p->scope[symtab].table, sym);
69 
70 	if (scope == NULL) {
71 		return NULL;
72 	}
73 	if (scope->scope != SCOPE_DECL) {
74 		return NULL;
75 	}
76 	if (scope->decl_ids_len != 1) {
77 		return NULL;
78 	}
79 
80 	return p->decl_val_to_struct[scope->decl_ids[0] - 1];
81 }
82