]> git.meshlink.io Git - meshlink/blob - test/import-export.c
The tests now all print proper error messages.
[meshlink] / test / import-export.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 #include "meshlink.h"
7
8 volatile bool bar_reachable = false;
9
10 void status_cb(meshlink_handle_t *mesh, meshlink_node_t *node, bool reachable) {
11         if(!strcmp(node->name, "bar"))
12                 bar_reachable = reachable;
13 }
14
15 int main(int argc, char *argv[]) {
16         // Open two new meshlink instance.
17
18         meshlink_handle_t *mesh1 = meshlink_open("import_export_conf.1", "foo");
19         if(!mesh1) {
20                 fprintf(stderr, "Could not initialize configuration for foo\n");
21                 return 1;
22         }
23
24         meshlink_handle_t *mesh2 = meshlink_open("import_export_conf.2", "bar");
25         if(!mesh2) {
26                 fprintf(stderr, "Could not initialize configuration for bar\n");
27                 return 1;
28         }
29
30         // Import and export both side's data
31
32         meshlink_add_address(mesh1, "localhost");
33         meshlink_add_address(mesh2, "localhost");
34
35         char *data = meshlink_export(mesh1);
36         if(!data) {
37                 fprintf(stderr, "Foo could not export its configuration\n");
38                 return 1;
39         }
40
41         if(!meshlink_import(mesh2, data)) {
42                 fprintf(stderr, "Bar could not import foo's configuration\n");
43                 return 1;
44         }
45
46         free(data);
47
48         data = meshlink_export(mesh2);
49         if(!data) {
50                 fprintf(stderr, "Bar could not export its configuration\n");
51                 return 1;
52         }
53
54
55         if(!meshlink_import(mesh1, data)) {
56                 fprintf(stderr, "Foo could not import bar's configuration\n");
57                 return 1;
58         }
59
60         free(data);
61
62         // Start both instances
63
64         meshlink_set_node_status_cb(mesh1, status_cb);
65         
66         if(!meshlink_start(mesh1)) {
67                 fprintf(stderr, "Foo could not start\n");
68                 return 1;
69         }
70
71         if(!meshlink_start(mesh2)) {
72                 fprintf(stderr, "Bar could not start\n");
73                 return 1;
74         }
75
76         // Wait for the two to connect.
77
78         for(int i = 0; i < 20; i++) {
79                 sleep(1);
80                 if(bar_reachable)
81                         break;
82         }
83
84         if(!bar_reachable) {
85                 fprintf(stderr, "Bar not reachable for foo after 20 seconds\n");
86                 return 1;
87         }
88
89         // Clean up.
90
91         meshlink_stop(mesh2);
92         meshlink_stop(mesh1);
93         meshlink_close(mesh2);
94         meshlink_close(mesh1);
95
96         return 0;
97 }