summaryrefslogtreecommitdiffstats
path: root/contrib/netbsd-tests/lib/librumphijack/t_tcpip.sh
blob: 411be3340bbcfb3a5a287534593a202b6ab73082 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#       $NetBSD: t_tcpip.sh,v 1.18 2016/08/13 11:22:11 christos Exp $
#
# Copyright (c) 2011 The NetBSD Foundation, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

rumpnetlibs="-lrumpnet -lrumpnet_net -lrumpnet_netinet6 -lrumpnet_netinet"
rumpnetsrv="rump_server $rumpnetlibs -lrumpdev"
export RUMP_SERVER=unix://csock

atf_test_case http cleanup
http_head()
{
        atf_set "descr" "Start hijacked httpd and get webpage from it"
}

http_body()
{

	atf_check -s exit:0 ${rumpnetsrv} ${RUMP_SERVER}

	# start bozo in daemon mode
	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so \
	    /usr/libexec/httpd -P ./httpd.pid -b -s $(atf_get_srcdir)

	atf_check -s exit:0 -o file:"$(atf_get_srcdir)/netstat.expout" \
	    rump.netstat -a

	# get the webpage
	atf_check -s exit:0 env LD_PRELOAD=/usr/lib/librumphijack.so 	\
	    $(atf_get_srcdir)/h_netget 127.0.0.1 80 webfile

	# check that we got what we wanted
	atf_check -o match:'HTTP/1.0 200 OK' cat webfile
	atf_check -o match:'Content-Length: 95' cat webfile
	blank_line_re="$(printf '^\r$')" # matches a line with only <CR><LF>
	atf_check -o file:"$(atf_get_srcdir)/index.html" \
	    sed -n "1,/${blank_line_re}/!p" webfile
}

http_cleanup()
{
	if [ -f httpd.pid ]; then
		kill -9 "$(cat httpd.pid)"
		rm -f httpd.pid
	fi

	rump.halt
}

#
# Starts a SSH server and sets up the client to access it.
# Authentication is allowed and done using an RSA key exclusively, which
# is generated on the fly as part of the test case.
# XXX: Ideally, all the tests in this test program should be able to share
# the generated key, because creating it can be a very slow process on some
# machines.
#
# XXX2: copypasted from jmmv's sshd thingamob in the psshfs test.
# ideally code (and keys, like jmmv notes above) could be shared
#
start_sshd() {
	echo "Setting up SSH server configuration"
	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
	    $(atf_get_srcdir)/sshd_config.in >sshd_config || \
	    atf_fail "Failed to create sshd_config"
	atf_check -s ignore -o empty -e ignore \
	    cp $(atf_get_srcdir)/ssh_host_key .
	atf_check -s ignore -o empty -e ignore \
	    cp $(atf_get_srcdir)/ssh_host_key.pub .
	atf_check -s eq:0 -o empty -e empty chmod 400 ssh_host_key
	atf_check -s eq:0 -o empty -e empty chmod 444 ssh_host_key.pub

        env LD_PRELOAD=/usr/lib/librumphijack.so \
	    /usr/sbin/sshd -e -f ./sshd_config
	while [ ! -f sshd.pid ]; do
		sleep 0.01
	done
	echo "SSH server started (pid $(cat sshd.pid))"

	echo "Setting up SSH client configuration"
	atf_check -s eq:0 -o empty -e empty \
	    ssh-keygen -f ssh_user_key -t rsa -b 1024 -N "" -q
	atf_check -s eq:0 -o empty -e empty \
	    cp ssh_user_key.pub authorized_keys
	echo "127.0.0.1,localhost,::1 " \
	    "$(cat $(atf_get_srcdir)/ssh_host_key.pub)" >known_hosts || \
	    atf_fail "Failed to create known_hosts"
	atf_check -s eq:0 -o empty -e empty chmod 600 authorized_keys
	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
	    $(atf_get_srcdir)/ssh_config.in >ssh_config || \
	    atf_fail "Failed to create ssh_config"
	
	echo "sshd running"
}

atf_test_case ssh cleanup
ssh_head()
{
        atf_set "descr" "Test that hijacked ssh/sshd works"
}

ssh_body()
{
	atf_expect_fail "PR lib/50174"

	atf_check -s exit:0 ${rumpnetsrv} ${RUMP_SERVER}
	# make sure clients die after we nuke the server
	export RUMPHIJACK_RETRYCONNECT='die'

	start_sshd

	# create some sort of directory for us to "ls"
	mkdir testdir
	cd testdir
	jot 11 | xargs touch
	jot 11 12 | xargs mkdir
	cd ..

	atf_check -s exit:0 -o save:ssh.out				\
	    env LD_PRELOAD=/usr/lib/librumphijack.so			\
	    ssh -T -F ssh_config 127.0.0.1 env BLOCKSIZE=512		\
	    ls -li $(pwd)/testdir
	atf_check -s exit:0 -o file:ssh.out env BLOCKSIZE=512 		\
	    ls -li $(pwd)/testdir
}

ssh_cleanup()
{
	rump.halt
	# sshd dies due to RUMPHIJACK_RETRYCONNECT=1d6
}

test_nfs()
{

	magicstr='wind in my hair'
	# create ffs file system we'll be serving from
	atf_check -s exit:0 -o ignore newfs -F -s 10000 ffs.img

	# start nfs kernel server.  this is a mouthful
	export RUMP_SERVER=unix://serversock
	atf_check -s exit:0 rump_server $* ${RUMP_SERVER}

	atf_check -s exit:0 rump.ifconfig shmif0 create
	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.1

	export RUMPHIJACK_RETRYCONNECT=die
	export LD_PRELOAD=/usr/lib/librumphijack.so

	atf_check -s exit:0 mkdir -p /rump/var/run
	atf_check -s exit:0 mkdir -p /rump/var/db
	atf_check -s exit:0 touch /rump/var/db/mountdtab
	atf_check -s exit:0 mkdir /rump/etc
	atf_check -s exit:0 mkdir /rump/export

	atf_check -s exit:0 -x \
	    'echo "/export -noresvport -noresvmnt 10.1.1.100" | \
		dd of=/rump/etc/exports 2> /dev/null'

	atf_check -s exit:0 rump.sysctl -q -w kern.module.autoload=1

	atf_check -s exit:0 -e ignore mount_ffs /dk /rump/export
	atf_check -s exit:0 -x "echo ${magicstr} > /rump/export/im_alive"

	# start rpcbind.  we want /var/run/rpcbind.sock
	export RUMPHIJACK='blanket=/var/run,socket=all' 
	atf_check -s exit:0 rpcbind

	# ok, then we want mountd in the similar fashion
	export RUMPHIJACK='blanket=/var/run:/var/db:/export,socket=all,path=/rump,vfs=all'
	atf_check -s exit:0 mountd /rump/etc/exports

	# finally, le nfschuck
	export RUMPHIJACK='blanket=/var/run,socket=all,vfs=all'
	atf_check -s exit:0 nfsd

	#
	# now, time for the client server and associated madness.
	#

	export RUMP_SERVER=unix://clientsock
	unset RUMPHIJACK
	unset LD_PRELOAD

	# at least the kernel server is easier
	atf_check -s exit:0 rump_server -lrumpvfs -lrumpnet -lrumpdev	\
	    -lrumpnet_net -lrumpnet_netinet -lrumpnet_shmif -lrumpfs_nfs\
	    ${RUMP_SERVER}

	atf_check -s exit:0 rump.ifconfig shmif0 create
	atf_check -s exit:0 rump.ifconfig shmif0 linkstr shmbus
	atf_check -s exit:0 rump.ifconfig shmif0 inet 10.1.1.100

	export LD_PRELOAD=/usr/lib/librumphijack.so

	atf_check -s exit:0 mkdir /rump/mnt
	atf_check -s exit:0 mount_nfs 10.1.1.1:/export /rump/mnt

	atf_check -s exit:0 -o inline:"${magicstr}\n" cat /rump/mnt/im_alive
	atf_check -s exit:0 -o match:'.*im_alive$' ls -l /rump/mnt/im_alive
}


atf_test_case nfs cleanup
nfs_head()
{
        atf_set "descr" "Test hijacked nfsd and mount_nfs"
}

nfs_body()
{
	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif -lrumpdev	\
	    -lrumpdev_disk -lrumpfs_ffs -lrumpfs_nfs -lrumpfs_nfsserver	\
	    -d key=/dk,hostpath=ffs.img,size=host
}

nfs_cleanup()
{
	RUMP_SERVER=unix://serversock rump.halt 2> /dev/null
	RUMP_SERVER=unix://clientsock rump.halt 2> /dev/null
	:
}

atf_test_case nfs_autoload cleanup
nfs_autoload_head()
{
        atf_set "descr" "Test hijacked nfsd with autoload from /stand"
}

nfs_autoload_body()
{
	[ `uname -m` = "i386" ] || atf_skip "test currently valid only on i386"
	test_nfs -lrumpvfs -lrumpdev -lrumpnet -lrumpnet_net		\
	    -lrumpnet_netinet -lrumpnet_local -lrumpnet_shmif -lrumpdev	\
	    -lrumpdev_disk -d key=/dk,hostpath=ffs.img,size=host
}

nfs_autoload_cleanup()
{
	nfs_cleanup
}

atf_init_test_cases()
{
	atf_add_test_case http
	atf_add_test_case ssh
	atf_add_test_case nfs
	atf_add_test_case nfs_autoload
}
OpenPOWER on IntegriCloud