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);
354 /* verify it against strcmp */
355 indicator = strcmp (s, s);
359 /* OK, seems to work */
364 test_strncmp_s (vlib_main_t * vm, unformat_input_t * input)
366 char s1[] = "Every moment is a fresh beginning";
367 uword s1len = sizeof (s1) - 1; // excluding null
371 vlib_cli_output (vm, "Test strncmp_s...");
373 /* s1 == s2, 0 is expected */
374 err = strncmp_s (s1, s1len, "Every moment is a fresh beginning", s1len,
381 /* s1 > s2, 0 is expected since comparison is no more than n character */
382 err = strncmp_s (s1, s1len, "Every moment is a fresh begin",
383 sizeof ("Every moment is a fresh begin") - 1, &indicator);
389 /* s1 < s2, < 0 is expected */
390 err = strncmp_s (s1, s1len, "Every moment is fresh beginning",
391 sizeof ("Every moment is fresh beginning") - 1,
395 if (!(indicator < 0))
398 /* s1 > s2, > 0 is expected */
399 err = strncmp_s ("Every moment is fresh beginning. ",
400 sizeof ("Every moment is fresh beginning. ") - 1, s1,
404 if (!(indicator > 0))
407 /* Try some negative tests */
410 err = strncmp_s (0, 0, 0, 0, 0);
415 err = strncmp_s (s1, s1len, "Every moment is a fresh beginning", s1len + 1,
420 /* unterminated s1 */
422 err = strncmp_s (s1, s1len, "Every moment is a fresh beginning",
423 sizeof ("Every moment is a fresh beginning") - 1,
428 /* OK, seems to work */
433 test_clib_strncmp (vlib_main_t * vm, unformat_input_t * input)
435 char s1[] = "Every moment is a fresh beginning";
436 uword s1len = sizeof (s1) - 1; // excluding null
437 int indicator, v_indicator;
439 vlib_cli_output (vm, "Test clib_strncmp...");
441 /* s1 == s2, 0 is expected */
442 indicator = clib_strncmp (s1, "Every moment is a fresh beginning", s1len);
445 /* verify it against strncmp */
446 v_indicator = strncmp (s1, "Every moment is a fresh beginning", s1len);
447 if (v_indicator != 0)
449 if (v_indicator != indicator)
452 /* s1 > s2, 0 is expected since comparison is no more than n character */
453 indicator = clib_strncmp (s1, "Every moment is a fresh begin",
454 sizeof ("Every moment is a fresh begin") - 1);
457 /* verify it against strncmp */
458 v_indicator = strncmp (s1, "Every moment is a fresh begin",
459 sizeof ("Every moment is a fresh begin") - 1);
460 if (v_indicator != 0)
462 if (v_indicator != indicator)
465 /* s1 < s2, < 0 is expected */
466 indicator = clib_strncmp (s1, "Every moment is fresh beginning",
467 sizeof ("Every moment is fresh beginning") - 1);
468 if (!(indicator < 0))
470 /* verify it against strncmp */
471 v_indicator = strncmp (s1, "Every moment is fresh beginning",
472 sizeof ("Every moment is fresh beginning") - 1);
473 if (!(v_indicator < 0))
475 if (v_indicator != indicator)
478 /* s1 > s2, > 0 is expected */
479 indicator = clib_strncmp ("Every moment is fresh beginning. ", s1, s1len);
480 if (!(indicator > 0))
482 /* verify it against strncmp */
483 v_indicator = strncmp ("Every moment is fresh beginning. ", s1, s1len);
484 if (!(v_indicator > 0))
486 if (v_indicator != indicator)
489 /* Try some negative tests */
493 /* make sure we don't crash */
494 indicator = clib_strncmp (0, 0, 0);
495 if (indicator != EOK)
500 clib_strncmp (s1, "Every moment is a fresh beginning", s1len + 1);
503 /* verify it against strncmp */
504 v_indicator = strncmp (s1, "Every moment is a fresh beginning", s1len + 1);
505 if (v_indicator != 0)
507 if (v_indicator != indicator)
510 /* unterminated s1 */
512 indicator = clib_strncmp (s1, "Every moment is a fresh beginning",
513 sizeof ("every moment is a fresh beginning") - 1);
516 /* verify it against strncmp */
517 v_indicator = strncmp (s1, "Every moment is a fresh beginning",
518 sizeof ("Every moment is a fresh beginning") - 1);
519 if (v_indicator != 0)
521 if (v_indicator != indicator)
524 /* OK, seems to work */
529 test_strcpy_s (vlib_main_t * vm, unformat_input_t * input)
531 char src[] = "To err is human.";
533 char *dst_alias = dst;
535 size_t s1size = sizeof (dst); // including null
538 vlib_cli_output (vm, "Test strcpy_s...");
540 err = strcpy_s (dst, s1size, src);
544 /* This better not fail but check anyhow */
545 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
553 err = strcpy_s (0, 0, 0);
558 err = strcpy_s (dst, 10, src);
563 err = strcpy_s (dst, s1size, dst_alias);
568 err = strcpy_s (dst, s1size, dst + 1);
572 /* OK, seems to work */
577 test_clib_strcpy (vlib_main_t * vm, unformat_input_t * input)
579 char src[] = "The journey of a one thousand miles begins with one step.";
581 char *dst_alias = dst;
585 vlib_cli_output (vm, "Test clib_strcpy...");
587 err = clib_strcpy (dst, src);
591 /* This better not fail but check anyhow */
592 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
598 /* verify it against strcpy */
601 /* This better not fail but check anyhow */
602 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
610 err = clib_strcpy (0, 0);
615 err = clib_strcpy (dst, dst_alias);
620 err = clib_strcpy (dst, dst + 1);
624 /* OK, seems to work */
629 test_strncpy_s (vlib_main_t * vm, unformat_input_t * input)
631 char src[] = "Those who dare to fail miserably can achieve greatly.";
632 char dst[100], old_dst[100];
633 char *dst_alias = dst;
635 size_t s1size = sizeof (dst); // including null
638 vlib_cli_output (vm, "Test strncpy_s...");
640 /* dmax includes null, n excludes null */
642 /* n == string len of src */
643 err = strncpy_s (dst, s1size, src, clib_strnlen (src, sizeof (src)));
646 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
652 /* limited copy -- strlen src > n, copy up to n */
653 err = strncpy_s (dst, s1size, "The price of greatness is responsibility.",
657 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), "The price ",
663 /* n > string len of src */
664 err = strncpy_s (dst, s1size, src, clib_strnlen (src, sizeof (src)) + 10);
667 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
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);
712 err = strncpy_s (dst, s1size, dst_alias, s1size);
716 /* OK, seems to work */
721 test_clib_strncpy (vlib_main_t * vm, unformat_input_t * input)
723 char src[] = "Those who dare to fail miserably can achieve greatly.";
724 char dst[100], old_dst[100];
725 char *dst_alias = dst;
727 size_t s1size = sizeof (dst); // including null
730 vlib_cli_output (vm, "Test clib_strncpy...");
732 /* n == string len of src */
733 err = clib_strncpy (dst, src, clib_strnlen (src, sizeof (src)));
737 /* This better not fail but check anyhow */
738 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
744 /* Verify it against strncpy */
745 strncpy (dst, src, strlen (src));
747 /* This better not fail but check anyhow */
748 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
754 /* limited copy -- strlen src > n, copy up to n */
755 err = clib_strncpy (dst, "The price of greatness is responsibility.", 10);
758 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), "The price ",
763 /* verify it against strncpy */
764 memset_s (dst, sizeof (dst), 0, sizeof (dst));
766 strncpy (dst, "The price of greatness is responsibility.", 10);
767 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), "The price ",
773 /* n > string len of src */
774 err = clib_strncpy (dst, src, clib_strnlen (src, sizeof (src)) + 10);
777 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
782 /* Verify it against strncpy */
783 strncpy (dst, src, strlen (src));
784 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
790 /* zero length copy */
791 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
792 err = clib_strncpy (dst, src, 0);
795 /* verify dst is untouched */
796 if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), old_dst, &indicator) !=
804 err = clib_strncpy (0, 0, 1);
809 err = clib_strncpy (dst, dst + 1, s1size - 1);
814 err = clib_strncpy (dst, dst_alias, s1size);
818 /* OK, seems to work */
823 test_strcat_s (vlib_main_t * vm, unformat_input_t * input)
825 char src[100], dst[100], old_dst[100];
826 char *dst_alias = dst;
827 size_t s1size = sizeof (dst); // including null
831 vlib_cli_output (vm, "Test strcat_s...");
833 strcpy_s (dst, sizeof (dst), "Tough time never last ");
834 strcpy_s (src, sizeof (src), "but tough people do");
835 err = strcat_s (dst, s1size, src);
838 if (strcmp_s (dst, s1size - 1,
839 "Tough time never last but tough people do",
845 /* empty string concatenation */
846 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
847 err = strcat_s (dst, s1size, "");
850 /* verify dst is untouched */
851 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
857 err = strcat_s (0, 0, 0);
862 err = strcat_s (dst, s1size, dst + 1);
867 err = strcat_s (dst, s1size, dst_alias);
871 /* not enough space for dst */
872 err = strcat_s (dst, 10, src);
876 /* OK, seems to work */
881 test_clib_strcat (vlib_main_t * vm, unformat_input_t * input)
883 char src[100], dst[100], old_dst[100];
884 char *dst_alias = dst;
885 size_t s1size = sizeof (dst); // including null
889 vlib_cli_output (vm, "Test clib_strcat...");
891 strcpy_s (dst, sizeof (dst), "Tough time never last ");
892 strcpy_s (src, sizeof (src), "but tough people do");
893 err = clib_strcat (dst, src);
896 if (strcmp_s (dst, s1size - 1,
897 "Tough time never last but tough people do",
902 /* verify it against strcat */
903 strcpy_s (dst, sizeof (dst), "Tough time never last ");
904 strcpy_s (src, sizeof (src), "but tough people do");
906 if (strcmp_s (dst, s1size - 1,
907 "Tough time never last but tough people do",
913 /* empty string concatenation */
914 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
915 err = clib_strcat (dst, "");
918 /* verify dst is untouched */
919 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
925 err = clib_strcat (0, 0);
930 err = clib_strcat (dst, dst + 1);
935 err = clib_strcat (dst, dst_alias);
939 /* OK, seems to work */
944 test_strncat_s (vlib_main_t * vm, unformat_input_t * input)
946 char src[100], dst[100], old_dst[100];
947 char *dst_alias = dst;
948 size_t s1size = sizeof (dst); // including null
950 char s1[] = "Two things are infinite: ";
951 char s2[] = "the universe and human stupidity; ";
952 char s3[] = "I am not sure about the universe.";
955 vlib_cli_output (vm, "Test strncat_s...");
957 strcpy_s (dst, sizeof (dst), s1);
958 strcpy_s (src, sizeof (src), s2);
959 err = strncat_s (dst, s1size, src, clib_strnlen (src, sizeof (src)));
962 if (strcmp_s (dst, s1size - 1,
963 "Two things are infinite: the universe and human stupidity; ",
969 /* truncation, n >= dmax - strnlen_s (dst, dmax) */
970 err = strncat_s (dst, clib_strnlen (dst, sizeof (dst)) +
971 clib_strnlen (s3, sizeof (s3)), s3,
972 clib_strnlen (s3, sizeof (s3)));
973 if (err != EOVERFLOW)
976 * resulting string is dst + strlen (s3) - 1 characters + null.
977 * notice the "." is missing at the end of the resulting string because
978 * the space is needed to accommodate the null
979 * Notice strcmp_s will check s1 or dst to make sure it is null terminated
981 if (strcmp_s (dst, s1size - 1,
982 "Two things are infinite: the universe and human stupidity; "
983 "I am not sure about the universe", &indicator) != EOK)
989 strcpy_s (dst, sizeof (dst), s1);
990 err = strncat_s (dst, s1size, src, clib_strnlen (src, sizeof (src)) + 10);
993 if (strcmp_s (dst, s1size - 1,
994 "Two things are infinite: the universe and human stupidity; ",
1000 /* zero length strncat */
1001 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
1002 err = strncat_s (dst, sizeof (dst), src, 0);
1005 /* verify dst is untouched */
1006 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
1011 /* empty string, wrong n concatenation */
1012 err = strncat_s (dst, sizeof (dst), "", 10);
1015 /* verify dst is untouched */
1016 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
1021 /* limited concatenation, string > n, copy up to n */
1022 strcpy_s (dst, sizeof (dst), s1);
1023 err = strncat_s (dst, s1size, s2, 13);
1026 if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
1031 /* verify it against strncat */
1032 strcpy_s (dst, sizeof (dst), s1);
1033 strncat (dst, s2, 13);
1034 if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
1040 /* negative stuff */
1041 err = strncat_s (0, 0, 0, 1);
1045 /* no room for dst -- dmax - strnlen_s (dst, dmax) == 0 */
1046 err = strncat_s (dst, clib_strnlen (dst, sizeof (dst)), s2,
1047 clib_strnlen (s2, sizeof (s2)));
1052 err = strncat_s (dst, s1size, dst + 1, clib_strnlen (dst + 1, s1size - 1));
1057 err = strncat_s (dst, s1size, dst_alias, clib_strnlen (dst, sizeof (dst)));
1061 /* OK, seems to work */
1066 test_clib_strncat (vlib_main_t * vm, unformat_input_t * input)
1068 char src[100], dst[100], old_dst[100];
1069 char *dst_alias = dst;
1070 size_t s1size = sizeof (dst); // including null
1072 char s1[] = "Two things are infinite: ";
1073 char s2[] = "the universe and human stupidity; ";
1076 vlib_cli_output (vm, "Test clib_strncat...");
1078 /* n == strlen src */
1079 strcpy_s (dst, sizeof (dst), s1);
1080 strcpy_s (src, sizeof (src), s2);
1081 err = clib_strncat (dst, src, clib_strnlen (src, sizeof (src)));
1084 if (strcmp_s (dst, s1size - 1,
1085 "Two things are infinite: the universe and human stupidity; ",
1090 /* verify it against strncat */
1091 strcpy_s (dst, sizeof (dst), s1);
1092 strncat (dst, src, clib_strnlen (src, sizeof (src)));
1093 if (strcmp_s (dst, s1size - 1,
1094 "Two things are infinite: the universe and human stupidity; ",
1100 /* n > strlen src */
1101 strcpy_s (dst, sizeof (dst), s1);
1102 err = clib_strncat (dst, src, clib_strnlen (src, sizeof (src)) + 10);
1105 if (strcmp_s (dst, s1size - 1,
1106 "Two things are infinite: the universe and human stupidity; ",
1111 /* verify it against strncat */
1112 strcpy_s (dst, sizeof (dst), s1);
1113 strncat (dst, src, clib_strnlen (src, sizeof (src)));
1114 if (strcmp_s (dst, s1size - 1,
1115 "Two things are infinite: the universe and human stupidity; ",
1121 /* zero length strncat */
1122 clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
1123 err = clib_strncat (dst, src, 0);
1126 /* verify dst is untouched */
1127 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
1132 /* empty string, wrong n concatenation */
1133 err = clib_strncat (dst, "", 10);
1136 /* verify dst is untouched */
1137 if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
1142 /* limited concatenation, string > n, copy up to n */
1143 strcpy_s (dst, sizeof (dst), s1);
1144 err = clib_strncat (dst, s2, 13);
1147 if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
1152 /* verify it against strncat */
1153 strcpy_s (dst, sizeof (dst), s1);
1154 strncat (dst, s2, 13);
1155 if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
1161 /* negative stuff */
1162 err = clib_strncat (0, 0, 1);
1167 err = clib_strncat (dst, dst + 1, s1size - 1);
1172 err = clib_strncat (dst, dst_alias, clib_strnlen (dst, sizeof (dst)));
1176 /* OK, seems to work */
1181 test_strtok_s (vlib_main_t * vm, unformat_input_t * input)
1189 char *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7;
1191 vlib_cli_output (vm, "Test strtok_s...");
1192 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1193 len = strnlen_s (str1, sizeof (str1));
1194 tok1 = strtok_s (str1, &len, " ", &p2str);
1195 tok2 = strtok_s (0, &len, " ", &p2str);
1196 tok3 = strtok_s (0, &len, " ", &p2str);
1197 tok4 = strtok_s (0, &len, " ", &p2str);
1198 tok5 = strtok_s (0, &len, " ", &p2str);
1199 tok6 = strtok_s (0, &len, " ", &p2str);
1200 tok7 = strtok_s (0, &len, " ", &p2str);
1202 strcmp_s (tok1, strlen (tok1), "brevity", &indicator) != EOK)
1206 if ((tok2 == 0) || strcmp_s (tok2, strlen (tok2), "is", &indicator) != EOK)
1210 if ((tok3 == 0) || strcmp_s (tok3, strlen (tok3), "the", &indicator) != EOK)
1215 || strcmp_s (tok4, strlen (tok4), "soul", &indicator) != EOK)
1219 if ((tok5 == 0) || strcmp_s (tok5, strlen (tok5), "of", &indicator) != EOK)
1223 if ((tok6 == 0) || strcmp_s (tok6, strlen (tok6), "wit", &indicator) != EOK)
1230 /* delimiter not present in the string -- the whole string is returned */
1231 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1232 len = strnlen_s (str1, sizeof (str1) - 1);
1234 tok1 = strtok_s (str1, &len, ",", &p2str);
1235 if ((tok1 == 0) || strcmp_s (tok1, strlen (tok1), str1, &indicator) != EOK)
1240 /* negative stuff */
1241 tok = strtok_s (0, 0, 0, 0);
1245 /* s1 and ptr contents are null */
1247 tok = strtok_s (0, 0, 0, &ptr);
1251 /* unterminate s1 */
1253 len = strnlen_s (str1, sizeof (str1) - 1);
1254 str1[strlen (str1)] = 0x2;
1255 tok = strtok_s (str1, &len, ",", &p2str);
1260 * unterminated s2. This test case in not perfect because there is no
1261 * argument for s2max. But s2 len is limited to 16 characters. If the API
1262 * does not find the null character at s2[15], it declares the string s2
1265 memset_s (str2, sizeof (str2), 0xfa, sizeof (str2));
1266 tok = strtok_s (str1, &len, str2, &p2str);
1270 /* OK, seems to work */
1275 test_clib_strtok (vlib_main_t * vm, unformat_input_t * input)
1278 char *s1, *s1_alias, *tok, *ptr;
1281 char *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7;
1283 vlib_cli_output (vm, "Test clib_strtok...");
1284 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1286 tok1 = clib_strtok (str1, " ", &p2str);
1287 tok2 = clib_strtok (0, " ", &p2str);
1288 tok3 = clib_strtok (0, " ", &p2str);
1289 tok4 = clib_strtok (0, " ", &p2str);
1290 tok5 = clib_strtok (0, " ", &p2str);
1291 tok6 = clib_strtok (0, " ", &p2str);
1292 tok7 = clib_strtok (0, " ", &p2str);
1294 strcmp_s (tok1, strlen (tok1), "brevity", &indicator) != EOK)
1298 if ((tok2 == 0) || strcmp_s (tok2, strlen (tok2), "is", &indicator) != EOK)
1302 if ((tok3 == 0) || strcmp_s (tok3, strlen (tok3), "the", &indicator) != EOK)
1307 || strcmp_s (tok4, strlen (tok4), "soul", &indicator) != EOK)
1311 if ((tok5 == 0) || strcmp_s (tok5, strlen (tok5), "of", &indicator) != EOK)
1315 if ((tok6 == 0) || strcmp_s (tok6, strlen (tok6), "wit", &indicator) != EOK)
1321 /* verify it againest strtok_r */
1322 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1324 tok1 = strtok_r (str1, " ", &p2str);
1325 tok2 = strtok_r (0, " ", &p2str);
1326 tok3 = strtok_r (0, " ", &p2str);
1327 tok4 = strtok_r (0, " ", &p2str);
1328 tok5 = strtok_r (0, " ", &p2str);
1329 tok6 = strtok_r (0, " ", &p2str);
1330 tok7 = strtok_r (0, " ", &p2str);
1332 strcmp_s (tok1, strlen (tok1), "brevity", &indicator) != EOK)
1336 if ((tok2 == 0) || strcmp_s (tok2, strlen (tok2), "is", &indicator) != EOK)
1340 if ((tok3 == 0) || strcmp_s (tok3, strlen (tok3), "the", &indicator) != EOK)
1345 || strcmp_s (tok4, strlen (tok4), "soul", &indicator) != EOK)
1349 if ((tok5 == 0) || strcmp_s (tok5, strlen (tok5), "of", &indicator) != EOK)
1353 if ((tok6 == 0) || strcmp_s (tok6, strlen (tok6), "wit", &indicator) != EOK)
1360 /* delimiter not present in the string -- the whole string is returned */
1361 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1363 tok1 = clib_strtok (str1, ",", &p2str);
1364 if ((tok1 == 0) || strcmp_s (tok1, strlen (tok1), str1, &indicator) != EOK)
1368 /* verify it against strtok_r */
1369 strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
1371 tok1 = strtok_r (str1, ",", &p2str);
1372 if ((tok1 == 0) || strcmp_s (tok1, strlen (tok1), str1, &indicator) != EOK)
1377 /* negative stuff */
1381 tok = clib_strtok (s1, s1_alias, (char **) 0);
1385 /* s1 and ptr contents are null */
1387 tok = clib_strtok (s1, s1_alias, &ptr);
1391 /* verify it against strtok_r */
1392 /* No can do. This causes a crash in strtok_r */
1393 // tok = strtok_r (s1, " ", &ptr);
1398 * Can't test unterminated string s1 and s2 becuase clib_strtok does not
1399 * supply s1 and s2 max
1402 /* OK, seems to work */
1407 test_strnlen_s (vlib_main_t * vm, unformat_input_t * input)
1409 const char s1[] = "Truth is incontrovertible";
1412 vlib_cli_output (vm, "Test strnlen_s...");
1414 len = strnlen_s (s1, sizeof (s1));
1415 if (len != sizeof (s1) - 1)
1418 len = strnlen_s (s1, 5);
1422 /* negative stuff */
1423 len = strnlen_s (0, 0);
1427 /* OK, seems to work */
1432 test_clib_strnlen (vlib_main_t * vm, unformat_input_t * input)
1434 const char s1[] = "Truth is incontrovertible";
1437 vlib_cli_output (vm, "Test clib_strnlen...");
1439 len = clib_strnlen (s1, sizeof (s1));
1440 if (len != sizeof (s1) - 1)
1443 len = clib_strnlen (s1, 5);
1447 /* negative stuff */
1448 len = clib_strnlen (0, 0);
1452 /* OK, seems to work */
1457 test_strstr_s (vlib_main_t * vm, unformat_input_t * input)
1462 size_t s1len = sizeof (s1) - 1; // excluding null
1465 vlib_cli_output (vm, "Test strstr_s...");
1467 /* substring not present */
1468 strcpy_s (s1, s1len, "success is not final, failure is not fatal.");
1469 err = strstr_s (s1, s1len, "failures", sizeof ("failures"), &sub);;
1473 /* substring present */
1474 err = strstr_s (s1, s1len, "failure", sizeof ("failure"), &sub);
1479 strcmp_s (sub, strlen (sub), "failure is not fatal.", &indicator)
1485 /* negative stuff */
1487 /* Null pointers test */
1488 err = strstr_s (0, 0, 0, 0, 0);
1492 /* unterminated s1 and s2 */
1493 memset_s (s1, ARRAY_LEN (s1), 0xfe, ARRAY_LEN (s1));
1494 err = strstr_s (s1, s1len, s1, s1len, &sub);
1498 /* OK, seems to work */
1503 test_clib_strstr (vlib_main_t * vm, unformat_input_t * input)
1507 size_t s1len = sizeof (s1) - 1; // excluding null
1510 vlib_cli_output (vm, "Test clib_strstr...");
1512 /* substring not present */
1513 strcpy_s (s1, s1len, "success is not final, failure is not fatal.");
1514 sub = clib_strstr (s1, "failures");
1517 /* verify it against strstr */
1518 sub = strstr (s1, "failures");
1522 /* substring present */
1523 sub = clib_strstr (s1, "failure");
1526 if (strcmp_s (sub, strlen (sub), "failure is not fatal.", &indicator) !=
1531 /* verify it against strstr */
1532 sub = strstr (s1, "failure");
1535 if (strcmp_s (sub, strlen (sub), "failure is not fatal.", &indicator) !=
1541 /* negative stuff */
1543 /* Null pointers test */
1545 sub = clib_strstr (s, s);
1549 * Can't verify it against strstr for this test. Null pointers cause strstr
1550 * to crash. Go figure!
1553 /* unterminated s1 and s2 */
1554 memset_s (s1, ARRAY_LEN (s1), 0xfe, ARRAY_LEN (s1));
1555 sub = clib_strstr (s1, s1);
1559 * Can't verify it against strstr for this test. Unterminated string causes
1560 * strstr to crash. Go figure!
1563 /* OK, seems to work */
1567 #define foreach_string_test \
1568 _ (0, MEMCPY_S, "memcpy_s", memcpy_s) \
1569 _ (1, CLIB_MEMCPY, "clib_memcpy", clib_memcpy) \
1570 _ (2, MEMSET_S , "memset_s", memset_s) \
1571 _ (3, CLIB_MEMSET , "clib_memset", clib_memset) \
1572 _ (4, MEMCMP_S, "memcmp_s", memcmp_s) \
1573 _ (5, CLIB_MEMCMP, "clib_memcmp", clib_memcmp) \
1574 _ (6, STRCMP_S, "strcmp_s", strcmp_s) \
1575 _ (7, CLIB_STRCMP, "clib_strcmp", clib_strcmp) \
1576 _ (8, STRNCMP_S, "strncmp_s", strncmp_s) \
1577 _ (9, CLIB_STRNCMP, "clib_strncmp", clib_strncmp) \
1578 _ (10, STRCPY_S, "strcpy_s", strcpy_s) \
1579 _ (11, CLIB_STRCPY, "clib_strcpy", clib_strcpy) \
1580 _ (12, STRNCPY_S, "strncpy_s", strncpy_s) \
1581 _ (13, CLIB_STRNCPY, "clib_strncpy", clib_strncpy) \
1582 _ (14, STRCAT_S, "strcat_s", strcat_s) \
1583 _ (15, CLIB_STRCAT, "clib_strcat", clib_strcat) \
1584 _ (16, STRNCAT_S, "strncat_s", strncat_s) \
1585 _ (17, CLIB_STRNCAT, "clib_strncat", clib_strncat) \
1586 _ (18, STRTOK_S, "strtok_s", strtok_s) \
1587 _ (19, CLIB_STRTOK, "clib_strtok", clib_strtok) \
1588 _ (20, STRNLEN_S, "strnlen_s", strnlen_s) \
1589 _ (21, CLIB_STRNLEN, "clib_strnlen", clib_strnlen) \
1590 _ (22, STRSTR_S, "strstr_s", strstr_s) \
1591 _ (23, CLIB_STRSTR, "clib_strstr", clib_strstr)
1595 #define _(v,f,s,p) STRING_TEST_##f = v,
1601 unformat_string_test (unformat_input_t * input, va_list * args)
1603 u8 *r = va_arg (*args, u8 *);
1607 #define _(v,f,s,p) else if (unformat (input, s)) *r = STRING_TEST_##f;
1616 typedef int (*string_test_func) (vlib_main_t * vm, unformat_input_t * input);
1620 string_test_func test;
1621 } string_test_func_t;
1623 static clib_error_t *
1624 string_test_command_fn (vlib_main_t * vm,
1625 unformat_input_t * input,
1626 vlib_cli_command_t * cmd_arg)
1628 string_test_func_t string_func[] = {
1629 #define _(v,f,s,p) { test_##p },
1633 const char *string_table[] = {
1634 #define _(v,f,s,p) s,
1639 i8 specific_test = ~0;
1641 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1643 if (unformat (input, "%U", unformat_string_test, &specific_test))
1646 return clib_error_return (0, "unknown input `%U'",
1647 format_unformat_error, input);
1650 if (specific_test == ~0)
1652 for (specific_test = STRING_TEST_MEMCPY_S;
1653 specific_test <= STRING_TEST_CLIB_STRSTR; specific_test++)
1655 ok = (string_func[specific_test]).test (vm, input);
1658 vlib_cli_output (vm, "test_%s failed",
1659 string_table[specific_test]);
1663 res = (string_func[specific_test]).test (vm, input);
1665 vlib_cli_output (vm, "String unit test(s) failed...");
1667 vlib_cli_output (vm, "String unit test(s) OK...");
1672 VLIB_CLI_COMMAND (string_test_command, static) =
1674 .path = "test string",
1675 .short_help = "test string [memcpy_s | clib_memcpy | memset_s | "
1676 "clib_memset | memcmp_s | clib_memcmp | strcmp_s | clib_strcmp | "
1677 "strncmp_s | clib_strncmp | strcpy_s | clib_strcpy | strncpy_s | "
1678 "clib_strncpy | strcat_s | clib_strcat | strncat_s | clib_strncat | "
1679 "strtok_s | clib_strtok | strnlen_s | clib_strnlen | strstr_s | "
1681 .function = string_test_command_fn,
1686 * fd.io coding-style-patch-verification: ON
1689 * eval: (c-set-style "gnu")