bio_addr_test.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright 2023 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. #include <openssl/bio.h>
  10. #include "internal/e_os.h"
  11. #include "internal/sockets.h"
  12. #include "testutil.h"
  13. static int families[] = {
  14. AF_INET,
  15. #if OPENSSL_USE_IPV6
  16. AF_INET6,
  17. #endif
  18. #ifndef OPENSSL_NO_UNIX_SOCK
  19. AF_UNIX
  20. #endif
  21. };
  22. static BIO_ADDR *make_dummy_addr(int family)
  23. {
  24. BIO_ADDR *addr;
  25. union {
  26. struct sockaddr_in sin;
  27. #if OPENSSL_USE_IPV6
  28. struct sockaddr_in6 sin6;
  29. #endif
  30. #ifndef OPENSSL_NO_UNIX_SOCK
  31. struct sockaddr_un sunaddr;
  32. #endif
  33. } sa;
  34. void *where;
  35. size_t wherelen;
  36. /* Fill with a dummy address */
  37. switch(family) {
  38. case AF_INET:
  39. where = &(sa.sin.sin_addr);
  40. wherelen = sizeof(sa.sin.sin_addr);
  41. break;
  42. #if OPENSSL_USE_IPV6
  43. case AF_INET6:
  44. where = &(sa.sin6.sin6_addr);
  45. wherelen = sizeof(sa.sin6.sin6_addr);
  46. break;
  47. #endif
  48. #ifndef OPENSSL_NO_UNIX_SOCK
  49. case AF_UNIX:
  50. where = &(sa.sunaddr.sun_path);
  51. /* BIO_ADDR_rawmake needs an extra byte for a NUL-terminator*/
  52. wherelen = sizeof(sa.sunaddr.sun_path) - 1;
  53. break;
  54. #endif
  55. default:
  56. TEST_error("Unsupported address family");
  57. return 0;
  58. }
  59. /*
  60. * Could be any data, but we make it printable because BIO_ADDR_rawmake
  61. * expects the AF_UNIX address to be a string.
  62. */
  63. memset(where, 'a', wherelen);
  64. addr = BIO_ADDR_new();
  65. if (!TEST_ptr(addr))
  66. return NULL;
  67. if (!TEST_true(BIO_ADDR_rawmake(addr, family, where, wherelen, 1000))) {
  68. BIO_ADDR_free(addr);
  69. return NULL;
  70. }
  71. return addr;
  72. }
  73. static int bio_addr_is_eq(const BIO_ADDR *a, const BIO_ADDR *b)
  74. {
  75. unsigned char *adata = NULL, *bdata = NULL;
  76. size_t alen, blen;
  77. int ret = 0;
  78. /* True even if a and b are NULL */
  79. if (a == b)
  80. return 1;
  81. /* If one is NULL the other cannot be due to the test above */
  82. if (a == NULL || b == NULL)
  83. return 0;
  84. if (BIO_ADDR_family(a) != BIO_ADDR_family(b))
  85. return 0;
  86. /* Works even with AF_UNIX/AF_UNSPEC which just returns 0 */
  87. if (BIO_ADDR_rawport(a) != BIO_ADDR_rawport(b))
  88. return 0;
  89. if (!BIO_ADDR_rawaddress(a, NULL, &alen))
  90. return 0;
  91. if (!BIO_ADDR_rawaddress(b, NULL, &blen))
  92. goto err;
  93. if (alen != blen)
  94. return 0;
  95. if (alen == 0)
  96. return 1;
  97. adata = OPENSSL_malloc(alen);
  98. if (!TEST_ptr(adata)
  99. || !BIO_ADDR_rawaddress(a, adata, &alen))
  100. goto err;
  101. bdata = OPENSSL_malloc(blen);
  102. if (!TEST_ptr(bdata)
  103. || !BIO_ADDR_rawaddress(b, bdata, &blen))
  104. goto err;
  105. ret = (memcmp(adata, bdata, alen) == 0);
  106. err:
  107. OPENSSL_free(adata);
  108. OPENSSL_free(bdata);
  109. return ret;
  110. }
  111. static int test_bio_addr_copy_dup(int idx)
  112. {
  113. BIO_ADDR *src = NULL, *dst = NULL;
  114. int ret = 0;
  115. int docopy = idx & 1;
  116. idx >>= 1;
  117. src = make_dummy_addr(families[idx]);
  118. if (!TEST_ptr(src))
  119. return 0;
  120. if (docopy) {
  121. dst = BIO_ADDR_new();
  122. if (!TEST_ptr(dst))
  123. goto err;
  124. if (!TEST_true(BIO_ADDR_copy(dst, src)))
  125. goto err;
  126. } else {
  127. dst = BIO_ADDR_dup(src);
  128. if (!TEST_ptr(dst))
  129. goto err;
  130. }
  131. if (!TEST_true(bio_addr_is_eq(src, dst)))
  132. goto err;
  133. ret = 1;
  134. err:
  135. BIO_ADDR_free(src);
  136. BIO_ADDR_free(dst);
  137. return ret;
  138. }
  139. int setup_tests(void)
  140. {
  141. if (!test_skip_common_options()) {
  142. TEST_error("Error parsing test options\n");
  143. return 0;
  144. }
  145. ADD_ALL_TESTS(test_bio_addr_copy_dup, OSSL_NELEM(families) * 2);
  146. return 1;
  147. }