Add a test for public variants of bn2bin()
[openssl.git] / test / bio_dgram_test.c
1 /*
2  * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11 #include <openssl/bio.h>
12 #include <openssl/rand.h>
13 #include "testutil.h"
14 #include "internal/sockets.h"
15
16 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
17
18 static int compare_addr(const BIO_ADDR *a, const BIO_ADDR *b)
19 {
20     struct in_addr xa, xb;
21 #if OPENSSL_USE_IPV6
22     struct in6_addr xa6, xb6;
23 #endif
24     void *pa, *pb;
25     size_t slen, tmplen;
26
27     if (BIO_ADDR_family(a) != BIO_ADDR_family(b))
28         return 0;
29
30     if (BIO_ADDR_family(a) == AF_INET) {
31         pa = &xa;
32         pb = &xb;
33         slen = sizeof(xa);
34     }
35 #if OPENSSL_USE_IPV6
36     else if (BIO_ADDR_family(a) == AF_INET6) {
37         pa = &xa6;
38         pb = &xb6;
39         slen = sizeof(xa6);
40     }
41 #endif
42     else {
43         return 0;
44     }
45
46     tmplen = slen;
47     if (!TEST_int_eq(BIO_ADDR_rawaddress(a, pa, &tmplen), 1))
48         return 0;
49
50     tmplen = slen;
51     if (!TEST_int_eq(BIO_ADDR_rawaddress(b, pb, &tmplen), 1))
52         return 0;
53
54     if (!TEST_mem_eq(pa, slen, pb, slen))
55         return 0;
56
57     if (!TEST_int_eq(BIO_ADDR_rawport(a), BIO_ADDR_rawport(b)))
58         return 0;
59
60     return 1;
61 }
62
63 static int do_sendmmsg(BIO *b, BIO_MSG *msg,
64                        size_t num_msg, uint64_t flags,
65                        size_t *num_processed)
66 {
67     size_t done;
68
69     for (done = 0; done < num_msg; ) {
70         if (!BIO_sendmmsg(b, msg + done, sizeof(BIO_MSG),
71                           num_msg - done, flags, num_processed))
72             return 0;
73
74         done += *num_processed;
75     }
76
77     *num_processed = done;
78     return 1;
79 }
80
81 static int do_recvmmsg(BIO *b, BIO_MSG *msg,
82                        size_t num_msg, uint64_t flags,
83                        size_t *num_processed)
84 {
85     size_t done;
86
87     for (done = 0; done < num_msg; ) {
88         if (!BIO_recvmmsg(b, msg + done, sizeof(BIO_MSG),
89                           num_msg - done, flags, num_processed))
90             return 0;
91
92         done += *num_processed;
93     }
94
95     *num_processed = done;
96     return 1;
97 }
98
99 static int test_bio_dgram_impl(int af, int use_local)
100 {
101     int testresult = 0;
102     BIO *b1 = NULL, *b2 = NULL;
103     int fd1 = -1, fd2 = -1;
104     BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL,
105              *addr5 = NULL, *addr6 = NULL;
106     struct in_addr ina;
107 #if OPENSSL_USE_IPV6
108     struct in6_addr ina6;
109 #endif
110     void *pina;
111     size_t inal, i;
112     union BIO_sock_info_u info1 = {0}, info2 = {0};
113     char rx_buf[128], rx_buf2[128];
114     BIO_MSG tx_msg[128], rx_msg[128];
115     char tx_buf[128];
116     size_t num_processed = 0;
117
118     if (af == AF_INET) {
119         TEST_info("# Testing with AF_INET, local=%d\n", use_local);
120         pina = &ina;
121         inal = sizeof(ina);
122     }
123 #if OPENSSL_USE_IPV6
124     else if (af == AF_INET6) {
125         TEST_info("# Testing with AF_INET6, local=%d\n", use_local);
126         pina = &ina6;
127         inal = sizeof(ina6);
128     }
129 #endif
130     else {
131         goto err;
132     }
133
134     memset(pina, 0, inal);
135     ina.s_addr = htonl(0x7f000001UL);
136 #if OPENSSL_USE_IPV6
137     ina6.s6_addr[15] = 1;
138 #endif
139
140     addr1 = BIO_ADDR_new();
141     if (!TEST_ptr(addr1))
142         goto err;
143
144     addr2 = BIO_ADDR_new();
145     if (!TEST_ptr(addr2))
146         goto err;
147
148     addr3 = BIO_ADDR_new();
149     if (!TEST_ptr(addr3))
150         goto err;
151
152     addr4 = BIO_ADDR_new();
153     if (!TEST_ptr(addr4))
154         goto err;
155
156     addr5 = BIO_ADDR_new();
157     if (!TEST_ptr(addr5))
158         goto err;
159
160     addr6 = BIO_ADDR_new();
161     if (!TEST_ptr(addr6))
162         goto err;
163
164     if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, af, pina, inal, 0), 1))
165         goto err;
166
167     if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, af, pina, inal, 0), 1))
168         goto err;
169
170     fd1 = BIO_socket(af, SOCK_DGRAM, IPPROTO_UDP, 0);
171     if (!TEST_int_ge(fd1, 0))
172         goto err;
173
174     fd2 = BIO_socket(af, SOCK_DGRAM, IPPROTO_UDP, 0);
175     if (!TEST_int_ge(fd2, 0))
176         goto err;
177
178     if (!TEST_int_gt(BIO_bind(fd1, addr1, 0), 0))
179         goto err;
180
181     if (!TEST_int_gt(BIO_bind(fd2, addr2, 0), 0))
182         goto err;
183
184     info1.addr = addr1;
185     if (!TEST_int_gt(BIO_sock_info(fd1, BIO_SOCK_INFO_ADDRESS, &info1), 0))
186         goto err;
187
188     info2.addr = addr2;
189     if (!TEST_int_gt(BIO_sock_info(fd2, BIO_SOCK_INFO_ADDRESS, &info2), 0))
190         goto err;
191
192     if (!TEST_int_gt(BIO_ADDR_rawport(addr1), 0))
193         goto err;
194
195     if (!TEST_int_gt(BIO_ADDR_rawport(addr2), 0))
196         goto err;
197
198     b1 = BIO_new_dgram(fd1, 0);
199     if (!TEST_ptr(b1))
200         goto err;
201
202     b2 = BIO_new_dgram(fd2, 0);
203     if (!TEST_ptr(b2))
204         goto err;
205
206     if (!TEST_int_gt(BIO_dgram_set_peer(b1, addr2), 0))
207         goto err;
208
209     if (!TEST_int_gt(BIO_write(b1, "hello", 5), 0))
210         goto err;
211
212     /* Receiving automatically sets peer as source addr */
213     if (!TEST_int_eq(BIO_read(b2, rx_buf, sizeof(rx_buf)), 5))
214         goto err;
215
216     if (!TEST_mem_eq(rx_buf, 5, "hello", 5))
217         goto err;
218
219     if (!TEST_int_gt(BIO_dgram_get_peer(b2, addr3), 0))
220         goto err;
221
222     if (!TEST_int_eq(compare_addr(addr3, addr1), 1))
223         goto err;
224
225     /* Clear peer */
226     if (!TEST_int_gt(BIO_ADDR_rawmake(addr3, af, pina, inal, 0), 0))
227         goto err;
228
229     if (!TEST_int_gt(BIO_dgram_set_peer(b1, addr3), 0))
230         goto err;
231
232     if (!TEST_int_gt(BIO_dgram_set_peer(b2, addr3), 0))
233         goto err;
234
235     /* Now test using sendmmsg/recvmmsg with no peer set */
236     tx_msg[0].data      = "apple";
237     tx_msg[0].data_len  = 5;
238     tx_msg[0].peer      = NULL;
239     tx_msg[0].local     = NULL;
240     tx_msg[0].flags     = 0;
241
242     tx_msg[1].data      = "orange";
243     tx_msg[1].data_len  = 6;
244     tx_msg[1].peer      = NULL;
245     tx_msg[1].local     = NULL;
246     tx_msg[1].flags     = 0;
247
248     /* First effort should fail due to missing destination address */
249     if (!TEST_false(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
250         || !TEST_size_t_eq(num_processed, 0))
251         goto err;
252
253     /*
254      * Second effort should fail due to local being requested
255      * when not enabled
256      */
257     tx_msg[0].peer  = addr2;
258     tx_msg[0].local = addr1;
259     tx_msg[1].peer  = addr2;
260     tx_msg[1].local = addr1;
261     if (!TEST_false(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed)
262         || !TEST_size_t_eq(num_processed, 0)))
263         goto err;
264
265     /* Enable local if we are using it */
266     if (BIO_dgram_get_local_addr_cap(b1) > 0 && use_local) {
267         if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(b1, 1), 1))
268             goto err;
269     } else {
270         tx_msg[0].local = NULL;
271         tx_msg[1].local = NULL;
272         use_local = 0;
273     }
274
275     /* Third effort should succeed */
276     if (!TEST_true(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
277         || !TEST_size_t_eq(num_processed, 2))
278         goto err;
279
280     /* Now try receiving */
281     rx_msg[0].data      = rx_buf;
282     rx_msg[0].data_len  = sizeof(rx_buf);
283     rx_msg[0].peer      = addr3;
284     rx_msg[0].local     = addr4;
285     rx_msg[0].flags     = (1UL<<31); /* undefined flag, should be erased */
286     memset(rx_buf, 0, sizeof(rx_buf));
287
288     rx_msg[1].data      = rx_buf2;
289     rx_msg[1].data_len  = sizeof(rx_buf2);
290     rx_msg[1].peer      = addr5;
291     rx_msg[1].local     = addr6;
292     rx_msg[1].flags     = (1UL<<31); /* undefined flag, should be erased */
293     memset(rx_buf2, 0, sizeof(rx_buf2));
294
295     /*
296      * Should fail at first due to local being requested when not
297      * enabled
298      */
299     if (!TEST_false(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
300         || !TEST_size_t_eq(num_processed, 0))
301         goto err;
302
303     /* Fields have not been modified */
304     if (!TEST_int_eq((int)rx_msg[0].data_len, sizeof(rx_buf)))
305         goto err;
306
307     if (!TEST_int_eq((int)rx_msg[1].data_len, sizeof(rx_buf2)))
308         goto err;
309
310     if (!TEST_ulong_eq((unsigned long)rx_msg[0].flags, 1UL<<31))
311         goto err;
312
313     if (!TEST_ulong_eq((unsigned long)rx_msg[1].flags, 1UL<<31))
314         goto err;
315
316     /* Enable local if we are using it */
317     if (BIO_dgram_get_local_addr_cap(b2) > 0 && use_local) {
318         if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(b2, 1), 1))
319             goto err;
320     } else {
321         rx_msg[0].local = NULL;
322         rx_msg[1].local = NULL;
323         use_local = 0;
324     }
325
326     /* Do the receive. */
327     if (!TEST_true(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
328         || !TEST_size_t_eq(num_processed, 2))
329         goto err;
330
331     /* data_len should have been updated correctly */
332     if (!TEST_int_eq((int)rx_msg[0].data_len, 5))
333         goto err;
334
335     if (!TEST_int_eq((int)rx_msg[1].data_len, 6))
336         goto err;
337
338     /* flags should have been zeroed */
339     if (!TEST_int_eq((int)rx_msg[0].flags, 0))
340         goto err;
341
342     if (!TEST_int_eq((int)rx_msg[1].flags, 0))
343         goto err;
344
345     /* peer address should match expected */
346     if (!TEST_int_eq(compare_addr(addr3, addr1), 1))
347         goto err;
348
349     if (!TEST_int_eq(compare_addr(addr5, addr1), 1))
350         goto err;
351
352     /*
353      * Do not test local address yet as some platforms do not reliably return
354      * local addresses for messages queued for RX before local address support
355      * was enabled. Instead, send some new messages and test they're received
356      * with the correct local addresses.
357      */
358     if (!TEST_true(do_sendmmsg(b1, tx_msg, 2, 0, &num_processed))
359         || !TEST_size_t_eq(num_processed, 2))
360         goto err;
361
362     /* Receive the messages. */
363     rx_msg[0].data_len = sizeof(rx_buf);
364     rx_msg[1].data_len = sizeof(rx_buf2);
365
366     if (!TEST_true(do_recvmmsg(b2, rx_msg, 2, 0, &num_processed))
367         || !TEST_size_t_eq(num_processed, 2))
368         goto err;
369
370     if (rx_msg[0].local != NULL) {
371         /* If we are using local, it should match expected */
372         if (!TEST_int_eq(compare_addr(addr4, addr2), 1))
373             goto err;
374
375         if (!TEST_int_eq(compare_addr(addr6, addr2), 1))
376             goto err;
377     }
378
379     /*
380      * Try sending more than can be handled in one sendmmsg call (when using the
381      * sendmmsg implementation)
382      */
383     for (i = 0; i < OSSL_NELEM(tx_msg); ++i) {
384         tx_buf[i] = (char)i;
385         tx_msg[i].data      = tx_buf + i;
386         tx_msg[i].data_len  = 1;
387         tx_msg[i].peer      = addr2;
388         tx_msg[i].local     = use_local ? addr1 : NULL;
389         tx_msg[i].flags     = 0;
390     }
391     if (!TEST_true(do_sendmmsg(b1, tx_msg, OSSL_NELEM(tx_msg), 0, &num_processed))
392         || !TEST_size_t_eq(num_processed, OSSL_NELEM(tx_msg)))
393         goto err;
394
395     /*
396      * Try receiving more than can be handled in one recvmmsg call (when using
397      * the recvmmsg implementation)
398      */
399     for (i = 0; i < OSSL_NELEM(rx_msg); ++i) {
400         rx_buf[i] = '\0';
401         rx_msg[i].data      = rx_buf + i;
402         rx_msg[i].data_len  = 1;
403         rx_msg[i].peer      = NULL;
404         rx_msg[i].local     = NULL;
405         rx_msg[i].flags     = 0;
406     }
407     if (!TEST_true(do_recvmmsg(b2, rx_msg, OSSL_NELEM(rx_msg), 0, &num_processed))
408         || !TEST_size_t_eq(num_processed, OSSL_NELEM(rx_msg)))
409         goto err;
410
411     if (!TEST_mem_eq(tx_buf, OSSL_NELEM(tx_msg), rx_buf, OSSL_NELEM(tx_msg)))
412         goto err;
413
414     testresult = 1;
415 err:
416     BIO_free(b1);
417     BIO_free(b2);
418     if (fd1 >= 0)
419         BIO_closesocket(fd1);
420     if (fd2 >= 0)
421         BIO_closesocket(fd2);
422     BIO_ADDR_free(addr1);
423     BIO_ADDR_free(addr2);
424     BIO_ADDR_free(addr3);
425     BIO_ADDR_free(addr4);
426     BIO_ADDR_free(addr5);
427     BIO_ADDR_free(addr6);
428     return testresult;
429 }
430
431 struct bio_dgram_case {
432     int af, local;
433 };
434
435 static const struct bio_dgram_case bio_dgram_cases[] = {
436     /* Test without local */
437     { AF_INET,  0 },
438 #if OPENSSL_USE_IPV6
439     { AF_INET6, 0 },
440 #endif
441     /* Test with local */
442     { AF_INET,  1 },
443 #if OPENSSL_USE_IPV6
444     { AF_INET6, 1 }
445 #endif
446 };
447
448 static int test_bio_dgram(int idx)
449 {
450     return test_bio_dgram_impl(bio_dgram_cases[idx].af,
451                                bio_dgram_cases[idx].local);
452 }
453
454 # if !defined(OPENSSL_NO_CHACHA)
455 static int random_data(const uint32_t *key, uint8_t *data, size_t data_len, size_t offset)
456 {
457     int ret = 0, outl;
458     EVP_CIPHER_CTX *ctx = NULL;
459     EVP_CIPHER *cipher = NULL;
460     static const uint8_t zeroes[2048];
461     uint32_t counter[4] = {0};
462
463     counter[0] = (uint32_t)offset;
464
465     ctx = EVP_CIPHER_CTX_new();
466     if (ctx == NULL)
467         goto err;
468
469     cipher = EVP_CIPHER_fetch(NULL, "ChaCha20", NULL);
470     if (cipher == NULL)
471         goto err;
472
473     if (EVP_EncryptInit_ex2(ctx, cipher, (uint8_t *)key, (uint8_t *)counter, NULL) == 0)
474         goto err;
475
476     while (data_len > 0) {
477         outl = data_len > sizeof(zeroes) ? (int)sizeof(zeroes) : (int)data_len;
478         if (EVP_EncryptUpdate(ctx, data, &outl, zeroes, outl) != 1)
479             goto err;
480
481         data     += outl;
482         data_len -= outl;
483     }
484
485     ret = 1;
486 err:
487     EVP_CIPHER_CTX_free(ctx);
488     EVP_CIPHER_free(cipher);
489     return ret;
490 }
491
492 static int test_bio_dgram_pair(void)
493 {
494     int testresult = 0, blen, mtu1, mtu2, r;
495     BIO *bio1 = NULL, *bio2 = NULL;
496     uint8_t scratch[2048 + 4], scratch2[2048];
497     uint32_t key[8];
498     size_t i, num_dgram, num_processed = 0;
499     BIO_MSG msgs[2], rmsgs[2];
500     BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL;
501     struct in_addr in_local;
502     size_t total = 0;
503     const uint32_t ref_caps = BIO_DGRAM_CAP_HANDLES_SRC_ADDR
504                             | BIO_DGRAM_CAP_HANDLES_DST_ADDR
505                             | BIO_DGRAM_CAP_PROVIDES_SRC_ADDR
506                             | BIO_DGRAM_CAP_PROVIDES_DST_ADDR;
507
508     memset(msgs, 0, sizeof(msgs));
509     memset(rmsgs, 0, sizeof(rmsgs));
510
511     in_local.s_addr = ntohl(0x7f000001);
512
513     for (i = 0; i < OSSL_NELEM(key); ++i)
514         key[i] = test_random();
515
516     if (!TEST_int_eq(BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0), 1))
517         goto err;
518
519     mtu1 = BIO_dgram_get_mtu(bio1);
520     if (!TEST_int_ge(mtu1, 1280))
521         goto err;
522
523     mtu2 = BIO_dgram_get_mtu(bio2);
524     if (!TEST_int_ge(mtu2, 1280))
525         goto err;
526
527     if (!TEST_int_eq(mtu1, mtu2))
528         goto err;
529
530     if (!TEST_int_le(mtu1, sizeof(scratch) - 4))
531         goto err;
532
533     for (i = 0;; ++i) {
534         if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
535             goto err;
536
537         blen = ((*(uint32_t*)scratch) % mtu1) + 1;
538         r = BIO_write(bio1, scratch + 4, blen);
539         if (r == -1)
540             break;
541
542         if (!TEST_int_eq(r, blen))
543             goto err;
544
545         total += blen;
546         if (!TEST_size_t_lt(total, 1 * 1024 * 1024))
547             goto err;
548     }
549
550     /*
551      * Should be able to fit at least 9 datagrams in default write buffer size
552      * in worst case
553      */
554     if (!TEST_int_ge(i, 9))
555         goto err;
556
557     /* Check we read back the same data */
558     num_dgram = i;
559     for (i = 0; i < num_dgram; ++i) {
560         if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
561             goto err;
562
563         blen = ((*(uint32_t*)scratch) % mtu1) + 1;
564         r = BIO_read(bio2, scratch2, sizeof(scratch2));
565         if (!TEST_int_eq(r, blen))
566             goto err;
567
568         if (!TEST_mem_eq(scratch + 4, blen, scratch2, blen))
569             goto err;
570     }
571
572     /* Should now be out of data */
573     if (!TEST_int_eq(BIO_read(bio2, scratch2, sizeof(scratch2)), -1))
574         goto err;
575
576     /* sendmmsg/recvmmsg */
577     if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), 0), 1))
578         goto err;
579
580     msgs[0].data     = scratch;
581     msgs[0].data_len = 19;
582     msgs[1].data     = scratch + 19;
583     msgs[1].data_len = 46;
584
585     if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs), 0,
586                                 &num_processed))
587         || !TEST_size_t_eq(num_processed, 2))
588         goto err;
589
590     rmsgs[0].data       = scratch2;
591     rmsgs[0].data_len   = 64;
592     rmsgs[1].data       = scratch2 + 64;
593     rmsgs[1].data_len   = 64;
594     if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
595                                 &num_processed))
596         || !TEST_size_t_eq(num_processed, 2))
597         goto err;
598
599     if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, scratch, 19))
600         goto err;
601
602     if (!TEST_mem_eq(rmsgs[1].data, rmsgs[1].data_len, scratch + 19, 46))
603         goto err;
604
605     /* sendmmsg/recvmmsg with peer */
606     addr1 = BIO_ADDR_new();
607     if (!TEST_ptr(addr1))
608         goto err;
609
610     if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, AF_INET, &in_local,
611                                       sizeof(in_local), 1234), 1))
612         goto err;
613
614     addr2 = BIO_ADDR_new();
615     if (!TEST_ptr(addr2))
616         goto err;
617
618     if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, AF_INET, &in_local,
619                                       sizeof(in_local), 2345), 1))
620         goto err;
621
622     addr3 = BIO_ADDR_new();
623     if (!TEST_ptr(addr3))
624         goto err;
625
626     addr4 = BIO_ADDR_new();
627     if (!TEST_ptr(addr4))
628         goto err;
629
630     msgs[0].peer = addr1;
631
632     /* fails due to lack of caps on peer */
633     if (!TEST_false(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs),
634                                  0, &num_processed))
635         || !TEST_size_t_eq(num_processed, 0))
636         goto err;
637
638     if (!TEST_int_eq(BIO_dgram_set_caps(bio2, ref_caps), 1))
639         goto err;
640
641     if (!TEST_int_eq(BIO_dgram_get_caps(bio2), ref_caps))
642         goto err;
643
644     if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio1), ref_caps))
645         goto err;
646
647     if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio2), 0))
648         goto err;
649
650     if (!TEST_int_eq(BIO_dgram_set_caps(bio1, ref_caps), 1))
651         goto err;
652
653     /* succeeds with cap now available */
654     if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
655         || !TEST_size_t_eq(num_processed, 1))
656         goto err;
657
658     /* enable local addr support */
659     if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio2, 1), 1))
660         goto err;
661
662     rmsgs[0].data       = scratch2;
663     rmsgs[0].data_len   = 64;
664     rmsgs[0].peer       = addr3;
665     rmsgs[0].local      = addr4;
666     if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
667                                 &num_processed))
668         || !TEST_size_t_eq(num_processed, 1))
669         goto err;
670
671     if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, msgs[0].data, 19))
672         goto err;
673
674     /* We didn't set the source address so this should be zero */
675     if (!TEST_int_eq(BIO_ADDR_family(addr3), 0))
676         goto err;
677
678     if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
679         goto err;
680
681     if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
682         goto err;
683
684     /* test source address */
685     msgs[0].local = addr2;
686
687     if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio1, 1), 1))
688         goto err;
689
690     if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
691         || !TEST_size_t_eq(num_processed, 1))
692         goto err;
693
694     rmsgs[0].data       = scratch2;
695     rmsgs[0].data_len   = 64;
696     if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0, &num_processed))
697         || !TEST_size_t_eq(num_processed, 1))
698         goto err;
699
700     if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len,
701                      msgs[0].data, msgs[0].data_len))
702         goto err;
703
704     if (!TEST_int_eq(BIO_ADDR_family(addr3), AF_INET))
705         goto err;
706
707     if (!TEST_int_eq(BIO_ADDR_rawport(addr3), 2345))
708         goto err;
709
710     if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
711         goto err;
712
713     if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
714         goto err;
715
716     /* test truncation, pending */
717     r = BIO_write(bio1, scratch, 64);
718     if (!TEST_int_eq(r, 64))
719         goto err;
720
721     memset(scratch2, 0, 64);
722     if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 1), 1))
723         goto err;
724
725     if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), -1))
726         goto err;
727
728     if (!TEST_int_eq(BIO_pending(bio2), 64))
729         goto err;
730
731     if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 0), 1))
732         goto err;
733
734     if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), 32))
735         goto err;
736
737     if (!TEST_mem_eq(scratch, 32, scratch2, 32))
738         goto err;
739
740     testresult = 1;
741 err:
742     BIO_free(bio1);
743     BIO_free(bio2);
744     BIO_ADDR_free(addr1);
745     BIO_ADDR_free(addr2);
746     BIO_ADDR_free(addr3);
747     BIO_ADDR_free(addr4);
748     return testresult;
749 }
750 # endif /* !defined(OPENSSL_NO_CHACHA) */
751 #endif /* !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK) */
752
753 int setup_tests(void)
754 {
755     if (!test_skip_common_options()) {
756         TEST_error("Error parsing test options\n");
757         return 0;
758     }
759
760 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
761     ADD_ALL_TESTS(test_bio_dgram, OSSL_NELEM(bio_dgram_cases));
762 # if !defined(OPENSSL_NO_CHACHA)
763     ADD_TEST(test_bio_dgram_pair);
764 # endif
765 #endif
766
767     return 1;
768 }