1 /* Basic functionality tests for inet6 option processing.
2 Copyright (C) 2016-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
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/>. */
22 #include <support/check_nss.h>
23 #include <support/resolv_test.h>
24 #include <support/xthread.h>
27 response (const struct resolv_response_context
*ctx
,
28 struct resolv_response_builder
*b
,
29 const char *qname
, uint16_t qclass
, uint16_t qtype
)
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
;
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
);
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
);
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
);
55 /* Test that getaddrinfo is not influenced by RES_USE_INET6. */
60 struct addrinfo hints
=
62 .ai_family
= AF_UNSPEC
,
63 .ai_socktype
= SOCK_STREAM
,
64 .ai_protocol
= IPPROTO_TCP
,
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");
73 ret
= getaddrinfo ("both.example", "80", &hints
, &ai
);
74 /* Combined A/AAAA responses currently result in address
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");
85 struct addrinfo hints
=
88 .ai_socktype
= SOCK_STREAM
,
89 .ai_protocol
= IPPROTO_TCP
,
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");
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");
104 struct addrinfo hints
=
106 .ai_family
= AF_INET6
,
107 .ai_socktype
= SOCK_STREAM
,
108 .ai_protocol
= IPPROTO_TCP
,
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");
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");
124 /* Test that gethostbyname2 is not influenced by RES_USE_INET6. */
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");
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");
148 threadfunc (void *ignored
)
150 struct resolv_test
*obj
= resolv_test_start
151 ((struct resolv_redirect_config
)
153 .response_callback
= response
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");
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");
179 resolv_test_end (obj
);
189 /* Attempt to run on a non-main thread first. */
191 pthread_t thr
= xpthread_create (NULL
, threadfunc
, NULL
);
195 /* Try the main thread next. */
201 #include <support/test-driver.c>