• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
alias_1(const struct test * t)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 
test_freadline_wrapped(const struct test * t)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 
test_strchr_replace(const struct test * t)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 
test_underscores(const struct test * t)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 
test_path_ends_with_kmod_ext(const struct test * t)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 #ifdef ENABLE_ZSTD
161 		{ "/bla.ko.zst", true },
162 #endif
163 		{ "/bla.ko.x", false },
164 		{ "/bla.ko.", false },
165 		{ "/bla.koz", false },
166 		{ "/b", false },
167 		{ }
168 	}, *iter;
169 
170 	for (iter = &teststr[0]; iter->val != NULL; iter++) {
171 		assert_return(path_ends_with_kmod_ext(iter->val,
172 						      strlen(iter->val)) == iter->res,
173 			      EXIT_FAILURE);
174 	}
175 
176 	return EXIT_SUCCESS;
177 }
178 DEFINE_TEST(test_path_ends_with_kmod_ext,
179 	.description = "check implementation of path_ends_with_kmod_ext()",
180 	.need_spawn = false,
181 	);
182 
183 #define TEST_WRITE_STR_SAFE_FILE "/write-str-safe"
184 #define TEST_WRITE_STR_SAFE_PATH TESTSUITE_ROOTFS "test-util2/" TEST_WRITE_STR_SAFE_FILE
test_write_str_safe(const struct test * t)185 static int test_write_str_safe(const struct test *t)
186 {
187 	const char *s = "test";
188 	int fd;
189 
190 	fd = open(TEST_WRITE_STR_SAFE_FILE ".txt", O_CREAT|O_TRUNC|O_WRONLY, 0644);
191 	assert_return(fd >= 0, EXIT_FAILURE);
192 
193 	write_str_safe(fd, s, strlen(s));
194 	close(fd);
195 
196 	return EXIT_SUCCESS;
197 }
198 DEFINE_TEST(test_write_str_safe,
199 	.description = "check implementation of write_str_safe()",
200 	.config = {
201 		[TC_ROOTFS] = TESTSUITE_ROOTFS "test-util2/",
202 	},
203 	.need_spawn = true,
204 	.output = {
205 		.files = (const struct keyval[]) {
206 			{ TEST_WRITE_STR_SAFE_PATH ".txt",
207 			  TEST_WRITE_STR_SAFE_PATH "-correct.txt" },
208 			{ }
209 		},
210 	});
211 
test_addu64_overflow(const struct test * t)212 static int test_addu64_overflow(const struct test *t)
213 {
214 	uint64_t res;
215 	bool overflow;
216 
217 	overflow = addu64_overflow(UINT64_MAX - 1, 1, &res);
218 	assert_return(!overflow, EXIT_FAILURE);
219 	assert_return(res == UINT64_MAX, EXIT_FAILURE);
220 
221 	overflow = addu64_overflow(UINT64_MAX, 1, &res);
222 	assert_return(overflow, EXIT_FAILURE);
223 
224 	return EXIT_SUCCESS;
225 }
226 DEFINE_TEST(test_addu64_overflow,
227 	.description = "check implementation of addu4_overflow()",
228 	.need_spawn = false,
229 	);
230 
231 
test_backoff_time(const struct test * t)232 static int test_backoff_time(const struct test *t)
233 {
234 	unsigned long long delta;
235 
236 	/* Check exponential increments */
237 	get_backoff_delta_msec(now_msec(), now_msec() + 10, &delta);
238 	assert_return(delta == 1, EXIT_FAILURE);
239 	get_backoff_delta_msec(now_msec(), now_msec() + 10, &delta);
240 	assert_return(delta == 2, EXIT_FAILURE);
241 	get_backoff_delta_msec(now_msec(), now_msec() + 10, &delta);
242 	assert_return(delta == 4, EXIT_FAILURE);
243 	get_backoff_delta_msec(now_msec(), now_msec() + 10, &delta);
244 	assert_return(delta == 8, EXIT_FAILURE);
245 
246 	{
247 		unsigned long long t0, tend;
248 
249 		/* Check tail */
250 		delta = 4;
251 		tend = now_msec() + 3;
252 		t0 = tend - 10;
253 		get_backoff_delta_msec(t0, tend, &delta);
254 		assert_return(delta == 2, EXIT_FAILURE);
255 		tend = now_msec() + 1;
256 		t0 = tend - 9;
257 		get_backoff_delta_msec(t0, tend, &delta);
258 		assert_return(delta == 1, EXIT_FAILURE);
259 		tend = now_msec();
260 		t0 = tend - 10;
261 		get_backoff_delta_msec(t0, tend, &delta);
262 		assert_return(delta == 0, EXIT_FAILURE);
263 	}
264 
265 	return EXIT_SUCCESS;
266 }
267 DEFINE_TEST(test_backoff_time,
268 	.description = "check implementation of get_backoff_delta_msec()",
269 	.need_spawn = false,
270 	);
271 
272 
273 TESTSUITE_MAIN();
274