]> git.ipfire.org Git - thirdparty/glibc.git/blob - resolv/tst-res_use_inet6.c
Initialize tunable list with the GLIBC_TUNABLES environment variable
[thirdparty/glibc.git] / resolv / tst-res_use_inet6.c
1 /* Basic functionality tests for inet6 option processing.
2 Copyright (C) 2016 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
18
19 #include <netdb.h>
20 #include <resolv.h>
21 #include <string.h>
22 #include <support/check_nss.h>
23 #include <support/resolv_test.h>
24 #include <support/xthread.h>
25
26 static void
27 response (const struct resolv_response_context *ctx,
28 struct resolv_response_builder *b,
29 const char *qname, uint16_t qclass, uint16_t qtype)
30 {
31 bool include_both = strcmp (qname, "both.example") == 0;
32 bool include_a = qtype == T_A || include_both;
33 bool include_aaaa = qtype == T_AAAA || include_both;
34
35 resolv_response_init (b, (struct resolv_response_flags) {});
36 resolv_response_add_question (b, qname, qclass, qtype);
37 resolv_response_section (b, ns_s_an);
38 if (include_a)
39 {
40 char ipv4[4] = {192, 0, 2, 17};
41 resolv_response_open_record (b, qname, qclass, T_A, 0);
42 resolv_response_add_data (b, &ipv4, sizeof (ipv4));
43 resolv_response_close_record (b);
44 }
45 if (include_aaaa)
46 {
47 char ipv6[16]
48 = {0x20, 0x01, 0xd, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
49 resolv_response_open_record (b, qname, qclass, T_AAAA, 0);
50 resolv_response_add_data (b, &ipv6, sizeof (ipv6));
51 resolv_response_close_record (b);
52 }
53 }
54
55 /* Test that getaddrinfo is not influenced by RES_USE_INET6. */
56 static void
57 test_gai (void)
58 {
59 {
60 struct addrinfo hints =
61 {
62 .ai_family = AF_UNSPEC,
63 .ai_socktype = SOCK_STREAM,
64 .ai_protocol = IPPROTO_TCP,
65 };
66 struct addrinfo *ai;
67 int ret = getaddrinfo ("www1.example", "80", &hints, &ai);
68 check_addrinfo ("getaddrinfo AF_UNSPEC www1.example", ai, ret,
69 "address: STREAM/TCP 192.0.2.17 80\n"
70 "address: STREAM/TCP 2001:db8::1 80\n");
71 if (ret == 0)
72 freeaddrinfo (ai);
73 ret = getaddrinfo ("both.example", "80", &hints, &ai);
74 /* Combined A/AAAA responses currently result in address
75 duplication. */
76 check_addrinfo ("getaddrinfo AF_UNSPEC both.example", ai, ret,
77 "address: STREAM/TCP 192.0.2.17 80\n"
78 "address: STREAM/TCP 192.0.2.17 80\n"
79 "address: STREAM/TCP 2001:db8::1 80\n"
80 "address: STREAM/TCP 2001:db8::1 80\n");
81 if (ret == 0)
82 freeaddrinfo (ai);
83 }
84 {
85 struct addrinfo hints =
86 {
87 .ai_family = AF_INET,
88 .ai_socktype = SOCK_STREAM,
89 .ai_protocol = IPPROTO_TCP,
90 };
91 struct addrinfo *ai;
92 int ret = getaddrinfo ("www1.example", "80", &hints, &ai);
93 check_addrinfo ("getaddrinfo AF_INET www1.example", ai, ret,
94 "address: STREAM/TCP 192.0.2.17 80\n");
95 if (ret == 0)
96 freeaddrinfo (ai);
97 ret = getaddrinfo ("both.example", "80", &hints, &ai);
98 check_addrinfo ("getaddrinfo AF_INET both.example", ai, ret,
99 "address: STREAM/TCP 192.0.2.17 80\n");
100 if (ret == 0)
101 freeaddrinfo (ai);
102 }
103 {
104 struct addrinfo hints =
105 {
106 .ai_family = AF_INET6,
107 .ai_socktype = SOCK_STREAM,
108 .ai_protocol = IPPROTO_TCP,
109 };
110 struct addrinfo *ai;
111 int ret = getaddrinfo ("www1.example", "80", &hints, &ai);
112 check_addrinfo ("getaddrinfo (AF_INET6)", ai, ret,
113 "address: STREAM/TCP 2001:db8::1 80\n");
114 if (ret == 0)
115 freeaddrinfo (ai);
116 ret = getaddrinfo ("both.example", "80", &hints, &ai);
117 check_addrinfo ("getaddrinfo AF_INET6 both.example", ai, ret,
118 "address: STREAM/TCP 2001:db8::1 80\n");
119 if (ret == 0)
120 freeaddrinfo (ai);
121 }
122 }
123
124 /* Test that gethostbyname2 is not influenced by RES_USE_INET6. */
125 static void
126 test_get2 (void)
127 {
128 check_hostent ("gethostbyname2 AF_INET www1.example",
129 gethostbyname2 ("www1.example", AF_INET),
130 "name: www1.example\n"
131 "address: 192.0.2.17\n");
132 check_hostent ("gethostbyname2 AF_INET both.example",
133 gethostbyname2 ("both.example", AF_INET),
134 "name: both.example\n"
135 "address: 192.0.2.17\n");
136
137 check_hostent ("gethostbyname2 AF_INET6 www1.example",
138 gethostbyname2 ("www1.example", AF_INET6),
139 "name: www1.example\n"
140 "address: 2001:db8::1\n");
141 check_hostent ("gethostbyname2 AF_INET6 both.example",
142 gethostbyname2 ("both.example", AF_INET6),
143 "name: both.example\n"
144 "address: 2001:db8::1\n");
145 }
146
147 static void *
148 threadfunc (void *ignored)
149 {
150 struct resolv_test *obj = resolv_test_start
151 ((struct resolv_redirect_config)
152 {
153 .response_callback = response
154 });
155
156 check_hostent ("gethostbyname (\"www1.example\")",
157 gethostbyname ("www1.example"),
158 "name: www1.example\n"
159 "address: 192.0.2.17\n");
160 check_hostent ("gethostbyname (\"both.example\")",
161 gethostbyname ("both.example"),
162 "name: both.example\n"
163 "address: 192.0.2.17\n");
164 test_get2 ();
165 test_gai ();
166
167 _res.options |= RES_USE_INET6;
168 check_hostent ("gethostbyname (\"www1.example\")",
169 gethostbyname ("www1.example"),
170 "name: www1.example\n"
171 "address: 2001:db8::1\n");
172 check_hostent ("gethostbyname (\"both.example\")",
173 gethostbyname ("both.example"),
174 "name: both.example\n"
175 "address: 2001:db8::1\n");
176 test_get2 ();
177 test_gai ();
178
179 resolv_test_end (obj);
180
181 return NULL;
182 }
183
184 static int
185 do_test (void)
186 {
187 resolv_test_init ();
188
189 /* Attempt to run on a non-main thread first. */
190 {
191 pthread_t thr = xpthread_create (NULL, threadfunc, NULL);
192 xpthread_join (thr);
193 }
194
195 /* Try the main thread next. */
196 threadfunc (NULL);
197
198 return 0;
199 }
200
201 #include <support/test-driver.c>