]> git.meshlink.io Git - meshlink/commitdiff
Add test cases for the PMTU discovery mechanism.
authorGuus Sliepen <guus@meshlink.io>
Thu, 31 Jan 2019 21:21:52 +0000 (22:21 +0100)
committerGuus Sliepen <guus@meshlink.io>
Thu, 31 Jan 2019 21:52:53 +0000 (22:52 +0100)
18 files changed:
test/blackbox/Makefile.am
test/blackbox/run_blackbox_tests/Makefile.am
test/blackbox/run_blackbox_tests/run_blackbox_tests.c
test/blackbox/run_blackbox_tests/test_optimal_pmtu.c [new file with mode: 0644]
test/blackbox/run_blackbox_tests/test_optimal_pmtu.h [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_01/Makefile.am [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_01/node_sim_nut.c [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_01/node_sim_peer.c [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_01/node_sim_relay.c [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_02/Makefile.am [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_03/Makefile.am [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_04/Makefile.am [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_05/Makefile.am [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_06/Makefile.am [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_07/Makefile.am [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_07/node_sim_nut.c [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_07/node_sim_peer.c [new file with mode: 0644]
test/blackbox/test_case_optimal_pmtu_07/node_sim_relay.c [new file with mode: 0644]

index 3d0e6005d6c812509adb19b384dbd7a8d0be3ac0..dd2d6af1b6db260961e14f498fb2500a90742f7c 100644 (file)
@@ -13,7 +13,14 @@ SUBDIRS = \
        test_case_meta_conn_02 \
        test_case_meta_conn_03 \
        test_case_meta_conn_04 \
-       test_case_meta_conn_05
+       test_case_meta_conn_05 \
+       test_case_optimal_pmtu_01 \
+       test_case_optimal_pmtu_02 \
+       test_case_optimal_pmtu_03 \
+       test_case_optimal_pmtu_04 \
+       test_case_optimal_pmtu_05 \
+       test_case_optimal_pmtu_06 \
+       test_case_optimal_pmtu_07 
 
 
 gen_invite_SOURCES = util/gen_invite.c common/common_handlers.c common/test_step.c common/mesh_event_handler.c
index f5691ea49524c8814661b5e6c73f4f1bfba2453f..e1e7b0a4c266c1e96ce85eb6681825127b218c94 100644 (file)
@@ -45,7 +45,8 @@ run_blackbox_tests_SOURCES = \
        test_cases_channel_close.c \
        test_cases_channel_send.c \
        test_cases_channel_shutdown.c \
-       test_cases_channel_conn.c
+       test_cases_channel_conn.c \
+       test_optimal_pmtu.c
 
 run_blackbox_tests_LDADD = ../../../src/libmeshlink.la $(LXC_LIBS) $(CMOCKA_LIBS)
 run_blackbox_tests_CFLAGS = -D_GNU_SOURCE $(LXC_CFLAGS) $(CMOCKA_CFLAGS)
index 5cfb21f77a2fed856b2d7c195b80073fc82971ad..0b706e1225e5a2f8c786ebfc6f206efa225c33ca 100644 (file)
@@ -62,6 +62,7 @@
 #include "test_cases_channel_set_poll_cb.h"
 #include "test_cases_channel_set_receive_cb.h"
 #include "test_cases_hint_address.h"
+#include "test_optimal_pmtu.h"
 
 #include "test_cases_channel_conn.h"
 
@@ -83,7 +84,7 @@ int main(int argc, char *argv[]) {
 
        int failed_tests = 0;
 
-       /*failed_tests += test_meta_conn();
+       failed_tests += test_meta_conn();
        failed_tests += test_meshlink_set_status_cb();
        failed_tests += test_meshlink_join();
        failed_tests += test_meshlink_set_channel_poll_cb();
@@ -123,8 +124,9 @@ int main(int argc, char *argv[]) {
        failed_tests += test_meshlink_channel_open();
        failed_tests += test_meshlink_channel_close();
 
-       failed_tests += test_meshlink_channel_conn();*/
+       failed_tests += test_meshlink_channel_conn();
 
+       failed_tests += test_optimal_pmtu();
 
        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_optimal_pmtu.c b/test/blackbox/run_blackbox_tests/test_optimal_pmtu.c
new file mode 100644 (file)
index 0000000..1c44d3e
--- /dev/null
@@ -0,0 +1,713 @@
+/*
+    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 "execute_tests.h"
+#include "test_cases.h"
+#include "pthread.h"
+#include "../common/containers.h"
+#include "../common/test_step.h"
+#include "../common/common_handlers.h"
+#include "../common/mesh_event_handler.h"
+#include "test_optimal_pmtu.h"
+
+#define RELAY_ID "0"
+#define PEER_ID  "1"
+#define NUT_ID   "2"
+#define PEER_NAT "peer_nat"
+#define NUT_NAT "nut_nat"
+
+#pragma pack(1)
+
+static void test_case_optimal_pmtu_01(void **state);
+static bool test_steps_optimal_pmtu_01(void);
+static void test_case_optimal_pmtu_02(void **state);
+static bool test_steps_optimal_pmtu_02(void);
+static void test_case_optimal_pmtu_03(void **state);
+static bool test_steps_optimal_pmtu_03(void);
+static void test_case_optimal_pmtu_04(void **state);
+static bool test_steps_optimal_pmtu_04(void);
+static void test_case_optimal_pmtu_05(void **state);
+static bool test_steps_optimal_pmtu_05(void);
+static void test_case_optimal_pmtu_06(void **state);
+static bool test_steps_optimal_pmtu_06(void);
+static void test_case_optimal_pmtu_07(void **state);
+static bool test_steps_optimal_pmtu_07(void);
+static void tcpdump_in_container(const char *cmd, const char *container_name);
+
+static char *test_optimal_pmtu_1_nodes[] = { "relay", "peer", "nut" };
+static black_box_state_t test_pmtu_state_01 = {
+       .test_case_name =  "test_case_optimal_pmtu_01",
+       .node_names =  test_optimal_pmtu_1_nodes,
+       .num_nodes =  3,
+};
+
+static black_box_state_t test_pmtu_state_02 = {
+       .test_case_name =  "test_case_optimal_pmtu_02",
+       .node_names =  test_optimal_pmtu_1_nodes,
+       .num_nodes =  3,
+};
+
+static black_box_state_t test_pmtu_state_03 = {
+       .test_case_name =  "test_case_optimal_pmtu_03",
+       .node_names =  test_optimal_pmtu_1_nodes,
+       .num_nodes =  3,
+};
+
+static black_box_state_t test_pmtu_state_04 = {
+       .test_case_name =  "test_case_optimal_pmtu_04",
+       .node_names =  test_optimal_pmtu_1_nodes,
+       .num_nodes =  3,
+};
+
+static black_box_state_t test_pmtu_state_05 = {
+       .test_case_name =  "test_case_optimal_pmtu_05",
+       .node_names =  test_optimal_pmtu_1_nodes,
+       .num_nodes =  3,
+};
+
+static black_box_state_t test_pmtu_state_06 = {
+       .test_case_name =  "test_case_optimal_pmtu_06",
+       .node_names =  test_optimal_pmtu_1_nodes,
+       .num_nodes =  3,
+};
+
+static black_box_state_t test_pmtu_state_07 = {
+       .test_case_name =  "test_case_optimal_pmtu_07",
+       .node_names =  test_optimal_pmtu_1_nodes,
+       .num_nodes =  3,
+};
+
+static pmtu_attr_t node_pmtu_peer;
+static pmtu_attr_t node_pmtu_relay;
+static char *import = NULL;
+
+
+static void tcpdump_in_container(const char *cmd, const char *container_name) {
+       execute_in_container(cmd, container_name, true);
+}
+
+static int setup_pmtu_test_case(void **state) {
+       char *ret_str;
+       char container_name[200];
+
+       PRINT_TEST_CASE_MSG("\n\n======================================================================\n\n\n");
+
+       fprintf(stderr, "Setting up Containers\n");
+       state_ptr = (black_box_state_t *)(*state);
+
+       // Setup containers for test cases
+       setup_containers(state);
+
+       // Switch bridges of test containers from lxcbr0 to it's respective NAT bridges
+       assert(snprintf(container_name, sizeof(container_name), "%s_%s", state_ptr->test_case_name, "nut") >= 0);
+       container_switch_bridge(container_name, lxc_path, lxc_bridge, "nut_nat_bridge");
+       assert(snprintf(container_name, sizeof(container_name), "%s_%s", state_ptr->test_case_name, "peer") >= 0);
+       container_switch_bridge(container_name, lxc_path, lxc_bridge, "peer_nat_bridge");
+
+       // Flush iptable filter rules if there are any
+       flush_nat_rules(NUT_NAT, "-t filter");
+       flush_nat_rules(PEER_NAT, "-t filter");
+
+       // Reset the MTU size of NAT's public interface to 1500 bytes
+       assert(change_container_mtu(NUT_NAT, PUB_INTERFACE, 1500) == NULL);
+       assert(change_container_mtu(PEER_NAT, PUB_INTERFACE, 1500) == NULL);
+
+       // Flush all the pushed data in the meshlink event handler receive queue
+       mesh_events_flush();
+
+       return EXIT_SUCCESS;
+}
+
+static int teardown_pmtu_test_case(void **state) {
+
+       // Reset the MTU size of NAT's public interface to 1500 bytes
+       change_container_mtu(NUT_NAT, PUB_INTERFACE, 1500);
+       change_container_mtu(PEER_NAT, PUB_INTERFACE, 1500);
+
+       // Flush iptable filter rules if there are any
+       flush_nat_rules(NUT_NAT, "-t filter");
+       flush_nat_rules(PEER_NAT, "-t filter");
+
+       black_box_state_t *test_state = (black_box_state_t *)(*state);
+       int i;
+
+       // Send SIG_TERM to all the running nodes
+       for(i = 0; i < test_state->num_nodes; i++) {
+               /* Shut down node */
+               node_step_in_container(test_state->node_names[i], "SIGTERM");
+       }
+
+       return teardown_test(state);
+}
+
+// Print the calculated values of MTU parameters obtained from the node
+static void print_mtu_calc(pmtu_attr_t node_pmtu) {
+       fprintf(stderr, "MTU size : %d\n", node_pmtu.mtu_size);
+       fprintf(stderr, "Probes took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes);
+       fprintf(stderr, "Probes total length took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes_total_len);
+       fprintf(stderr, "Time took for calculating PMTU discovery : %lu\n", node_pmtu.mtu_discovery.time);
+       fprintf(stderr, "Total MTU ping probes : %d\n", node_pmtu.mtu_ping.probes);
+       fprintf(stderr, "Total MTU ping probes length : %d\n", node_pmtu.mtu_ping.probes_total_len);
+       float avg = 0;
+
+       if(node_pmtu.mtu_ping.probes) {
+               avg = (float)node_pmtu.mtu_ping.time / (float)node_pmtu.mtu_ping.probes;
+       }
+
+       fprintf(stderr, "Average MTU ping probes ping time : %f\n", avg);
+       fprintf(stderr, "Total probes received %d\n", node_pmtu.mtu_recv_probes.probes);
+       fprintf(stderr, "Total probes sent %d\n", node_pmtu.mtu_sent_probes.probes);
+}
+
+static int black_box_pmtu_group_setup(void **state) {
+       const char *nodes[] = { "relay", "peer", "nut" };
+       int num_nodes = sizeof(nodes) / sizeof(nodes[0]);
+
+       /*
+                               ------------
+           ____________________|  lxcbr0  |_____________________
+          |eth0                ------------                    |eth0
+        -------------               |                  --------------
+       // NUT NAT  //               |                 //  Peer NAT //
+        ------------           -------------           --------------
+              |eth1            |           |                   |eth1
+              |                |   relay   |                   |
+       -------------------     |           |        --------------------
+       |  nut_nat_bridge |     -------------        |  peer_nat_bridge |
+       -------------------                          --------------------
+              |                                                |
+       ---------------                                ------------------
+       |             |                                |                |
+       |     NUT     |                                |      peer      |
+       |             |                                |                |
+       ---------------                                ------------------
+       */
+
+       PRINT_TEST_CASE_MSG("Creating Containers\n");
+
+       // Create Node-Under-Test(NUT) and Peer node's containers
+
+       destroy_containers();
+       create_containers(nodes, num_nodes);
+
+       // Create NAT containers for NUT and peer nodes
+
+       nat_create(NUT_NAT, lxc_path, FULLCONE_NAT);
+       nat_create(PEER_NAT, lxc_path, FULLCONE_NAT);
+
+       tcpdump_in_container("tcpdump udp -i any > tcpdump.log", NUT_NAT);
+
+       /*char cmd[200];
+       assert(snprintf(cmd, sizeof(cmd),
+                       "%s/" LXC_UTIL_REL_PATH "/log_drops.sh %s",
+                       meshlink_root_path, NUT_NAT) >= 0);
+       assert(system(cmd) == 0);*/
+
+       // Switch the Node-under-test node and peer node containers to the respective bridge
+       // i.e making the node to sit behind the NAT's private network interface
+
+       container_switch_bridge("run_nut", lxc_path, lxc_bridge, "nut_nat_bridge");
+       container_switch_bridge("run_peer", lxc_path, lxc_bridge, "peer_nat_bridge");
+
+       // Open mesh event handling UDP socket
+       import = mesh_event_sock_create(eth_if_name);
+       assert(import);
+
+       return 0;
+}
+
+static int black_box_pmtu_group_teardown(void **state) {
+       PRINT_TEST_CASE_MSG("Destroying Containers\n");
+       destroy_containers();
+       nat_destroy(NUT_NAT);
+       nat_destroy(PEER_NAT);
+
+       mesh_event_destroy();
+
+       return 0;
+}
+
+static bool mtu_calc_peer = false;
+static bool mtu_calc_relay = false;
+
+// Common event handler callback for all the test cases
+static bool event_mtu_handle_cb(mesh_event_payload_t payload) {
+       char event_node_name[][10] = {"RELAY", "PEER", "NUT"};
+       fprintf(stderr, " %s : ", event_node_name[payload.client_id]);
+       char *name;
+       uint32_t payload_length = payload.payload_length;
+       uint32_t node_pmtu_calc_size = sizeof(pmtu_attr_t);
+
+       switch(payload.mesh_event) {
+       case META_CONN_CLOSED   :
+               name = (char *)payload.payload;
+               fprintf(stderr, "NUT closed connection with %s\n", name);
+               break;
+
+       case META_CONN_SUCCESSFUL   :
+               name = (char *)payload.payload;
+               fprintf(stderr, "NUT made connection with %s\n", name);
+               break;
+
+       case NODE_JOINED  :
+               name = (char *)payload.payload;
+               fprintf(stderr, "Node %s joined with NUT\n", name);
+               break;
+
+       case NODE_LEFT  :
+               name = (char *)payload.payload;
+               fprintf(stderr, "Node %s the left mesh\n", name);
+               break;
+
+       case ERR_NETWORK            :
+               name = (char *)payload.payload;
+               fprintf(stderr, "NUT closed channel with %s\n", name);
+               break;
+
+       case NODE_STARTED           :
+               fprintf(stderr, "%s node started\n", event_node_name[payload.client_id]);
+               break;
+
+       case CHANNEL_OPENED         :
+               fprintf(stderr, "Channel opened\n");
+               break;
+
+       case OPTIMAL_PMTU_PEER      :
+               assert(payload.payload);
+               fprintf(stderr, "Obtained peer MTU values from NUT\n");
+               memcpy(&node_pmtu_peer, payload.payload, payload_length);
+               fprintf(stderr, "NUT and peer PMTU handling in 120 seconds with ICMP unblocked\n");
+               print_mtu_calc(node_pmtu_peer);
+               mtu_calc_peer = true;
+
+               if(mtu_calc_peer && mtu_calc_relay) {
+                       return true;
+               }
+
+               break;
+
+       case OPTIMAL_PMTU_RELAY      :
+               assert(payload.payload);
+               //assert(payload_length != node_pmtu_calc_size);
+               fprintf(stderr, "Obtained relay MTU values from NUT\n");
+               memcpy(&node_pmtu_relay, payload.payload, payload_length);
+               fprintf(stderr, "NUT and peer PMTU handling in 120 seconds with ICMP unblocked\n");
+               print_mtu_calc(node_pmtu_relay);
+               mtu_calc_relay = true;
+
+               if(mtu_calc_peer && mtu_calc_relay) {
+                       return true;
+               }
+
+               break;
+
+       default :
+               fprintf(stderr, "UNDEFINED EVENT RECEIVED (%d)\n", payload.mesh_event);
+       }
+
+       return false;
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 1 -
+    Validating NUT MTU parameters without blocking ICMP under designed
+    network topology */
+static void test_case_optimal_pmtu_01(void **state) {
+       execute_test(test_steps_optimal_pmtu_01, state);
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 1 - Success case
+
+    Test Steps:
+    1. Create NAT setup and run each node instances in discrete containers.
+    2. Open a channel from NUT to peer and hence triggering Peer to peer connection
+    3. Send the analyzed MTU parameters mesh event information to test driver
+    Expected Result:
+      NUT and Peer should be able to hole puch the NATs and MTU parameters should be in
+      the expected range
+*/
+static bool test_steps_optimal_pmtu_01(void) {
+       char *invite_peer, *invite_nut;
+
+       mtu_calc_peer = false;
+       mtu_calc_relay = false;
+       memset(&node_pmtu_peer, 0, sizeof(node_pmtu_peer));
+       memset(&node_pmtu_relay, 0, sizeof(node_pmtu_relay));
+
+       // Invite peer and nut nodes by relay node
+       invite_peer = invite_in_container("relay", "peer");
+       assert(invite_peer);
+       invite_nut = invite_in_container("relay", NUT_NODE_NAME);
+       assert(invite_nut);
+
+       // Launch NUT, relay and peer nodes in discrete containers
+       node_sim_in_container_event("relay", "1", NULL, RELAY_ID, import);
+       node_sim_in_container_event("peer", "1", invite_peer, PEER_ID, import);
+       node_sim_in_container_event("nut", "1", invite_nut, NUT_ID, import);
+
+       // Wait for test case events
+       bool ret = wait_for_event(event_mtu_handle_cb, PING_TRACK_TIMEOUT + 100);
+       assert_int_equal(ret && mtu_calc_peer && mtu_calc_relay, true);
+
+       // Verify the obtained values
+       assert_in_range(node_pmtu_peer.mtu_size, 1450, 1501);
+       assert_in_range(node_pmtu_peer.mtu_discovery.probes, 38, 42);
+       assert_in_range(node_pmtu_relay.mtu_size, 1450, 1501);
+       assert_in_range(node_pmtu_relay.mtu_discovery.probes, 38, 42);
+
+       return true;
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 2 -
+    Validating NUT MTU parameters blocking ICMP under designed
+    network topology */
+static void test_case_optimal_pmtu_02(void **state) {
+       execute_test(test_steps_optimal_pmtu_02, state);
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 2 -
+
+    Test Steps:
+    1. Create NAT setup and run each node instances in discrete containers,
+    2. Block ICMP protocol at NUT's NAT
+    3. Open a channel from NUT to peer and hence triggering Peer to peer connection
+    4. Send the analyzed MTU parameters mesh event information to test driver
+    Expected Result:
+      NUT and Peer should be able to hole puch the NATs and MTU parameters should be in
+      the expected range
+*/
+static bool test_steps_optimal_pmtu_02(void) {
+       char *invite_peer, *invite_nut;
+
+       mtu_calc_peer = false;
+       mtu_calc_relay = false;
+       memset(&node_pmtu_peer, 0, sizeof(node_pmtu_peer));
+       memset(&node_pmtu_relay, 0, sizeof(node_pmtu_relay));
+
+       block_icmp(NUT_NAT);
+
+       invite_peer = invite_in_container("relay", "peer");
+       assert(invite_peer);
+       invite_nut = invite_in_container("relay", NUT_NODE_NAME);
+       assert(invite_nut);
+       node_sim_in_container_event("relay", "1", NULL, RELAY_ID, import);
+       node_sim_in_container_event("peer", "1", invite_peer, PEER_ID, import);
+       node_sim_in_container_event("nut", "1", invite_nut, NUT_ID, import);
+
+       bool ret = wait_for_event(event_mtu_handle_cb, PING_TRACK_TIMEOUT + 100);
+       assert_int_equal(ret && mtu_calc_peer && mtu_calc_relay, true);
+
+       assert_in_range(node_pmtu_peer.mtu_size, 1450, 1501);
+       assert_in_range(node_pmtu_peer.mtu_discovery.probes, 38, 42);
+       assert_in_range(node_pmtu_relay.mtu_size, 1450, 1501);
+       assert_in_range(node_pmtu_relay.mtu_discovery.probes, 38, 42);
+       return true;
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 3 -
+    Validating NUT MTU parameters with MTU size of NAT = 1250 under designed
+    network topology */
+static void test_case_optimal_pmtu_03(void **state) {
+       execute_test(test_steps_optimal_pmtu_03, state);
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 3 -
+
+    Test Steps:
+    1. Create NAT setup and run each node instances in discrete containers,
+    2. Change the MTU size of NUT's NAT to 1250
+    3. Open a channel from NUT to peer and hence triggering Peer to peer connection
+    4. Send the analyzed MTU parameters mesh event information to test driver
+    Expected Result:
+      NUT and Peer should be able to hole puch the NATs and MTU parameters should be in
+      the expected range
+*/
+static bool test_steps_optimal_pmtu_03(void) {
+       char *invite_peer, *invite_nut;
+
+       mtu_calc_peer = false;
+       mtu_calc_relay = false;
+       memset(&node_pmtu_peer, 0, sizeof(node_pmtu_peer));
+       memset(&node_pmtu_relay, 0, sizeof(node_pmtu_relay));
+
+       change_container_mtu(NUT_NAT, "eth0", 1250);
+
+       invite_peer = invite_in_container("relay", "peer");
+       assert(invite_peer);
+       invite_nut = invite_in_container("relay", NUT_NODE_NAME);
+       assert(invite_nut);
+       node_sim_in_container_event("relay", "1", NULL, RELAY_ID, import);
+       node_sim_in_container_event("peer", "1", invite_peer, PEER_ID, import);
+       node_sim_in_container_event("nut", "1", invite_nut, NUT_ID, import);
+
+       bool ret = wait_for_event(event_mtu_handle_cb, PING_TRACK_TIMEOUT + 100);
+       assert_int_equal(ret && mtu_calc_peer && mtu_calc_relay, true);
+
+       assert_in_range(node_pmtu_relay.mtu_size, 1200, 1250);
+       assert_in_range(node_pmtu_peer.mtu_size, 1200, 1250);
+       assert_in_range(node_pmtu_peer.mtu_discovery.probes, 38, 42);
+       assert_in_range(node_pmtu_relay.mtu_size, 1450, 1501);
+       assert_in_range(node_pmtu_relay.mtu_discovery.probes, 38, 42);
+       return true;
+}
+/* Test Steps for optimal PMTU discovery Test Case # 4 -
+    Validating NUT MTU parameters with MTU size of NAT = 1000 under designed
+    network topology */
+static void test_case_optimal_pmtu_04(void **state) {
+       execute_test(test_steps_optimal_pmtu_04, state);
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 4 -
+
+    Test Steps:
+    1. Create NAT setup and run each node instances in discrete containers,
+    2. Change the MTU size of NUT's NAT to 1000
+    3. Open a channel from NUT to peer and hence triggering Peer to peer connection
+    4. Send the analyzed MTU parameters mesh event information to test driver
+    Expected Result:
+      NUT and Peer should be able to hole puch the NATs and MTU parameters should be in
+      the expected range
+*/
+static bool test_steps_optimal_pmtu_04(void) {
+       char *invite_peer, *invite_nut;
+
+       mtu_calc_peer = false;
+       mtu_calc_relay = false;
+       memset(&node_pmtu_peer, 0, sizeof(node_pmtu_peer));
+       memset(&node_pmtu_relay, 0, sizeof(node_pmtu_relay));
+
+       change_container_mtu(NUT_NAT, "eth0", 1000);
+
+       invite_peer = invite_in_container("relay", "peer");
+       assert(invite_peer);
+       invite_nut = invite_in_container("relay", NUT_NODE_NAME);
+       assert(invite_nut);
+       node_sim_in_container_event("relay", "1", NULL, RELAY_ID, import);
+       node_sim_in_container_event("peer", "1", invite_peer, PEER_ID, import);
+       node_sim_in_container_event("nut", "1", invite_nut, NUT_ID, import);
+
+       bool ret = wait_for_event(event_mtu_handle_cb, PING_TRACK_TIMEOUT + 100);
+       assert_int_equal(ret && mtu_calc_peer && mtu_calc_relay, true);
+
+       assert_in_range(node_pmtu_relay.mtu_size, 925, 1000);
+       assert_in_range(node_pmtu_peer.mtu_size, 925, 1000);
+       assert_in_range(node_pmtu_peer.mtu_discovery.probes, 38, 42);
+       assert_in_range(node_pmtu_relay.mtu_size, 1450, 1501);
+       assert_in_range(node_pmtu_relay.mtu_discovery.probes, 38, 42);
+       return true;
+}
+/* Test Steps for optimal PMTU discovery Test Case # 5 -
+    Validating NUT MTU parameters with MTU size of NAT = 800 under designed
+    network topology */
+static void test_case_optimal_pmtu_05(void **state) {
+       execute_test(test_steps_optimal_pmtu_05, state);
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 5 -
+
+    Test Steps:
+    1. Create NAT setup and run each node instances in discrete containers,
+    2. Change the MTU size of NUT's NAT to 800
+    3. Open a channel from NUT to peer and hence triggering Peer to peer connection
+    4. Send the analyzed MTU parameters mesh event information to test driver
+    Expected Result:
+      NUT and Peer should be able to hole puch the NATs and MTU parameters should be in
+      the expected range
+*/
+static bool test_steps_optimal_pmtu_05(void) {
+       char *invite_peer, *invite_nut;
+
+       mtu_calc_peer = false;
+       mtu_calc_relay = false;
+       memset(&node_pmtu_peer, 0, sizeof(node_pmtu_peer));
+       memset(&node_pmtu_relay, 0, sizeof(node_pmtu_relay));
+
+       change_container_mtu(NUT_NAT, "eth0", 800);
+
+       invite_peer = invite_in_container("relay", "peer");
+       assert(invite_peer);
+       invite_nut = invite_in_container("relay", NUT_NODE_NAME);
+       assert(invite_nut);
+       node_sim_in_container_event("relay", "1", NULL, RELAY_ID, import);
+       node_sim_in_container_event("peer", "1", invite_peer, PEER_ID, import);
+       node_sim_in_container_event("nut", "1", invite_nut, NUT_ID, import);
+
+       bool ret = wait_for_event(event_mtu_handle_cb, PING_TRACK_TIMEOUT + 100);
+       assert_int_equal(ret && mtu_calc_peer && mtu_calc_relay, true);
+
+       assert_in_range(node_pmtu_relay.mtu_size, 750, 800);
+       assert_in_range(node_pmtu_peer.mtu_size, 750, 800);
+       //assert_in_range(node_pmtu_peer.mtu_discovery.probes, 38, 42);
+       //assert_in_range(node_pmtu_relay.mtu_size, 1450, 1501);
+       //assert_in_range(node_pmtu_relay.mtu_discovery.probes, 38, 42);
+       return true;
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 6 -
+    Flushing the tracked connections via NUT NAT for every 60 seconds */
+static void test_case_optimal_pmtu_06(void **state) {
+       execute_test(test_steps_optimal_pmtu_06, state);
+}
+
+static pthread_t pmtu_test_case_conntrack_thread;
+static void *conntrcak_flush(void *timeout) {
+       int thread_timeout = *((int *)timeout);
+       time_t thread_end_time = time(NULL) + thread_timeout;
+
+       while(thread_end_time > time(NULL)) {
+               sleep(90);
+               flush_conntrack(NUT_NAT);
+               flush_conntrack(PEER_NAT);
+       }
+
+}
+
+static int teardown_conntrack_test_case(void **state) {
+
+       // Close the conntrack thread
+       pthread_cancel(pmtu_test_case_conntrack_thread);
+       // Close all the test cases
+       return teardown_pmtu_test_case(state);
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 6 -
+
+    Test Steps:
+    1. Create NAT setup and Launch conntrack thread which flushes the tracked connections for every 90 seconds
+    2. Run each node instances in discrete containers,
+    3. Open a channel from NUT to peer and hence triggering Peer to peer connection
+    4. Send the analyzed MTU parameters mesh event information to test driver
+    Expected Result:
+      NUT and Peer should be able to hole puch the NATs and MTU parameters should be in
+      the expected range
+*/
+static bool test_steps_optimal_pmtu_06(void) {
+       char *invite_peer, *invite_nut;
+       float ping_avg;
+
+       mtu_calc_peer = false;
+       mtu_calc_relay = false;
+       memset(&node_pmtu_peer, 0, sizeof(node_pmtu_peer));
+       memset(&node_pmtu_relay, 0, sizeof(node_pmtu_relay));
+
+       int thread_timeout = PING_TRACK_TIMEOUT + 30;
+       assert(!pthread_create(&pmtu_test_case_conntrack_thread, NULL, conntrcak_flush, &thread_timeout));
+       invite_peer = invite_in_container("relay", "peer");
+       assert(invite_peer);
+       invite_nut = invite_in_container("relay", NUT_NODE_NAME);
+       assert(invite_nut);
+       node_sim_in_container_event("relay", "1", NULL, RELAY_ID, import);
+       node_sim_in_container_event("peer", "1", invite_peer, PEER_ID, import);
+       node_sim_in_container_event("nut", "1", invite_nut, NUT_ID, import);
+
+       bool ret = wait_for_event(event_mtu_handle_cb, PING_TRACK_TIMEOUT + 100);
+       assert_int_equal(ret && mtu_calc_peer && mtu_calc_relay, true);
+
+       assert_in_range(node_pmtu_relay.mtu_size, 1440, 1500);
+       assert_in_range(node_pmtu_peer.mtu_size, 1440, 1500);
+       assert_in_range(node_pmtu_peer.mtu_ping.probes, 38, 42);
+       assert_in_range(node_pmtu_relay.mtu_ping.probes, 38, 42);
+
+       ping_avg = 0;
+
+       if(node_pmtu_peer.mtu_ping.probes) {
+               ping_avg = ping_avg = node_pmtu_peer.mtu_ping.time / node_pmtu_peer.mtu_ping.probes;
+       }
+
+       assert_in_range(ping_avg, 15.0, 20.0);
+
+       ping_avg = 0;
+
+       if(node_pmtu_relay.mtu_ping.probes) {
+               ping_avg = ping_avg = node_pmtu_relay.mtu_ping.time / node_pmtu_relay.mtu_ping.probes;
+       }
+
+       assert_in_range(ping_avg, 15.0, 20.0);
+       return true;
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 7 -
+    NUT sending data to peer node via channel for every 30 seconds
+    */
+static void test_case_optimal_pmtu_07(void **state) {
+       execute_test(test_steps_optimal_pmtu_07, state);
+}
+
+/* Test Steps for optimal PMTU discovery Test Case # 7 -
+
+    Test Steps:
+    1. Create NAT setup and run each node instances in discrete containers.
+    2. Open a channel from NUT to peer and hence triggering Peer to peer connection
+    3. Send data periodically via channel from NUT to peer node.
+    4. Send the analyzed MTU parameters mesh event information to test driver
+    Expected Result:
+      NUT and Peer should be able to hole puch the NATs and MTU parameters should be in
+      the expected range
+*/
+static bool test_steps_optimal_pmtu_07(void) {
+       char *invite_peer, *invite_nut;
+
+       mtu_calc_peer = false;
+       mtu_calc_relay = false;
+       memset(&node_pmtu_peer, 0, sizeof(node_pmtu_peer));
+       memset(&node_pmtu_relay, 0, sizeof(node_pmtu_relay));
+       invite_peer = invite_in_container("relay", "peer");
+       assert(invite_peer);
+       invite_nut = invite_in_container("relay", NUT_NODE_NAME);
+       assert(invite_nut);
+       node_sim_in_container_event("relay", "1", NULL, RELAY_ID, import);
+       node_sim_in_container_event("peer", "1", invite_peer, PEER_ID, import);
+       node_sim_in_container_event("nut", "1", invite_nut, NUT_ID, import);
+
+       bool ret = wait_for_event(event_mtu_handle_cb, PING_TRACK_TIMEOUT + 100);
+       assert_int_equal(ret && mtu_calc_peer && mtu_calc_relay, true);
+
+       assert_in_range(node_pmtu_peer.mtu_size, 1450, 1501);
+       assert_in_range(node_pmtu_peer.mtu_discovery.probes, 38, 42);
+       assert_in_range(node_pmtu_relay.mtu_size, 1450, 1501);
+       assert_in_range(node_pmtu_relay.mtu_discovery.probes, 38, 42);
+
+       return true;
+}
+
+// Optimal PMTU test case driver
+int test_optimal_pmtu(void) {
+       const struct CMUnitTest blackbox_group0_tests[] = {
+               cmocka_unit_test_prestate_setup_teardown(test_case_optimal_pmtu_01, setup_pmtu_test_case, teardown_pmtu_test_case,
+                               (void *)&test_pmtu_state_01),
+               cmocka_unit_test_prestate_setup_teardown(test_case_optimal_pmtu_02, setup_pmtu_test_case, teardown_pmtu_test_case,
+                               (void *)&test_pmtu_state_02),
+               cmocka_unit_test_prestate_setup_teardown(test_case_optimal_pmtu_03, setup_pmtu_test_case, teardown_pmtu_test_case,
+                               (void *)&test_pmtu_state_03),
+               cmocka_unit_test_prestate_setup_teardown(test_case_optimal_pmtu_04, setup_pmtu_test_case, teardown_pmtu_test_case,
+                               (void *)&test_pmtu_state_04),
+               cmocka_unit_test_prestate_setup_teardown(test_case_optimal_pmtu_05, setup_pmtu_test_case, teardown_pmtu_test_case,
+                               (void *)&test_pmtu_state_05),
+               cmocka_unit_test_prestate_setup_teardown(test_case_optimal_pmtu_06, setup_pmtu_test_case, teardown_pmtu_test_case,
+                               (void *)&test_pmtu_state_06),
+               cmocka_unit_test_prestate_setup_teardown(test_case_optimal_pmtu_07, setup_pmtu_test_case, teardown_pmtu_test_case,
+                               (void *)&test_pmtu_state_07),
+       };
+       total_tests += sizeof(blackbox_group0_tests) / sizeof(blackbox_group0_tests[0]);
+
+       return cmocka_run_group_tests(blackbox_group0_tests, black_box_pmtu_group_setup, NULL);
+}
diff --git a/test/blackbox/run_blackbox_tests/test_optimal_pmtu.h b/test/blackbox/run_blackbox_tests/test_optimal_pmtu.h
new file mode 100644 (file)
index 0000000..b3333c7
--- /dev/null
@@ -0,0 +1,63 @@
+#ifndef TEST_CASES_OPTIMAL_PMTU_H
+#define TEST_CASES_OPTIMAL_PMTU_H
+
+/*
+    test_optimal_pmtu.h -- Declarations for Individual Test Case implementation functions
+    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 <stdbool.h>
+
+#pragma pack(1)
+
+extern int test_optimal_pmtu(void);
+extern int total_tests;
+extern char *lxc_path;
+
+typedef struct pmtu_attr_para {
+       int    probes;
+       int    probes_total_len;
+       int    count;
+       time_t time;
+       time_t time_l;
+       time_t time_h;
+} pmtu_attr_para_t;
+
+typedef struct pmtu_attr {
+       pmtu_attr_para_t mtu_sent_probes;
+       pmtu_attr_para_t mtu_recv_probes;
+       pmtu_attr_para_t mtu_discovery;
+       pmtu_attr_para_t mtu_ping;
+       pmtu_attr_para_t mtu_increase;
+       pmtu_attr_para_t mtu_start;
+       int mtu_size;
+} pmtu_attr_t;
+
+#define NODE_PMTU_RELAY 0
+#define NODE_PMTU_PEER 1
+
+#define find_node_index(i, node_name) if(!strcasecmp(node_name, "peer")) {          \
+               i = NODE_PMTU_PEER;                        \
+       } else if(!strcasecmp(node_name, "relay")) {  \
+               i = NODE_PMTU_RELAY;                        \
+       } else {                                      \
+               abort();                                    \
+       }
+
+#define PING_TRACK_TIMEOUT 500
+
+#endif // TEST_CASES_OPTIMAL_PMTU_H
diff --git a/test/blackbox/test_case_optimal_pmtu_01/Makefile.am b/test/blackbox/test_case_optimal_pmtu_01/Makefile.am
new file mode 100644 (file)
index 0000000..8bde53d
--- /dev/null
@@ -0,0 +1,13 @@
+check_PROGRAMS = node_sim_peer node_sim_nut node_sim_relay
+
+node_sim_peer_SOURCES = node_sim_peer.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_peer_LDADD = ../../../src/libmeshlink.la
+node_sim_peer_CFLAGS = -D_GNU_SOURCE
+
+node_sim_nut_SOURCES = node_sim_nut.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_nut_LDADD = ../../../src/libmeshlink.la
+node_sim_nut_CFLAGS = -D_GNU_SOURCE
+
+node_sim_relay_SOURCES = node_sim_relay.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_relay_LDADD = ../../../src/libmeshlink.la
+node_sim_relay_CFLAGS = -D_GNU_SOURCE
diff --git a/test/blackbox/test_case_optimal_pmtu_01/node_sim_nut.c b/test/blackbox/test_case_optimal_pmtu_01/node_sim_nut.c
new file mode 100644 (file)
index 0000000..120b018
--- /dev/null
@@ -0,0 +1,326 @@
+/*
+    node_sim_nut.c -- Implementation of Node Simulation for Meshlink Testing
+                    for meta connection test case 01 - re-connection of
+                    two nodes when relay node goes down
+    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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <assert.h>
+#include <signal.h>
+#include <time.h>
+#include "../common/common_handlers.h"
+#include "../common/test_step.h"
+#include "../common/mesh_event_handler.h"
+#include "../../utils.h"
+#include "../run_blackbox_tests/test_optimal_pmtu.h"
+
+#define CMD_LINE_ARG_NODENAME   1
+#define CMD_LINE_ARG_DEVCLASS   2
+#define CMD_LINE_ARG_CLIENTID   3
+#define CMD_LINE_ARG_IMPORTSTR  4
+#define CMD_LINE_ARG_INVITEURL  5
+#define CHANNEL_PORT 1234
+
+#pragma pack(1)
+
+static int client_id = -1;
+
+static struct sync_flag peer_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
+static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
+
+static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
+                           bool reachable);
+static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
+static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
+
+static pmtu_attr_t node_pmtu[2];
+
+static void print_mtu_calc(pmtu_attr_t node_pmtu) {
+       fprintf(stderr, "MTU size : %d\n", node_pmtu.mtu_size);
+       fprintf(stderr, "Probes took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes);
+       fprintf(stderr, "Probes total length took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes_total_len);
+       fprintf(stderr, "Time took for calculating PMTU discovery : %lu\n", node_pmtu.mtu_discovery.time);
+       fprintf(stderr, "Total MTU ping probes : %d\n", node_pmtu.mtu_ping.probes);
+       fprintf(stderr, "Total MTU ping probes length : %d\n", node_pmtu.mtu_ping.probes_total_len);
+       float avg = 0;
+
+       if(node_pmtu.mtu_ping.probes) {
+               avg = (float)node_pmtu.mtu_ping.time / (float)node_pmtu.mtu_ping.probes;
+       }
+
+       fprintf(stderr, "Average MTU ping probes ping time : %f\n", avg);
+       fprintf(stderr, "Total probes received %d\n", node_pmtu.mtu_recv_probes.probes);
+       fprintf(stderr, "Total probes sent %d\n", node_pmtu.mtu_sent_probes.probes);
+}
+
+// Node status callback
+static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
+                           bool reachable) {
+       // Signal pthread_cond_wait if peer is reachable
+       if(!strcasecmp(node->name, "peer") && reachable) {
+               set_sync_flag(&peer_reachable, true);
+       }
+
+       // Notify the JOIN or LEFT event of a node to the test driver
+       mesh_event_sock_send(client_id, reachable ? NODE_JOINED : NODE_LEFT, node->name, 100);
+       return;
+}
+
+// Channel poll callback
+static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
+       (void)len;
+       meshlink_set_channel_poll_cb(mesh, channel, NULL);
+
+       // Send data via channel to trigger UDP peer to peer hole punching
+       assert(meshlink_channel_send(mesh, channel, "test", 5) >= 0);
+       return;
+}
+
+static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
+       (void)dat;
+       (void)len;
+
+       assert(port == CHANNEL_PORT);
+
+       // If the channel is from peer node set receive callback for it else reject the channel
+       if(!strcmp(channel->node->name, "peer")) {
+               meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
+               mesh->priv = channel;
+
+               return true;
+       }
+
+       return false;
+}
+
+/* channel receive callback */
+static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
+       if(len == 0) {
+               mesh_event_sock_send(client_id, ERR_NETWORK, channel->node->name, 100);
+               return;
+       }
+
+       if(!strcmp(channel->node->name, "peer")) {
+               if(!memcmp(dat, "reply", 5)) {
+                       set_sync_flag(&channel_opened, true);
+                       fprintf(stderr, "GOT REPLY FROM PEER\n");
+               } else if(!memcmp(dat, "test", 5)) {
+                       assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
+               }
+       }
+
+       return;
+}
+
+// Meshlink log handler
+void meshlink_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
+                     const char *text) {
+       (void)mesh;
+       (void)level;
+       int probe_len;
+       int mtu_len;
+       int probes;
+       char node_name[100];
+       int i = -1;
+
+       time_t cur_time;
+       time_t probe_interval;
+
+       cur_time = time(NULL);
+       assert(cur_time != -1);
+
+       static time_t node_shutdown_time = 0;
+       bool mtu_probe = false;
+
+       if(node_shutdown_time && cur_time >= node_shutdown_time) {
+               test_running = false;
+       }
+
+       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);
+
+       /* Calculate the MTU parameter values from the meshlink logs */
+       if(sscanf(text, "Sending MTU probe length %d to %s", &probe_len, node_name) == 2) {
+               find_node_index(i, node_name);
+               node_pmtu[i].mtu_sent_probes.probes += 1;
+               node_pmtu[i].mtu_sent_probes.probes_total_len += probe_len;
+
+               if(node_pmtu[i].mtu_size) {
+                       if(node_pmtu[i].mtu_sent_probes.time > node_pmtu[i].mtu_recv_probes.time) {
+                               probe_interval = cur_time - node_pmtu[i].mtu_sent_probes.time;
+                       } else {
+                               probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
+                       }
+
+                       node_pmtu[i].mtu_ping.probes += 1;
+                       node_pmtu[i].mtu_ping.time += probe_interval;
+                       node_pmtu[i].mtu_ping.probes_total_len += probe_len;
+               }
+
+               node_pmtu[i].mtu_sent_probes.time = cur_time;
+
+       } else if(sscanf(text, "Got MTU probe length %d from %s", &probe_len, node_name) == 2) {
+               find_node_index(i, node_name);
+               node_pmtu[i].mtu_recv_probes.probes += 1;
+               node_pmtu[i].mtu_recv_probes.probes_total_len += probe_len;
+
+               if(node_pmtu[i].mtu_size) {
+                       if(node_pmtu[i].mtu_sent_probes.time > node_pmtu[i].mtu_recv_probes.time) {
+                               probe_interval = cur_time - node_pmtu[i].mtu_sent_probes.time;
+                       } else {
+                               probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
+                       }
+
+                       node_pmtu[i].mtu_ping.probes += 1;
+                       node_pmtu[i].mtu_ping.time += probe_interval;
+                       node_pmtu[i].mtu_ping.probes_total_len += probe_len;
+               }
+
+               node_pmtu[i].mtu_recv_probes.time = cur_time;
+
+       } else if(sscanf(text, "Fixing MTU of %s to %d after %d probes", node_name, &mtu_len, &probes) == 3) {
+               static bool mtu_set = true;
+
+               if(!node_shutdown_time && !strcasecmp("relay", node_name) && mtu_set) {
+                       node_shutdown_time = cur_time + PING_TRACK_TIMEOUT;
+                       mtu_set = false;
+               }
+
+               find_node_index(i, node_name);
+               node_pmtu[i].mtu_discovery.probes = node_pmtu[i].mtu_recv_probes.probes + node_pmtu[i].mtu_sent_probes.probes;
+               node_pmtu[i].mtu_discovery.probes_total_len = node_pmtu[i].mtu_sent_probes.probes_total_len + node_pmtu[i].mtu_recv_probes.probes_total_len;
+               node_pmtu[i].mtu_discovery.time = cur_time - node_pmtu[i].mtu_start.time;
+               node_pmtu[i].mtu_discovery.count += 1;
+               node_pmtu[i].mtu_size = mtu_len;
+
+       } else if(sscanf(text, "SPTPS key exchange with %s succesful", node_name) == 1) {
+               find_node_index(i, node_name);
+               node_pmtu[i].mtu_start.time = cur_time;
+               node_pmtu[i].mtu_start.count += 1;
+               memset(&node_pmtu[i].mtu_discovery, 0, sizeof(struct pmtu_attr_para));
+               memset(&node_pmtu[i].mtu_ping, 0, sizeof(struct pmtu_attr_para));
+               memset(&node_pmtu[i].mtu_increase, 0, sizeof(struct pmtu_attr_para));
+
+       } else if(sscanf(text, "Increase in PMTU to %s detected, restarting PMTU discovery", node_name) == 1) {
+               find_node_index(i, node_name);
+               node_pmtu[i].mtu_increase.time = cur_time - node_pmtu[i].mtu_start.time;
+               node_pmtu[i].mtu_increase.count += 1;
+
+       } else if(sscanf(text, "Trying to send MTU probe to unreachable or rekeying node %s", node_name) == 1) {
+
+       } else if(sscanf(text, "%s did not respond to UDP ping, restarting PMTU discovery", node_name) == 1) {
+
+       } else if(sscanf(text, "No response to MTU probes from %s", node_name) == 1) {
+
+       } else if((sscanf(text, "Connection with %s activated", node_name) == 1) || (sscanf(text, "Already connected to %s", node_name) == 1)) {
+               mesh_event_sock_send(client_id, META_CONN_SUCCESSFUL, node_name, sizeof(node_name));
+
+       } else if((sscanf(text, "Connection closed by %s", node_name) == 1) || (sscanf(text, "Closing connection with %s", node_name) == 1)) {
+               mesh_event_sock_send(client_id, META_CONN_CLOSED, node_name, sizeof(node_name));
+
+       }
+}
+
+int main(int argc, char *argv[]) {
+       struct timeval main_loop_wait = { 5, 0 };
+       int i;
+
+       // Import mesh event handler
+
+       if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
+               client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
+               mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
+       }
+
+       setup_signals();
+
+       // Execute test steps
+
+       meshlink_handle_t *mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
+                                               "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
+       assert(mesh);
+       meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_logger);
+       meshlink_set_node_status_cb(mesh, node_status_cb);
+       meshlink_enable_discovery(mesh, false);
+       sleep(1);
+
+       // Join relay node and if fails to join then try few more attempts
+
+       if(argv[CMD_LINE_ARG_INVITEURL]) {
+               int attempts;
+               bool join_ret;
+
+               for(attempts = 0; attempts < 10; attempts++) {
+                       join_ret = meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]);
+
+                       if(join_ret) {
+                               break;
+                       }
+
+                       sleep(1);
+               }
+
+               if(attempts == 10) {
+                       abort();
+               }
+       }
+
+       assert(meshlink_start(mesh));
+
+       // Wait for peer node to join
+
+       assert(wait_sync_flag(&peer_reachable, 10));
+
+       // Open a channel to peer node
+
+       meshlink_node_t *peer_node = meshlink_get_node(mesh, "peer");
+       assert(peer_node);
+       meshlink_channel_t *channel = meshlink_channel_open(mesh, peer_node, CHANNEL_PORT,
+                                     channel_receive_cb, NULL, 0);
+       meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
+
+       assert(wait_sync_flag(&channel_opened, 30));
+       assert(mesh_event_sock_send(client_id, CHANNEL_OPENED, NULL, 0));
+
+       // All test steps executed - wait for signals to stop/start or close the mesh
+
+       while(test_running) {
+               select(1, NULL, NULL, NULL, &main_loop_wait);
+       }
+
+       // Send MTU probe parameters data to the test driver
+
+       pmtu_attr_t send_mtu_data;
+       send_mtu_data = node_pmtu[NODE_PMTU_PEER];
+       print_mtu_calc(send_mtu_data);
+       assert(mesh_event_sock_send(client_id, OPTIMAL_PMTU_PEER, &send_mtu_data, sizeof(send_mtu_data)));
+       send_mtu_data = node_pmtu[NODE_PMTU_RELAY];
+       print_mtu_calc(send_mtu_data);
+       assert(mesh_event_sock_send(client_id, OPTIMAL_PMTU_RELAY, &send_mtu_data, sizeof(send_mtu_data)));
+
+       meshlink_close(mesh);
+}
diff --git a/test/blackbox/test_case_optimal_pmtu_01/node_sim_peer.c b/test/blackbox/test_case_optimal_pmtu_01/node_sim_peer.c
new file mode 100644 (file)
index 0000000..293bbea
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+    node_sim_peer.c -- Implementation of Node Simulation for Meshlink Testing
+                    for meta connection test case 01 - re-connection of
+                    two nodes when relay node goes down
+    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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <assert.h>
+#include <signal.h>
+#include "../common/common_handlers.h"
+#include "../common/test_step.h"
+#include "../common/mesh_event_handler.h"
+#include "../../utils.h"
+
+#define CMD_LINE_ARG_NODENAME   1
+#define CMD_LINE_ARG_DEVCLASS   2
+#define CMD_LINE_ARG_CLIENTID   3
+#define CMD_LINE_ARG_IMPORTSTR  4
+#define CMD_LINE_ARG_INVITEURL  5
+#define CHANNEL_PORT 1234
+
+static struct sync_flag nut_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
+static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
+
+static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
+static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
+
+static int client_id = -1;
+
+static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
+                           bool reachable) {
+       if(!strcasecmp(node->name, "nut") && reachable) {
+               //set_sync_flag(&nut_reachable, true);
+               mesh_event_sock_send(client_id, reachable ? NODE_JOINED : NODE_LEFT, node->name, 100);
+       }
+
+       return;
+}
+
+static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
+       (void)dat;
+       (void)len;
+
+       assert(port == CHANNEL_PORT);
+
+       if(!strcmp(channel->node->name, "nut")) {
+               meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
+               //channel->node->priv = channel;
+
+               return true;
+       }
+
+       return false;
+}
+
+static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
+       (void)len;
+       meshlink_set_channel_poll_cb(mesh, channel, NULL);
+       assert(meshlink_channel_send(mesh, channel, "test", 5) >= 0);
+       return;
+}
+
+/* channel receive callback */
+static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
+       if(len == 0) {
+               mesh_event_sock_send(client_id, ERR_NETWORK, channel->node->name, 100);
+               return;
+       }
+
+       if(!strcmp(channel->node->name, "nut")) {
+               if(!memcmp(dat, "reply", 5)) {
+                       set_sync_flag(&channel_opened, true);
+               } else if(!memcmp(dat, "test", 5)) {
+                       assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
+               }
+       }
+
+       return;
+}
+
+int main(int argc, char *argv[]) {
+       struct timeval main_loop_wait = { 2, 0 };
+
+       // Import mesh event handler
+
+       if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
+               client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
+               mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
+       }
+
+       // Setup required signals
+
+       setup_signals();
+
+       // Run peer node instance
+
+       meshlink_handle_t *mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
+                                               "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
+       assert(mesh);
+       meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
+       meshlink_set_channel_accept_cb(mesh, channel_accept);
+       meshlink_enable_discovery(mesh, false);
+
+       if(argv[CMD_LINE_ARG_INVITEURL]) {
+               int attempts;
+               bool join_ret;
+
+               for(attempts = 0; attempts < 10; attempts++) {
+                       join_ret = meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]);
+
+                       if(join_ret) {
+                               break;
+                       }
+
+                       sleep(1);
+               }
+
+               if(attempts == 10) {
+                       abort();
+               }
+       }
+
+       assert(meshlink_start(mesh));
+       mesh_event_sock_send(client_id, NODE_STARTED, NULL, 0);
+
+       //assert(wait_sync_flag(&nut_reachable, 10));
+
+       /*meshlink_node_t *nut_node = meshlink_get_node(mesh, "nut");
+       assert(nut_node);
+       meshlink_channel_t *channel = meshlink_channel_open(mesh, nut_node, CHANNEL_PORT,
+                                     channel_receive_cb, NULL, 0);
+       meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
+
+       assert(wait_sync_flag(&channel_opened, 20));
+       mesh_event_sock_send(client_id, NODE_STARTED, NULL, 0);*/
+
+       // All test steps executed - wait for signals to stop/start or close the mesh
+
+       while(test_running) {
+               select(1, NULL, NULL, NULL, &main_loop_wait);
+       }
+
+       meshlink_close(mesh);
+
+       return EXIT_SUCCESS;
+}
diff --git a/test/blackbox/test_case_optimal_pmtu_01/node_sim_relay.c b/test/blackbox/test_case_optimal_pmtu_01/node_sim_relay.c
new file mode 100644 (file)
index 0000000..dd62209
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+    node_sim_relay.c -- Implementation of Node Simulation for Meshlink Testing
+                    for meta connection test case 01 - re-connection of
+                    two nodes when relay node goes down
+    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 <stdio.h>
+#include <stdlib.h>
+#include "../common/common_handlers.h"
+#include "../common/test_step.h"
+#include "../common/mesh_event_handler.h"
+
+#define CMD_LINE_ARG_NODENAME   1
+#define CMD_LINE_ARG_DEVCLASS   2
+#define CMD_LINE_ARG_CLIENTID   3
+#define CMD_LINE_ARG_IMPORTSTR  4
+#define CMD_LINE_ARG_INVITEURL  5
+
+int main(int argc, char *argv[]) {
+       struct timeval main_loop_wait = { 5, 0 };
+
+       // Setup required signals
+
+       setup_signals();
+
+       // Run relay node instance
+
+       meshlink_handle_t *mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
+                                               "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
+       assert(mesh);
+       meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
+       meshlink_enable_discovery(mesh, false);
+
+       meshlink_start(mesh);
+       //send_event(NODE_STARTED);
+
+       /* All test steps executed - wait for signals to stop/start or close the mesh */
+       while(test_running) {
+               select(1, NULL, NULL, NULL, &main_loop_wait);
+       }
+
+       meshlink_close(mesh);
+
+       return 0;
+}
diff --git a/test/blackbox/test_case_optimal_pmtu_02/Makefile.am b/test/blackbox/test_case_optimal_pmtu_02/Makefile.am
new file mode 100644 (file)
index 0000000..fc9b98f
--- /dev/null
@@ -0,0 +1,13 @@
+check_PROGRAMS = node_sim_peer node_sim_nut node_sim_relay
+
+node_sim_peer_SOURCES = ../test_case_optimal_pmtu_01/node_sim_peer.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_peer_LDADD = ../../../src/libmeshlink.la
+node_sim_peer_CFLAGS = -D_GNU_SOURCE
+
+node_sim_nut_SOURCES = ../test_case_optimal_pmtu_01/node_sim_nut.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_nut_LDADD = ../../../src/libmeshlink.la
+node_sim_nut_CFLAGS = -D_GNU_SOURCE
+
+node_sim_relay_SOURCES = ../test_case_optimal_pmtu_01/node_sim_relay.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_relay_LDADD = ../../../src/libmeshlink.la
+node_sim_relay_CFLAGS = -D_GNU_SOURCE
diff --git a/test/blackbox/test_case_optimal_pmtu_03/Makefile.am b/test/blackbox/test_case_optimal_pmtu_03/Makefile.am
new file mode 100644 (file)
index 0000000..fc9b98f
--- /dev/null
@@ -0,0 +1,13 @@
+check_PROGRAMS = node_sim_peer node_sim_nut node_sim_relay
+
+node_sim_peer_SOURCES = ../test_case_optimal_pmtu_01/node_sim_peer.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_peer_LDADD = ../../../src/libmeshlink.la
+node_sim_peer_CFLAGS = -D_GNU_SOURCE
+
+node_sim_nut_SOURCES = ../test_case_optimal_pmtu_01/node_sim_nut.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_nut_LDADD = ../../../src/libmeshlink.la
+node_sim_nut_CFLAGS = -D_GNU_SOURCE
+
+node_sim_relay_SOURCES = ../test_case_optimal_pmtu_01/node_sim_relay.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_relay_LDADD = ../../../src/libmeshlink.la
+node_sim_relay_CFLAGS = -D_GNU_SOURCE
diff --git a/test/blackbox/test_case_optimal_pmtu_04/Makefile.am b/test/blackbox/test_case_optimal_pmtu_04/Makefile.am
new file mode 100644 (file)
index 0000000..fc9b98f
--- /dev/null
@@ -0,0 +1,13 @@
+check_PROGRAMS = node_sim_peer node_sim_nut node_sim_relay
+
+node_sim_peer_SOURCES = ../test_case_optimal_pmtu_01/node_sim_peer.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_peer_LDADD = ../../../src/libmeshlink.la
+node_sim_peer_CFLAGS = -D_GNU_SOURCE
+
+node_sim_nut_SOURCES = ../test_case_optimal_pmtu_01/node_sim_nut.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_nut_LDADD = ../../../src/libmeshlink.la
+node_sim_nut_CFLAGS = -D_GNU_SOURCE
+
+node_sim_relay_SOURCES = ../test_case_optimal_pmtu_01/node_sim_relay.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_relay_LDADD = ../../../src/libmeshlink.la
+node_sim_relay_CFLAGS = -D_GNU_SOURCE
diff --git a/test/blackbox/test_case_optimal_pmtu_05/Makefile.am b/test/blackbox/test_case_optimal_pmtu_05/Makefile.am
new file mode 100644 (file)
index 0000000..fc9b98f
--- /dev/null
@@ -0,0 +1,13 @@
+check_PROGRAMS = node_sim_peer node_sim_nut node_sim_relay
+
+node_sim_peer_SOURCES = ../test_case_optimal_pmtu_01/node_sim_peer.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_peer_LDADD = ../../../src/libmeshlink.la
+node_sim_peer_CFLAGS = -D_GNU_SOURCE
+
+node_sim_nut_SOURCES = ../test_case_optimal_pmtu_01/node_sim_nut.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_nut_LDADD = ../../../src/libmeshlink.la
+node_sim_nut_CFLAGS = -D_GNU_SOURCE
+
+node_sim_relay_SOURCES = ../test_case_optimal_pmtu_01/node_sim_relay.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_relay_LDADD = ../../../src/libmeshlink.la
+node_sim_relay_CFLAGS = -D_GNU_SOURCE
diff --git a/test/blackbox/test_case_optimal_pmtu_06/Makefile.am b/test/blackbox/test_case_optimal_pmtu_06/Makefile.am
new file mode 100644 (file)
index 0000000..fc9b98f
--- /dev/null
@@ -0,0 +1,13 @@
+check_PROGRAMS = node_sim_peer node_sim_nut node_sim_relay
+
+node_sim_peer_SOURCES = ../test_case_optimal_pmtu_01/node_sim_peer.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_peer_LDADD = ../../../src/libmeshlink.la
+node_sim_peer_CFLAGS = -D_GNU_SOURCE
+
+node_sim_nut_SOURCES = ../test_case_optimal_pmtu_01/node_sim_nut.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_nut_LDADD = ../../../src/libmeshlink.la
+node_sim_nut_CFLAGS = -D_GNU_SOURCE
+
+node_sim_relay_SOURCES = ../test_case_optimal_pmtu_01/node_sim_relay.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_relay_LDADD = ../../../src/libmeshlink.la
+node_sim_relay_CFLAGS = -D_GNU_SOURCE
diff --git a/test/blackbox/test_case_optimal_pmtu_07/Makefile.am b/test/blackbox/test_case_optimal_pmtu_07/Makefile.am
new file mode 100644 (file)
index 0000000..8bde53d
--- /dev/null
@@ -0,0 +1,13 @@
+check_PROGRAMS = node_sim_peer node_sim_nut node_sim_relay
+
+node_sim_peer_SOURCES = node_sim_peer.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_peer_LDADD = ../../../src/libmeshlink.la
+node_sim_peer_CFLAGS = -D_GNU_SOURCE
+
+node_sim_nut_SOURCES = node_sim_nut.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_nut_LDADD = ../../../src/libmeshlink.la
+node_sim_nut_CFLAGS = -D_GNU_SOURCE
+
+node_sim_relay_SOURCES = node_sim_relay.c ../common/common_handlers.c ../common/test_step.c ../common/mesh_event_handler.c ../../utils.c
+node_sim_relay_LDADD = ../../../src/libmeshlink.la
+node_sim_relay_CFLAGS = -D_GNU_SOURCE
diff --git a/test/blackbox/test_case_optimal_pmtu_07/node_sim_nut.c b/test/blackbox/test_case_optimal_pmtu_07/node_sim_nut.c
new file mode 100644 (file)
index 0000000..75194bb
--- /dev/null
@@ -0,0 +1,325 @@
+/*
+    node_sim_nut.c -- Implementation of Node Simulation for Meshlink Testing
+                    for meta connection test case 01 - re-connection of
+                    two nodes when relay node goes down
+    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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <assert.h>
+#include <signal.h>
+#include <time.h>
+#include "../common/common_handlers.h"
+#include "../common/test_step.h"
+#include "../common/mesh_event_handler.h"
+#include "../../utils.h"
+#include "../run_blackbox_tests/test_optimal_pmtu.h"
+
+#define CMD_LINE_ARG_NODENAME   1
+#define CMD_LINE_ARG_DEVCLASS   2
+#define CMD_LINE_ARG_CLIENTID   3
+#define CMD_LINE_ARG_IMPORTSTR  4
+#define CMD_LINE_ARG_INVITEURL  5
+#define CHANNEL_PORT 1234
+
+#pragma pack(1)
+
+static int client_id = -1;
+
+static struct sync_flag peer_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
+static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
+
+static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
+                           bool reachable);
+static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
+static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
+
+static pmtu_attr_t node_pmtu[2];
+
+static void print_mtu_calc(pmtu_attr_t node_pmtu) {
+       fprintf(stderr, "MTU size : %d\n", node_pmtu.mtu_size);
+       fprintf(stderr, "Probes took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes);
+       fprintf(stderr, "Probes total length took for calculating PMTU discovery : %d\n", node_pmtu.mtu_discovery.probes_total_len);
+       fprintf(stderr, "Time took for calculating PMTU discovery : %lu\n", node_pmtu.mtu_discovery.time);
+       fprintf(stderr, "Total MTU ping probes : %d\n", node_pmtu.mtu_ping.probes);
+       fprintf(stderr, "Total MTU ping probes length : %d\n", node_pmtu.mtu_ping.probes_total_len);
+       float avg = 0;
+
+       if(node_pmtu.mtu_ping.probes) {
+               avg = (float)node_pmtu.mtu_ping.time / (float)node_pmtu.mtu_ping.probes;
+       }
+
+       fprintf(stderr, "Average MTU ping probes ping time : %f\n", avg);
+       fprintf(stderr, "Total probes received %d\n", node_pmtu.mtu_recv_probes.probes);
+       fprintf(stderr, "Total probes sent %d\n", node_pmtu.mtu_sent_probes.probes);
+}
+
+static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
+                           bool reachable) {
+       if(!strcasecmp(node->name, "peer") && reachable) {
+               set_sync_flag(&peer_reachable, true);
+       }
+
+       mesh_event_sock_send(client_id, reachable ? NODE_JOINED : NODE_LEFT, node->name, 100);
+       return;
+}
+
+static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
+       (void)len;
+       meshlink_set_channel_poll_cb(mesh, channel, NULL);
+       assert(meshlink_channel_send(mesh, channel, "test", 5) >= 0);
+       return;
+}
+
+static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
+       (void)dat;
+       (void)len;
+
+       assert(port == CHANNEL_PORT);
+
+       if(!strcmp(channel->node->name, "peer")) {
+               meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
+               mesh->priv = channel;
+
+               return true;
+       }
+
+       return false;
+}
+
+/* channel receive callback */
+static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
+       if(len == 0) {
+               mesh_event_sock_send(client_id, ERR_NETWORK, channel->node->name, 100);
+               return;
+       }
+
+       if(!strcmp(channel->node->name, "peer")) {
+               if(!memcmp(dat, "reply", 5)) {
+                       set_sync_flag(&channel_opened, true);
+                       fprintf(stderr, "GOT REPLY FROM PEER\n");
+               } else if(!memcmp(dat, "test", 5)) {
+                       assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
+               }
+       }
+
+       return;
+}
+
+void meshlink_logger(meshlink_handle_t *mesh, meshlink_log_level_t level,
+                     const char *text) {
+       (void)mesh;
+       (void)level;
+       int probe_len;
+       int mtu_len;
+       int probes;
+       char node_name[100];
+       int i = -1;
+
+       time_t cur_time;
+       time_t probe_interval;
+
+       cur_time = time(NULL);
+       assert(cur_time != -1);
+
+       static time_t node_shutdown_time = 0;
+       bool mtu_probe = false;
+
+       if(node_shutdown_time && cur_time >= node_shutdown_time) {
+               test_running = false;
+       }
+
+       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);
+
+       if(sscanf(text, "Sending MTU probe length %d to %s", &probe_len, node_name) == 2) {
+               find_node_index(i, node_name);
+               node_pmtu[i].mtu_sent_probes.probes += 1;
+               node_pmtu[i].mtu_sent_probes.probes_total_len += probe_len;
+
+               if(node_pmtu[i].mtu_size) {
+                       if(node_pmtu[i].mtu_sent_probes.time > node_pmtu[i].mtu_recv_probes.time) {
+                               probe_interval = cur_time - node_pmtu[i].mtu_sent_probes.time;
+                       } else {
+                               probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
+                       }
+
+                       node_pmtu[i].mtu_ping.probes += 1;
+                       node_pmtu[i].mtu_ping.time += probe_interval;
+                       node_pmtu[i].mtu_ping.probes_total_len += probe_len;
+               }
+
+               node_pmtu[i].mtu_sent_probes.time = cur_time;
+
+       } else if(sscanf(text, "Got MTU probe length %d from %s", &probe_len, node_name) == 2) {
+               find_node_index(i, node_name);
+               node_pmtu[i].mtu_recv_probes.probes += 1;
+               node_pmtu[i].mtu_recv_probes.probes_total_len += probe_len;
+
+               if(node_pmtu[i].mtu_size) {
+                       if(node_pmtu[i].mtu_sent_probes.time > node_pmtu[i].mtu_recv_probes.time) {
+                               probe_interval = cur_time - node_pmtu[i].mtu_sent_probes.time;
+                       } else {
+                               probe_interval = cur_time - node_pmtu[i].mtu_recv_probes.time;
+                       }
+
+                       node_pmtu[i].mtu_ping.probes += 1;
+                       node_pmtu[i].mtu_ping.time += probe_interval;
+                       node_pmtu[i].mtu_ping.probes_total_len += probe_len;
+               }
+
+               node_pmtu[i].mtu_recv_probes.time = cur_time;
+
+       } else if(sscanf(text, "Fixing MTU of %s to %d after %d probes", node_name, &mtu_len, &probes) == 3) {
+               static bool mtu_set = true;
+
+               if(!node_shutdown_time && !strcasecmp("relay", node_name) && mtu_set) {
+                       node_shutdown_time = cur_time + PING_TRACK_TIMEOUT;
+                       mtu_set = false;
+               }
+
+               find_node_index(i, node_name);
+               node_pmtu[i].mtu_discovery.probes = node_pmtu[i].mtu_recv_probes.probes + node_pmtu[i].mtu_sent_probes.probes;
+               node_pmtu[i].mtu_discovery.probes_total_len = node_pmtu[i].mtu_sent_probes.probes_total_len + node_pmtu[i].mtu_recv_probes.probes_total_len;
+               node_pmtu[i].mtu_discovery.time = cur_time - node_pmtu[i].mtu_start.time;
+               node_pmtu[i].mtu_discovery.count += 1;
+               node_pmtu[i].mtu_size = mtu_len;
+
+       } else if(sscanf(text, "SPTPS key exchange with %s succesful", node_name) == 1) {
+               find_node_index(i, node_name);
+               node_pmtu[i].mtu_start.time = cur_time;
+               node_pmtu[i].mtu_start.count += 1;
+               memset(&node_pmtu[i].mtu_discovery, 0, sizeof(struct pmtu_attr_para));
+               memset(&node_pmtu[i].mtu_ping, 0, sizeof(struct pmtu_attr_para));
+               memset(&node_pmtu[i].mtu_increase, 0, sizeof(struct pmtu_attr_para));
+
+       } else if(sscanf(text, "Increase in PMTU to %s detected, restarting PMTU discovery", node_name) == 1) {
+               find_node_index(i, node_name);
+               node_pmtu[i].mtu_increase.time = cur_time - node_pmtu[i].mtu_start.time;
+               node_pmtu[i].mtu_increase.count += 1;
+
+       } else if(sscanf(text, "Trying to send MTU probe to unreachable or rekeying node %s", node_name) == 1) {
+
+       } else if(sscanf(text, "%s did not respond to UDP ping, restarting PMTU discovery", node_name) == 1) {
+
+       } else if(sscanf(text, "No response to MTU probes from %s", node_name) == 1) {
+
+       } else if((sscanf(text, "Connection with %s activated", node_name) == 1) || (sscanf(text, "Already connected to %s", node_name) == 1)) {
+               mesh_event_sock_send(client_id, META_CONN_SUCCESSFUL, node_name, sizeof(node_name));
+
+       } else if((sscanf(text, "Connection closed by %s", node_name) == 1) || (sscanf(text, "Closing connection with %s", node_name) == 1)) {
+               mesh_event_sock_send(client_id, META_CONN_CLOSED, node_name, sizeof(node_name));
+
+       }
+}
+
+int main(int argc, char *argv[]) {
+       struct timeval main_loop_wait = { 5, 0 };
+       int i;
+
+       // Import mesh event handler
+
+       if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
+               client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
+               mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
+       }
+
+       setup_signals();
+
+       // Execute test steps
+
+       meshlink_handle_t *mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
+                                               "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
+       assert(mesh);
+       meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_logger);
+       meshlink_set_node_status_cb(mesh, node_status_cb);
+       meshlink_enable_discovery(mesh, false);
+       sleep(1);
+
+       if(argv[CMD_LINE_ARG_INVITEURL]) {
+               int attempts;
+               bool join_ret;
+
+               for(attempts = 0; attempts < 10; attempts++) {
+                       join_ret = meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]);
+
+                       if(join_ret) {
+                               break;
+                       }
+
+                       sleep(1);
+               }
+
+               if(attempts == 10) {
+                       abort();
+               }
+       }
+
+       assert(meshlink_start(mesh));
+
+       // Wait for peer node to join
+
+       assert(wait_sync_flag(&peer_reachable, 10));
+
+       // Open a channel to peer node
+
+       meshlink_node_t *peer_node = meshlink_get_node(mesh, "peer");
+       assert(peer_node);
+       meshlink_channel_t *channel = meshlink_channel_open(mesh, peer_node, CHANNEL_PORT,
+                                     channel_receive_cb, NULL, 0);
+       meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
+
+       assert(wait_sync_flag(&channel_opened, 30));
+       assert(mesh_event_sock_send(client_id, CHANNEL_OPENED, NULL, 0));
+
+       // All test steps executed - wait for signals to stop/start or close the mesh
+
+       time_t time_stamp, send_time;
+
+       time_stamp = time(NULL);
+       send_time = time_stamp + 10;
+
+       while(test_running) {
+               select(1, NULL, NULL, NULL, &main_loop_wait);
+
+               time_stamp = time(NULL);
+
+               if(time_stamp >= send_time) {
+                       send_time = time_stamp + 10;
+                       meshlink_channel_send(mesh, channel, "ping", 5);
+               }
+       }
+
+       pmtu_attr_t send_mtu_data;
+       send_mtu_data = node_pmtu[NODE_PMTU_PEER];
+       print_mtu_calc(send_mtu_data);
+       assert(mesh_event_sock_send(client_id, OPTIMAL_PMTU_PEER, &send_mtu_data, sizeof(send_mtu_data)));
+       send_mtu_data = node_pmtu[NODE_PMTU_RELAY];
+       print_mtu_calc(send_mtu_data);
+       assert(mesh_event_sock_send(client_id, OPTIMAL_PMTU_RELAY, &send_mtu_data, sizeof(send_mtu_data)));
+
+       meshlink_close(mesh);
+}
diff --git a/test/blackbox/test_case_optimal_pmtu_07/node_sim_peer.c b/test/blackbox/test_case_optimal_pmtu_07/node_sim_peer.c
new file mode 100644 (file)
index 0000000..293bbea
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+    node_sim_peer.c -- Implementation of Node Simulation for Meshlink Testing
+                    for meta connection test case 01 - re-connection of
+                    two nodes when relay node goes down
+    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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <pthread.h>
+#include <assert.h>
+#include <signal.h>
+#include "../common/common_handlers.h"
+#include "../common/test_step.h"
+#include "../common/mesh_event_handler.h"
+#include "../../utils.h"
+
+#define CMD_LINE_ARG_NODENAME   1
+#define CMD_LINE_ARG_DEVCLASS   2
+#define CMD_LINE_ARG_CLIENTID   3
+#define CMD_LINE_ARG_IMPORTSTR  4
+#define CMD_LINE_ARG_INVITEURL  5
+#define CHANNEL_PORT 1234
+
+static struct sync_flag nut_reachable = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
+static struct sync_flag channel_opened = {.mutex  = PTHREAD_MUTEX_INITIALIZER, .cond = PTHREAD_COND_INITIALIZER};
+
+static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len);
+static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len);
+
+static int client_id = -1;
+
+static void node_status_cb(meshlink_handle_t *mesh, meshlink_node_t *node,
+                           bool reachable) {
+       if(!strcasecmp(node->name, "nut") && reachable) {
+               //set_sync_flag(&nut_reachable, true);
+               mesh_event_sock_send(client_id, reachable ? NODE_JOINED : NODE_LEFT, node->name, 100);
+       }
+
+       return;
+}
+
+static bool channel_accept(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *dat, size_t len) {
+       (void)dat;
+       (void)len;
+
+       assert(port == CHANNEL_PORT);
+
+       if(!strcmp(channel->node->name, "nut")) {
+               meshlink_set_channel_receive_cb(mesh, channel, channel_receive_cb);
+               //channel->node->priv = channel;
+
+               return true;
+       }
+
+       return false;
+}
+
+static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
+       (void)len;
+       meshlink_set_channel_poll_cb(mesh, channel, NULL);
+       assert(meshlink_channel_send(mesh, channel, "test", 5) >= 0);
+       return;
+}
+
+/* channel receive callback */
+static void channel_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *dat, size_t len) {
+       if(len == 0) {
+               mesh_event_sock_send(client_id, ERR_NETWORK, channel->node->name, 100);
+               return;
+       }
+
+       if(!strcmp(channel->node->name, "nut")) {
+               if(!memcmp(dat, "reply", 5)) {
+                       set_sync_flag(&channel_opened, true);
+               } else if(!memcmp(dat, "test", 5)) {
+                       assert(meshlink_channel_send(mesh, channel, "reply", 5) >= 0);
+               }
+       }
+
+       return;
+}
+
+int main(int argc, char *argv[]) {
+       struct timeval main_loop_wait = { 2, 0 };
+
+       // Import mesh event handler
+
+       if((argv[CMD_LINE_ARG_CLIENTID]) && (argv[CMD_LINE_ARG_IMPORTSTR])) {
+               client_id = atoi(argv[CMD_LINE_ARG_CLIENTID]);
+               mesh_event_sock_connect(argv[CMD_LINE_ARG_IMPORTSTR]);
+       }
+
+       // Setup required signals
+
+       setup_signals();
+
+       // Run peer node instance
+
+       meshlink_handle_t *mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
+                                               "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
+       assert(mesh);
+       meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
+       meshlink_set_channel_accept_cb(mesh, channel_accept);
+       meshlink_enable_discovery(mesh, false);
+
+       if(argv[CMD_LINE_ARG_INVITEURL]) {
+               int attempts;
+               bool join_ret;
+
+               for(attempts = 0; attempts < 10; attempts++) {
+                       join_ret = meshlink_join(mesh, argv[CMD_LINE_ARG_INVITEURL]);
+
+                       if(join_ret) {
+                               break;
+                       }
+
+                       sleep(1);
+               }
+
+               if(attempts == 10) {
+                       abort();
+               }
+       }
+
+       assert(meshlink_start(mesh));
+       mesh_event_sock_send(client_id, NODE_STARTED, NULL, 0);
+
+       //assert(wait_sync_flag(&nut_reachable, 10));
+
+       /*meshlink_node_t *nut_node = meshlink_get_node(mesh, "nut");
+       assert(nut_node);
+       meshlink_channel_t *channel = meshlink_channel_open(mesh, nut_node, CHANNEL_PORT,
+                                     channel_receive_cb, NULL, 0);
+       meshlink_set_channel_poll_cb(mesh, channel, poll_cb);
+
+       assert(wait_sync_flag(&channel_opened, 20));
+       mesh_event_sock_send(client_id, NODE_STARTED, NULL, 0);*/
+
+       // All test steps executed - wait for signals to stop/start or close the mesh
+
+       while(test_running) {
+               select(1, NULL, NULL, NULL, &main_loop_wait);
+       }
+
+       meshlink_close(mesh);
+
+       return EXIT_SUCCESS;
+}
diff --git a/test/blackbox/test_case_optimal_pmtu_07/node_sim_relay.c b/test/blackbox/test_case_optimal_pmtu_07/node_sim_relay.c
new file mode 100644 (file)
index 0000000..dd62209
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+    node_sim_relay.c -- Implementation of Node Simulation for Meshlink Testing
+                    for meta connection test case 01 - re-connection of
+                    two nodes when relay node goes down
+    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 <stdio.h>
+#include <stdlib.h>
+#include "../common/common_handlers.h"
+#include "../common/test_step.h"
+#include "../common/mesh_event_handler.h"
+
+#define CMD_LINE_ARG_NODENAME   1
+#define CMD_LINE_ARG_DEVCLASS   2
+#define CMD_LINE_ARG_CLIENTID   3
+#define CMD_LINE_ARG_IMPORTSTR  4
+#define CMD_LINE_ARG_INVITEURL  5
+
+int main(int argc, char *argv[]) {
+       struct timeval main_loop_wait = { 5, 0 };
+
+       // Setup required signals
+
+       setup_signals();
+
+       // Run relay node instance
+
+       meshlink_handle_t *mesh = meshlink_open("testconf", argv[CMD_LINE_ARG_NODENAME],
+                                               "test_channel_conn", atoi(argv[CMD_LINE_ARG_DEVCLASS]));
+       assert(mesh);
+       meshlink_set_log_cb(mesh, MESHLINK_DEBUG, meshlink_callback_logger);
+       meshlink_enable_discovery(mesh, false);
+
+       meshlink_start(mesh);
+       //send_event(NODE_STARTED);
+
+       /* All test steps executed - wait for signals to stop/start or close the mesh */
+       while(test_running) {
+               select(1, NULL, NULL, NULL, &main_loop_wait);
+       }
+
+       meshlink_close(mesh);
+
+       return 0;
+}