zhangmeng
2024-04-22 16935f4aebffdd1b6580b844391a0aa0f4f3012b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//
 
// Basic UDP tests.
 
#ifndef _WIN32
#include <arpa/inet.h>
#endif
 
#include "convey.h"
#include "core/nng_impl.h"
#include "trantest.h"
 
TestMain("UDP support", {
    nni_init();
 
    trantest_port = trantest_port ? trantest_port : 5555;
 
    Convey("We can start a pair of UDP ports", {
        nng_sockaddr  sa1;
        nng_sockaddr  sa2;
        uint16_t      p1;
        uint16_t      p2;
        nni_plat_udp *u1;
        nni_plat_udp *u2;
        uint32_t      loopback;
 
        loopback = htonl(0x7f000001); // 127.0.0.1
 
        p1 = trantest_port++;
        p2 = trantest_port++;
 
        sa1.s_in.sa_family = NNG_AF_INET;
        sa1.s_in.sa_addr   = loopback;
        sa1.s_in.sa_port   = htons(p1);
 
        sa2.s_in.sa_family = NNG_AF_INET;
        sa2.s_in.sa_addr   = loopback;
        sa2.s_in.sa_port   = htons(p2);
 
        So(nni_plat_udp_open(&u1, &sa1) == 0);
        So(nni_plat_udp_open(&u2, &sa2) == 0);
        Reset({
            nni_plat_udp_close(u1);
            nni_plat_udp_close(u2);
        });
 
        Convey("They can talk to each other", {
            char         msg[] = "hello";
            char         rbuf[1024];
            nng_sockaddr to;
            nng_sockaddr from;
            nng_aio *    aio1;
            nng_aio *    aio2;
            nng_iov      iov1;
            nng_iov      iov2;
 
            So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
            So(nng_aio_alloc(&aio2, NULL, NULL) == 0);
 
            to           = sa2;
            iov1.iov_buf = msg;
            iov1.iov_len = strlen(msg) + 1;
            So(nng_aio_set_iov(aio1, 1, &iov1) == 0);
            nng_aio_set_input(aio1, 0, &to);
 
            iov2.iov_buf = rbuf;
            iov2.iov_len = 1024;
            So(nng_aio_set_iov(aio2, 1, &iov2) == 0);
            nng_aio_set_input(aio2, 0, &from);
 
            nni_plat_udp_recv(u2, aio2);
            nni_plat_udp_send(u1, aio1);
            nng_aio_wait(aio1);
            nng_aio_wait(aio2);
 
            So(nng_aio_result(aio1) == 0);
            So(nng_aio_result(aio2) == 0);
 
            So(nng_aio_count(aio2) == strlen(msg) + 1);
            So(strcmp(msg, rbuf) == 0);
 
            So(from.s_in.sa_family == sa1.s_in.sa_family);
            So(from.s_in.sa_port == sa1.s_in.sa_port);
            So(from.s_in.sa_addr == sa1.s_in.sa_addr);
 
            // Set up some calls that will ever complete, so
            // we test cancellation, etc.
            nni_plat_udp_recv(u2, aio2);
            nni_plat_udp_send(u2, aio1);
 
            nng_aio_free(aio1);
            nng_aio_free(aio2);
        });
 
        Convey("Sending without an address fails", {
            nng_aio *aio1;
            char *   msg = "nope";
            nng_iov  iov;
 
            So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
 
            iov.iov_buf = msg;
            iov.iov_len = strlen(msg) + 1;
            So(nng_aio_set_iov(aio1, 1, &iov) == 0);
 
            nni_plat_udp_send(u1, aio1);
            nng_aio_wait(aio1);
            So(nng_aio_result(aio1) == NNG_EADDRINVAL);
            nng_aio_free(aio1);
        });
 
        Convey("Multiple operations work", {
            char         msg1[] = "hello";
            char         msg2[] = "there";
            char         rbuf1[32];
            char         rbuf2[32];
            nng_sockaddr to1;
            nng_sockaddr to2;
            nng_sockaddr from1;
            nng_sockaddr from2;
            nng_aio *    aio1;
            nng_aio *    aio2;
            nng_aio *    aio3;
            nng_aio *    aio4;
            nng_iov      iov1;
            nng_iov      iov2;
            nng_iov      iov3;
            nng_iov      iov4;
 
            So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
            So(nng_aio_alloc(&aio2, NULL, NULL) == 0);
            So(nng_aio_alloc(&aio3, NULL, NULL) == 0);
            So(nng_aio_alloc(&aio4, NULL, NULL) == 0);
 
            to1          = sa2;
            iov1.iov_buf = msg1;
            iov1.iov_len = strlen(msg1) + 1;
            So(nng_aio_set_iov(aio1, 1, &iov1) == 0);
            nng_aio_set_input(aio1, 0, &to1);
 
            to2          = sa2;
            iov2.iov_buf = msg2;
            iov2.iov_len = strlen(msg2) + 1;
            So(nng_aio_set_iov(aio2, 1, &iov2) == 0);
            nng_aio_set_input(aio2, 0, &to2);
 
            iov3.iov_buf = rbuf1;
            iov3.iov_len = 1024;
            So(nng_aio_set_iov(aio3, 1, &iov3) == 0);
            nng_aio_set_input(aio3, 0, &from1);
 
            iov4.iov_buf = rbuf2;
            iov4.iov_len = 1024;
            So(nng_aio_set_iov(aio4, 1, &iov4) == 0);
            nng_aio_set_input(aio4, 0, &from2);
 
            nni_plat_udp_recv(u2, aio4);
            nni_plat_udp_recv(u2, aio3);
            nni_plat_udp_send(u1, aio2);
            // This delay here is required to test for a race
            // condition that does not occur if it is absent.
            nng_msleep(1);
            nni_plat_udp_send(u1, aio1);
 
            nng_aio_wait(aio2);
            nng_aio_wait(aio1);
            nng_aio_wait(aio3);
            nng_aio_wait(aio4);
 
            So(nng_aio_result(aio1) == 0);
            So(nng_aio_result(aio2) == 0);
            So(nng_aio_result(aio3) == 0);
            So(nng_aio_result(aio4) == 0);
 
            So(from1.s_in.sa_family == sa1.s_in.sa_family);
            So(from1.s_in.sa_port == sa1.s_in.sa_port);
            So(from1.s_in.sa_addr == sa1.s_in.sa_addr);
 
            nng_aio_free(aio1);
            nng_aio_free(aio2);
            nng_aio_free(aio3);
            nng_aio_free(aio4);
        });
 
        Convey("Sending without an address fails", {
            nng_aio *aio1;
            char *   msg = "nope";
            nng_iov  iov;
 
            So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
            iov.iov_buf = msg;
            iov.iov_len = strlen(msg) + 1;
            So(nng_aio_set_iov(aio1, 1, &iov) == 0);
 
            nni_plat_udp_send(u1, aio1);
            nng_aio_wait(aio1);
            So(nng_aio_result(aio1) == NNG_EADDRINVAL);
            nng_aio_free(aio1);
        });
 
        Convey("Sending to an IPv6 address on IPv4 fails", {
            nng_aio *    aio1;
            char *       msg = "nope";
            nng_sockaddr sa;
            int          rv;
            nng_iov      iov;
 
            sa.s_in6.sa_family = NNG_AF_INET6;
            // address is for google.com
            inet_ntop(AF_INET6, "2607:f8b0:4007:804::200e",
                (void *) sa.s_in6.sa_addr, 16);
            sa.s_in6.sa_port = 80;
            So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
            iov.iov_buf = msg;
            iov.iov_len = strlen(msg) + 1;
            So(nng_aio_set_iov(aio1, 1, &iov) == 0);
            nng_aio_set_input(aio1, 0, &sa);
 
            nni_plat_udp_send(u1, aio1);
            nng_aio_wait(aio1);
            So((rv = nng_aio_result(aio1)) != 0);
            So(rv == NNG_EADDRINVAL || rv == NNG_ENOTSUP ||
                rv == NNG_EUNREACHABLE || rv == NNG_EINVAL);
            nng_aio_free(aio1);
        });
 
        Convey("Sending to an IPC address fails", {
            nng_aio *    aio1;
            char *       msg = "nope";
            nng_sockaddr sa;
            int          rv;
            nng_iov      iov;
 
            sa.s_in6.sa_family = NNG_AF_INET6;
            // address is for google.com
            inet_ntop(AF_INET6, "2607:f8b0:4007:804::200e",
                (void *) sa.s_in6.sa_addr, 16);
            sa.s_in6.sa_port = 80;
            So(nng_aio_alloc(&aio1, NULL, NULL) == 0);
            iov.iov_buf = msg;
            iov.iov_len = strlen(msg) + 1;
            So(nng_aio_set_iov(aio1, 1, &iov) == 0);
            nng_aio_set_input(aio1, 0, &sa);
 
            nni_plat_udp_send(u1, aio1);
            nng_aio_wait(aio1);
            So((rv = nng_aio_result(aio1)) != 0);
            So(rv == NNG_EADDRINVAL || rv == NNG_ENOTSUP ||
                rv == NNG_EUNREACHABLE || rv == NNG_EINVAL);
            nng_aio_free(aio1);
        });
    });
 
    Convey("Cannot open using bogus sockaddr", {
        nni_plat_udp *u;
        nng_sockaddr  sa;
        int           rv;
 
        sa.s_ipc.sa_family = NNG_AF_IPC;
        strcpy(sa.s_ipc.sa_path, "/tmp/t");
        So((rv = nni_plat_udp_open(&u, &sa)) != 0);
        // Some platforms reject IPC addresses altogether (Windows),
        // whereas others just say it's not supported with UDP.
        So((rv == NNG_ENOTSUP) || (rv == NNG_EADDRINVAL));
 
        // NULL address also bad.
        So((rv = nni_plat_udp_open(&u, NULL)) == NNG_EADDRINVAL);
    });
 
    Convey("Duplicate binds fail", {
        nni_plat_udp *u1;
        nni_plat_udp *u2;
        nng_sockaddr  sa;
        uint16_t      p1;
 
        p1                = trantest_port++;
        sa.s_in.sa_family = NNG_AF_INET;
        sa.s_in.sa_port   = htons(p1);
        sa.s_in.sa_addr   = htonl(0x7f000001);
        So(nni_plat_udp_open(&u1, &sa) == 0);
        So(nni_plat_udp_open(&u2, &sa) == NNG_EADDRINUSE);
        nni_plat_udp_close(u1);
    });
})