X-Git-Url: http://git.meshlink.io/?p=meshlink;a=blobdiff_plain;f=test%2Finvite-join.c;h=52a922a89a9509695639dd88e90623255e039884;hp=50b79fb47531b0a59c79275e26611427e6dedc2d;hb=98d0eb964413f6cc655640f207dde4bf5f1ce89d;hpb=6d1ac53f5c34ece4c7a82efb432a8e1d81fcff0d diff --git a/test/invite-join.c b/test/invite-join.c index 50b79fb4..52a922a8 100644 --- a/test/invite-join.c +++ b/test/invite-join.c @@ -2,56 +2,110 @@ #include #include #include +#include #include "meshlink.h" 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) { - if(!strcmp(node->name, "baz")) + (void)mesh; + + if(!strcmp(node->name, "baz")) { baz_reachable = reachable; + } } -int main(int argc, char *argv[]) { - // Open two new meshlink instance. +int main() { + 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); - meshlink_handle_t *mesh1 = meshlink_open("invite_join_conf.1", "foo"); if(!mesh1) { fprintf(stderr, "Could not initialize configuration for foo\n"); return 1; } - meshlink_handle_t *mesh2 = meshlink_open("invite_join_conf.2", "bar"); + 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) { fprintf(stderr, "Could not initialize configuration for bar\n"); return 1; } - // Start the first instance and have it generate an invitation. + 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 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) { + char *baz_url = meshlink_invite(mesh1, NULL, "baz"); + + 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); - if(!meshlink_start(mesh2)) { fprintf(stderr, "Baz could not start\n"); return 1; @@ -61,8 +115,10 @@ int main(int argc, char *argv[]) { for(int i = 0; i < 60; i++) { sleep(1); - if(baz_reachable) + + if(baz_reachable) { break; + } } if(!baz_reachable) { @@ -71,6 +127,7 @@ int main(int argc, char *argv[]) { } 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")); @@ -81,10 +138,54 @@ int main(int argc, char *argv[]) { return 1; } - // Clean up. + // 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); + + if(meshlink_join(mesh3, quux_url)) { + fprintf(stderr, "Quux could join foo's mesh using an outdated invitation\n"); + return 1; + } + + 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); - meshlink_stop(mesh1); + + if(meshlink_join(mesh2, corge_url)) { + fprintf(stderr, "Bar could join twice\n"); + return 1; + } + + free(corge_url); + + // Clean up. + + meshlink_close(mesh3); meshlink_close(mesh2); meshlink_close(mesh1);