xref: /netbsd-src/tests/lib/librumphijack/t_tcpip.sh (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1#       $NetBSD: t_tcpip.sh,v 1.13 2014/01/03 13:18:00 pooka Exp $
2#
3# Copyright (c) 2011 The NetBSD Foundation, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25# POSSIBILITY OF SUCH DAMAGE.
26#
27
28rumpnetsrv='rump_server -lrumpnet -lrumpnet_net -lrumpnet_netinet'
29export RUMP_SERVER=unix://csock
30
31atf_test_case http cleanup
32http_head()
33{
34        atf_set "descr" "Start hijacked httpd and get webpage from it"
35}
36
37http_body()
38{
39
40	atf_check -s exit:0 ${rumpnetsrv} -lrumpnet_netinet6 ${RUMP_SERVER}
41
42	# start bozo in daemon mode
43	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so \
44	    /usr/libexec/httpd -P ./httpd.pid -b -s $(atf_get_srcdir)
45
46	atf_check -s exit:0 -o file:"$(atf_get_srcdir)/netstat.expout" \
47	    rump.netstat -a
48
49	# get the webpage
50	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so 	\
51	    $(atf_get_srcdir)/h_netget 127.0.0.1 80 webfile
52
53	# check that we got what we wanted
54	atf_check -o match:'HTTP/1.0 200 OK' cat webfile
55	atf_check -o match:'Content-Length: 95' cat webfile
56	atf_check -o file:"$(atf_get_srcdir)/index.html" \
57	    sed -n '1,/^
58$/!p' webfile
59}
60
61http_cleanup()
62{
63	if [ -f httpd.pid ]; then
64		kill -9 "$(cat httpd.pid)"
65		rm -f httpd.pid
66	fi
67
68	rump.halt
69}
70
71#
72# Starts a SSH server and sets up the client to access it.
73# Authentication is allowed and done using an RSA key exclusively, which
74# is generated on the fly as part of the test case.
75# XXX: Ideally, all the tests in this test program should be able to share
76# the generated key, because creating it can be a very slow process on some
77# machines.
78#
79# XXX2: copypasted from jmmv's sshd thingamob in the psshfs test.
80# ideally code (and keys, like jmmv notes above) could be shared
81#
82start_sshd() {
83	echo "Setting up SSH server configuration"
84	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
85	    $(atf_get_srcdir)/sshd_config.in >sshd_config || \
86	    atf_fail "Failed to create sshd_config"
87	atf_check -s ignore -o empty -e ignore \
88	    cp $(atf_get_srcdir)/ssh_host_key .
89	atf_check -s ignore -o empty -e ignore \
90	    cp $(atf_get_srcdir)/ssh_host_key.pub .
91	atf_check -s eq:0 -o empty -e empty chmod 400 ssh_host_key
92	atf_check -s eq:0 -o empty -e empty chmod 444 ssh_host_key.pub
93
94        env LD_PRELOAD=/usr/lib/librumphijack.so \
95	    /usr/sbin/sshd -e -f ./sshd_config
96	while [ ! -f sshd.pid ]; do
97		sleep 0.01
98	done
99	echo "SSH server started (pid $(cat sshd.pid))"
100
101	echo "Setting up SSH client configuration"
102	atf_check -s eq:0 -o empty -e empty \
103	    ssh-keygen -f ssh_user_key -t rsa -b 1024 -N "" -q
104	atf_check -s eq:0 -o empty -e empty \
105	    cp ssh_user_key.pub authorized_keys
106	echo "127.0.0.1,localhost,::1 " \
107	    "$(cat $(atf_get_srcdir)/ssh_host_key.pub)" >known_hosts || \
108	    atf_fail "Failed to create known_hosts"
109	atf_check -s eq:0 -o empty -e empty chmod 600 authorized_keys
110	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
111	    $(atf_get_srcdir)/ssh_config.in >ssh_config || \
112	    atf_fail "Failed to create ssh_config"
113
114	echo "sshd running"
115}
116
117atf_test_case ssh cleanup
118ssh_head()
119{
120        atf_set "descr" "Test that hijacked ssh/sshd works"
121}
122
123ssh_body()
124{
125
126	atf_check -s exit:0 ${rumpnetsrv} ${RUMP_SERVER}
127	# make sure clients die after we nuke the server
128	export RUMPHIJACK_RETRYCONNECT='die'
129
130	start_sshd
131
132	# create some sort of directory for us to "ls"
133	mkdir testdir
134	cd testdir
135	jot 11 | xargs touch
136	jot 11 12 | xargs mkdir
137	cd ..
138
139	atf_check -s exit:0 -o save:ssh.out				\
140	    env LD_PRELOAD=/usr/lib/librumphijack.so			\
141	    ssh -T -F ssh_config 127.0.0.1 env BLOCKSIZE=512		\
142	    ls -li $(pwd)/testdir
143	atf_check -s exit:0 -o file:ssh.out env BLOCKSIZE=512 		\
144	    ls -li $(pwd)/testdir
145}
146
147ssh_cleanup()
148{
149	rump.halt
150	# sshd dies due to RUMPHIJACK_RETRYCONNECT=1d6
151}
152
153test_nfs()
154{
155
156	magicstr='wind in my hair'
157	# create ffs file system we'll be serving from
158	atf_check -s exit:0 -o ignore newfs -F -s 10000 ffs.img
159
160	# start nfs kernel server.  this is a mouthful
161	export RUMP_SERVER=unix://serversock
162	atf_check -s exit:0 rump_server $* ${RUMP_SERVER}
163
164	atf_check -s exit:0 rump.ifconfig shmif0 create
165	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
166	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.1
167
168	export RUMPHIJACK_RETRYCONNECT=die
169	export LD_PRELOAD=/usr/lib/librumphijack.so
170
171	atf_check -s exit:0 mkdir -p /rump/var/run
172	atf_check -s exit:0 mkdir -p /rump/var/db
173	atf_check -s exit:0 touch /rump/var/db/mountdtab
174	atf_check -s exit:0 mkdir /rump/etc
175	atf_check -s exit:0 mkdir /rump/export
176
177	atf_check -s exit:0 -x \
178	    'echo "/export -noresvport -noresvmnt 10.1.1.100" | \
179		dd of=/rump/etc/exports 2> /dev/null'
180
181	atf_check -s exit:0 -e ignore mount_ffs /dk /rump/export
182	atf_check -s exit:0 -x "echo ${magicstr} > /rump/export/im_alive"
183
184	# start rpcbind.  we want /var/run/rpcbind.sock
185	export RUMPHIJACK='blanket=/var/run,socket=all'
186	atf_check -s exit:0 rpcbind
187
188	# ok, then we want mountd in the similar fashion
189	export RUMPHIJACK='blanket=/var/run:/var/db:/export,socket=all,path=/rump,vfs=all'
190	atf_check -s exit:0 mountd /rump/etc/exports
191
192	# finally, le nfschuck
193	export RUMPHIJACK='blanket=/var/run,socket=all,vfs=all'
194	atf_check -s exit:0 nfsd
195
196	#
197	# now, time for the client server and associated madness.
198	#
199
200	export RUMP_SERVER=unix://clientsock
201	unset RUMPHIJACK
202	unset LD_PRELOAD
203
204	# at least the kernel server is easier
205	atf_check -s exit:0 rump_server -lrumpvfs -lrumpnet		\
206	    -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif -lrumpfs_nfs\
207	    ${RUMP_SERVER}
208
209	atf_check -s exit:0 rump.ifconfig shmif0 create
210	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
211	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.100
212
213	export LD_PRELOAD=/usr/lib/librumphijack.so
214
215	atf_check -s exit:0 mkdir /rump/mnt
216	atf_check -s exit:0 mount_nfs 10.1.1.1:/export /rump/mnt
217
218	atf_check -s exit:0 -o inline:"${magicstr}\n" cat /rump/mnt/im_alive
219	atf_check -s exit:0 -o match:'.*im_alive$' ls -l /rump/mnt/im_alive
220}
221
222
223atf_test_case nfs cleanup
224nfs_head()
225{
226        atf_set "descr" "Test hijacked nfsd and mount_nfs"
227}
228
229nfs_body()
230{
231	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
232	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif		\
233	    -lrumpdev_disk -lrumpfs_ffs -lrumpfs_nfs -lrumpfs_nfsserver	\
234	    -d key=/dk,hostpath=ffs.img,size=host
235}
236
237nfs_cleanup()
238{
239	RUMP_SERVER=unix://serversock rump.halt 2> /dev/null
240	RUMP_SERVER=unix://clientsock rump.halt 2> /dev/null
241	:
242}
243
244atf_test_case nfs_autoload cleanup
245nfs_autoload_head()
246{
247        atf_set "descr" "Test hijacked nfsd with autoload from /stand"
248}
249
250nfs_autoload_body()
251{
252	[ `uname -m` = "i386" ] || atf_skip "test currently valid only on i386"
253	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
254	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif		\
255	    -lrumpdev_disk -d key=/dk,hostpath=ffs.img,size=host
256}
257
258nfs_autoload_cleanup()
259{
260	nfs_cleanup
261}
262
263atf_init_test_cases()
264{
265	atf_add_test_case http
266	atf_add_test_case ssh
267	atf_add_test_case nfs
268	atf_add_test_case nfs_autoload
269}
270