]> git.meshlink.io Git - meshlink/blob - test/channels-framed.c
Update the channels-*-framed tests to test for isolated packets.
[meshlink] / test / channels-framed.c
1 #ifdef NDEBUG
2 #undef NDEBUG
3 #endif
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <sys/time.h>
11 #include <time.h>
12
13 #include "../src/meshlink.h"
14 #include "utils.h"
15
16 static size_t received;
17 static struct sync_flag accept_flag;
18 static struct sync_flag small_flag;
19 static struct sync_flag large_flag;
20 static struct sync_flag close_flag;
21
22 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
23         (void)mesh;
24
25         if(!data && !len) {
26                 meshlink_channel_close(mesh, channel);
27                 set_sync_flag(&close_flag, true);
28         }
29
30         if(len >= 2) {
31                 uint16_t checklen;
32                 memcpy(&checklen, data, sizeof(checklen));
33                 assert(len == checklen);
34         }
35
36         if(len == 65535) {
37                 set_sync_flag(&large_flag, true);
38         }
39
40         if(len == 0) {
41                 set_sync_flag(&small_flag, true);
42         }
43
44         received += len;
45 }
46
47 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
48         assert(port == 1);
49         assert(!data);
50         assert(!len);
51         assert(meshlink_channel_get_flags(mesh, channel) == (MESHLINK_CHANNEL_TCP | MESHLINK_CHANNEL_FRAMED));
52         meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
53         set_sync_flag(&accept_flag, true);
54
55         return true;
56 }
57
58 int main(void) {
59         meshlink_set_log_cb(NULL, MESHLINK_WARNING, log_cb);
60
61         // Open two meshlink instances
62
63         meshlink_handle_t *mesh_a, *mesh_b;
64         open_meshlink_pair(&mesh_a, &mesh_b, "channels_framed");
65         start_meshlink_pair(mesh_a, mesh_b);
66
67         // Create a channel from a to b
68
69         meshlink_set_channel_accept_cb(mesh_b, accept_cb);
70
71         meshlink_node_t *b = meshlink_get_node(mesh_a, "b");
72         assert(b);
73
74         meshlink_channel_t *channel = meshlink_channel_open_ex(mesh_a, b, 1, NULL, NULL, 0, MESHLINK_CHANNEL_TCP | MESHLINK_CHANNEL_FRAMED);
75         assert(channel);
76
77         size_t sndbuf_size = 128 * 1024;
78         meshlink_set_channel_sndbuf(mesh_a, channel, sndbuf_size);
79
80         // Check that we can send zero bytes
81
82         assert(meshlink_channel_send(mesh_a, channel, "", 0) == 0);
83         assert(wait_sync_flag(&small_flag, 1));
84
85         // Check that we cannot send more than 65535 bytes without errors
86
87         char data[65535] = "";
88         assert(meshlink_channel_send(mesh_a, channel, data, 65536) == -1);
89
90         // Check that we can send 65535 bytes
91
92         uint16_t framelen = 65535;
93         memcpy(data, &framelen, sizeof(framelen));
94         assert(meshlink_channel_send(mesh_a, channel, data, framelen) == framelen);
95         assert(wait_sync_flag(&large_flag, 1));
96
97         // Send randomly sized frames from a to b
98
99         size_t total_len = framelen;
100
101         for(int j = 0; j < 2500; j++) {
102                 framelen = rand() % 2048;
103                 memcpy(data, &framelen, sizeof(framelen));
104
105                 while(meshlink_channel_get_sendq(mesh_a, channel) > sndbuf_size - framelen - 2) {
106                         const struct timespec req = {0, 2000000};
107                         clock_nanosleep(CLOCK_MONOTONIC, 0, &req, NULL);
108                 }
109
110                 assert(meshlink_channel_send(mesh_a, channel, data, framelen) == framelen);
111                 total_len += framelen;
112         }
113
114         // Closes the channel and wait for the other end to closes it as well
115
116         meshlink_channel_close(mesh_a, channel);
117         assert(wait_sync_flag(&close_flag, 10));
118
119         // Check that the clients have received all the data we sent
120
121         assert(received == total_len);
122
123         close_meshlink_pair(mesh_a, mesh_b);
124
125         return 0;
126 }