]> git.meshlink.io Git - meshlink/blob - test/import-export.c
Refactor the non-blackbox test suite.
[meshlink] / test / import-export.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/time.h>
6 #include <assert.h>
7
8 #include "meshlink.h"
9 #include "utils.h"
10
11 struct sync_flag bar_reachable;
12
13 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
14         (void)mesh;
15
16         if(reachable && !strcmp(node->name, "bar")) {
17                 set_sync_flag(&bar_reachable, true);
18         }
19 }
20
21 int main() {
22         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
23
24         // Open two new meshlink instance.
25
26         assert(meshlink_destroy("import_export_conf.1"));
27         assert(meshlink_destroy("import_export_conf.2"));
28
29         meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE);
30         assert(mesh1);
31
32         meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar", "import-export", DEV_CLASS_BACKBONE);
33         assert(mesh2);
34
35         // Disable local discovery
36
37         meshlink_enable_discovery(mesh1, false);
38         meshlink_enable_discovery(mesh2, false);
39
40         // Import and export both side's data
41
42         meshlink_add_address(mesh1, "localhost");
43         meshlink_add_address(mesh2, "localhost");
44
45         char *data = meshlink_export(mesh1);
46         assert(data);
47
48         assert(meshlink_import(mesh2, data));
49         free(data);
50
51         data = meshlink_export(mesh2);
52         assert(data);
53
54         assert(meshlink_import(mesh1, data));
55         free(data);
56
57         // Start both instances
58
59         meshlink_set_node_status_cb(mesh1, status_cb);
60
61         assert(meshlink_start(mesh1));
62         assert(meshlink_start(mesh2));
63
64         // Wait for the two to connect.
65
66         assert(wait_sync_flag(&bar_reachable, 20));
67
68         // Wait for UDP communication to become possible.
69
70         int pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
71
72         for(int i = 0; i < 10 && !pmtu; i++) {
73                 sleep(1);
74                 pmtu = meshlink_get_pmtu(mesh2, meshlink_get_node(mesh2, "bar"));
75         }
76
77         assert(pmtu);
78
79         // Clean up.
80
81         meshlink_close(mesh2);
82         meshlink_close(mesh1);
83 }