X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=test%2Fimport-export.c;h=b3d52da674599bbec7da4122a6770e800018ec41;hb=9a2520c36431a8a5fd90451e97f488c22f4decc5;hp=d6498c24b6647828a1fffb45f676623ee67eb2fc;hpb=09d1b540e60d5f7c32e80f24401bc2d468904902;p=meshlink diff --git a/test/import-export.c b/test/import-export.c index d6498c24..b3d52da6 100644 --- a/test/import-export.c +++ b/test/import-export.c @@ -2,30 +2,40 @@ #include #include #include +#include +#include #include "meshlink.h" +#include "utils.h" -volatile bool bar_reachable = false; +struct sync_flag bar_reachable; void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) { - if(!strcmp(node->name, "bar")) - bar_reachable = reachable; + (void)mesh; + + if(reachable && !strcmp(node->name, "bar")) { + set_sync_flag(&bar_reachable, true); + } } -int main(int argc, char *argv[]) { +int main() { + meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb); + // Open two new meshlink instance. - meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo"); - if(!mesh1) { - fprintf(stderr, "Could not initialize configuration for foo\n"); - return 1; - } + assert(meshlink_destroy("import_export_conf.1")); + assert(meshlink_destroy("import_export_conf.2")); - meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar"); - if(!mesh2) { - fprintf(stderr, "Could not initialize configuration for bar\n"); - return 1; - } + meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE); + assert(mesh1); + + meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar", "import-export", DEV_CLASS_BACKBONE); + assert(mesh2); + + // Disable local discovery + + meshlink_enable_discovery(mesh1, false); + meshlink_enable_discovery(mesh2, false); // Import and export both side's data @@ -33,76 +43,41 @@ int main(int argc, char *argv[]) { meshlink_add_address(mesh2, "localhost"); char *data = meshlink_export(mesh1); - if(!data) { - fprintf(stderr, "Foo could not export its configuration\n"); - return 1; - } - - if(!meshlink_import(mesh2, data)) { - fprintf(stderr, "Bar could not import foo's configuration\n"); - return 1; - } + assert(data); + assert(meshlink_import(mesh2, data)); free(data); data = meshlink_export(mesh2); - if(!data) { - fprintf(stderr, "Bar could not export its configuration\n"); - return 1; - } - - - if(!meshlink_import(mesh1, data)) { - fprintf(stderr, "Foo could not import bar's configuration\n"); - return 1; - } + assert(data); + assert(meshlink_import(mesh1, data)); free(data); // Start both instances meshlink_set_node_status_cb(mesh1, status_cb); - - if(!meshlink_start(mesh1)) { - fprintf(stderr, "Foo could not start\n"); - return 1; - } - if(!meshlink_start(mesh2)) { - fprintf(stderr, "Bar could not start\n"); - return 1; - } + assert(meshlink_start(mesh1)); + assert(meshlink_start(mesh2)); // Wait for the two to connect. - for(int i = 0; i < 20; i++) { - sleep(1); - if(bar_reachable) - break; - } + assert(wait_sync_flag(&bar_reachable, 20)); - if(!bar_reachable) { - fprintf(stderr, "Bar not reachable for foo after 20 seconds\n"); - return 1; - } + // Wait for UDP communication to become possible. int pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar")); + for(int i = 0; i < 10 && !pmtu; i++) { sleep(1); pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar")); } - if(!pmtu) { - fprintf(stderr, "UDP communication with bar not possible after 10 seconds\n"); - return 1; - } + assert(pmtu); // Clean up. - meshlink_stop(mesh2); - meshlink_stop(mesh1); meshlink_close(mesh2); meshlink_close(mesh1); - - return 0; }