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