2 * Copyright (C) Andreas Schneider 2013 <asn@samba.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the author nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/types.h>
42 #include <sys/socket.h>
53 #define TORTURE_ECHO_SRV_IPV4 "127.0.0.10"
54 /* socket wrapper IPv6 prefix fd00::5357:5fxx */
55 #define TORTURE_ECHO_SRV_IPV6 "fd00::5357:5f0a"
56 #define TORTURE_ECHO_SRV_PORT 7
58 #define TORTURE_SOCKET_DIR "/tmp/w_XXXXXX"
59 #define TORTURE_ECHO_SRV_PIDFILE "echo_srv.pid"
60 #define TORTURE_PCAP_FILE "socket_trace.pcap"
62 const char *torture_server_address(int family)
66 const char *ip4 = getenv("TORTURE_SERVER_ADDRESS_IPV4");
68 if (ip4 != NULL && ip4[0] != '\0') {
72 return TORTURE_ECHO_SRV_IPV4;
76 const char *ip6 = getenv("TORTURE_SERVER_ADDRESS_IPV6");
78 if (ip6 != NULL && ip6[0] != '\0') {
82 return TORTURE_ECHO_SRV_IPV6;
92 int torture_server_port(void)
94 char *env = getenv("TORTURE_SERVER_PORT");
96 if (env != NULL && env[0] != '\0' && strlen(env) < 6) {
99 if (port > 0 && port < 65536) {
104 return TORTURE_ECHO_SRV_PORT;
107 void torture_setup_socket_dir(void **state)
109 struct torture_state *s;
113 s = malloc(sizeof(struct torture_state));
116 s->socket_dir = strdup(TORTURE_SOCKET_DIR);
117 assert_non_null(s->socket_dir);
119 p = mkdtemp(s->socket_dir);
123 len = strlen(p) + 1 + strlen(TORTURE_PCAP_FILE) + 1;
125 s->pcap_file = malloc(len);
126 assert_non_null(s->pcap_file);
128 snprintf(s->pcap_file, len, "%s/%s", p, TORTURE_PCAP_FILE);
131 len = strlen(p) + 1 + strlen(TORTURE_ECHO_SRV_PIDFILE) + 1;
133 s->srv_pidfile = malloc(len);
134 assert_non_null(s->srv_pidfile);
136 snprintf(s->srv_pidfile, len, "%s/%s", p, TORTURE_ECHO_SRV_PIDFILE);
138 setenv("SOCKET_WRAPPER_DIR", p, 1);
139 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "170", 1);
140 setenv("SOCKET_WRAPPER_PCAP_FILE", s->pcap_file, 1);
145 static void torture_setup_echo_srv_ip(void **state,
150 struct torture_state *s;
151 char start_echo_srv[1024] = {0};
156 torture_setup_socket_dir(state);
172 /* set default iface for the server */
173 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "10", 1);
175 snprintf(start_echo_srv, sizeof(start_echo_srv),
176 "%s/tests/echo_srv -b %s -p %d -D %s --pid %s",
177 BINARYDIR, ip, port, t, s->srv_pidfile);
179 rc = system(start_echo_srv);
180 assert_int_equal(rc, 0);
190 rc = stat(s->srv_pidfile, &sb);
191 usleep(50000L); /* 0.05s * 20 */
193 assert_int_equal(rc, 0);
195 /* set default iface for the client */
196 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "170", 1);
199 void torture_setup_echo_srv_udp_ipv4(void **state)
201 torture_setup_echo_srv_ip(state,
203 torture_server_port(),
207 void torture_setup_echo_srv_udp_ipv6(void **state)
209 torture_setup_echo_srv_ip(state,
211 torture_server_port(),
215 void torture_setup_echo_srv_tcp_ipv4(void **state)
217 torture_setup_echo_srv_ip(state,
219 torture_server_port(),
223 void torture_setup_echo_srv_tcp_ipv6(void **state)
225 torture_setup_echo_srv_ip(state,
227 torture_server_port(),
231 void torture_teardown_socket_dir(void **state)
233 struct torture_state *s = *state;
234 char *env = getenv("TORTURE_SKIP_CLEANUP");
235 char remove_cmd[1024] = {0};
238 if (env != NULL && env[0] == '1') {
239 fprintf(stderr, ">>> Skipping cleanup of %s", s->socket_dir);
241 snprintf(remove_cmd, sizeof(remove_cmd), "rm -rf %s", s->socket_dir);
243 rc = system(remove_cmd);
245 fprintf(stderr, "%s failed: %s", remove_cmd, strerror(errno));
251 free(s->srv_pidfile);
255 void torture_teardown_echo_srv(void **state)
257 struct torture_state *s = *state;
258 char buf[12] = {0}; /* -2147483648 + null byte */
262 bool is_running = true;
265 /* read the pidfile */
266 fd = open(s->srv_pidfile, O_RDONLY);
271 rc = read(fd, buf, sizeof(buf));
277 buf[sizeof(buf) - 1] = '\0';
280 pid = strtol(buf, NULL, 10);
281 if (pid == 0 || errno != 0) {
285 for (count = 0; count < 10; count++) {
286 /* Make sure the daemon goes away! */
300 "WARNING the echo server is still running!\n");
304 torture_teardown_socket_dir(state);
307 void torture_generate_random_buffer(uint8_t *out, int len)
313 for (i = 0; i < len; i++) {
314 out[i] = (uint8_t)rand();