| 1 |
/* |
| 2 |
* Author: Joshua Brindle <[email protected]> |
| 3 |
* Chad Sellers <[email protected]> |
| 4 |
* Chris PeBenito <[email protected]> |
| 5 |
* |
| 6 |
* Copyright (C) 2006 Tresys Technology, LLC |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Lesser General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2.1 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Lesser General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Lesser General Public |
| 19 |
* License along with this library; if not, write to the Free Software |
| 20 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 |
*/ |
| 22 |
|
| 23 |
/* This has tests that are common between test suites*/ |
| 24 |
|
| 25 |
#include <sepol/policydb/avrule_block.h> |
| 26 |
|
| 27 |
#include <CUnit/Basic.h> |
| 28 |
|
| 29 |
#include "test-common.h" |
| 30 |
#include "helpers.h" |
| 31 |
|
| 32 |
void test_sym_presence(policydb_t * p, const char *id, int sym_type, unsigned int scope_type, unsigned int *decls, unsigned int len) |
| 33 |
{ |
| 34 |
scope_datum_t *scope; |
| 35 |
int found; |
| 36 |
unsigned int i, j; |
| 37 |
/* make sure it is in global symtab */ |
| 38 |
if (!hashtab_search(p->symtab[sym_type].table, id)) { |
| 39 |
fprintf(stderr, "symbol %s not found in table %d\n", id, sym_type); |
| 40 |
CU_FAIL_FATAL(); |
| 41 |
} |
| 42 |
/* make sure its scope is correct */ |
| 43 |
scope = hashtab_search(p->scope[sym_type].table, id); |
| 44 |
CU_ASSERT_FATAL(scope != NULL); |
| 45 |
CU_ASSERT(scope->scope == scope_type); |
| 46 |
CU_ASSERT(scope->decl_ids_len == len); |
| 47 |
if (scope->decl_ids_len != len) |
| 48 |
fprintf(stderr, "sym %s has %d decls, %d expected\n", id, scope->decl_ids_len, len); |
| 49 |
for (i = 0; i < len; i++) { |
| 50 |
found = 0; |
| 51 |
for (j = 0; j < len; j++) { |
| 52 |
if (decls[i] == scope->decl_ids[j]) |
| 53 |
found++; |
| 54 |
} |
| 55 |
CU_ASSERT(found == 1); |
| 56 |
} |
| 57 |
|
| 58 |
} |
| 59 |
|
| 60 |
static int common_test_index(hashtab_key_t key, hashtab_datum_t datum, void *data) |
| 61 |
{ |
| 62 |
common_datum_t *d = (common_datum_t *) datum; |
| 63 |
policydb_t *p = (policydb_t *) data; |
| 64 |
|
| 65 |
CU_ASSERT(p->sym_val_to_name[SYM_COMMONS][d->s.value - 1] == (char *)key); |
| 66 |
return 0; |
| 67 |
} |
| 68 |
|
| 69 |
static int class_test_index(hashtab_key_t key, hashtab_datum_t datum, void *data) |
| 70 |
{ |
| 71 |
class_datum_t *d = (class_datum_t *) datum; |
| 72 |
policydb_t *p = (policydb_t *) data; |
| 73 |
|
| 74 |
CU_ASSERT(p->sym_val_to_name[SYM_CLASSES][d->s.value - 1] == (char *)key); |
| 75 |
CU_ASSERT(p->class_val_to_struct[d->s.value - 1] == d); |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
static int role_test_index(hashtab_key_t key, hashtab_datum_t datum, void *data) |
| 80 |
{ |
| 81 |
role_datum_t *d = (role_datum_t *) datum; |
| 82 |
policydb_t *p = (policydb_t *) data; |
| 83 |
|
| 84 |
CU_ASSERT(p->sym_val_to_name[SYM_ROLES][d->s.value - 1] == (char *)key); |
| 85 |
CU_ASSERT(p->role_val_to_struct[d->s.value - 1] == d); |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static int type_test_index(hashtab_key_t key, hashtab_datum_t datum, void *data) |
| 90 |
{ |
| 91 |
type_datum_t *d = (type_datum_t *) datum; |
| 92 |
policydb_t *p = (policydb_t *) data; |
| 93 |
|
| 94 |
if (!d->primary) |
| 95 |
return 0; |
| 96 |
|
| 97 |
CU_ASSERT(p->sym_val_to_name[SYM_TYPES][d->s.value - 1] == (char *)key); |
| 98 |
CU_ASSERT(p->type_val_to_struct[d->s.value - 1] == d); |
| 99 |
|
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
static int user_test_index(hashtab_key_t key, hashtab_datum_t datum, void *data) |
| 104 |
{ |
| 105 |
user_datum_t *d = (user_datum_t *) datum; |
| 106 |
policydb_t *p = (policydb_t *) data; |
| 107 |
|
| 108 |
CU_ASSERT(p->sym_val_to_name[SYM_USERS][d->s.value - 1] == (char *)key); |
| 109 |
CU_ASSERT(p->user_val_to_struct[d->s.value - 1] == d); |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
static int cond_test_index(hashtab_key_t key, hashtab_datum_t datum, void *data) |
| 114 |
{ |
| 115 |
cond_bool_datum_t *d = (cond_bool_datum_t *) datum; |
| 116 |
policydb_t *p = (policydb_t *) data; |
| 117 |
|
| 118 |
CU_ASSERT(p->sym_val_to_name[SYM_BOOLS][d->s.value - 1] == (char *)key); |
| 119 |
CU_ASSERT(p->bool_val_to_struct[d->s.value - 1] == d); |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
static int level_test_index(hashtab_key_t key, hashtab_datum_t datum, void *data) |
| 124 |
{ |
| 125 |
level_datum_t *d = (level_datum_t *) datum; |
| 126 |
policydb_t *p = (policydb_t *) data; |
| 127 |
|
| 128 |
CU_ASSERT(p->sym_val_to_name[SYM_LEVELS][d->level->sens - 1] == (char *)key); |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
static int cat_test_index(hashtab_key_t key, hashtab_datum_t datum, void *data) |
| 133 |
{ |
| 134 |
cat_datum_t *d = (cat_datum_t *) datum; |
| 135 |
policydb_t *p = (policydb_t *) data; |
| 136 |
|
| 137 |
CU_ASSERT(p->sym_val_to_name[SYM_CATS][d->s.value - 1] == (char *)key); |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
static int (*test_index_f[SYM_NUM]) (hashtab_key_t key, hashtab_datum_t datum, void *p) = { |
| 142 |
common_test_index, class_test_index, role_test_index, type_test_index, user_test_index, cond_test_index, level_test_index, cat_test_index,}; |
| 143 |
|
| 144 |
void test_policydb_indexes(policydb_t * p) |
| 145 |
{ |
| 146 |
int i; |
| 147 |
|
| 148 |
for (i = 0; i < SYM_NUM; i++) { |
| 149 |
hashtab_map(p->symtab[i].table, test_index_f[i], p); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
void test_alias_datum(policydb_t * p, const char *id, const char *primary_id, char mode, unsigned int flavor) |
| 154 |
{ |
| 155 |
type_datum_t *type, *primary; |
| 156 |
unsigned int my_primary, my_flavor, my_value; |
| 157 |
|
| 158 |
type = hashtab_search(p->p_types.table, id); |
| 159 |
primary = hashtab_search(p->p_types.table, primary_id); |
| 160 |
|
| 161 |
CU_ASSERT_PTR_NOT_NULL(type); |
| 162 |
CU_ASSERT_PTR_NOT_NULL(primary); |
| 163 |
|
| 164 |
if (type && primary) { |
| 165 |
if (mode) { |
| 166 |
my_flavor = type->flavor; |
| 167 |
} else { |
| 168 |
my_flavor = flavor; |
| 169 |
} |
| 170 |
|
| 171 |
if (my_flavor == TYPE_TYPE) { |
| 172 |
my_primary = 0; |
| 173 |
my_value = primary->s.value; |
| 174 |
} else { |
| 175 |
CU_ASSERT(my_flavor == TYPE_ALIAS); |
| 176 |
my_primary = primary->s.value; |
| 177 |
CU_ASSERT_NOT_EQUAL(type->s.value, primary->s.value); |
| 178 |
my_value = type->s.value; |
| 179 |
} |
| 180 |
|
| 181 |
CU_ASSERT(type->primary == my_primary); |
| 182 |
CU_ASSERT(type->flavor == my_flavor); |
| 183 |
CU_ASSERT(type->s.value == my_value); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
role_datum_t *test_role_type_set(policydb_t * p, const char *id, avrule_decl_t * decl, const char **types, unsigned int len, unsigned int flags) |
| 188 |
{ |
| 189 |
ebitmap_node_t *tnode; |
| 190 |
unsigned int i, j, new, found = 0; |
| 191 |
role_datum_t *role; |
| 192 |
|
| 193 |
if (decl) |
| 194 |
role = hashtab_search(decl->p_roles.table, id); |
| 195 |
else |
| 196 |
role = hashtab_search(p->p_roles.table, id); |
| 197 |
|
| 198 |
if (!role) |
| 199 |
printf("role %s can't be found! \n", id); |
| 200 |
|
| 201 |
CU_ASSERT_FATAL(role != NULL); |
| 202 |
|
| 203 |
ebitmap_for_each_positive_bit(&role->types.types, tnode, i) { |
| 204 |
new = 0; |
| 205 |
for (j = 0; j < len; j++) { |
| 206 |
if (strcmp(p->sym_val_to_name[SYM_TYPES][i], types[j]) == 0) { |
| 207 |
found++; |
| 208 |
new = 1; |
| 209 |
} |
| 210 |
} |
| 211 |
if (new == 0) { |
| 212 |
printf("\nRole %s had type %s not in types array\n", |
| 213 |
id, p->sym_val_to_name[SYM_TYPES][i]); |
| 214 |
} |
| 215 |
CU_ASSERT(new == 1); |
| 216 |
} |
| 217 |
CU_ASSERT(found == len); |
| 218 |
if (found != len) |
| 219 |
printf("\nrole %s has %d types, %d expected\n", p->sym_val_to_name[SYM_ROLES][role->s.value - 1], found, len); |
| 220 |
/* roles should never have anything in the negset */ |
| 221 |
CU_ASSERT(role->types.negset.highbit == 0); |
| 222 |
CU_ASSERT(role->types.flags == flags); |
| 223 |
|
| 224 |
return role; |
| 225 |
} |
| 226 |
|
| 227 |
void test_attr_types(policydb_t * p, const char *id, avrule_decl_t * decl, const char **types, int len) |
| 228 |
{ |
| 229 |
ebitmap_node_t *tnode; |
| 230 |
int j, new, found = 0; |
| 231 |
unsigned int i; |
| 232 |
type_datum_t *attr; |
| 233 |
|
| 234 |
if (decl) { |
| 235 |
attr = hashtab_search(decl->p_types.table, id); |
| 236 |
if (attr == NULL) |
| 237 |
printf("could not find attr %s in decl %d\n", id, decl->decl_id); |
| 238 |
} else { |
| 239 |
attr = hashtab_search(p->p_types.table, id); |
| 240 |
if (attr == NULL) |
| 241 |
printf("could not find attr %s in policy\n", id); |
| 242 |
} |
| 243 |
|
| 244 |
CU_ASSERT_FATAL(attr != NULL); |
| 245 |
CU_ASSERT(attr->flavor == TYPE_ATTRIB); |
| 246 |
CU_ASSERT(attr->primary == 1); |
| 247 |
|
| 248 |
ebitmap_for_each_positive_bit(&attr->types, tnode, i) { |
| 249 |
new = 0; |
| 250 |
for (j = 0; j < len; j++) { |
| 251 |
if (strcmp(p->sym_val_to_name[SYM_TYPES][i], types[j]) == 0) { |
| 252 |
found++; |
| 253 |
new = 1; |
| 254 |
} |
| 255 |
} |
| 256 |
if (new == 0) { |
| 257 |
printf("\nattr %s had type %s not in types array\n", |
| 258 |
id, p->sym_val_to_name[SYM_TYPES][i]); |
| 259 |
} |
| 260 |
CU_ASSERT(new == 1); |
| 261 |
} |
| 262 |
CU_ASSERT(found == len); |
| 263 |
if (found != len) |
| 264 |
printf("\nattr %s has %d types, %d expected\n", id, found, len); |
| 265 |
} |