]> git.meshlink.io Git - meshlink/blob - test/channels-failure.c
Ignore ADD_EDGE messages we know are outdated.
[meshlink] / test / channels-failure.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 "../src/meshlink.h"
13 #include "utils.h"
14
15 static bool listen_cb(meshlink_handle_t *mesh, meshlink_node_t *node, uint16_t port) {
16         (void)mesh;
17         (void)node;
18
19         return port == 7;
20 }
21
22 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
23         (void)mesh;
24         (void)channel;
25         (void)port;
26         (void)data;
27         (void)len;
28
29         return true;
30 }
31
32 static struct sync_flag poll_flag;
33 static size_t poll_len;
34
35 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
36         meshlink_set_channel_poll_cb(mesh, channel, NULL);
37         poll_len = len;
38         set_sync_flag(&poll_flag, true);
39 }
40
41 static struct sync_flag receive_flag;
42 static size_t receive_len;
43
44 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
45         (void)mesh;
46         (void)channel;
47         (void)data;
48
49         receive_len = len;
50         set_sync_flag(&receive_flag, true);
51 }
52
53 int main(void) {
54         init_sync_flag(&poll_flag);
55         init_sync_flag(&receive_flag);
56
57         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
58
59         // Open two meshlink instances.
60
61         meshlink_handle_t *mesh_a, *mesh_b;
62         open_meshlink_pair(&mesh_a, &mesh_b, "channels_failure");
63
64         // Set the callbacks.
65
66         meshlink_set_channel_listen_cb(mesh_b, listen_cb);
67         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
68
69         // Open a channel from a to b
70
71         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
72         assert(b);
73
74         meshlink_channel_t *channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
75         assert(channel);
76
77         meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
78
79         // Start both instances
80
81         start_meshlink_pair(mesh_a, mesh_b);
82
83         // Wait for the channel to be established
84
85         assert(wait_sync_flag(&poll_flag, 10));
86         assert(poll_len != 0);
87
88         sleep(1);
89
90         // Set a very small timeout for channels to b.
91
92         meshlink_set_node_channel_timeout(mesh_a, b, 1);
93
94         // Stop mesh_b. We should get a notification that the channel has closed after a while.
95
96         meshlink_stop(mesh_b);
97
98         assert(wait_sync_flag(&receive_flag, 5));
99         assert(receive_len == 0);
100
101         meshlink_channel_close(mesh_a, channel);
102
103         // Try setting up a new channel while b is still down.
104
105         reset_sync_flag(&poll_flag);
106         reset_sync_flag(&receive_flag);
107
108         channel = meshlink_channel_open(mesh_a, b, 7, NULL, NULL, 0);
109         assert(channel);
110
111         meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
112
113         assert(wait_sync_flag(&poll_flag, 5));
114         assert(poll_len == 0);
115
116         meshlink_channel_close(mesh_a, channel);
117
118         // Restart b and create a new channel to the wrong port
119
120         reset_sync_flag(&poll_flag);
121         reset_sync_flag(&receive_flag);
122
123         meshlink_set_node_channel_timeout(mesh_a, b, 60);
124
125         assert(meshlink_start(mesh_b));
126
127         channel = meshlink_channel_open(mesh_a, b, 42, receive_cb, NULL, 0);
128         meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
129         assert(channel);
130         assert(wait_sync_flag(&poll_flag, 10));
131         assert(poll_len == 0);
132         meshlink_channel_close(mesh_a, channel);
133
134         // Create a channel that will be accepted
135
136         reset_sync_flag(&poll_flag);
137
138         channel = meshlink_channel_open(mesh_a, b, 7, receive_cb, NULL, 0);
139         meshlink_set_channel_poll_cb(mesh_a, channel, poll_cb);
140         assert(channel);
141         assert(wait_sync_flag(&poll_flag, 10));
142         assert(poll_len != 0);
143
144         // Close and reopen b, we should get a fast notification that the channel has been closed.
145
146         meshlink_close(mesh_b);
147         mesh_b = meshlink_open("channels_failure_conf.2", "b", "channels_failure", DEV_CLASS_BACKBONE);
148         assert(mesh_b);
149         assert(meshlink_start(mesh_b));
150
151         assert(wait_sync_flag(&receive_flag, 10));
152         assert(receive_len == 0);
153
154         // Clean up.
155
156         close_meshlink_pair(mesh_a, mesh_b);
157 }