X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=test%2Finvite-join.c;h=c9ebea6043d2a39b05f02c9e163711eec028a61b;hb=facb61a34353a06d8286df34a6224585cc561c3e;hp=90326a0d1d8b574e40395f97c7f5a0e05a5e11cf;hpb=dc0e52cb3e42620c3139e713b373d130aa30b698;p=meshlink diff --git a/test/invite-join.c b/test/invite-join.c index 90326a0d..c9ebea60 100644 --- a/test/invite-join.c +++ b/test/invite-join.c @@ -7,6 +7,24 @@ volatile bool baz_reachable = false; +void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) { + static struct timeval tv0; + struct timeval tv; + + if(tv0.tv_sec == 0) { + gettimeofday(&tv0, NULL); + } + + gettimeofday(&tv, NULL); + fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000); + + if(mesh) { + fprintf(stderr, "(%s) ", mesh->name); + } + + fprintf(stderr, "[%d] %s\n", level, text); +} + void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { (void)mesh; @@ -16,7 +34,9 @@ void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { } int main() { - // Open two new meshlink instance. + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + + // Open thee new meshlink instance. meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo", "invite-join", DEV_CLASS_BACKBONE); @@ -25,6 +45,8 @@ int main() { return 1; } + meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb); + meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar", "invite-join", DEV_CLASS_BACKBONE); if(!mesh2) { @@ -32,12 +54,24 @@ int main() { return 1; } + meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb); + + meshlink_handle_t *mesh3 = meshlink_open("invite_join_conf.3", "quux", "invite-join", DEV_CLASS_BACKBONE); + + if(!mesh3) { + fprintf(stderr, "Could not initialize configuration for quux\n"); + return 1; + } + + meshlink_set_log_cb(mesh3, MESHLINK_DEBUG, log_cb); + // Disable local discovery. meshlink_enable_discovery(mesh1, false); meshlink_enable_discovery(mesh2, false); + meshlink_enable_discovery(mesh3, false); - // Start the first instance and have it generate an invitation. + // Start the first instance and have it generate invitations. meshlink_set_node_status_cb(mesh1, status_cb); @@ -47,21 +81,31 @@ int main() { } meshlink_add_address(mesh1, "localhost"); - char *url = meshlink_invite(mesh1, "baz"); + char *baz_url = meshlink_invite(mesh1, NULL, "baz"); - if(!url) { + if(!baz_url) { fprintf(stderr, "Foo could not generate an invitation for baz\n"); return 1; } + char *quux_url = meshlink_invite(mesh1, NULL, "quux"); + + if(!quux_url) { + fprintf(stderr, "Foo could not generate an invitation for quux\n"); + return 1; + } + + fprintf(stderr, "Invitation URL for baz: %s\n", baz_url); + fprintf(stderr, "Invitation URL for quux: %s\n", quux_url); + // Have the second instance join the first. - if(!meshlink_join(mesh2, url)) { + if(!meshlink_join(mesh2, baz_url)) { fprintf(stderr, "Baz could not join foo's mesh\n"); return 1; } - free(url); + free(baz_url); if(!meshlink_start(mesh2)) { fprintf(stderr, "Baz could not start\n"); @@ -95,10 +139,20 @@ int main() { return 1; } + // Check that nodes cannot join with expired invitations + + meshlink_set_invitation_timeout(mesh1, 0); + + if(meshlink_join(mesh3, quux_url)) { + fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n"); + return 1; + } + + free(quux_url); + // Clean up. - meshlink_stop(mesh2); - meshlink_stop(mesh1); + meshlink_close(mesh3); meshlink_close(mesh2); meshlink_close(mesh1);