1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2020 Google LLC
4  */
5 
6 #include <vsprintf.h>
7 #include <test/lib.h>
8 #include <test/test.h>
9 #include <test/ut.h>
10 
11 /* This is large enough for any of the test strings */
12 #define TEST_STR_SIZE	200
13 
14 static const char str1[] = "I'm sorry I'm late.";
15 static const char str2[] = "1099abNo, don't bother apologising.";
16 static const char str3[] = "0xbI'm sorry you're alive.";
17 static const char str4[] = "1234567890123 I lost closer friends";
18 static const char str5[] = "0x9876543210the last time I was deloused";
19 static const char str6[] = "0778octal is seldom used";
20 static const char str7[] = "707it is a piece of computing history";
21 static const char str8[] = "0x887e2561352d80fa";
22 static const char str9[] = "614FF7EAA63009DA";
23 
str_upper(struct unit_test_state * uts)24 static int str_upper(struct unit_test_state *uts)
25 {
26 	char out[TEST_STR_SIZE];
27 
28 	/* Make sure it adds a terminator */
29 	out[strlen(str1)] = 'a';
30 	str_to_upper(str1, out, SIZE_MAX);
31 	ut_asserteq_str("I'M SORRY I'M LATE.", out);
32 
33 	/* In-place operation */
34 	strcpy(out, str2);
35 	str_to_upper(out, out, SIZE_MAX);
36 	ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out);
37 
38 	/* Limited length */
39 	str_to_upper(str1, out, 7);
40 	ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out);
41 
42 	/* In-place with limited length */
43 	strcpy(out, str2);
44 	str_to_upper(out, out, 7);
45 	ut_asserteq_str("1099ABNo, don't bother apologising.", out);
46 
47 	/* Copy an empty string to a buffer with space*/
48 	out[1] = 0x7f;
49 	str_to_upper("", out, SIZE_MAX);
50 	ut_asserteq('\0', *out);
51 	ut_asserteq(0x7f, out[1]);
52 
53 	/* Copy an empty string to a buffer with no space*/
54 	out[0] = 0x7f;
55 	str_to_upper("", out, 0);
56 	ut_asserteq(0x7f, out[0]);
57 
58 	return 0;
59 }
60 LIB_TEST(str_upper, 0);
61 
run_strtoul(struct unit_test_state * uts,const char * str,int base,ulong expect_val,int expect_endp_offset,bool upper)62 static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
63 		       ulong expect_val, int expect_endp_offset, bool upper)
64 {
65 	char out[TEST_STR_SIZE];
66 	char *endp;
67 	ulong val;
68 
69 	strcpy(out, str);
70 	if (upper)
71 		str_to_upper(out, out, -1);
72 
73 	val = simple_strtoul(out, &endp, base);
74 	ut_asserteq(expect_val, val);
75 	ut_asserteq(expect_endp_offset, endp - out);
76 
77 	return 0;
78 }
79 
str_simple_strtoul(struct unit_test_state * uts)80 static int str_simple_strtoul(struct unit_test_state *uts)
81 {
82 	int upper;
83 
84 	/* Check that it is case-insentive */
85 	for (upper = 0; upper < 2; upper++) {
86 		/* Base 10 and base 16 */
87 		ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper));
88 		ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper));
89 		ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper));
90 		ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper));
91 
92 		/* Octal */
93 		ut_assertok(run_strtoul(uts, str6, 0, 63, 3, upper));
94 		ut_assertok(run_strtoul(uts, str7, 8, 0x1c7, 3, upper));
95 
96 		/* Invalid string */
97 		ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
98 
99 		/* Base 0 */
100 		ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
101 		ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
102 		ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
103 
104 		/* Base 2 */
105 		ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
106 		ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
107 	}
108 
109 	/* Check endp being NULL */
110 	ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
111 
112 	return 0;
113 }
114 LIB_TEST(str_simple_strtoul, 0);
115 
run_strtoull(struct unit_test_state * uts,const char * str,int base,unsigned long long expect_val,int expect_endp_offset,bool upper)116 static int run_strtoull(struct unit_test_state *uts, const char *str, int base,
117 			unsigned long long expect_val, int expect_endp_offset,
118 			bool upper)
119 {
120 	char out[TEST_STR_SIZE];
121 	char *endp;
122 	unsigned long long val;
123 
124 	strcpy(out, str);
125 	if (upper)
126 		str_to_upper(out, out, -1);
127 
128 	val = simple_strtoull(out, &endp, base);
129 	ut_asserteq(expect_val, val);
130 	ut_asserteq(expect_endp_offset, endp - out);
131 
132 	return 0;
133 }
134 
str_simple_strtoull(struct unit_test_state * uts)135 static int str_simple_strtoull(struct unit_test_state *uts)
136 {
137 	int upper;
138 
139 	/* Check that it is case-insentive */
140 	for (upper = 0; upper < 2; upper++) {
141 		/* Base 10 and base 16 */
142 		ut_assertok(run_strtoull(uts, str2, 10, 1099, 4, upper));
143 		ut_assertok(run_strtoull(uts, str2, 16, 0x1099ab, 6, upper));
144 		ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper));
145 		ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper));
146 
147 		/* Octal */
148 		ut_assertok(run_strtoull(uts, str6, 0, 63, 3, upper));
149 		ut_assertok(run_strtoull(uts, str7, 8, 0x1c7, 3, upper));
150 
151 		/* Large values */
152 		ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13,
153 					 upper));
154 		ut_assertok(run_strtoull(uts, str4, 16, 0x1234567890123, 13,
155 					 upper));
156 		ut_assertok(run_strtoull(uts, str5, 0, 0x9876543210, 12,
157 					 upper));
158 
159 		/* Invalid string */
160 		ut_assertok(run_strtoull(uts, str1, 10, 0, 0, upper));
161 
162 		/* Base 0 */
163 		ut_assertok(run_strtoull(uts, str1, 0, 0, 0, upper));
164 		ut_assertok(run_strtoull(uts, str2, 0, 1099, 4, upper));
165 		ut_assertok(run_strtoull(uts, str3, 0, 0xb, 3, upper));
166 
167 		/* Base 2 */
168 		ut_assertok(run_strtoull(uts, str1, 2, 0, 0, upper));
169 		ut_assertok(run_strtoull(uts, str2, 2, 2, 2, upper));
170 	}
171 
172 	/* Check endp being NULL */
173 	ut_asserteq(1099, simple_strtoull(str2, NULL, 0));
174 
175 	return 0;
176 }
177 LIB_TEST(str_simple_strtoull, 0);
178 
str_hextoul(struct unit_test_state * uts)179 static int str_hextoul(struct unit_test_state *uts)
180 {
181 	char *endp;
182 
183 	/* Just a simple test, since we know this uses simple_strtoul() */
184 	ut_asserteq(0x1099ab, hextoul(str2, &endp));
185 	ut_asserteq(6, endp - str2);
186 
187 	return 0;
188 }
189 LIB_TEST(str_hextoul, 0);
190 
str_hextoull(struct unit_test_state * uts)191 static int str_hextoull(struct unit_test_state *uts)
192 {
193 	char *endp;
194 
195 	/* Just a simple test, since we know this uses simple_strtoull() */
196 	ut_asserteq_64(0x887e2561352d80faULL, hextoull(str8, &endp));
197 	ut_asserteq_64(0x12, endp - str8);
198 	ut_asserteq_64(0x614ff7eaa63009daULL, hextoull(str9, &endp));
199 	ut_asserteq_64(0x10, endp - str9);
200 	ut_asserteq_64(0x887e2561352d80faULL, hextoull(str8, NULL));
201 	ut_asserteq_64(0x614ff7eaa63009daULL, hextoull(str9, NULL));
202 
203 	return 0;
204 }
205 LIB_TEST(str_hextoull, 0);
206 
str_dectoul(struct unit_test_state * uts)207 static int str_dectoul(struct unit_test_state *uts)
208 {
209 	char *endp;
210 
211 	/* Just a simple test, since we know this uses simple_strtoul() */
212 	ut_asserteq(1099, dectoul(str2, &endp));
213 	ut_asserteq(4, endp - str2);
214 
215 	return 0;
216 }
217 LIB_TEST(str_dectoul, 0);
218 
str_itoa(struct unit_test_state * uts)219 static int str_itoa(struct unit_test_state *uts)
220 {
221 	ut_asserteq_str("123", simple_itoa(123));
222 	ut_asserteq_str("0", simple_itoa(0));
223 	ut_asserteq_str("2147483647", simple_itoa(0x7fffffff));
224 	ut_asserteq_str("4294967295", simple_itoa(0xffffffff));
225 
226 	/* Use #ifdef here to avoid a compiler warning on 32-bit machines */
227 #ifdef CONFIG_64BIT
228 	if (sizeof(ulong) == 8) {
229 		ut_asserteq_str("9223372036854775807",
230 				simple_itoa((1UL << 63) - 1));
231 		ut_asserteq_str("18446744073709551615", simple_itoa(-1));
232 	}
233 #endif /* CONFIG_64BIT */
234 
235 	return 0;
236 }
237 LIB_TEST(str_itoa, 0);
238 
str_xtoa(struct unit_test_state * uts)239 static int str_xtoa(struct unit_test_state *uts)
240 {
241 	ut_asserteq_str("7f", simple_xtoa(127));
242 	ut_asserteq_str("00", simple_xtoa(0));
243 	ut_asserteq_str("7fffffff", simple_xtoa(0x7fffffff));
244 	ut_asserteq_str("ffffffff", simple_xtoa(0xffffffff));
245 
246 	/* Use #ifdef here to avoid a compiler warning on 32-bit machines */
247 #ifdef CONFIG_64BIT
248 	if (sizeof(ulong) == 8) {
249 		ut_asserteq_str("7fffffffffffffff",
250 				simple_xtoa((1UL << 63) - 1));
251 		ut_asserteq_str("ffffffffffffffff", simple_xtoa(-1));
252 	}
253 #endif /* CONFIG_64BIT */
254 
255 	return 0;
256 }
257 LIB_TEST(str_xtoa, 0);
258 
str_trailing(struct unit_test_state * uts)259 static int str_trailing(struct unit_test_state *uts)
260 {
261 	const char str1[] = "abc123def";
262 	const char str2[] = "abc123def456";
263 	const char *end;
264 
265 	ut_asserteq(-1, trailing_strtol(""));
266 	ut_asserteq(-1, trailing_strtol("123"));
267 	ut_asserteq(123, trailing_strtol("abc123"));
268 	ut_asserteq(4, trailing_strtol("12c4"));
269 	ut_asserteq(-1, trailing_strtol("abd"));
270 	ut_asserteq(-1, trailing_strtol("abc123def"));
271 
272 	ut_asserteq(-1, trailing_strtoln(str1, NULL));
273 	ut_asserteq(123, trailing_strtoln(str1, str1 + 6));
274 	ut_asserteq(-1, trailing_strtoln(str1, str1 + 9));
275 
276 	ut_asserteq(3, trailing_strtol("a3"));
277 
278 	ut_asserteq(123, trailing_strtoln_end(str1, str1 + 6, &end));
279 	ut_asserteq(3, end - str1);
280 
281 	ut_asserteq(-1, trailing_strtoln_end(str1, str1 + 7, &end));
282 	ut_asserteq(7, end - str1);
283 
284 	ut_asserteq(456, trailing_strtoln_end(str2, NULL, &end));
285 	ut_asserteq(9, end - str2);
286 
287 	return 0;
288 }
289 LIB_TEST(str_trailing, 0);
290 
test_str_to_list(struct unit_test_state * uts)291 static int test_str_to_list(struct unit_test_state *uts)
292 {
293 	const char **ptr;
294 	ulong start;
295 
296 	/* check out of memory */
297 	start = ut_check_delta(0);
298 	malloc_enable_testing(0);
299 	ut_assertnull(str_to_list(""));
300 	ut_assertok(ut_check_delta(start));
301 
302 	ut_assertnull(str_to_list("this is a test"));
303 	ut_assertok(ut_check_delta(start));
304 
305 	malloc_enable_testing(1);
306 	ut_assertnull(str_to_list("this is a test"));
307 	ut_assertok(ut_check_delta(start));
308 
309 	/* for an empty string, only one nalloc is needed */
310 	malloc_enable_testing(1);
311 	ptr = str_to_list("");
312 	ut_assertnonnull(ptr);
313 	ut_assertnull(ptr[0]);
314 	str_free_list(ptr);
315 	ut_assertok(ut_check_delta(start));
316 
317 	malloc_disable_testing();
318 
319 	/* test the same again, without any nalloc restrictions */
320 	ptr = str_to_list("");
321 	ut_assertnonnull(ptr);
322 	ut_assertnull(ptr[0]);
323 	str_free_list(ptr);
324 	ut_assertok(ut_check_delta(start));
325 
326 	/* test a single string */
327 	start = ut_check_delta(0);
328 	ptr = str_to_list("hi");
329 	ut_assertnonnull(ptr);
330 	ut_assertnonnull(ptr[0]);
331 	ut_asserteq_str("hi", ptr[0]);
332 	ut_assertnull(ptr[1]);
333 	str_free_list(ptr);
334 	ut_assertok(ut_check_delta(start));
335 
336 	/* test two strings */
337 	ptr = str_to_list("hi there");
338 	ut_assertnonnull(ptr);
339 	ut_assertnonnull(ptr[0]);
340 	ut_asserteq_str("hi", ptr[0]);
341 	ut_assertnonnull(ptr[1]);
342 	ut_asserteq_str("there", ptr[1]);
343 	ut_assertnull(ptr[2]);
344 	str_free_list(ptr);
345 	ut_assertok(ut_check_delta(start));
346 
347 	/* test leading, trailing and multiple spaces */
348 	ptr = str_to_list(" more  space  ");
349 	ut_assertnonnull(ptr);
350 	ut_assertnonnull(ptr[0]);
351 	ut_asserteq_str("", ptr[0]);
352 	ut_assertnonnull(ptr[1]);
353 	ut_asserteq_str("more", ptr[1]);
354 	ut_assertnonnull(ptr[2]);
355 	ut_asserteq_str("", ptr[2]);
356 	ut_assertnonnull(ptr[3]);
357 	ut_asserteq_str("space", ptr[3]);
358 	ut_assertnonnull(ptr[4]);
359 	ut_asserteq_str("", ptr[4]);
360 	ut_assertnull(ptr[5]);
361 	str_free_list(ptr);
362 	ut_assertok(ut_check_delta(start));
363 
364 	/* test freeing a NULL pointer */
365 	str_free_list(NULL);
366 
367 	return 0;
368 }
369 LIB_TEST(test_str_to_list, 0);
370