xref: /spdk/test/unit/lib/util/string.c/string_ut.c (revision 2f5c602574a98ede645991abe279a96e19c50196)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright (c) Intel Corporation.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "spdk/stdinc.h"
35 
36 #include "spdk_cunit.h"
37 
38 #include "util/string.c"
39 
40 static void
41 test_parse_ip_addr(void)
42 {
43 	int rc;
44 	char *host;
45 	char *port;
46 	char ip[255];
47 
48 	/* IPv4 */
49 	snprintf(ip, 255, "%s", "192.168.0.1");
50 	rc = spdk_parse_ip_addr(ip, &host, &port);
51 	CU_ASSERT_EQUAL(rc, 0);
52 	SPDK_CU_ASSERT_FATAL(host != NULL);
53 	CU_ASSERT(strcmp(host, "192.168.0.1") == 0);
54 	CU_ASSERT_EQUAL(strlen(host), 11);
55 	CU_ASSERT_EQUAL(port, NULL);
56 
57 	/* IPv4 with port */
58 	snprintf(ip, 255, "%s", "123.456.789.0:5520");
59 	rc = spdk_parse_ip_addr(ip, &host, &port);
60 	CU_ASSERT_EQUAL(rc, 0);
61 	SPDK_CU_ASSERT_FATAL(host != NULL);
62 	CU_ASSERT(strcmp(host, "123.456.789.0") == 0);
63 	CU_ASSERT_EQUAL(strlen(host), 13);
64 	SPDK_CU_ASSERT_FATAL(port != NULL);
65 	CU_ASSERT(strcmp(port, "5520") == 0);
66 	CU_ASSERT_EQUAL(strlen(port), 4);
67 
68 	/* IPv6 */
69 	snprintf(ip, 255, "%s", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]");
70 	rc = spdk_parse_ip_addr(ip, &host, &port);
71 	CU_ASSERT_EQUAL(rc, 0);
72 	SPDK_CU_ASSERT_FATAL(host != NULL);
73 	CU_ASSERT(strcmp(host, "2001:db8:85a3:8d3:1319:8a2e:370:7348") == 0);
74 	CU_ASSERT_EQUAL(strlen(host), 36);
75 	CU_ASSERT_EQUAL(port, NULL);
76 
77 	/* IPv6 with port */
78 	snprintf(ip, 255, "%s", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443");
79 	rc = spdk_parse_ip_addr(ip, &host, &port);
80 	CU_ASSERT_EQUAL(rc, 0);
81 	SPDK_CU_ASSERT_FATAL(host != NULL);
82 	CU_ASSERT(strcmp(host, "2001:db8:85a3:8d3:1319:8a2e:370:7348") == 0);
83 	CU_ASSERT_EQUAL(strlen(host), 36);
84 	SPDK_CU_ASSERT_FATAL(port != NULL);
85 	CU_ASSERT(strcmp(port, "443") == 0);
86 	CU_ASSERT_EQUAL(strlen(port), 3);
87 
88 	/* IPv6 dangling colon */
89 	snprintf(ip, 255, "%s", "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:");
90 	rc = spdk_parse_ip_addr(ip, &host, &port);
91 	CU_ASSERT_EQUAL(rc, 0);
92 	SPDK_CU_ASSERT_FATAL(host != NULL);
93 	CU_ASSERT(strcmp(host, "2001:db8:85a3:8d3:1319:8a2e:370:7348") == 0);
94 	CU_ASSERT_EQUAL(strlen(host), 36);
95 	CU_ASSERT_EQUAL(port, NULL);
96 }
97 
98 static void
99 test_str_chomp(void)
100 {
101 	char s[1024];
102 
103 	/* One \n newline */
104 	snprintf(s, sizeof(s), "%s", "hello world\n");
105 	CU_ASSERT(spdk_str_chomp(s) == 1);
106 	CU_ASSERT(strcmp(s, "hello world") == 0);
107 
108 	/* One \r\n newline */
109 	snprintf(s, sizeof(s), "%s", "hello world\r\n");
110 	CU_ASSERT(spdk_str_chomp(s) == 2);
111 	CU_ASSERT(strcmp(s, "hello world") == 0);
112 
113 	/* No newlines */
114 	snprintf(s, sizeof(s), "%s", "hello world");
115 	CU_ASSERT(spdk_str_chomp(s) == 0);
116 	CU_ASSERT(strcmp(s, "hello world") == 0);
117 
118 	/* Two newlines */
119 	snprintf(s, sizeof(s), "%s", "hello world\n\n");
120 	CU_ASSERT(spdk_str_chomp(s) == 2);
121 	CU_ASSERT(strcmp(s, "hello world") == 0);
122 
123 	/* Empty string */
124 	snprintf(s, sizeof(s), "%s", "");
125 	CU_ASSERT(spdk_str_chomp(s) == 0);
126 	CU_ASSERT(strcmp(s, "") == 0);
127 
128 	/* One-character string with only \n */
129 	snprintf(s, sizeof(s), "%s", "\n");
130 	CU_ASSERT(spdk_str_chomp(s) == 1);
131 	CU_ASSERT(strcmp(s, "") == 0);
132 
133 	/* One-character string without a newline */
134 	snprintf(s, sizeof(s), "%s", "a");
135 	CU_ASSERT(spdk_str_chomp(s) == 0);
136 	CU_ASSERT(strcmp(s, "a") == 0);
137 }
138 
139 static void
140 test_parse_capacity(void)
141 {
142 	char str[128];
143 	uint64_t cap;
144 	int rc;
145 	bool has_prefix = true;
146 
147 	rc = spdk_parse_capacity("472", &cap, &has_prefix);
148 	CU_ASSERT(rc == 0);
149 	CU_ASSERT(cap == 472);
150 	CU_ASSERT(has_prefix == false);
151 
152 	snprintf(str, sizeof(str), "%"PRIu64, UINT64_MAX);
153 	rc = spdk_parse_capacity(str, &cap, &has_prefix);
154 	CU_ASSERT(rc == 0);
155 	CU_ASSERT(cap == UINT64_MAX);
156 	CU_ASSERT(has_prefix == false);
157 
158 	rc = spdk_parse_capacity("12k", &cap, &has_prefix);
159 	CU_ASSERT(rc == 0);
160 	CU_ASSERT(cap == 12 * 1024);
161 	CU_ASSERT(has_prefix == true);
162 
163 	rc = spdk_parse_capacity("12K", &cap, &has_prefix);
164 	CU_ASSERT(rc == 0);
165 	CU_ASSERT(cap == 12 * 1024);
166 	CU_ASSERT(has_prefix == true);
167 
168 	rc = spdk_parse_capacity("12KB", &cap, &has_prefix);
169 	CU_ASSERT(rc == 0);
170 	CU_ASSERT(cap == 12 * 1024);
171 	CU_ASSERT(has_prefix == true);
172 
173 	rc = spdk_parse_capacity("100M", &cap, &has_prefix);
174 	CU_ASSERT(rc == 0);
175 	CU_ASSERT(cap == 100 * 1024 * 1024);
176 	CU_ASSERT(has_prefix == true);
177 
178 	rc = spdk_parse_capacity("128M", &cap, &has_prefix);
179 	CU_ASSERT(rc == 0);
180 	CU_ASSERT(cap == 128 * 1024 * 1024);
181 	CU_ASSERT(has_prefix == true);
182 
183 	rc = spdk_parse_capacity("4G", &cap, &has_prefix);
184 	CU_ASSERT(rc == 0);
185 	CU_ASSERT(cap == 4ULL * 1024 * 1024 * 1024);
186 	CU_ASSERT(has_prefix == true);
187 
188 	rc = spdk_parse_capacity("100M 512k", &cap, &has_prefix);
189 	CU_ASSERT(rc == 0);
190 	CU_ASSERT(cap == 100ULL * 1024 * 1024);
191 
192 	rc = spdk_parse_capacity("12k8K", &cap, &has_prefix);
193 	CU_ASSERT(rc == 0);
194 	CU_ASSERT(cap == 12 * 1024);
195 	CU_ASSERT(has_prefix == true);
196 
197 	/* Non-number */
198 	rc = spdk_parse_capacity("G", &cap, &has_prefix);
199 	CU_ASSERT(rc != 0);
200 
201 	rc = spdk_parse_capacity("darsto", &cap, &has_prefix);
202 	CU_ASSERT(rc != 0);
203 }
204 
205 static void
206 test_sprintf_append_realloc(void)
207 {
208 	char *str1, *str2, *str3, *str4;
209 
210 	/* Test basic functionality. */
211 	str1 = spdk_sprintf_alloc("hello world\ngood morning\n" \
212 				  "good afternoon\ngood evening\n");
213 	SPDK_CU_ASSERT_FATAL(str1 != NULL);
214 
215 	str2 = spdk_sprintf_append_realloc(NULL, "hello world\n");
216 	SPDK_CU_ASSERT_FATAL(str2);
217 
218 	str2 = spdk_sprintf_append_realloc(str2, "good morning\n");
219 	SPDK_CU_ASSERT_FATAL(str2);
220 
221 	str2 = spdk_sprintf_append_realloc(str2, "good afternoon\n");
222 	SPDK_CU_ASSERT_FATAL(str2);
223 
224 	str2 = spdk_sprintf_append_realloc(str2, "good evening\n");
225 	SPDK_CU_ASSERT_FATAL(str2);
226 
227 	CU_ASSERT(strcmp(str1, str2) == 0);
228 
229 	free(str1);
230 	free(str2);
231 
232 	/* Test doubling buffer size. */
233 	str3 = spdk_sprintf_append_realloc(NULL, "aaaaaaaaaa\n");
234 	str3 = spdk_sprintf_append_realloc(str3, "bbbbbbbbbb\n");
235 	str3 = spdk_sprintf_append_realloc(str3, "cccccccccc\n");
236 
237 	str4 = malloc(33 + 1);
238 	memset(&str4[0], 'a', 10);
239 	str4[10] = '\n';
240 	memset(&str4[11], 'b', 10);
241 	str4[21] = '\n';
242 	memset(&str4[22], 'c', 10);
243 	str4[32] = '\n';
244 	str4[33] = 0;
245 
246 	CU_ASSERT(strcmp(str3, str4) == 0);
247 
248 	free(str3);
249 	free(str4);
250 }
251 
252 static void
253 generate_string(char *str, size_t len, int64_t limit, int adjust)
254 {
255 	/* There isn't a portable way of handling the arithmetic, so */
256 	/* perform the calculation in two parts to avoid overflow */
257 	int64_t hi = (limit / 10) + (adjust / 10);
258 	int64_t lo = (limit % 10) + (adjust % 10);
259 
260 	/* limit is large and adjust is small, so hi part will be */
261 	/* non-zero even if there is a carry, but check it */
262 	CU_ASSERT(hi < -1 || hi > 1);
263 
264 	/* Correct a difference in sign */
265 	if ((hi < 0) != (lo < 0) && lo != 0) {
266 		lo += (hi < 0) ? -10 : 10;
267 		hi += (hi < 0) ? 1 : -1;
268 	}
269 
270 	snprintf(str, len, "%" PRId64 "%01" PRId64, hi + (lo / 10),
271 		 (lo < 0) ? (-lo % 10) : (lo % 10));
272 }
273 
274 static void
275 test_strtol(void)
276 {
277 	long int val;
278 	char str[256];
279 
280 	const char *val1 = "no_digits";
281 	/* digits + chars */
282 	const char *val8 = "10_is_ten";
283 	/* chars + digits */
284 	const char *val9 = "ten_is_10";
285 	/* all zeroes */
286 	const char *val10 = "00000000";
287 	/* leading minus sign, but not negative */
288 	const char *val11 = "-0";
289 
290 	val = spdk_strtol(val1, 10);
291 	CU_ASSERT(val == -EINVAL);
292 
293 	/* LONG_MIN - 1 */
294 	generate_string(str, sizeof(str), LONG_MIN, -1);
295 	val = spdk_strtol(str, 10);
296 	CU_ASSERT(val == -ERANGE);
297 
298 	/* LONG_MIN */
299 	generate_string(str, sizeof(str), LONG_MIN, 0);
300 	val = spdk_strtol(str, 10);
301 	CU_ASSERT(val == -ERANGE);
302 
303 	/* LONG_MIN + 1 */
304 	generate_string(str, sizeof(str), LONG_MIN, +1);
305 	val = spdk_strtol(str, 10);
306 	CU_ASSERT(val == -ERANGE);
307 
308 	/* LONG_MAX - 1 */
309 	generate_string(str, sizeof(str), LONG_MAX, -1);
310 	val = spdk_strtol(str, 10);
311 	CU_ASSERT(val == LONG_MAX - 1);
312 
313 	/* LONG_MAX */
314 	generate_string(str, sizeof(str), LONG_MAX, 0);
315 	val = spdk_strtol(str, 10);
316 	CU_ASSERT(val == LONG_MAX);
317 
318 	/* LONG_MAX + 1 */
319 	generate_string(str, sizeof(str), LONG_MAX, +1);
320 	val = spdk_strtol(str, 10);
321 	CU_ASSERT(val == -ERANGE);
322 
323 	val = spdk_strtol(val8, 10);
324 	CU_ASSERT(val == -EINVAL);
325 
326 	val = spdk_strtol(val9, 10);
327 	CU_ASSERT(val == -EINVAL);
328 
329 	val = spdk_strtol(val10, 10);
330 	CU_ASSERT(val == 0);
331 
332 	/* Invalid base */
333 	val = spdk_strtol(val10, 1);
334 	CU_ASSERT(val == -EINVAL);
335 
336 	val = spdk_strtol(val11, 10);
337 	CU_ASSERT(val == 0);
338 }
339 
340 static void
341 test_strtoll(void)
342 {
343 	long long int val;
344 	char str[256];
345 
346 	const char *val1 = "no_digits";
347 	/* digits + chars */
348 	const char *val8 = "10_is_ten";
349 	/* chars + digits */
350 	const char *val9 = "ten_is_10";
351 	/* all zeroes */
352 	const char *val10 = "00000000";
353 	/* leading minus sign, but not negative */
354 	const char *val11 = "-0";
355 
356 	val = spdk_strtoll(val1, 10);
357 	CU_ASSERT(val == -EINVAL);
358 
359 	/* LLONG_MIN - 1 */
360 	generate_string(str, sizeof(str), LLONG_MIN, -1);
361 	val = spdk_strtoll(str, 10);
362 	CU_ASSERT(val == -ERANGE);
363 
364 	/* LLONG_MIN */
365 	generate_string(str, sizeof(str), LLONG_MIN, 0);
366 	val = spdk_strtoll(str, 10);
367 	CU_ASSERT(val == -ERANGE);
368 
369 	/* LLONG_MIN + 1 */
370 	generate_string(str, sizeof(str), LLONG_MIN, +1);
371 	val = spdk_strtoll(str, 10);
372 	CU_ASSERT(val == -ERANGE);
373 
374 	/* LLONG_MAX - 1 */
375 	generate_string(str, sizeof(str), LLONG_MAX, -1);
376 	val = spdk_strtoll(str, 10);
377 	CU_ASSERT(val == LLONG_MAX - 1);
378 
379 	/* LLONG_MAX */
380 	generate_string(str, sizeof(str), LLONG_MAX, 0);
381 	val = spdk_strtoll(str, 10);
382 	CU_ASSERT(val == LLONG_MAX);
383 
384 	/* LLONG_MAX + 1 */
385 	generate_string(str, sizeof(str), LLONG_MAX, +1);
386 	val = spdk_strtoll(str, 10);
387 	CU_ASSERT(val == -ERANGE);
388 
389 	val = spdk_strtoll(val8, 10);
390 	CU_ASSERT(val == -EINVAL);
391 
392 	val = spdk_strtoll(val9, 10);
393 	CU_ASSERT(val == -EINVAL);
394 
395 	val = spdk_strtoll(val10, 10);
396 	CU_ASSERT(val == 0);
397 
398 	/* Invalid base */
399 	val = spdk_strtoll(val10, 1);
400 	CU_ASSERT(val == -EINVAL);
401 
402 	val = spdk_strtoll(val11, 10);
403 	CU_ASSERT(val == 0);
404 }
405 
406 int
407 main(int argc, char **argv)
408 {
409 	CU_pSuite	suite = NULL;
410 	unsigned int	num_failures;
411 
412 	CU_set_error_action(CUEA_ABORT);
413 	CU_initialize_registry();
414 
415 	suite = CU_add_suite("string", NULL, NULL);
416 
417 	CU_ADD_TEST(suite, test_parse_ip_addr);
418 	CU_ADD_TEST(suite, test_str_chomp);
419 	CU_ADD_TEST(suite, test_parse_capacity);
420 	CU_ADD_TEST(suite, test_sprintf_append_realloc);
421 	CU_ADD_TEST(suite, test_strtol);
422 	CU_ADD_TEST(suite, test_strtoll);
423 
424 	CU_basic_set_mode(CU_BRM_VERBOSE);
425 
426 	CU_basic_run_tests();
427 
428 	num_failures = CU_get_number_of_failures();
429 	CU_cleanup_registry();
430 
431 	return num_failures;
432 }
433