1 /*
2  * Copyright (c) 2018-2020 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "qdf_mem.h"
21 #include "qdf_trace.h"
22 #include "qdf_types.h"
23 #include "qdf_types_test.h"
24 
25 #define WHITESPACE "\t\n\r \x20"
26 
27 #define ut_bool_pass(str, exp) __ut_bool(str, QDF_STATUS_SUCCESS, exp)
28 #define ut_bool_fail(str) __ut_bool(str, QDF_STATUS_E_FAILURE, false)
29 
30 static uint32_t
__ut_bool(const char * str,QDF_STATUS exp_status,bool exp_value)31 __ut_bool(const char *str, QDF_STATUS exp_status, bool exp_value)
32 {
33 	bool value;
34 	QDF_STATUS status = qdf_bool_parse(str, &value);
35 
36 	if (status != exp_status) {
37 		qdf_nofl_alert("FAIL: qdf_bool_parse(\"%s\") -> status %d; expected status %d",
38 			       str, status, exp_status);
39 		return 1;
40 	}
41 
42 	if (QDF_IS_STATUS_ERROR(status))
43 		return 0;
44 
45 	if (value != exp_value) {
46 		qdf_nofl_alert("FAIL: qdf_bool_parse(\"%s\") -> %s; expected %s",
47 			       str, value ? "true" : "false",
48 			       exp_value ? "true" : "false");
49 		return 1;
50 	}
51 
52 	return 0;
53 }
54 
qdf_types_ut_bool_parse(void)55 static uint32_t qdf_types_ut_bool_parse(void)
56 {
57 	uint32_t errors = 0;
58 
59 	errors += ut_bool_pass("1", true);
60 	errors += ut_bool_pass("y", true);
61 	errors += ut_bool_pass("Y", true);
62 	errors += ut_bool_pass("0", false);
63 	errors += ut_bool_pass("n", false);
64 	errors += ut_bool_pass("N", false);
65 	errors += ut_bool_pass(WHITESPACE "1" WHITESPACE, true);
66 
67 	errors += ut_bool_fail("true");
68 	errors += ut_bool_fail("false");
69 	errors += ut_bool_fail("日本");
70 
71 	return errors;
72 }
73 
74 #define ut_int32_pass(str, exp) __ut_int32(str, QDF_STATUS_SUCCESS, exp)
75 #define ut_int32_fail(str, exp_status) __ut_int32(str, exp_status, 0)
76 
77 static uint32_t
__ut_int32(const char * str,QDF_STATUS exp_status,int32_t exp_value)78 __ut_int32(const char *str, QDF_STATUS exp_status, int32_t exp_value)
79 {
80 	int32_t value;
81 	QDF_STATUS status = qdf_int32_parse(str, &value);
82 
83 	if (status != exp_status) {
84 		qdf_nofl_alert("FAIL: qdf_int32_parse(\"%s\") -> status %d; expected status %d",
85 			       str, status, exp_status);
86 		return 1;
87 	}
88 
89 	if (QDF_IS_STATUS_ERROR(status))
90 		return 0;
91 
92 	if (value != exp_value) {
93 		qdf_nofl_alert("FAIL: qdf_int32_parse(\"%s\") -> %d; expected %d",
94 			       str, value, exp_value);
95 		return 1;
96 	}
97 
98 	return 0;
99 }
100 
qdf_types_ut_int32_parse(void)101 static uint32_t qdf_types_ut_int32_parse(void)
102 {
103 	uint32_t errors = 0;
104 
105 	errors += ut_int32_pass("1", 1);
106 	errors += ut_int32_pass("+1", 1);
107 	errors += ut_int32_pass("-1", -1);
108 	errors += ut_int32_pass(WHITESPACE "1" WHITESPACE, 1);
109 	errors += ut_int32_fail("1;", QDF_STATUS_E_FAILURE);
110 	errors += ut_int32_pass(" 2147483647", 2147483647);
111 	errors += ut_int32_fail(" 2147483648", QDF_STATUS_E_RANGE);
112 	errors += ut_int32_pass("-2147483648", -2147483647 - 1);
113 	errors += ut_int32_fail("-2147483649", QDF_STATUS_E_RANGE);
114 	errors += ut_int32_fail("日本", QDF_STATUS_E_FAILURE);
115 
116 	return errors;
117 }
118 
119 #define ut_int64_pass(str, exp) __ut_int64(str, QDF_STATUS_SUCCESS, exp)
120 #define ut_int64_fail(str, exp_status) __ut_int64(str, exp_status, 0)
121 
122 static uint32_t
__ut_int64(const char * str,QDF_STATUS exp_status,int64_t exp_value)123 __ut_int64(const char *str, QDF_STATUS exp_status, int64_t exp_value)
124 {
125 	int64_t value;
126 	QDF_STATUS status = qdf_int64_parse(str, &value);
127 
128 	if (status != exp_status) {
129 		qdf_nofl_alert("FAIL: qdf_int64_parse(\"%s\") -> status %d; expected status %d",
130 			       str, status, exp_status);
131 		return 1;
132 	}
133 
134 	if (QDF_IS_STATUS_ERROR(status))
135 		return 0;
136 
137 	if (value != exp_value) {
138 		qdf_nofl_alert("FAIL: qdf_int64_parse(\"%s\") -> %lld; expected %lld",
139 			       str, value, exp_value);
140 		return 1;
141 	}
142 
143 	return 0;
144 }
145 
qdf_types_ut_int64_parse(void)146 static uint32_t qdf_types_ut_int64_parse(void)
147 {
148 	uint32_t errors = 0;
149 
150 	errors += ut_int64_pass("1", 1);
151 	errors += ut_int64_pass("+1", 1);
152 	errors += ut_int64_pass("-1", -1);
153 	errors += ut_int64_pass(WHITESPACE "1" WHITESPACE, 1);
154 	errors += ut_int64_fail("1;", QDF_STATUS_E_FAILURE);
155 	errors += ut_int64_pass(" 9223372036854775807", 9223372036854775807ll);
156 	errors += ut_int64_fail(" 9223372036854775808", QDF_STATUS_E_RANGE);
157 	errors += ut_int64_pass("-9223372036854775808",
158 				-9223372036854775807ll - 1);
159 	errors += ut_int64_fail("-9223372036854775809", QDF_STATUS_E_RANGE);
160 	errors += ut_int64_fail("日本", QDF_STATUS_E_FAILURE);
161 
162 	return errors;
163 }
164 
165 #define ut_uint16_array_pass(str, max_size, exp_arr, exp_arr_size) \
166 __ut_uint16_array(str, QDF_STATUS_SUCCESS, max_size, exp_arr, exp_arr_size)
167 
168 #define ut_uint16_array_fail(str, max_size, exp_status, exp_arr, exp_arr_size)\
169 __ut_uint16_array(str, exp_status, max_size, exp_arr, exp_arr_size)
170 
171 static uint32_t
__ut_uint16_array(const char * str,QDF_STATUS exp_status,uint8_t max_array_size,uint16_t * exp_array,uint8_t exp_array_size)172 __ut_uint16_array(const char *str, QDF_STATUS exp_status,
173 		  uint8_t max_array_size, uint16_t *exp_array,
174 		  uint8_t exp_array_size)
175 {
176 	uint16_t parsed_array[10];
177 	qdf_size_t parsed_array_size;
178 	QDF_STATUS status;
179 	uint8_t i;
180 
181 	status = qdf_uint16_array_parse(str, parsed_array, max_array_size,
182 					&parsed_array_size);
183 
184 	if (status != exp_status) {
185 		qdf_nofl_alert("FAIL: qdf_uint16_array_parse(\"%s\") -> status %d; expected status %d",
186 			       str, status, exp_status);
187 		return 1;
188 	}
189 
190 	if (QDF_IS_STATUS_ERROR(status))
191 		return 0;
192 
193 	if (parsed_array_size != exp_array_size) {
194 		qdf_nofl_alert("FAIL: qdf_uint16_array_parse(\"%s\") -> parsed_array_size %zu; exp_array_size %d",
195 			       str, parsed_array_size, exp_array_size);
196 		return 1;
197 	}
198 
199 	for (i = 0; i < exp_array_size; i++)
200 		if (parsed_array[i] != exp_array[i]) {
201 			qdf_nofl_alert("FAIL: qdf_uint16_array_parse(\"%s\") -> parsed_array[%d] %d; exp_array[%d] %d",
202 				       str, i, parsed_array[i], i,
203 				       exp_array[i]);
204 		return 1;
205 	}
206 
207 	return 0;
208 }
209 
qdf_types_ut_uint16_array_parse(void)210 static uint32_t qdf_types_ut_uint16_array_parse(void)
211 {
212 	uint32_t errors = 0;
213 	uint16_t exp_array_value[10] = {
214 			1, 10, 2412, 2417, 100, 65535, 0, 5486, 5180, 9999};
215 
216 	errors += ut_uint16_array_pass(
217 			"1, 10, 2412, 2417, 100, 65535, 0, 5486, 5180, 9999",
218 			10, exp_array_value, 10);
219 	errors += ut_uint16_array_pass(
220 		"+1, +10, +2412, +2417, +100, +65535, 0, +5486, +5180, +9999",
221 		10, exp_array_value, 10);
222 	errors += ut_uint16_array_fail("1;", 10, QDF_STATUS_E_FAILURE,
223 				       exp_array_value, 0);
224 	/* Out of range test where 65536 is out of range */
225 	errors += ut_uint16_array_fail(
226 			"1, 10, 2412, 2417, 100, 65536, 0, 5486, 5180, 9999",
227 			10, QDF_STATUS_E_RANGE, exp_array_value, 0);
228 	errors += ut_uint16_array_fail(
229 		"-1, -10, -2412, -2417, -100, -65535, 0, -5486, -5180, -9999",
230 		10, QDF_STATUS_E_RANGE, exp_array_value, 0);
231 	errors += ut_uint16_array_fail(
232 			"1, 10, 2412, 2417, 100, 日本, 0, 5486, 5180, 9999",
233 			10, QDF_STATUS_E_FAILURE, exp_array_value, 0);
234 
235 	return errors;
236 }
237 
238 #define ut_int32_array_pass(str, max_size, exp_arr, exp_arr_size) \
239 __ut_int32_array(str, QDF_STATUS_SUCCESS, max_size, exp_arr, exp_arr_size)
240 
241 #define ut_int32_array_fail(str, max_size, exp_status, exp_arr, exp_arr_size)\
242 __ut_int32_array(str, exp_status, max_size, exp_arr, exp_arr_size)
243 
244 static uint32_t
__ut_int32_array(const char * str,QDF_STATUS exp_status,uint8_t max_array_size,uint32_t * exp_array,uint8_t exp_array_size)245 __ut_int32_array(const char *str, QDF_STATUS exp_status,
246 		 uint8_t max_array_size, uint32_t *exp_array,
247 		 uint8_t exp_array_size)
248 {
249 	uint32_t parsed_array[10];
250 	qdf_size_t parsed_array_size;
251 	QDF_STATUS status;
252 	uint8_t i;
253 
254 	status = qdf_int32_array_parse(str, parsed_array, max_array_size,
255 				       &parsed_array_size);
256 
257 	if (status != exp_status) {
258 		qdf_nofl_alert("FAIL: qdf_int32_array_parse(\"%s\") -> status %d; expected status %d",
259 			       str, status, exp_status);
260 		return 1;
261 	}
262 
263 	if (QDF_IS_STATUS_ERROR(status))
264 		return 0;
265 
266 	if (parsed_array_size != exp_array_size) {
267 		qdf_nofl_alert("FAIL: qdf_int32_array_parse(\"%s\") -> parsed_array_size %zu; exp_array_size %d",
268 			       str, parsed_array_size, exp_array_size);
269 		return 1;
270 	}
271 
272 	for (i = 0; i < exp_array_size; i++)
273 		if (parsed_array[i] != exp_array[i]) {
274 			qdf_nofl_alert("FAIL: qdf_int32_array_parse(\"%s\") -> parsed_array[%d] %d; exp_array[%d] %d",
275 				       str, i, parsed_array[i], i,
276 				       exp_array[i]);
277 		return 1;
278 	}
279 
280 	return 0;
281 }
282 
283 #define ut_uint32_array_pass(str, max_size, exp_arr, exp_arr_size) \
284 __ut_uint32_array(str, QDF_STATUS_SUCCESS, max_size, exp_arr, exp_arr_size)
285 
286 #define ut_uint32_array_fail(str, max_size, exp_status, exp_arr, exp_arr_size)\
287 __ut_uint32_array(str, exp_status, max_size, exp_arr, exp_arr_size)
288 
289 static uint32_t
__ut_uint32_array(const char * str,QDF_STATUS exp_status,uint8_t max_array_size,uint32_t * exp_array,uint8_t exp_array_size)290 __ut_uint32_array(const char *str, QDF_STATUS exp_status,
291 		  uint8_t max_array_size, uint32_t *exp_array,
292 		  uint8_t exp_array_size)
293 {
294 	uint32_t parsed_array[10];
295 	qdf_size_t parsed_array_size;
296 	QDF_STATUS status;
297 	uint8_t i;
298 
299 	status = qdf_uint32_array_parse(str, parsed_array, max_array_size,
300 					&parsed_array_size);
301 
302 	if (status != exp_status) {
303 		qdf_nofl_alert("FAIL: qdf_uint32_array_parse(\"%s\") -> status %d; expected status %d",
304 			       str, status, exp_status);
305 		return 1;
306 	}
307 
308 	if (QDF_IS_STATUS_ERROR(status))
309 		return 0;
310 
311 	if (parsed_array_size != exp_array_size) {
312 		qdf_nofl_alert("FAIL: qdf_uint32_array_parse(\"%s\") -> parsed_array_size %zu; exp_array_size %d",
313 			       str, parsed_array_size, exp_array_size);
314 		return 1;
315 	}
316 
317 	for (i = 0; i < exp_array_size; i++)
318 		if (parsed_array[i] != exp_array[i]) {
319 			qdf_nofl_alert("FAIL: qdf_uint32_array_parse(\"%s\") -> parsed_array[%d] %d; exp_array[%d] %d",
320 				       str, i, parsed_array[i], i,
321 				       exp_array[i]);
322 		return 1;
323 	}
324 
325 	return 0;
326 }
327 
qdf_types_ut_int32_array_parse(void)328 static uint32_t qdf_types_ut_int32_array_parse(void)
329 {
330 	uint32_t errors = 0;
331 	uint32_t exp_array_value[10] = { 1, 100, 9997, 899965, 65536, 0,
332 					 2147483647U, -65536,
333 					 -899965, -9997};
334 
335 	errors += ut_int32_array_pass(
336 		  "1, 100, 9997, 899965, 65536, 0, 2147483647, -65536, -899965, -9997",
337 		  10, exp_array_value, 10);
338 	errors += ut_int32_array_pass(
339 		  "+1, +100, +9997, +899965, +65536, 0, +2147483647, -65536, -899965, -9997",
340 		  10, exp_array_value, 10);
341 	errors += ut_int32_array_fail("1;", 10, QDF_STATUS_E_FAILURE,
342 				       exp_array_value, 0);
343 	errors += ut_int32_array_fail(
344 			"1, 100, 9997, 899965, 65536, 日本, 2147483647, -65536, -899965, -9997",
345 			10, QDF_STATUS_E_FAILURE, exp_array_value, 0);
346 
347 	return errors;
348 }
349 
qdf_types_ut_uint32_array_parse(void)350 static uint32_t qdf_types_ut_uint32_array_parse(void)
351 {
352 	uint32_t errors = 0;
353 	uint32_t exp_array_value[10] = { 1, 100, 9997, 899965, 65536, 0,
354 					 4294967295U, 268435456U,
355 					 2164184149U, 999999999U};
356 
357 	errors += ut_uint32_array_pass(
358 		  "1, 100, 9997, 899965, 65536, 0, 4294967295, 268435456, 2164184149, 999999999",
359 		  10, exp_array_value, 10);
360 	errors += ut_uint32_array_pass(
361 		  "+1, +100, +9997, +899965, +65536, 0, +4294967295, +268435456, +2164184149, +999999999",
362 		  10, exp_array_value, 10);
363 	errors += ut_uint32_array_fail("1;", 10, QDF_STATUS_E_FAILURE,
364 				       exp_array_value, 0);
365 	/* Out of range test where 4294967296 is out of range */
366 	errors += ut_uint32_array_fail(
367 		  "1, 100, 9997, 899965, 65536, 0, 4294967296, 268435456, 2164184149, 999999999",
368 		  10, QDF_STATUS_E_RANGE, exp_array_value, 0);
369 	errors += ut_uint32_array_fail(
370 		  "-1, -100, -9997, -899965, -65536, 0, -4294967295, -268435456, -2164184149, -999999999",
371 		  10, QDF_STATUS_E_RANGE, exp_array_value, 0);
372 	errors += ut_uint32_array_fail(
373 			"1, 100, 9997, 899965, 65536, 日本, 0, 4294967295, 268435456, 999999999",
374 			10, QDF_STATUS_E_FAILURE, exp_array_value, 0);
375 
376 	return errors;
377 }
378 
379 #define ut_uint32_pass(str, exp) __ut_uint32(str, QDF_STATUS_SUCCESS, exp)
380 #define ut_uint32_fail(str, exp_status) __ut_uint32(str, exp_status, 0)
381 
382 static uint32_t
__ut_uint32(const char * str,QDF_STATUS exp_status,uint32_t exp_value)383 __ut_uint32(const char *str, QDF_STATUS exp_status, uint32_t exp_value)
384 {
385 	uint32_t value;
386 	QDF_STATUS status = qdf_uint32_parse(str, &value);
387 
388 	if (status != exp_status) {
389 		qdf_nofl_alert("FAIL: qdf_uint32_parse(\"%s\") -> status %d; expected status %d",
390 			       str, status, exp_status);
391 		return 1;
392 	}
393 
394 	if (QDF_IS_STATUS_ERROR(status))
395 		return 0;
396 
397 	if (value != exp_value) {
398 		qdf_nofl_alert("FAIL: qdf_uint32_parse(\"%s\") -> %d; expected %d",
399 			       str, value, exp_value);
400 		return 1;
401 	}
402 
403 	return 0;
404 }
405 
qdf_types_ut_uint32_parse(void)406 static uint32_t qdf_types_ut_uint32_parse(void)
407 {
408 	uint32_t errors = 0;
409 
410 	errors += ut_uint32_pass("1", 1);
411 	errors += ut_uint32_pass("+1", 1);
412 	errors += ut_uint32_pass(WHITESPACE "1" WHITESPACE, 1);
413 	errors += ut_uint32_fail("1;", QDF_STATUS_E_FAILURE);
414 	errors += ut_uint32_pass("4294967295", 4294967295U);
415 	errors += ut_uint32_fail("4294967296", QDF_STATUS_E_RANGE);
416 	errors += ut_uint32_pass(" 0", 0);
417 	errors += ut_uint32_fail("-1", QDF_STATUS_E_RANGE);
418 	errors += ut_uint32_fail("日本", QDF_STATUS_E_FAILURE);
419 
420 	return errors;
421 }
422 
423 #define ut_uint64_pass(str, exp) __ut_uint64(str, QDF_STATUS_SUCCESS, exp)
424 #define ut_uint64_fail(str, exp_status) __ut_uint64(str, exp_status, 0)
425 
426 static uint32_t
__ut_uint64(const char * str,QDF_STATUS exp_status,uint64_t exp_value)427 __ut_uint64(const char *str, QDF_STATUS exp_status, uint64_t exp_value)
428 {
429 	uint64_t value;
430 	QDF_STATUS status = qdf_uint64_parse(str, &value);
431 
432 	if (status != exp_status) {
433 		qdf_nofl_alert("FAIL: qdf_uint64_parse(\"%s\") -> status %d; expected status %d",
434 			       str, status, exp_status);
435 		return 1;
436 	}
437 
438 	if (QDF_IS_STATUS_ERROR(status))
439 		return 0;
440 
441 	if (value != exp_value) {
442 		qdf_nofl_alert("FAIL: qdf_uint64_parse(\"%s\") -> %llu; expected %llu",
443 			       str, value, exp_value);
444 		return 1;
445 	}
446 
447 	return 0;
448 }
449 
qdf_types_ut_uint64_parse(void)450 static uint32_t qdf_types_ut_uint64_parse(void)
451 {
452 	uint32_t errors = 0;
453 
454 	errors += ut_uint64_pass("1", 1);
455 	errors += ut_uint64_pass("+1", 1);
456 	errors += ut_uint64_pass(WHITESPACE "1" WHITESPACE, 1);
457 	errors += ut_uint64_fail("1;", QDF_STATUS_E_FAILURE);
458 	errors += ut_uint64_pass("18446744073709551615",
459 				 18446744073709551615ull);
460 	errors += ut_uint64_fail("18446744073709551616", QDF_STATUS_E_RANGE);
461 	errors += ut_uint64_pass(" 0", 0);
462 	errors += ut_uint64_fail("-1", QDF_STATUS_E_RANGE);
463 	errors += ut_uint64_fail("日本", QDF_STATUS_E_FAILURE);
464 
465 	return errors;
466 }
467 
qdf_types_ut_int_formats_parse(void)468 static uint32_t qdf_types_ut_int_formats_parse(void)
469 {
470 	uint32_t errors = 0;
471 
472 	errors += ut_uint64_pass("0b01", 1);
473 	errors += ut_uint64_pass("0o01234567", 342391);
474 	errors += ut_uint64_pass("0123456789", 123456789);
475 	errors += ut_uint64_pass("0x0123456789abcdef", 81985529216486895ll);
476 
477 	errors += ut_uint64_fail("0b012", QDF_STATUS_E_FAILURE);
478 	errors += ut_uint64_fail("0o012345678", QDF_STATUS_E_FAILURE);
479 	errors += ut_uint64_fail("0123456789a", QDF_STATUS_E_FAILURE);
480 	errors += ut_uint64_fail("0x0123456789abcdefg", QDF_STATUS_E_FAILURE);
481 
482 	return errors;
483 }
484 
485 #define ut_mac_pass(str, exp) __ut_mac(str, #str, QDF_STATUS_SUCCESS, &(exp))
486 #define ut_mac_fail(str) __ut_mac(str, #str, QDF_STATUS_E_FAILURE, NULL)
487 
488 static uint32_t
__ut_mac(const char * str,const char * display_str,QDF_STATUS exp_status,struct qdf_mac_addr * exp_value)489 __ut_mac(const char *str, const char *display_str, QDF_STATUS exp_status,
490 	 struct qdf_mac_addr *exp_value)
491 {
492 	struct qdf_mac_addr value;
493 	QDF_STATUS status = qdf_mac_parse(str, &value);
494 
495 	if (status != exp_status) {
496 		qdf_nofl_alert("FAIL: qdf_mac_parse(%s) -> status %d; expected status %d",
497 			       display_str, status, exp_status);
498 		return 1;
499 	}
500 
501 	if (QDF_IS_STATUS_ERROR(status))
502 		return 0;
503 
504 	if (qdf_mem_cmp(&value, exp_value, sizeof(value))) {
505 		qdf_nofl_alert("FAIL: qdf_mac_parse(%s) -> " QDF_MAC_ADDR_FMT
506 			       "; expected " QDF_MAC_ADDR_FMT,
507 			       display_str,
508 			       QDF_MAC_ADDR_REF(value.bytes),
509 			       QDF_MAC_ADDR_REF(exp_value->bytes));
510 		return 1;
511 	}
512 
513 	return 0;
514 }
515 
qdf_types_ut_mac_parse(void)516 static uint32_t qdf_types_ut_mac_parse(void)
517 {
518 	uint32_t errors = 0;
519 	struct qdf_mac_addr addr_aabbccddeeff = { {
520 		0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff } };
521 	struct qdf_mac_addr addr_0123456789ab = { {
522 		0x01, 0x23, 0x45, 0x67, 0x89, 0xab } };
523 
524 	errors += ut_mac_fail("");
525 	errors += ut_mac_fail("test");
526 	errors += ut_mac_fail("¥円");
527 	errors += ut_mac_pass("aabbccddeeff", addr_aabbccddeeff);
528 	errors += ut_mac_pass("AABBCCDDEEFF", addr_aabbccddeeff);
529 	errors += ut_mac_fail("aa:bbccddeeff");
530 	errors += ut_mac_fail("aabbccddee:ff");
531 	errors += ut_mac_pass("aa:bb:cc:dd:ee:ff", addr_aabbccddeeff);
532 	errors += ut_mac_pass("01:23:45:67:89:ab", addr_0123456789ab);
533 	errors += ut_mac_fail("01:23:45:67:89:ab:cd:ef");
534 	errors += ut_mac_fail("01:23:45\0:67:89:ab");
535 	errors += ut_mac_pass(WHITESPACE "01:23:45:67:89:ab" WHITESPACE,
536 			      addr_0123456789ab);
537 	errors += ut_mac_pass("01:23:45:67:89:ab\n", addr_0123456789ab);
538 	errors += ut_mac_fail("01:23:45:67:89:ab\t ,");
539 
540 	return errors;
541 }
542 
543 #define ut_ipv4_pass(str, exp) __ut_ipv4(str, #str, QDF_STATUS_SUCCESS, &(exp))
544 #define ut_ipv4_fail(str) __ut_ipv4(str, #str, QDF_STATUS_E_FAILURE, NULL)
545 
546 static uint32_t
__ut_ipv4(const char * str,const char * display_str,QDF_STATUS exp_status,struct qdf_ipv4_addr * exp_value)547 __ut_ipv4(const char *str, const char *display_str, QDF_STATUS exp_status,
548 	  struct qdf_ipv4_addr *exp_value)
549 {
550 	struct qdf_ipv4_addr value;
551 	QDF_STATUS status = qdf_ipv4_parse(str, &value);
552 
553 	if (status != exp_status) {
554 		qdf_nofl_alert("FAIL: qdf_ipv4_parse(%s) -> status %d; expected status %d",
555 			       display_str, status, exp_status);
556 		return 1;
557 	}
558 
559 	if (QDF_IS_STATUS_ERROR(status))
560 		return 0;
561 
562 	if (qdf_mem_cmp(&value, exp_value, sizeof(value))) {
563 		qdf_nofl_alert("FAIL: qdf_ipv4_parse(%s) -> " QDF_IPV4_ADDR_STR
564 			       "; expected " QDF_IPV4_ADDR_STR,
565 			       display_str,
566 			       QDF_IPV4_ADDR_ARRAY(value.bytes),
567 			       QDF_IPV4_ADDR_ARRAY(exp_value->bytes));
568 		return 1;
569 	}
570 
571 	return 0;
572 }
573 
qdf_types_ut_ipv4_parse(void)574 static uint32_t qdf_types_ut_ipv4_parse(void)
575 {
576 	uint32_t errors = 0;
577 	struct qdf_ipv4_addr addr_0000 = { { 0, 0, 0, 0 } };
578 	struct qdf_ipv4_addr addr_127001 = { { 127, 0, 0, 1 } };
579 	struct qdf_ipv4_addr addr_0112123 = { { 0, 1, 12, 123 } };
580 	struct qdf_ipv4_addr addr_255255255255 = { { 255, 255, 255, 255 } };
581 
582 	errors += ut_ipv4_fail("");
583 	errors += ut_ipv4_fail("test");
584 	errors += ut_ipv4_fail("¥円");
585 	errors += ut_ipv4_pass("0.0.0.0", addr_0000);
586 	errors += ut_ipv4_pass("127.0.0.1", addr_127001);
587 	errors += ut_ipv4_pass("255.255.255.255", addr_255255255255);
588 	errors += ut_ipv4_fail(".0.0.1");
589 	errors += ut_ipv4_fail("127.0.0.");
590 	errors += ut_ipv4_fail("abc.123.123.123");
591 	errors += ut_ipv4_fail("256.0.0.0");
592 	errors += ut_ipv4_pass("0.1.12.123", addr_0112123);
593 	errors += ut_ipv4_pass(WHITESPACE "0.1.12.123" WHITESPACE,
594 			       addr_0112123);
595 	errors += ut_ipv4_fail("0.1.12\0.123");
596 	errors += ut_ipv4_fail("0.1.12.123 ,");
597 
598 	return errors;
599 }
600 
601 #define ut_ipv6_pass(str, exp) __ut_ipv6(str, #str, QDF_STATUS_SUCCESS, &(exp))
602 #define ut_ipv6_fail(str) __ut_ipv6(str, #str, QDF_STATUS_E_FAILURE, NULL)
603 
604 static uint32_t
__ut_ipv6(const char * str,const char * display_str,QDF_STATUS exp_status,struct qdf_ipv6_addr * exp_value)605 __ut_ipv6(const char *str, const char *display_str, QDF_STATUS exp_status,
606 	  struct qdf_ipv6_addr *exp_value)
607 {
608 	struct qdf_ipv6_addr value;
609 	QDF_STATUS status = qdf_ipv6_parse(str, &value);
610 
611 	if (status != exp_status) {
612 		qdf_nofl_alert("FAIL: qdf_ipv6_parse(%s) -> status %d; expected status %d",
613 			       display_str, status, exp_status);
614 		return 1;
615 	}
616 
617 	if (QDF_IS_STATUS_ERROR(status))
618 		return 0;
619 
620 	if (qdf_mem_cmp(&value, exp_value, sizeof(value))) {
621 		qdf_nofl_alert("FAIL: qdf_ipv6_parse(%s) -> " QDF_IPV6_ADDR_STR
622 			       "; expected " QDF_IPV6_ADDR_STR,
623 			       display_str,
624 			       QDF_IPV6_ADDR_ARRAY(value.bytes),
625 			       QDF_IPV6_ADDR_ARRAY(exp_value->bytes));
626 		return 1;
627 	}
628 
629 	return 0;
630 }
631 
qdf_types_ut_ipv6_parse(void)632 static uint32_t qdf_types_ut_ipv6_parse(void)
633 {
634 	uint32_t errors = 0;
635 	struct qdf_ipv6_addr addr_00000000000000000000000000000000 = { {
636 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
637 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
638 	} };
639 	struct qdf_ipv6_addr addr_00000000000000000000000000000001 = { {
640 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
641 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
642 	} };
643 	struct qdf_ipv6_addr addr_00010000000000000000000000000000 = { {
644 		0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
645 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
646 	} };
647 	struct qdf_ipv6_addr addr_0123456789abcdefabcdef0123456789 = { {
648 		0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
649 		0xab, 0xcd, 0xef, 0x01, 0x23, 0x45, 0x67, 0x89,
650 	} };
651 	struct qdf_ipv6_addr addr_20010db885a3000000008a2e03707334 = { {
652 		0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00,
653 		0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34,
654 	} };
655 	struct qdf_ipv6_addr addr_ff020000000000000000000000000001 = { {
656 		0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
657 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
658 	} };
659 	struct qdf_ipv6_addr addr_00000000000000000000ffffc0000280 = { {
660 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
661 		0x00, 0x00, 0xff, 0xff, 0xc0, 0x00, 0x02, 0x80,
662 	} };
663 	struct qdf_ipv6_addr addr_00010000000000000000000000000001 = { {
664 		0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
665 		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
666 	} };
667 
668 	errors += ut_ipv6_fail("");
669 	errors += ut_ipv6_fail("test");
670 	errors += ut_ipv6_fail("¥円");
671 	errors += ut_ipv6_pass("::",
672 			       addr_00000000000000000000000000000000);
673 	errors += ut_ipv6_pass("::0",
674 			       addr_00000000000000000000000000000000);
675 	errors += ut_ipv6_pass("0:0:0:0:0:0:0:0",
676 			       addr_00000000000000000000000000000000);
677 	errors += ut_ipv6_pass("::1",
678 			       addr_00000000000000000000000000000001);
679 	errors += ut_ipv6_pass("1::",
680 			       addr_00010000000000000000000000000000);
681 	errors += ut_ipv6_pass("0:0:0:0:0:0:0:1",
682 			       addr_00000000000000000000000000000001);
683 	errors += ut_ipv6_pass("0123:4567:89ab:cdef:ABCD:EF01:2345:6789",
684 			       addr_0123456789abcdefabcdef0123456789);
685 	errors += ut_ipv6_fail("::0123:4567:89ab:cdef:ABCD:EF01:2345:6789");
686 	errors += ut_ipv6_fail("0123:4567:89ab:cdef:ABCD:EF01:2345:6789::");
687 	errors += ut_ipv6_pass("2001:0db8:85a3:0000:0000:8a2e:0370:7334",
688 			       addr_20010db885a3000000008a2e03707334);
689 	errors += ut_ipv6_pass("2001:db8:85a3:0:0:8a2e:370:7334",
690 			       addr_20010db885a3000000008a2e03707334);
691 	errors += ut_ipv6_pass("2001:db8:85a3::8a2e:370:7334",
692 			       addr_20010db885a3000000008a2e03707334);
693 	errors += ut_ipv6_pass("ff02::1",
694 			       addr_ff020000000000000000000000000001);
695 	errors += ut_ipv6_pass("::ffff:c000:0280",
696 			       addr_00000000000000000000ffffc0000280);
697 	errors += ut_ipv6_fail(":0:0:0:0:0:0:1");
698 	errors += ut_ipv6_fail(":0:0::0:0:1");
699 	errors += ut_ipv6_fail("0:0:0:0:0:0:0:");
700 	errors += ut_ipv6_fail("0:0:0::0:0:");
701 	errors += ut_ipv6_fail("0:0::0:0::0:0");
702 	errors += ut_ipv6_fail("xyz::zyx");
703 	errors += ut_ipv6_pass(WHITESPACE "1::1" WHITESPACE,
704 			       addr_00010000000000000000000000000001);
705 	errors += ut_ipv6_fail("1\0::1");
706 	errors += ut_ipv6_fail("1::1 ,");
707 	errors += ut_ipv6_fail("abcd");
708 
709 	return errors;
710 }
711 
qdf_types_unit_test(void)712 uint32_t qdf_types_unit_test(void)
713 {
714 	uint32_t errors = 0;
715 
716 	errors += qdf_types_ut_bool_parse();
717 	errors += qdf_types_ut_int32_parse();
718 	errors += qdf_types_ut_int64_parse();
719 	errors += qdf_types_ut_uint32_parse();
720 	errors += qdf_types_ut_uint64_parse();
721 	errors += qdf_types_ut_int_formats_parse();
722 	errors += qdf_types_ut_mac_parse();
723 	errors += qdf_types_ut_ipv4_parse();
724 	errors += qdf_types_ut_ipv6_parse();
725 	errors += qdf_types_ut_uint16_array_parse();
726 	errors += qdf_types_ut_uint32_array_parse();
727 	errors += qdf_types_ut_int32_array_parse();
728 
729 	return errors;
730 }
731 
732