]> git.meshlink.io Git - meshlink-tiny/blob - test/import-export.c
Update the test suite for Meshlink-tiny.
[meshlink-tiny] / 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-tiny.h"
13 #include "utils.h"
14
15 static struct sync_flag bar_reachable;
16
17 static 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(void) {
26         init_sync_flag(&bar_reachable);
27
28         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
29
30         // Open two new meshlink instance.
31
32         assert(meshlink_destroy("import_export_conf.1"));
33         assert(meshlink_destroy("import_export_conf.2"));
34
35         meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo", "import-export", DEV_CLASS_BACKBONE);
36         assert(mesh1);
37
38         meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar", "import-export", DEV_CLASS_BACKBONE);
39         assert(mesh2);
40
41         // Import and export both side's data
42
43         assert(meshlink_set_canonical_address(mesh1, meshlink_get_self(mesh1), "localhost", NULL));
44         assert(meshlink_set_canonical_address(mesh2, meshlink_get_self(mesh2), "localhost", NULL));
45
46         char *data = meshlink_export(mesh1);
47         assert(data);
48
49         assert(meshlink_import(mesh2, data));
50         free(data);
51
52         data = meshlink_export(mesh2);
53         assert(data);
54
55         assert(meshlink_import(mesh1, data));
56
57         // Check that importing twice is fine
58         assert(meshlink_import(mesh1, data));
59         free(data);
60
61         // Check that importing garbage is not fine
62         assert(!meshlink_import(mesh1, "Garbage\n"));
63
64         // Check that foo knows bar, but that it is not reachable.
65
66         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
67         assert(bar);
68
69         // Clean up.
70
71         meshlink_close(mesh2);
72         meshlink_close(mesh1);
73 }