]> git.meshlink.io Git - meshlink/blob - test/trio.c
Clean up resources in the test cases.
[meshlink] / test / trio.c
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <errno.h>
8 #include <assert.h>
9 #include <sys/time.h>
10
11 #include "meshlink.h"
12 #include "devtools.h"
13 #include "utils.h"
14
15 static void log_cb(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *text) {
16         (void)mesh;
17
18         static struct timeval tv0;
19         struct timeval tv;
20
21         if(tv0.tv_sec == 0) {
22                 gettimeofday(&tv0, NULL);
23         }
24
25         gettimeofday(&tv, NULL);
26         fprintf(stderr, "%u.%.03u ", (unsigned int)(tv.tv_sec - tv0.tv_sec), (unsigned int)tv.tv_usec / 1000);
27
28         if(mesh) {
29                 fprintf(stderr, "(%s) ", mesh->name);
30         }
31
32         fprintf(stderr, "[%d] %s\n", level, text);
33 }
34
35 static bool received = false;
36
37 static void receive_cb(meshlink_handle_t *mesh, meshlink_node_t *source, const void *data, size_t len) {
38         (void)mesh;
39         (void)source;
40
41         fprintf(stderr, "RECEIVED SOMETHING\n");
42
43         if(len == 5 && !memcmp(data, "Hello", 5)) {
44                 received = true;
45         }
46 }
47
48 int main() {
49         // Create three instances.
50
51         const char *name[3] = {"foo", "bar", "baz"};
52         meshlink_handle_t *mesh[3];
53         char *data[3];
54
55         for(int i = 0; i < 3; i++) {
56                 char *path = NULL;
57                 assert(asprintf(&path, "trio_conf.%d", i) != -1 && path);
58
59                 mesh[i] = meshlink_open(path, name[i], "trio", DEV_CLASS_BACKBONE);
60                 assert(mesh[i]);
61                 free(path);
62
63                 meshlink_add_address(mesh[i], "localhost");
64
65                 data[i] = meshlink_export(mesh[i]);
66                 assert(data[i]);
67         }
68
69         // first node knows the two other nodes
70
71         for(int i = 1; i < 3; i++) {
72                 assert(meshlink_import(mesh[i], data[0]));
73                 assert(meshlink_import(mesh[0], data[i]));
74
75                 assert(meshlink_get_node(mesh[i], name[0]));
76                 assert(meshlink_get_node(mesh[0], name[i]));
77         }
78
79         // second and third node should not know each other yet
80
81         assert(!meshlink_get_node(mesh[1], name[2]));
82         assert(!meshlink_get_node(mesh[2], name[1]));
83
84         // start the nodes
85
86         for(int i = 0; i < 3; i++) {
87                 free(data[i]);
88                 assert(meshlink_start(mesh[i]));
89         }
90
91         // the nodes should now learn about each other
92
93         assert_after(meshlink_get_node(mesh[1], name[2]), 5);
94         assert_after(meshlink_get_node(mesh[2], name[1]), 5);
95
96         // Send a packet, expect it is received
97
98         meshlink_set_receive_cb(mesh[1], receive_cb);
99         assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 15);
100
101         // Check that the second and third node have autoconnected to each other
102
103         devtool_edge_t *edges = NULL;
104         size_t nedges = 0;
105         assert_after((edges = devtool_get_all_edges(mesh[1], edges, &nedges), nedges == 3), 15);
106         free(edges);
107
108         // Stop the first node
109
110         meshlink_stop(mesh[0]);
111         sleep(1);
112
113         // Communication should still be possible
114
115         assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 15);
116
117         // Stop the other nodes
118
119         for(int i = 1; i < 3; i++) {
120                 meshlink_stop(mesh[i]);
121         }
122
123         sleep(1);
124
125         // Start just the other two nodes
126
127         meshlink_set_log_cb(mesh[1], MESHLINK_DEBUG, log_cb);
128
129         for(int i = 1; i < 3; i++) {
130                 assert(meshlink_start(mesh[i]));
131         }
132
133         assert(meshlink_get_node(mesh[1], name[2]));
134         assert(meshlink_get_node(mesh[2], name[1]));
135
136         // Communication should still be possible
137
138         received = false;
139         assert_after((meshlink_send(mesh[2], meshlink_get_node(mesh[2], name[1]), "Hello", 5), received), 25);
140
141         // Clean up.
142
143         for(int i = 0; i < 3; i++) {
144                 meshlink_close(mesh[i]);
145         }
146 }