summaryrefslogtreecommitdiffstats
path: root/src/plugins/unittest
diff options
context:
space:
mode:
authorDave Barach <dave@barachs.net>2018-10-17 10:38:51 -0400
committerDamjan Marion <dmarion@me.com>2018-10-23 13:06:46 +0000
commitb7b929931a07fbb27b43d5cd105f366c3e29807e (patch)
tree438681c89738802dbb5d339715b96ea2c31bafb4 /src/plugins/unittest
parentb9a4c445c1d4e9cdab476a8e1fb8a46ff0fc6080 (diff)
c11 safe string handling support
Change-Id: Ied34720ca5a6e6e717eea4e86003e854031b6eab Signed-off-by: Dave Barach <dave@barachs.net>
Diffstat (limited to 'src/plugins/unittest')
-rw-r--r--src/plugins/unittest/CMakeLists.txt1
-rw-r--r--src/plugins/unittest/string_test.c145
-rw-r--r--src/plugins/unittest/tcp_test.c16
3 files changed, 154 insertions, 8 deletions
diff --git a/src/plugins/unittest/CMakeLists.txt b/src/plugins/unittest/CMakeLists.txt
index f790a23a60e..2dfa8379c52 100644
--- a/src/plugins/unittest/CMakeLists.txt
+++ b/src/plugins/unittest/CMakeLists.txt
@@ -16,4 +16,5 @@ add_vpp_plugin(unittest
unittest.c
tcp_test.c
bihash_test.c
+ string_test.c
)
diff --git a/src/plugins/unittest/string_test.c b/src/plugins/unittest/string_test.c
new file mode 100644
index 00000000000..a5caa9094f0
--- /dev/null
+++ b/src/plugins/unittest/string_test.c
@@ -0,0 +1,145 @@
+/*
+ * 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_clib_memset (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_memcpy (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 fail */
+ 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 clib_error_t *
+string_test_command_fn (vlib_main_t * vm,
+ unformat_input_t * input,
+ vlib_cli_command_t * cmd_arg)
+{
+ int res = 0;
+ u8 t_memcpy = 0;
+ u8 t_clib_memset = 0;
+ u8 specific_test;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "memcpy_s"))
+ t_memcpy = 1;
+ else if (unformat (input, "memset_s"))
+ t_clib_memset = 1;
+ break;
+ }
+
+ specific_test = t_memcpy + t_clib_memset;
+
+ if (specific_test == 0)
+ {
+ res = test_memcpy (vm, input);
+ res += test_clib_memset (vm, input);
+ goto done;
+ }
+
+ if (t_memcpy)
+ res = test_memcpy (vm, input);
+ else if (t_clib_memset)
+ res = test_clib_memset (vm, input);
+
+done:
+ 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 = "string library tests",
+ .function = string_test_command_fn,
+};
+/* *INDENT-ON* */
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */
diff --git a/src/plugins/unittest/tcp_test.c b/src/plugins/unittest/tcp_test.c
index d06578771c0..3bde8054901 100644
--- a/src/plugins/unittest/tcp_test.c
+++ b/src/plugins/unittest/tcp_test.c
@@ -83,7 +83,7 @@ tcp_test_sack_rx (vlib_main_t * vm, unformat_input_t * input)
return tcp_test_scoreboard_replay (vm, input);
}
- memset (tc, 0, sizeof (*tc));
+ clib_memset (tc, 0, sizeof (*tc));
tc->snd_una = 0;
tc->snd_una_max = 1000;
@@ -458,7 +458,7 @@ tcp_test_sack_tx (vlib_main_t * vm, unformat_input_t * input)
}
}
- memset (tc, 0, sizeof (*tc));
+ clib_memset (tc, 0, sizeof (*tc));
/*
* Add odd sack block pairs
@@ -740,7 +740,7 @@ fifo_prepare (u32 fifo_size)
f = svm_fifo_create (fifo_size);
/* Paint fifo data vector with -1's */
- memset (f->data, 0xFF, fifo_size);
+ clib_memset (f->data, 0xFF, fifo_size);
return f;
}
@@ -1647,11 +1647,11 @@ tcp_test_lookup (vlib_main_t * vm, unformat_input_t * input)
* Allocate fake session and connection 1
*/
pool_get (smm->sessions[0], s);
- memset (s, 0, sizeof (*s));
+ clib_memset (s, 0, sizeof (*s));
s->session_index = sidx = s - smm->sessions[0];
pool_get (tm->connections[0], tc);
- memset (tc, 0, sizeof (*tc));
+ clib_memset (tc, 0, sizeof (*tc));
tc->connection.c_index = tc - tm->connections[0];
tc->connection.s_index = s->session_index;
s->connection_index = tc->connection.c_index;
@@ -1668,11 +1668,11 @@ tcp_test_lookup (vlib_main_t * vm, unformat_input_t * input)
* Allocate fake session and connection 2
*/
pool_get (session_manager_main.sessions[0], s);
- memset (s, 0, sizeof (*s));
+ clib_memset (s, 0, sizeof (*s));
s->session_index = s - smm->sessions[0];
pool_get (tm->connections[0], tc);
- memset (tc, 0, sizeof (*tc));
+ clib_memset (tc, 0, sizeof (*tc));
tc->connection.c_index = tc - tm->connections[0];
tc->connection.s_index = s->session_index;
s->connection_index = tc->connection.c_index;
@@ -1769,7 +1769,7 @@ tcp_test_session (vlib_main_t * vm, unformat_input_t * input)
remote_port = clib_host_to_net_u16 (11234);
pool_get (tm->connections[0], tc0);
- memset (tc0, 0, sizeof (*tc0));
+ clib_memset (tc0, 0, sizeof (*tc0));
tc0->state = TCP_STATE_ESTABLISHED;
tc0->rcv_las = 1;