From 7d21bf7e18fb37ed08a3117657c9a0ddce64432a Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 6 Nov 2022 23:50:18 +0100 Subject: [PATCH] Add a test for the discovery algorithm. Create two nodes and link them, then change their ports and check if they can still find each other. --- test/Makefile.am | 5 +++++ test/discovery.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 test/discovery.c diff --git a/test/Makefile.am b/test/Makefile.am index 94a6317d..001291cc 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -14,6 +14,7 @@ TESTS = \ channels-no-partial \ channels-udp \ channels-udp-cornercases \ + discovery \ duplicate \ encrypted \ ephemeral \ @@ -62,6 +63,7 @@ check_PROGRAMS = \ channels-no-partial \ channels-udp \ channels-udp-cornercases \ + discovery \ duplicate \ echo-fork \ encrypted \ @@ -133,6 +135,9 @@ channels_udp_LDADD = $(top_builddir)/src/libmeshlink.la channels_udp_cornercases_SOURCES = channels-udp-cornercases.c utils.c utils.h channels_udp_cornercases_LDADD = $(top_builddir)/src/libmeshlink.la +discovery_SOURCES = discovery.c utils.c utils.h +discovery_LDADD = $(top_builddir)/src/libmeshlink.la + duplicate_SOURCES = duplicate.c utils.c utils.h duplicate_LDADD = $(top_builddir)/src/libmeshlink.la diff --git a/test/discovery.c b/test/discovery.c new file mode 100644 index 00000000..a9ecb831 --- /dev/null +++ b/test/discovery.c @@ -0,0 +1,54 @@ +#ifdef NDEBUG +#undef NDEBUG +#endif + +#include +#include + +#include "utils.h" +#include "../src/meshlink.h" + +int main(void) { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + + // Open two new meshlink instance. + + meshlink_handle_t *mesh_a, *mesh_b; + open_meshlink_pair(&mesh_a, &mesh_b, "discovery"); + + // Forget the canoncial address + + assert(meshlink_clear_canonical_address(mesh_a, meshlink_get_node(mesh_a, "b"))); + assert(meshlink_clear_canonical_address(mesh_b, meshlink_get_node(mesh_b, "a"))); + + int port_a = meshlink_get_port(mesh_a); + int port_b = meshlink_get_port(mesh_b); + + // Swap and change ports + + port_a++; + port_b++; + + meshlink_close(mesh_a); + assert(meshlink_set_port(mesh_b, port_a)); + meshlink_close(mesh_b); + mesh_a = meshlink_open("discovery_conf.1", "a", "discovery", DEV_CLASS_BACKBONE); + assert(mesh_a); + assert(meshlink_set_port(mesh_a, port_b)); + mesh_b = meshlink_open("discovery_conf.2", "b", "discovery", DEV_CLASS_BACKBONE); + assert(mesh_b); + + assert(meshlink_get_port(mesh_a) == port_b); + assert(meshlink_get_port(mesh_b) == port_a); + + // Verify that the nodes can find each other + + meshlink_enable_discovery(mesh_a, true); + meshlink_enable_discovery(mesh_b, true); + + start_meshlink_pair(mesh_a, mesh_b); + + // Clean up. + + close_meshlink_pair(mesh_a, mesh_b); +} -- 2.39.5