xref: /aosp_15_r20/external/selinux/libsepol/tests/test-expander-users.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker /*
2*2d543d20SAndroid Build Coastguard Worker  * Authors: Chad Sellers <[email protected]>
3*2d543d20SAndroid Build Coastguard Worker  *          Joshua Brindle <[email protected]>
4*2d543d20SAndroid Build Coastguard Worker  *          Chris PeBenito <[email protected]>
5*2d543d20SAndroid Build Coastguard Worker  *
6*2d543d20SAndroid Build Coastguard Worker  * Copyright (C) 2006 Tresys Technology, LLC
7*2d543d20SAndroid Build Coastguard Worker  *
8*2d543d20SAndroid Build Coastguard Worker  *  This library is free software; you can redistribute it and/or
9*2d543d20SAndroid Build Coastguard Worker  *  modify it under the terms of the GNU Lesser General Public
10*2d543d20SAndroid Build Coastguard Worker  *  License as published by the Free Software Foundation; either
11*2d543d20SAndroid Build Coastguard Worker  *  version 2.1 of the License, or (at your option) any later version.
12*2d543d20SAndroid Build Coastguard Worker  *
13*2d543d20SAndroid Build Coastguard Worker  *  This library is distributed in the hope that it will be useful,
14*2d543d20SAndroid Build Coastguard Worker  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15*2d543d20SAndroid Build Coastguard Worker  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16*2d543d20SAndroid Build Coastguard Worker  *  Lesser General Public License for more details.
17*2d543d20SAndroid Build Coastguard Worker  *
18*2d543d20SAndroid Build Coastguard Worker  *  You should have received a copy of the GNU Lesser General Public
19*2d543d20SAndroid Build Coastguard Worker  *  License along with this library; if not, write to the Free Software
20*2d543d20SAndroid Build Coastguard Worker  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21*2d543d20SAndroid Build Coastguard Worker  */
22*2d543d20SAndroid Build Coastguard Worker 
23*2d543d20SAndroid Build Coastguard Worker #include "test-expander-users.h"
24*2d543d20SAndroid Build Coastguard Worker #include "helpers.h"
25*2d543d20SAndroid Build Coastguard Worker 
26*2d543d20SAndroid Build Coastguard Worker #include <sepol/policydb/policydb.h>
27*2d543d20SAndroid Build Coastguard Worker #include <CUnit/Basic.h>
28*2d543d20SAndroid Build Coastguard Worker #include <stdlib.h>
29*2d543d20SAndroid Build Coastguard Worker 
30*2d543d20SAndroid Build Coastguard Worker extern policydb_t user_expanded;
31*2d543d20SAndroid Build Coastguard Worker 
check_user_roles(policydb_t * p,const char * user_name,const char ** role_names,int num_roles)32*2d543d20SAndroid Build Coastguard Worker static void check_user_roles(policydb_t * p, const char *user_name, const char **role_names, int num_roles)
33*2d543d20SAndroid Build Coastguard Worker {
34*2d543d20SAndroid Build Coastguard Worker 	user_datum_t *user;
35*2d543d20SAndroid Build Coastguard Worker 	ebitmap_node_t *tnode;
36*2d543d20SAndroid Build Coastguard Worker 	unsigned int i;
37*2d543d20SAndroid Build Coastguard Worker 	int j;
38*2d543d20SAndroid Build Coastguard Worker 	unsigned char *found;	/* array of booleans of roles found */
39*2d543d20SAndroid Build Coastguard Worker 	int extra = 0;		/* number of extra roles found */
40*2d543d20SAndroid Build Coastguard Worker 
41*2d543d20SAndroid Build Coastguard Worker 	user = (user_datum_t *) hashtab_search(p->p_users.table, user_name);
42*2d543d20SAndroid Build Coastguard Worker 	if (!user) {
43*2d543d20SAndroid Build Coastguard Worker 		printf("%s not found\n", user_name);
44*2d543d20SAndroid Build Coastguard Worker 		CU_FAIL("user not found");
45*2d543d20SAndroid Build Coastguard Worker 		return;
46*2d543d20SAndroid Build Coastguard Worker 	}
47*2d543d20SAndroid Build Coastguard Worker 	found = calloc(num_roles, sizeof(unsigned char));
48*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_FATAL(found != NULL);
49*2d543d20SAndroid Build Coastguard Worker 	ebitmap_for_each_positive_bit(&user->roles.roles, tnode, i) {
50*2d543d20SAndroid Build Coastguard Worker 		extra++;
51*2d543d20SAndroid Build Coastguard Worker 		for (j = 0; j < num_roles; j++) {
52*2d543d20SAndroid Build Coastguard Worker 			if (strcmp(role_names[j], p->p_role_val_to_name[i]) == 0) {
53*2d543d20SAndroid Build Coastguard Worker 				extra--;
54*2d543d20SAndroid Build Coastguard Worker 				found[j] += 1;
55*2d543d20SAndroid Build Coastguard Worker 				break;
56*2d543d20SAndroid Build Coastguard Worker 			}
57*2d543d20SAndroid Build Coastguard Worker 		}
58*2d543d20SAndroid Build Coastguard Worker 	}
59*2d543d20SAndroid Build Coastguard Worker 	for (j = 0; j < num_roles; j++) {
60*2d543d20SAndroid Build Coastguard Worker 		if (found[j] != 1) {
61*2d543d20SAndroid Build Coastguard Worker 			printf("role %s associated with user %s %d times\n", role_names[j], user_name, found[j]);
62*2d543d20SAndroid Build Coastguard Worker 			CU_FAIL("user mapping failure\n");
63*2d543d20SAndroid Build Coastguard Worker 		}
64*2d543d20SAndroid Build Coastguard Worker 	}
65*2d543d20SAndroid Build Coastguard Worker 	free(found);
66*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_EQUAL(extra, 0);
67*2d543d20SAndroid Build Coastguard Worker }
68*2d543d20SAndroid Build Coastguard Worker 
test_expander_user_mapping(void)69*2d543d20SAndroid Build Coastguard Worker void test_expander_user_mapping(void)
70*2d543d20SAndroid Build Coastguard Worker {
71*2d543d20SAndroid Build Coastguard Worker 	const char *roles1[] = { "user_check_1_1_r", "user_check_1_2_r" };
72*2d543d20SAndroid Build Coastguard Worker 
73*2d543d20SAndroid Build Coastguard Worker 	check_user_roles(&user_expanded, "user_check_1", roles1, 2);
74*2d543d20SAndroid Build Coastguard Worker }
75