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#include "helpers.h"
25
26#include <sepol/policydb/policydb.h>
27#include <CUnit/Basic.h>
28#include <stdlib.h>
29
30extern policydb_t user_expanded;
31
32static void check_user_roles(policydb_t * p, const char *user_name, const char **role_names, int num_roles)
33{
34	user_datum_t *user;
35	ebitmap_node_t *tnode;
36	unsigned int i;
37	int j;
38	unsigned char *found;	/* array of booleans of roles found */
39	int extra = 0;		/* number of extra roles found */
40
41	user = (user_datum_t *) hashtab_search(p->p_users.table, user_name);
42	if (!user) {
43		printf("%s not found\n", user_name);
44		CU_FAIL("user not found");
45		return;
46	}
47	found = calloc(num_roles, sizeof(unsigned char));
48	CU_ASSERT_FATAL(found != NULL);
49	ebitmap_for_each_positive_bit(&user->roles.roles, 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	for (j = 0; j < num_roles; j++) {
60		if (found[j] != 1) {
61			printf("role %s associated with user %s %d times\n", role_names[j], user_name, found[j]);
62			CU_FAIL("user mapping failure\n");
63		}
64	}
65	free(found);
66	CU_ASSERT_EQUAL(extra, 0);
67}
68
69void test_expander_user_mapping(void)
70{
71	const char *roles1[] = { "user_check_1_1_r", "user_check_1_2_r" };
72
73	check_user_roles(&user_expanded, "user_check_1", roles1, 2);
74}
75