xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testsockets.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 
17 #include "apr_network_io.h"
18 #include "apr_errno.h"
19 #include "apr_general.h"
20 #include "apr_lib.h"
21 #include "testutil.h"
22 
23 #define STRLEN 21
24 
tcp_socket(abts_case * tc,void * data)25 static void tcp_socket(abts_case *tc, void *data)
26 {
27     apr_status_t rv;
28     apr_socket_t *sock = NULL;
29     int type;
30 
31     rv = apr_socket_create(&sock, APR_INET, SOCK_STREAM, 0, p);
32     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
33     ABTS_PTR_NOTNULL(tc, sock);
34 
35     rv = apr_socket_type_get(sock, &type);
36     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
37     ABTS_INT_EQUAL(tc, SOCK_STREAM, type);
38 
39     apr_socket_close(sock);
40 }
41 
udp_socket(abts_case * tc,void * data)42 static void udp_socket(abts_case *tc, void *data)
43 {
44     apr_status_t rv;
45     apr_socket_t *sock = NULL;
46     int type;
47 
48     rv = apr_socket_create(&sock, APR_INET, SOCK_DGRAM, 0, p);
49     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
50     ABTS_PTR_NOTNULL(tc, sock);
51 
52     rv = apr_socket_type_get(sock, &type);
53     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
54     ABTS_INT_EQUAL(tc, SOCK_DGRAM, type);
55 
56     apr_socket_close(sock);
57 }
58 
59 #if APR_HAVE_IPV6
tcp6_socket(abts_case * tc,void * data)60 static void tcp6_socket(abts_case *tc, void *data)
61 {
62     apr_status_t rv;
63     apr_socket_t *sock = NULL;
64 
65     rv = apr_socket_create(&sock, APR_INET6, SOCK_STREAM, 0, p);
66     if (APR_STATUS_IS_EAFNOSUPPORT(rv)) {
67         ABTS_NOT_IMPL(tc, "IPv6 not enabled");
68         return;
69     }
70     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
71     ABTS_PTR_NOTNULL(tc, sock);
72     apr_socket_close(sock);
73 }
74 
udp6_socket(abts_case * tc,void * data)75 static void udp6_socket(abts_case *tc, void *data)
76 {
77     apr_status_t rv;
78     apr_socket_t *sock = NULL;
79 
80     rv = apr_socket_create(&sock, APR_INET6, SOCK_DGRAM, 0, p);
81     if (APR_STATUS_IS_EAFNOSUPPORT(rv)) {
82         ABTS_NOT_IMPL(tc, "IPv6 not enabled");
83         return;
84     }
85     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
86     ABTS_PTR_NOTNULL(tc, sock);
87     apr_socket_close(sock);
88 }
89 #endif
90 
sendto_receivefrom_helper(abts_case * tc,const char * addr,int family)91 static void sendto_receivefrom_helper(abts_case *tc, const char *addr,
92                                       int family)
93 {
94     apr_status_t rv;
95     apr_socket_t *sock = NULL;
96     apr_socket_t *sock2 = NULL;
97     char sendbuf[STRLEN] = "APR_INET, SOCK_DGRAM";
98     char recvbuf[80];
99     char *ip_addr;
100     apr_port_t fromport;
101     apr_sockaddr_t *from;
102     apr_sockaddr_t *to;
103     apr_size_t len = 30;
104 
105     rv = apr_socket_create(&sock, family, SOCK_DGRAM, 0, p);
106 #if APR_HAVE_IPV6
107     if ((family == APR_INET6) && APR_STATUS_IS_EAFNOSUPPORT(rv)) {
108         ABTS_NOT_IMPL(tc, "IPv6 not enabled");
109         return;
110     }
111 #endif
112     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
113     if (rv != APR_SUCCESS)
114         return;
115     rv = apr_socket_create(&sock2, family, SOCK_DGRAM, 0, p);
116     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
117     if (rv != APR_SUCCESS)
118         return;
119 
120     rv = apr_sockaddr_info_get(&to, addr, family, 7772, 0, p);
121     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
122     rv = apr_sockaddr_info_get(&from, addr, family, 7771, 0, p);
123     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
124 
125     rv = apr_socket_opt_set(sock, APR_SO_REUSEADDR, 1);
126     APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket", rv);
127     rv = apr_socket_opt_set(sock2, APR_SO_REUSEADDR, 1);
128     APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket2", rv);
129 
130     rv = apr_socket_bind(sock, to);
131     APR_ASSERT_SUCCESS(tc, "Could not bind socket", rv);
132     if (rv != APR_SUCCESS)
133         return;
134     rv = apr_mcast_hops(sock, 10);
135     APR_ASSERT_SUCCESS(tc, "Could not set multicast hops", rv);
136     if (rv != APR_SUCCESS)
137         return;
138 
139     rv = apr_socket_bind(sock2, from);
140     APR_ASSERT_SUCCESS(tc, "Could not bind second socket", rv);
141     if (rv != APR_SUCCESS)
142         return;
143 
144     len = STRLEN;
145     rv = apr_socket_sendto(sock2, to, 0, sendbuf, &len);
146     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
147     ABTS_SIZE_EQUAL(tc, STRLEN, len);
148 
149     /* fill the "from" sockaddr with a random address from another
150      * family to ensure that recvfrom sets it up properly. */
151 #if APR_HAVE_IPV6
152     if (family == APR_INET)
153         rv = apr_sockaddr_info_get(&from, "3ffE:816e:abcd:1234::1",
154                                    APR_INET6, 4242, 0, p);
155     else
156 #endif
157         rv = apr_sockaddr_info_get(&from, "127.1.2.3", APR_INET, 4242, 0, p);
158     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
159 
160     len = 80;
161     rv = apr_socket_recvfrom(from, sock, 0, recvbuf, &len);
162     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
163     ABTS_SIZE_EQUAL(tc, STRLEN, len);
164     ABTS_STR_EQUAL(tc, "APR_INET, SOCK_DGRAM", recvbuf);
165 
166     apr_sockaddr_ip_get(&ip_addr, from);
167     fromport = from->port;
168     ABTS_STR_EQUAL(tc, addr, ip_addr);
169     ABTS_INT_EQUAL(tc, 7771, fromport);
170 
171     apr_socket_close(sock);
172     apr_socket_close(sock2);
173 }
174 
sendto_receivefrom(abts_case * tc,void * data)175 static void sendto_receivefrom(abts_case *tc, void *data)
176 {
177     int failed;
178     sendto_receivefrom_helper(tc, "127.0.0.1", APR_INET);
179     failed = tc->failed; tc->failed = 0;
180     ABTS_TRUE(tc, !failed);
181 }
182 
183 #if APR_HAVE_IPV6
sendto_receivefrom6(abts_case * tc,void * data)184 static void sendto_receivefrom6(abts_case *tc, void *data)
185 {
186     int failed;
187     sendto_receivefrom_helper(tc, "::1", APR_INET6);
188     failed = tc->failed; tc->failed = 0;
189     ABTS_TRUE(tc, !failed);
190 }
191 #endif
192 
socket_userdata(abts_case * tc,void * data)193 static void socket_userdata(abts_case *tc, void *data)
194 {
195     apr_socket_t *sock1, *sock2;
196     apr_status_t rv;
197     void *user;
198     const char *key = "GENERICKEY";
199 
200     rv = apr_socket_create(&sock1, AF_INET, SOCK_STREAM, 0, p);
201     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
202     rv = apr_socket_create(&sock2, AF_INET, SOCK_STREAM, 0, p);
203     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
204 
205     rv = apr_socket_data_set(sock1, "SOCK1", key, NULL);
206     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
207     rv = apr_socket_data_set(sock2, "SOCK2", key, NULL);
208     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
209 
210     rv = apr_socket_data_get(&user, key, sock1);
211     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
212     ABTS_STR_EQUAL(tc, "SOCK1", user);
213     rv = apr_socket_data_get(&user, key, sock2);
214     ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
215     ABTS_STR_EQUAL(tc, "SOCK2", user);
216 }
217 
testsockets(abts_suite * suite)218 abts_suite *testsockets(abts_suite *suite)
219 {
220     suite = ADD_SUITE(suite)
221 
222     abts_run_test(suite, tcp_socket, NULL);
223     abts_run_test(suite, udp_socket, NULL);
224 
225     abts_run_test(suite, sendto_receivefrom, NULL);
226 
227 #if APR_HAVE_IPV6
228     abts_run_test(suite, tcp6_socket, NULL);
229     abts_run_test(suite, udp6_socket, NULL);
230 
231     abts_run_test(suite, sendto_receivefrom6, NULL);
232 #endif
233 
234     abts_run_test(suite, socket_userdata, NULL);
235 
236     return suite;
237 }
238 
239