1272343Sngie# $NetBSD: t_hostent.sh,v 1.10 2014/01/13 11:08:14 gson Exp $
2272343Sngie#
3272343Sngie# Copyright (c) 2008 The NetBSD Foundation, Inc.
4272343Sngie# All rights reserved.
5272343Sngie#
6272343Sngie# Redistribution and use in source and binary forms, with or without
7272343Sngie# modification, are permitted provided that the following conditions
8272343Sngie# are met:
9272343Sngie# 1. Redistributions of source code must retain the above copyright
10272343Sngie#    notice, this list of conditions and the following disclaimer.
11272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie#    notice, this list of conditions and the following disclaimer in the
13272343Sngie#    documentation and/or other materials provided with the distribution.
14272343Sngie#
15272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25272343Sngie# POSSIBILITY OF SUCH DAMAGE.
26272343Sngie#
27272343Sngie
28272343Sngien6="sixthavenue.astron.com"
29272343Sngiea6="2620:106:3003:1f00:3e4a:92ff:fef4:e180"
30272343Sngieans6="name=$n6, length=16, addrtype=24, aliases=[] addr_list=[$a6]\n"
31272343Sngie
32272343Sngien4="sixthavenue.astron.com"
33272343Sngiea4="38.117.134.16"
34272343Sngieans4="name=$n4, length=4, addrtype=2, aliases=[] addr_list=[$a4]\n"
35272343Sngie
36272343Sngiel6="localhost"
37272343Sngieal6="::1"
38272343Sngieloc6="name=$l6, length=16, addrtype=24, aliases=[localhost. localhost.localdomain.] addr_list=[$al6]\n"
39272343Sngie
40272343Sngiel4="localhost"
41272343Sngieal4="127.0.0.1"
42272343Sngieloc4="name=$l4, length=4, addrtype=2, aliases=[localhost. localhost.localdomain.] addr_list=[$al4]\n"
43272343Sngie
44272343Sngiedir="$(atf_get_srcdir)"
45272343Sngieres="-r ${dir}/resolv.conf"
46272343Sngie
47272343Sngie# Hijack DNS traffic using a single rump server instance and a DNS
48272343Sngie# server listening on its loopback address.
49272343Sngie
50272343Sngiestart_dns_server() {
51272343Sngie	export RUMP_SERVER=unix:///tmp/rumpserver
52272343Sngie	rump_server -lrumpdev -lrumpnet -lrumpnet_net -lrumpnet_netinet \
53272343Sngie	    -lrumpnet_netinet6 -lrumpnet_local $RUMP_SERVER
54272343Sngie	HIJACK_DNS="LD_PRELOAD=/usr/lib/librumphijack.so RUMPHIJACK='socket=inet:inet6'"
55272343Sngie	eval $HIJACK_DNS ${dir}/h_dns_server $1
56272343Sngie}
57272343Sngie
58272343Sngiestop_dns_server() {
59272343Sngie	export RUMP_SERVER=unix:///tmp/rumpserver
60272343Sngie	kill $(cat dns_server_$1.pid)
61272343Sngie	rump.halt
62272343Sngie}
63272343Sngie
64272343Sngieatf_test_case gethostbyname4 cleanup
65272343Sngiegethostbyname4_head()
66272343Sngie{
67272343Sngie	atf_set "descr" "Checks gethostbyname2(3) for AF_INET (auto, as determined by nsswitch.conf(5)"
68272343Sngie}
69272343Sngiegethostbyname4_body()
70272343Sngie{
71272343Sngie	start_dns_server 4
72272343Sngie	atf_check -o inline:"$ans4" -x "$HIJACK_DNS ${dir}/h_hostent ${res} -t auto -4 $n4"
73272343Sngie}
74272343Sngiegethostbyname4_cleanup()
75272343Sngie{
76272343Sngie	stop_dns_server 4
77272343Sngie}
78272343Sngie
79272343Sngieatf_test_case gethostbyname6 cleanup cleanup
80272343Sngiegethostbyname6_head()
81272343Sngie{
82272343Sngie	atf_set "descr" "Checks gethostbyname2(3) for AF_INET6 (auto, as determined by nsswitch.conf(5)"
83272343Sngie}
84272343Sngiegethostbyname6_body()
85272343Sngie{
86272343Sngie	start_dns_server 4
87272343Sngie	atf_check -o inline:"$ans6" -x "$HIJACK_DNS ${dir}/h_hostent ${res} -t auto -6 $n6"
88272343Sngie}
89272343Sngiegethostbyname6_cleanup()
90272343Sngie{
91272343Sngie	stop_dns_server 4
92272343Sngie}
93272343Sngie
94272343Sngieatf_test_case gethostbyaddr4 cleanup
95272343Sngiegethostbyaddr4_head()
96272343Sngie{
97272343Sngie	atf_set "descr" "Checks gethostbyaddr(3) for AF_INET (auto, as determined by nsswitch.conf(5)"
98272343Sngie}
99272343Sngiegethostbyaddr4_body()
100272343Sngie{
101272343Sngie	start_dns_server 4
102272343Sngie        atf_check -o inline:"$ans4" -x "$HIJACK_DNS ${dir}/h_hostent ${res} -t auto -a $a4"
103272343Sngie}
104272343Sngiegethostbyaddr4_cleanup()
105272343Sngie{
106272343Sngie	stop_dns_server 4
107272343Sngie}
108272343Sngie
109272343Sngieatf_test_case gethostbyaddr6 cleanup
110272343Sngiegethostbyaddr6_head()
111272343Sngie{
112272343Sngie	atf_set "descr" "Checks gethostbyaddr(3) for AF_INET6 (auto, as determined by nsswitch.conf(5)"
113272343Sngie}
114272343Sngiegethostbyaddr6_body()
115272343Sngie{
116272343Sngie	start_dns_server 4
117272343Sngie	atf_check -o inline:"$ans6" -x "$HIJACK_DNS ${dir}/h_hostent ${res} -t auto -a $a6"
118272343Sngie}
119272343Sngiegethostbyaddr6_cleanup()
120272343Sngie{
121272343Sngie	stop_dns_server 4
122272343Sngie}
123272343Sngie
124272343Sngieatf_test_case hostsbynamelookup4
125272343Sngiehostsbynamelookup4_head()
126272343Sngie{
127272343Sngie	atf_set "descr" "Checks /etc/hosts name lookup for AF_INET"
128272343Sngie}
129272343Sngiehostsbynamelookup4_body()
130272343Sngie{
131272343Sngie	atf_check -o inline:"$loc4" -x "${dir}/h_hostent -f ${dir}/hosts -t file -4 $l4"
132272343Sngie}
133272343Sngie
134272343Sngieatf_test_case hostsbynamelookup6
135272343Sngiehostsbynamelookup6_head()
136272343Sngie{
137272343Sngie	atf_set "descr" "Checks /etc/hosts name lookup for AF_INET6"
138272343Sngie}
139272343Sngiehostsbynamelookup6_body()
140272343Sngie{
141272343Sngie	atf_check -o inline:"$loc6" -x "${dir}/h_hostent -f ${dir}/hosts -t file -6 $l6"
142272343Sngie}
143272343Sngie
144272343Sngieatf_test_case hostsbyaddrlookup4
145272343Sngiehostsbyaddrlookup4_head()
146272343Sngie{
147272343Sngie	atf_set "descr" "Checks /etc/hosts address lookup for AF_INET"
148272343Sngie}
149272343Sngiehostsbyaddrlookup4_body()
150272343Sngie{
151272343Sngie	atf_check -o inline:"$loc4" -x "${dir}/h_hostent -f ${dir}/hosts -t file -4 -a $al4"
152272343Sngie}
153272343Sngie
154272343Sngieatf_test_case hostsbyaddrlookup6
155272343Sngiehostsbyaddrlookup6_head()
156272343Sngie{
157272343Sngie	atf_set "descr" "Checks /etc/hosts address lookup for AF_INET6"
158272343Sngie}
159272343Sngiehostsbyaddrlookup6_body()
160272343Sngie{
161272343Sngie	atf_check -o inline:"$loc6" -x "${dir}/h_hostent -f ${dir}/hosts -t file -6 -a $al6"
162272343Sngie}
163272343Sngie
164272343Sngieatf_test_case dnsbynamelookup4 cleanup
165272343Sngiednsbynamelookup4_head()
166272343Sngie{
167272343Sngie	atf_set "descr" "Checks DNS name lookup for AF_INET"
168272343Sngie}
169272343Sngiednsbynamelookup4_body()
170272343Sngie{
171272343Sngie	start_dns_server 4
172272343Sngie	atf_check -o inline:"$ans4" -x "$HIJACK_DNS ${dir}/h_hostent ${res} -t dns -4 $n4"
173272343Sngie}
174272343Sngiednsbynamelookup4_cleanup()
175272343Sngie{
176272343Sngie	stop_dns_server 4
177272343Sngie}
178272343Sngie
179272343Sngieatf_test_case dnsbynamelookup6 cleanup
180272343Sngiednsbynamelookup6_head()
181272343Sngie{
182272343Sngie	atf_set "descr" "Checks DNS name lookup for AF_INET6"
183272343Sngie}
184272343Sngiednsbynamelookup6_body()
185272343Sngie{
186272343Sngie	start_dns_server 4
187272343Sngie	atf_check -o inline:"$ans6" -x "$HIJACK_DNS ${dir}/h_hostent ${res} -t dns -6 $n6"
188272343Sngie}
189272343Sngiednsbynamelookup6_cleanup()
190272343Sngie{
191272343Sngie	stop_dns_server 4
192272343Sngie}
193272343Sngie
194272343Sngieatf_test_case dnsbyaddrlookup4 cleanup
195272343Sngiednsbyaddrlookup4_head()
196272343Sngie{
197272343Sngie	atf_set "descr" "Checks DNS address lookup for AF_INET"
198272343Sngie}
199272343Sngiednsbyaddrlookup4_body()
200272343Sngie{
201272343Sngie	start_dns_server 4
202272343Sngie	atf_check -o inline:"$ans4" -x "$HIJACK_DNS ${dir}/h_hostent ${res} -t dns -4 -a $a4"
203272343Sngie}
204272343Sngiednsbyaddrlookup4_cleanup()
205272343Sngie{
206272343Sngie	stop_dns_server 4
207272343Sngie}
208272343Sngie
209272343Sngieatf_test_case dnsbyaddrlookup6 cleanup
210272343Sngiednsbyaddrlookup6_head()
211272343Sngie{
212272343Sngie	atf_set "descr" "Checks dns address lookup for AF_INET6"
213272343Sngie}
214272343Sngiednsbyaddrlookup6_body()
215272343Sngie{
216272343Sngie	start_dns_server 4
217272343Sngie	atf_check -o inline:"$ans6" -x "$HIJACK_DNS ${dir}/h_hostent ${res} -t dns -6 -a $a6"
218272343Sngie}
219272343Sngiednsbyaddrlookup6_cleanup()
220272343Sngie{
221272343Sngie	stop_dns_server 4
222272343Sngie}
223272343Sngie
224272343Sngieatf_init_test_cases()
225272343Sngie{
226272343Sngie	atf_add_test_case gethostbyname4
227272343Sngie	atf_add_test_case gethostbyname6
228272343Sngie	atf_add_test_case gethostbyaddr4
229272343Sngie	atf_add_test_case gethostbyaddr6
230272343Sngie
231272343Sngie	atf_add_test_case hostsbynamelookup4
232272343Sngie	atf_add_test_case hostsbynamelookup6
233272343Sngie	atf_add_test_case hostsbyaddrlookup4
234272343Sngie	atf_add_test_case hostsbyaddrlookup6
235272343Sngie
236272343Sngie	atf_add_test_case dnsbynamelookup4
237272343Sngie	atf_add_test_case dnsbynamelookup6
238272343Sngie	atf_add_test_case dnsbyaddrlookup4
239272343Sngie	atf_add_test_case dnsbyaddrlookup6
240272343Sngie}
241