16cd6a6acSopenharmony_ci/* 26cd6a6acSopenharmony_ci * Authors: Chad Sellers <csellers@tresys.com> 36cd6a6acSopenharmony_ci * Joshua Brindle <jbrindle@tresys.com> 46cd6a6acSopenharmony_ci * Chris PeBenito <cpebenito@tresys.com> 56cd6a6acSopenharmony_ci * 66cd6a6acSopenharmony_ci * Copyright (C) 2006 Tresys Technology, LLC 76cd6a6acSopenharmony_ci * 86cd6a6acSopenharmony_ci * This library is free software; you can redistribute it and/or 96cd6a6acSopenharmony_ci * modify it under the terms of the GNU Lesser General Public 106cd6a6acSopenharmony_ci * License as published by the Free Software Foundation; either 116cd6a6acSopenharmony_ci * version 2.1 of the License, or (at your option) any later version. 126cd6a6acSopenharmony_ci * 136cd6a6acSopenharmony_ci * This library is distributed in the hope that it will be useful, 146cd6a6acSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 156cd6a6acSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 166cd6a6acSopenharmony_ci * Lesser General Public License for more details. 176cd6a6acSopenharmony_ci * 186cd6a6acSopenharmony_ci * You should have received a copy of the GNU Lesser General Public 196cd6a6acSopenharmony_ci * License along with this library; if not, write to the Free Software 206cd6a6acSopenharmony_ci * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 216cd6a6acSopenharmony_ci */ 226cd6a6acSopenharmony_ci 236cd6a6acSopenharmony_ci#include "test-expander-users.h" 246cd6a6acSopenharmony_ci#include "helpers.h" 256cd6a6acSopenharmony_ci 266cd6a6acSopenharmony_ci#include <sepol/policydb/policydb.h> 276cd6a6acSopenharmony_ci#include <CUnit/Basic.h> 286cd6a6acSopenharmony_ci#include <stdlib.h> 296cd6a6acSopenharmony_ci 306cd6a6acSopenharmony_ciextern policydb_t user_expanded; 316cd6a6acSopenharmony_ci 326cd6a6acSopenharmony_cistatic void check_user_roles(policydb_t * p, const char *user_name, const char **role_names, int num_roles) 336cd6a6acSopenharmony_ci{ 346cd6a6acSopenharmony_ci user_datum_t *user; 356cd6a6acSopenharmony_ci ebitmap_node_t *tnode; 366cd6a6acSopenharmony_ci unsigned int i; 376cd6a6acSopenharmony_ci int j; 386cd6a6acSopenharmony_ci unsigned char *found; /* array of booleans of roles found */ 396cd6a6acSopenharmony_ci int extra = 0; /* number of extra roles found */ 406cd6a6acSopenharmony_ci 416cd6a6acSopenharmony_ci user = (user_datum_t *) hashtab_search(p->p_users.table, user_name); 426cd6a6acSopenharmony_ci if (!user) { 436cd6a6acSopenharmony_ci printf("%s not found\n", user_name); 446cd6a6acSopenharmony_ci CU_FAIL("user not found"); 456cd6a6acSopenharmony_ci return; 466cd6a6acSopenharmony_ci } 476cd6a6acSopenharmony_ci found = calloc(num_roles, sizeof(unsigned char)); 486cd6a6acSopenharmony_ci CU_ASSERT_FATAL(found != NULL); 496cd6a6acSopenharmony_ci ebitmap_for_each_positive_bit(&user->roles.roles, tnode, i) { 506cd6a6acSopenharmony_ci extra++; 516cd6a6acSopenharmony_ci for (j = 0; j < num_roles; j++) { 526cd6a6acSopenharmony_ci if (strcmp(role_names[j], p->p_role_val_to_name[i]) == 0) { 536cd6a6acSopenharmony_ci extra--; 546cd6a6acSopenharmony_ci found[j] += 1; 556cd6a6acSopenharmony_ci break; 566cd6a6acSopenharmony_ci } 576cd6a6acSopenharmony_ci } 586cd6a6acSopenharmony_ci } 596cd6a6acSopenharmony_ci for (j = 0; j < num_roles; j++) { 606cd6a6acSopenharmony_ci if (found[j] != 1) { 616cd6a6acSopenharmony_ci printf("role %s associated with user %s %d times\n", role_names[j], user_name, found[j]); 626cd6a6acSopenharmony_ci CU_FAIL("user mapping failure\n"); 636cd6a6acSopenharmony_ci } 646cd6a6acSopenharmony_ci } 656cd6a6acSopenharmony_ci free(found); 666cd6a6acSopenharmony_ci CU_ASSERT_EQUAL(extra, 0); 676cd6a6acSopenharmony_ci} 686cd6a6acSopenharmony_ci 696cd6a6acSopenharmony_civoid test_expander_user_mapping(void) 706cd6a6acSopenharmony_ci{ 716cd6a6acSopenharmony_ci const char *roles1[] = { "user_check_1_1_r", "user_check_1_2_r" }; 726cd6a6acSopenharmony_ci 736cd6a6acSopenharmony_ci check_user_roles(&user_expanded, "user_check_1", roles1, 2); 746cd6a6acSopenharmony_ci} 75