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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
// Copyright 2017 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.
//
 
#include <nuts.h>
 
#define SECOND 1000
 
#define APPEND_STR(m, s) NUTS_TRUE(nng_msg_append(m, s, strlen(s)) == 0)
#define CHECK_STR(m, s)                         \
    NUTS_TRUE(nng_msg_len(m) == strlen(s)); \
    NUTS_TRUE(memcmp(nng_msg_body(m), s, strlen(s)) == 0)
 
static void
test_mono_identity(void)
{
    nng_socket s;
    int        p;
    char *     n;
 
    NUTS_PASS(nng_pair1_open(&s));
    NUTS_PASS(nng_socket_get_int(s, NNG_OPT_PROTO, &p));
    NUTS_TRUE(p == NUTS_PROTO(1u, 1u)); // 32
    NUTS_PASS(nng_socket_get_int(s, NNG_OPT_PEER, &p));
    NUTS_TRUE(p == NUTS_PROTO(1u, 1u)); // 33
    NUTS_PASS(nng_socket_get_string(s, NNG_OPT_PROTONAME, &n));
    NUTS_MATCH(n, "pair1");
    nng_strfree(n);
    NUTS_PASS(nng_socket_get_string(s, NNG_OPT_PEERNAME, &n));
    NUTS_MATCH(n, "pair1");
    nng_strfree(n);
    NUTS_CLOSE(s);
}
 
void
test_mono_cooked(void)
{
    nng_socket s1;
    nng_socket c1;
    nng_msg *  msg;
 
    NUTS_PASS(nng_pair1_open(&s1));
    NUTS_PASS(nng_pair1_open(&c1));
    NUTS_PASS(nuts_marry(s1, c1));
 
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_append(msg, "ALPHA", strlen("ALPHA") + 1));
    NUTS_PASS(nng_sendmsg(c1, msg, 0));
    NUTS_PASS(nng_recvmsg(s1, &msg, 0));
    NUTS_TRUE(nng_msg_len(msg) == strlen("ALPHA") + 1);
    NUTS_MATCH(nng_msg_body(msg), "ALPHA");
    nng_msg_free(msg);
 
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_append(msg, "BETA", strlen("BETA") + 1));
    NUTS_PASS(nng_sendmsg(s1, msg, 0));
    NUTS_PASS(nng_recvmsg(c1, &msg, 0));
    NUTS_TRUE(nng_msg_len(msg) == strlen("BETA") + 1);
    NUTS_MATCH(nng_msg_body(msg), "BETA");
 
    nng_msg_free(msg);
    NUTS_CLOSE(c1);
    NUTS_CLOSE(s1);
}
 
void
test_mono_faithful(void)
{
    nng_socket  s1;
    nng_socket  c1;
    nng_socket  c2;
    nng_msg *   msg;
    const char *addr = "inproc://pair1_mono_faithful";
 
    NUTS_PASS(nng_pair1_open(&s1));
    NUTS_PASS(nng_pair1_open(&c1));
    NUTS_PASS(nng_pair1_open(&c2));
    NUTS_PASS(nng_setopt_ms(s1, NNG_OPT_RECVTIMEO, SECOND / 4));
    NUTS_PASS(nng_setopt_ms(c1, NNG_OPT_SENDTIMEO, SECOND));
    NUTS_PASS(nng_setopt_ms(c2, NNG_OPT_SENDTIMEO, SECOND));
    NUTS_PASS(nng_setopt_int(c2, NNG_OPT_SENDBUF, 2));
 
    NUTS_PASS(nng_listen(s1, addr, NULL, 0));
    NUTS_MARRY(s1, c1);
    NUTS_PASS(nng_dial(c2, addr, NULL, 0));
 
    NUTS_SLEEP(100);
 
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    APPEND_STR(msg, "ONE");
    NUTS_PASS(nng_sendmsg(c1, msg, 0));
    NUTS_PASS(nng_recvmsg(s1, &msg, 0));
    CHECK_STR(msg, "ONE");
    nng_msg_free(msg);
 
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    APPEND_STR(msg, "TWO");
    NUTS_PASS(nng_sendmsg(c2, msg, 0));
    NUTS_FAIL(nng_recvmsg(s1, &msg, 0), NNG_ETIMEDOUT);
 
    NUTS_CLOSE(s1);
    NUTS_CLOSE(c1);
    NUTS_CLOSE(c2);
}
 
void
test_mono_back_pressure(void)
{
    nng_socket   s1;
    nng_socket   c1;
    int          i;
    int          rv;
    nng_msg *    msg;
    nng_duration to = 100;
 
    NUTS_PASS(nng_pair1_open(&s1));
    NUTS_PASS(nng_pair1_open(&c1));
    NUTS_PASS(nng_setopt_int(s1, NNG_OPT_RECVBUF, 1));
    NUTS_PASS(nng_setopt_int(s1, NNG_OPT_SENDBUF, 1));
    NUTS_PASS(nng_setopt_int(c1, NNG_OPT_RECVBUF, 1));
    NUTS_PASS(nng_setopt_ms(s1, NNG_OPT_SENDTIMEO, to));
 
    NUTS_MARRY(s1, c1);
 
    // We choose to allow some buffering.  In reality the
    // buffer size is just 1, and we will fail after 2.
    for (i = 0, rv = 0; i < 10; i++) {
        NUTS_PASS(nng_msg_alloc(&msg, 0));
        if ((rv = nng_sendmsg(s1, msg, 0)) != 0) {
            nng_msg_free(msg);
            break;
        }
    }
    NUTS_FAIL(rv, NNG_ETIMEDOUT);
    NUTS_TRUE(i < 10);
    NUTS_CLOSE(s1);
    NUTS_CLOSE(c1);
}
 
void
test_send_no_peer(void)
{
    nng_socket   s1;
    nng_msg *    msg;
    nng_duration to = 100;
 
    NUTS_PASS(nng_pair1_open(&s1));
    NUTS_PASS(nng_setopt_ms(s1, NNG_OPT_SENDTIMEO, to));
 
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_FAIL(nng_sendmsg(s1, msg, 0), NNG_ETIMEDOUT);
    nng_msg_free(msg);
    NUTS_CLOSE(s1);
}
 
void
test_mono_raw_exchange(void)
{
    nng_socket s1;
    nng_socket c1;
 
    nng_msg *msg;
    uint32_t hops;
 
    NUTS_PASS(nng_pair1_open_raw(&s1));
    NUTS_PASS(nng_pair1_open_raw(&c1));
 
    NUTS_PASS(nng_setopt_ms(s1, NNG_OPT_RECVTIMEO, SECOND));
    NUTS_PASS(nng_setopt_ms(c1, NNG_OPT_RECVTIMEO, SECOND));
    NUTS_MARRY(s1, c1);
 
    nng_pipe p = NNG_PIPE_INITIALIZER;
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    APPEND_STR(msg, "GAMMA");
    NUTS_PASS(nng_msg_header_append_u32(msg, 1));
    NUTS_TRUE(nng_msg_header_len(msg) == sizeof(uint32_t));
    NUTS_PASS(nng_sendmsg(c1, msg, 0));
    NUTS_PASS(nng_recvmsg(s1, &msg, 0));
    p = nng_msg_get_pipe(msg);
    NUTS_TRUE(nng_pipe_id(p) > 0);
 
    CHECK_STR(msg, "GAMMA");
    NUTS_TRUE(nng_msg_header_len(msg) == sizeof(uint32_t));
    NUTS_PASS(nng_msg_header_trim_u32(msg, &hops));
    NUTS_TRUE(hops == 2);
    nng_msg_free(msg);
 
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    APPEND_STR(msg, "EPSILON");
    NUTS_PASS(nng_msg_header_append_u32(msg, 1));
    NUTS_PASS(nng_sendmsg(s1, msg, 0));
    NUTS_PASS(nng_recvmsg(c1, &msg, 0));
    CHECK_STR(msg, "EPSILON");
    NUTS_TRUE(nng_msg_header_len(msg) == sizeof(uint32_t));
    NUTS_PASS(nng_msg_header_trim_u32(msg, &hops));
    p = nng_msg_get_pipe(msg);
    NUTS_TRUE(nng_pipe_id(p) > 0);
 
    NUTS_TRUE(hops == 2);
    nng_msg_free(msg);
 
    NUTS_CLOSE(s1);
    NUTS_CLOSE(c1);
}
 
void
test_mono_raw_header(void)
{
    nng_socket s1;
    nng_socket c1;
    nng_msg *  msg;
    uint32_t   v;
 
    NUTS_PASS(nng_pair1_open_raw(&s1));
    NUTS_PASS(nng_pair1_open_raw(&c1));
 
    NUTS_PASS(nng_setopt_ms(s1, NNG_OPT_RECVTIMEO, SECOND / 5));
    NUTS_PASS(nng_setopt_ms(c1, NNG_OPT_RECVTIMEO, SECOND / 5));
    NUTS_MARRY(s1, c1);
 
    // Missing bits in the header
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_FAIL(nng_sendmsg(c1, msg, 0), NNG_EPROTO);
    nng_msg_free(msg);
 
    // Valid header works
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_append_u32(msg, 0xFEEDFACE));
    NUTS_PASS(nng_msg_header_append_u32(msg, 1));
    NUTS_PASS(nng_sendmsg(c1, msg, 0));
    NUTS_PASS(nng_recvmsg(s1, &msg, 0));
    NUTS_PASS(nng_msg_trim_u32(msg, &v));
    NUTS_TRUE(v == 0xFEEDFACE);
    nng_msg_free(msg);
 
    // Header with reserved bits set dropped
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_header_append_u32(msg, 0xDEAD0000));
    NUTS_FAIL(nng_sendmsg(c1, msg, 0), NNG_EPROTO);
    nng_msg_free(msg);
 
    // Header with no chance to add another hop gets dropped
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_header_append_u32(msg, 0xff));
    NUTS_FAIL(nng_sendmsg(c1, msg, 0), NNG_EPROTO);
    nng_msg_free(msg);
 
    // With the same bits clear it works
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_append_u32(msg, 0xFEEDFACE));
    NUTS_PASS(nng_msg_header_append_u32(msg, 1));
    NUTS_PASS(nng_sendmsg(c1, msg, 0));
    NUTS_PASS(nng_recvmsg(s1, &msg, 0));
    NUTS_PASS(nng_msg_trim_u32(msg, &v));
    NUTS_TRUE(v == 0xFEEDFACE);
    nng_msg_free(msg);
 
    NUTS_CLOSE(s1);
    NUTS_CLOSE(c1);
}
 
void
test_pair1_send_closed_aio(void)
{
    nng_socket s1;
    nng_aio *  aio;
    nng_msg *  msg;
 
    NUTS_PASS(nng_aio_alloc(&aio, NULL, NULL));
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_pair1_open(&s1));
    nng_aio_set_msg(aio, msg);
    nng_aio_stop(aio);
    nng_send_aio(s1, aio);
    nng_aio_wait(aio);
    NUTS_FAIL(nng_aio_result(aio), NNG_ECANCELED);
    nng_msg_free(msg);
    nng_aio_free(aio);
    NUTS_PASS(nng_close(s1));
}
 
void
test_pair1_raw(void)
{
    nng_socket s1;
    bool       raw;
 
    NUTS_PASS(nng_pair1_open(&s1));
    NUTS_PASS(nng_getopt_bool(s1, NNG_OPT_RAW, &raw));
    NUTS_TRUE(raw == false);
    NUTS_FAIL(nng_setopt_bool(s1, NNG_OPT_RAW, true), NNG_EREADONLY);
    NUTS_PASS(nng_close(s1));
 
    NUTS_PASS(nng_pair1_open_raw(&s1));
    NUTS_PASS(nng_getopt_bool(s1, NNG_OPT_RAW, &raw));
    NUTS_TRUE(raw == true);
    NUTS_FAIL(nng_setopt_bool(s1, NNG_OPT_RAW, false), NNG_EREADONLY);
    NUTS_PASS(nng_close(s1));
}
 
void
test_pair1_ttl(void)
{
    nng_socket s1;
    nng_socket c1;
    nng_msg *  msg;
    uint32_t   val;
    int        ttl;
 
    NUTS_PASS(nng_pair1_open_raw(&s1));
    NUTS_PASS(nng_pair1_open_raw(&c1));
    NUTS_PASS(nng_setopt_ms(s1, NNG_OPT_RECVTIMEO, SECOND / 5));
    NUTS_PASS(nng_setopt_ms(c1, NNG_OPT_RECVTIMEO, SECOND / 5));
 
    // cannot set insane TTLs
    NUTS_FAIL(nng_setopt_int(s1, NNG_OPT_MAXTTL, 0), NNG_EINVAL);
    NUTS_FAIL(nng_setopt_int(s1, NNG_OPT_MAXTTL, 1000), NNG_EINVAL);
    ttl = 8;
    NUTS_FAIL(nng_setopt(s1, NNG_OPT_MAXTTL, &ttl, 1), NNG_EINVAL);
    NUTS_FAIL(nng_setopt_bool(s1, NNG_OPT_MAXTTL, true), NNG_EBADTYPE);
 
    NUTS_MARRY(s1, c1);
 
    // Let's check enforcement of TTL
    NUTS_PASS(nng_setopt_int(s1, NNG_OPT_MAXTTL, 4));
    NUTS_PASS(nng_getopt_int(s1, NNG_OPT_MAXTTL, &ttl));
    NUTS_TRUE(ttl == 4);
 
    // Bad TTL bounces
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_header_append_u32(msg, 4));
    NUTS_PASS(nng_sendmsg(c1, msg, 0));
    NUTS_FAIL(nng_recvmsg(s1, &msg, 0), NNG_ETIMEDOUT);
 
    // Good TTL passes
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_append_u32(msg, 0xFEEDFACE));
    NUTS_PASS(nng_msg_header_append_u32(msg, 3));
    NUTS_PASS(nng_sendmsg(c1, msg, 0));
    NUTS_PASS(nng_recvmsg(s1, &msg, 0));
    NUTS_PASS(nng_msg_trim_u32(msg, &val));
    NUTS_TRUE(val == 0xFEEDFACE);
    NUTS_PASS(nng_msg_header_trim_u32(msg, &val));
    NUTS_TRUE(val == 4);
    nng_msg_free(msg);
 
    // Large TTL passes
    NUTS_PASS(nng_setopt_int(s1, NNG_OPT_MAXTTL, 15));
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_append_u32(msg, 1234));
    NUTS_PASS(nng_msg_header_append_u32(msg, 14));
    NUTS_PASS(nng_sendmsg(c1, msg, 0));
    NUTS_PASS(nng_recvmsg(s1, &msg, 0));
    NUTS_PASS(nng_msg_trim_u32(msg, &val));
    NUTS_TRUE(val == 1234);
    NUTS_PASS(nng_msg_header_trim_u32(msg, &val));
    NUTS_TRUE(val == 15);
    nng_msg_free(msg);
 
    // Max TTL fails
    NUTS_PASS(nng_setopt_int(s1, NNG_OPT_MAXTTL, 15));
    NUTS_PASS(nng_msg_alloc(&msg, 0));
    NUTS_PASS(nng_msg_header_append_u32(msg, 15));
    NUTS_PASS(nng_sendmsg(c1, msg, 0));
    NUTS_FAIL(nng_recvmsg(s1, &msg, 0), NNG_ETIMEDOUT);
 
    NUTS_CLOSE(s1);
    NUTS_CLOSE(c1);
}
 
void
test_pair1_validate_peer(void)
{
    nng_socket s1, s2;
    nng_stat * stats;
    nng_stat * reject;
    char *     addr;
 
    NUTS_ADDR(addr, "inproc");
    NUTS_PASS(nng_pair1_open(&s1));
    NUTS_PASS(nng_pair0_open(&s2));
 
    NUTS_PASS(nng_listen(s1, addr, NULL, 0));
    NUTS_PASS(nng_dial(s2, addr, NULL, NNG_FLAG_NONBLOCK));
 
    NUTS_SLEEP(100);
    NUTS_PASS(nng_stats_get(&stats));
 
    NUTS_TRUE(stats != NULL);
    NUTS_TRUE((reject = nng_stat_find_socket(stats, s1)) != NULL);
    NUTS_TRUE((reject = nng_stat_find(reject, "reject")) != NULL);
 
    NUTS_TRUE(nng_stat_type(reject) == NNG_STAT_COUNTER);
    NUTS_TRUE(nng_stat_value(reject) > 0);
 
    NUTS_CLOSE(s1);
    NUTS_CLOSE(s2);
    nng_stats_free(stats);
}
 
void
test_pair1_recv_no_header(void)
{
    nng_socket s;
    nng_socket c;
    nng_msg *  m;
 
    NUTS_PASS(nng_pair1_open(&s));
    NUTS_PASS(nng_pair1_open(&c));
    NUTS_PASS(nng_setopt_bool(c, "pair1_test_inject_header", true));
    NUTS_PASS(nng_setopt_ms(s, NNG_OPT_RECVTIMEO, 100));
    NUTS_PASS(nng_setopt_ms(s, NNG_OPT_SENDTIMEO, 200));
 
    NUTS_MARRY(c, s);
 
    NUTS_PASS(nng_msg_alloc(&m, 0));
    NUTS_PASS(nng_sendmsg(c, m, 0));
    NUTS_FAIL(nng_recvmsg(s, &m, 0), NNG_ETIMEDOUT);
 
    NUTS_CLOSE(c);
    NUTS_CLOSE(s);
}
 
void
test_pair1_recv_garbage(void)
{
    nng_socket s;
    nng_socket c;
    nng_msg *  m;
 
    NUTS_PASS(nng_pair1_open(&s));
    NUTS_PASS(nng_pair1_open(&c));
    NUTS_PASS(nng_setopt_bool(c, "pair1_test_inject_header", true));
    NUTS_PASS(nng_setopt_ms(s, NNG_OPT_RECVTIMEO, 100));
    NUTS_PASS(nng_setopt_ms(s, NNG_OPT_SENDTIMEO, 200));
 
    NUTS_MARRY(c, s);
 
    // ridiculous hop count
    NUTS_PASS(nng_msg_alloc(&m, 0));
    NUTS_PASS(nng_msg_header_append_u32(m, 0x1000));
    NUTS_PASS(nng_sendmsg(c, m, 0));
    NUTS_FAIL(nng_recvmsg(s, &m, 0), NNG_ETIMEDOUT);
 
    NUTS_CLOSE(c);
    NUTS_CLOSE(s);
}
 
static void
test_pair1_no_context(void)
{
    nng_socket s;
    nng_ctx    ctx;
 
    NUTS_PASS(nng_pair1_open(&s));
    NUTS_FAIL(nng_ctx_open(&ctx, s), NNG_ENOTSUP);
    NUTS_CLOSE(s);
}
 
static void
test_pair1_send_buffer(void)
{
    nng_socket s;
    int        v;
    bool       b;
    size_t     sz;
 
    NUTS_PASS(nng_pair1_open(&s));
    NUTS_PASS(nng_getopt_int(s, NNG_OPT_SENDBUF, &v));
    NUTS_TRUE(v == 0);
    NUTS_FAIL(nng_getopt_bool(s, NNG_OPT_SENDBUF, &b), NNG_EBADTYPE);
    sz = 1;
    NUTS_FAIL(nng_getopt(s, NNG_OPT_SENDBUF, &b, &sz), NNG_EINVAL);
    NUTS_FAIL(nng_setopt_int(s, NNG_OPT_SENDBUF, -1), NNG_EINVAL);
    NUTS_FAIL(nng_setopt_int(s, NNG_OPT_SENDBUF, 100000), NNG_EINVAL);
    NUTS_FAIL(nng_setopt_bool(s, NNG_OPT_SENDBUF, false), NNG_EBADTYPE);
    NUTS_FAIL(nng_setopt(s, NNG_OPT_SENDBUF, &b, 1), NNG_EINVAL);
    NUTS_PASS(nng_setopt_int(s, NNG_OPT_SENDBUF, 100));
    NUTS_PASS(nng_getopt_int(s, NNG_OPT_SENDBUF, &v));
    NUTS_TRUE(v == 100);
    NUTS_CLOSE(s);
}
 
static void
test_pair1_recv_buffer(void)
{
    nng_socket s;
    int        v;
    bool       b;
    size_t     sz;
 
    NUTS_PASS(nng_pair1_open(&s));
    NUTS_PASS(nng_getopt_int(s, NNG_OPT_RECVBUF, &v));
    NUTS_TRUE(v == 0);
    NUTS_FAIL(nng_getopt_bool(s, NNG_OPT_RECVBUF, &b), NNG_EBADTYPE);
    sz = 1;
    NUTS_FAIL(nng_getopt(s, NNG_OPT_RECVBUF, &b, &sz), NNG_EINVAL);
    NUTS_FAIL(nng_setopt_int(s, NNG_OPT_RECVBUF, -1), NNG_EINVAL);
    NUTS_FAIL(nng_setopt_int(s, NNG_OPT_RECVBUF, 100000), NNG_EINVAL);
    NUTS_FAIL(nng_setopt_bool(s, NNG_OPT_RECVBUF, false), NNG_EBADTYPE);
    NUTS_FAIL(nng_setopt(s, NNG_OPT_RECVBUF, &b, 1), NNG_EINVAL);
    NUTS_PASS(nng_setopt_int(s, NNG_OPT_RECVBUF, 100));
    NUTS_PASS(nng_getopt_int(s, NNG_OPT_RECVBUF, &v));
    NUTS_TRUE(v == 100);
    NUTS_CLOSE(s);
}
 
static void
test_pair1_poll_readable(void)
{
    int        fd;
    nng_socket s1;
    nng_socket s2;
 
    NUTS_PASS(nng_pair1_open(&s1));
    NUTS_PASS(nng_pair1_open(&s2));
    NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_RECVTIMEO, 1000));
    NUTS_PASS(nng_socket_set_ms(s2, NNG_OPT_SENDTIMEO, 1000));
    NUTS_PASS(nng_socket_get_int(s1, NNG_OPT_RECVFD, &fd));
    NUTS_TRUE(fd >= 0);
 
    // Not readable if not connected!
    NUTS_TRUE(nuts_poll_fd(fd) == false);
 
    // Even after connect (no message yet)
    NUTS_MARRY(s1, s2);
    NUTS_TRUE(nuts_poll_fd(fd) == false);
 
    // But once we send messages, it is.
    // We have to send a request, in order to send a reply.
    NUTS_SEND(s2, "abc");
    NUTS_SLEEP(100);
    NUTS_TRUE(nuts_poll_fd(fd));
 
    // and receiving makes it no longer ready
    NUTS_RECV(s1, "abc");
    NUTS_TRUE(nuts_poll_fd(fd) == false);
 
    // also let's confirm handling when we shrink the buffer size.
    NUTS_PASS(nng_socket_set_int(s1, NNG_OPT_RECVBUF, 1));
    NUTS_SEND(s2, "def");
    NUTS_SLEEP(100);
    NUTS_TRUE(nuts_poll_fd(fd));
    NUTS_PASS(nng_socket_set_int(s1, NNG_OPT_RECVBUF, 0));
    NUTS_TRUE(nuts_poll_fd(fd) == false);
    // growing doesn't magically make it readable either
    NUTS_PASS(nng_socket_set_int(s1, NNG_OPT_RECVBUF, 10));
    NUTS_TRUE(nuts_poll_fd(fd) == false);
 
    NUTS_CLOSE(s1);
    NUTS_CLOSE(s2);
}
 
static void
test_pair1_poll_writable(void)
{
    int        fd;
    nng_socket s1;
    nng_socket s2;
 
    NUTS_PASS(nng_pair1_open(&s1));
    NUTS_PASS(nng_pair1_open(&s2));
    NUTS_PASS(nng_socket_set_ms(s1, NNG_OPT_RECVTIMEO, 1000));
    NUTS_PASS(nng_socket_set_ms(s2, NNG_OPT_SENDTIMEO, 1000));
    NUTS_PASS(nng_socket_get_int(s1, NNG_OPT_SENDFD, &fd));
    NUTS_TRUE(fd >= 0);
 
    // Not writable if not connected!
    NUTS_TRUE(nuts_poll_fd(fd) == false);
 
    // But after connect, we can.
    NUTS_MARRY(s1, s2);
    NUTS_TRUE(nuts_poll_fd(fd));
 
    // We are unbuffered.
    NUTS_SEND(s1, "abc"); // first one in the receiver
    NUTS_SEND(s1, "def"); // second one on the sending pipe
    NUTS_TRUE(nuts_poll_fd(fd) == false);
 
    // and receiving makes it ready
    NUTS_RECV(s2, "abc");
    NUTS_SLEEP(100); // time for the sender to complete
    NUTS_TRUE(nuts_poll_fd(fd));
 
    // close the peer for now.
    NUTS_CLOSE(s2);
    NUTS_SLEEP(100);
 
    // resize up, makes us writable.
    NUTS_TRUE(nuts_poll_fd(fd) == false);
    NUTS_PASS(nng_socket_set_int(s1, NNG_OPT_SENDBUF, 1));
    NUTS_TRUE(nuts_poll_fd(fd));
    // resize down and we aren't anymore.
    NUTS_PASS(nng_socket_set_int(s1, NNG_OPT_SENDBUF, 0));
    NUTS_TRUE(nuts_poll_fd(fd) == false);
 
    NUTS_CLOSE(s1);
}
 
NUTS_TESTS = {
    { "pair1 mono identity", test_mono_identity },
    { "pair1 mono cooked", test_mono_cooked },
    { "pair1 mono faithful", test_mono_faithful },
    { "pair1 mono back pressure", test_mono_back_pressure },
    { "pair1 send no peer", test_send_no_peer },
    { "pair1 mono raw exchange", test_mono_raw_exchange },
    { "pair1 mono raw header", test_mono_raw_header },
    { "pair1 send closed aio", test_pair1_send_closed_aio },
    { "pair1 raw", test_pair1_raw },
    { "pair1 ttl", test_pair1_ttl },
    { "pair1 validate peer", test_pair1_validate_peer },
    { "pair1 recv no header", test_pair1_recv_no_header },
    { "pair1 recv garbage", test_pair1_recv_garbage },
    { "pair1 no context", test_pair1_no_context },
    { "pair1 send buffer", test_pair1_send_buffer },
    { "pair1 recv buffer", test_pair1_recv_buffer },
    { "pair1 poll readable", test_pair1_poll_readable },
    { "pair1 poll writable", test_pair1_poll_writable },
 
    { NULL, NULL },
};