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