]> git.meshlink.io Git - meshlink/commitdiff
Add test cases for random port bindings
authorlakshminarayanagurram <lakshminarayana@elear.solutions>
Tue, 4 Jun 2019 02:03:33 +0000 (07:33 +0530)
committerGuus Sliepen <guus@meshlink.io>
Wed, 5 Jun 2019 18:51:22 +0000 (20:51 +0200)
src/devtools.c
src/devtools.h
src/meshlink.c
src/meshlink.sym
test/blackbox/common/network_namespace_framework.c
test/blackbox/run_blackbox_tests/Makefile.am
test/blackbox/run_blackbox_tests/run_blackbox_tests.c
test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.c [new file with mode: 0644]
test/blackbox/run_blackbox_tests/test_cases_random_port_bindings01.h [new file with mode: 0644]
test/blackbox/run_blackbox_tests/test_cases_random_port_bindings02.c [new file with mode: 0644]
test/blackbox/run_blackbox_tests/test_cases_random_port_bindings02.h [new file with mode: 0644]

index 6d78119c49846a3c177ce575742adda961c67b45..f7d388cad90826cea6b683829cfd2cba926c3481 100644 (file)
 
 #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.
index 97924384aff55e04fe6034d3b055b3349f5d5696..04a334529627f50ba73ae08b7d5a55c38d39daee 100644 (file)
@@ -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
index 496e5d3c753763560bf173591a3aa8f883559dd6..bd25e2aece872f13ba758b15d3993ba8b9d9e76e 100644 (file)
@@ -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));
index bc275939e04e231c068470d9137a70d8ebbba8fd..514b0f5b878b225b5e7a5953e9ab812ee14cde86 100644 (file)
@@ -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
index d72286e9c5cd30875c4f97d11c99023702c3bb6e..1c39bcccba7659ff5beadf3ac15df14ee73c9be1 100644 (file)
@@ -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);
 
index 89dc838208ddf20ea7c922bf3b47130c80b89c19..379ce46ad11cd4c7363a9e93cefe41802c20b111 100644 (file)
@@ -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)
index b174ab7e2314169af9776c970b572e4290ddbe9c..62387c94991e6af9d06e932470c1dd05f351d2e4 100644 (file)
@@ -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 (file)
index 0000000..24a7e40
--- /dev/null
@@ -0,0 +1,304 @@
+/*
+    test_cases_random_port_bindings01.c -- Execution of specific meshlink black box test cases
+    Copyright (C) 2018  Guus Sliepen <guus@meshlink.io>
+
+    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 <stdlib.h>
+#include <stdarg.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <assert.h>
+#include <string.h>
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <netdb.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+
+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 (file)
index 0000000..c79ea91
--- /dev/null
@@ -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 <guus@meshlink.io>
+
+    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 <stdbool.h>
+
+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 (file)
index 0000000..2d24c3a
--- /dev/null
@@ -0,0 +1,422 @@
+/*
+    test_optimal_pmtu.c -- Execution of specific meshlink black box test cases
+    Copyright (C) 2019  Guus Sliepen <guus@meshlink.io>
+
+    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 <stdlib.h>
+#include <stdarg.h>
+#include <setjmp.h>
+#include <cmocka.h>
+#include <assert.h>
+#include <pthread.h>
+#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 (file)
index 0000000..03adb23
--- /dev/null
@@ -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 <guus@meshlink.io>
+
+    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 <stdbool.h>
+
+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