xref: /freebsd/lib/libc/tests/net/getaddrinfo/getaddrinfo_test.sh (revision 423a21a5b65cc7b7809d29c4d40a8ca4a224a3f5)
1#	$NetBSD: t_getaddrinfo.sh,v 1.2 2011/06/15 07:54:32 jmmv Exp $
2
3#
4# Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, and 2002 WIDE Project.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15# 3. Neither the name of the project nor the names of its contributors
16#    may be used to endorse or promote products derived from this software
17#    without specific prior written permission.
18#
19# THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22# ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29# SUCH DAMAGE.
30#
31
32if [ "$(sysctl -i -n kern.features.vimage)" != 1 ]; then
33	atf_skip "This test requires VIMAGE"
34fi
35
36vnet_mkjail()
37{
38       jailname=getaddrinfo_test_$1
39       jail -c name=${jailname} persist vnet
40       ifconfig -j ${jailname} lo0 inet 127.0.0.1/8
41       # For those machines not support IPv6
42       ifconfig -j ${jailname} lo0 inet6 ::1/64 || true
43       service -j ${jailname} ip6addrctl $2 || true
44}
45
46vnet_cleanup()
47{
48       jailname=getaddrinfo_test_$1
49       jail -r ${jailname}
50}
51
52check_output()
53{
54	if [ "$2" = "none" ]; then
55		if [ "$3" = "prefer_v6" ]; then
56			exp="${1}.exp"
57		else
58			exp="${1}_v4_only.exp"
59		fi
60	elif [ "$2" = "hosts" ]; then
61		lcl=$(cat /etc/hosts | sed -e 's/#.*$//' -e 's/[ 	][ 	]*/ /g' | awk '/ localhost($| )/ {printf "%s ", $1}')
62		if [ "${lcl%*::*}" = "${lcl}" ]; then
63			exp="${1}_v4_only.exp"
64		else
65			if [ "$3" = "prefer_v6" ]; then
66				exp="${1}_v4v6.exp"
67			else
68				exp="${1}_v4v6_prefer_v4.exp"
69			fi
70		fi
71	elif [ "$2" = "ifconfig" ]; then
72		lcl=$(ifconfig lo0 | grep inet6)
73		if [ -n "${lcl}" ]; then
74			if [ "$3" = "prefer_v6" ]; then
75				exp="${1}_v4v6.exp"
76			else
77				exp="${1}_v4v6_prefer_v4.exp"
78			fi
79		else
80			exp="${1}_v4_only.exp"
81		fi
82	else
83		atf_fail "Invalid family_match_type $2 requested."
84	fi
85
86	cmp -s "$(atf_get_srcdir)/data/${exp}" out && return
87	diff -u "$(atf_get_srcdir)/data/${exp}" out || atf_fail "Actual output does not match expected output"
88}
89
90atf_test_case basic_prefer_v4 cleanup
91basic_prefer_v4_head()
92{
93	atf_set "descr" "Testing basic ones with prefer_v4"
94	atf_set "require.user" "root"
95}
96basic_prefer_v4_body()
97{
98	vnet_mkjail basic_prefer_v4 prefer_ipv4
99	TEST="jexec getaddrinfo_test_basic_prefer_v4 $(atf_get_srcdir)/h_gai"
100
101	( $TEST ::1 http
102	  $TEST 127.0.0.1 http
103	  $TEST localhost http
104	  $TEST ::1 tftp
105	  $TEST 127.0.0.1 tftp
106	  $TEST localhost tftp
107	  $TEST ::1 echo
108	  $TEST 127.0.0.1 echo
109	  $TEST localhost echo ) > out 2>&1
110
111	check_output basics hosts prefer_v4
112}
113basic_prefer_v4_cleanup()
114{
115	vnet_cleanup basic_prefer_v4
116}
117
118atf_test_case basic cleanup
119basic_head()
120{
121	atf_set "descr" "Testing basic ones with prefer_v6"
122	atf_set "require.user" "root"
123}
124basic_body()
125{
126	vnet_mkjail basic prefer_ipv6
127	TEST="jexec getaddrinfo_test_basic $(atf_get_srcdir)/h_gai"
128
129	( $TEST ::1 http
130	  $TEST 127.0.0.1 http
131	  $TEST localhost http
132	  $TEST ::1 tftp
133	  $TEST 127.0.0.1 tftp
134	  $TEST localhost tftp
135	  $TEST ::1 echo
136	  $TEST 127.0.0.1 echo
137	  $TEST localhost echo ) > out 2>&1
138
139	check_output basics ifconfig prefer_v6
140}
141basic_cleanup()
142{
143	vnet_cleanup basic
144}
145
146atf_test_case specific_prefer_v4 cleanup
147specific_prefer_v4_head()
148{
149	atf_set "descr" "Testing specific address family with prefer_v4"
150	atf_set "require.user" "root"
151}
152specific_prefer_v4_body()
153{
154	vnet_mkjail specific_prefer_v4 prefer_ipv4
155	TEST="jexec getaddrinfo_test_specific_prefer_v4 $(atf_get_srcdir)/h_gai"
156
157	( $TEST -4 localhost http
158	  $TEST -6 localhost http ) > out 2>&1
159
160	check_output spec_fam hosts prefer_v4
161}
162specific_prefer_v4_cleanup()
163{
164	vnet_cleanup specific_prefer_v4
165}
166
167atf_test_case specific cleanup
168specific_head()
169{
170	atf_set "descr" "Testing specific address family with prefer_v6"
171	atf_set "require.user" "root"
172}
173specific_body()
174{
175	vnet_mkjail specific prefer_ipv6
176	TEST="jexec getaddrinfo_test_specific $(atf_get_srcdir)/h_gai"
177
178	( $TEST -4 localhost http
179	  $TEST -6 localhost http ) > out 2>&1
180
181	check_output spec_fam hosts prefer_v6
182}
183specific_cleanup()
184{
185	vnet_cleanup specific
186}
187
188atf_test_case empty_hostname_prefer_v4 cleanup
189empty_hostname_prefer_v4_head()
190{
191	atf_set "descr" "Testing empty hostname with prefer_v4"
192	atf_set "require.user" "root"
193}
194empty_hostname_prefer_v4_body()
195{
196	vnet_mkjail empty_hostname_prefer_v4 prefer_ipv4
197	TEST="jexec getaddrinfo_test_empty_hostname_prefer_v4 $(atf_get_srcdir)/h_gai"
198
199	( $TEST '' http
200	  $TEST '' echo
201	  $TEST '' tftp
202	  $TEST '' 80
203	  $TEST -P '' http
204	  $TEST -P '' echo
205	  $TEST -P '' tftp
206	  $TEST -P '' 80
207	  $TEST -S '' 80
208	  $TEST -D '' 80 ) > out 2>&1
209
210	check_output no_host ifconfig prefer_v4
211}
212empty_hostname_prefer_v4_cleanup()
213{
214	vnet_cleanup empty_hostname_prefer_v4
215}
216
217atf_test_case empty_hostname cleanup
218empty_hostname_head()
219{
220	atf_set "descr" "Testing empty hostname with prefer_v6"
221	atf_set "require.user" "root"
222}
223empty_hostname_body()
224{
225	vnet_mkjail empty_hostname prefer_ipv6
226	TEST="jexec getaddrinfo_test_empty_hostname $(atf_get_srcdir)/h_gai"
227
228	( $TEST '' http
229	  $TEST '' echo
230	  $TEST '' tftp
231	  $TEST '' 80
232	  $TEST -P '' http
233	  $TEST -P '' echo
234	  $TEST -P '' tftp
235	  $TEST -P '' 80
236	  $TEST -S '' 80
237	  $TEST -D '' 80 ) > out 2>&1
238
239	check_output no_host ifconfig prefer_v6
240}
241empty_hostname_cleanup()
242{
243	vnet_cleanup empty_hostname
244}
245
246atf_test_case empty_servname_prefer_v4 cleanup
247empty_servname_prefer_v4_head()
248{
249	atf_set "descr" "Testing empty service name with prefer_v4"
250	atf_set "require.user" "root"
251}
252empty_servname_prefer_v4_body()
253{
254	vnet_mkjail empty_servname_prefer_v4 prefer_ipv4
255	TEST="jexec getaddrinfo_test_empty_servname_prefer_v4 $(atf_get_srcdir)/h_gai"
256
257	( $TEST ::1 ''
258	  $TEST 127.0.0.1 ''
259	  $TEST localhost ''
260	  $TEST '' '' ) > out 2>&1
261
262	check_output no_serv hosts prefer_v4
263}
264empty_servname_prefer_v4_cleanup()
265{
266	vnet_cleanup empty_servname_prefer_v4
267}
268
269atf_test_case empty_servname cleanup
270empty_servname_head()
271{
272	atf_set "descr" "Testing empty service name with prefer_v6"
273	atf_set "require.user" "root"
274}
275empty_servname_body()
276{
277	vnet_mkjail empty_servname prefer_ipv6
278	TEST="jexec getaddrinfo_test_empty_servname $(atf_get_srcdir)/h_gai"
279
280	( $TEST ::1 ''
281	  $TEST 127.0.0.1 ''
282	  $TEST localhost ''
283	  $TEST '' '' ) > out 2>&1
284
285	check_output no_serv ifconfig prefer_v6
286}
287empty_servname_cleanup()
288{
289	vnet_cleanup empty_servname
290}
291
292atf_test_case sock_raw_prefer_v4 cleanup
293sock_raw_prefer_v4_head()
294{
295	atf_set "descr" "Testing raw socket with prefer_v4"
296	atf_set "require.user" "root"
297}
298sock_raw_prefer_v4_body()
299{
300	vnet_mkjail sock_raw_prefer_v4 prefer_ipv4
301	TEST="jexec getaddrinfo_test_sock_raw_prefer_v4 $(atf_get_srcdir)/h_gai"
302
303	( $TEST -R -p 0 localhost ''
304	  $TEST -R -p 59 localhost ''
305	  $TEST -R -p 59 localhost 80
306	  $TEST -R -p 59 localhost www
307	  $TEST -R -p 59 ::1 '' ) > out 2>&1
308
309	check_output sock_raw hosts prefer_v4
310}
311sock_raw_prefer_v4_cleanup()
312{
313	vnet_cleanup sock_raw_prefer_v4
314}
315
316atf_test_case sock_raw cleanup
317sock_raw_head()
318{
319	atf_set "descr" "Testing raw socket with prefer_v6"
320	atf_set "require.user" "root"
321}
322sock_raw_body()
323{
324	vnet_mkjail sock_raw prefer_ipv6
325	TEST="jexec getaddrinfo_test_sock_raw $(atf_get_srcdir)/h_gai"
326
327	( $TEST -R -p 0 localhost ''
328	  $TEST -R -p 59 localhost ''
329	  $TEST -R -p 59 localhost 80
330	  $TEST -R -p 59 localhost www
331	  $TEST -R -p 59 ::1 '' ) > out 2>&1
332
333	check_output sock_raw ifconfig prefer_v6
334}
335sock_raw_cleanup()
336{
337	vnet_cleanup sock_raw
338}
339
340atf_test_case unsupported_family_prefer_v4 cleanup
341unsupported_family_prefer_v4_head()
342{
343	atf_set "descr" "Testing unsupported family with prefer_v4"
344	atf_set "require.user" "root"
345}
346unsupported_family_prefer_v4_body()
347{
348	vnet_mkjail unsupported_family_prefer_v4 prefer_ipv4
349	TEST="jexec getaddrinfo_test_unsupported_family_prefer_v4 $(atf_get_srcdir)/h_gai"
350
351	( $TEST -f 99 localhost '' ) > out 2>&1
352
353	check_output unsup_fam ifconfig prefer_v4
354}
355unsupported_family_prefer_v4_cleanup()
356{
357	vnet_cleanup unsupported_family_prefer_v4
358}
359
360atf_test_case unsupported_family cleanup
361unsupported_family_head()
362{
363	atf_set "descr" "Testing unsupported family with prefer_v6"
364	atf_set "require.user" "root"
365}
366unsupported_family_body()
367{
368	vnet_mkjail unsupported_family prefer_ipv6
369	TEST="jexec getaddrinfo_test_unsupported_family $(atf_get_srcdir)/h_gai"
370
371	( $TEST -f 99 localhost '' ) > out 2>&1
372
373	check_output unsup_fam none prefer_v6
374}
375unsupported_family_cleanup()
376{
377	vnet_cleanup unsupported_family
378}
379
380atf_test_case scopeaddr_prefer_v4 cleanup
381scopeaddr_prefer_v4_head()
382{
383	atf_set "descr" "Testing scoped address format with prefer_v4"
384	atf_set "require.user" "root"
385}
386scopeaddr_prefer_v4_body()
387{
388	vnet_mkjail scopeaddr_prefer_v4 prefer_ipv4
389	TEST="jexec getaddrinfo_test_scopeaddr_prefer_v4 $(atf_get_srcdir)/h_gai"
390
391	( $TEST fe80::1%lo0 http
392#	 IF=ifconfig -a | grep -v '^  ' | sed -e 's/:.*//' | head -1 | awk '{print $1}'
393#	 $TEST fe80::1%$IF http
394	) > out 2>&1
395
396	check_output scoped ifconfig prefer_v4
397}
398scopeaddr_prefer_v4_cleanup()
399{
400	vnet_cleanup scopeaddr_prefer_v4
401}
402
403atf_test_case scopeaddr cleanup
404scopeaddr_head()
405{
406	atf_set "descr" "Testing scoped address format with prefer_v6"
407	atf_set "require.user" "root"
408}
409scopeaddr_body()
410{
411	vnet_mkjail scopeaddr prefer_ipv6
412	TEST="jexec getaddrinfo_test_scopeaddr $(atf_get_srcdir)/h_gai"
413
414	( $TEST fe80::1%lo0 http
415#	 IF=ifconfig -a | grep -v '^  ' | sed -e 's/:.*//' | head -1 | awk '{print $1}'
416#	 $TEST fe80::1%$IF http
417	) > out 2>&1
418
419	check_output scoped none prefer_v6
420}
421scopeaddr_cleanup()
422{
423	vnet_cleanup scopeaddr
424}
425
426atf_init_test_cases()
427{
428	atf_add_test_case basic_prefer_v4
429	atf_add_test_case specific_prefer_v4
430	atf_add_test_case empty_hostname_prefer_v4
431	atf_add_test_case empty_servname_prefer_v4
432	atf_add_test_case sock_raw_prefer_v4
433	atf_add_test_case unsupported_family_prefer_v4
434	atf_add_test_case scopeaddr_prefer_v4
435
436	atf_add_test_case basic
437	atf_add_test_case specific
438	atf_add_test_case empty_hostname
439	atf_add_test_case empty_servname
440	atf_add_test_case sock_raw
441	atf_add_test_case unsupported_family
442	atf_add_test_case scopeaddr
443}
444