]> git.meshlink.io Git - meshlink/blobdiff - test/import-export.c
Refactor the non-blackbox test suite.
[meshlink] / test / import-export.c
index 0671cf4770dddc68a29220a1e7088809e128694d..b3d52da674599bbec7da4122a6770e800018ec41 100644 (file)
@@ -3,34 +3,18 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/time.h>
+#include <assert.h>
 
 #include "meshlink.h"
+#include "utils.h"
 
-volatile bool bar_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);
-}
+struct sync_flag bar_reachable;
 
 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
        (void)mesh;
 
-       if(!strcmp(node->name, "bar")) {
-               bar_reachable = reachable;
+       if(reachable && !strcmp(node->name, "bar")) {
+               set_sync_flag(&bar_reachable, true);
        }
 }
 
@@ -39,22 +23,14 @@ int main() {
 
        // Open two new meshlink instance.
 
-       meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE);
+       assert(meshlink_destroy("import_export_conf.1"));
+       assert(meshlink_destroy("import_export_conf.2"));
 
-       if(!mesh1) {
-               fprintf(stderr, "Could not initialize configuration for foo: %s\n", meshlink_strerror(meshlink_errno));
-               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);
-
-       if(!mesh2) {
-               fprintf(stderr, "Could not initialize configuration for bar\n");
-               return 1;
-       }
-
-       meshlink_set_log_cb(mesh1, MESHLINK_DEBUG, log_cb);
-       meshlink_set_log_cb(mesh2, MESHLINK_DEBUG, log_cb);
+       assert(mesh2);
 
        // Disable local discovery
 
@@ -67,64 +43,29 @@ int main() {
        meshlink_add_address(mesh2, "localhost");
 
        char *data = meshlink_export(mesh1);
+       assert(data);
 
-       if(!data) {
-               fprintf(stderr, "Foo could not export its configuration\n");
-               return 1;
-       }
-
-       fprintf(stderr, "Foo export data:\n%s\n", data);
-
-       if(!meshlink_import(mesh2, data)) {
-               fprintf(stderr, "Bar could not import foo's configuration\n");
-               return 1;
-       }
-
+       assert(meshlink_import(mesh2, data));
        free(data);
 
        data = meshlink_export(mesh2);
+       assert(data);
 
-       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(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"));
 
@@ -133,17 +74,10 @@ int main() {
                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;
 }