c11 safe string handling support
[vpp.git] / src / plugins / unittest / string_test.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #include <vlib/vlib.h>
16 #include <vppinfra/string.h>
17
18 static int
19 test_clib_memset (vlib_main_t * vm, unformat_input_t * input)
20 {
21   u8 dst[64];
22   int i;
23   errno_t err;
24
25   vlib_cli_output (vm, "Test memset_s...");
26
27   err = memset_s (dst, ARRAY_LEN (dst), 0xfe, ARRAY_LEN (dst));
28
29   if (err != EOK)
30     return -1;
31
32   for (i = 0; i < ARRAY_LEN (dst); i++)
33     if (dst[i] != 0xFE)
34       return -1;
35
36   err = memset_s (dst, ARRAY_LEN (dst), 0xfa, ARRAY_LEN (dst) + 1);
37
38   if (err == EOK)
39     return -1;
40
41   return 0;
42 }
43
44 static int
45 test_memcpy (vlib_main_t * vm, unformat_input_t * input)
46 {
47   char src[64], dst[64];
48   int i;
49   errno_t err;
50
51   vlib_cli_output (vm, "Test memcpy_s...");
52
53   for (i = 0; i < ARRAY_LEN (src); i++)
54     src[i] = i + 1;
55
56   /* Typical case */
57   err = memcpy_s (dst, sizeof (dst), src, sizeof (src));
58
59   if (err != EOK)
60     return -1;
61
62   /* This better not fail but check anyhow */
63   for (i = 0; i < ARRAY_LEN (dst); i++)
64     if (src[i] != dst[i])
65       return -1;
66
67   /* Size fail */
68   err = memcpy_s (dst + 1, sizeof (dst) - 1, src, sizeof (src));
69
70   if (err == EOK)
71     return -1;
72
73   /* overlap fail */
74   err = memcpy_s (dst, sizeof (dst), dst + 1, sizeof (dst) - 1);
75
76   if (err == EOK)
77     return -1;
78
79   /* Zero length copy */
80   err = memcpy_s (0, sizeof (dst), src, 0);
81
82   if (err != EOK)
83     return -1;
84
85   /* OK, seems to work */
86   return 0;
87 }
88
89 static clib_error_t *
90 string_test_command_fn (vlib_main_t * vm,
91                         unformat_input_t * input,
92                         vlib_cli_command_t * cmd_arg)
93 {
94   int res = 0;
95   u8 t_memcpy = 0;
96   u8 t_clib_memset = 0;
97   u8 specific_test;
98
99   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
100     {
101       if (unformat (input, "memcpy_s"))
102         t_memcpy = 1;
103       else if (unformat (input, "memset_s"))
104         t_clib_memset = 1;
105       break;
106     }
107
108   specific_test = t_memcpy + t_clib_memset;
109
110   if (specific_test == 0)
111     {
112       res = test_memcpy (vm, input);
113       res += test_clib_memset (vm, input);
114       goto done;
115     }
116
117   if (t_memcpy)
118     res = test_memcpy (vm, input);
119   else if (t_clib_memset)
120     res = test_clib_memset (vm, input);
121
122 done:
123   if (res)
124     vlib_cli_output (vm, "String unit test(s) failed...");
125   else
126     vlib_cli_output (vm, "String unit test(s) OK...");
127   return 0;
128 }
129
130 /* *INDENT-OFF* */
131 VLIB_CLI_COMMAND (string_test_command, static) =
132 {
133   .path = "test string",
134   .short_help = "string library tests",
135   .function = string_test_command_fn,
136 };
137 /* *INDENT-ON* */
138
139 /*
140  * fd.io coding-style-patch-verification: ON
141  *
142  * Local Variables:
143  * eval: (c-set-style "gnu")
144  * End:
145  */