1/*
2 * Authors: Chad Sellers <csellers@tresys.com>
3 *          Joshua Brindle <jbrindle@tresys.com>
4 *          Chris PeBenito <cpebenito@tresys.com>
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#include "test-expander-users.h"
24
25#include <sepol/policydb/policydb.h>
26#include <CUnit/Basic.h>
27#include <stdlib.h>
28
29extern policydb_t user_expanded;
30
31static void check_user_roles(policydb_t * p, char *user_name, char **role_names, int num_roles)
32{
33	user_datum_t *user;
34	ebitmap_node_t *tnode;
35	unsigned int i;
36	int j;
37	unsigned char *found;	/* array of booleans of roles found */
38	int extra = 0;		/* number of extra roles found */
39
40	user = (user_datum_t *) hashtab_search(p->p_users.table, user_name);
41	if (!user) {
42		printf("%s not found\n", user_name);
43		CU_FAIL("user not found");
44		return;
45	}
46	found = calloc(num_roles, sizeof(unsigned char));
47	CU_ASSERT_FATAL(found != NULL);
48	ebitmap_for_each_bit(&user->roles.roles, tnode, i) {
49		if (ebitmap_node_get_bit(tnode, i)) {
50			extra++;
51			for (j = 0; j < num_roles; j++) {
52				if (strcmp(role_names[j], p->p_role_val_to_name[i]) == 0) {
53					extra--;
54					found[j] += 1;
55					break;
56				}
57			}
58		}
59	}
60	for (j = 0; j < num_roles; j++) {
61		if (found[j] != 1) {
62			printf("role %s associated with user %s %d times\n", role_names[j], user_name, found[j]);
63			CU_FAIL("user mapping failure\n");
64		}
65	}
66	free(found);
67	CU_ASSERT_EQUAL(extra, 0);
68}
69
70void test_expander_user_mapping(void)
71{
72	char *roles1[] = { "user_check_1_1_r", "user_check_1_2_r" };
73
74	check_user_roles(&user_expanded, "user_check_1", roles1, 2);
75}
76