1 /* Authors: Mark Goldman <mgoldman@tresys.com>
2 *
3 * Copyright (C) 2007 Tresys Technology, LLC
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /* The purpose of this file is to provide unit tests of the functions in:
21 *
22 * libsemanage/src/utilities.c
23 *
24 */
25
26 #include <CUnit/Basic.h>
27 #include <CUnit/Console.h>
28 #include <CUnit/TestDB.h>
29
30 #include <utilities.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36
37 #include "utilities.h"
38 #include "test_utilities.h"
39
40 static void test_semanage_is_prefix(void);
41 static void test_semanage_split_on_space(void);
42 static void test_semanage_split(void);
43 static void test_semanage_list(void);
44 static void test_semanage_str_count(void);
45 static void test_semanage_rtrim(void);
46 static void test_semanage_str_replace(void);
47 static void test_semanage_findval(void);
48 static void test_slurp_file_filter(void);
49
50 char fname[] = {
51 'T', 'E', 'S', 'T', '_', 'T', 'E', 'M', 'P', '_', 'X', 'X', 'X', 'X',
52 'X', 'X', '\0'
53 };
54 int fd;
55 FILE *fptr;
56
semanage_utilities_test_init(void)57 int semanage_utilities_test_init(void)
58 {
59 fd = mkstemp(fname);
60
61 if (fd < 0) {
62 perror("test_semanage_findval: ");
63 CU_FAIL_FATAL
64 ("Error opening temporary file, test cannot start.");
65 }
66
67 fptr = fdopen(fd, "w+");
68 if (!fptr) {
69 perror("test_semanage_findval file: ");
70 CU_FAIL_FATAL("Error opening file stream, test cannot start.");
71 }
72
73 fprintf(fptr, "one\ntwo\nthree\nsigma=foo\n#boo\n#bar\n");
74
75 rewind(fptr);
76 return 0;
77 }
78
semanage_utilities_test_cleanup(void)79 int semanage_utilities_test_cleanup(void)
80 {
81 unlink(fname);
82 return 0;
83 }
84
semanage_utilities_add_tests(CU_pSuite suite)85 int semanage_utilities_add_tests(CU_pSuite suite)
86 {
87 if (NULL == CU_add_test(suite, "semanage_is_prefix",
88 test_semanage_is_prefix)) {
89 goto err;
90 }
91 if (NULL == CU_add_test(suite, "semanage_split_on_space",
92 test_semanage_split_on_space)) {
93 goto err;
94 }
95 if (NULL == CU_add_test(suite, "semanage_split", test_semanage_split)) {
96 goto err;
97 }
98 if (NULL == CU_add_test(suite, "semanage_list", test_semanage_list)) {
99 goto err;
100 }
101 if (NULL == CU_add_test(suite, "semanage_str_count",
102 test_semanage_str_count)) {
103 goto err;
104 }
105 if (NULL == CU_add_test(suite, "semanage_rtrim", test_semanage_rtrim)) {
106 goto err;
107 }
108 if (NULL == CU_add_test(suite, "semanage_str_replace",
109 test_semanage_str_replace)) {
110 goto err;
111 }
112 if (NULL == CU_add_test(suite, "semanage_findval",
113 test_semanage_findval)) {
114 goto err;
115 }
116 if (NULL == CU_add_test(suite, "slurp_file_filter",
117 test_slurp_file_filter)) {
118 goto err;
119 }
120 return 0;
121 err:
122 CU_cleanup_registry();
123 return CU_get_error();
124 }
125
test_semanage_is_prefix(void)126 static void test_semanage_is_prefix(void)
127 {
128 const char *str = "some string";
129 const char *pre = "some";
130 const char *not_pre = "not this";
131
132 CU_ASSERT_TRUE(semanage_is_prefix(str, pre));
133 CU_ASSERT_TRUE(semanage_is_prefix(str, ""));
134 CU_ASSERT_TRUE(semanage_is_prefix(str, NULL));
135 CU_ASSERT_FALSE(semanage_is_prefix(str, not_pre));
136 }
137
test_semanage_split_on_space(void)138 static void test_semanage_split_on_space(void)
139 {
140 char *str = strdup(" foo bar baz");
141 char *temp;
142
143 if (!str) {
144 CU_FAIL
145 ("semanage_split_on_space: unable to perform test, no memory");
146 return;
147 }
148 temp = semanage_split_on_space(str);
149 CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
150 CU_ASSERT_STRING_EQUAL(temp, "bar baz");
151 free(str);
152 str = temp;
153
154 temp = semanage_split_on_space(str);
155 CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
156 CU_ASSERT_STRING_EQUAL(temp, "baz");
157 free(str);
158 str = temp;
159
160 temp = semanage_split_on_space(str);
161 CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
162 CU_ASSERT_STRING_EQUAL(temp, "");
163 free(str);
164 free(temp);
165 }
166
test_semanage_split(void)167 static void test_semanage_split(void)
168 {
169 char *str = strdup("foo1 foo2 foo:bar:");
170 char *temp;
171
172 if (!str) {
173 CU_FAIL
174 ("semanage_split_on_space: unable to perform test, no memory");
175 return;
176 }
177 temp = semanage_split(str, NULL);
178 CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
179 CU_ASSERT_STRING_EQUAL(temp, "foo2 foo:bar:");
180 free(str);
181 str = temp;
182
183 temp = semanage_split(str, "");
184 CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
185 CU_ASSERT_STRING_EQUAL(temp, "foo:bar:");
186 free(str);
187 str = temp;
188
189 temp = semanage_split(str, ":");
190 CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
191 CU_ASSERT_STRING_EQUAL(temp, "bar:");
192 free(str);
193 str = temp;
194
195 temp = semanage_split(str, ":");
196 CU_ASSERT_PTR_NOT_NULL_FATAL(temp);
197 CU_ASSERT_STRING_EQUAL(temp, "");
198 free(str);
199 free(temp);
200 }
201
test_semanage_list(void)202 static void test_semanage_list(void)
203 {
204 semanage_list_t *list = NULL;
205 semanage_list_t *ptr = NULL;
206 char *temp = NULL;
207 int retval = 0;
208
209 CU_ASSERT_FALSE(semanage_list_push(&list, "foo"));
210 CU_ASSERT_PTR_NOT_NULL(list);
211 CU_ASSERT_FALSE(semanage_list_push(&list, "bar"));
212 CU_ASSERT_FALSE(semanage_list_push(&list, "gonk"));
213 CU_ASSERT_FALSE(semanage_list_push(&list, "zebra"));
214
215 for (ptr = list; ptr; ptr = ptr->next)
216 retval++;
217 CU_ASSERT_EQUAL(retval, 4);
218
219 temp = semanage_list_pop(&list);
220 CU_ASSERT_STRING_EQUAL(temp, "zebra");
221 CU_ASSERT_FALSE(semanage_list_push(&list, temp));
222 free(temp);
223 temp = NULL;
224
225 retval = 0;
226 for (ptr = list; ptr; ptr = ptr->next)
227 retval++;
228 CU_ASSERT_EQUAL(retval, 4);
229
230 retval = semanage_list_sort(&list);
231 if (retval) {
232 CU_FAIL
233 ("semanage_list_sort: error unrelated to sort (memory?)");
234 goto past_sort;
235 }
236 CU_ASSERT_STRING_EQUAL(list->data, "bar");
237 CU_ASSERT_STRING_EQUAL(list->next->data, "foo");
238 CU_ASSERT_STRING_EQUAL(list->next->next->data, "gonk");
239 CU_ASSERT_STRING_EQUAL(list->next->next->next->data, "zebra");
240
241 past_sort:
242 ptr = semanage_list_find(list, "zebra");
243 CU_ASSERT_PTR_NOT_NULL(ptr);
244 ptr = semanage_list_find(list, "bogus");
245 CU_ASSERT_PTR_NULL(ptr);
246
247 semanage_list_destroy(&list);
248 CU_ASSERT_PTR_NULL(list);
249 }
250
test_semanage_str_count(void)251 static void test_semanage_str_count(void)
252 {
253 const char *test_string = "abaababbaaaba";
254
255 CU_ASSERT_EQUAL(semanage_str_count(test_string, 'z'), 0);
256 CU_ASSERT_EQUAL(semanage_str_count(test_string, 'a'), 8);
257 CU_ASSERT_EQUAL(semanage_str_count(test_string, 'b'), 5);
258 }
259
test_semanage_rtrim(void)260 static void test_semanage_rtrim(void)
261 {
262 char *str = strdup("/blah/foo/bar/baz/");
263
264 CU_ASSERT_PTR_NOT_NULL_FATAL(str);
265
266 semanage_rtrim(str, 'Q');
267 CU_ASSERT_STRING_EQUAL(str, "/blah/foo/bar/baz/");
268 semanage_rtrim(str, 'a');
269 CU_ASSERT_STRING_EQUAL(str, "/blah/foo/bar/b");
270 semanage_rtrim(str, '/');
271 CU_ASSERT_STRING_EQUAL(str, "/blah/foo/bar");
272
273 free(str);
274 }
275
test_semanage_str_replace(void)276 static void test_semanage_str_replace(void)
277 {
278 const char *test_str = "Hello, I am %{USERNAME} and my id is %{USERID}";
279 char *str1, *str2;
280
281 str1 = semanage_str_replace("%{USERNAME}", "root", test_str, 0);
282 CU_ASSERT_STRING_EQUAL(str1, "Hello, I am root and my id is %{USERID}");
283
284 str2 = semanage_str_replace("%{USERID}", "0", str1, 1);
285 CU_ASSERT_STRING_EQUAL(str2, "Hello, I am root and my id is 0");
286 free(str1);
287 free(str2);
288
289 str1 = semanage_str_replace(":(", ";)", "Test :( :) ! :(:(:))(:(", 0);
290 CU_ASSERT_STRING_EQUAL(str1, "Test ;) :) ! ;);):))(;)");
291 free(str1);
292
293 str1 = semanage_str_replace(":(", ";)", "Test :( :) ! :(:(:))(:(", 3);
294 CU_ASSERT_STRING_EQUAL(str1, "Test ;) :) ! ;);):))(:(");
295 free(str1);
296
297 str1 = semanage_str_replace("", "empty search string", "test", 0);
298 CU_ASSERT_EQUAL(str1, NULL);
299
300 str1 = semanage_str_replace("a", "", "abracadabra", 0);
301 CU_ASSERT_STRING_EQUAL(str1, "brcdbr");
302 free(str1);
303 }
304
test_semanage_findval(void)305 static void test_semanage_findval(void)
306 {
307 char *tok;
308 if (!fptr) {
309 CU_FAIL_FATAL("Temporary file was not created, aborting test.");
310 }
311 tok = semanage_findval(fname, "one", NULL);
312 CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
313 CU_ASSERT_STRING_EQUAL(tok, "");
314 free(tok);
315 rewind(fptr);
316 tok = semanage_findval(fname, "one", "");
317 CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
318 CU_ASSERT_STRING_EQUAL(tok, "");
319 free(tok);
320 rewind(fptr);
321 tok = semanage_findval(fname, "sigma", "=");
322 CU_ASSERT_PTR_NOT_NULL_FATAL(tok);
323 CU_ASSERT_STRING_EQUAL(tok, "foo");
324 free(tok);
325 }
326
PREDICATE(const char * str)327 static int PREDICATE(const char *str)
328 {
329 return semanage_is_prefix(str, "#");
330 }
331
test_slurp_file_filter(void)332 static void test_slurp_file_filter(void)
333 {
334 semanage_list_t *data, *tmp;
335 int cnt = 0;
336
337 if (!fptr) {
338 CU_FAIL_FATAL("Temporary file was not created, aborting test.");
339 }
340 rewind(fptr);
341 data = semanage_slurp_file_filter(fptr, PREDICATE);
342 CU_ASSERT_PTR_NOT_NULL_FATAL(data);
343 for (tmp = data; tmp; tmp = tmp->next)
344 cnt++;
345 CU_ASSERT_EQUAL(cnt, 2);
346
347 semanage_list_destroy(&data);
348 }
349