2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vlib/vlib.h>
16 #include <vppinfra/string.h>
19 test_memset_s (vlib_main_t * vm, unformat_input_t * input)
25 vlib_cli_output (vm, "Test memset_s...");
27 err = memset_s (dst, ARRAY_LEN (dst), 0xfe, ARRAY_LEN (dst));
32 for (i = 0; i < ARRAY_LEN (dst); i++)
36 err = memset_s (dst, ARRAY_LEN (dst), 0xfa, ARRAY_LEN (dst) + 1);
45 test_clib_memset (vlib_main_t * vm, unformat_input_t * input)
51 vlib_cli_output (vm, "Test clib_memset...");
53 err = clib_memset (dst, 0xfe, ARRAY_LEN (dst));
58 for (i = 0; i < ARRAY_LEN (dst); i++)
66 test_memcpy_s (vlib_main_t * vm, unformat_input_t * input)
68 char src[64], dst[64];
72 vlib_cli_output (vm, "Test memcpy_s...");
74 for (i = 0; i < ARRAY_LEN (src); i++)
78 err = memcpy_s (dst, sizeof (dst), src, sizeof (src));
83 /* This better not fail but check anyhow */
84 for (i = 0; i < ARRAY_LEN (dst); i++)
89 err = memcpy_s (dst + 1, sizeof (dst) - 1, src, sizeof (src));
95 err = memcpy_s (dst, sizeof (dst), dst + 1, sizeof (dst) - 1);
100 /* Zero length copy */
101 err = memcpy_s (0, sizeof (dst), src, 0);
106 /* OK, seems to work */
111 test_clib_memcpy (vlib_main_t * vm, unformat_input_t * input)
113 char src[64], dst[64];
117 vlib_cli_output (vm, "Test clib_memcpy...");
119 for (i = 0; i < ARRAY_LEN (src); i++)
123 err = clib_memcpy (dst, src, sizeof (src));
128 /* This better not fail but check anyhow */
129 for (i = 0; i < ARRAY_LEN (dst); i++)
130 if (src[i] != dst[i])
132 /* verify it against memcpy */
133 memcpy (dst, src, sizeof (src));
135 /* This better not fail but check anyhow */
136 for (i = 0; i < ARRAY_LEN (dst); i++)
137 if (src[i] != dst[i])
140 /* Zero length copy */
141 err = clib_memcpy (0, src, 0);
146 /* OK, seems to work */
151 test_memcmp_s (vlib_main_t * vm, unformat_input_t * input)
153 char src[64], dst[64];
157 vlib_cli_output (vm, "Test memcmp_s...");
159 /* Fill array with different values */
160 err = clib_memset (src, 0x1, ARRAY_LEN (src));
163 err = clib_memset (dst, 0x3, ARRAY_LEN (dst));
167 /* s1 > s2, > 0 is expected in diff */
168 err = memcmp_s (dst, ARRAY_LEN (dst), src, ARRAY_LEN (src), &diff);
174 /* s1 < s2, < 0 is expected in diff */
175 err = memcmp_s (src, ARRAY_LEN (src), dst, ARRAY_LEN (dst), &diff);
181 err = clib_memset (dst, 0x1, ARRAY_LEN (dst));
185 /* s1 == s2, 0 is expected in diff */
186 err = memcmp_s (src, ARRAY_LEN (src), dst, ARRAY_LEN (dst), &diff);
192 /* Try negative tests */
193 err = memcmp_s (0, 0, 0, 0, 0);
197 /* Try s2max > s1max */
198 err = memcmp_s (src, ARRAY_LEN (src) - 1, dst, ARRAY_LEN (dst), &diff);
202 /* OK, seems to work */
207 test_clib_memcmp (vlib_main_t * vm, unformat_input_t * input)
209 char src[64], dst[64];
213 vlib_cli_output (vm, "Test clib_memcmp...");
215 /* Fill array with different values */
216 err = clib_memset (src, 0x1, ARRAY_LEN (src));
219 err = clib_memset (dst, 0x3, ARRAY_LEN (dst));
223 /* s1 > s2, > 0 is expected in diff */
224 if (!(clib_memcmp (dst, src, ARRAY_LEN (src)) > 0))
226 /* verify it against memcmp */
227 if (!(memcmp (dst, src, ARRAY_LEN (src)) > 0))
230 /* s1 < s2, < 0 is expected in diff */
231 if (!(clib_memcmp (src, dst, ARRAY_LEN (dst)) < 0))
233 /* verify it against memcmp */
234 if (!(memcmp (src, dst, ARRAY_LEN (dst)) < 0))
237 err = clib_memset (dst, 0x1, ARRAY_LEN (dst));
241 /* s1 == s2, 0 is expected in diff */
242 if (clib_memcmp (src, dst, ARRAY_LEN (dst)) != 0)
244 /* verify it against memcmp */
245 if (memcmp (src, dst, ARRAY_LEN (dst)) != 0)
248 /* Try negative tests */
250 if (clib_memcmp (s, s, 0) != 0)
252 /* verify it against memcmp */
253 if (memcmp (s, s, 0) != 0)
256 /* OK, seems to work */
261 test_strcmp_s (vlib_main_t * vm, unformat_input_t * input)
263 char s1[] = "Simplicity is the ultimate sophistication";
264 uword s1len = sizeof (s1) - 1; // excluding null
268 vlib_cli_output (vm, "Test strcmp_s...");
270 /* s1 == s2, 0 is expected */
271 err = strcmp_s (s1, s1len, "Simplicity is the ultimate sophistication",
278 /* s1 > s2, > 0 is expected */
279 err = strcmp_s (s1, s1len, "Simplicity is the ultimate", &indicator);
282 if (!(indicator > 0))
285 /* s1 < s2, < 0 is expected */
286 err = strcmp_s (s1, s1len, "Simplicity is the ultimate sophistication!",
290 if (!(indicator < 0))
293 /* Try some negative tests */
295 /* Null pointers test */
296 err = strcmp_s (0, 0, 0, 0);
300 /* non-null terminated s1 */
302 err = strcmp_s (s1, s1len, "Simplicity is the ultimate sophistication",
307 /* OK, seems to work */
312 test_clib_strcmp (vlib_main_t * vm, unformat_input_t * input)
314 char s1[] = "Simplicity is the ultimate sophistication";
318 vlib_cli_output (vm, "Test clib_strcmp...");
320 /* s1 == s2, 0 is expected */
321 indicator = clib_strcmp (s1, "Simplicity is the ultimate sophistication");
324 /* verify it against strcmp */
325 indicator = strcmp (s1, "Simplicity is the ultimate sophistication");
329 /* s1 > s2, > 0 is expected */
330 indicator = clib_strcmp (s1, "Simplicity is the ultimate");
331 if (!(indicator > 0))
333 /* verify it against strcmp */
334 indicator = strcmp (s1, "Simplicity is the ultimate");
335 if (!(indicator > 0))
338 /* s1 < s2, < 0 is expected */
339 indicator = clib_strcmp (s1, "Simplicity is the ultimate sophistication!");
340 if (!(indicator < 0))
342 /* verify it against strcmp */
343 indicator = strcmp (s1, "Simplicity is the ultimate sophistication!");
344 if (!(indicator < 0))
347 /* Try some negative tests */
349 /* Null pointers comparison */
351 indicator = clib_strcmp (s, s);
355 /* OK, seems to work */
360 test_strncmp_s (vlib_main_t * vm, unformat_input_t * input)
362 char s1[] = "Every moment is a fresh beginning";
363 uword s1len = sizeof (s1) - 1; // excluding null
367 vlib_cli_output (vm, "Test strncmp_s...");
369 /* s1 == s2, 0 is expected */
370 err = strncmp_s (s1, s1len, "Every moment is a fresh beginning", s1len,
377 /* s1 > s2, 0 is expected since comparison is no more than n character */
378 err = strncmp_s (s1, s1len, "Every moment is a fresh begin",
379 sizeof ("Every moment is a fresh begin") - 1, &indicator);
385 /* s1 < s2, < 0 is expected */
386 err = strncmp_s (s1, s1len, "Every moment is fresh beginning",
387 sizeof ("Every moment is fresh beginning") - 1,
391 if (!(indicator < 0))
394 /* s1 > s2, > 0 is expected */
395 err = strncmp_s ("Every moment is fresh beginning. ",
396 sizeof ("Every moment is fresh beginning. ") - 1, s1,
400 if (!(indicator > 0))
403 /* Try some negative tests */
406 err = strncmp_s (0, 0, 0, 0, 0);
411 err = strncmp_s (s1, s1len, "Every moment is a fresh beginning", s1len + 1,
416 /* unterminated s1 */
418 err = strncmp_s (s1, s1len, "Every moment is a fresh beginning",
419 sizeof ("Every moment is a fresh beginning") - 1,
424 /* OK, seems to work */
429 test_clib_strncmp (vlib_main_t * vm, unformat_input_t * input)
431 char s1[] = "Every moment is a fresh beginning";
432 uword s1len = sizeof (s1) - 1; // excluding null
433 int indicator, v_indicator;
435 vlib_cli_output (vm, "Test clib_strncmp...");
437 /* s1 == s2, 0 is expected */
438 indicator = clib_strncmp (s1, "Every moment is a fresh beginning", s1len);
441 /* verify it against strncmp */
442 v_indicator = strncmp (s1, "Every moment is a fresh beginning", s1len);
443 if (v_indicator != 0)
446 /* s1 > s2, 0 is expected since comparison is no more than n character */
447 indicator = clib_strncmp (s1, "Every moment is a fresh begin",
448 sizeof ("Every moment is a fresh begin") - 1);
451 /* verify it against strncmp */
452 v_indicator = strncmp (s1, "Every moment is a fresh begin",
453 sizeof ("Every moment is a fresh begin") - 1);
454 if (v_indicator != 0)
457 /* s1 < s2, < 0 is expected */
458 indicator = clib_strncmp (s1, "Every moment is fresh beginning",
459 sizeof ("Every moment is fresh beginning") - 1);
460 if (!(indicator < 0))
462 /* verify it against strncmp */
463 v_indicator = strncmp (s1, "Every moment is fresh beginning",
464 sizeof ("Every moment is fresh beginning") - 1);
465 if (!(v_indicator < 0))
467 if (v_indicator != indicator)
470 /* s1 > s2, > 0 is expected */
471 indicator = clib_strncmp ("Every moment is fresh beginning. ", s1, s1len);
472 if (!(indicator > 0))
474 /* verify it against strncmp */
475 v_indicator = strncmp ("Every moment is fresh beginning. ", s1, s1len);
476 if (!(v_indicator > 0))
478 if (v_indicator != indicator)
481 /* Try some negative tests */
485 /* make sure we don't crash */
486 indicator = clib_strncmp (0, 0, 0);
487 if (indicator != EOK)
492 clib_strncmp (s1, "Every moment is a fresh beginning", s1len + 1);
495 /* verify it against strncmp */
496 v_indicator = strncmp (s1, "Every moment is a fresh beginning", s1len + 1);
497 if (v_indicator != 0)
500 /* unterminated s1 */
502 indicator = clib_strncmp (s1, "Every moment is a fresh beginning",
503 sizeof ("every moment is a fresh beginning") - 1);
506 /* verify it against strncmp */
507 v_indicator = strncmp (s1, "Every moment is a fresh beginning",
508 sizeof ("Every moment is a fresh beginning") - 1);
509 if (v_indicator != 0)
512 /* OK, seems to work */
517 test_strcpy_s (vlib_main_t * vm, unformat_input_t * input)
519 char src[] = "To err is human.";
522 size_t s1size = sizeof (dst); // including null
525 vlib_cli_output (vm, "Test strcpy_s...");
527 err = strcpy_s (dst, s1size, src);
531 /* This better not fail but check anyhow */
532 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
540 err = strcpy_s (0, 0, 0);
545 err = strcpy_s (dst, 10, src);
551 /* GCC 8 flunks this one at compile time... */
552 err = strcpy_s (dst, s1size, dst);
558 err = strcpy_s (dst, s1size, dst + 1);
562 /* OK, seems to work */
567 test_clib_strcpy (vlib_main_t * vm, unformat_input_t * input)
569 char src[] = "The journey of a one thousand miles begins with one step.";
574 vlib_cli_output (vm, "Test clib_strcpy...");
576 err = clib_strcpy (dst, src);
580 /* This better not fail but check anyhow */
581 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
587 /* verify it against strcpy */
590 /* This better not fail but check anyhow */
591 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
599 err = clib_strcpy (0, 0);
605 /* GCC 8 flunks this one at compile time... */
606 err = clib_strcpy (dst, dst);
612 err = clib_strcpy (dst, dst + 1);
616 /* OK, seems to work */
621 test_strncpy_s (vlib_main_t * vm, unformat_input_t * input)
623 char src[] = "Those who dare to fail miserably can achieve greatly.";
624 char dst[100], old_dst[100];
626 size_t s1size = sizeof (dst); // including null
629 vlib_cli_output (vm, "Test strncpy_s...");
631 /* dmax includes null, n excludes null */
633 /* n == string len of src */
634 err = strncpy_s (dst, s1size, src, clib_strnlen (src, sizeof (src)));
637 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
643 /* limited copy -- strlen src > n, copy up to n */
644 err = strncpy_s (dst, s1size, "The price of greatness is responsibility.",
648 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), "The price ",
654 /* n > string len of src */
655 err = clib_memset (dst, 1, sizeof (dst));
659 err = strncpy_s (dst, s1size, src, clib_strnlen (src, sizeof (src)) + 10);
662 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
668 /* Make sure bytes after strlen(dst) is untouched */
669 for (i = 1 + clib_strnlen (dst, sizeof (dst)); i < sizeof (dst); i++)
673 /* truncation, n >= dmax */
674 err = strncpy_s (dst, clib_strnlen (src, sizeof (src)), src,
675 clib_strnlen (src, sizeof (src)));
676 if (err != EOVERFLOW)
679 /* Check dst content */
680 if (dst[strlen (dst)] != '\0')
682 if (strncmp_s (dst, clib_strnlen (dst, sizeof (dst)), src,
683 clib_strnlen (dst, sizeof (dst)), &indicator) != EOK)
688 /* zero length copy */
689 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
690 err = strncpy_s (dst, sizeof (dst), src, 0);
693 /* verify dst is untouched */
694 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), old_dst, &indicator) !=
702 err = strncpy_s (0, 0, 0, 1);
707 err = strncpy_s (dst, s1size, dst + 1, s1size - 1);
713 /* GCC 8 flunks this one at compile time... */
714 err = strncpy_s (dst, s1size, dst, s1size);
719 /* OK, seems to work */
724 test_clib_strncpy (vlib_main_t * vm, unformat_input_t * input)
726 char src[] = "Those who dare to fail miserably can achieve greatly.";
727 char dst[100], old_dst[100];
729 size_t s1size = sizeof (dst); // including null
732 vlib_cli_output (vm, "Test clib_strncpy...");
734 /* n == string len of src */
735 err = clib_strncpy (dst, src, clib_strnlen (src, sizeof (src)));
739 /* This better not fail but check anyhow */
740 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
746 /* Verify it against strncpy */
748 /* GCC 8 debian flunks this one at compile time */
749 strncpy (dst, src, strlen (src));
751 /* This better not fail but check anyhow */
752 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
759 /* limited copy -- strlen src > n, copy up to n */
760 err = clib_strncpy (dst, "The price of greatness is responsibility.", 10);
763 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), "The price ",
768 /* verify it against strncpy */
769 memset_s (dst, sizeof (dst), 0, sizeof (dst));
772 /* GCC 8 flunks this one at compile time... */
773 strncpy (dst, "The price of greatness is responsibility.", 10);
774 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), "The price ",
781 /* n > string len of src */
782 err = clib_strncpy (dst, src, clib_strnlen (src, sizeof (src)) + 10);
785 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
790 /* Verify it against strncpy */
792 /* GCC 8 debian flunks this one at compile time */
793 strncpy (dst, src, strlen (src));
794 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
801 /* zero length copy */
802 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
803 err = clib_strncpy (dst, src, 0);
806 /* verify dst is untouched */
807 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), old_dst, &indicator) !=
815 err = clib_strncpy (0, 0, 1);
820 err = clib_strncpy (dst, dst + 1, s1size);
826 /* GCC 8 flunks this one at compile time... */
827 err = clib_strncpy (dst, dst, s1size);
832 /* OK, seems to work */
837 test_strcat_s (vlib_main_t * vm, unformat_input_t * input)
839 char src[100], dst[100], old_dst[100];
840 size_t s1size = sizeof (dst); // including null
844 vlib_cli_output (vm, "Test strcat_s...");
846 strcpy_s (dst, sizeof (dst), "Tough time never last ");
847 strcpy_s (src, sizeof (src), "but tough people do");
848 err = strcat_s (dst, s1size, src);
851 if (strcmp_s (dst, s1size - 1,
852 "Tough time never last but tough people do",
858 /* empty string concatenation */
859 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
860 err = strcat_s (dst, s1size, "");
863 /* verify dst is untouched */
864 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
870 err = strcat_s (0, 0, 0);
875 err = strcat_s (dst, s1size, dst + 1);
881 /* GCC 8 flunks this one at compile time... */
882 err = strcat_s (dst, s1size, dst);
887 /* not enough space for dst */
888 err = strcat_s (dst, 10, src);
892 /* OK, seems to work */
897 test_clib_strcat (vlib_main_t * vm, unformat_input_t * input)
899 char src[100], dst[100], old_dst[100];
900 size_t s1size = sizeof (dst); // including null
904 vlib_cli_output (vm, "Test clib_strcat...");
906 strcpy_s (dst, sizeof (dst), "Tough time never last ");
907 strcpy_s (src, sizeof (src), "but tough people do");
908 err = clib_strcat (dst, src);
911 if (strcmp_s (dst, s1size - 1,
912 "Tough time never last but tough people do",
917 /* verify it against strcat */
918 strcpy_s (dst, sizeof (dst), "Tough time never last ");
919 strcpy_s (src, sizeof (src), "but tough people do");
921 if (strcmp_s (dst, s1size - 1,
922 "Tough time never last but tough people do",
928 /* empty string concatenation */
929 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
930 err = clib_strcat (dst, "");
933 /* verify dst is untouched */
934 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
940 err = clib_strcat (0, 0);
945 err = clib_strcat (dst, dst + 1);
951 /* GCC 8 flunks this one at compile time... */
952 err = clib_strcat (dst, dst);
957 /* OK, seems to work */
962 test_strncat_s (vlib_main_t * vm, unformat_input_t * input)
964 char src[100], dst[100], old_dst[100];
965 size_t s1size = sizeof (dst); // including null
967 char s1[] = "Two things are infinite: ";
968 char s2[] = "the universe and human stupidity; ";
969 char s3[] = "I am not sure about the universe.";
972 vlib_cli_output (vm, "Test strncat_s...");
974 strcpy_s (dst, sizeof (dst), s1);
975 strcpy_s (src, sizeof (src), s2);
976 err = strncat_s (dst, s1size, src, clib_strnlen (src, sizeof (src)));
979 if (strcmp_s (dst, s1size - 1,
980 "Two things are infinite: the universe and human stupidity; ",
986 /* truncation, n >= dmax - strnlen_s (dst, dmax) */
987 err = strncat_s (dst, clib_strnlen (dst, sizeof (dst)) +
988 clib_strnlen (s3, sizeof (s3)), s3,
989 clib_strnlen (s3, sizeof (s3)));
990 if (err != EOVERFLOW)
993 * resulting string is dst + strlen (s3) - 1 characters + null.
994 * notice the "." is missing at the end of the resulting string because
995 * the space is needed to accommodate the null
996 * Notice strcmp_s will check s1 or dst to make sure it is null terminated
998 if (strcmp_s (dst, s1size - 1,
999 "Two things are infinite: the universe and human stupidity; "
1000 "I am not sure about the universe", &indicator) != EOK)
1005 /* n > strlen src */
1006 strcpy_s (dst, sizeof (dst), s1);
1007 err = strncat_s (dst, s1size, src, clib_strnlen (src, sizeof (src)) + 10);
1010 if (strcmp_s (dst, s1size - 1,
1011 "Two things are infinite: the universe and human stupidity; ",
1017 /* zero length strncat */
1018 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
1019 err = strncat_s (dst, sizeof (dst), src, 0);
1022 /* verify dst is untouched */
1023 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
1028 /* empty string, wrong n concatenation */
1029 err = strncat_s (dst, sizeof (dst), "", 10);
1032 /* verify dst is untouched */
1033 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
1038 /* limited concatenation, string > n, copy up to n */
1039 strcpy_s (dst, sizeof (dst), s1);
1040 err = strncat_s (dst, s1size, s2, 13);
1043 if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
1048 /* verify it against strncat */
1050 /* GCC 8 debian flunks this one at compile time */
1051 strcpy_s (dst, sizeof (dst), s1);
1052 strncat (dst, s2, 13);
1053 if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
1060 /* negative stuff */
1061 err = strncat_s (0, 0, 0, 1);
1065 /* no room for dst -- dmax - strnlen_s (dst, dmax) == 0 */
1066 err = strncat_s (dst, clib_strnlen (dst, sizeof (dst)), s2,
1067 clib_strnlen (s2, sizeof (s2)));
1072 err = strncat_s (dst, s1size, dst + 1, clib_strnlen (dst + 1, s1size - 1));
1078 /* GCC 8 flunks this one at compile time... */
1079 err = strncat_s (dst, s1size, dst, clib_strnlen (dst, sizeof (dst)));
1084 /* OK, seems to work */
1089 test_clib_strncat (vlib_main_t * vm, unformat_input_t * input)
1091 char src[100], dst[100], old_dst[100];
1092 size_t s1size = sizeof (dst); // including null
1094 char s1[] = "Two things are infinite: ";
1095 char s2[] = "the universe and human stupidity; ";
1098 vlib_cli_output (vm, "Test clib_strncat...");
1100 /* n == strlen src */
1101 strcpy_s (dst, sizeof (dst), s1);
1102 strcpy_s (src, sizeof (src), s2);
1103 err = clib_strncat (dst, src, clib_strnlen (src, sizeof (src)));
1106 if (strcmp_s (dst, s1size - 1,
1107 "Two things are infinite: the universe and human stupidity; ",
1112 /* verify it against strncat */
1113 strcpy_s (dst, sizeof (dst), s1);
1114 strncat (dst, src, clib_strnlen (src, sizeof (src)));
1115 if (strcmp_s (dst, s1size - 1,
1116 "Two things are infinite: the universe and human stupidity; ",
1122 /* n > strlen src */
1123 strcpy_s (dst, sizeof (dst), s1);
1124 err = clib_strncat (dst, src, clib_strnlen (src, sizeof (src)) + 10);
1127 if (strcmp_s (dst, s1size - 1,
1128 "Two things are infinite: the universe and human stupidity; ",
1133 /* verify it against strncat */
1134 strcpy_s (dst, sizeof (dst), s1);
1135 strncat (dst, src, clib_strnlen (src, sizeof (src)));
1136 if (strcmp_s (dst, s1size - 1,
1137 "Two things are infinite: the universe and human stupidity; ",
1143 /* zero length strncat */
1144 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
1145 err = clib_strncat (dst, src, 0);
1148 /* verify dst is untouched */
1149 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
1154 /* empty string, wrong n concatenation */
1155 err = clib_strncat (dst, "", 10);
1158 /* verify dst is untouched */
1159 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
1164 /* limited concatenation, string > n, copy up to n */
1165 strcpy_s (dst, sizeof (dst), s1);
1166 err = clib_strncat (dst, s2, 13);
1169 if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
1174 /* verify it against strncat */
1176 /* GCC 8 debian flunks this one at compile time */
1177 strcpy_s (dst, sizeof (dst), s1);
1178 strncat (dst, s2, 13);
1179 if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
1186 /* negative stuff */
1187 err = clib_strncat (0, 0, 1);
1192 err = clib_strncat (dst, dst + 1, s1size - 1);
1198 /* GCC 8 flunks this one at compile time... */
1199 err = clib_strncat (dst, dst, clib_strnlen (dst, sizeof (dst)));
1204 /* OK, seems to work */
1209 test_strtok_s (vlib_main_t * vm, unformat_input_t * input)
1217 char *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7;
1219 vlib_cli_output (vm, "Test strtok_s...");
1220 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1221 len = strnlen_s (str1, sizeof (str1));
1222 tok1 = strtok_s (str1, &len, " ", &p2str);
1223 tok2 = strtok_s (0, &len, " ", &p2str);
1224 tok3 = strtok_s (0, &len, " ", &p2str);
1225 tok4 = strtok_s (0, &len, " ", &p2str);
1226 tok5 = strtok_s (0, &len, " ", &p2str);
1227 tok6 = strtok_s (0, &len, " ", &p2str);
1228 tok7 = strtok_s (0, &len, " ", &p2str);
1230 strcmp_s (tok1, strlen (tok1), "brevity", &indicator) != EOK)
1234 if ((tok2 == 0) || strcmp_s (tok2, strlen (tok2), "is", &indicator) != EOK)
1238 if ((tok3 == 0) || strcmp_s (tok3, strlen (tok3), "the", &indicator) != EOK)
1243 || strcmp_s (tok4, strlen (tok4), "soul", &indicator) != EOK)
1247 if ((tok5 == 0) || strcmp_s (tok5, strlen (tok5), "of", &indicator) != EOK)
1251 if ((tok6 == 0) || strcmp_s (tok6, strlen (tok6), "wit", &indicator) != EOK)
1258 /* delimiter not present in the string -- the whole string is returned */
1259 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1260 len = strnlen_s (str1, sizeof (str1) - 1);
1262 tok1 = strtok_s (str1, &len, ",", &p2str);
1263 if ((tok1 == 0) || strcmp_s (tok1, strlen (tok1), str1, &indicator) != EOK)
1268 /* negative stuff */
1269 tok = strtok_s (0, 0, 0, 0);
1273 /* s1 and ptr contents are null */
1275 tok = strtok_s (0, 0, 0, &ptr);
1279 /* unterminate s1 */
1281 len = strnlen_s (str1, sizeof (str1) - 1);
1282 str1[strlen (str1)] = 0x2;
1283 tok = strtok_s (str1, &len, ",", &p2str);
1288 * unterminated s2. This test case in not perfect because there is no
1289 * argument for s2max. But s2 len is limited to 16 characters. If the API
1290 * does not find the null character at s2[15], it declares the string s2
1293 memset_s (str2, sizeof (str2), 0xfa, sizeof (str2));
1294 tok = strtok_s (str1, &len, str2, &p2str);
1298 /* OK, seems to work */
1303 test_clib_strtok (vlib_main_t * vm, unformat_input_t * input)
1306 char *s1 __attribute__ ((unused));
1307 char *tok __attribute__ ((unused));
1308 char *ptr __attribute__ ((unused));
1311 char *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7;
1313 vlib_cli_output (vm, "Test clib_strtok...");
1314 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1316 tok1 = clib_strtok (str1, " ", &p2str);
1317 tok2 = clib_strtok (0, " ", &p2str);
1318 tok3 = clib_strtok (0, " ", &p2str);
1319 tok4 = clib_strtok (0, " ", &p2str);
1320 tok5 = clib_strtok (0, " ", &p2str);
1321 tok6 = clib_strtok (0, " ", &p2str);
1322 tok7 = clib_strtok (0, " ", &p2str);
1324 strcmp_s (tok1, strlen (tok1), "brevity", &indicator) != EOK)
1328 if ((tok2 == 0) || strcmp_s (tok2, strlen (tok2), "is", &indicator) != EOK)
1332 if ((tok3 == 0) || strcmp_s (tok3, strlen (tok3), "the", &indicator) != EOK)
1337 || strcmp_s (tok4, strlen (tok4), "soul", &indicator) != EOK)
1341 if ((tok5 == 0) || strcmp_s (tok5, strlen (tok5), "of", &indicator) != EOK)
1345 if ((tok6 == 0) || strcmp_s (tok6, strlen (tok6), "wit", &indicator) != EOK)
1351 /* verify it againest strtok_r */
1352 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1354 tok1 = strtok_r (str1, " ", &p2str);
1355 tok2 = strtok_r (0, " ", &p2str);
1356 tok3 = strtok_r (0, " ", &p2str);
1357 tok4 = strtok_r (0, " ", &p2str);
1358 tok5 = strtok_r (0, " ", &p2str);
1359 tok6 = strtok_r (0, " ", &p2str);
1360 tok7 = strtok_r (0, " ", &p2str);
1362 strcmp_s (tok1, strlen (tok1), "brevity", &indicator) != EOK)
1366 if ((tok2 == 0) || strcmp_s (tok2, strlen (tok2), "is", &indicator) != EOK)
1370 if ((tok3 == 0) || strcmp_s (tok3, strlen (tok3), "the", &indicator) != EOK)
1375 || strcmp_s (tok4, strlen (tok4), "soul", &indicator) != EOK)
1379 if ((tok5 == 0) || strcmp_s (tok5, strlen (tok5), "of", &indicator) != EOK)
1383 if ((tok6 == 0) || strcmp_s (tok6, strlen (tok6), "wit", &indicator) != EOK)
1390 /* delimiter not present in the string -- the whole string is returned */
1391 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1393 tok1 = clib_strtok (str1, ",", &p2str);
1394 if ((tok1 == 0) || strcmp_s (tok1, strlen (tok1), str1, &indicator) != EOK)
1398 /* verify it against strtok_r */
1399 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1401 tok1 = strtok_r (str1, ",", &p2str);
1402 if ((tok1 == 0) || strcmp_s (tok1, strlen (tok1), str1, &indicator) != EOK)
1407 /* negative stuff */
1411 /* GCC 8 flunks this one at compile time... */
1412 tok = clib_strtok (s1, s1, (char **) 0);
1416 /* s1 and ptr contents are null */
1417 tok = clib_strtok (s1, s1, &ptr);
1422 /* verify it against strtok_r */
1423 /* No can do. This causes a crash in strtok_r */
1424 // tok = strtok_r (s1, " ", &ptr);
1429 * Can't test unterminated string s1 and s2 becuase clib_strtok does not
1430 * supply s1 and s2 max
1433 /* OK, seems to work */
1438 test_strnlen_s (vlib_main_t * vm, unformat_input_t * input)
1440 const char s1[] = "Truth is incontrovertible";
1443 vlib_cli_output (vm, "Test strnlen_s...");
1445 len = strnlen_s (s1, sizeof (s1));
1446 if (len != sizeof (s1) - 1)
1449 len = strnlen_s (s1, 5);
1453 /* negative stuff */
1454 len = strnlen_s (0, 0);
1458 /* OK, seems to work */
1463 test_clib_strnlen (vlib_main_t * vm, unformat_input_t * input)
1465 const char s1[] = "Truth is incontrovertible";
1468 vlib_cli_output (vm, "Test clib_strnlen...");
1470 len = clib_strnlen (s1, sizeof (s1));
1471 if (len != sizeof (s1) - 1)
1474 len = clib_strnlen (s1, 5);
1478 /* negative stuff */
1479 len = clib_strnlen (0, 0);
1483 /* OK, seems to work */
1488 test_strstr_s (vlib_main_t * vm, unformat_input_t * input)
1493 size_t s1len = sizeof (s1) - 1; // excluding null
1496 vlib_cli_output (vm, "Test strstr_s...");
1498 /* substring not present */
1499 strcpy_s (s1, s1len, "success is not final, failure is not fatal.");
1500 err = strstr_s (s1, s1len, "failures", sizeof ("failures"), &sub);;
1504 /* substring present */
1505 err = strstr_s (s1, s1len, "failure", sizeof ("failure"), &sub);
1510 strcmp_s (sub, strlen (sub), "failure is not fatal.", &indicator)
1516 /* negative stuff */
1518 /* Null pointers test */
1519 err = strstr_s (0, 0, 0, 0, 0);
1523 /* unterminated s1 and s2 */
1524 memset_s (s1, ARRAY_LEN (s1), 0xfe, ARRAY_LEN (s1));
1525 err = strstr_s (s1, s1len, s1, s1len, &sub);
1529 /* OK, seems to work */
1534 test_clib_strstr (vlib_main_t * vm, unformat_input_t * input)
1538 size_t s1len = sizeof (s1) - 1; // excluding null
1541 vlib_cli_output (vm, "Test clib_strstr...");
1543 /* substring not present */
1544 strcpy_s (s1, s1len, "success is not final, failure is not fatal.");
1545 sub = clib_strstr (s1, "failures");
1548 /* verify it against strstr */
1549 sub = strstr (s1, "failures");
1553 /* substring present */
1554 sub = clib_strstr (s1, "failure");
1557 if (strcmp_s (sub, strlen (sub), "failure is not fatal.", &indicator) !=
1562 /* verify it against strstr */
1563 sub = strstr (s1, "failure");
1566 if (strcmp_s (sub, strlen (sub), "failure is not fatal.", &indicator) !=
1572 /* negative stuff */
1574 /* Null pointers test */
1576 sub = clib_strstr (s, s);
1580 * Can't verify it against strstr for this test. Null pointers cause strstr
1581 * to crash. Go figure!
1584 /* unterminated s1 and s2 */
1585 memset_s (s1, ARRAY_LEN (s1), 0xfe, ARRAY_LEN (s1));
1586 sub = clib_strstr (s1, s1);
1590 * Can't verify it against strstr for this test. Unterminated string causes
1591 * strstr to crash. Go figure!
1594 /* OK, seems to work */
1598 #define foreach_string_test \
1599 _ (0, MEMCPY_S, "memcpy_s", memcpy_s) \
1600 _ (1, CLIB_MEMCPY, "clib_memcpy", clib_memcpy) \
1601 _ (2, MEMSET_S , "memset_s", memset_s) \
1602 _ (3, CLIB_MEMSET , "clib_memset", clib_memset) \
1603 _ (4, MEMCMP_S, "memcmp_s", memcmp_s) \
1604 _ (5, CLIB_MEMCMP, "clib_memcmp", clib_memcmp) \
1605 _ (6, STRCMP_S, "strcmp_s", strcmp_s) \
1606 _ (7, CLIB_STRCMP, "clib_strcmp", clib_strcmp) \
1607 _ (8, STRNCMP_S, "strncmp_s", strncmp_s) \
1608 _ (9, CLIB_STRNCMP, "clib_strncmp", clib_strncmp) \
1609 _ (10, STRCPY_S, "strcpy_s", strcpy_s) \
1610 _ (11, CLIB_STRCPY, "clib_strcpy", clib_strcpy) \
1611 _ (12, STRNCPY_S, "strncpy_s", strncpy_s) \
1612 _ (13, CLIB_STRNCPY, "clib_strncpy", clib_strncpy) \
1613 _ (14, STRCAT_S, "strcat_s", strcat_s) \
1614 _ (15, CLIB_STRCAT, "clib_strcat", clib_strcat) \
1615 _ (16, STRNCAT_S, "strncat_s", strncat_s) \
1616 _ (17, CLIB_STRNCAT, "clib_strncat", clib_strncat) \
1617 _ (18, STRTOK_S, "strtok_s", strtok_s) \
1618 _ (19, CLIB_STRTOK, "clib_strtok", clib_strtok) \
1619 _ (20, STRNLEN_S, "strnlen_s", strnlen_s) \
1620 _ (21, CLIB_STRNLEN, "clib_strnlen", clib_strnlen) \
1621 _ (22, STRSTR_S, "strstr_s", strstr_s) \
1622 _ (23, CLIB_STRSTR, "clib_strstr", clib_strstr)
1626 #define _(v,f,s,p) STRING_TEST_##f = v,
1632 unformat_string_test (unformat_input_t * input, va_list * args)
1634 u8 *r = va_arg (*args, u8 *);
1638 #define _(v,f,s,p) else if (unformat (input, s)) *r = STRING_TEST_##f;
1647 typedef int (*string_test_func) (vlib_main_t * vm, unformat_input_t * input);
1651 string_test_func test;
1652 } string_test_func_t;
1654 static clib_error_t *
1655 string_test_command_fn (vlib_main_t * vm,
1656 unformat_input_t * input,
1657 vlib_cli_command_t * cmd_arg)
1659 string_test_func_t string_func[] = {
1660 #define _(v,f,s,p) { test_##p },
1664 const char *string_table[] = {
1665 #define _(v,f,s,p) s,
1670 i8 specific_test = ~0;
1672 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1674 if (unformat (input, "%U", unformat_string_test, &specific_test))
1677 return clib_error_return (0, "unknown input `%U'",
1678 format_unformat_error, input);
1681 if (specific_test == ~0)
1683 for (specific_test = STRING_TEST_MEMCPY_S;
1684 specific_test <= STRING_TEST_CLIB_STRSTR; specific_test++)
1686 ok = (string_func[specific_test]).test (vm, input);
1689 vlib_cli_output (vm, "test_%s failed",
1690 string_table[specific_test]);
1694 res = (string_func[specific_test]).test (vm, input);
1696 vlib_cli_output (vm, "String unit test(s) failed...");
1698 vlib_cli_output (vm, "String unit test(s) OK...");
1703 VLIB_CLI_COMMAND (string_test_command, static) =
1705 .path = "test string",
1706 .short_help = "test string [memcpy_s | clib_memcpy | memset_s | "
1707 "clib_memset | memcmp_s | clib_memcmp | strcmp_s | clib_strcmp | "
1708 "strncmp_s | clib_strncmp | strcpy_s | clib_strcpy | strncpy_s | "
1709 "clib_strncpy | strcat_s | clib_strcat | strncat_s | clib_strncat | "
1710 "strtok_s | clib_strtok | strnlen_s | clib_strnlen | strstr_s | "
1712 .function = string_test_command_fn,
1717 * fd.io coding-style-patch-verification: ON
1720 * eval: (c-set-style "gnu")