Home | History | Annotate | Download | only in tests
      1 /* Authors: Mark Goldman <mgoldman (at) 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 void test_semanage_is_prefix(void);
     38 void test_semanage_split_on_space(void);
     39 void test_semanage_split(void);
     40 void test_semanage_list(void);
     41 void test_semanage_str_count(void);
     42 void test_semanage_rtrim(void);
     43 void test_semanage_findval(void);
     44 void test_slurp_file_filter(void);
     45 
     46 char fname[] = {
     47 	'T', 'E', 'S', 'T', '_', 'T', 'E', 'M', 'P', '_', 'X', 'X', 'X', 'X',
     48 	'X', 'X', '\0'
     49 };
     50 int fd;
     51 FILE *fptr;
     52 
     53 int semanage_utilities_test_init(void)
     54 {
     55 	fd = mkstemp(fname);
     56 
     57 	if (fd < 0) {
     58 		perror("test_semanage_findval: ");
     59 		CU_FAIL_FATAL
     60 		    ("Error opening temporary file, test cannot start.");
     61 	}
     62 
     63 	fptr = fdopen(fd, "w+");
     64 	if (!fptr) {
     65 		perror("test_semanage_findval file: ");
     66 		CU_FAIL_FATAL("Error opening file stream, test cannot start.");
     67 	}
     68 
     69 	fprintf(fptr, "one\ntwo\nthree\nsigma=foo\n#boo\n#bar\n");
     70 
     71 	rewind(fptr);
     72 	return 0;
     73 }
     74 
     75 int semanage_utilities_test_cleanup(void)
     76 {
     77 	unlink(fname);
     78 	return 0;
     79 }
     80 
     81 int semanage_utilities_add_tests(CU_pSuite suite)
     82 {
     83 	if (NULL == CU_add_test(suite, "semanage_is_prefix",
     84 				test_semanage_is_prefix)) {
     85 		goto err;
     86 	}
     87 	if (NULL == CU_add_test(suite, "semanage_split_on_space",
     88 				test_semanage_split_on_space)) {
     89 		goto err;
     90 	}
     91 	if (NULL == CU_add_test(suite, "semanage_split", test_semanage_split)) {
     92 		goto err;
     93 	}
     94 	if (NULL == CU_add_test(suite, "semanage_list", test_semanage_list)) {
     95 		goto err;
     96 	}
     97 	if (NULL == CU_add_test(suite, "semanage_str_count",
     98 				test_semanage_str_count)) {
     99 		goto err;
    100 	}
    101 	if (NULL == CU_add_test(suite, "semanage_rtrim", test_semanage_rtrim)) {
    102 		goto err;
    103 	}
    104 	if (NULL == CU_add_test(suite, "semanage_findval",
    105 				test_semanage_findval)) {
    106 		goto err;
    107 	}
    108 	if (NULL == CU_add_test(suite, "slurp_file_filter",
    109 				test_slurp_file_filter)) {
    110 		goto err;
    111 	}
    112 	return 0;
    113       err:
    114 	CU_cleanup_registry();
    115 	return CU_get_error();
    116 }
    117 
    118 void test_semanage_is_prefix(void)
    119 {
    120 	char *str = "some string";
    121 	char *pre = "some";
    122 	char *not_pre = "not this";
    123 
    124 	CU_ASSERT_TRUE(semanage_is_prefix(str, pre));
    125 	CU_ASSERT_TRUE(semanage_is_prefix(str, ""));
    126 	CU_ASSERT_TRUE(semanage_is_prefix(str, NULL));
    127 	CU_ASSERT_FALSE(semanage_is_prefix(str, not_pre));
    128 }
    129 
    130 void test_semanage_split_on_space(void)
    131 {
    132 	char *str = strdup("foo bar baz");
    133 	char *temp;
    134 
    135 	if (!str) {
    136 		CU_FAIL
    137 		    ("semanage_split_on_space: unable to perform test, no memory");
    138 	}
    139 	temp = semanage_split_on_space(str);
    140 	if (strncmp(temp, "bar", 3)) {
    141 		CU_FAIL("semanage_split_on_space: token did not match");
    142 	}
    143 	temp = semanage_split_on_space(temp);
    144 	if (strncmp(temp, "baz", 3)) {
    145 		CU_FAIL("semanage_split_on_space: token did not match");
    146 	}
    147 	temp = semanage_split_on_space(temp);
    148 	if (strcmp(temp, "")) {
    149 		CU_FAIL("semanage_split_on_space: token did not match");
    150 	}
    151 
    152 	free(str);
    153 }
    154 
    155 void test_semanage_split(void)
    156 {
    157 	char *str = strdup("foo1 foo2 foo:bar");
    158 	char *temp;
    159 
    160 	if (!str) {
    161 		CU_FAIL
    162 		    ("semanage_split_on_space: unable to perform test, no memory");
    163 		return;
    164 	}
    165 	temp = semanage_split(str, NULL);
    166 	CU_ASSERT_NSTRING_EQUAL(temp, "foo2", 4);
    167 	temp = semanage_split(temp, "");
    168 	CU_ASSERT_NSTRING_EQUAL(temp, "foo", 3);
    169 	temp = semanage_split(temp, ":");
    170 	CU_ASSERT_NSTRING_EQUAL(temp, "bar", 3);
    171 
    172 	free(str);
    173 }
    174 
    175 void test_semanage_list(void)
    176 {
    177 	semanage_list_t *list = NULL;
    178 	semanage_list_t *ptr = NULL;
    179 	char *temp = NULL;
    180 	int retval = 0;
    181 
    182 	CU_ASSERT_FALSE(semanage_list_push(&list, "foo"));
    183 	CU_ASSERT_PTR_NOT_NULL(list);
    184 	CU_ASSERT_FALSE(semanage_list_push(&list, "bar"));
    185 	CU_ASSERT_FALSE(semanage_list_push(&list, "gonk"));
    186 	CU_ASSERT_FALSE(semanage_list_push(&list, "zebra"));
    187 
    188 	for (ptr = list; ptr; ptr = ptr->next)
    189 		retval++;
    190 	CU_ASSERT_EQUAL(retval, 4);
    191 
    192 	temp = semanage_list_pop(&list);
    193 	CU_ASSERT_STRING_EQUAL(temp, "zebra");
    194 	CU_ASSERT_FALSE(semanage_list_push(&list, temp));
    195 	free(temp);
    196 	temp = NULL;
    197 
    198 	retval = 0;
    199 	for (ptr = list; ptr; ptr = ptr->next)
    200 		retval++;
    201 	CU_ASSERT_EQUAL(retval, 4);
    202 
    203 	retval = semanage_list_sort(&list);
    204 	if (retval) {
    205 		CU_FAIL
    206 		    ("semanage_list_sort: error unrelated to sort (memory?)");
    207 		goto past_sort;
    208 	}
    209 	CU_ASSERT_STRING_EQUAL(list->data, "bar");
    210 	CU_ASSERT_STRING_EQUAL(list->next->data, "foo");
    211 	CU_ASSERT_STRING_EQUAL(list->next->next->data, "gonk");
    212 	CU_ASSERT_STRING_EQUAL(list->next->next->next->data, "zebra");
    213 
    214       past_sort:
    215 	ptr = semanage_list_find(list, "zebra");
    216 	CU_ASSERT_PTR_NOT_NULL(ptr);
    217 	ptr = semanage_list_find(list, "bogus");
    218 	CU_ASSERT_PTR_NULL(ptr);
    219 
    220 	semanage_list_destroy(&list);
    221 	CU_ASSERT_PTR_NULL(list);
    222 }
    223 
    224 void test_semanage_str_count(void)
    225 {
    226 	char *test_string = "abaababbaaaba";
    227 
    228 	CU_ASSERT_EQUAL(semanage_str_count(test_string, 'z'), 0);
    229 	CU_ASSERT_EQUAL(semanage_str_count(test_string, 'a'), 8);
    230 	CU_ASSERT_EQUAL(semanage_str_count(test_string, 'b'), 5);
    231 }
    232 
    233 void test_semanage_rtrim(void)
    234 {
    235 	char *str = strdup("/blah/foo/bar/baz/");
    236 
    237 	CU_ASSERT_PTR_NOT_NULL_FATAL(str);
    238 
    239 	semanage_rtrim(str, 'Q');
    240 	CU_ASSERT_STRING_EQUAL(str, "/blah/foo/bar/baz/");
    241 	semanage_rtrim(str, 'a');
    242 	CU_ASSERT_STRING_EQUAL(str, "/blah/foo/bar/b");
    243 	semanage_rtrim(str, '/');
    244 	CU_ASSERT_STRING_EQUAL(str, "/blah/foo/bar");
    245 }
    246 
    247 void test_semanage_findval(void)
    248 {
    249 	char *tok;
    250 	if (!fptr) {
    251 		CU_FAIL_FATAL("Temporary file was not created, aborting test.");
    252 	}
    253 	tok = semanage_findval(fname, "one", NULL);
    254 	CU_ASSERT_STRING_EQUAL(tok, "");
    255 	rewind(fptr);
    256 	tok = semanage_findval(fname, "one", "");
    257 	CU_ASSERT_STRING_EQUAL(tok, "");
    258 	free(tok);
    259 	rewind(fptr);
    260 	tok = semanage_findval(fname, "sigma", "=");
    261 	CU_ASSERT_STRING_EQUAL(tok, "foo");
    262 }
    263 
    264 int PREDICATE(const char *str)
    265 {
    266 	return semanage_is_prefix(str, "#");
    267 }
    268 
    269 void test_slurp_file_filter(void)
    270 {
    271 	semanage_list_t *data, *tmp;
    272 	int cnt = 0;
    273 
    274 	if (!fptr) {
    275 		CU_FAIL_FATAL("Temporary file was not created, aborting test.");
    276 	}
    277 	rewind(fptr);
    278 	data = semanage_slurp_file_filter(fptr, PREDICATE);
    279 	CU_ASSERT_PTR_NOT_NULL_FATAL(data);
    280 	for (tmp = data; tmp; tmp = tmp->next)
    281 		cnt++;
    282 	CU_ASSERT_EQUAL(cnt, 2);
    283 
    284 	semanage_list_destroy(&data);
    285 }
    286