]> git.meshlink.io Git - meshlink/commitdiff
Add a test for the discovery algorithm.
authorGuus Sliepen <guus@meshlink.io>
Sun, 6 Nov 2022 22:50:18 +0000 (23:50 +0100)
committerGuus Sliepen <guus@meshlink.io>
Tue, 8 Nov 2022 21:11:06 +0000 (22:11 +0100)
Create two nodes and link them, then change their ports and check if
they can still find each other.

test/Makefile.am
test/discovery.c [new file with mode: 0644]

index 94a6317d8b6034d765f915942fcd7e13bd1f9705..001291cc4d7c1b9bdf19f472dbd64875248c3004 100644 (file)
@@ -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 (file)
index 0000000..a9ecb83
--- /dev/null
@@ -0,0 +1,54 @@
+#ifdef NDEBUG
+#undef NDEBUG
+#endif
+
+#include <stdlib.h>
+#include <assert.h>
+
+#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);
+}