Debian Codegraph @

tests/torture.c

raw symbols SCIP
1 /*
2 * Copyright (C) Andreas Schneider 2013 <[email protected]>
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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
31 * SUCH DAMAGE.
32 */
33
34 #include "config.h"
35
36 #include "torture.h"
37
38 #include <errno.h>
39
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/socket.h>
43 #include <signal.h>
44 #include <fcntl.h>
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <unistd.h>
50 #include <time.h>
51 #include <stdbool.h>
52
53 #define TORTURE_DNS_SRV_IPV4 "127.0.0.10"
54 /* socket wrapper IPv6 prefix fd00::5357:5fxx */
55 #define TORTURE_DNS_SRV_IPV6 "fd00::5357:5f0a"
56 #define TORTURE_DNS_SRV_PORT 53
57
58 #define TORTURE_SOCKET_DIR "/tmp/test_resolv_wrapper_XXXXXX"
59 #define TORTURE_DNS_SRV_PIDFILE "dns_srv.pid"
60 #define TORTURE_PCAP_FILE "socket_trace.pcap"
61
62 #define RWRAP_RESOLV_CONF_TMPL "rwrap_resolv_conf_XXXXXX"
63
64 const char *torture_server_address(int family)
65 {
66 switch (family) {
67 case AF_INET: {
68 const char *ip4 = getenv("TORTURE_SERVER_ADDRESS_IPV4");
69
70 if (ip4 != NULL && ip4[0] != '\0') {
71 return ip4;
72 }
73
74 return TORTURE_DNS_SRV_IPV4;
75 }
76 #ifdef HAVE_IPV6
77 case AF_INET6: {
78 const char *ip6 = getenv("TORTURE_SERVER_ADDRESS_IPV6");
79
80 if (ip6 != NULL && ip6[0] != '\0') {
81 return ip6;
82 }
83
84 return TORTURE_DNS_SRV_IPV6;
85 }
86 #endif
87 default:
88 return NULL;
89 }
90
91 return NULL;
92 }
93
94 int torture_server_port(void)
95 {
96 char *env = getenv("TORTURE_SERVER_PORT");
97
98 if (env != NULL && env[0] != '\0' && strlen(env) < 6) {
99 int port = atoi(env);
100
101 if (port > 0 && port < 65536) {
102 return port;
103 }
104 }
105
106 return TORTURE_DNS_SRV_PORT;
107 }
108
109 void torture_setup_socket_dir(void **state)
110 {
111 struct torture_state *s;
112 const char *p;
113 size_t len;
114
115 s = malloc(sizeof(struct torture_state));
116 assert_non_null(s);
117
118 s->socket_dir = strdup(TORTURE_SOCKET_DIR);
119 assert_non_null(s->socket_dir);
120
121 p = mkdtemp(s->socket_dir);
122 assert_non_null(p);
123
124 /* pcap file */
125 len = strlen(p) + 1 + strlen(TORTURE_PCAP_FILE) + 1;
126
127 s->pcap_file = malloc(len);
128 assert_non_null(s->pcap_file);
129
130 snprintf(s->pcap_file, len, "%s/%s", p, TORTURE_PCAP_FILE);
131
132 /* pid file */
133 len = strlen(p) + 1 + strlen(TORTURE_DNS_SRV_PIDFILE) + 1;
134
135 s->srv_pidfile = malloc(len);
136 assert_non_null(s->srv_pidfile);
137
138 snprintf(s->srv_pidfile, len, "%s/%s", p, TORTURE_DNS_SRV_PIDFILE);
139
140 setenv("SOCKET_WRAPPER_DIR", p, 1);
141 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "170", 1);
142 setenv("SOCKET_WRAPPER_PCAP_FILE", s->pcap_file, 1);
143
144 *state = s;
145 }
146
147 const char *torture_server_resolv_conf(void **state)
148 {
149 struct torture_state *s = (struct torture_state *) *state;
150 if (s == NULL) {
151 return NULL;
152 }
153 return s->resolv_conf;
154 }
155
156 static char *torture_setup_resolv_conf(const char **nameservers, size_t num_ns)
157 {
158 char *path;
159 int rc_fd;
160 FILE *resolv_conf;
161 size_t i;
162
163 path = strdup(RWRAP_RESOLV_CONF_TMPL);
164 assert_non_null(path);
165 rc_fd = mkstemp(path);
166 assert_return_code(rc_fd, errno);
167 assert_non_null(path);
168 resolv_conf = fdopen(rc_fd, "a");
169 assert_non_null(resolv_conf);
170
171 for (i = 0; i < num_ns; i++) {
172 fputs("nameserver ", resolv_conf);
173 fputs(nameservers[i], resolv_conf);
174 fputs("\n", resolv_conf);
175 }
176 fflush(resolv_conf);
177
178 fclose(resolv_conf);
179 close(rc_fd);
180 return path;
181 }
182
183 static void torture_teardown_resolv_conf(char *resolv_conf_path)
184 {
185 unlink(resolv_conf_path);
186 free(resolv_conf_path);
187 }
188
189 static void torture_setup_dns_srv_ip(void **state,
190 int family,
191 const char *ip,
192 int port)
193 {
194 struct torture_state *s;
195 char start_dns_srv[1024] = {0};
196 int count = 0;
197 int rc;
198 const char *nameservers[1] = {
199 torture_server_address(family),
200 };
201
202 torture_setup_socket_dir(state);
203
204 s = (struct torture_state *) *state;
205
206 /* set default iface for the server */
207 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "10", 1);
208
209 snprintf(start_dns_srv, sizeof(start_dns_srv),
210 "%s/tests/dns_srv -b %s -p %d -D --pid %s",
211 BINARYDIR, ip, port, s->srv_pidfile);
212
213 rc = system(start_dns_srv);
214 assert_int_equal(rc, 0);
215
216 do {
217 struct stat sb;
218
219 count++;
220 if (count > 100) {
221 break;
222 }
223
224 rc = stat(s->srv_pidfile, &sb);
225 /* Wait 200 ms before retrying */
226 usleep(200 * 1000);
227 } while (rc != 0);
228 assert_int_equal(rc, 0);
229
230 /* set default iface for the client */
231 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "170", 1);
232
233 /* write a resolv.conf for the client */
234 s->resolv_conf = torture_setup_resolv_conf(nameservers, 1);
235 }
236
237 void torture_setup_dns_srv_ipv4(void **state)
238 {
239 torture_setup_dns_srv_ip(state,
240 AF_INET,
241 "0.0.0.0",
242 torture_server_port());
243 }
244
245 void torture_setup_dns_srv_ipv6(void **state)
246 {
247 torture_setup_dns_srv_ip(state,
248 AF_INET6,
249 "::",
250 torture_server_port());
251 }
252
253 void torture_teardown_socket_dir(void **state)
254 {
255 struct torture_state *s = (struct torture_state *) *state;
256 char *env = getenv("TORTURE_SKIP_CLEANUP");
257 char remove_cmd[1024] = {0};
258 int rc;
259
260 if (env != NULL && env[0] == '1') {
261 fprintf(stderr, ">>> Skipping cleanup of %s", s->socket_dir);
262 } else {
263 snprintf(remove_cmd, sizeof(remove_cmd), "rm -rf %s", s->socket_dir);
264
265 rc = system(remove_cmd);
266 if (rc < 0) {
267 fprintf(stderr, "%s failed: %s", remove_cmd, strerror(errno));
268 }
269 }
270
271 free(s->socket_dir);
272 free(s->pcap_file);
273 free(s->srv_pidfile);
274 free(s);
275 }
276
277 void torture_teardown_dns_srv(void **state)
278 {
279 struct torture_state *s = (struct torture_state *) *state;
280 char buf[8] = {0};
281 long int tmp;
282 ssize_t rc;
283 pid_t pid;
284 int fd;
285 bool is_running = true;
286 int count;
287
288 /* read the pidfile */
289 fd = open(s->srv_pidfile, O_RDONLY);
290 if (fd < 0) {
291 goto done;
292 }
293
294 rc = read(fd, buf, sizeof(buf));
295 close(fd);
296 if (rc <= 0) {
297 goto done;
298 }
299
300 buf[sizeof(buf) - 1] = '\0';
301
302 tmp = strtol(buf, NULL, 10);
303 if (tmp == 0 || tmp > 0xFFFF || errno == ERANGE) {
304 goto done;
305 }
306
307 pid = (pid_t)(tmp & 0xFFFF);
308
309 /* Make sure the daemon goes away! */
310 for (count = 0; count < 10; count++) {
311 kill(pid, SIGTERM);
312
313 usleep(200);
314
315 rc = kill(pid, 0);
316 if (rc != 0) {
317 is_running = false;
318 break;
319 }
320 }
321
322 if (is_running) {
323 fprintf(stderr,
324 "WARNING the DNS server is still running!\n");
325 }
326
327 done:
328 torture_teardown_resolv_conf(s->resolv_conf);
329 torture_teardown_socket_dir(state);
330 }
331
332 void torture_generate_random_buffer(uint8_t *out, int len)
333 {
334 int i;
335
336 srand(time(NULL));
337
338 for (i = 0; i < len; i++) {
339 out[i] = (uint8_t)rand();
340 }
341 }