xref: /freebsd/contrib/netbsd-tests/fs/psshfs/t_psshfs.sh (revision 6829dae12bb055451fa467da4589c43bd03b1e64)
1# $NetBSD: t_psshfs.sh,v 1.8 2016/09/05 08:53:57 christos Exp $
2#
3# Copyright (c) 2007, 2008 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
28# -------------------------------------------------------------------------
29# Auxiliary functions.
30# -------------------------------------------------------------------------
31
32#
33# Skips the calling test case if puffs is not supported in the kernel
34# or if the calling user does not have the necessary permissions to mount
35# file systems.
36#
37require_puffs() {
38	case "$($(atf_get_srcdir)/h_have_puffs)" in
39		eacces)
40			atf_skip "Cannot open /dev/puffs for read/write access"
41			;;
42		enxio)
43			atf_skip "puffs support not built into the kernel"
44			;;
45		failed)
46			atf_skip "Unknown error trying to access /dev/puffs"
47			;;
48		yes)
49			;;
50		*)
51			atf_fail "Unknown value returned by h_have_puffs"
52			;;
53	esac
54
55	if [ $(id -u) -ne 0 -a $(sysctl -n vfs.generic.usermount) -eq 0 ]
56	then
57		atf_skip "Regular users cannot mount file systems" \
58		    "(vfs.generic.usermount is set to 0)"
59	fi
60}
61
62#
63# Starts a SSH server and sets up the client to access it.
64# Authentication is allowed and done using an RSA key exclusively, which
65# is generated on the fly as part of the test case.
66# XXX: Ideally, all the tests in this test program should be able to share
67# the generated key, because creating it can be a very slow process on some
68# machines.
69#
70start_ssh() {
71	echo "Setting up SSH server configuration"
72	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
73	    $(atf_get_srcdir)/sshd_config.in >sshd_config || \
74	    atf_fail "Failed to create sshd_config"
75	atf_check -s eq:0 -o empty -e empty cp /usr/libexec/sftp-server .
76	atf_check -s eq:0 -o empty -e empty \
77	    cp $(atf_get_srcdir)/ssh_host_key .
78	atf_check -s eq:0 -o empty -e empty \
79	    cp $(atf_get_srcdir)/ssh_host_key.pub .
80	atf_check -s eq:0 -o empty -e empty chmod 400 ssh_host_key
81	atf_check -s eq:0 -o empty -e empty chmod 444 ssh_host_key.pub
82
83	/usr/sbin/sshd -e -f ./sshd_config >sshd.log 2>&1 &
84	while [ ! -f sshd.pid ]; do
85		sleep 0.01
86	done
87	echo "SSH server started (pid $(cat sshd.pid))"
88
89	echo "Setting up SSH client configuration"
90	atf_check -s eq:0 -o empty -e empty \
91	    ssh-keygen -f ssh_user_key -t rsa -b 1024 -N "" -q
92	atf_check -s eq:0 -o empty -e empty \
93	    cp ssh_user_key.pub authorized_keys
94	echo "[localhost]:10000,[127.0.0.1]:10000,[::1]:10000" \
95	    "$(cat $(atf_get_srcdir)/ssh_host_key.pub)" >known_hosts || \
96	    atf_fail "Failed to create known_hosts"
97	atf_check -s eq:0 -o empty -e empty chmod 600 authorized_keys
98	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
99	    $(atf_get_srcdir)/ssh_config.in >ssh_config || \
100	    atf_fail "Failed to create ssh_config"
101}
102
103#
104# Stops the SSH server spawned by start_ssh and prints diagnosis data.
105#
106stop_ssh() {
107	if [ -f sshd.pid ]; then
108		echo "Stopping SSH server (pid $(cat sshd.pid))"
109		kill $(cat sshd.pid)
110	fi
111	if [ -f sshd.log ]; then
112		echo "Server output was:"
113		sed -e 's,^,    ,' sshd.log
114	fi
115}
116
117#
118# Mounts the given source directory on the target directory using psshfs.
119# Both directories are supposed to live on the current directory.
120#
121mount_psshfs() {
122	atf_check -s eq:0 -o empty -e empty \
123	    mount -t psshfs -o -F=$(pwd)/ssh_config localhost:$(pwd)/${1} ${2}
124}
125
126# -------------------------------------------------------------------------
127# The test cases.
128# -------------------------------------------------------------------------
129
130atf_test_case inode_nos cleanup
131inode_nos_head() {
132	atf_set "descr" "Checks that different files get different inode" \
133	    "numbers"
134}
135inode_nos_body() {
136	require_puffs
137
138	start_ssh
139
140	mkdir root
141	mkdir root/dir
142	touch root/dir/file1
143	touch root/dir/file2
144	touch root/file3
145	touch root/file4
146
147	cat >ne_inodes.sh <<EOF
148#! /bin/sh
149#
150# Compares the inodes of the two given files and returns true if they are
151# different; false otherwise.
152#
153set -e
154ino1=\$(stat -f %i \${1})
155ino2=\$(stat -f %i \${2})
156test \${ino1} -ne \${ino2}
157EOF
158	chmod +x ne_inodes.sh
159
160	mkdir mnt
161	mount_psshfs root mnt
162	atf_check -s eq:0 -o empty -e empty \
163	    ./ne_inodes.sh root/dir root/dir/file1
164	atf_check -s eq:0 -o empty -e empty \
165	    ./ne_inodes.sh root/dir root/dir/file2
166	atf_check -s eq:0 -o empty -e empty \
167	    ./ne_inodes.sh root/dir/file1 root/dir/file2
168	atf_check -s eq:0 -o empty -e empty \
169	    ./ne_inodes.sh root/file3 root/file4
170}
171inode_nos_cleanup() {
172	umount mnt
173	stop_ssh
174}
175
176atf_test_case pwd cleanup
177pwd_head() {
178	atf_set "descr" "Checks that pwd works correctly"
179}
180pwd_body() {
181	require_puffs
182
183	start_ssh
184
185	mkdir root
186	mkdir root/dir
187
188	mkdir mnt
189	atf_check -s eq:0 -o save:stdout -e empty \
190	    -x 'echo $(cd mnt && /bin/pwd)/dir'
191	mv stdout expout
192	mount_psshfs root mnt
193	atf_check -s eq:0 -o file:expout -e empty \
194	    -x 'cd mnt/dir && ls .. >/dev/null && /bin/pwd'
195}
196pwd_cleanup() {
197	umount mnt
198	stop_ssh
199}
200
201atf_test_case ls cleanup
202ls_head() {
203	atf_set "descr" "Uses ls, attempts to exercise puffs_cc"
204}
205ls_body() {
206	require_puffs
207
208	start_ssh
209
210	mkdir mnt
211	mkdir root
212	mkdir root/dir
213	touch root/dir/file1
214	touch root/dir/file2
215	touch root/file3
216	touch root/file4
217
218	mount_psshfs root mnt
219
220	ls -l mnt &
221
222	IFS=' '
223lsout='dir
224file3
225file4
226
227mnt/dir:
228file1
229file2
230'
231	atf_check -s exit:0 -o inline:"$lsout" ls -R mnt
232}
233ls_cleanup() {
234	umount mnt
235	stop_ssh
236}
237
238atf_test_case setattr_cache cleanup
239setattr_cache_head() {
240	atf_set "descr" "Checks that setattr caches"
241	# Don't wait for the eternity that atf usually waits.  Twenty
242	# seconds should be good enough, except maybe on a VAX...
243	atf_set "timeout" 20
244}
245setattr_cache_body() {
246	require_puffs
247	start_ssh
248	atf_check -s exit:0 mkdir root
249	atf_check -s exit:0 mkdir mnt
250	mount_psshfs root mnt
251	atf_check -s exit:0 -x ': > mnt/loser'
252	atf_check -s exit:0 -o save:stat stat mnt/loser
253	# Oops -- this doesn't work.  We need to stop the child of the
254	# sshd that is handling the sftp session.
255	atf_check -s exit:0 kill -STOP $(cat sshd.pid)
256	atf_check -s exit:0 -x ': > mnt/loser'
257	atf_check -s exit:0 -o file:stat stat mnt/loser
258}
259setattr_cache_cleanup() {
260	umount mnt
261	kill -CONT $(cat sshd.pid)
262	stop_ssh
263}
264
265atf_test_case read_empty_file cleanup
266read_empty_file_head() {
267	atf_set "descr" "Checks whether an empty file can be read"
268	# This test is supposed to make sure psshfs does not hang
269	# when reading from an empty file, hence the timeout.
270	atf_set "timeout" 8
271}
272read_empty_file_body() {
273	require_puffs
274	start_ssh
275	atf_check mkdir root mnt
276	atf_check -x ': > root/empty'
277	mount_psshfs root mnt
278	atf_check cat mnt/empty
279}
280read_empty_file_cleanup() {
281	umount mnt
282	stop_ssh
283}
284
285# -------------------------------------------------------------------------
286# Initialization.
287# -------------------------------------------------------------------------
288
289atf_init_test_cases() {
290	atf_add_test_case inode_nos
291	atf_add_test_case pwd
292	atf_add_test_case ls
293	#atf_add_test_case setattr_cache
294	atf_add_test_case read_empty_file
295}
296