2 * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/bio.h>
11 #include "internal/e_os.h"
12 #include "internal/sockets.h"
15 static int families[] = {
20 #ifndef OPENSSL_NO_UNIX_SOCK
25 static BIO_ADDR *make_dummy_addr(int family)
29 struct sockaddr_in sin;
31 struct sockaddr_in6 sin6;
33 #ifndef OPENSSL_NO_UNIX_SOCK
34 struct sockaddr_un sunaddr;
40 /* Fill with a dummy address */
43 where = &(sa.sin.sin_addr);
44 wherelen = sizeof(sa.sin.sin_addr);
48 where = &(sa.sin6.sin6_addr);
49 wherelen = sizeof(sa.sin6.sin6_addr);
52 #ifndef OPENSSL_NO_UNIX_SOCK
54 where = &(sa.sunaddr.sun_path);
55 /* BIO_ADDR_rawmake needs an extra byte for a NUL-terminator*/
56 wherelen = sizeof(sa.sunaddr.sun_path) - 1;
60 TEST_error("Unsupported address family");
64 * Could be any data, but we make it printable because BIO_ADDR_rawmake
65 * expects the AF_UNIX address to be a string.
67 memset(where, 'a', wherelen);
69 addr = BIO_ADDR_new();
73 if (!TEST_true(BIO_ADDR_rawmake(addr, family, where, wherelen, 1000))) {
81 static int bio_addr_is_eq(const BIO_ADDR *a, const BIO_ADDR *b)
83 unsigned char *adata = NULL, *bdata = NULL;
87 /* True even if a and b are NULL */
91 /* If one is NULL the other cannot be due to the test above */
92 if (a == NULL || b == NULL)
95 if (BIO_ADDR_family(a) != BIO_ADDR_family(b))
98 /* Works even with AF_UNIX/AF_UNSPEC which just returns 0 */
99 if (BIO_ADDR_rawport(a) != BIO_ADDR_rawport(b))
102 if (!BIO_ADDR_rawaddress(a, NULL, &alen))
105 if (!BIO_ADDR_rawaddress(b, NULL, &blen))
114 adata = OPENSSL_malloc(alen);
116 || !BIO_ADDR_rawaddress(a, adata, &alen))
119 bdata = OPENSSL_malloc(blen);
121 || !BIO_ADDR_rawaddress(b, bdata, &blen))
124 ret = (memcmp(adata, bdata, alen) == 0);
132 static int test_bio_addr_copy_dup(int idx)
134 BIO_ADDR *src = NULL, *dst = NULL;
136 int docopy = idx & 1;
140 src = make_dummy_addr(families[idx]);
145 dst = BIO_ADDR_new();
149 if (!TEST_true(BIO_ADDR_copy(dst, src)))
152 dst = BIO_ADDR_dup(src);
157 if (!TEST_true(bio_addr_is_eq(src, dst)))
167 int setup_tests(void)
169 if (!test_skip_common_options()) {
170 TEST_error("Error parsing test options\n");
174 ADD_ALL_TESTS(test_bio_addr_copy_dup, OSSL_NELEM(families) * 2);