From: Guus Sliepen Date: Sun, 6 Nov 2022 22:18:22 +0000 (+0100) Subject: Add a test for changing ports and handling of port conflicts. X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=8d7deb258ba8311c87b3c501d71c847da2ddd747;p=meshlink Add a test for changing ports and handling of port conflicts. --- diff --git a/test/Makefile.am b/test/Makefile.am index 688d8b7d..94a6317d 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -25,6 +25,7 @@ TESTS = \ metering-slowping \ metering-tcponly \ meta-connections \ + port \ sign-verify \ storage-policy \ trio \ @@ -73,6 +74,7 @@ check_PROGRAMS = \ metering-slowping \ metering-tcponly \ meta-connections \ + port \ sign-verify \ storage-policy \ stream \ @@ -167,6 +169,9 @@ metering_tcponly_LDADD = $(top_builddir)/src/libmeshlink.la meta_connections_SOURCES = meta-connections.c netns_utils.c netns_utils.h utils.c utils.h meta_connections_LDADD = $(top_builddir)/src/libmeshlink.la +port_SOURCES = port.c utils.c utils.h +port_LDADD = $(top_builddir)/src/libmeshlink.la + sign_verify_SOURCES = sign-verify.c utils.c utils.h sign_verify_LDADD = $(top_builddir)/src/libmeshlink.la diff --git a/test/port.c b/test/port.c new file mode 100644 index 00000000..cd24f345 --- /dev/null +++ b/test/port.c @@ -0,0 +1,67 @@ +#ifdef NDEBUG +#undef NDEBUG +#endif + +#include +#include +#include +#include +#include +#include +#include + +#include "meshlink.h" +#include "devtools.h" +#include "utils.h" + + +int main(void) { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + + meshlink_handle_t *mesh1; + meshlink_handle_t *mesh2; + + // Open two instances + + assert(meshlink_destroy("port_conf.1")); + assert(meshlink_destroy("port_conf.2")); + + mesh1 = meshlink_open("port_conf.1", "foo", "port", DEV_CLASS_BACKBONE); + mesh2 = meshlink_open("port_conf.2", "bar", "port", DEV_CLASS_BACKBONE); + + assert(mesh1); + assert(mesh2); + + meshlink_enable_discovery(mesh1, false); + meshlink_enable_discovery(mesh2, false); + + int port1 = meshlink_get_port(mesh1); + int port2 = meshlink_get_port(mesh2); + assert(port1); + assert(port2); + assert(port1 != port2); + + // bar cannot take foo's port if foo is still open + assert(!meshlink_set_port(mesh2, port1)); + + // bar can take foo's port of foo is closed + meshlink_close(mesh1); + + assert(meshlink_set_port(mesh2, port1)); + assert(meshlink_get_port(mesh2) == port1); + + // foo can open but will now use a different port + mesh1 = meshlink_open("port_conf.1", "foo", "port", DEV_CLASS_BACKBONE); + assert(mesh1); + int port1b = meshlink_get_port(mesh1); + assert(port1b); + assert(port1b != port1); + + assert(!meshlink_set_port(mesh1, port1)); + + // foo can take over bar's old port + assert(meshlink_set_port(mesh1, port2)); + + meshlink_close(mesh1); + meshlink_close(mesh2); +}