]> git.meshlink.io Git - meshlink/blob - test/channels-cornercases.c
ca0d9b67b21b86993af2dcca4626adb6c3a3c64d
[meshlink] / test / channels-cornercases.c
1 #define _GNU_SOURCE
2
3 #ifdef NDEBUG
4 #undef NDEBUG
5 #endif
6
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/time.h>
12 #include <assert.h>
13 #include <pthread.h>
14
15 #include "../src/meshlink.h"
16 #include "utils.h"
17
18 static volatile bool b_responded = false;
19 static volatile bool b_closed = false;
20 static volatile size_t a_poll_cb_len;
21
22 static void a_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
23         (void)mesh;
24         (void)channel;
25
26         if(len == 5 && !memcmp(data, "Hello", 5)) {
27                 b_responded = true;
28         } else if(len == 0) {
29                 b_closed = true;
30                 set_sync_flag(channel->priv, true);
31         }
32 }
33
34 static void b_receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
35         // Send one message back, then close the channel.
36         if(len) {
37                 assert(meshlink_channel_send(mesh, channel, data, len) == (ssize_t)len);
38         }
39
40         meshlink_channel_close(mesh, channel);
41 }
42
43 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
44         (void)port;
45
46         meshlink_set_channel_accept_cb(mesh, NULL);
47         meshlink_set_channel_receive_cb(mesh, channel, b_receive_cb);
48
49         if(data) {
50                 b_receive_cb(mesh, channel, data, len);
51         }
52
53         return true;
54 }
55
56 static void poll_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
57         (void)len;
58
59         meshlink_set_channel_poll_cb(mesh, channel, NULL);
60         set_sync_flag(channel->priv, true);
61 }
62
63 static void poll_cb2(meshlink_handle_t *mesh, meshlink_channel_t *channel, size_t len) {
64         (void)mesh;
65         (void)channel;
66
67         a_poll_cb_len = len;
68         meshlink_set_channel_poll_cb(mesh, channel, NULL);
69         set_sync_flag(channel->priv, true);
70 }
71
72 int main(void) {
73         meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_cb);
74
75         meshlink_handle_t *a, *b;
76         open_meshlink_pair(&a, &b, "channels-cornercases");
77
78         // Set the callbacks.
79
80         meshlink_set_channel_accept_cb(b, accept_cb);
81
82         // Open a channel from a to b before starting the mesh.
83
84         meshlink_node_t *nb = meshlink_get_node(a, "b");
85         assert(nb);
86
87         struct sync_flag channel_opened = {.flag = false};
88         init_sync_flag(&channel_opened);
89
90         meshlink_channel_t *channel = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_opened, 0);
91         assert(channel);
92
93         meshlink_set_channel_poll_cb(a, channel, poll_cb);
94
95         // Start MeshLink and wait for the channel to become connected.
96         start_meshlink_pair(a, b);
97
98         assert(wait_sync_flag(&channel_opened, 20));
99
100         // Re-initialize everything
101         meshlink_channel_close(a, channel);
102         close_meshlink_pair(a, b);
103         b_responded = false;
104         b_closed = false;
105         channel_opened.flag = false;
106         open_meshlink_pair(&a, &b, "channels-cornercases");
107
108         meshlink_set_channel_accept_cb(b, accept_cb);
109
110         start_meshlink_pair(a, b);
111
112         // Create a channel to b
113         nb = meshlink_get_node(a, "b");
114         assert(nb);
115
116         channel = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_opened, 0);
117         assert(channel);
118         meshlink_set_channel_poll_cb(a, channel, poll_cb);
119
120         assert(wait_sync_flag(&channel_opened, 20));
121
122         assert(!b_responded);
123         assert(!b_closed);
124
125         // Send a message to b
126
127         struct sync_flag channel_closed = {.flag = false};
128         init_sync_flag(&channel_closed);
129         channel->priv = &channel_closed;
130
131         assert(meshlink_channel_send(a, channel, "Hello", 5) == 5);
132         assert(wait_sync_flag(&channel_closed, 20));
133         assert(b_responded);
134         assert(b_closed);
135
136         // Try to create a second channel
137
138         struct sync_flag channel_polled = {.flag = false};
139         init_sync_flag(&channel_polled);
140
141         meshlink_channel_t *channel2 = meshlink_channel_open(a, nb, 7, a_receive_cb, &channel_polled, 0);
142         assert(channel2);
143         meshlink_set_channel_poll_cb(a, channel2, poll_cb2);
144
145         assert(wait_sync_flag(&channel_polled, 20));
146
147         assert(0 == a_poll_cb_len);
148
149         meshlink_channel_close(a, channel);
150         meshlink_channel_close(a, channel2);
151         close_meshlink_pair(a, b);
152 }