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_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
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"
62 #define RWRAP_RESOLV_CONF_TMPL "rwrap_resolv_conf_XXXXXX"
64 const char *torture_server_address(int family)
68 const char *ip4 = getenv("TORTURE_SERVER_ADDRESS_IPV4");
70 if (ip4 != NULL && ip4[0] != '\0') {
74 return TORTURE_DNS_SRV_IPV4;
78 const char *ip6 = getenv("TORTURE_SERVER_ADDRESS_IPV6");
80 if (ip6 != NULL && ip6[0] != '\0') {
84 return TORTURE_DNS_SRV_IPV6;
94 int torture_server_port(void)
96 char *env = getenv("TORTURE_SERVER_PORT");
98 if (env != NULL && env[0] != '\0' && strlen(env) < 6) {
101 if (port > 0 && port < 65536) {
106 return TORTURE_DNS_SRV_PORT;
109 void torture_setup_socket_dir(void **state)
111 struct torture_state *s;
115 s = malloc(sizeof(struct torture_state));
118 s->socket_dir = strdup(TORTURE_SOCKET_DIR);
119 assert_non_null(s->socket_dir);
121 p = mkdtemp(s->socket_dir);
125 len = strlen(p) + 1 + strlen(TORTURE_PCAP_FILE) + 1;
127 s->pcap_file = malloc(len);
128 assert_non_null(s->pcap_file);
130 snprintf(s->pcap_file, len, "%s/%s", p, TORTURE_PCAP_FILE);
133 len = strlen(p) + 1 + strlen(TORTURE_DNS_SRV_PIDFILE) + 1;
135 s->srv_pidfile = malloc(len);
136 assert_non_null(s->srv_pidfile);
138 snprintf(s->srv_pidfile, len, "%s/%s", p, TORTURE_DNS_SRV_PIDFILE);
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);
147 const char *torture_server_resolv_conf(void **state)
149 struct torture_state *s = (struct torture_state *) *state;
153 return s->resolv_conf;
156 static char *torture_setup_resolv_conf(const char **nameservers, size_t num_ns)
163 path = strdup(RWRAP_RESOLV_CONF_TMPL);
164 assert_non_null(path);
165 rc_fd = mkstemp(path);
166 assert_non_null(path);
167 resolv_conf = fdopen(rc_fd, "a");
168 assert_non_null(resolv_conf);
170 for (i = 0; i < num_ns; i++) {
171 fputs("nameserver ", resolv_conf);
172 fputs(nameservers[i], resolv_conf);
173 fputs("\n", resolv_conf);
182 static void torture_teardown_resolv_conf(char *resolv_conf_path)
184 unlink(resolv_conf_path);
185 free(resolv_conf_path);
188 static void torture_setup_dns_srv_ip(void **state,
193 struct torture_state *s;
194 char start_dns_srv[1024] = {0};
197 const char *nameservers[1] = {
198 torture_server_address(family),
201 torture_setup_socket_dir(state);
203 s = (struct torture_state *) *state;
205 /* set default iface for the server */
206 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "10", 1);
208 snprintf(start_dns_srv, sizeof(start_dns_srv),
209 "%s/tests/dns_srv -b %s -p %d -D --pid %s",
210 BINARYDIR, ip, port, s->srv_pidfile);
212 rc = system(start_dns_srv);
213 assert_int_equal(rc, 0);
223 rc = stat(s->srv_pidfile, &sb);
226 assert_int_equal(rc, 0);
228 /* set default iface for the client */
229 setenv("SOCKET_WRAPPER_DEFAULT_IFACE", "170", 1);
231 /* write a resolv.conf for the client */
232 s->resolv_conf = torture_setup_resolv_conf(nameservers, 1);
235 void torture_setup_dns_srv_ipv4(void **state)
237 torture_setup_dns_srv_ip(state,
240 torture_server_port());
243 void torture_setup_dns_srv_ipv6(void **state)
245 torture_setup_dns_srv_ip(state,
248 torture_server_port());
251 void torture_teardown_socket_dir(void **state)
253 struct torture_state *s = (struct torture_state *) *state;
254 char *env = getenv("TORTURE_SKIP_CLEANUP");
255 char remove_cmd[1024] = {0};
258 if (env != NULL && env[0] == '1') {
259 fprintf(stderr, ">>> Skipping cleanup of %s", s->socket_dir);
261 snprintf(remove_cmd, sizeof(remove_cmd), "rm -rf %s", s->socket_dir);
263 rc = system(remove_cmd);
265 fprintf(stderr, "%s failed: %s", remove_cmd, strerror(errno));
271 free(s->srv_pidfile);
275 void torture_teardown_dns_srv(void **state)
277 struct torture_state *s = (struct torture_state *) *state;
283 bool is_running = true;
286 /* read the pidfile */
287 fd = open(s->srv_pidfile, O_RDONLY);
292 rc = read(fd, buf, sizeof(buf));
298 buf[sizeof(buf) - 1] = '\0';
300 tmp = strtol(buf, NULL, 10);
301 if (tmp == 0 || tmp > 0xFFFF || errno == ERANGE) {
305 pid = (pid_t)(tmp & 0xFFFF);
307 /* Make sure the daemon goes away! */
308 for (count = 0; count < 10; count++) {
322 "WARNING the DNS server is still running!\n");
326 torture_teardown_resolv_conf(s->resolv_conf);
327 torture_teardown_socket_dir(state);
330 void torture_generate_random_buffer(uint8_t *out, int len)
336 for (i = 0; i < len; i++) {
337 out[i] = (uint8_t)rand();