From c17fc44735b509b22476e8663e33be62678837a2 Mon Sep 17 00:00:00 2001 From: Dave Barach Date: Mon, 18 Nov 2019 17:16:49 -0500 Subject: [PATCH] vlib: improve code coverage in src/vlib Type: test Signed-off-by: Dave Barach Change-Id: I2dc4c09730f77931a2b484a6dd5dc669068f8e57 --- src/plugins/unittest/CMakeLists.txt | 17 ++--- src/plugins/unittest/vlib_test.c | 137 ++++++++++++++++++++++++++++++++++++ test/test_vlib.py | 29 ++++++++ 3 files changed, 175 insertions(+), 8 deletions(-) create mode 100644 src/plugins/unittest/vlib_test.c diff --git a/src/plugins/unittest/CMakeLists.txt b/src/plugins/unittest/CMakeLists.txt index e074d317b0a..a029e0e3259 100644 --- a/src/plugins/unittest/CMakeLists.txt +++ b/src/plugins/unittest/CMakeLists.txt @@ -15,29 +15,30 @@ add_vpp_plugin(unittest SOURCES bier_test.c bihash_test.c - crypto_test.c crypto/aes_cbc.c - crypto/aes_gcm.c crypto/aes_ctr.c - crypto/rfc2202_hmac_sha1.c + crypto/aes_gcm.c crypto/rfc2202_hmac_md5.c + crypto/rfc2202_hmac_sha1.c crypto/rfc4231.c + crypto_test.c fib_test.c - ipsec_test.c interface_test.c + ipsec_test.c lisp_cp_test.c llist_test.c mactime_test.c - mpcap_node.c mfib_test.c + mpcap_node.c punt_test.c rbtree_test.c session_test.c - string_test.c - tcp_test.c sparse_vec_test.c + string_test.c svm_fifo_test.c + tcp_test.c + test_buffer.c unittest.c util_test.c - test_buffer.c + vlib_test.c ) diff --git a/src/plugins/unittest/vlib_test.c b/src/plugins/unittest/vlib_test.c new file mode 100644 index 00000000000..275584e01eb --- /dev/null +++ b/src/plugins/unittest/vlib_test.c @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2019 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 + +u8 *vlib_validate_buffers (vlib_main_t * vm, + u32 * buffers, + uword next_buffer_stride, + uword n_buffers, + vlib_buffer_known_state_t known_state, + uword follow_buffer_next); + +static clib_error_t * +test_vlib_command_fn (vlib_main_t * vm, + unformat_input_t * input, vlib_cli_command_t * cmd) +{ + u32 bi; + u8 *res; + u32 allocated; + vlib_buffer_t *b; + vlib_buffer_t *last_b; + u8 junk[4] = { 1, 2, 3, 4 }; + vlib_packet_template_t _t, *t = &_t; + u8 *data_copy = 0; + + /* Cover vlib_packet_template_get_packet */ + t->packet_data = format (0, "silly packet data"); + t->min_n_buffers_each_alloc = 1; + t->name = (u8 *) "test template"; + + if (vlib_packet_template_get_packet (vm, t, &bi)) + vlib_buffer_free_one (vm, bi); + + vec_free (t->packet_data); + + /* Get a buffer */ + allocated = vlib_buffer_alloc (vm, &bi, 1); + if (allocated != 1) + return clib_error_return (0, "Buffer allocation failure!"); + + b = vlib_get_buffer (vm, bi); + + /* Force buffer allocation */ + b->current_length = 2048; + last_b = b; + vlib_buffer_chain_append_data_with_alloc (vm, b, &last_b, + junk, ARRAY_LEN (junk)); + + /* Cover vlib_buffer_length_in_chain_slow_path(...) */ + b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID); + vlib_cli_output (vm, "buffer length %d", + vlib_buffer_length_in_chain (vm, b)); + b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID); + vlib_cli_output (vm, "%u", vlib_buffer_index_length_in_chain (vm, bi)); + + /* Add more data. Eat Mor Chikin. */ + vlib_buffer_add_data (vm, &bi, junk, ARRAY_LEN (junk)); + + /* Dump the resulting two-chunk pkt */ + vlib_cli_output (vm, "%U", format_vlib_buffer_and_data, b); + + vec_validate (data_copy, vlib_buffer_length_in_chain (vm, b) - 1); + vlib_cli_output (vm, "%u", vlib_buffer_contents (vm, bi, data_copy)); + vec_free (data_copy); + + /* Cover simple functions in buffer.h / buffer_funcs.h */ + vlib_cli_output (vm, "%llx", vlib_buffer_get_va (b)); + vlib_cli_output (vm, "%llx", vlib_buffer_get_current_va (b)); + vlib_cli_output (vm, "%d", vlib_buffer_has_space (b, 100ll)); + vlib_buffer_reset (b); + vlib_cli_output (vm, "%llx", vlib_buffer_get_tail (b)); + vlib_buffer_put_uninit (b, 0); + vlib_buffer_push_uninit (b, 0); + vlib_buffer_make_headroom (b, 0); + (void) vlib_buffer_pull (b, 0); + vlib_cli_output (vm, "%llx", vlib_buffer_get_pa (vm, b)); + vlib_cli_output (vm, "%llx", vlib_buffer_get_current_pa (vm, b)); + + /* Validate it one way */ + res = vlib_validate_buffer (vm, bi, 1 /* follow_buffer_next */ ); + if (res) + return clib_error_return (0, "%v", res); + + /* Validate it a different way */ + res = vlib_validate_buffers (vm, &bi, 0 /* stride */ , + 1, VLIB_BUFFER_KNOWN_ALLOCATED, + 1 /* follow_buffer_next */ ); + if (res) + return clib_error_return (0, "%v", res); + + /* Free it */ + vlib_buffer_free_one (vm, bi); + /* It will be free */ + res = vlib_validate_buffers (vm, &bi, 0 /* stride */ , + 1, VLIB_BUFFER_KNOWN_FREE, + 1 /* follow_buffer_next */ ); + if (res) + return clib_error_return (0, "%v", res); + + /* It will not be allocated, exercise error path */ + res = vlib_validate_buffers (vm, &bi, 0 /* stride */ , + 1, VLIB_BUFFER_KNOWN_ALLOCATED, + 1 /* follow_buffer_next */ ); + if (res) + return clib_error_return (0, "%v", res); + + return 0; +} + +/* *INDENT-OFF* */ +VLIB_CLI_COMMAND (test_vlib_command, static) = +{ + .path = "test vlib", + .short_help = "vlib code coverate unit test", + .function = test_vlib_command_fn, +}; +/* *INDENT-ON* */ + +/* + * fd.io coding-style-patch-verification: ON + * + * Local Variables: + * eval: (c-set-style "gnu") + * End: + */ diff --git a/test/test_vlib.py b/test/test_vlib.py index 20b60be7d0c..36a4340fa57 100644 --- a/test/test_vlib.py +++ b/test/test_vlib.py @@ -110,5 +110,34 @@ class TestVlib(VppTestCase): else: self.logger.info(cmd + " FAIL retval " + str(r.retval)) + def test_vlib_buffer_c_unittest(self): + """ Vlib buffer.c Code Coverage Test """ + + cmds = ["loopback create", + "packet-generator new {\n" + " name vlib\n" + " limit 15\n" + " size 128-128\n" + " interface loop0\n" + " node ethernet-input\n" + " data {\n" + " IP6: 00:d0:2d:5e:86:85 -> 00:0d:ea:d0:00:00\n" + " ICMP: db00::1 -> db00::2\n" + " incrementing 30\n" + " }\n", + "}\n", + "pa en", + "test vlib", + "show buffers", + ] + + for cmd in cmds: + r = self.vapi.cli_return_response(cmd) + if r.retval != 0: + if hasattr(r, 'reply'): + self.logger.info(cmd + " FAIL reply " + r.reply) + else: + self.logger.info(cmd + " FAIL retval " + str(r.retval)) + if __name__ == '__main__': unittest.main(testRunner=VppTestRunner) -- 2.16.6