testsockets.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "fspr_network_io.h"
  17. #include "fspr_errno.h"
  18. #include "fspr_general.h"
  19. #include "fspr_lib.h"
  20. #include "testutil.h"
  21. #define STRLEN 21
  22. static void tcp_socket(abts_case *tc, void *data)
  23. {
  24. fspr_status_t rv;
  25. fspr_socket_t *sock = NULL;
  26. int type;
  27. rv = fspr_socket_create(&sock, APR_INET, SOCK_STREAM, 0, p);
  28. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  29. ABTS_PTR_NOTNULL(tc, sock);
  30. rv = fspr_socket_type_get(sock, &type);
  31. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  32. ABTS_INT_EQUAL(tc, SOCK_STREAM, type);
  33. fspr_socket_close(sock);
  34. }
  35. static void udp_socket(abts_case *tc, void *data)
  36. {
  37. fspr_status_t rv;
  38. fspr_socket_t *sock = NULL;
  39. int type;
  40. rv = fspr_socket_create(&sock, APR_INET, SOCK_DGRAM, 0, p);
  41. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  42. ABTS_PTR_NOTNULL(tc, sock);
  43. rv = fspr_socket_type_get(sock, &type);
  44. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  45. ABTS_INT_EQUAL(tc, SOCK_DGRAM, type);
  46. fspr_socket_close(sock);
  47. }
  48. /* On recent Linux systems, whilst IPv6 is always supported by glibc,
  49. * socket(AF_INET6, ...) calls will fail with EAFNOSUPPORT if the
  50. * "ipv6" kernel module is not loaded. */
  51. #ifdef EAFNOSUPPORT
  52. #define V6_NOT_ENABLED(e) ((e) == EAFNOSUPPORT)
  53. #else
  54. #define V6_NOT_ENABLED(e) (0)
  55. #endif
  56. static void tcp6_socket(abts_case *tc, void *data)
  57. {
  58. #if APR_HAVE_IPV6
  59. fspr_status_t rv;
  60. fspr_socket_t *sock = NULL;
  61. rv = fspr_socket_create(&sock, APR_INET6, SOCK_STREAM, 0, p);
  62. if (V6_NOT_ENABLED(rv)) {
  63. ABTS_NOT_IMPL(tc, "IPv6 not enabled");
  64. return;
  65. }
  66. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  67. ABTS_PTR_NOTNULL(tc, sock);
  68. fspr_socket_close(sock);
  69. #else
  70. ABTS_NOT_IMPL(tc, "IPv6");
  71. #endif
  72. }
  73. static void udp6_socket(abts_case *tc, void *data)
  74. {
  75. #if APR_HAVE_IPV6
  76. fspr_status_t rv;
  77. fspr_socket_t *sock = NULL;
  78. rv = fspr_socket_create(&sock, APR_INET6, SOCK_DGRAM, 0, p);
  79. if (V6_NOT_ENABLED(rv)) {
  80. ABTS_NOT_IMPL(tc, "IPv6 not enabled");
  81. return;
  82. }
  83. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  84. ABTS_PTR_NOTNULL(tc, sock);
  85. fspr_socket_close(sock);
  86. #else
  87. ABTS_NOT_IMPL(tc, "IPv6");
  88. #endif
  89. }
  90. static void sendto_receivefrom(abts_case *tc, void *data)
  91. {
  92. fspr_status_t rv;
  93. fspr_socket_t *sock = NULL;
  94. fspr_socket_t *sock2 = NULL;
  95. char sendbuf[STRLEN] = "APR_INET, SOCK_DGRAM";
  96. char recvbuf[80];
  97. char *ip_addr;
  98. fspr_port_t fromport;
  99. fspr_sockaddr_t *from;
  100. fspr_sockaddr_t *to;
  101. fspr_size_t len = 30;
  102. int family;
  103. const char *addr;
  104. #if APR_HAVE_IPV6
  105. family = APR_INET6;
  106. addr = "::1";
  107. rv = fspr_socket_create(&sock, family, SOCK_DGRAM, 0, p);
  108. if (V6_NOT_ENABLED(rv)) {
  109. #endif
  110. family = APR_INET;
  111. addr = "127.0.0.1";
  112. rv = fspr_socket_create(&sock, family, SOCK_DGRAM, 0, p);
  113. #if APR_HAVE_IPV6
  114. }
  115. #endif
  116. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  117. rv = fspr_socket_create(&sock2, family, SOCK_DGRAM, 0, p);
  118. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  119. rv = fspr_sockaddr_info_get(&to, addr, APR_UNSPEC, 7772, 0, p);
  120. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  121. rv = fspr_sockaddr_info_get(&from, addr, APR_UNSPEC, 7771, 0, p);
  122. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  123. rv = fspr_socket_opt_set(sock, APR_SO_REUSEADDR, 1);
  124. APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket", rv);
  125. rv = fspr_socket_opt_set(sock2, APR_SO_REUSEADDR, 1);
  126. APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket2", rv);
  127. rv = fspr_socket_bind(sock, to);
  128. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  129. rv = fspr_socket_bind(sock2, from);
  130. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  131. len = STRLEN;
  132. rv = fspr_socket_sendto(sock2, to, 0, sendbuf, &len);
  133. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  134. ABTS_INT_EQUAL(tc, STRLEN, len);
  135. len = 80;
  136. rv = fspr_socket_recvfrom(from, sock, 0, recvbuf, &len);
  137. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  138. ABTS_INT_EQUAL(tc, STRLEN, len);
  139. ABTS_STR_EQUAL(tc, "APR_INET, SOCK_DGRAM", recvbuf);
  140. fspr_sockaddr_ip_get(&ip_addr, from);
  141. fromport = from->port;
  142. ABTS_STR_EQUAL(tc, addr, ip_addr);
  143. ABTS_INT_EQUAL(tc, 7771, fromport);
  144. fspr_socket_close(sock);
  145. fspr_socket_close(sock2);
  146. }
  147. static void socket_userdata(abts_case *tc, void *data)
  148. {
  149. fspr_socket_t *sock1, *sock2;
  150. fspr_status_t rv;
  151. void *user;
  152. const char *key = "GENERICKEY";
  153. rv = fspr_socket_create(&sock1, AF_INET, SOCK_STREAM, 0, p);
  154. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  155. rv = fspr_socket_create(&sock2, AF_INET, SOCK_STREAM, 0, p);
  156. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  157. rv = fspr_socket_data_set(sock1, "SOCK1", key, NULL);
  158. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  159. rv = fspr_socket_data_set(sock2, "SOCK2", key, NULL);
  160. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  161. rv = fspr_socket_data_get(&user, key, sock1);
  162. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  163. ABTS_STR_EQUAL(tc, "SOCK1", user);
  164. rv = fspr_socket_data_get(&user, key, sock2);
  165. ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
  166. ABTS_STR_EQUAL(tc, "SOCK2", user);
  167. }
  168. abts_suite *testsockets(abts_suite *suite)
  169. {
  170. suite = ADD_SUITE(suite)
  171. abts_run_test(suite, tcp_socket, NULL);
  172. abts_run_test(suite, udp_socket, NULL);
  173. abts_run_test(suite, tcp6_socket, NULL);
  174. abts_run_test(suite, udp6_socket, NULL);
  175. abts_run_test(suite, sendto_receivefrom, NULL);
  176. abts_run_test(suite, socket_userdata, NULL);
  177. return suite;
  178. }