xref: /netbsd/tests/fs/psshfs/t_psshfs.sh (revision 6550d01e)
1# $NetBSD: t_psshfs.sh,v 1.3 2011/01/06 07:28:32 pooka 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 -D -f ./sshd_config >sshd.log 2>&1 &
84	echo $! >sshd.pid
85	echo "SSH server started (pid $(cat sshd.pid))"
86
87	echo "Setting up SSH client configuration"
88	atf_check -s eq:0 -o empty -e empty \
89	    ssh-keygen -f ssh_user_key -t rsa -b 1024 -N "" -q
90	atf_check -s eq:0 -o empty -e empty \
91	    cp ssh_user_key.pub authorized_keys
92	echo "[localhost]:10000,[127.0.0.1]:10000,[::1]:10000" \
93	    "$(cat $(atf_get_srcdir)/ssh_host_key.pub)" >known_hosts || \
94	    atf_fail "Failed to create known_hosts"
95	atf_check -s eq:0 -o empty -e empty chmod 600 authorized_keys
96	sed -e "s,@SRCDIR@,$(atf_get_srcdir),g" -e "s,@WORKDIR@,$(pwd),g" \
97	    $(atf_get_srcdir)/ssh_config.in >ssh_config || \
98	    atf_fail "Failed to create ssh_config"
99}
100
101#
102# Stops the SSH server spawned by start_ssh and prints diagnosis data.
103#
104stop_ssh() {
105	if [ -f sshd.pid ]; then
106		echo "Stopping SSH server (pid $(cat sshd.pid))"
107		kill $(cat sshd.pid)
108	fi
109	if [ -f sshd.log ]; then
110		echo "Server output was:"
111		sed -e 's,^,    ,' sshd.log
112	fi
113}
114
115#
116# Mounts the given source directory on the target directory using psshfs.
117# Both directories are supposed to live on the current directory.
118#
119mount_psshfs() {
120	atf_check -s eq:0 -o empty -e empty \
121	    mount -t psshfs -o -F=$(pwd)/ssh_config localhost:$(pwd)/${1} ${2}
122}
123
124# -------------------------------------------------------------------------
125# The test cases.
126# -------------------------------------------------------------------------
127
128atf_test_case inode_nos cleanup
129inode_nos_head() {
130	atf_set "descr" "Checks that different files get different inode" \
131	    "numbers"
132}
133inode_nos_body() {
134	require_puffs
135
136	start_ssh
137
138	mkdir root
139	mkdir root/dir
140	touch root/dir/file1
141	touch root/dir/file2
142	touch root/file3
143	touch root/file4
144
145	cat >ne_inodes.sh <<EOF
146#! $(atf-config -t atf_shell)
147#
148# Compares the inodes of the two given files and returns true if they are
149# different; false otherwise.
150#
151test \$(stat -f %i \${1}) -ne \$(stat -f %i \${2})
152EOF
153	chmod +x ne_inodes.sh
154
155	mkdir mnt
156	mount_psshfs root mnt
157	atf_check -s eq:0 -o empty -e empty \
158	    ./ne_inodes.sh root/dir root/dir/file1
159	atf_check -s eq:0 -o empty -e empty \
160	    ./ne_inodes.sh root/dir root/dir/file2
161	atf_check -s eq:0 -o empty -e empty \
162	    ./ne_inodes.sh root/dir/file1 root/dir/file2
163	atf_check -s eq:0 -o empty -e empty \
164	    ./ne_inodes.sh root/file3 root/file4
165}
166inode_nos_cleanup() {
167	umount mnt
168	stop_ssh
169}
170
171atf_test_case pwd cleanup
172pwd_head() {
173	atf_set "descr" "Checks that pwd works correctly"
174}
175pwd_body() {
176	require_puffs
177
178	start_ssh
179
180	mkdir root
181	mkdir root/dir
182
183	mkdir mnt
184	atf_check -s eq:0 -o save:stdout -e empty \
185	    -x 'echo $(cd mnt && /bin/pwd)/dir'
186	mv stdout expout
187	mount_psshfs root mnt
188	atf_check -s eq:0 -o file:expout -e empty \
189	    -x 'cd mnt/dir && ls .. >/dev/null && /bin/pwd'
190}
191pwd_cleanup() {
192	umount mnt
193	stop_ssh
194}
195
196atf_test_case ls cleanup
197ls_head() {
198	atf_set "descr" "Uses ls, attempts to exercise puffs_cc"
199}
200ls_body() {
201	require_puffs
202
203	start_ssh
204
205	mkdir mnt
206	mkdir root
207	mkdir root/dir
208	touch root/dir/file1
209	touch root/dir/file2
210	touch root/file3
211	touch root/file4
212
213	mount_psshfs root mnt
214
215	ls -l mnt &
216
217	IFS=' '
218lsout='dir
219file3
220file4
221
222mnt/dir:
223file1
224file2
225'
226	atf_check -s exit:0 -o inline:"$lsout" ls -R mnt
227}
228ls_cleanup() {
229	umount mnt
230	stop_ssh
231}
232
233# -------------------------------------------------------------------------
234# Initialization.
235# -------------------------------------------------------------------------
236
237atf_init_test_cases() {
238	atf_add_test_case inode_nos
239	atf_add_test_case pwd
240	atf_add_test_case ls
241}
242