diff options
author | YohanPipereau <ypiperea@cisco.com> | 2019-03-06 14:01:58 +0100 |
---|---|---|
committer | YohanPipereau <ypiperea@cisco.com> | 2019-03-29 14:39:36 +0100 |
commit | a760dfb253161911fc3aa3c8b879c461d53ade6e (patch) | |
tree | 0e79953f4ed5615879a58f49a74df3f9c6739a42 /src/scvpp/tests | |
parent | 2b9b6b9b130b75799a40989c0ebe5040fa3e45fb (diff) |
Sweetcomb global cleanup
-Merge IETF and Openconfig to use SCVPP 2
-Move L2 bridge from sc_vpp_interface to sc_vpp_v3po
-Implement tav2 dump
-Make openconfig-interfaces functions static
-Try one more dispatch after failure in VAPI_CALL
-Add error return code for scvpp
-Remove unused length maccros
-Return appropriate error code for interface dump when interface not found
-Improve scvpp test suite
-Change get_interface_id prototype
-Use interface_dump_iface in openconfig_interface.
-No more vapi types in openconfig_interfaces.c
-Move openconfig_local_routing VAPI operations to sc_vpp_ip
-Implement a multiple dump with a stack data structure
-Comment out state_cb code from openconfig_local_routing to use new
functions later.
-Rename YANG model to their fully qualified name : <module>@<revision>
-Remove headers almost empty and put registration declaration in
sc_model.h
-Shorten vapi context global variable name
-Reorganize scvpp unit test suite
-Add instructions to Makefile to install/uninstall YANG models in sysrepo.
-Add this new instructions to README.md.
-Reimplement interface_dump_all
-Use a common message at INFO Log Level to know when sysrepo callbacks
are triggered
-Remove old structure to perform dump of all interfaces
-Reimplement get_interface_name and add scvpp test for it
-Clean sys_util
-Use UNUSED maccro everywhere to have lighter prototypes
-Have ietf-interfaces:interfaces-state work with new dump function
-Add setup and teardown for NAT tests
-Remove unused tapv2 dump
-Remove useless sysrepo module callback
-Remove xpath_find_first_key usage in openconfig-interfaces
-Remove xpath_find_first_key in oc_local_routing and in the rest of
sweetcomb
-Reorganize scvpp include dir and fix scvpp_test new warnings
-Fix scvpp tests for ip routes
-Factorize scvpp nat and test return code of its function
-Correct test_dump_if_all if there is an existing hardware interface
-Implement a per-prefix dump in scvpp
-free changes iterator in ietf-interfaces
-Add new XPATH in oc local-routing
-Introduce helper methods for sysrepo config callbacks
-Factorize config callback
-Refactor the openconfig-local-routing config callback
-Use common foreach_change to iterate over changes in all models
-Create a sample directory gathering example of configurations supposed
to work with sweetcomb
-Fix state callback of oc-local-routing
-Add new sample for get operation on next-hop
-foreach_elt maccro condition forgets to read one element
Change-Id: I8e87fce577a00337977588f057a6e095a20f457c
Signed-off-by: YohanPipereau <ypiperea@cisco.com>
Diffstat (limited to 'src/scvpp/tests')
-rw-r--r-- | src/scvpp/tests/CMakeLists.txt | 37 | ||||
-rw-r--r-- | src/scvpp/tests/scvpp_iface_test.c | 130 | ||||
-rw-r--r-- | src/scvpp/tests/scvpp_ip_test.c | 184 | ||||
-rw-r--r-- | src/scvpp/tests/scvpp_nat_test.c | 51 | ||||
-rw-r--r-- | src/scvpp/tests/scvpp_test.c | 160 | ||||
-rw-r--r-- | src/scvpp/tests/scvpp_test.h (renamed from src/scvpp/tests/scvpp_nat_test.h) | 17 |
6 files changed, 432 insertions, 147 deletions
diff --git a/src/scvpp/tests/CMakeLists.txt b/src/scvpp/tests/CMakeLists.txt index 3153fe0..2b6de4a 100644 --- a/src/scvpp/tests/CMakeLists.txt +++ b/src/scvpp/tests/CMakeLists.txt @@ -18,24 +18,23 @@ include_directories ("${PROJECT_SOURCE_DIR}/src") # check whether valgrind is installed find_program(valgrind_FOUND valgrind) -# macro for adding of an unit test -macro(ADD_UNIT_TEST TEST_NAME) - set(TEST_SRC - ${TEST_NAME}.c - scvpp_nat_test.c - ) - add_executable(${TEST_NAME} ${TEST_SRC}) - target_link_libraries(${TEST_NAME} ${CMOCKA_LIBRARIES} scvpp_a) - add_test(${TEST_NAME} ${TEST_NAME}) +set(TEST_SRC + scvpp_test.h + scvpp_test.c + scvpp_iface_test.c + scvpp_ip_test.c + scvpp_nat_test.c +) - if(valgrind_FOUND) - add_test(${TEST_NAME}_valgrind valgrind - --error-exitcode=1 --read-var-info=yes - --leak-check=full --show-leak-kinds=all - ./${TEST_NAME} - ) - endif(valgrind_FOUND) -endmacro(ADD_UNIT_TEST) +add_executable(scvpp_test ${TEST_SRC}) +target_link_libraries(scvpp_test ${CMOCKA_LIBRARIES} scvpp_a) +target_include_directories(scvpp_test PRIVATE ../inc) +add_test(scvpp_test scvpp_test) -# add individual unit-tests -ADD_UNIT_TEST(scvpp_test) +if(valgrind_FOUND) + add_test(${TEST_NAME}_valgrind valgrind + --error-exitcode=1 --read-var-info=yes + --leak-check=full --show-leak-kinds=all + ./${TEST_NAME} + ) +endif(valgrind_FOUND) diff --git a/src/scvpp/tests/scvpp_iface_test.c b/src/scvpp/tests/scvpp_iface_test.c new file mode 100644 index 0000000..6a96694 --- /dev/null +++ b/src/scvpp/tests/scvpp_iface_test.c @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2016 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 <unistd.h> +#include <setjmp.h> +#include <stdarg.h> +#include <cmocka.h> + +#include "scvpp_test.h" + +#include <scvpp/interface.h> +#include <scvpp/v3po.h> + +static void test_enable_disable(void **state) +{ + UNUSED(state); + sw_interface_dump_t dump = {0}; + int rc; + + rc = interface_enable("tap0", 1); + assert_int_equal(rc, SCVPP_OK); + + rc = interface_dump_iface(&dump, "tap0"); + assert_int_equal(rc, SCVPP_OK); + + assert_int_equal(dump.admin_up_down, true); + + rc = interface_enable("tap0", 0); + assert_int_equal(rc, SCVPP_OK); +} + +static void test_create_tapv2(void **state) +{ + UNUSED(state); + tapv2_create_t query = {0}; + sw_interface_dump_t dump = {0}; + int rc; + + query.id = 1; + query.use_random_mac = 1; + + rc = create_tapv2(&query); + assert_int_equal(rc, SCVPP_OK); + + rc = interface_dump_iface(&dump, "tap1"); + assert_int_equal(rc, SCVPP_OK); +} + +static int teardown_tapv2(void **state) +{ + UNUSED(state); + return delete_tapv2("tap1"); +} + +static void test_dump_iface_all(void **state) +{ + UNUSED(state); + struct elt *stack = NULL; + sw_interface_dump_t *dump; + bool exist = false; + + stack = interface_dump_all(); + assert_non_null(stack); + foreach_stack_elt(stack) { + dump = (sw_interface_dump_t *) data; + if (!strncmp((char*) dump->interface_name, "tap0", VPP_INTFC_NAME_LEN)) + exist = true; + free(dump); + } + assert_true(exist); +} + +static void test_dump_iface_exist(void **state) +{ + UNUSED(state); + vapi_payload_sw_interface_details details = {0}; + int rc; + + rc = interface_dump_iface(&details, "local0"); + assert_int_equal(rc, SCVPP_OK); + + assert_string_equal(details.interface_name, "local0"); +} + +static void test_dump_iface_unexist(void **state) +{ + UNUSED(state); + vapi_payload_sw_interface_details details = {0}; + int rc; + + rc = interface_dump_iface(&details, "unexisting"); + assert_int_equal(rc, -SCVPP_NOT_FOUND); +} + +static void test_get_interface_name(void **state) +{ + UNUSED(state); + char interface_name[VPP_INTFC_NAME_LEN]; + uint32_t tap0_if_index; + int rc; + + rc = get_interface_id("tap0", &tap0_if_index); + assert_int_equal(rc, SCVPP_OK); + + rc = get_interface_name(interface_name, tap0_if_index); + assert_int_equal(rc, SCVPP_OK); + + assert_string_equal(interface_name, "tap0"); +} + +const struct CMUnitTest iface_tests[IFACE_TEST_SIZE] = { + cmocka_unit_test_teardown(test_create_tapv2, teardown_tapv2), + cmocka_unit_test(test_enable_disable), + cmocka_unit_test(test_dump_iface_all), + cmocka_unit_test(test_dump_iface_exist), + cmocka_unit_test(test_dump_iface_unexist), + cmocka_unit_test(test_get_interface_name), +}; diff --git a/src/scvpp/tests/scvpp_ip_test.c b/src/scvpp/tests/scvpp_ip_test.c new file mode 100644 index 0000000..5e28123 --- /dev/null +++ b/src/scvpp/tests/scvpp_ip_test.c @@ -0,0 +1,184 @@ +/* + * Copyright (c) 2019 Cisco + * + * 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 <unistd.h> +#include <setjmp.h> +#include <stdarg.h> +#include <cmocka.h> + +#include "scvpp_test.h" + +#include <scvpp/ip.h> +#include <scvpp/interface.h> +#include <scvpp/comm.h> + +static void test_add_remove_ipv4(void **state) +{ + UNUSED(state); + const char ip_q[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.144"; + char ip_r[VPP_IP4_ADDRESS_STRING_LEN]; + u8 prefix_q = 24; + u8 prefix_r; + int rc; + + //add ipv4 on tap0 + rc = ipv46_config_add_remove("tap0", ip_q, prefix_q, false, true); + assert_int_equal(rc, SCVPP_OK); + + //dump ipv4 on tap0 and check if it mach + rc = ipv46_address_dump("tap0", ip_r, &prefix_r, false); + assert_int_equal(rc, SCVPP_OK); + assert_string_equal(ip_q, ip_r); + assert_int_equal(prefix_q, prefix_r); + + //remove ipv4 on tap0 + rc = ipv46_config_add_remove("tap0", ip_q, prefix_q, false, false); + assert_int_equal(rc, SCVPP_OK); + + //dump ipv4 after removal and check if equals 0.0.0.0 + rc = ipv46_address_dump("tap0", ip_r, &prefix_r, false); + assert_int_equal(rc, SCVPP_OK); + assert_string_equal(ip_r, "0.0.0.0"); +} + +static void test_ipv4_add_del_route_no_iface(void **state) +{ + UNUSED(state); + int rc; + char dst_address[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.0"; + uint8_t prefix_len = 24; + char next_hop[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.100"; + uint32_t table_id = 0; + fib_dump_t *dump; + struct elt *stack; + bool route_found = false; + + /* Must fail, can not have both interface and next hop IP null */ + rc = ipv46_config_add_del_route(dst_address, prefix_len, NULL, true, + table_id, NULL); + assert_int_equal(rc, -SCVPP_EINVAL); + + rc = ipv46_config_add_del_route(dst_address, prefix_len, next_hop, true, + table_id, NULL); + assert_int_equal(rc, SCVPP_OK); + + /* Dump all FIB routes and check if we find ours */ + stack = ipv4_fib_dump_all(); + assert_non_null(stack); + foreach_stack_elt(stack) { + dump = (fib_dump_t *) data; + + if (strncmp(sc_ntoa(dump->address), dst_address, + VPP_IP4_ADDRESS_STRING_LEN) == 0 && + dump->address_length == prefix_len) { + route_found = true; + assert_int_equal(dump->table_id, table_id); + assert_int_equal(dump->count, 1); + assert_string_equal(sc_ntoa(dump->path[0].next_hop), next_hop); + } + free(dump); + } + assert_true(route_found); + + /* Delete previously set route */ + rc = ipv46_config_add_del_route(dst_address, prefix_len, next_hop, false, + table_id, NULL); + assert_int_equal(rc, SCVPP_OK); + + /* Check our route has been deleted */ + route_found = false; + stack = ipv4_fib_dump_all(); + assert_non_null(stack); + foreach_stack_elt(stack) { + dump = (fib_dump_t *) data; + if (strncmp(sc_ntoa(dump->address), dst_address, + VPP_IP4_ADDRESS_STRING_LEN) == 0 && + dump->address_length == prefix_len) { + route_found = true; + } + free(dump); + } + assert_false(route_found); +} + +static void test_ipv4_add_del_route_no_next_hop_ip(void **state) +{ + UNUSED(state); + int rc; + char dst_address[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.0"; + uint8_t prefix_len = 24; + char interface[VPP_IP4_ADDRESS_STRING_LEN] = "tap0"; + uint32_t table_id = 0; + uint32_t sw_if_index; + fib_dump_t *dump; + struct elt *stack; + bool route_found = false; + + //Add a new route + rc = ipv46_config_add_del_route(dst_address, prefix_len, NULL, true, + table_id, interface); + assert_int_equal(rc, SCVPP_OK); + + //Dump all FIB routes and check we find ours + stack = ipv4_fib_dump_all(); + assert_non_null(stack); + + rc = get_interface_id(interface, &sw_if_index); + assert_int_equal(rc, SCVPP_OK); + + foreach_stack_elt(stack) { + dump = (fib_dump_t *) data; + + if (strncmp(sc_ntoa(dump->address), dst_address, + VPP_IP4_ADDRESS_STRING_LEN) == 0 && + dump->address_length == prefix_len) { + route_found = true; + assert_int_equal(dump->table_id, table_id); + assert_int_equal(dump->count, 1); + assert_int_equal(dump->path[0].sw_if_index, sw_if_index); + } + free(dump); + } + assert_true(route_found); + + //Delete route + rc = ipv46_config_add_del_route(dst_address, prefix_len, NULL, false, + table_id, interface); + assert_int_equal(rc, SCVPP_OK); + + //Check our route has been deleted + route_found = false; + stack = ipv4_fib_dump_all(); + assert_non_null(stack); + + foreach_stack_elt(stack) { + dump = (fib_dump_t *) data; + if (strncmp(sc_ntoa(dump->address), dst_address, + VPP_IP4_ADDRESS_STRING_LEN) == 0 && + dump->address_length == prefix_len) { + route_found = true; + } + free(dump); + } + assert_false(route_found); +} + + +const struct CMUnitTest ip_tests[IP_TEST_SIZE] = { + cmocka_unit_test(test_add_remove_ipv4), + cmocka_unit_test(test_ipv4_add_del_route_no_next_hop_ip), + cmocka_unit_test(test_ipv4_add_del_route_no_iface), +}; diff --git a/src/scvpp/tests/scvpp_nat_test.c b/src/scvpp/tests/scvpp_nat_test.c index fae9615..044102e 100644 --- a/src/scvpp/tests/scvpp_nat_test.c +++ b/src/scvpp/tests/scvpp_nat_test.c @@ -20,36 +20,37 @@ #include <setjmp.h> #include <cmocka.h> -#include "scvpp_nat_test.h" -#include "sc_vpp_comm.h" -#include "sc_vpp_nat.h" +#include "scvpp_test.h" -void test_nat44_static_mapping(__attribute__((unused)) void **state) +#include <scvpp/comm.h> +#include <scvpp/nat.h> + + +void test_nat44_static_mapping(void **state) { + UNUSED(state); nat44_add_del_static_mapping_t map = {0}; nat44_static_mapping_details_t dump = {0}; u8 empty_ip[4] = {0}; + int rc; /*Configure the static mapping Alternative to this CLI command: nat44 add static mapping local 172.168.0.1 external 172.168.8.5 */ - sc_aton("172.168.0.1", map.local_ip_address, sizeof(map.local_ip_address)); - sc_aton("172.168.8.5", map.external_ip_address, sizeof(map.external_ip_address)); - map.addr_only = 1; - map.external_sw_if_index = ~0; - map.is_add = 1; - nat44_add_del_static_mapping(&map); + rc = nat44_add_del_static_mapping(&map); + assert_int_equal(rc, SCVPP_OK); - nat44_static_mapping_dump(&dump); + rc = nat44_static_mapping_dump(&dump); + assert_int_equal(rc, SCVPP_OK); assert_int_equal(dump.addr_only, map.addr_only); @@ -62,11 +63,13 @@ void test_nat44_static_mapping(__attribute__((unused)) void **state) /* Remove previous config*/ map.is_add = 0; - nat44_add_del_static_mapping(&map); + rc = nat44_add_del_static_mapping(&map); + assert_int_equal(rc, SCVPP_OK); memset(&dump, 0, sizeof(dump)); - nat44_static_mapping_dump(&dump); + rc = nat44_static_mapping_dump(&dump); + assert_int_equal(rc, SCVPP_OK); assert_int_equal(dump.addr_only, 0); @@ -77,14 +80,16 @@ void test_nat44_static_mapping(__attribute__((unused)) void **state) sizeof(dump.external_ip_address)); } -void test_nat44_static_mapping_with_ports(__attribute__((unused)) void **state) +void test_nat44_static_mapping_with_ports(void **state) { + UNUSED(state); nat44_add_del_static_mapping_t map = {0}; nat44_static_mapping_details_t dump = {0}; nat44_add_del_address_range_t range = {0}; u8 empty_ip[4] = {0}; const u16 lport = 77; const u16 eport = 88; + int rc; /*Configure address pool Alternative to this CLI: @@ -98,7 +103,8 @@ void test_nat44_static_mapping_with_ports(__attribute__((unused)) void **state) range.is_add = 1; - nat44_add_del_addr_range(&range); + rc = nat44_add_del_addr_range(&range); + assert_int_equal(rc, SCVPP_OK); /*Configure NAT with ports Alternative to this CLI: @@ -122,9 +128,11 @@ void test_nat44_static_mapping_with_ports(__attribute__((unused)) void **state) map.is_add = 1; - nat44_add_del_static_mapping(&map); + rc = nat44_add_del_static_mapping(&map); + assert_int_equal(rc, SCVPP_OK); - nat44_static_mapping_dump(&dump); + rc = nat44_static_mapping_dump(&dump); + assert_int_equal(rc, SCVPP_OK); assert_int_equal(dump.addr_only, map.addr_only); @@ -142,11 +150,13 @@ void test_nat44_static_mapping_with_ports(__attribute__((unused)) void **state) map.is_add = 0; - nat44_add_del_static_mapping(&map); + rc = nat44_add_del_static_mapping(&map); + assert_int_equal(rc, SCVPP_OK); memset(&dump, 0, sizeof(dump)); - nat44_static_mapping_dump(&dump); + rc = nat44_static_mapping_dump(&dump); + assert_int_equal(rc, SCVPP_OK); assert_int_equal(dump.addr_only, 0); @@ -162,7 +172,8 @@ void test_nat44_static_mapping_with_ports(__attribute__((unused)) void **state) range.is_add = 0; - nat44_add_del_addr_range(&range); + rc = nat44_add_del_addr_range(&range); + assert_int_equal(rc, SCVPP_OK); } const struct CMUnitTest nat_tests[] = { diff --git a/src/scvpp/tests/scvpp_test.c b/src/scvpp/tests/scvpp_test.c index cc23e43..f6e99a0 100644 --- a/src/scvpp/tests/scvpp_test.c +++ b/src/scvpp/tests/scvpp_test.c @@ -14,89 +14,19 @@ */ #include <stdlib.h> -#include <stdio.h> #include <unistd.h> +#include <stdio.h> #include <setjmp.h> -#include <cmocka.h> - -#include "sc_vpp_comm.h" -#include "sc_vpp_interface.h" -#include "sc_vpp_ip.h" -#include "sc_vpp_v3po.h" -#include "scvpp_nat_test.h" - -//TODO Check with future function get_interface_state -static void test_enable_disable(void **state) -{ - int rc; - - rc = interface_enable("tap0", 1); - assert_int_equal(rc, 0); - - rc = interface_enable("tap0", 0); - assert_int_equal(rc, 0); -} - -//TODO would need to make sure tap0 is index 1 -//TODO delete eventually because get_interface_id will not be extern -static void test_name2index(void **state) -{ - int rc; - const char iface_name[] = "tap0"; - sw_interface_details_query_t query = {0}; - sw_interface_details_query_set_name(&query, iface_name); - - rc = get_interface_id(&query); - assert_int_equal(rc, 1); - - assert_string_equal(iface_name, query.sw_interface_details.interface_name); - assert_int_equal(query.sw_interface_details.sw_if_index, 1); -} - -static void test_add_ipv4(void **state) -{ - const char ip_q[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.144"; - char ip_r[VPP_IP4_ADDRESS_STRING_LEN]; - u8 prefix_q = 24; - u8 prefix_r; - int rc; - - //add ipv4 on tap0 - rc = ipv46_config_add_remove("tap0", ip_q, prefix_q, false, true); - assert_int_equal(rc, 0); - - rc = ipv46_address_dump("tap0", ip_r, &prefix_r, false); - assert_int_equal(rc, 0); - - assert_string_equal(ip_q, ip_r); - assert_int_equal(prefix_q, prefix_r); -} - -static void test_remove_ipv4(void **state) -{ - const char ip_q[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.144"; - char ip_r[VPP_IP4_ADDRESS_STRING_LEN]; - u8 prefix_q = 24; - u8 prefix_r; - int rc; - - //add ipv4 on tap0 - rc = ipv46_config_add_remove("tap0", ip_q, prefix_q, false, true); - assert_int_equal(rc, 0); - //remove ipv4 on tap0 - rc = ipv46_config_add_remove("tap0", ip_q, prefix_q, false, false); - assert_int_equal(rc, 0); - - //dump ipv4 - rc = ipv46_address_dump("tap0", ip_r, &prefix_r, false); - assert_int_equal(rc, 0); +#include <scvpp/comm.h> +#include <scvpp/v3po.h> - assert_string_equal(ip_r, "0.0.0.0"); -} +#include "scvpp_test.h" +/* test "AAA.BBB.CCC.DDD" -> {A, B, C, D} */ static void test_sc_ntoa(void **state) { + UNUSED(state); u8 buf[4] = {192, 168, 100, 44}; char *res; @@ -104,52 +34,76 @@ static void test_sc_ntoa(void **state) assert_string_equal(res, "192.168.100.44"); } -static void test_create_tapv2(void **state) +/* test {A, B, C, D} -> "AAA.BBB.CCC.DDD" */ +static void test_sc_aton(void **state) { - tapv2_create_t query = {0}; + UNUSED(state); + char ip[VPP_IP4_ADDRESS_STRING_LEN] = "192.168.100.44"; + uint8_t buf[4]; int rc; - query.id = 1; - query.use_random_mac = 1; - - rc = create_tapv2(&query); + rc = sc_aton(ip, buf, VPP_IP4_ADDRESS_LEN); assert_int_equal(rc, 0); - //TODO dump_tav2 and compare values - - rc = delete_tapv2("tap1"); - assert_int_equal(rc, 0); + assert_int_equal(buf[0], 192); + assert_int_equal(buf[1], 168); + assert_int_equal(buf[2], 100); + assert_int_equal(buf[3], 44); } -int main() +static int setup(void **state) { + UNUSED(state); tapv2_create_t query = {0}; - const struct CMUnitTest tests[] = { - cmocka_unit_test_setup_teardown(test_enable_disable, NULL, NULL), - cmocka_unit_test_setup_teardown(test_name2index, NULL, NULL), - cmocka_unit_test_setup_teardown(test_add_ipv4, NULL, NULL), - cmocka_unit_test_setup_teardown(test_remove_ipv4, NULL, NULL), - cmocka_unit_test_setup_teardown(test_sc_ntoa, NULL, NULL), - cmocka_unit_test_setup_teardown(test_create_tapv2, NULL, NULL), - }; - if (sc_connect_vpp() != 0) + if (sc_connect_vpp() != 0) { fprintf(stderr, "Error connecting to VPP\n"); + return -1; + } /* Create interface tap0 to test several functions */ query.id = 0; query.use_random_mac = 1; - if (create_tapv2(&query) != 0) + if (create_tapv2(&query) != 0) { fprintf(stderr, "Error creating tap0\n"); + return -1; + } - cmocka_run_group_tests(tests, NULL, NULL); - - print_message("\nNAT Tests\n"); - cmocka_run_group_tests(nat_tests, NULL, NULL); + return 0; +} +static int teardown(void **state) +{ + UNUSED(state); /* Delete tap0 */ - if (delete_tapv2("tap0") != 0) + if (delete_tapv2("tap0") != 0) { fprintf(stderr, "Failed deleting tap0\n"); + return -1; + } + + sc_disconnect_vpp(); + + return 0; +} + +/* return code for scvpp-test binary is the number of failed test */ +int main() +{ + int rc = 0; + + const struct CMUnitTest common_tests[] = { + cmocka_unit_test(test_sc_ntoa), + cmocka_unit_test(test_sc_aton), + }; + + print_message("Common tests\n"); + rc |= cmocka_run_group_tests(common_tests, NULL, NULL); + print_message("Interface tests\n"); + rc |= cmocka_run_group_tests(iface_tests, setup, teardown); + print_message("IP tests\n"); + rc |= cmocka_run_group_tests(ip_tests, setup, teardown); + print_message("NAT tests\n"); + rc |= cmocka_run_group_tests(nat_tests, setup, teardown); - return sc_disconnect_vpp(); + return rc; } diff --git a/src/scvpp/tests/scvpp_nat_test.h b/src/scvpp/tests/scvpp_test.h index 8a22f87..28bd514 100644 --- a/src/scvpp/tests/scvpp_nat_test.h +++ b/src/scvpp/tests/scvpp_test.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 PANTHEON.tech. + * Copyright (c) 2019 Cisco * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,11 +14,18 @@ * limitations under the License. */ -/* inclusion guard */ -#ifndef __SCVPP_NAT_TEST_H__ -#define __SCVPP_NAT_TEST_H__ +#ifndef __SCVPP_TEST_H +#define __SCVPP_TEST_H__ + +#include <cmocka.h> + +#define IFACE_TEST_SIZE 6 +extern const struct CMUnitTest iface_tests[IFACE_TEST_SIZE]; + +#define IP_TEST_SIZE 3 +extern const struct CMUnitTest ip_tests[IP_TEST_SIZE]; #define NAT_TEST_SIZE 2 extern const struct CMUnitTest nat_tests[NAT_TEST_SIZE]; -#endif /* __SCVPP_NAT_TEST_H__ */ +#endif /* __SCVPP_TEST_H__ */ |