~ubuntu-branches/ubuntu/feisty/apache2/feisty

« back to all changes in this revision

Viewing changes to srclib/apr/test/testsockets.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Barth
  • Date: 2006-12-09 21:05:45 UTC
  • mfrom: (0.6.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20061209210545-h70s0xaqc2v8vqr2
Tags: 2.2.3-3.2
* Non-maintainer upload.
* 043_ajp_connection_reuse: Patch from upstream Bugzilla, fixing a critical
  issue with regard to connection reuse in mod_proxy_ajp.
  Closes: #396265

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
 
2
 * applicable.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * 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
 
 
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
 
 
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
/* On recent Linux systems, whilst IPv6 is always supported by glibc,
 
60
 * socket(AF_INET6, ...) calls will fail with EAFNOSUPPORT if the
 
61
 * "ipv6" kernel module is not loaded.  */
 
62
#ifdef EAFNOSUPPORT
 
63
#define V6_NOT_ENABLED(e) ((e) == EAFNOSUPPORT)
 
64
#else
 
65
#define V6_NOT_ENABLED(e) (0)
 
66
#endif
 
67
 
 
68
static void tcp6_socket(abts_case *tc, void *data)
 
69
{
 
70
#if APR_HAVE_IPV6
 
71
    apr_status_t rv;
 
72
    apr_socket_t *sock = NULL;
 
73
 
 
74
    rv = apr_socket_create(&sock, APR_INET6, SOCK_STREAM, 0, p);
 
75
    if (V6_NOT_ENABLED(rv)) {
 
76
        ABTS_NOT_IMPL(tc, "IPv6 not enabled");
 
77
        return;
 
78
    }
 
79
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
80
    ABTS_PTR_NOTNULL(tc, sock);
 
81
    apr_socket_close(sock);
 
82
#else
 
83
    ABTS_NOT_IMPL(tc, "IPv6");
 
84
#endif
 
85
}
 
86
 
 
87
static void udp6_socket(abts_case *tc, void *data)
 
88
{
 
89
#if APR_HAVE_IPV6
 
90
    apr_status_t rv;
 
91
    apr_socket_t *sock = NULL;
 
92
 
 
93
    rv = apr_socket_create(&sock, APR_INET6, SOCK_DGRAM, 0, p);
 
94
    if (V6_NOT_ENABLED(rv)) {
 
95
        ABTS_NOT_IMPL(tc, "IPv6 not enabled");
 
96
        return;
 
97
    }
 
98
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
99
    ABTS_PTR_NOTNULL(tc, sock);
 
100
    apr_socket_close(sock);
 
101
#else
 
102
    ABTS_NOT_IMPL(tc, "IPv6");
 
103
#endif
 
104
}
 
105
 
 
106
static void sendto_receivefrom(abts_case *tc, void *data)
 
107
{
 
108
    apr_status_t rv;
 
109
    apr_socket_t *sock = NULL;
 
110
    apr_socket_t *sock2 = NULL;
 
111
    char sendbuf[STRLEN] = "APR_INET, SOCK_DGRAM";
 
112
    char recvbuf[80];
 
113
    char *ip_addr;
 
114
    apr_port_t fromport;
 
115
    apr_sockaddr_t *from;
 
116
    apr_sockaddr_t *to;
 
117
    apr_size_t len = 30;
 
118
    int family;
 
119
    const char *addr;
 
120
 
 
121
#if APR_HAVE_IPV6
 
122
    family = APR_INET6;
 
123
    addr = "::1";
 
124
    rv = apr_socket_create(&sock, family, SOCK_DGRAM, 0, p);
 
125
    if (V6_NOT_ENABLED(rv)) {
 
126
#endif
 
127
        family = APR_INET;
 
128
        addr = "127.0.0.1";
 
129
        rv = apr_socket_create(&sock, family, SOCK_DGRAM, 0, p);
 
130
#if APR_HAVE_IPV6
 
131
    } 
 
132
#endif
 
133
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
134
    rv = apr_socket_create(&sock2, family, SOCK_DGRAM, 0, p);
 
135
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
136
 
 
137
    rv = apr_sockaddr_info_get(&to, addr, APR_UNSPEC, 7772, 0, p);
 
138
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
139
    rv = apr_sockaddr_info_get(&from, addr, APR_UNSPEC, 7771, 0, p);
 
140
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
141
 
 
142
    rv = apr_socket_opt_set(sock, APR_SO_REUSEADDR, 1);
 
143
    APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket", rv);
 
144
    rv = apr_socket_opt_set(sock2, APR_SO_REUSEADDR, 1);
 
145
    APR_ASSERT_SUCCESS(tc, "Could not set REUSEADDR on socket2", rv);
 
146
 
 
147
    rv = apr_socket_bind(sock, to);
 
148
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
149
    rv = apr_socket_bind(sock2, from);
 
150
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
151
 
 
152
    len = STRLEN;
 
153
    rv = apr_socket_sendto(sock2, to, 0, sendbuf, &len);
 
154
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
155
    ABTS_INT_EQUAL(tc, STRLEN, len);
 
156
 
 
157
    len = 80;
 
158
    rv = apr_socket_recvfrom(from, sock, 0, recvbuf, &len);
 
159
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
160
    ABTS_INT_EQUAL(tc, STRLEN, len);
 
161
    ABTS_STR_EQUAL(tc, "APR_INET, SOCK_DGRAM", recvbuf);
 
162
 
 
163
    apr_sockaddr_ip_get(&ip_addr, from);
 
164
    fromport = from->port;
 
165
    ABTS_STR_EQUAL(tc, addr, ip_addr);
 
166
    ABTS_INT_EQUAL(tc, 7771, fromport);
 
167
 
 
168
    apr_socket_close(sock);
 
169
    apr_socket_close(sock2);
 
170
}
 
171
 
 
172
static void socket_userdata(abts_case *tc, void *data)
 
173
{
 
174
    apr_socket_t *sock1, *sock2;
 
175
    apr_status_t rv;
 
176
    void *user;
 
177
    const char *key = "GENERICKEY";
 
178
 
 
179
    rv = apr_socket_create(&sock1, AF_INET, SOCK_STREAM, 0, p);
 
180
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
181
    rv = apr_socket_create(&sock2, AF_INET, SOCK_STREAM, 0, p);
 
182
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
183
 
 
184
    rv = apr_socket_data_set(sock1, "SOCK1", key, NULL);
 
185
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
186
    rv = apr_socket_data_set(sock2, "SOCK2", key, NULL);
 
187
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
188
 
 
189
    rv = apr_socket_data_get(&user, key, sock1);
 
190
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
191
    ABTS_STR_EQUAL(tc, "SOCK1", user);
 
192
    rv = apr_socket_data_get(&user, key, sock2);
 
193
    ABTS_INT_EQUAL(tc, APR_SUCCESS, rv);
 
194
    ABTS_STR_EQUAL(tc, "SOCK2", user);
 
195
}
 
196
 
 
197
abts_suite *testsockets(abts_suite *suite)
 
198
{
 
199
    suite = ADD_SUITE(suite)
 
200
 
 
201
    abts_run_test(suite, tcp_socket, NULL);
 
202
    abts_run_test(suite, udp_socket, NULL);
 
203
 
 
204
    abts_run_test(suite, tcp6_socket, NULL);
 
205
    abts_run_test(suite, udp6_socket, NULL);
 
206
 
 
207
    abts_run_test(suite, sendto_receivefrom, NULL);
 
208
 
 
209
    abts_run_test(suite, socket_userdata, NULL);
 
210
    
 
211
    return suite;
 
212
}
 
213