Make sure we call get_max_records() in the record layer code
[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 static int random_data(const uint32_t *key, uint8_t *data, size_t data_len, size_t offset)
455 {
456     int ret = 0, outl;
457     EVP_CIPHER_CTX *ctx = NULL;
458     EVP_CIPHER *cipher = NULL;
459     static const uint8_t zeroes[2048];
460     uint32_t counter[4] = {0};
461
462     counter[0] = (uint32_t)offset;
463
464     ctx = EVP_CIPHER_CTX_new();
465     if (ctx == NULL)
466         goto err;
467
468     cipher = EVP_CIPHER_fetch(NULL, "ChaCha20", NULL);
469     if (cipher == NULL)
470         goto err;
471
472     if (EVP_EncryptInit_ex2(ctx, cipher, (uint8_t *)key, (uint8_t *)counter, NULL) == 0)
473         goto err;
474
475     while (data_len > 0) {
476         outl = data_len > sizeof(zeroes) ? (int)sizeof(zeroes) : (int)data_len;
477         if (EVP_EncryptUpdate(ctx, data, &outl, zeroes, outl) != 1)
478             goto err;
479
480         data     += outl;
481         data_len -= outl;
482     }
483
484     ret = 1;
485 err:
486     EVP_CIPHER_CTX_free(ctx);
487     EVP_CIPHER_free(cipher);
488     return ret;
489 }
490
491 static int test_bio_dgram_pair(void)
492 {
493     int testresult = 0, blen, mtu1, mtu2, r;
494     BIO *bio1 = NULL, *bio2 = NULL;
495     uint8_t scratch[2048 + 4], scratch2[2048];
496     uint32_t key[8];
497     size_t i, num_dgram, num_processed = 0;
498     BIO_MSG msgs[2] = {0}, rmsgs[2] = {0};
499     BIO_ADDR *addr1 = NULL, *addr2 = NULL, *addr3 = NULL, *addr4 = NULL;
500     struct in_addr in_local;
501     size_t total = 0;
502     const uint32_t ref_caps = BIO_DGRAM_CAP_HANDLES_SRC_ADDR
503                             | BIO_DGRAM_CAP_HANDLES_DST_ADDR
504                             | BIO_DGRAM_CAP_PROVIDES_SRC_ADDR
505                             | BIO_DGRAM_CAP_PROVIDES_DST_ADDR;
506
507     in_local.s_addr = ntohl(0x7f000001);
508
509     for (i = 0; i < OSSL_NELEM(key); ++i)
510         key[i] = test_random();
511
512     if (!TEST_int_eq(BIO_new_bio_dgram_pair(&bio1, 0, &bio2, 0), 1))
513         goto err;
514
515     mtu1 = BIO_dgram_get_mtu(bio1);
516     if (!TEST_int_ge(mtu1, 1280))
517         goto err;
518
519     mtu2 = BIO_dgram_get_mtu(bio2);
520     if (!TEST_int_ge(mtu2, 1280))
521         goto err;
522
523     if (!TEST_int_eq(mtu1, mtu2))
524         goto err;
525
526     if (!TEST_int_le(mtu1, sizeof(scratch) - 4))
527         goto err;
528
529     for (i = 0;; ++i) {
530         if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
531             goto err;
532
533         blen = ((*(uint32_t*)scratch) % mtu1) + 1;
534         r = BIO_write(bio1, scratch + 4, blen);
535         if (r == -1)
536             break;
537
538         if (!TEST_int_eq(r, blen))
539             goto err;
540
541         total += blen;
542         if (!TEST_size_t_lt(total, 1 * 1024 * 1024))
543             goto err;
544     }
545
546     /*
547      * Should be able to fit at least 9 datagrams in default write buffer size
548      * in worst case
549      */
550     if (!TEST_int_ge(i, 9))
551         goto err;
552
553     /* Check we read back the same data */
554     num_dgram = i;
555     for (i = 0; i < num_dgram; ++i) {
556         if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), i), 1))
557             goto err;
558
559         blen = ((*(uint32_t*)scratch) % mtu1) + 1;
560         r = BIO_read(bio2, scratch2, sizeof(scratch2));
561         if (!TEST_int_eq(r, blen))
562             goto err;
563
564         if (!TEST_mem_eq(scratch + 4, blen, scratch2, blen))
565             goto err;
566     }
567
568     /* Should now be out of data */
569     if (!TEST_int_eq(BIO_read(bio2, scratch2, sizeof(scratch2)), -1))
570         goto err;
571
572     /* sendmmsg/recvmmsg */
573     if (!TEST_int_eq(random_data(key, scratch, sizeof(scratch), 0), 1))
574         goto err;
575
576     msgs[0].data     = scratch;
577     msgs[0].data_len = 19;
578     msgs[1].data     = scratch + 19;
579     msgs[1].data_len = 46;
580
581     if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs), 0,
582                                 &num_processed))
583         || !TEST_size_t_eq(num_processed, 2))
584         goto err;
585
586     rmsgs[0].data       = scratch2;
587     rmsgs[0].data_len   = 64;
588     rmsgs[1].data       = scratch2 + 64;
589     rmsgs[1].data_len   = 64;
590     if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
591                                 &num_processed))
592         || !TEST_size_t_eq(num_processed, 2))
593         goto err;
594
595     if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, scratch, 19))
596         goto err;
597
598     if (!TEST_mem_eq(rmsgs[1].data, rmsgs[1].data_len, scratch + 19, 46))
599         goto err;
600
601     /* sendmmsg/recvmmsg with peer */
602     addr1 = BIO_ADDR_new();
603     if (!TEST_ptr(addr1))
604         goto err;
605
606     if (!TEST_int_eq(BIO_ADDR_rawmake(addr1, AF_INET, &in_local,
607                                       sizeof(in_local), 1234), 1))
608         goto err;
609
610     addr2 = BIO_ADDR_new();
611     if (!TEST_ptr(addr2))
612         goto err;
613
614     if (!TEST_int_eq(BIO_ADDR_rawmake(addr2, AF_INET, &in_local,
615                                       sizeof(in_local), 2345), 1))
616         goto err;
617
618     addr3 = BIO_ADDR_new();
619     if (!TEST_ptr(addr3))
620         goto err;
621
622     addr4 = BIO_ADDR_new();
623     if (!TEST_ptr(addr4))
624         goto err;
625
626     msgs[0].peer = addr1;
627
628     /* fails due to lack of caps on peer */
629     if (!TEST_false(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), OSSL_NELEM(msgs),
630                                  0, &num_processed))
631         || !TEST_size_t_eq(num_processed, 0))
632         goto err;
633
634     if (!TEST_int_eq(BIO_dgram_set_caps(bio2, ref_caps), 1))
635         goto err;
636
637     if (!TEST_int_eq(BIO_dgram_get_caps(bio2), ref_caps))
638         goto err;
639
640     if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio1), ref_caps))
641         goto err;
642
643     if (!TEST_int_eq(BIO_dgram_get_effective_caps(bio2), 0))
644         goto err;
645
646     if (!TEST_int_eq(BIO_dgram_set_caps(bio1, ref_caps), 1))
647         goto err;
648
649     /* succeeds with cap now available */
650     if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
651         || !TEST_size_t_eq(num_processed, 1))
652         goto err;
653
654     /* enable local addr support */
655     if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio2, 1), 1))
656         goto err;
657
658     rmsgs[0].data       = scratch2;
659     rmsgs[0].data_len   = 64;
660     rmsgs[0].peer       = addr3;
661     rmsgs[0].local      = addr4;
662     if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0,
663                                 &num_processed))
664         || !TEST_size_t_eq(num_processed, 1))
665         goto err;
666
667     if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len, msgs[0].data, 19))
668         goto err;
669
670     /* We didn't set the source address so this should be zero */
671     if (!TEST_int_eq(BIO_ADDR_family(addr3), 0))
672         goto err;
673
674     if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
675         goto err;
676
677     if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
678         goto err;
679
680     /* test source address */
681     msgs[0].local = addr2;
682
683     if (!TEST_int_eq(BIO_dgram_set_local_addr_enable(bio1, 1), 1))
684         goto err;
685
686     if (!TEST_true(BIO_sendmmsg(bio1, msgs, sizeof(BIO_MSG), 1, 0, &num_processed))
687         || !TEST_size_t_eq(num_processed, 1))
688         goto err;
689
690     rmsgs[0].data       = scratch2;
691     rmsgs[0].data_len   = 64;
692     if (!TEST_true(BIO_recvmmsg(bio2, rmsgs, sizeof(BIO_MSG), OSSL_NELEM(rmsgs), 0, &num_processed))
693         || !TEST_size_t_eq(num_processed, 1))
694         goto err;
695
696     if (!TEST_mem_eq(rmsgs[0].data, rmsgs[0].data_len,
697                      msgs[0].data, msgs[0].data_len))
698         goto err;
699
700     if (!TEST_int_eq(BIO_ADDR_family(addr3), AF_INET))
701         goto err;
702
703     if (!TEST_int_eq(BIO_ADDR_rawport(addr3), 2345))
704         goto err;
705
706     if (!TEST_int_eq(BIO_ADDR_family(addr4), AF_INET))
707         goto err;
708
709     if (!TEST_int_eq(BIO_ADDR_rawport(addr4), 1234))
710         goto err;
711
712     /* test truncation, pending */
713     r = BIO_write(bio1, scratch, 64);
714     if (!TEST_int_eq(r, 64))
715         goto err;
716
717     memset(scratch2, 0, 64);
718     if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 1), 1))
719         goto err;
720
721     if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), -1))
722         goto err;
723
724     if (!TEST_int_eq(BIO_pending(bio2), 64))
725         goto err;
726
727     if (!TEST_int_eq(BIO_dgram_set_no_trunc(bio2, 0), 1))
728         goto err;
729
730     if (!TEST_int_eq(BIO_read(bio2, scratch2, 32), 32))
731         goto err;
732
733     if (!TEST_mem_eq(scratch, 32, scratch2, 32))
734         goto err;
735
736     testresult = 1;
737 err:
738     BIO_free(bio1);
739     BIO_free(bio2);
740     BIO_ADDR_free(addr1);
741     BIO_ADDR_free(addr2);
742     BIO_ADDR_free(addr3);
743     BIO_ADDR_free(addr4);
744     return testresult;
745 }
746
747 #endif /* !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK) */
748
749 int setup_tests(void)
750 {
751     if (!test_skip_common_options()) {
752         TEST_error("Error parsing test options\n");
753         return 0;
754     }
755
756 #if !defined(OPENSSL_NO_DGRAM) && !defined(OPENSSL_NO_SOCK)
757     ADD_ALL_TESTS(test_bio_dgram, OSSL_NELEM(bio_dgram_cases));
758     ADD_TEST(test_bio_dgram_pair);
759 #endif
760
761     return 1;
762 }