bottleneck testcase based on rubbos
[bottlenecks.git] / rubbos / app / httpd-2.0.64 / srclib / apr / test / server.c
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 #define APR_TEST_PREFIX "server: "
18
19 #include "aprtest.h"
20 #include <stdlib.h>
21 #include "apr_network_io.h"
22 #include "apr_getopt.h"
23 #include "apr_poll.h"
24
25 #define STRLEN 15
26
27 int main(int argc, const char * const argv[])
28 {
29     apr_pool_t *context;
30     apr_status_t rv;
31     apr_socket_t *sock;
32     apr_socket_t *sock2;
33     apr_size_t length;
34     apr_int32_t pollres;
35     apr_pollfd_t *sdset;
36     char datasend[STRLEN];
37     char datarecv[STRLEN] = "Recv data test";
38     const char *bind_to_ipaddr = NULL;
39     char *local_ipaddr, *remote_ipaddr;
40     apr_port_t local_port, remote_port;
41     apr_sockaddr_t *localsa = NULL, *remotesa;
42     apr_status_t stat;
43     int family = APR_UNSPEC;
44     int protocol;
45     apr_getopt_t *opt;
46     const char *optarg;
47     char optchar;
48
49     APR_TEST_INITIALIZE(rv, context);
50
51     APR_TEST_SUCCESS(rv, "Preparing getopt", 
52                      apr_getopt_init(&opt, context, argc, argv))
53     
54     while ((stat = apr_getopt(opt, "i:", &optchar, &optarg)) == APR_SUCCESS) {
55         switch(optchar) {
56         case 'i':
57             bind_to_ipaddr = optarg;
58             break;
59         }
60     }
61     if (stat != APR_EOF) {
62         fprintf(stderr,
63                 "usage: %s [-i local-interface-address]\n",
64                 argv[0]);
65         exit(-1);
66     }
67
68     if (bind_to_ipaddr) {
69         /* First, parse/resolve ipaddr so we know what address family of
70          * socket we need.  We'll use the returned sockaddr later when
71          * we bind.
72          */
73         APR_TEST_SUCCESS(rv, "Preparing sockaddr", 
74             apr_sockaddr_info_get(&localsa, bind_to_ipaddr, APR_UNSPEC, 8021, 0, context))
75         family = localsa->family;
76     }
77
78     APR_TEST_SUCCESS(rv, "Creating new socket", 
79         apr_socket_create_ex(&sock, family, SOCK_STREAM, APR_PROTO_TCP, context))
80
81     APR_TEST_SUCCESS(rv, "Setting option APR_SO_NONBLOCK",
82         apr_socket_opt_set(sock, APR_SO_NONBLOCK, 1))
83
84     APR_TEST_SUCCESS(rv, "Setting option APR_SO_REUSEADDR",
85         apr_socket_opt_set(sock, APR_SO_REUSEADDR, 1))
86
87     if (!localsa) {
88         apr_socket_addr_get(&localsa, APR_LOCAL, sock);
89         apr_sockaddr_port_set(localsa, 8021);
90     }
91
92     APR_TEST_SUCCESS(rv, "Binding socket to port",
93         apr_socket_bind(sock, localsa))
94     
95     APR_TEST_SUCCESS(rv, "Listening to socket",
96         apr_socket_listen(sock, 5))
97     
98     APR_TEST_BEGIN(rv, "Setting up for polling",
99         apr_poll_setup(&sdset, 1, context))
100     APR_TEST_END(rv, 
101         apr_poll_socket_add(sdset, sock, APR_POLLIN))
102     
103     pollres = 1; 
104     APR_TEST_BEGIN(rv, "Polling for socket",
105         apr_poll(sdset, 1, &pollres, -1))
106
107     if (pollres == 0) {
108         fprintf(stdout, "Failed\n");
109         apr_socket_close(sock);
110         fprintf(stderr, "Error: Unrecognized poll result, "
111                 "expected 1, received %d\n", pollres);
112         exit(-1);
113     }
114     fprintf(stdout, "OK\n");
115
116     APR_TEST_SUCCESS(rv, "Accepting a connection",
117         apr_socket_accept(&sock2, sock, context))
118
119     apr_socket_protocol_get(sock2, &protocol);
120     if (protocol != APR_PROTO_TCP) {
121         fprintf(stderr, "Error: protocol not conveyed from listening socket "
122                 "to connected socket!\n");
123         exit(1);
124     }
125     apr_socket_addr_get(&remotesa, APR_REMOTE, sock2);
126     apr_sockaddr_ip_get(&remote_ipaddr, remotesa);
127     apr_sockaddr_port_get(&remote_port, remotesa);
128     apr_socket_addr_get(&localsa, APR_LOCAL, sock2);
129     apr_sockaddr_ip_get(&local_ipaddr, localsa);
130     apr_sockaddr_port_get(&local_port, localsa);
131     fprintf(stdout, "Server socket: %s:%u -> %s:%u\n", local_ipaddr, 
132             local_port, remote_ipaddr, remote_port);
133
134     APR_TEST_SUCCESS(rv, "Setting timeout on client socket",
135         apr_socket_timeout_set(sock2, apr_time_from_sec(3)));
136
137     length = STRLEN;
138     APR_TEST_BEGIN(rv, "Receiving data from socket",
139         apr_socket_recv(sock2, datasend, &length))
140
141     if (strcmp(datasend, "Send data test")) {
142         fprintf(stdout, "Failed\n");
143         apr_socket_close(sock);
144         apr_socket_close(sock2);
145         fprintf(stderr, "Error: Unrecognized response;\n"
146                 "Expected: \"Send data test\"\n"
147                 "Received: \"%s\"\n", datarecv);
148         exit(-1);
149     }
150     fprintf(stdout, "OK\n");
151
152     length = STRLEN;
153     APR_TEST_SUCCESS(rv, "Sending data over socket",
154         apr_socket_send(sock2, datarecv, &length))
155     
156     APR_TEST_SUCCESS(rv, "Shutting down accepted socket",
157         apr_socket_shutdown(sock2, APR_SHUTDOWN_READ))
158
159     APR_TEST_SUCCESS(rv, "Closing duplicate socket",
160         apr_socket_close(sock2))
161     
162     APR_TEST_SUCCESS(rv, "Closing original socket",
163         apr_socket_close(sock))
164
165     return 0;
166 }
167