aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/unittest/string_test.c
blob: c59a05ced088bcb4b136c1d3db88fc01ef5d9d39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 *------------------------------------------------------------------
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *------------------------------------------------------------------
 */
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vlib/pci/pci.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/ip/ip4_packet.h>
#include <vnet/ip/ip6_packet.h>
#include <vnet/devices/virtio/virtio.h>
#include <vnet/devices/virtio/pci.h>

static clib_error_t *
virtio_pci_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
			      vlib_cli_command_t * cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  virtio_pci_create_if_args_t args;
  u64 feature_mask = (u64) ~ (0ULL);

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  memset (&args, 0, sizeof (args));
  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
	;
      else if (unformat (line_input, "feature-mask 0x%llx", &feature_mask))
	args.features = feature_mask;
      else if (unformat (line_input, "gso-enabled"))
	args.gso_enabled = 1;
      else
	return clib_error_return (0, "unknown input `%U'",
				  format_unformat_error, input);
    }
  unformat_free (line_input);

  virtio_pci_create_if (vm, &args);

  return args.error;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (virtio_pci_create_command, static) = {
  .path = "create interface virtio",
  .short_help = "create interface virtio <pci-address> "
                "[feature-mask <hex-mask>]",
  .function = virtio_pci_create_command_fn,
};
/* *INDENT-ON* */

static clib_error_t *
virtio_pci_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
			      vlib_cli_command_t * cmd)
{
  unformat_input_t _line_input, *line_input = &_line_input;
  u32 sw_if_index = ~0;
  vnet_hw_interface_t *hw;
  virtio_main_t *vim = &virtio_main;
  virtio_if_t *vif;
  vnet_main_t *vnm = vnet_get_main ();

  /* Get a line of input. */
  if (!unformat_user (input, unformat_line_input, line_input))
    return 0;

  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (line_input, "sw_if_index %d", &sw_if_index))
	;
      else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
			 vnm, &sw_if_index))
	;
      else
	return clib_error_return (0, "unknown input `%U'",
				  format_unformat_error, input);
    }
  unformat_free (line_input);

  if (sw_if_index == ~0)
    return clib_error_return (0,
			      "please specify interface name or sw_if_index");

  hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
  if (hw == NULL || virtio_device_class.index != hw->dev_class_index)
    return clib_error_return (0, "not a virtio interface");

  vif = pool_elt_at_index (vim->interfaces, hw->dev_instance);

  if (virtio_pci_delete_if (vm, vif) < 0)
    return clib_error_return (0, "not a virtio pci interface");

  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (virtio_pci_delete_command, static) = {
  .path = "delete interface virtio",
  .short_help = "delete interface virtio "
    "{<interface> | sw_if_index <sw_idx>}",
  .function = virtio_pci_delete_command_fn,
};
/* *INDENT-ON* */

static clib_error_t *
show_virtio_pci_fn (vlib_main_t * vm, unformat_input_t * input,
		    vlib_cli_command_t * cmd)
{
  virtio_main_t *vim = &virtio_main;
  vnet_main_t *vnm = &vnet_main;
  virtio_if_t *vif;
  clib_error_t *error = 0;
  u32 hw_if_index, *hw_if_indices = 0;
  vnet_hw_interface_t *hi;
  u8 show_descr = 0, show_device_config = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat
	  (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
	{
	  hi = vnet_get_hw_interface (vnm, hw_if_index);
	  if (virtio_device_class.index != hi->dev_class_index)
	    {
	      error = clib_error_return (0, "unknown input `%U'",
					 format_unformat_error, input);
	      goto done;
	    }
	  vec_add1 (hw_if_indices, hw_if_index);
	}
      else if (unformat (input, "descriptors") || unformat (input, "desc"))
	show_descr = 1;
      else if (unformat (input, "debug-device"))
	show_device_config = 1;
      else
	{
	  error = clib_error_return (0, "unknown input `%U'",
				     format_unformat_error, input);
	  goto done;
	}
    }

  if (vec_len (hw_if_indices) == 0)
    {
      pool_foreach (vif, vim->interfaces,
		    vec_add1 (hw_if_indices, vif->hw_if_index);
	);
    }
  else if (show_device_config)
    {
      vif = pool_elt_at_index (vim->interfaces, hi->dev_instance);
      if (vif->type == VIRTIO_IF_TYPE_PCI)
	debug_device_config_space (vm, vif);
    }

  virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_PCI);

done:
  vec_free (hw_if_indices);
  return error;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (show_virtio_pci_command, static) = {
  .path = "show virtio pci",
  .short_help = "show virtio pci [<interface>] [descriptors | desc] [debug-device]",
  .function = show_virtio_pci_fn,
};
/* *INDENT-ON* */

clib_error_t *
virtio_pci_cli_init (vlib_main_t * vm)
{
  virtio_main_t *vim = &virtio_main;
  vim->log_default = vlib_log_register_class ("virtio-pci", 0);
  return 0;
}

VLIB_INIT_FUNCTION (virtio_pci_cli_init);

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
'>1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
/*
 * Copyright (c) 2018 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <vlib/vlib.h>
#include <vppinfra/string.h>

static int
test_memset_s (vlib_main_t * vm, unformat_input_t * input)
{
  u8 dst[64];
  int i;
  errno_t err;

  vlib_cli_output (vm, "Test memset_s...");

  err = memset_s (dst, ARRAY_LEN (dst), 0xfe, ARRAY_LEN (dst));

  if (err != EOK)
    return -1;

  for (i = 0; i < ARRAY_LEN (dst); i++)
    if (dst[i] != 0xFE)
      return -1;

  err = memset_s (dst, ARRAY_LEN (dst), 0xfa, ARRAY_LEN (dst) + 1);

  if (err == EOK)
    return -1;

  return 0;
}

static int
test_clib_memset (vlib_main_t * vm, unformat_input_t * input)
{
  u8 dst[64];
  int i;
  errno_t err;

  vlib_cli_output (vm, "Test clib_memset...");

  err = clib_memset (dst, 0xfe, ARRAY_LEN (dst));

  if (err != EOK)
    return -1;

  for (i = 0; i < ARRAY_LEN (dst); i++)
    if (dst[i] != 0xFE)
      return -1;

  return 0;
}

static int
test_memcpy_s (vlib_main_t * vm, unformat_input_t * input)
{
  char src[64], dst[64];
  int i;
  errno_t err;

  vlib_cli_output (vm, "Test memcpy_s...");

  for (i = 0; i < ARRAY_LEN (src); i++)
    src[i] = i + 1;

  /* Typical case */
  err = memcpy_s (dst, sizeof (dst), src, sizeof (src));

  if (err != EOK)
    return -1;

  /* This better not fail but check anyhow */
  for (i = 0; i < ARRAY_LEN (dst); i++)
    if (src[i] != dst[i])
      return -1;

  /*
   * Size test: sizeof (src) > sizeof (dst)
   * Skip this test when __builtin_constant_p (sizeof (src)) is true.
   * This is because memcpy_s_inline skips all the errors checking when the
   * the above buildin function returns true which may cause overrun problem
   * for dst buffer if this test is executed.
   */
  if (__builtin_constant_p (sizeof (src)) == 0)
    {
      err = memcpy_s (dst + 1, sizeof (dst) - 1, src, sizeof (src));

      if (err == EOK)
	return -1;
    }

  /* overlap fail */
  err = memcpy_s (dst, sizeof (dst), dst + 1, sizeof (dst) - 1);

  if (err == EOK)
    return -1;

  /* Zero length copy */
  err = memcpy_s (0, sizeof (dst), src, 0);

  if (err != EOK)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_clib_memcpy (vlib_main_t * vm, unformat_input_t * input)
{
  char src[64], dst[64];
  int i;
  errno_t err;

  vlib_cli_output (vm, "Test clib_memcpy...");

  for (i = 0; i < ARRAY_LEN (src); i++)
    src[i] = i + 1;

  /* Typical case */
  err = clib_memcpy (dst, src, sizeof (src));

  if (err != EOK)
    return -1;

  /* This better not fail but check anyhow */
  for (i = 0; i < ARRAY_LEN (dst); i++)
    if (src[i] != dst[i])
      return -1;
  /* verify it against memcpy */
  memcpy (dst, src, sizeof (src));

  /* This better not fail but check anyhow */
  for (i = 0; i < ARRAY_LEN (dst); i++)
    if (src[i] != dst[i])
      return -1;

  /* Zero length copy */
  err = clib_memcpy (0, src, 0);

  if (err != EOK)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_memcmp_s (vlib_main_t * vm, unformat_input_t * input)
{
  char src[64], dst[64];
  errno_t err;
  int diff = 0;

  vlib_cli_output (vm, "Test memcmp_s...");

  /* Fill array with different values */
  err = clib_memset (src, 0x1, ARRAY_LEN (src));
  if (err != EOK)
    return -1;
  err = clib_memset (dst, 0x3, ARRAY_LEN (dst));
  if (err != EOK)
    return -1;

  /* s1 > s2, > 0 is expected in diff */
  err = memcmp_s (dst, ARRAY_LEN (dst), src, ARRAY_LEN (src), &diff);
  if (err != EOK)
    return -1;
  if (!(diff > 0))
    return -1;

  /* s1 < s2, < 0 is expected in diff */
  err = memcmp_s (src, ARRAY_LEN (src), dst, ARRAY_LEN (dst), &diff);
  if (err != EOK)
    return -1;
  if (!(diff < 0))
    return -1;

  err = clib_memset (dst, 0x1, ARRAY_LEN (dst));
  if (err != EOK)
    return -1;

  /* s1 == s2, 0 is expected in diff */
  err = memcmp_s (src, ARRAY_LEN (src), dst, ARRAY_LEN (dst), &diff);
  if (err != EOK)
    return -1;
  if (diff != 0)
    return -1;

  /* Try negative tests */
  err = memcmp_s (0, 0, 0, 0, 0);
  if (err != EINVAL)
    return -1;

  /* Try s2max > s1max */
  err = memcmp_s (src, ARRAY_LEN (src) - 1, dst, ARRAY_LEN (dst), &diff);
  if (err != EINVAL)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_clib_memcmp (vlib_main_t * vm, unformat_input_t * input)
{
  char src[64], dst[64];
  errno_t err;
  char *s;

  vlib_cli_output (vm, "Test clib_memcmp...");

  /* Fill array with different values */
  err = clib_memset (src, 0x1, ARRAY_LEN (src));
  if (err != EOK)
    return -1;
  err = clib_memset (dst, 0x3, ARRAY_LEN (dst));
  if (err != EOK)
    return -1;

  /* s1 > s2, > 0 is expected in diff */
  if (!(clib_memcmp (dst, src, ARRAY_LEN (src)) > 0))
    return -1;
  /* verify it against memcmp */
  if (!(memcmp (dst, src, ARRAY_LEN (src)) > 0))
    return -1;

  /* s1 < s2, < 0 is expected in diff */
  if (!(clib_memcmp (src, dst, ARRAY_LEN (dst)) < 0))
    return -1;
  /* verify it against memcmp */
  if (!(memcmp (src, dst, ARRAY_LEN (dst)) < 0))
    return -1;

  err = clib_memset (dst, 0x1, ARRAY_LEN (dst));
  if (err != EOK)
    return -1;

  /* s1 == s2, 0 is expected in diff */
  if (clib_memcmp (src, dst, ARRAY_LEN (dst)) != 0)
    return -1;
  /* verify it against memcmp */
  if (memcmp (src, dst, ARRAY_LEN (dst)) != 0)
    return -1;

  /* Try negative tests */
  s = 0;
  if (clib_memcmp (s, s, 0) != 0)
    return -1;
  /* verify it against memcmp */
  if (memcmp (s, s, 0) != 0)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_strcmp_s (vlib_main_t * vm, unformat_input_t * input)
{
  char s1[] = "Simplicity is the ultimate sophistication";
  uword s1len = sizeof (s1) - 1;	// excluding null
  errno_t err;
  int indicator = 0;

  vlib_cli_output (vm, "Test strcmp_s...");

  /* s1 == s2, 0 is expected */
  err = strcmp_s (s1, s1len, "Simplicity is the ultimate sophistication",
		  &indicator);
  if (err != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* s1 > s2, > 0 is expected */
  err = strcmp_s (s1, s1len, "Simplicity is the ultimate", &indicator);
  if (err != EOK)
    return -1;
  if (!(indicator > 0))
    return -1;

  /* s1 < s2, < 0 is expected */
  err = strcmp_s (s1, s1len, "Simplicity is the ultimate sophistication!",
		  &indicator);
  if (err != EOK)
    return -1;
  if (!(indicator < 0))
    return -1;

  /* Try some negative tests */

  /* Null pointers test */
  err = strcmp_s (0, 0, 0, 0);
  if (err != EINVAL)
    return -1;

  /* non-null terminated s1 */
  s1[s1len] = 0x1;
  err = strcmp_s (s1, s1len, "Simplicity is the ultimate sophistication",
		  &indicator);
  if (err != EINVAL)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_clib_strcmp (vlib_main_t * vm, unformat_input_t * input)
{
  char s1[] = "Simplicity is the ultimate sophistication";
  int indicator;
  char *s;

  vlib_cli_output (vm, "Test clib_strcmp...");

  /* s1 == s2, 0 is expected */
  indicator = clib_strcmp (s1, "Simplicity is the ultimate sophistication");
  if (indicator != 0)
    return -1;
  /* verify it against strcmp */
  indicator = strcmp (s1, "Simplicity is the ultimate sophistication");
  if (indicator != 0)
    return -1;

  /* s1 > s2, > 0 is expected */
  indicator = clib_strcmp (s1, "Simplicity is the ultimate");
  if (!(indicator > 0))
    return -1;
  /* verify it against strcmp */
  indicator = strcmp (s1, "Simplicity is the ultimate");
  if (!(indicator > 0))
    return -1;

  /* s1 < s2, < 0 is expected */
  indicator = clib_strcmp (s1, "Simplicity is the ultimate sophistication!");
  if (!(indicator < 0))
    return -1;
  /* verify it against strcmp */
  indicator = strcmp (s1, "Simplicity is the ultimate sophistication!");
  if (!(indicator < 0))
    return -1;

  /* Try some negative tests */

  /* Null pointers comparison */
  s = 0;
  indicator = clib_strcmp (s, s);
  if (indicator != 0)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_strncmp_s (vlib_main_t * vm, unformat_input_t * input)
{
  char s1[] = "Every moment is a fresh beginning";
  uword s1len = sizeof (s1) - 1;	// excluding null
  errno_t err;
  int indicator = 0;

  vlib_cli_output (vm, "Test strncmp_s...");

  /* s1 == s2, 0 is expected */
  err = strncmp_s (s1, s1len, "Every moment is a fresh beginning", s1len,
		   &indicator);
  if (err != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* s1 > s2, 0 is expected since comparison is no more than n character */
  err = strncmp_s (s1, s1len, "Every moment is a fresh begin",
		   sizeof ("Every moment is a fresh begin") - 1, &indicator);
  if (err != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* s1 < s2, < 0 is expected */
  err = strncmp_s (s1, s1len, "Every moment is fresh beginning",
		   sizeof ("Every moment is fresh beginning") - 1,
		   &indicator);
  if (err != EOK)
    return -1;
  if (!(indicator < 0))
    return -1;

  /* s1 > s2, > 0 is expected */
  err = strncmp_s ("Every moment is fresh beginning. ",
		   sizeof ("Every moment is fresh beginning. ") - 1, s1,
		   s1len, &indicator);
  if (err != EOK)
    return -1;
  if (!(indicator > 0))
    return -1;

  /* Try some negative tests */

  /* Null pointers */
  err = strncmp_s (0, 0, 0, 0, 0);
  if (err != EINVAL)
    return -1;

  /* n > s1max */
  err = strncmp_s (s1, s1len, "Every moment is a fresh beginning", s1len + 1,
		   &indicator);
  if (err != EINVAL)
    return -1;

  /* unterminated s1 */
  s1[s1len] = 0x1;
  err = strncmp_s (s1, s1len, "Every moment is a fresh beginning",
		   sizeof ("Every moment is a fresh beginning") - 1,
		   &indicator);
  if (err != EINVAL)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_clib_strncmp (vlib_main_t * vm, unformat_input_t * input)
{
  char s1[] = "Every moment is a fresh beginning";
  uword s1len = sizeof (s1) - 1;	// excluding null
  int indicator, v_indicator;

  vlib_cli_output (vm, "Test clib_strncmp...");

  /* s1 == s2, 0 is expected */
  indicator = clib_strncmp (s1, "Every moment is a fresh beginning", s1len);
  if (indicator != 0)
    return -1;
  /* verify it against strncmp */
  v_indicator = strncmp (s1, "Every moment is a fresh beginning", s1len);
  if (v_indicator != 0)
    return -1;

  /* s1 > s2, 0 is expected since comparison is no more than n character */
  indicator = clib_strncmp (s1, "Every moment is a fresh begin",
			    sizeof ("Every moment is a fresh begin") - 1);
  if (indicator != 0)
    return -1;
  /* verify it against strncmp */
  v_indicator = strncmp (s1, "Every moment is a fresh begin",
			 sizeof ("Every moment is a fresh begin") - 1);
  if (v_indicator != 0)
    return -1;

  /* s1 < s2, < 0 is expected */
  indicator = clib_strncmp (s1, "Every moment is fresh beginning",
			    sizeof ("Every moment is fresh beginning") - 1);
  if (!(indicator < 0))
    return -1;
  /* verify it against strncmp */
  v_indicator = strncmp (s1, "Every moment is fresh beginning",
			 sizeof ("Every moment is fresh beginning") - 1);
  if (!(v_indicator < 0))
    return -1;
  if (v_indicator != indicator)
    return -1;

  /* s1 > s2, > 0 is expected */
  indicator = clib_strncmp ("Every moment is fresh beginning. ", s1, s1len);
  if (!(indicator > 0))
    return -1;
  /* verify it against strncmp */
  v_indicator = strncmp ("Every moment is fresh beginning. ", s1, s1len);
  if (!(v_indicator > 0))
    return -1;
  if (v_indicator != indicator)
    return -1;

  /* Try some negative tests */

  /* Null pointers */

  /* make sure we don't crash */
  indicator = clib_strncmp (0, 0, 0);
  if (indicator != EOK)
    return -1;

  /* n > s1 len */
  indicator =
    clib_strncmp (s1, "Every moment is a fresh beginning", s1len + 1);
  if (indicator != 0)
    return -1;
  /* verify it against strncmp */
  v_indicator = strncmp (s1, "Every moment is a fresh beginning", s1len + 1);
  if (v_indicator != 0)
    return -1;

  /* unterminated s1 */
  s1[s1len] = 0x1;
  CLIB_MEM_UNPOISON (s1, CLIB_STRING_MACRO_MAX);
  indicator = clib_strncmp (s1, "Every moment is a fresh beginning",
			    sizeof ("every moment is a fresh beginning") - 1);
  if (indicator != 0)
    return -1;
  /* verify it against strncmp */
  v_indicator = strncmp (s1, "Every moment is a fresh beginning",
			 sizeof ("Every moment is a fresh beginning") - 1);
  if (v_indicator != 0)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_strcpy_s (vlib_main_t * vm, unformat_input_t * input)
{
  char src[] = "To err is human.";
  char dst[64];
  int indicator;
  size_t s1size = sizeof (dst);	// including null
  errno_t err;

  vlib_cli_output (vm, "Test strcpy_s...");

  err = strcpy_s (dst, s1size, src);
  if (err != EOK)
    return -1;

  /* This better not fail but check anyhow */
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
      EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* Negative tests */

  err = strcpy_s (0, 0, 0);
  if (err == EOK)
    return -1;

  /* Size fail */
  err = strcpy_s (dst, 10, src);
  if (err == EOK)
    return -1;

  /* overlap fail */
#if __GNUC__ < 8
  /* GCC 8 flunks this one at compile time... */
  err = strcpy_s (dst, s1size, dst);
  if (err == EOK)
    return -1;
#endif

  /* overlap fail */
  err = strcpy_s (dst, s1size, dst + 1);
  if (err == EOK)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_strncpy_s (vlib_main_t * vm, unformat_input_t * input)
{
  char src[] = "Those who dare to fail miserably can achieve greatly.";
  char dst[100], old_dst[100];
  int indicator, i;
  size_t s1size = sizeof (dst);	// including null
  errno_t err;

  vlib_cli_output (vm, "Test strncpy_s...");

  /* dmax includes null, n excludes null */

  /* n == string len of src */
  err = strncpy_s (dst, s1size, src, clib_strnlen (src, sizeof (src)));
  if (err != EOK)
    return -1;
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
      EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* limited copy -- strlen src > n, copy up to n */
  err = strncpy_s (dst, s1size, "The price of greatness is responsibility.",
		   10);
  if (err != EOK)
    return -1;
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), "The price ",
		&indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* n > string len of src */
  err = clib_memset (dst, 1, sizeof (dst));
  if (err != EOK)
    return -1;

  err = strncpy_s (dst, s1size, src, clib_strnlen (src, sizeof (src)) + 10);
  if (err != EOK)
    return -1;
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
      EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* Make sure bytes after strlen(dst) is untouched */
  for (i = 1 + clib_strnlen (dst, sizeof (dst)); i < sizeof (dst); i++)
    if (dst[i] != 1)
      return -1;

  /* truncation, n >= dmax */
  err = strncpy_s (dst, clib_strnlen (src, sizeof (src)), src,
		   clib_strnlen (src, sizeof (src)));
  if (err != EOVERFLOW)
    return -1;

  /* Check dst content */
  if (dst[strlen (dst)] != '\0')
    return -1;
  if (strncmp_s (dst, clib_strnlen (dst, sizeof (dst)), src,
		 clib_strnlen (dst, sizeof (dst)), &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* zero length copy */
  clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
  err = strncpy_s (dst, sizeof (dst), src, 0);
  if (err != EOK)
    return -1;
  /* verify dst is untouched */
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), old_dst, &indicator) !=
      EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* Negative tests */

  err = strncpy_s (0, 0, 0, 1);
  if (err == EOK)
    return -1;

  /* overlap fail */
  err = strncpy_s (dst, s1size, dst + 1, s1size - 1);
  if (err == EOK)
    return -1;

  /* overlap fail */
#if __GNUC__ < 8
  /* GCC 8 flunks this one at compile time... */
  err = strncpy_s (dst, s1size, dst, s1size);
  if (err == EOK)
    return -1;
#endif

  /* OK, seems to work */
  return 0;
}

static int
test_clib_strncpy (vlib_main_t * vm, unformat_input_t * input)
{
  char src[] = "Those who dare to fail miserably can achieve greatly.";
  char dst[100], old_dst[100];
  int indicator;
  size_t s1size = sizeof (dst);	// including null
  errno_t err;

  vlib_cli_output (vm, "Test clib_strncpy...");

  /* n == string len of src */
  err = clib_strncpy (dst, src, clib_strnlen (src, sizeof (src)));
  if (err != EOK)
    return -1;

  /* This better not fail but check anyhow */
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
      EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* Verify it against strncpy */
#if __GNUC__ < 8
  /* GCC 8 debian flunks this one at compile time */
  strncpy (dst, src, strlen (src));

  /* This better not fail but check anyhow */
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
      EOK)
    return -1;
  if (indicator != 0)
    return -1;
#endif

  /* limited copy -- strlen src > n, copy up to n */
  err = clib_strncpy (dst, "The price of greatness is responsibility.", 10);
  if (err != EOK)
    return -1;
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), "The price ",
		&indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  /* verify it against strncpy */
  memset_s (dst, sizeof (dst), 0, sizeof (dst));

#if __GNUC__ < 8
  /* GCC 8 flunks this one at compile time... */
  strncpy (dst, "The price of greatness is responsibility.", 10);
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), "The price ",
		&indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
#endif

  /* n > string len of src */
  err = clib_strncpy (dst, src, clib_strnlen (src, sizeof (src)) + 10);
  if (err != EOK)
    return -1;
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
      EOK)
    return -1;
  if (indicator != 0)
    return -1;
  /* Verify it against strncpy */
#if __GNUC__ < 8
  /* GCC 8 debian flunks this one at compile time */
  strncpy (dst, src, strlen (src));
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), src, &indicator) !=
      EOK)
    return -1;
  if (indicator != 0)
    return -1;
#endif

  /* zero length copy */
  clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
  err = clib_strncpy (dst, src, 0);
  if (err != EOK)
    return -1;
  /* verify dst is untouched */
  if (strcmp_s (dst, clib_strnlen (dst, sizeof (dst)), old_dst, &indicator) !=
      EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* Negative tests */

  err = clib_strncpy (0, 0, 1);
  if (err == EOK)
    return -1;

  /* overlap fail */
  err = clib_strncpy (dst, dst + 1, s1size);
  if (err == EOK)
    return -1;

  /* overlap fail */
#if __GNUC__ < 8
  /* GCC 8 flunks this one at compile time... */
  err = clib_strncpy (dst, dst, s1size);
  if (err == EOK)
    return -1;
#endif

  /* OK, seems to work */
  return 0;
}

static int
test_strcat_s (vlib_main_t * vm, unformat_input_t * input)
{
  char src[100], dst[100], old_dst[100];
  size_t s1size = sizeof (dst);	// including null
  errno_t err;
  int indicator;

  vlib_cli_output (vm, "Test strcat_s...");

  strcpy_s (dst, sizeof (dst), "Tough time never last ");
  strcpy_s (src, sizeof (src), "but tough people do");
  err = strcat_s (dst, s1size, src);
  if (err != EOK)
    return -1;
  if (strcmp_s (dst, s1size - 1,
		"Tough time never last but tough people do",
		&indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* empty string concatenation */
  clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
  err = strcat_s (dst, s1size, "");
  if (err != EOK)
    return -1;
  /* verify dst is untouched */
  if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* negative stuff */
  err = strcat_s (0, 0, 0);
  if (err != EINVAL)
    return -1;

  /* overlap fail */
  err = strcat_s (dst, s1size, dst + 1);
  if (err != EINVAL)
    return -1;

  /* overlap fail */
#if __GNUC__ < 8
  /* GCC 8 flunks this one at compile time... */
  err = strcat_s (dst, s1size, dst);
  if (err != EINVAL)
    return -1;
#endif

  /* not enough space for dst */
  err = strcat_s (dst, 10, src);
  if (err != EINVAL)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_strncat_s (vlib_main_t * vm, unformat_input_t * input)
{
  char src[100], dst[100], old_dst[100];
  size_t s1size = sizeof (dst);	// including null
  errno_t err;
  char s1[] = "Two things are infinite: ";
  char s2[] = "the universe and human stupidity; ";
  char s3[] = "I am not sure about the universe.";
  int indicator;

  vlib_cli_output (vm, "Test strncat_s...");

  strcpy_s (dst, sizeof (dst), s1);
  strcpy_s (src, sizeof (src), s2);
  err = strncat_s (dst, s1size, src, clib_strnlen (src, sizeof (src)));
  if (err != EOK)
    return -1;
  if (strcmp_s (dst, s1size - 1,
		"Two things are infinite: the universe and human stupidity; ",
		&indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* truncation, n >= dmax - strnlen_s (dst, dmax) */
  err = strncat_s (dst, clib_strnlen (dst, sizeof (dst)) +
		   clib_strnlen (s3, sizeof (s3)), s3,
		   clib_strnlen (s3, sizeof (s3)));
  if (err != EOVERFLOW)
    return -1;
  /*
   * resulting string is dst + strlen (s3) - 1 characters + null.
   * notice the "." is missing at the end of the resulting string because
   * the space is needed to accommodate the null
   * Notice strcmp_s will check s1 or dst to make sure it is null terminated
   */
  if (strcmp_s (dst, s1size - 1,
		"Two things are infinite: the universe and human stupidity; "
		"I am not sure about the universe", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* n > strlen src */
  strcpy_s (dst, sizeof (dst), s1);
  err = strncat_s (dst, s1size, src, clib_strnlen (src, sizeof (src)) + 10);
  if (err != EOK)
    return -1;
  if (strcmp_s (dst, s1size - 1,
		"Two things are infinite: the universe and human stupidity; ",
		&indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* zero length strncat */
  clib_strncpy (old_dst, dst, clib_strnlen (dst, sizeof (dst)));
  err = strncat_s (dst, sizeof (dst), src, 0);
  if (err != EOK)
    return -1;
  /* verify dst is untouched */
  if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* empty string, wrong n concatenation */
  err = strncat_s (dst, sizeof (dst), "", 10);
  if (err != EOK)
    return -1;
  /* verify dst is untouched */
  if (strcmp_s (dst, s1size - 1, old_dst, &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* limited concatenation, string > n, copy up to n */
  strcpy_s (dst, sizeof (dst), s1);
  err = strncat_s (dst, s1size, s2, 13);
  if (err != EOK)
    return -1;
  if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
		&indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  /* verify it against strncat */
#if __GNUC__ < 8
  /* GCC 8 debian flunks this one at compile time */
  strcpy_s (dst, sizeof (dst), s1);
  strncat (dst, s2, 13);
  if (strcmp_s (dst, s1size - 1, "Two things are infinite: the universe ",
		&indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
#endif

  /* negative stuff */
  err = strncat_s (0, 0, 0, 1);
  if (err != EINVAL)
    return -1;

  /* no room for dst -- dmax - strnlen_s (dst, dmax) == 0 */
  err = strncat_s (dst, clib_strnlen (dst, sizeof (dst)), s2,
		   clib_strnlen (s2, sizeof (s2)));
  if (err != EINVAL)
    return -1;

  /* overlap fail */
  err = strncat_s (dst, s1size, dst + 1, clib_strnlen (dst + 1, s1size - 1));
  if (err != EINVAL)
    return -1;

  /* overlap fail */
#if __GNUC__ < 8
  /* GCC 8 flunks this one at compile time... */
  err = strncat_s (dst, s1size, dst, clib_strnlen (dst, sizeof (dst)));
  if (err != EINVAL)
    return -1;
#endif

  /* OK, seems to work */
  return 0;
}

static int
test_strtok_s (vlib_main_t * vm, unformat_input_t * input)
{
  int indicator;
  char *tok, *ptr;
  char str2[20];
  char str1[40];
  uword len;
  char *p2str = 0;
  char *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7;

  vlib_cli_output (vm, "Test strtok_s...");
  strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
  len = strnlen_s (str1, sizeof (str1));
  tok1 = strtok_s (str1, &len, " ", &p2str);
  tok2 = strtok_s (0, &len, " ", &p2str);
  tok3 = strtok_s (0, &len, " ", &p2str);
  tok4 = strtok_s (0, &len, " ", &p2str);
  tok5 = strtok_s (0, &len, " ", &p2str);
  tok6 = strtok_s (0, &len, " ", &p2str);
  tok7 = strtok_s (0, &len, " ", &p2str);
  if ((tok1 == 0) ||
      strcmp_s (tok1, strlen (tok1), "brevity", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok2 == 0) || strcmp_s (tok2, strlen (tok2), "is", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok3 == 0) || strcmp_s (tok3, strlen (tok3), "the", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok4 == 0)
      || strcmp_s (tok4, strlen (tok4), "soul", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok5 == 0) || strcmp_s (tok5, strlen (tok5), "of", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok6 == 0) || strcmp_s (tok6, strlen (tok6), "wit", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if (tok7 != 0)
    return -1;

  /* delimiter not present in the string -- the whole string is returned */
  strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
  len = strnlen_s (str1, sizeof (str1) - 1);
  p2str = 0;
  tok1 = strtok_s (str1, &len, ",", &p2str);
  if ((tok1 == 0) || strcmp_s (tok1, strlen (tok1), str1, &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* negative stuff */
  tok = strtok_s (0, 0, 0, 0);
  if (tok != 0)
    return -1;

  /* s1 and ptr contents are null */
  ptr = 0;
  tok = strtok_s (0, 0, 0, &ptr);
  if (tok != 0)
    return -1;

  /* unterminate s1 */
  p2str = 0;
  len = strnlen_s (str1, sizeof (str1) - 1);
  str1[strlen (str1)] = 0x2;
  tok = strtok_s (str1, &len, ",", &p2str);
  if (tok != 0)
    return -1;

  /*
   * unterminated s2. This test case in not perfect because there is no
   * argument for s2max. But s2 len is limited to 16 characters. If the API
   * does not find the null character at s2[15], it declares the string s2
   * as unterminated.
   */
  memset_s (str2, sizeof (str2), 0xfa, sizeof (str2));
  tok = strtok_s (str1, &len, str2, &p2str);
  if (tok != 0)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_clib_strtok (vlib_main_t * vm, unformat_input_t * input)
{
  int indicator;
  char *s1 __attribute__ ((unused));
  char *tok __attribute__ ((unused));
  char *ptr __attribute__ ((unused));
  char str1[40];
  char *p2str;
  char *tok1, *tok2, *tok3, *tok4, *tok5, *tok6, *tok7;

  vlib_cli_output (vm, "Test clib_strtok...");
  strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
  p2str = 0;
  tok1 = clib_strtok (str1, " ", &p2str);
  tok2 = clib_strtok (0, " ", &p2str);
  tok3 = clib_strtok (0, " ", &p2str);
  tok4 = clib_strtok (0, " ", &p2str);
  tok5 = clib_strtok (0, " ", &p2str);
  tok6 = clib_strtok (0, " ", &p2str);
  tok7 = clib_strtok (0, " ", &p2str);
  if ((tok1 == 0) ||
      strcmp_s (tok1, strlen (tok1), "brevity", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok2 == 0) || strcmp_s (tok2, strlen (tok2), "is", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok3 == 0) || strcmp_s (tok3, strlen (tok3), "the", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok4 == 0)
      || strcmp_s (tok4, strlen (tok4), "soul", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok5 == 0) || strcmp_s (tok5, strlen (tok5), "of", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok6 == 0) || strcmp_s (tok6, strlen (tok6), "wit", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if (tok7 != 0)
    return -1;
  /* verify it againest strtok_r */
  strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
  p2str = 0;
  tok1 = strtok_r (str1, " ", &p2str);
  tok2 = strtok_r (0, " ", &p2str);
  tok3 = strtok_r (0, " ", &p2str);
  tok4 = strtok_r (0, " ", &p2str);
  tok5 = strtok_r (0, " ", &p2str);
  tok6 = strtok_r (0, " ", &p2str);
  tok7 = strtok_r (0, " ", &p2str);
  if ((tok1 == 0) ||
      strcmp_s (tok1, strlen (tok1), "brevity", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok2 == 0) || strcmp_s (tok2, strlen (tok2), "is", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok3 == 0) || strcmp_s (tok3, strlen (tok3), "the", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok4 == 0)
      || strcmp_s (tok4, strlen (tok4), "soul", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok5 == 0) || strcmp_s (tok5, strlen (tok5), "of", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if ((tok6 == 0) || strcmp_s (tok6, strlen (tok6), "wit", &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  if (tok7 != 0)
    return -1;

  /* delimiter not present in the string -- the whole string is returned */
  strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
  p2str = 0;
  tok1 = clib_strtok (str1, ",", &p2str);
  if ((tok1 == 0) || strcmp_s (tok1, strlen (tok1), str1, &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;
  /* verify it against strtok_r */
  strcpy_s (str1, sizeof (str1), "brevity is the soul of wit");
  p2str = 0;
  tok1 = strtok_r (str1, ",", &p2str);
  if ((tok1 == 0) || strcmp_s (tok1, strlen (tok1), str1, &indicator) != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* negative stuff */
  s1 = 0;
  ptr = 0;
#if __GNUC__ < 8
  /* GCC 8 flunks this one at compile time... */
  tok = clib_strtok (s1, s1, (char **) 0);
  if (tok != 0)
    return -1;

  /* s1 and ptr contents are null */
  tok = clib_strtok (s1, s1, &ptr);
  if (tok != 0)
    return -1;
#endif

  /* verify it against strtok_r */
  /* No can do. This causes a crash in strtok_r */
  // tok = strtok_r (s1, " ", &ptr);
  // if (tok != 0)
  //  return -1;

  /*
   * Can't test unterminated string s1 and s2 becuase clib_strtok does not
   * supply s1 and s2 max
   */

  /* OK, seems to work */
  return 0;
}

static int
test_strnlen_s (vlib_main_t * vm, unformat_input_t * input)
{
  const char s1[] = "Truth is incontrovertible";
  size_t len;

  vlib_cli_output (vm, "Test strnlen_s...");

  len = strnlen_s (s1, sizeof (s1));
  if (len != sizeof (s1) - 1)
    return -1;

  len = strnlen_s (s1, 5);
  if (len != 5)
    return -1;

  /* negative stuff */
  len = strnlen_s (0, 0);
  if (len != 0)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_clib_strnlen (vlib_main_t * vm, unformat_input_t * input)
{
  const char s1[] = "Truth is incontrovertible";
  size_t len;

  vlib_cli_output (vm, "Test clib_strnlen...");

  len = clib_strnlen (s1, sizeof (s1));
  if (len != sizeof (s1) - 1)
    return -1;

  len = clib_strnlen (s1, 5);
  if (len != 5)
    return -1;

  /* negative stuff */
  len = clib_strnlen (0, 0);
  if (len != 0)
    return -1;

  /* OK, seems to work */
  return 0;
}

static int
test_strstr_s (vlib_main_t * vm, unformat_input_t * input)
{
  errno_t err;
  char *sub = 0;
  char s1[64];
  size_t s1len = sizeof (s1) - 1;	// excluding null
  int indicator;

  vlib_cli_output (vm, "Test strstr_s...");

  /* substring not present */
  strcpy_s (s1, s1len, "success is not final, failure is not fatal.");
  err = strstr_s (s1, s1len, "failures", sizeof ("failures"), &sub);;
  if (err != ESRCH)
    return -1;

  /* substring present */
  err = strstr_s (s1, s1len, "failure", sizeof ("failure"), &sub);
  if (err != EOK)
    return -1;

  if ((sub == 0) ||
      strcmp_s (sub, strlen (sub), "failure is not fatal.", &indicator)
      != EOK)
    return -1;
  if (indicator != 0)
    return -1;

  /* negative stuff */

  /* Null pointers test */
  err = strstr_s (0, 0, 0, 0, 0);
  if (err != EINVAL)
    return -1;

  /* unterminated s1 and s2 */
  memset_s (s1, ARRAY_LEN (s1), 0xfe, ARRAY_LEN (s1));
  err = strstr_s (s1, s1len, s1, s1len, &sub);
  if (err != EINVAL)
    return -1;

  /* OK, seems to work */
  return 0;
}

#define foreach_string_test                                                   \
  _ (0, MEMCPY_S, "memcpy_s", memcpy_s)                                       \
  _ (1, CLIB_MEMCPY, "clib_memcpy", clib_memcpy)                              \
  _ (2, MEMSET_S, "memset_s", memset_s)                                       \
  _ (3, CLIB_MEMSET, "clib_memset", clib_memset)                              \
  _ (4, MEMCMP_S, "memcmp_s", memcmp_s)                                       \
  _ (5, CLIB_MEMCMP, "clib_memcmp", clib_memcmp)                              \
  _ (6, STRCMP_S, "strcmp_s", strcmp_s)                                       \
  _ (7, CLIB_STRCMP, "clib_strcmp", clib_strcmp)                              \
  _ (8, STRNCMP_S, "strncmp_s", strncmp_s)                                    \
  _ (9, CLIB_STRNCMP, "clib_strncmp", clib_strncmp)                           \
  _ (10, STRCPY_S, "strcpy_s", strcpy_s)                                      \
  _ (11, STRNCPY_S, "strncpy_s", strncpy_s)                                   \
  _ (12, CLIB_STRNCPY, "clib_strncpy", clib_strncpy)                          \
  _ (13, STRCAT_S, "strcat_s", strcat_s)                                      \
  _ (14, STRNCAT_S, "strncat_s", strncat_s)                                   \
  _ (15, STRTOK_S, "strtok_s", strtok_s)                                      \
  _ (16, CLIB_STRTOK, "clib_strtok", clib_strtok)                             \
  _ (17, STRNLEN_S, "strnlen_s", strnlen_s)                                   \
  _ (18, CLIB_STRNLEN, "clib_strnlen", clib_strnlen)                          \
  _ (19, STRSTR_S, "strstr_s", strstr_s)

typedef enum
{
#define _(v,f,s,p) STRING_TEST_##f = v,
  foreach_string_test
#undef _
#define STRING_TEST_FIRST       STRING_TEST_MEMCPY_S
#define STRING_TEST_LAST	STRING_TEST_STRSTR_S
} string_test_t;

static uword
unformat_string_test (unformat_input_t * input, va_list * args)
{
  u8 *r = va_arg (*args, u8 *);

  if (0)
    ;
#define _(v,f,s,p) else if (unformat (input, s)) *r = STRING_TEST_##f;
  foreach_string_test
#undef _
    else
    return 0;

  return 1;
}

typedef int (*string_test_func) (vlib_main_t * vm, unformat_input_t * input);

typedef struct
{
  string_test_func test;
} string_test_func_t;

static clib_error_t *
string_test_command_fn (vlib_main_t * vm,
			unformat_input_t * input,
			vlib_cli_command_t * cmd_arg)
{
  string_test_func_t string_func[] = {
#define _(v,f,s,p) { test_##p },
    foreach_string_test
#undef _
  };
  const char *string_table[] = {
#define _(v,f,s,p) s,
    foreach_string_test
#undef _
  };
  int res = 0, ok;
  i8 specific_test = ~0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U", unformat_string_test, &specific_test))
	break;
      else
	return clib_error_return (0, "unknown input `%U'",
				  format_unformat_error, input);
    }

  if (specific_test == ~0)
    {
      for (specific_test = STRING_TEST_FIRST;
	   specific_test <= STRING_TEST_LAST; specific_test++)
	{
	  ok = (string_func[specific_test]).test (vm, input);
	  res += ok;
	  if (ok != 0)
	    vlib_cli_output (vm, "test_%s failed",
			     string_table[specific_test]);
	}
    }
  else
    res = (string_func[specific_test]).test (vm, input);
  if (res)
    vlib_cli_output (vm, "String unit test(s) failed...");
  else
    vlib_cli_output (vm, "String unit test(s) OK...");
  return 0;
}

/* *INDENT-OFF* */
VLIB_CLI_COMMAND (string_test_command, static) = {
  .path = "test string",
  .short_help =
    "test string [memcpy_s | clib_memcpy | memset_s | "
    "clib_memset | memcmp_s | clib_memcmp | strcmp_s | clib_strcmp | "
    "strncmp_s | clib_strncmp | strcpy_s | strncpy_s | "
    "clib_strncpy | strcat_s | strncat_s | "
    "strtok_s |  clib_strtok | strnlen_s | clib_strnlen | strstr_s ]",
  .function = string_test_command_fn,
};
/* *INDENT-ON* */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */