xref: /aosp_15_r20/external/selinux/libsemanage/tests/test_other.c (revision 2d543d20722ada2425b5bdab9d0d1d29470e7bba)
1*2d543d20SAndroid Build Coastguard Worker /*
2*2d543d20SAndroid Build Coastguard Worker  * Authors: Jan Zarsky <[email protected]>
3*2d543d20SAndroid Build Coastguard Worker  *
4*2d543d20SAndroid Build Coastguard Worker  * Copyright (C) 2019 Red Hat, Inc.
5*2d543d20SAndroid Build Coastguard Worker  *
6*2d543d20SAndroid Build Coastguard Worker  * This library is free software; you can redistribute it and/or
7*2d543d20SAndroid Build Coastguard Worker  * modify it under the terms of the GNU Lesser General Public
8*2d543d20SAndroid Build Coastguard Worker  * License as published by the Free Software Foundation; either
9*2d543d20SAndroid Build Coastguard Worker  * version 2.1 of the License, or (at your option) any later version.
10*2d543d20SAndroid Build Coastguard Worker  *
11*2d543d20SAndroid Build Coastguard Worker  * This library is distributed in the hope that it will be useful,
12*2d543d20SAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*2d543d20SAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*2d543d20SAndroid Build Coastguard Worker  * Lesser General Public License for more details.
15*2d543d20SAndroid Build Coastguard Worker  *
16*2d543d20SAndroid Build Coastguard Worker  * You should have received a copy of the GNU Lesser General Public
17*2d543d20SAndroid Build Coastguard Worker  * License along with this library; if not, write to the Free Software
18*2d543d20SAndroid Build Coastguard Worker  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19*2d543d20SAndroid Build Coastguard Worker  */
20*2d543d20SAndroid Build Coastguard Worker 
21*2d543d20SAndroid Build Coastguard Worker #include "utilities.h"
22*2d543d20SAndroid Build Coastguard Worker #include "test_other.h"
23*2d543d20SAndroid Build Coastguard Worker 
24*2d543d20SAndroid Build Coastguard Worker /* context_record.h */
25*2d543d20SAndroid Build Coastguard Worker void test_semanage_context(void);
26*2d543d20SAndroid Build Coastguard Worker 
27*2d543d20SAndroid Build Coastguard Worker /* debug.h */
28*2d543d20SAndroid Build Coastguard Worker void test_debug(void);
29*2d543d20SAndroid Build Coastguard Worker 
30*2d543d20SAndroid Build Coastguard Worker extern semanage_handle_t *sh;
31*2d543d20SAndroid Build Coastguard Worker 
other_test_init(void)32*2d543d20SAndroid Build Coastguard Worker int other_test_init(void)
33*2d543d20SAndroid Build Coastguard Worker {
34*2d543d20SAndroid Build Coastguard Worker 	return 0;
35*2d543d20SAndroid Build Coastguard Worker }
36*2d543d20SAndroid Build Coastguard Worker 
other_test_cleanup(void)37*2d543d20SAndroid Build Coastguard Worker int other_test_cleanup(void)
38*2d543d20SAndroid Build Coastguard Worker {
39*2d543d20SAndroid Build Coastguard Worker 	return 0;
40*2d543d20SAndroid Build Coastguard Worker }
41*2d543d20SAndroid Build Coastguard Worker 
other_add_tests(CU_pSuite suite)42*2d543d20SAndroid Build Coastguard Worker int other_add_tests(CU_pSuite suite)
43*2d543d20SAndroid Build Coastguard Worker {
44*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "semanage_context", test_semanage_context);
45*2d543d20SAndroid Build Coastguard Worker 	CU_add_test(suite, "debug", test_debug);
46*2d543d20SAndroid Build Coastguard Worker 
47*2d543d20SAndroid Build Coastguard Worker 	return 0;
48*2d543d20SAndroid Build Coastguard Worker }
49*2d543d20SAndroid Build Coastguard Worker 
50*2d543d20SAndroid Build Coastguard Worker /* Function semanage_context_get_user, semanage_context_set_user,
51*2d543d20SAndroid Build Coastguard Worker  * semanage_context_get_role, semanage_context_set_role,
52*2d543d20SAndroid Build Coastguard Worker  * semanage_context_get_type, semanage_context_set_type,
53*2d543d20SAndroid Build Coastguard Worker  * semanage_context_get_mls, semanage_context_set_mls,
54*2d543d20SAndroid Build Coastguard Worker  * semanage_context_create, semanage_context_clone,
55*2d543d20SAndroid Build Coastguard Worker  * semanage_context_free, semanage_context_from_string
56*2d543d20SAndroid Build Coastguard Worker  * semanage_context_to_string
57*2d543d20SAndroid Build Coastguard Worker  */
test_semanage_context(void)58*2d543d20SAndroid Build Coastguard Worker void test_semanage_context(void)
59*2d543d20SAndroid Build Coastguard Worker {
60*2d543d20SAndroid Build Coastguard Worker 	semanage_context_t *con = NULL;
61*2d543d20SAndroid Build Coastguard Worker 	semanage_context_t *con_clone = NULL;
62*2d543d20SAndroid Build Coastguard Worker 	char *str = NULL;
63*2d543d20SAndroid Build Coastguard Worker 
64*2d543d20SAndroid Build Coastguard Worker 	/* setup */
65*2d543d20SAndroid Build Coastguard Worker 	setup_handle(SH_CONNECT);
66*2d543d20SAndroid Build Coastguard Worker 
67*2d543d20SAndroid Build Coastguard Worker 	/* test */
68*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_context_create(sh, &con) >= 0);
69*2d543d20SAndroid Build Coastguard Worker 
70*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_context_set_user(sh, con, "user_u") >= 0);
71*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_user(con), "user_u");
72*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_context_set_role(sh, con, "role_r") >= 0);
73*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_role(con), "role_r");
74*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_context_set_type(sh, con, "type_t") >= 0);
75*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_type(con), "type_t");
76*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_context_set_mls(sh, con, "s0") >= 0);
77*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_mls(con), "s0");
78*2d543d20SAndroid Build Coastguard Worker 
79*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_context_to_string(sh, con, &str) >= 0);
80*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(str);
81*2d543d20SAndroid Build Coastguard Worker 	assert(str);
82*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(str, "user_u:role_r:type_t:s0");
83*2d543d20SAndroid Build Coastguard Worker 
84*2d543d20SAndroid Build Coastguard Worker 	semanage_context_free(con);
85*2d543d20SAndroid Build Coastguard Worker 	con = NULL;
86*2d543d20SAndroid Build Coastguard Worker 
87*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_context_from_string(sh, "my_u:my_r:my_t:s0",
88*2d543d20SAndroid Build Coastguard Worker 					       &con) >= 0);
89*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_user(con), "my_u");
90*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_role(con), "my_r");
91*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_type(con), "my_t");
92*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_mls(con), "s0");
93*2d543d20SAndroid Build Coastguard Worker 
94*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_context_clone(sh, con, &con_clone) >= 0);
95*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_user(con_clone), "my_u");
96*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_role(con_clone), "my_r");
97*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_type(con_clone), "my_t");
98*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_STRING_EQUAL(semanage_context_get_mls(con_clone), "s0");
99*2d543d20SAndroid Build Coastguard Worker 
100*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
101*2d543d20SAndroid Build Coastguard Worker 	free(str);
102*2d543d20SAndroid Build Coastguard Worker 	semanage_context_free(con);
103*2d543d20SAndroid Build Coastguard Worker 	semanage_context_free(con_clone);
104*2d543d20SAndroid Build Coastguard Worker 	cleanup_handle(SH_CONNECT);
105*2d543d20SAndroid Build Coastguard Worker }
106*2d543d20SAndroid Build Coastguard Worker 
107*2d543d20SAndroid Build Coastguard Worker /* Function semanage_msg_default_handler */
test_debug(void)108*2d543d20SAndroid Build Coastguard Worker void test_debug(void)
109*2d543d20SAndroid Build Coastguard Worker {
110*2d543d20SAndroid Build Coastguard Worker 	semanage_module_info_t *modinfo = NULL;
111*2d543d20SAndroid Build Coastguard Worker 
112*2d543d20SAndroid Build Coastguard Worker 	/* setup */
113*2d543d20SAndroid Build Coastguard Worker 	sh = semanage_handle_create();
114*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT_PTR_NOT_NULL(sh);
115*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_connect(sh) >= 0);
116*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_module_info_create(sh, &modinfo) >= 0);
117*2d543d20SAndroid Build Coastguard Worker 
118*2d543d20SAndroid Build Coastguard Worker 	/* test */
119*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_module_info_set_priority(sh, modinfo, -42) < 0);
120*2d543d20SAndroid Build Coastguard Worker 
121*2d543d20SAndroid Build Coastguard Worker 	/* cleanup */
122*2d543d20SAndroid Build Coastguard Worker 	semanage_module_info_destroy(sh, modinfo);
123*2d543d20SAndroid Build Coastguard Worker 	free(modinfo);
124*2d543d20SAndroid Build Coastguard Worker 	CU_ASSERT(semanage_disconnect(sh) >= 0);
125*2d543d20SAndroid Build Coastguard Worker 	semanage_handle_destroy(sh);
126*2d543d20SAndroid Build Coastguard Worker }
127