X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=test%2Finvite-join.c;h=dbf55a22f06018dd97cf2b316bb178da9560a8ae;hb=9e8e77dba3462c4a7f7e758ade4d16bc669fc4a7;hp=b69eb3d87a267179cc560d212da2706dc6fd6ca5;hpb=cdb3dada645394bb1e0d0bf7816291fbd9a2a4a0;p=meshlink diff --git a/test/invite-join.c b/test/invite-join.c index b69eb3d8..dbf55a22 100644 --- a/test/invite-join.c +++ b/test/invite-join.c @@ -3,6 +3,7 @@ #include #include #include +#include #include "meshlink.h" @@ -106,8 +107,6 @@ int main() { return 1; } - free(baz_url); - if(!meshlink_start(mesh2)) { fprintf(stderr, "Baz could not start\n"); return 1; @@ -140,6 +139,15 @@ int main() { return 1; } + // Check that an invitation cannot be used twice + + if(meshlink_join(mesh3, baz_url)) { + fprintf(stderr, "Quux could join foo's mesh using an already used invitation\n"); + return 1; + } + + free(baz_url); + // Check that nodes cannot join with expired invitations meshlink_set_invitation_timeout(mesh1, 0); @@ -151,6 +159,68 @@ int main() { free(quux_url); + // Check that existing nodes cannot join another mesh + + char *corge_url = meshlink_invite(mesh3, NULL, "corge"); + + if(!corge_url) { + fprintf(stderr, "Quux could not generate an invitation for corge\n"); + return 1; + } + + fprintf(stderr, "Invitation URL for corge: %s\n", corge_url); + + if(!meshlink_start(mesh3)) { + fprintf(stderr, "Quux could not start\n"); + return 1; + } + + meshlink_stop(mesh2); + + if(meshlink_join(mesh2, corge_url)) { + fprintf(stderr, "Bar could join twice\n"); + return 1; + } + + 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; + + for(int i = 0; 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"); + fprintf(stderr, "Invitation URL for quux: %s\n", 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); + // Clean up. meshlink_close(mesh3);