From 0685a3b696644739cf0a6c68e558b417af23c591 Mon Sep 17 00:00:00 2001 From: lakshminarayanagurram Date: Tue, 4 Jun 2019 07:33:33 +0530 Subject: [PATCH] Add test cases for random port bindings --- src/devtools.c | 6 + src/devtools.h | 8 + src/meshlink.c | 3 + src/meshlink.sym | 1 + .../common/network_namespace_framework.c | 2 +- test/blackbox/run_blackbox_tests/Makefile.am | 4 +- .../run_blackbox_tests/run_blackbox_tests.c | 6 + .../test_cases_random_port_bindings01.c | 304 +++++++++++++ .../test_cases_random_port_bindings01.h | 28 ++ .../test_cases_random_port_bindings02.c | 422 ++++++++++++++++++ .../test_cases_random_port_bindings02.h | 28 ++ 11 files changed, 810 insertions(+), 2 deletions(-) create mode 100644 test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.c create mode 100644 test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.h create mode 100644 test/blackbox/run_blackbox_tests/test_cases_random_port_bindings02.c create mode 100644 test/blackbox/run_blackbox_tests/test_cases_random_port_bindings02.h diff --git a/src/devtools.c b/src/devtools.c index 6d78119c..f7d388ca 100644 --- a/src/devtools.c +++ b/src/devtools.c @@ -30,6 +30,12 @@ #include "devtools.h" +static void nop_probe(void) { + return; +} + +void (*devtool_trybind_probe)(void) = nop_probe; + /* Return an array of edges in the current network graph. * Data captures the current state and will not be updated. * Caller must deallocate data when done. diff --git a/src/devtools.h b/src/devtools.h index 97924384..04a33452 100644 --- a/src/devtools.h +++ b/src/devtools.h @@ -151,4 +151,12 @@ extern meshlink_submesh_t **devtool_get_all_submeshes(meshlink_handle_t *mesh, m */ extern meshlink_handle_t *devtool_open_in_netns(const char *confbase, const char *name, const char *appname, dev_class_t devclass, int netns); +/// Debug function pointer variable for set port API +/** This function pointer variable is a userspace tracepoint or debugger callback for + * set port function @a meshlink_set_port @a. + * On assigning a debug function variable invokes callback when try_bind() succeeds in meshlink_set_port API. + * + */ +extern void (*devtool_trybind_probe)(void); + #endif diff --git a/src/meshlink.c b/src/meshlink.c index 496e5d3c..bd25e2ae 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -45,6 +45,7 @@ typedef struct { #include "xalloc.h" #include "ed25519/sha512.h" #include "discovery.h" +#include "devtools.h" #ifndef MSG_NOSIGNAL #define MSG_NOSIGNAL 0 @@ -2308,6 +2309,8 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) { return false; } + devtool_trybind_probe(); + bool rval = false; pthread_mutex_lock(&(mesh->mesh_mutex)); diff --git a/src/meshlink.sym b/src/meshlink.sym index bc275939..514b0f5b 100644 --- a/src/meshlink.sym +++ b/src/meshlink.sym @@ -4,6 +4,7 @@ devtool_get_all_edges devtool_get_all_submeshes devtool_get_node_status devtool_open_in_netns +devtool_trybind_probe meshlink_add_address meshlink_add_external_address meshlink_blacklist diff --git a/test/blackbox/common/network_namespace_framework.c b/test/blackbox/common/network_namespace_framework.c index d72286e9..1c39bccc 100644 --- a/test/blackbox/common/network_namespace_framework.c +++ b/test/blackbox/common/network_namespace_framework.c @@ -513,7 +513,7 @@ void netns_destroy_topology(netns_state_t *test_state) { for(i = 0; i < namespace_handle->pid_nos; i++) { pid = (namespace_handle->pids)[i]; - assert(kill(pid, SIGINT) != -1); + kill(pid, SIGINT); pid_ret = waitpid(pid, NULL, WNOHANG); assert(pid_ret != -1); diff --git a/test/blackbox/run_blackbox_tests/Makefile.am b/test/blackbox/run_blackbox_tests/Makefile.am index 89dc8382..379ce46a 100644 --- a/test/blackbox/run_blackbox_tests/Makefile.am +++ b/test/blackbox/run_blackbox_tests/Makefile.am @@ -62,7 +62,9 @@ run_blackbox_tests_SOURCES = \ test_cases_submesh03.c \ test_cases_submesh04.c \ test_cases_autoconnect.c \ - test_cases_set_connection_try_cb.c + test_cases_set_connection_try_cb.c \ + test_cases_random_port_bindings01.c \ + test_cases_random_port_bindings02.c run_blackbox_tests_LDADD = ../../../src/libmeshlink.la $(LXC_LIBS) $(CMOCKA_LIBS) run_blackbox_tests_CFLAGS = -D_GNU_SOURCE $(LXC_CFLAGS) $(CMOCKA_CFLAGS) diff --git a/test/blackbox/run_blackbox_tests/run_blackbox_tests.c b/test/blackbox/run_blackbox_tests/run_blackbox_tests.c index b174ab7e..62387c94 100644 --- a/test/blackbox/run_blackbox_tests/run_blackbox_tests.c +++ b/test/blackbox/run_blackbox_tests/run_blackbox_tests.c @@ -73,6 +73,9 @@ #include "test_cases_autoconnect.h" #include "test_cases_set_connection_try_cb.h" +#include "test_cases_random_port_bindings01.h" +#include "test_cases_random_port_bindings02.h" + #include "../common/containers.h" #include "../common/common_handlers.h" @@ -145,6 +148,9 @@ int main(int argc, char *argv[]) { failed_tests += test_optimal_pmtu(); + failed_tests += test_meshlink_random_port_bindings01(); + failed_tests += test_meshlink_random_port_bindings02(); + printf("[ PASSED ] %d test(s).\n", total_tests - failed_tests); printf("[ FAILED ] %d test(s).\n", failed_tests); diff --git a/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.c b/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.c new file mode 100644 index 00000000..24a7e409 --- /dev/null +++ b/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.c @@ -0,0 +1,304 @@ +/* + test_cases_random_port_bindings01.c -- Execution of specific meshlink black box test cases + Copyright (C) 2018 Guus Sliepen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +/* Modify this to change the logging level of Meshlink */ +#define TEST_MESHLINK_LOG_LEVEL MESHLINK_DEBUG + +#include "execute_tests.h" +#include "test_cases_random_port_bindings01.h" +#include "../../../src/meshlink.h" +#include "../../../src/devtools.h" +#include "../common/containers.h" +#include "../common/test_step.h" +#include "../common/common_handlers.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +static void test_case_mesh_random_port_bindings_01(void **state); +static bool test_steps_mesh_random_port_bindings_01(void); +static void test_case_mesh_random_port_bindings_02(void **state); +static bool test_steps_mesh_random_port_bindings_02(void); +static void test_case_mesh_random_port_bindings_03(void **state); +static bool test_steps_mesh_random_port_bindings_03(void); + +/* State structure for meshlink_random_port_bindings Test Case #1 */ +static black_box_state_t test_mesh_random_port_bindings_01_state = { + .test_case_name = "test_case_mesh_random_port_bindings_01", +}; + +/* State structure for meshlink_random_port_bindings Test Case #2 */ +static black_box_state_t test_mesh_random_port_bindings_02_state = { + .test_case_name = "test_case_mesh_random_port_bindings_02", +}; + +/* State structure for meshlink_random_port_bindings Test Case #3 */ +static black_box_state_t test_mesh_random_port_bindings_03_state = { + .test_case_name = "test_case_mesh_random_port_bindings_03", +}; + +static int sockfd = -1, ipv6_fd = -1; + +static void log_message(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { + (void) mesh; + + static const char *levelstr[] = { + [MESHLINK_DEBUG] = "\x1b[34mDEBUG", + [MESHLINK_INFO] = "\x1b[32mINFO", + [MESHLINK_WARNING] = "\x1b[33mWARNING", + [MESHLINK_ERROR] = "\x1b[31mERROR", + [MESHLINK_CRITICAL] = "\x1b[31mCRITICAL", + }; + fprintf(stderr, "%s:\x1b[0m %s\n", levelstr[level], text); +} + +static void occupy_port(int port) { + int ret_val; + int mode = 1; + struct sockaddr_in servaddr; + struct sockaddr_in6 ipv6addr; + + sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + assert_int_not_equal(sockfd, -1); + memset(&servaddr, 0, sizeof(servaddr)); + + servaddr.sin_family = AF_INET; + servaddr.sin_addr.s_addr = htonl(INADDR_ANY); + servaddr.sin_port = htons(port); + + assert_int_equal(bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)), 0); + + ipv6_fd = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); + assert_int_not_equal(ipv6_fd, -1); + + mode = 1; + setsockopt(ipv6_fd, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&mode, sizeof(mode)); + + memset(&ipv6addr, 0, sizeof(ipv6addr)); + + ipv6addr.sin6_family = AF_INET6; + ipv6addr.sin6_addr = in6addr_any; + ipv6addr.sin6_port = htons(port); + + if((ret_val = bind(ipv6_fd, (const struct sockaddr *)&ipv6addr, sizeof(ipv6addr))) < 0) { + fprintf(stderr, "Bind to ipv6 failed due to %s\n", strerror(errno)); + assert(false); + } + + listen(ipv6_fd, 5); + + return; +} + +static void occupy_trybind_port(void) { + occupy_port(10000); + return; +} + +/* Execute meshlink_random_port_bindings Test Case # 1*/ +void test_case_mesh_random_port_bindings_01(void **state) { + execute_test(test_steps_mesh_random_port_bindings_01, state); +} + +/* Test Steps for meshlink random port bindings Test Case # 1 + + Test Steps: + 1. Open a node instance + 2. Bind a Socket on port 10000 + 3. Call meshlink_set_port() with same port 10000 + + Expected Result: + The meshlink_set_port() API should fail and the Listening Port + of the instance should be unchanged. +*/ +bool test_steps_mesh_random_port_bindings_01(void) { + struct sockaddr_in servaddr; + meshlink_handle_t *relay = NULL; + meshlink_destroy("relay_conf"); + + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message); + + relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE); + fprintf(stderr, "Got mesh handle %p\n", (void *)relay); + assert_non_null(relay); + + meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message); + meshlink_enable_discovery(relay, false); + + assert_true(meshlink_start(relay)); + + occupy_port(10000); + + meshlink_stop(relay); + fprintf(stderr, "Meshlink stop returned\n"); + + assert_int_equal(meshlink_set_port(relay, 10000), false); + fprintf(stderr, "Meshlink set port returned\n"); + + close(sockfd); + close(ipv6_fd); + + sockfd = -1; + ipv6_fd = -1; + + meshlink_close(relay); + meshlink_destroy("relay_conf"); + + return true; +} + +/* Execute meshlink_blacklist Test Case # 2*/ +void test_case_mesh_random_port_bindings_02(void **state) { + execute_test(test_steps_mesh_random_port_bindings_02, state); +} + +/* Test Steps for meshlink random port bindings Test Case # 2 + + Test Steps: + 1. Open a node and start the instance. + 2. Call meshlink_set_port() with port 10000 + 3. When try bind succeds block the port using devtool_trybind_probe() callback. + + Expected Result: + The meshlink_set_port() API should fail. +*/ +bool test_steps_mesh_random_port_bindings_02(void) { + int port = -1; + meshlink_handle_t *relay = NULL; + meshlink_destroy("relay_conf"); + + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message); + + relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE); + fprintf(stderr, "Got mesh handle %p\n", (void *)relay); + assert_non_null(relay); + + meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message); + meshlink_enable_discovery(relay, false); + + assert_true(meshlink_start(relay)); + + sleep(1); + port = meshlink_get_port(relay); + + devtool_trybind_probe = occupy_trybind_port; + meshlink_stop(relay); + + assert_int_equal(meshlink_set_port(relay, 10000), false); + + close(sockfd); + close(ipv6_fd); + + sockfd = -1; + ipv6_fd = -1; + + meshlink_close(relay); + meshlink_destroy("relay_conf"); + return true; +} + +/* Execute meshlink_blacklist Test Case # 3*/ +void test_case_mesh_random_port_bindings_03(void **state) { + execute_test(test_steps_mesh_random_port_bindings_03, state); +} + +/* Test Steps for meshlink random port bindings Test Case # 3 + + Test Steps: + 1. Open a node and start the instance. + 2. Retrieve the port number of current instance using meshlink_get_port(). + 3. Close the instance and try to occupy the meshlink instance port. + 4. Start the instance again with same confdir. + + Expected Result: + The meshlink instance should start with a new random port different to + previous port number. +*/ +bool test_steps_mesh_random_port_bindings_03(void) { + int mode = 1; + int port, new_port; + struct sockaddr_in servaddr; + struct sockaddr_in6 ipv6addr; + meshlink_handle_t *relay = NULL; + meshlink_destroy("relay_conf"); + + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message); + + relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE); + fprintf(stderr, "Got mesh handle %p\n", (void *)relay); + assert_non_null(relay); + + meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message); + meshlink_enable_discovery(relay, false); + + assert_true(meshlink_start(relay)); + port = meshlink_get_port(relay); + + meshlink_close(relay); + + occupy_port(port); + + relay = meshlink_open("relay_conf", "relay", "test", DEV_CLASS_BACKBONE); + fprintf(stderr, "Got mesh handle %p\n", (void *)relay); + assert_non_null(relay); + + meshlink_set_log_cb(relay, MESHLINK_DEBUG, log_message); + meshlink_enable_discovery(relay, false); + + assert_true(meshlink_start(relay)); + + new_port = meshlink_get_port(relay); + + assert_int_not_equal(port, new_port); + + close(sockfd); + close(ipv6_fd); + + sockfd = -1; + ipv6_fd = -1; + + meshlink_close(relay); + meshlink_destroy("relay_conf"); + return true; +} + +int test_meshlink_random_port_bindings01(void) { + const struct CMUnitTest blackbox_random_port_bindings_tests[] = { + cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_01, NULL, NULL, + (void *)&test_mesh_random_port_bindings_01_state), + cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_02, NULL, NULL, + (void *)&test_mesh_random_port_bindings_02_state), + cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_03, NULL, NULL, + (void *)&test_mesh_random_port_bindings_03_state) + }; + + total_tests += sizeof(blackbox_random_port_bindings_tests) / sizeof(blackbox_random_port_bindings_tests[0]); + + return cmocka_run_group_tests(blackbox_random_port_bindings_tests, NULL, NULL); +} diff --git a/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.h b/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.h new file mode 100644 index 00000000..c79ea918 --- /dev/null +++ b/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.h @@ -0,0 +1,28 @@ +#ifndef TEST_CASES_RANDOM_PORT_BINDINGS01_H +#define TEST_CASES_RANDOM_PORT_BINDINGS01_H + +/* + test_cases_random_port_bindings01.h -- Declarations for Individual Test Case implementation functions + Copyright (C) 2018 Guus Sliepen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include + +extern int test_meshlink_random_port_bindings01(void); +extern int total_tests; + +#endif //TEST_CASES_RANDOM_PORT_BINDINGS01_H \ No newline at end of file diff --git a/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings02.c b/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings02.c new file mode 100644 index 00000000..2d24c3af --- /dev/null +++ b/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings02.c @@ -0,0 +1,422 @@ +/* + test_optimal_pmtu.c -- Execution of specific meshlink black box test cases + Copyright (C) 2019 Guus Sliepen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ +#include +#include +#include +#include +#include +#include +#include "../../../src/meshlink.h" +#include "../common/containers.h" +#include "../common/test_step.h" +#include "../common/common_handlers.h" +#include "../common/network_namespace_framework.h" +#include "../../utils.h" +#include "test_cases_random_port_bindings02.h" + +static void test_case_mesh_random_port_bindings_04(void **state); +static bool test_steps_mesh_random_port_bindings_04(void); +static void test_case_mesh_random_port_bindings_05(void **state); +static bool test_steps_mesh_random_port_bindings_05(void); + +typedef bool (*test_step_func_t)(void); +static int setup_test(void **state); + +static meshlink_handle_t *peer, *nut_instance, *relay; +static char *peer_invite, *nut_invite; +struct sync_flag test_random_port_binding_node_connected = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER}; +struct sync_flag test_random_port_binding_node_started = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER}; +struct sync_flag test_random_port_binding_peer_reachable = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER}; +struct sync_flag test_random_port_binding_make_switch = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER}; +struct sync_flag test_random_port_binding_relay_closed = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER}; +struct sync_flag test_random_port_binding_peer_closed = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER}; +struct sync_flag test_random_port_binding_nut_closed = {.mutex = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER}; +static netns_state_t *test_random_port_bindings_state; +static bool localnode = false; + +static int setup_test(void **state) { + netns_create_topology(test_random_port_bindings_state); + fprintf(stderr, "\nCreated topology\n"); + + set_sync_flag(&test_random_port_binding_node_connected, false); + set_sync_flag(&test_random_port_binding_node_started, false); + set_sync_flag(&test_random_port_binding_peer_reachable, false); + set_sync_flag(&test_random_port_binding_make_switch, false); + set_sync_flag(&test_random_port_binding_relay_closed, false); + set_sync_flag(&test_random_port_binding_peer_closed, false); + set_sync_flag(&test_random_port_binding_nut_closed, false); + + meshlink_destroy("nut"); + meshlink_destroy("peer"); + meshlink_destroy("relay"); + + return EXIT_SUCCESS; +} + +static int teardown_test(void **state) { + meshlink_destroy("nut"); + meshlink_destroy("peer"); + meshlink_destroy("relay"); + netns_destroy_topology(test_random_port_bindings_state); + + return EXIT_SUCCESS; +} + +static void execute_test(test_step_func_t step_func, void **state) { + + fprintf(stderr, "\n\x1b[32mRunning Test\x1b[0m\n"); + bool test_result = step_func(); + + if(!test_result) { + fail(); + } +} + +static void message_log(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { + char *levelstr = "\x1b[32mRELAY"; + + if(strcmp(mesh->name, "peer") == 0) { + if(strcmp("Connection with nut activated", text) == 0) { + set_sync_flag(&test_random_port_binding_node_connected, true); + } + + levelstr = "\x1b[34mPEER"; + } else if(strcmp(mesh->name, "nut") == 0) { + if(strcmp("Connection with peer activated", text) == 0) { + set_sync_flag(&test_random_port_binding_node_connected, true); + } + + levelstr = "\x1b[33mNUT"; + } + + fprintf(stderr, "%s:\x1b[0m %s\n", levelstr, text); +} + +static void node_status(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { + (void)mesh; + + if(reachable) { + if((strcmp(mesh->name, "nut") == 0) && (strcmp(node->name, "peer") == 0)) { + set_sync_flag(&test_random_port_binding_peer_reachable, true); + } + + fprintf(stderr, "%s: %s joined.\n", mesh->name, node->name); + } +} + +static void *relay_node(void *arg) { + mesh_arg_t *mesh_arg = (mesh_arg_t *)arg; + + //system("ifconfig"); + + meshlink_destroy("relay"); + + relay = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class); + assert(relay); + + assert_true(meshlink_start(relay)); + fprintf(stderr, "\n\x1b[32mRelay Started\x1b[0m\n"); + + assert((peer_invite = meshlink_invite(relay, NULL, "peer"))); + assert((nut_invite = meshlink_invite(relay, NULL, "nut"))); + + set_sync_flag(&test_random_port_binding_node_started, true); + + meshlink_set_log_cb(relay, MESHLINK_DEBUG, message_log); + + if(localnode == true) { + assert(wait_sync_flag(&test_random_port_binding_make_switch, 300)); + meshlink_close(relay); + meshlink_destroy("relay"); + + + set_sync_flag(&test_random_port_binding_relay_closed, true); + + return NULL; + } + + assert(wait_sync_flag(&test_random_port_binding_node_connected, 300)); + + meshlink_close(relay); + meshlink_destroy("relay"); + + + set_sync_flag(&test_random_port_binding_relay_closed, true); + + return NULL; +} + +static void *peer_node(void *arg) { + mesh_arg_t *mesh_arg = (mesh_arg_t *)arg; + + fprintf(stderr, "\n\x1b[32mPeer Thread Started\x1b[0m\n"); + + meshlink_destroy("peer"); + + peer = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class); + assert(peer); + meshlink_set_log_cb(peer, MESHLINK_DEBUG, message_log); + + fprintf(stderr, "\n\x1b[32mPeer joining relay\x1b[0m\n"); + + assert_true(meshlink_join(peer, (const char *)mesh_arg->join_invitation)); + + assert_true(meshlink_start(peer)); + + fprintf(stderr, "\n\x1b[32mPeer Started\x1b[0m\n"); + + set_sync_flag(&test_random_port_binding_node_started, true); + + assert(wait_sync_flag(&test_random_port_binding_make_switch, 300)); + + meshlink_stop(peer); + + //meshlink_set_log_cb(peer, MESHLINK_DEBUG, message_log); + + assert(meshlink_set_port(peer, 20000)); + + assert_true(meshlink_start(peer)); + + assert(wait_sync_flag(&test_random_port_binding_node_connected, 300)); + + meshlink_close(peer); + meshlink_destroy("peer"); + + set_sync_flag(&test_random_port_binding_peer_closed, true); + + return NULL; +} + +static void *nut_node(void *arg) { + mesh_arg_t *mesh_arg = (mesh_arg_t *)arg; + + fprintf(stderr, "\n\x1b[32mNut Thread Started\x1b[0m\n"); + + meshlink_destroy("nut"); + + nut_instance = meshlink_open(mesh_arg->node_name, mesh_arg->confbase, mesh_arg->app_name, mesh_arg->dev_class); + assert(nut_instance); + + meshlink_set_log_cb(nut_instance, MESHLINK_DEBUG, message_log); + + fprintf(stderr, "\n\x1b[32mNut joining relay\x1b[0m\n"); + + assert_true(meshlink_join(nut_instance, (const char *)mesh_arg->join_invitation)); + + meshlink_set_node_status_cb(nut_instance, node_status); + + assert_true(meshlink_start(nut_instance)); + + fprintf(stderr, "\n\x1b[32mNut Started\x1b[0m\n"); + sleep(5); + + set_sync_flag(&test_random_port_binding_node_started, true); + + assert(wait_sync_flag(&test_random_port_binding_make_switch, 300)); + + meshlink_stop(nut_instance); + + //meshlink_set_log_cb(nut_instance, MESHLINK_DEBUG, message_log); + + assert(meshlink_set_port(nut_instance, 30000)); + + assert_true(meshlink_start(nut_instance)); + + assert(wait_sync_flag(&test_random_port_binding_node_connected, 300)); + + meshlink_close(nut_instance); + meshlink_destroy("nut"); + + set_sync_flag(&test_random_port_binding_nut_closed, true); + + return NULL; +} + +/* Test Steps for Random port bindings Test Case # 4 */ +static void test_case_mesh_random_port_bindings_04(void **state) { + execute_test(test_steps_mesh_random_port_bindings_04, state); + return; +} + +/* Test Steps for Random port bindings Test Case # 4 + + Test Steps: + 1. Create three node nut, peer and relay in three different name spaces. + 2. Join nut and peer to relay with invitation. + 3. Stop the three nodes and change the ports of nut and peer. + 4. Start all the nodes again. + Expected Result: + NUT and Peer should be able to discover each others port with the help + of RELAY and form the direct meta connection. +*/ +static bool test_steps_mesh_random_port_bindings_04(void) { + mesh_arg_t relay_arg = {.node_name = "relay", .confbase = "relay", .app_name = "chat", .dev_class = 0 }; + mesh_arg_t peer_arg = {.node_name = "peer", .confbase = "peer", .app_name = "chat", .dev_class = 1 }; + mesh_arg_t nut_arg = {.node_name = "nut", .confbase = "nut", .app_name = "chat", .dev_class = 1 }; + + netns_thread_t netns_relay_handle = {.namespace_name = "relay", .netns_thread = relay_node, .arg = &relay_arg}; + run_node_in_namespace_thread(&netns_relay_handle); + + assert(wait_sync_flag(&test_random_port_binding_node_started, 5)); + fprintf(stderr, "\n\x1b[32mTest-04 : Relay Started\x1b[0m\n"); + + set_sync_flag(&test_random_port_binding_node_started, false); + peer_arg.join_invitation = peer_invite; + fprintf(stderr, "\n\x1b[32mTest-04: Got Invite {%s} for peer\x1b[0m\n", peer_arg.join_invitation); + netns_thread_t netns_peer_handle = {.namespace_name = "peer", .netns_thread = peer_node, .arg = &peer_arg}; + run_node_in_namespace_thread(&netns_peer_handle); + + assert(wait_sync_flag(&test_random_port_binding_node_started, 20)); + fprintf(stderr, "\n\x1b[32mTest-04 : Peer Started\x1b[0m\n"); + + set_sync_flag(&test_random_port_binding_node_started, false); + nut_arg.join_invitation = nut_invite; + fprintf(stderr, "\n\x1b[32mTest-04: Got Invite {%s} for nut\x1b[0m\n", nut_arg.join_invitation); + netns_thread_t netns_nut_handle = {.namespace_name = "nut", .netns_thread = nut_node, .arg = &nut_arg}; + run_node_in_namespace_thread(&netns_nut_handle); + + assert(wait_sync_flag(&test_random_port_binding_node_started, 20)); + fprintf(stderr, "\n\x1b[32mTest-04 : Nut Started\x1b[0m\n"); + + set_sync_flag(&test_random_port_binding_make_switch, true); + fprintf(stderr, "\n\x1b[32mTest-04 : Making Switch\x1b[0m\n"); + + assert(wait_sync_flag(&test_random_port_binding_node_connected, 300)); + + fprintf(stderr, "\n\x1b[32mDone Test-04\x1b[0m\n"); + + assert(wait_sync_flag(&test_random_port_binding_relay_closed, 10)); + assert(wait_sync_flag(&test_random_port_binding_peer_closed, 10)); + assert(wait_sync_flag(&test_random_port_binding_nut_closed, 10)); + + return true; +} + +/* Test Steps for Random port bindings Test Case # 5 */ +static void test_case_mesh_random_port_bindings_05(void **state) { + execute_test(test_steps_mesh_random_port_bindings_05, state); + return; +} + +/* Test Steps for Random port bindings Test Case # 5 + + Test Steps: + 1. Create three node nut, peer and relay in same name spaces. + 2. Join nut and peer to relay with invitation. + 3. Stop the three nodes and change the ports of nut and peer. + 4. Close the relay node and start nut and peer nodes again. + Expected Result: + NUT and Peer should be able to discover each others port with the help + of CATTA and form the direct meta connection. +*/ +static bool test_steps_mesh_random_port_bindings_05(void) { + localnode = true; + + mesh_arg_t relay_arg = {.node_name = "relay", .confbase = "relay", .app_name = "chat", .dev_class = 1 }; + mesh_arg_t peer_arg = {.node_name = "peer", .confbase = "peer", .app_name = "chat", .dev_class = 1 }; + mesh_arg_t nut_arg = {.node_name = "nut", .confbase = "nut", .app_name = "chat", .dev_class = 1 }; + + netns_thread_t netns_relay_handle = {.namespace_name = "relay", .netns_thread = relay_node, .arg = &relay_arg}; + run_node_in_namespace_thread(&netns_relay_handle); + + assert(wait_sync_flag(&test_random_port_binding_node_started, 20)); + + set_sync_flag(&test_random_port_binding_node_started, false); + peer_arg.join_invitation = peer_invite; + netns_thread_t netns_peer_handle = {.namespace_name = "peer", .netns_thread = peer_node, .arg = &peer_arg}; + run_node_in_namespace_thread(&netns_peer_handle); + + assert(wait_sync_flag(&test_random_port_binding_node_started, 20)); + + set_sync_flag(&test_random_port_binding_node_started, false); + nut_arg.join_invitation = nut_invite; + netns_thread_t netns_nut_handle = {.namespace_name = "nut", .netns_thread = nut_node, .arg = &nut_arg}; + run_node_in_namespace_thread(&netns_nut_handle); + + assert(wait_sync_flag(&test_random_port_binding_node_started, 20)); + + assert(wait_sync_flag(&test_random_port_binding_peer_reachable, 300)); + + set_sync_flag(&test_random_port_binding_make_switch, true); + + assert(wait_sync_flag(&test_random_port_binding_node_connected, 300)); + + fprintf(stderr, "\n\x1b[32mDone Test-05\x1b[0m\n"); + + assert(wait_sync_flag(&test_random_port_binding_relay_closed, 10)); + assert(wait_sync_flag(&test_random_port_binding_peer_closed, 10)); + assert(wait_sync_flag(&test_random_port_binding_nut_closed, 10)); + + return true; +} + +// Optimal PMTU test case driver + +int test_meshlink_random_port_bindings02(void) { + interface_t nut_ifs[] = {{.if_peer = "wan_bridge"}}; + namespace_t nut = { + .name = "nut", + .type = HOST, + .interfaces = nut_ifs, + .interfaces_no = 1, + }; + + interface_t peer_ifs[] = {{.if_peer = "wan_bridge"}}; + namespace_t peer = { + .name = "peer", + .type = HOST, + .interfaces = peer_ifs, + .interfaces_no = 1, + }; + + interface_t relay_ifs[] = {{.if_peer = "wan_bridge"}}; + namespace_t relay = { + .name = "relay", + .type = HOST, + .interfaces = relay_ifs, + .interfaces_no = 1, + }; + + interface_t wan_ifs[] = { { .if_peer = "nut" }, { .if_peer = "peer" }, { .if_peer = "relay" } }; + namespace_t wan_bridge = { + .name = "wan_bridge", + .type = BRIDGE, + .interfaces = wan_ifs, + .interfaces_no = 3, + }; + + namespace_t test_random_port_bindings_02_nodes[] = {wan_bridge, nut, peer, relay }; + + netns_state_t test_port_bindings_nodes = { + .test_case_name = "test_case_random_port_bindings_02", + .namespaces = test_random_port_bindings_02_nodes, + .num_namespaces = 4, + }; + test_random_port_bindings_state = &test_port_bindings_nodes; + + const struct CMUnitTest blackbox_group0_tests[] = { + cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_04, setup_test, teardown_test, + (void *)&test_random_port_bindings_state), + cmocka_unit_test_prestate_setup_teardown(test_case_mesh_random_port_bindings_05, setup_test, teardown_test, + (void *)&test_random_port_bindings_state) + }; + total_tests += sizeof(blackbox_group0_tests) / sizeof(blackbox_group0_tests[0]); + + return cmocka_run_group_tests(blackbox_group0_tests, NULL, NULL); +} diff --git a/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings02.h b/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings02.h new file mode 100644 index 00000000..03adb237 --- /dev/null +++ b/test/blackbox/run_blackbox_tests/test_cases_random_port_bindings02.h @@ -0,0 +1,28 @@ +#ifndef TEST_CASES_RANDOM_PORT_BINDINGS02_H +#define TEST_CASES_RANDOM_PORT_BINDINGS02_H + +/* + test_cases_random_port_bindings02.h -- Declarations for Individual Test Case implementation functions + Copyright (C) 2018 Guus Sliepen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include + +extern int test_meshlink_random_port_bindings02(void); +extern int total_tests; + +#endif //TEST_CASES_RANDOM_PORT_BINDINGS02_H \ No newline at end of file -- 2.39.2