X-Git-Url: http://git.meshlink.io/?p=meshlink;a=blobdiff_plain;f=test%2Finvite-join.c;h=e3f85ed69a09569780b01a977fba402d7cd9d9d8;hp=a7bee95c0f9b9440812a4bc0745b8a38174e09fb;hb=fec95d1221c2d7e2059d6ba2fe244211ccee95ad;hpb=01eaeb3c9fa60ae9c6e5b866acd9baef79622d99 diff --git a/test/invite-join.c b/test/invite-join.c index a7bee95c..e3f85ed6 100644 --- a/test/invite-join.c +++ b/test/invite-join.c @@ -1,92 +1,182 @@ +#ifdef NDEBUG +#undef NDEBUG +#endif + #include #include #include #include +#include +#include #include "meshlink.h" +#include "utils.h" + +static struct sync_flag baz_reachable; -volatile bool baz_reachable = false; +static void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { + (void)mesh; -void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { - if(!strcmp(node->name, "baz")) - baz_reachable = reachable; + if(reachable && !strcmp(node->name, "baz")) { + set_sync_flag(&baz_reachable, true); + } } -int main(int argc, char *argv[]) { - // Open two new meshlink instance. +int main() { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); - meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join"); - if(!mesh1) { - fprintf(stderr, "Could not initialize configuration for foo\n"); - return 1; - } + assert(meshlink_destroy("invite_join_conf.1")); + assert(meshlink_destroy("invite_join_conf.2")); + assert(meshlink_destroy("invite_join_conf.3")); - meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join"); - if(!mesh2) { - fprintf(stderr, "Could not initialize configuration for bar\n"); - return 1; - } + // Open thee new meshlink instance. + + meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE); + assert(mesh1); + + meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE); + assert(mesh2); - // Start the first instance and have it generate an invitation. + meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE); + assert(mesh3); + + // Disable local discovery. + + meshlink_enable_discovery(mesh1, false); + meshlink_enable_discovery(mesh2, false); + meshlink_enable_discovery(mesh3, false); + + // Have the first instance generate invitations. meshlink_set_node_status_cb(mesh1, status_cb); - - if(!meshlink_start(mesh1)) { - fprintf(stderr, "Foo could not start\n"); - return 1; - } - meshlink_add_address(mesh1, "localhost"); - char *url = meshlink_invite(mesh1, "baz"); - if(!url) { - fprintf(stderr, "Foo could not generate an invitation for baz\n"); - return 1; - } + assert(meshlink_set_canonical_address(mesh1, meshlink_get_self(mesh1), "localhost", NULL)); - // Have the second instance join the first. + char *baz_url = meshlink_invite(mesh1, NULL, "baz"); + assert(baz_url); - if(!meshlink_join(mesh2, url)) { - fprintf(stderr, "Baz could not join foo's mesh\n"); - return 1; - } + char *quux_url = meshlink_invite(mesh1, NULL, "quux"); + assert(quux_url); - free(url); + // Have the second instance join the first. - if(!meshlink_start(mesh2)) { - fprintf(stderr, "Baz could not start\n"); - return 1; - } + assert(meshlink_start(mesh1)); + + assert(meshlink_join(mesh2, baz_url)); + assert(meshlink_start(mesh2)); // Wait for the two to connect. - for(int i = 0; i < 60; i++) { - sleep(1); - if(baz_reachable) - break; - } + assert(wait_sync_flag(&baz_reachable, 20)); - if(!baz_reachable) { - fprintf(stderr, "Baz not reachable for foo after 20 seconds\n"); - return 1; - } + // Wait for UDP communication to become possible. int pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz")); + for(int i = 0; i < 10 && !pmtu; i++) { sleep(1); pmtu = meshlink_get_pmtu(mesh1, meshlink_get_node(mesh1, "baz")); } - if(!pmtu) { - fprintf(stderr, "UDP communication with baz not possible after 10 seconds\n"); - return 1; - } + assert(pmtu); - // Clean up. + // Check that an invitation cannot be used twice + + assert(!meshlink_join(mesh3, baz_url)); + free(baz_url); + + // Check that nodes cannot join with expired invitations + + meshlink_set_invitation_timeout(mesh1, 0); + + assert(!meshlink_join(mesh3, quux_url)); + free(quux_url); + + // Check that existing nodes cannot join another mesh + + char *corge_url = meshlink_invite(mesh3, NULL, "corge"); + assert(corge_url); + + assert(meshlink_start(mesh3)); meshlink_stop(mesh2); + + assert(!meshlink_join(mesh2, corge_url)); + free(corge_url); + + // Check that invitations work correctly after changing ports + + meshlink_set_invitation_timeout(mesh1, 86400); meshlink_stop(mesh1); + meshlink_stop(mesh3); + + int oldport = meshlink_get_port(mesh1); + bool success = false; + + for(int i = 0; !success && i < 100; i++) { + success = meshlink_set_port(mesh1, 0x9000 + rand() % 0x1000); + } + + assert(success); + int newport = meshlink_get_port(mesh1); + assert(oldport != newport); + + assert(meshlink_start(mesh1)); + quux_url = meshlink_invite(mesh1, NULL, "quux"); + assert(quux_url); + + // The old port should not be in the invitation URL + + char portstr[10]; + snprintf(portstr, sizeof(portstr), ":%d", oldport); + assert(!strstr(quux_url, portstr)); + + // The new port should be in the invitation URL + + snprintf(portstr, sizeof(portstr), ":%d", newport); + assert(strstr(quux_url, portstr)); + + // The invitation should work + + assert(meshlink_join(mesh3, quux_url)); + free(quux_url); + + // Check that adding duplicate addresses get removed correctly + + assert(meshlink_add_invitation_address(mesh1, "localhost", portstr + 1)); + corge_url = meshlink_invite(mesh1, NULL, "corge"); + assert(corge_url); + char *localhost = strstr(corge_url, "localhost"); + assert(localhost); + assert(!strstr(localhost + 1, "localhost")); + free(corge_url); + + // Check that resetting and adding multiple, different invitation address works + + meshlink_clear_invitation_addresses(mesh1); + assert(meshlink_add_invitation_address(mesh1, "1.invalid.", "12345")); + assert(meshlink_add_invitation_address(mesh1, "2.invalid.", NULL)); + assert(meshlink_add_invitation_address(mesh1, "3.invalid.", NULL)); + assert(meshlink_add_invitation_address(mesh1, "4.invalid.", NULL)); + assert(meshlink_add_invitation_address(mesh1, "5.invalid.", NULL)); + char *grault_url = meshlink_invite(mesh1, NULL, "grault"); + assert(grault_url); + localhost = strstr(grault_url, "localhost"); + assert(localhost); + char *invalid1 = strstr(grault_url, "1.invalid.:12345"); + assert(invalid1); + char *invalid5 = strstr(grault_url, "5.invalid."); + assert(invalid5); + + // Check that explicitly added invitation addresses come before others, in the order they were specified. + + assert(invalid1 < invalid5); + assert(invalid5 < localhost); + free(grault_url); + + // Clean up. + + meshlink_close(mesh3); meshlink_close(mesh2); meshlink_close(mesh1); - - return 0; }