xref: /minix3/tests/fs/psshfs/t_psshfs.sh (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc# $NetBSD: t_psshfs.sh,v 1.7 2013/03/16 07:54:04 jmmv Exp $
2*11be35a1SLionel Sambuc#
3*11be35a1SLionel Sambuc# Copyright (c) 2007, 2008 The NetBSD Foundation, Inc.
4*11be35a1SLionel Sambuc# All rights reserved.
5*11be35a1SLionel Sambuc#
6*11be35a1SLionel Sambuc# Redistribution and use in source and binary forms, with or without
7*11be35a1SLionel Sambuc# modification, are permitted provided that the following conditions
8*11be35a1SLionel Sambuc# are met:
9*11be35a1SLionel Sambuc# 1. Redistributions of source code must retain the above copyright
10*11be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer.
11*11be35a1SLionel Sambuc# 2. Redistributions in binary form must reproduce the above copyright
12*11be35a1SLionel Sambuc#    notice, this list of conditions and the following disclaimer in the
13*11be35a1SLionel Sambuc#    documentation and/or other materials provided with the distribution.
14*11be35a1SLionel Sambuc#
15*11be35a1SLionel Sambuc# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16*11be35a1SLionel Sambuc# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17*11be35a1SLionel Sambuc# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18*11be35a1SLionel Sambuc# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19*11be35a1SLionel Sambuc# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20*11be35a1SLionel Sambuc# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21*11be35a1SLionel Sambuc# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22*11be35a1SLionel Sambuc# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23*11be35a1SLionel Sambuc# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24*11be35a1SLionel Sambuc# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25*11be35a1SLionel Sambuc# POSSIBILITY OF SUCH DAMAGE.
26*11be35a1SLionel Sambuc#
27*11be35a1SLionel Sambuc
28*11be35a1SLionel Sambuc# -------------------------------------------------------------------------
29*11be35a1SLionel Sambuc# Auxiliary functions.
30*11be35a1SLionel Sambuc# -------------------------------------------------------------------------
31*11be35a1SLionel Sambuc
32*11be35a1SLionel Sambuc#
33*11be35a1SLionel Sambuc# Skips the calling test case if puffs is not supported in the kernel
34*11be35a1SLionel Sambuc# or if the calling user does not have the necessary permissions to mount
35*11be35a1SLionel Sambuc# file systems.
36*11be35a1SLionel Sambuc#
37*11be35a1SLionel Sambucrequire_puffs() {
38*11be35a1SLionel Sambuc	case "$($(atf_get_srcdir)/h_have_puffs)" in
39*11be35a1SLionel Sambuc		eacces)
40*11be35a1SLionel Sambuc			atf_skip "Cannot open /dev/puffs for read/write access"
41*11be35a1SLionel Sambuc			;;
42*11be35a1SLionel Sambuc		enxio)
43*11be35a1SLionel Sambuc			atf_skip "puffs support not built into the kernel"
44*11be35a1SLionel Sambuc			;;
45*11be35a1SLionel Sambuc		failed)
46*11be35a1SLionel Sambuc			atf_skip "Unknown error trying to access /dev/puffs"
47*11be35a1SLionel Sambuc			;;
48*11be35a1SLionel Sambuc		yes)
49*11be35a1SLionel Sambuc			;;
50*11be35a1SLionel Sambuc		*)
51*11be35a1SLionel Sambuc			atf_fail "Unknown value returned by h_have_puffs"
52*11be35a1SLionel Sambuc			;;
53*11be35a1SLionel Sambuc	esac
54*11be35a1SLionel Sambuc
55*11be35a1SLionel Sambuc	if [ $(id -u) -ne 0 -a $(sysctl -n vfs.generic.usermount) -eq 0 ]
56*11be35a1SLionel Sambuc	then
57*11be35a1SLionel Sambuc		atf_skip "Regular users cannot mount file systems" \
58*11be35a1SLionel Sambuc		    "(vfs.generic.usermount is set to 0)"
59*11be35a1SLionel Sambuc	fi
60*11be35a1SLionel Sambuc}
61*11be35a1SLionel Sambuc
62*11be35a1SLionel Sambuc#
63*11be35a1SLionel Sambuc# Starts a SSH server and sets up the client to access it.
64*11be35a1SLionel Sambuc# Authentication is allowed and done using an RSA key exclusively, which
65*11be35a1SLionel Sambuc# is generated on the fly as part of the test case.
66*11be35a1SLionel Sambuc# XXX: Ideally, all the tests in this test program should be able to share
67*11be35a1SLionel Sambuc# the generated key, because creating it can be a very slow process on some
68*11be35a1SLionel Sambuc# machines.
69*11be35a1SLionel Sambuc#
70*11be35a1SLionel Sambucstart_ssh() {
71*11be35a1SLionel Sambuc	echo "Setting up SSH server configuration"
72*11be35a1SLionel Sambuc	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
73*11be35a1SLionel Sambuc	    $(atf_get_srcdir)/sshd_config.in >sshd_config || \
74*11be35a1SLionel Sambuc	    atf_fail "Failed to create sshd_config"
75*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty cp /usr/libexec/sftp-server .
76*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
77*11be35a1SLionel Sambuc	    cp $(atf_get_srcdir)/ssh_host_key .
78*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
79*11be35a1SLionel Sambuc	    cp $(atf_get_srcdir)/ssh_host_key.pub .
80*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty chmod 400 ssh_host_key
81*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty chmod 444 ssh_host_key.pub
82*11be35a1SLionel Sambuc
83*11be35a1SLionel Sambuc	/usr/sbin/sshd -e -f ./sshd_config >sshd.log 2>&1 &
84*11be35a1SLionel Sambuc	while [ ! -f sshd.pid ]; do
85*11be35a1SLionel Sambuc		sleep 0.01
86*11be35a1SLionel Sambuc	done
87*11be35a1SLionel Sambuc	echo "SSH server started (pid $(cat sshd.pid))"
88*11be35a1SLionel Sambuc
89*11be35a1SLionel Sambuc	echo "Setting up SSH client configuration"
90*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
91*11be35a1SLionel Sambuc	    ssh-keygen -f ssh_user_key -t rsa -b 1024 -N "" -q
92*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
93*11be35a1SLionel Sambuc	    cp ssh_user_key.pub authorized_keys
94*11be35a1SLionel Sambuc	echo "[localhost]:10000,[127.0.0.1]:10000,[::1]:10000" \
95*11be35a1SLionel Sambuc	    "$(cat $(atf_get_srcdir)/ssh_host_key.pub)" >known_hosts || \
96*11be35a1SLionel Sambuc	    atf_fail "Failed to create known_hosts"
97*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty chmod 600 authorized_keys
98*11be35a1SLionel Sambuc	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
99*11be35a1SLionel Sambuc	    $(atf_get_srcdir)/ssh_config.in >ssh_config || \
100*11be35a1SLionel Sambuc	    atf_fail "Failed to create ssh_config"
101*11be35a1SLionel Sambuc}
102*11be35a1SLionel Sambuc
103*11be35a1SLionel Sambuc#
104*11be35a1SLionel Sambuc# Stops the SSH server spawned by start_ssh and prints diagnosis data.
105*11be35a1SLionel Sambuc#
106*11be35a1SLionel Sambucstop_ssh() {
107*11be35a1SLionel Sambuc	if [ -f sshd.pid ]; then
108*11be35a1SLionel Sambuc		echo "Stopping SSH server (pid $(cat sshd.pid))"
109*11be35a1SLionel Sambuc		kill $(cat sshd.pid)
110*11be35a1SLionel Sambuc	fi
111*11be35a1SLionel Sambuc	if [ -f sshd.log ]; then
112*11be35a1SLionel Sambuc		echo "Server output was:"
113*11be35a1SLionel Sambuc		sed -e 's,^,    ,' sshd.log
114*11be35a1SLionel Sambuc	fi
115*11be35a1SLionel Sambuc}
116*11be35a1SLionel Sambuc
117*11be35a1SLionel Sambuc#
118*11be35a1SLionel Sambuc# Mounts the given source directory on the target directory using psshfs.
119*11be35a1SLionel Sambuc# Both directories are supposed to live on the current directory.
120*11be35a1SLionel Sambuc#
121*11be35a1SLionel Sambucmount_psshfs() {
122*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
123*11be35a1SLionel Sambuc	    mount -t psshfs -o -F=$(pwd)/ssh_config localhost:$(pwd)/${1} ${2}
124*11be35a1SLionel Sambuc}
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc# -------------------------------------------------------------------------
127*11be35a1SLionel Sambuc# The test cases.
128*11be35a1SLionel Sambuc# -------------------------------------------------------------------------
129*11be35a1SLionel Sambuc
130*11be35a1SLionel Sambucatf_test_case inode_nos cleanup
131*11be35a1SLionel Sambucinode_nos_head() {
132*11be35a1SLionel Sambuc	atf_set "descr" "Checks that different files get different inode" \
133*11be35a1SLionel Sambuc	    "numbers"
134*11be35a1SLionel Sambuc}
135*11be35a1SLionel Sambucinode_nos_body() {
136*11be35a1SLionel Sambuc	require_puffs
137*11be35a1SLionel Sambuc
138*11be35a1SLionel Sambuc	start_ssh
139*11be35a1SLionel Sambuc
140*11be35a1SLionel Sambuc	mkdir root
141*11be35a1SLionel Sambuc	mkdir root/dir
142*11be35a1SLionel Sambuc	touch root/dir/file1
143*11be35a1SLionel Sambuc	touch root/dir/file2
144*11be35a1SLionel Sambuc	touch root/file3
145*11be35a1SLionel Sambuc	touch root/file4
146*11be35a1SLionel Sambuc
147*11be35a1SLionel Sambuc	cat >ne_inodes.sh <<EOF
148*11be35a1SLionel Sambuc#! /bin/sh
149*11be35a1SLionel Sambuc#
150*11be35a1SLionel Sambuc# Compares the inodes of the two given files and returns true if they are
151*11be35a1SLionel Sambuc# different; false otherwise.
152*11be35a1SLionel Sambuc#
153*11be35a1SLionel Sambucset -e
154*11be35a1SLionel Sambucino1=\$(stat -f %i \${1})
155*11be35a1SLionel Sambucino2=\$(stat -f %i \${2})
156*11be35a1SLionel Sambuctest \${ino1} -ne \${ino2}
157*11be35a1SLionel SambucEOF
158*11be35a1SLionel Sambuc	chmod +x ne_inodes.sh
159*11be35a1SLionel Sambuc
160*11be35a1SLionel Sambuc	mkdir mnt
161*11be35a1SLionel Sambuc	mount_psshfs root mnt
162*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
163*11be35a1SLionel Sambuc	    ./ne_inodes.sh root/dir root/dir/file1
164*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
165*11be35a1SLionel Sambuc	    ./ne_inodes.sh root/dir root/dir/file2
166*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
167*11be35a1SLionel Sambuc	    ./ne_inodes.sh root/dir/file1 root/dir/file2
168*11be35a1SLionel Sambuc	atf_check -s eq:0 -o empty -e empty \
169*11be35a1SLionel Sambuc	    ./ne_inodes.sh root/file3 root/file4
170*11be35a1SLionel Sambuc}
171*11be35a1SLionel Sambucinode_nos_cleanup() {
172*11be35a1SLionel Sambuc	umount mnt
173*11be35a1SLionel Sambuc	stop_ssh
174*11be35a1SLionel Sambuc}
175*11be35a1SLionel Sambuc
176*11be35a1SLionel Sambucatf_test_case pwd cleanup
177*11be35a1SLionel Sambucpwd_head() {
178*11be35a1SLionel Sambuc	atf_set "descr" "Checks that pwd works correctly"
179*11be35a1SLionel Sambuc}
180*11be35a1SLionel Sambucpwd_body() {
181*11be35a1SLionel Sambuc	require_puffs
182*11be35a1SLionel Sambuc
183*11be35a1SLionel Sambuc	start_ssh
184*11be35a1SLionel Sambuc
185*11be35a1SLionel Sambuc	mkdir root
186*11be35a1SLionel Sambuc	mkdir root/dir
187*11be35a1SLionel Sambuc
188*11be35a1SLionel Sambuc	mkdir mnt
189*11be35a1SLionel Sambuc	atf_check -s eq:0 -o save:stdout -e empty \
190*11be35a1SLionel Sambuc	    -x 'echo $(cd mnt && /bin/pwd)/dir'
191*11be35a1SLionel Sambuc	mv stdout expout
192*11be35a1SLionel Sambuc	mount_psshfs root mnt
193*11be35a1SLionel Sambuc	atf_check -s eq:0 -o file:expout -e empty \
194*11be35a1SLionel Sambuc	    -x 'cd mnt/dir && ls .. >/dev/null && /bin/pwd'
195*11be35a1SLionel Sambuc}
196*11be35a1SLionel Sambucpwd_cleanup() {
197*11be35a1SLionel Sambuc	umount mnt
198*11be35a1SLionel Sambuc	stop_ssh
199*11be35a1SLionel Sambuc}
200*11be35a1SLionel Sambuc
201*11be35a1SLionel Sambucatf_test_case ls cleanup
202*11be35a1SLionel Sambucls_head() {
203*11be35a1SLionel Sambuc	atf_set "descr" "Uses ls, attempts to exercise puffs_cc"
204*11be35a1SLionel Sambuc}
205*11be35a1SLionel Sambucls_body() {
206*11be35a1SLionel Sambuc	require_puffs
207*11be35a1SLionel Sambuc
208*11be35a1SLionel Sambuc	start_ssh
209*11be35a1SLionel Sambuc
210*11be35a1SLionel Sambuc	mkdir mnt
211*11be35a1SLionel Sambuc	mkdir root
212*11be35a1SLionel Sambuc	mkdir root/dir
213*11be35a1SLionel Sambuc	touch root/dir/file1
214*11be35a1SLionel Sambuc	touch root/dir/file2
215*11be35a1SLionel Sambuc	touch root/file3
216*11be35a1SLionel Sambuc	touch root/file4
217*11be35a1SLionel Sambuc
218*11be35a1SLionel Sambuc	mount_psshfs root mnt
219*11be35a1SLionel Sambuc
220*11be35a1SLionel Sambuc	ls -l mnt &
221*11be35a1SLionel Sambuc
222*11be35a1SLionel Sambuc	IFS=' '
223*11be35a1SLionel Sambuclsout='dir
224*11be35a1SLionel Sambucfile3
225*11be35a1SLionel Sambucfile4
226*11be35a1SLionel Sambuc
227*11be35a1SLionel Sambucmnt/dir:
228*11be35a1SLionel Sambucfile1
229*11be35a1SLionel Sambucfile2
230*11be35a1SLionel Sambuc'
231*11be35a1SLionel Sambuc	atf_check -s exit:0 -o inline:"$lsout" ls -R mnt
232*11be35a1SLionel Sambuc}
233*11be35a1SLionel Sambucls_cleanup() {
234*11be35a1SLionel Sambuc	umount mnt
235*11be35a1SLionel Sambuc	stop_ssh
236*11be35a1SLionel Sambuc}
237*11be35a1SLionel Sambuc
238*11be35a1SLionel Sambucatf_test_case setattr_cache cleanup
239*11be35a1SLionel Sambucsetattr_cache_head() {
240*11be35a1SLionel Sambuc	atf_set "descr" "Checks that setattr caches"
241*11be35a1SLionel Sambuc	# Don't wait for the eternity that atf usually waits.  Twenty
242*11be35a1SLionel Sambuc	# seconds should be good enough, except maybe on a VAX...
243*11be35a1SLionel Sambuc	atf_set "timeout" 20
244*11be35a1SLionel Sambuc}
245*11be35a1SLionel Sambucsetattr_cache_body() {
246*11be35a1SLionel Sambuc	require_puffs
247*11be35a1SLionel Sambuc	start_ssh
248*11be35a1SLionel Sambuc	atf_check -s exit:0 mkdir root
249*11be35a1SLionel Sambuc	atf_check -s exit:0 mkdir mnt
250*11be35a1SLionel Sambuc	mount_psshfs root mnt
251*11be35a1SLionel Sambuc	atf_check -s exit:0 -x ': > mnt/loser'
252*11be35a1SLionel Sambuc	atf_check -s exit:0 -o save:stat stat mnt/loser
253*11be35a1SLionel Sambuc	# Oops -- this doesn't work.  We need to stop the child of the
254*11be35a1SLionel Sambuc	# sshd that is handling the sftp session.
255*11be35a1SLionel Sambuc	atf_check -s exit:0 kill -STOP $(cat sshd.pid)
256*11be35a1SLionel Sambuc	atf_check -s exit:0 -x ': > mnt/loser'
257*11be35a1SLionel Sambuc	atf_check -s exit:0 -o file:stat stat mnt/loser
258*11be35a1SLionel Sambuc}
259*11be35a1SLionel Sambucsetattr_cache_cleanup() {
260*11be35a1SLionel Sambuc	umount mnt
261*11be35a1SLionel Sambuc	kill -CONT $(cat sshd.pid)
262*11be35a1SLionel Sambuc	stop_ssh
263*11be35a1SLionel Sambuc}
264*11be35a1SLionel Sambuc
265*11be35a1SLionel Sambuc# -------------------------------------------------------------------------
266*11be35a1SLionel Sambuc# Initialization.
267*11be35a1SLionel Sambuc# -------------------------------------------------------------------------
268*11be35a1SLionel Sambuc
269*11be35a1SLionel Sambucatf_init_test_cases() {
270*11be35a1SLionel Sambuc	atf_add_test_case inode_nos
271*11be35a1SLionel Sambuc	atf_add_test_case pwd
272*11be35a1SLionel Sambuc	atf_add_test_case ls
273*11be35a1SLionel Sambuc	#atf_add_test_case setattr_cache
274*11be35a1SLionel Sambuc}
275