From 2b9b6b9b130b75799a40989c0ebe5040fa3e45fb Mon Sep 17 00:00:00 2001 From: Andrej Kozemcak Date: Mon, 11 Mar 2019 10:22:32 +0100 Subject: SCVPP_TEST: Add NAT44 test Change-Id: I3f5814e44126e8996c94475dd7529a75ba958b32 Signed-off-by: Andrej Kozemcak --- src/scvpp/tests/CMakeLists.txt | 1 + src/scvpp/tests/scvpp_nat_test.c | 173 +++++++++++++++++++++++++++++++++++++++ src/scvpp/tests/scvpp_nat_test.h | 24 ++++++ src/scvpp/tests/scvpp_test.c | 4 + 4 files changed, 202 insertions(+) create mode 100644 src/scvpp/tests/scvpp_nat_test.c create mode 100644 src/scvpp/tests/scvpp_nat_test.h diff --git a/src/scvpp/tests/CMakeLists.txt b/src/scvpp/tests/CMakeLists.txt index bd135bf..3153fe0 100644 --- a/src/scvpp/tests/CMakeLists.txt +++ b/src/scvpp/tests/CMakeLists.txt @@ -22,6 +22,7 @@ find_program(valgrind_FOUND valgrind) 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) diff --git a/src/scvpp/tests/scvpp_nat_test.c b/src/scvpp/tests/scvpp_nat_test.c new file mode 100644 index 0000000..fae9615 --- /dev/null +++ b/src/scvpp/tests/scvpp_nat_test.c @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2019 PANTHEON.tech. + * + * 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 +#include +#include +#include +#include + +#include "scvpp_nat_test.h" +#include "sc_vpp_comm.h" +#include "sc_vpp_nat.h" + +void test_nat44_static_mapping(__attribute__((unused)) void **state) +{ + nat44_add_del_static_mapping_t map = {0}; + nat44_static_mapping_details_t dump = {0}; + u8 empty_ip[4] = {0}; + + /*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); + + nat44_static_mapping_dump(&dump); + + assert_int_equal(dump.addr_only, map.addr_only); + + assert_memory_equal(dump.local_ip_address, map.local_ip_address, + sizeof(dump.local_ip_address)); + + assert_memory_equal(dump.external_ip_address, map.external_ip_address, + sizeof(dump.external_ip_address)); + + /* Remove previous config*/ + map.is_add = 0; + + nat44_add_del_static_mapping(&map); + + memset(&dump, 0, sizeof(dump)); + + nat44_static_mapping_dump(&dump); + + assert_int_equal(dump.addr_only, 0); + + assert_memory_equal(dump.local_ip_address, empty_ip, + sizeof(dump.local_ip_address)); + + assert_memory_equal(dump.external_ip_address, empty_ip, + sizeof(dump.external_ip_address)); +} + +void test_nat44_static_mapping_with_ports(__attribute__((unused)) void **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; + + /*Configure address pool + Alternative to this CLI: + nat44 add address 5.4.4.4 + */ + sc_aton("5.4.4.4", range.first_ip_address, + sizeof(range.first_ip_address)); + + sc_aton("5.4.4.4", range.last_ip_address, + sizeof(range.last_ip_address)); + + range.is_add = 1; + + nat44_add_del_addr_range(&range); + + /*Configure NAT with ports + Alternative to this CLI: + nat44 add static mapping tcp local 172.168.0.1 77 external 5.4.4.4 88 + */ + + sc_aton("172.168.0.1", map.local_ip_address, + sizeof(map.local_ip_address)); + + sc_aton("5.4.4.4", map.external_ip_address, + sizeof(map.external_ip_address)); + + map.protocol = 6; + + map.addr_only = 0; + + map.external_sw_if_index = ~0; + + map.external_port = eport; + map.local_port = lport; + + map.is_add = 1; + + nat44_add_del_static_mapping(&map); + + nat44_static_mapping_dump(&dump); + + assert_int_equal(dump.addr_only, map.addr_only); + + assert_memory_equal(dump.local_ip_address, map.local_ip_address, + sizeof(dump.local_ip_address)); + + assert_memory_equal(dump.external_ip_address, map.external_ip_address, + sizeof(dump.external_ip_address)); + + assert_int_equal(dump.local_port, lport); + + assert_int_equal(dump.external_port, eport); + + /* Remove all previous config*/ + + map.is_add = 0; + + nat44_add_del_static_mapping(&map); + + memset(&dump, 0, sizeof(dump)); + + nat44_static_mapping_dump(&dump); + + assert_int_equal(dump.addr_only, 0); + + assert_memory_equal(dump.local_ip_address, empty_ip, + sizeof(dump.local_ip_address)); + + assert_memory_equal(dump.external_ip_address, empty_ip, + sizeof(dump.external_ip_address)); + + assert_int_equal(dump.local_port, 0); + + assert_int_equal(dump.external_port, 0); + + range.is_add = 0; + + nat44_add_del_addr_range(&range); +} + +const struct CMUnitTest nat_tests[] = { + cmocka_unit_test_setup_teardown(test_nat44_static_mapping, NULL, + NULL), + cmocka_unit_test_setup_teardown(test_nat44_static_mapping_with_ports, + NULL, NULL), + }; diff --git a/src/scvpp/tests/scvpp_nat_test.h b/src/scvpp/tests/scvpp_nat_test.h new file mode 100644 index 0000000..8a22f87 --- /dev/null +++ b/src/scvpp/tests/scvpp_nat_test.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2019 PANTHEON.tech. + * + * 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. + */ + +/* inclusion guard */ +#ifndef __SCVPP_NAT_TEST_H__ +#define __SCVPP_NAT_TEST_H__ + +#define NAT_TEST_SIZE 2 +extern const struct CMUnitTest nat_tests[NAT_TEST_SIZE]; + +#endif /* __SCVPP_NAT_TEST_H__ */ diff --git a/src/scvpp/tests/scvpp_test.c b/src/scvpp/tests/scvpp_test.c index adc2b55..cc23e43 100644 --- a/src/scvpp/tests/scvpp_test.c +++ b/src/scvpp/tests/scvpp_test.c @@ -23,6 +23,7 @@ #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) @@ -143,6 +144,9 @@ int main() cmocka_run_group_tests(tests, NULL, NULL); + print_message("\nNAT Tests\n"); + cmocka_run_group_tests(nat_tests, NULL, NULL); + /* Delete tap0 */ if (delete_tapv2("tap0") != 0) fprintf(stderr, "Failed deleting tap0\n"); -- cgit 1.2.3-korg