Home | History | Annotate | Download | only in testsuite
      1 /*
      2  * Copyright (C) 2012-2013  ProFUSION embedded systems
      3  * Copyright (C) 2012  Pedro Pedruzzi
      4  *
      5  * This program 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 program 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, see <http://www.gnu.org/licenses/>.
     17  */
     18 
     19 #include <fcntl.h>
     20 #include <stdbool.h>
     21 #include <stdio.h>
     22 #include <stdlib.h>
     23 #include <string.h>
     24 #include <unistd.h>
     25 #include <sys/stat.h>
     26 #include <sys/types.h>
     27 
     28 #include <shared/util.h>
     29 
     30 #include "testsuite.h"
     31 
     32 static int alias_1(const struct test *t)
     33 {
     34 	static const char *input[] = {
     35 		"test1234",
     36 		"test[abcfoobar]2211",
     37 		"bar[aaa][bbbb]sss",
     38 		"kmod[p.b]lib",
     39 		"[az]1234[AZ]",
     40 		NULL,
     41 	};
     42 
     43 	char buf[PATH_MAX];
     44 	size_t len;
     45 	const char **alias;
     46 
     47 	for (alias = input; *alias != NULL; alias++) {
     48 		int ret;
     49 
     50 		ret = alias_normalize(*alias, buf, &len);
     51 		printf("input   %s\n", *alias);
     52 		printf("return  %d\n", ret);
     53 
     54 		if (ret == 0) {
     55 			printf("len     %zu\n", len);
     56 			printf("output  %s\n", buf);
     57 		}
     58 
     59 		printf("\n");
     60 	}
     61 
     62 	return EXIT_SUCCESS;
     63 }
     64 DEFINE_TEST(alias_1,
     65 	.description = "check if alias_normalize does the right thing",
     66 	.config = {
     67 		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
     68 	},
     69 	.need_spawn = true,
     70 	.output = {
     71 		.out = TESTSUITE_ROOTFS "test-util/alias-correct.txt",
     72 	});
     73 
     74 static int test_freadline_wrapped(const struct test *t)
     75 {
     76 	FILE *fp = fopen("/freadline_wrapped-input.txt", "re");
     77 
     78 	if (!fp)
     79 		return EXIT_FAILURE;
     80 
     81 	while (!feof(fp) && !ferror(fp)) {
     82 		unsigned int num = 0;
     83 		char *s = freadline_wrapped(fp, &num);
     84 		if (!s)
     85 			break;
     86 		puts(s);
     87 		free(s);
     88 		printf("%u\n", num);
     89 	}
     90 
     91 	fclose(fp);
     92 	return EXIT_SUCCESS;
     93 }
     94 DEFINE_TEST(test_freadline_wrapped,
     95 	.description = "check if freadline_wrapped() does the right thing",
     96 	.config = {
     97 		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-util/",
     98 	},
     99 	.need_spawn = true,
    100 	.output = {
    101 		.out = TESTSUITE_ROOTFS "test-util/freadline_wrapped-correct.txt",
    102 	});
    103 
    104 static int test_strchr_replace(const struct test *t)
    105 {
    106 	_cleanup_free_ char *s = strdup("this is a test string");
    107 	const char *res = "thiC iC a teCt Ctring";
    108 
    109 	strchr_replace(s, 's', 'C');
    110 	assert_return(streq(s, res), EXIT_FAILURE);
    111 
    112 	return EXIT_SUCCESS;
    113 }
    114 DEFINE_TEST(test_strchr_replace,
    115 	.description = "check implementation of strchr_replace()",
    116 	.need_spawn = false,
    117 	);
    118 
    119 static int test_underscores(const struct test *t)
    120 {
    121 	struct teststr {
    122 		char *val;
    123 		const char *res;
    124 	} teststr[] = {
    125 		{ strdup("aa-bb-cc_"), "aa_bb_cc_" },
    126 		{ strdup("-aa-bb-cc-"), "_aa_bb_cc_" },
    127 		{ strdup("-aa[-bb-]cc-"), "_aa[-bb-]cc_" },
    128 		{ strdup("-aa-[bb]-cc-"), "_aa_[bb]_cc_" },
    129 		{ strdup("-aa-[b-b]-cc-"), "_aa_[b-b]_cc_" },
    130 		{ strdup("-aa-b[-]b-cc"), "_aa_b[-]b_cc" },
    131 		{ }
    132 	}, *iter;
    133 
    134 	for (iter = &teststr[0]; iter->val != NULL; iter++) {
    135 		_cleanup_free_ char *val = iter->val;
    136 		underscores(val);
    137 		assert_return(streq(val, iter->res), EXIT_FAILURE);
    138 	}
    139 
    140 	return EXIT_SUCCESS;
    141 }
    142 DEFINE_TEST(test_underscores,
    143 	.description = "check implementation of underscores()",
    144 	.need_spawn = false,
    145 	);
    146 
    147 static int test_path_ends_with_kmod_ext(const struct test *t)
    148 {
    149 	struct teststr {
    150 		const char *val;
    151 		bool res;
    152 	} teststr[] = {
    153 		{ "/bla.ko", true },
    154 #ifdef ENABLE_ZLIB
    155 		{ "/bla.ko.gz", true },
    156 #endif
    157 #ifdef ENABLE_XZ
    158 		{ "/bla.ko.xz", true },
    159 #endif
    160 		{ "/bla.ko.x", false },
    161 		{ "/bla.ko.", false },
    162 		{ "/bla.koz", false },
    163 		{ "/b", false },
    164 		{ }
    165 	}, *iter;
    166 
    167 	for (iter = &teststr[0]; iter->val != NULL; iter++) {
    168 		assert_return(path_ends_with_kmod_ext(iter->val,
    169 						      strlen(iter->val)) == iter->res,
    170 			      EXIT_FAILURE);
    171 	}
    172 
    173 	return EXIT_SUCCESS;
    174 }
    175 DEFINE_TEST(test_path_ends_with_kmod_ext,
    176 	.description = "check implementation of path_ends_with_kmod_ext()",
    177 	.need_spawn = false,
    178 	);
    179 
    180 #define TEST_WRITE_STR_SAFE_FILE "/write-str-safe"
    181 #define TEST_WRITE_STR_SAFE_PATH TESTSUITE_ROOTFS "test-util2/" TEST_WRITE_STR_SAFE_FILE
    182 static int test_write_str_safe(const struct test *t)
    183 {
    184 	const char *s = "test";
    185 	int fd;
    186 
    187 	fd = open(TEST_WRITE_STR_SAFE_FILE ".txt", O_CREAT|O_TRUNC|O_WRONLY, 0644);
    188 	assert_return(fd >= 0, EXIT_FAILURE);
    189 
    190 	write_str_safe(fd, s, strlen(s));
    191 	close(fd);
    192 
    193 	return EXIT_SUCCESS;
    194 }
    195 DEFINE_TEST(test_write_str_safe,
    196 	.description = "check implementation of write_str_safe()",
    197 	.config = {
    198 		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-util2/",
    199 	},
    200 	.need_spawn = true,
    201 	.output = {
    202 		.files = (const struct keyval[]) {
    203 			{ TEST_WRITE_STR_SAFE_PATH ".txt",
    204 			  TEST_WRITE_STR_SAFE_PATH "-correct.txt" },
    205 			{ }
    206 		},
    207 	});
    208 
    209 static int test_addu64_overflow(const struct test *t)
    210 {
    211 	uint64_t res;
    212 	bool overflow;
    213 
    214 	overflow = addu64_overflow(UINT64_MAX - 1, 1, &res);
    215 	assert_return(!overflow, EXIT_FAILURE);
    216 	assert_return(res == UINT64_MAX, EXIT_FAILURE);
    217 
    218 	overflow = addu64_overflow(UINT64_MAX, 1, &res);
    219 	assert_return(overflow, EXIT_FAILURE);
    220 
    221 	return EXIT_SUCCESS;
    222 }
    223 DEFINE_TEST(test_addu64_overflow,
    224 	.description = "check implementation of addu4_overflow()",
    225 	.need_spawn = false,
    226 	);
    227 
    228 
    229 TESTSUITE_MAIN();
    230