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