1319714Sasomers# 2319714Sasomers# Copyright 2017 Shivansh Rai 3319714Sasomers# All rights reserved. 4319714Sasomers# 5319714Sasomers# Redistribution and use in source and binary forms, with or without 6319714Sasomers# modification, are permitted provided that the following conditions 7319714Sasomers# are met: 8319714Sasomers# 1. Redistributions of source code must retain the above copyright 9319714Sasomers# notice, this list of conditions and the following disclaimer. 10319714Sasomers# 2. Redistributions in binary form must reproduce the above copyright 11319714Sasomers# notice, this list of conditions and the following disclaimer in the 12319714Sasomers# documentation and/or other materials provided with the distribution. 13319714Sasomers# 14319714Sasomers# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15319714Sasomers# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16319714Sasomers# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17319714Sasomers# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18319714Sasomers# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19319714Sasomers# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20319714Sasomers# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21319714Sasomers# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22319714Sasomers# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23319714Sasomers# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24319714Sasomers# SUCH DAMAGE. 25319714Sasomers# 26319714Sasomers# $FreeBSD: stable/10/bin/ln/tests/ln_test.sh 321819 2017-07-31 21:42:18Z asomers $ 27319714Sasomers# 28319714Sasomers 29319714Sasomersset_umask() 30319714Sasomers{ 31319714Sasomers if ! umask 022; then 32319714Sasomers atf_fail "setting umask failed" 33319714Sasomers fi 34319714Sasomers} 35319714Sasomers 36319714Sasomersatf_test_case L_flag 37319714SasomersL_flag_head() 38319714Sasomers{ 39319714Sasomers atf_set "descr" "Verify that when creating a hard link to a " \ 40319714Sasomers "symbolic link, '-L' option creates a hard" \ 41319714Sasomers "link to the target of the symbolic link" 42319714Sasomers} 43319714Sasomers 44319714SasomersL_flag_body() 45319714Sasomers{ 46319714Sasomers set_umask 47319714Sasomers atf_check touch A 48319714Sasomers atf_check ln -s A B 49319714Sasomers atf_check ln -L B C 50319714Sasomers stat_A=$(stat -f %i A) 51319714Sasomers stat_C=$(stat -f %i C) 52319714Sasomers atf_check_equal "$stat_A" "$stat_C" 53319714Sasomers atf_check -o inline:'B: symbolic link to A\n' file B 54319714Sasomers} 55319714Sasomers 56319714Sasomersatf_test_case P_flag 57319714SasomersP_flag_head() 58319714Sasomers{ 59319714Sasomers atf_set "descr" "Verify that when creating a hard link to a " \ 60319714Sasomers "symbolic link, '-P' option creates a hard " \ 61319714Sasomers "link to the symbolic link itself" 62319714Sasomers} 63319714Sasomers 64319714SasomersP_flag_body() 65319714Sasomers{ 66319714Sasomers set_umask 67319714Sasomers atf_check touch A 68319714Sasomers atf_check ln -s A B 69319714Sasomers atf_check ln -P B C 70319714Sasomers stat_B=$(stat -f %i B) 71319714Sasomers stat_C=$(stat -f %i C) 72319714Sasomers atf_check_equal "$stat_B" "$stat_C" 73319714Sasomers} 74319714Sasomers 75319714Sasomersatf_test_case f_flag 76319714Sasomersf_flag_head() 77319714Sasomers{ 78319714Sasomers atf_set "descr" "Verify that if the target file already exists, " \ 79319714Sasomers "'-f' option unlinks it so that link may occur" 80319714Sasomers} 81319714Sasomers 82319714Sasomersf_flag_body() 83319714Sasomers{ 84319714Sasomers set_umask 85319714Sasomers atf_check touch A B 86319714Sasomers atf_check ln -f A B 87319714Sasomers stat_A=$(stat -f %i A) 88319714Sasomers stat_B=$(stat -f %i B) 89319714Sasomers atf_check_equal "$stat_A" "$stat_B" 90319714Sasomers} 91319714Sasomers 92319714Sasomersatf_test_case target_exists_hard 93319714Sasomerstarget_exists_hard_head() 94319714Sasomers{ 95319714Sasomers atf_set "descr" "Verify whether creating a hard link fails if the " \ 96319714Sasomers "target file already exists" 97319714Sasomers} 98319714Sasomers 99319714Sasomerstarget_exists_hard_body() 100319714Sasomers{ 101321819Sasomers set_umask 102319714Sasomers atf_check touch A B 103319714Sasomers atf_check -s exit:1 -e inline:'ln: B: File exists\n' \ 104319714Sasomers ln A B 105319714Sasomers} 106319714Sasomers 107319714Sasomersatf_test_case target_exists_symbolic 108319714Sasomerstarget_exists_symbolic_head() 109319714Sasomers{ 110319714Sasomers atf_set "descr" "Verify whether creating a symbolic link fails if " \ 111319714Sasomers "the target file already exists" 112319714Sasomers} 113319714Sasomers 114319714Sasomerstarget_exists_symbolic_body() 115319714Sasomers{ 116321819Sasomers set_umask 117319714Sasomers atf_check touch A B 118319714Sasomers atf_check -s exit:1 -e inline:'ln: B: File exists\n' \ 119319714Sasomers ln -s A B 120319714Sasomers} 121319714Sasomers 122319714Sasomersatf_test_case shf_flag_dir 123319714Sasomersshf_flag_dir_head() { 124319714Sasomers atf_set "descr" "Verify that if the target directory is a symbolic " \ 125319714Sasomers "link, '-shf' option prevents following the link" 126319714Sasomers} 127319714Sasomers 128319714Sasomersshf_flag_dir_body() 129319714Sasomers{ 130319714Sasomers atf_check mkdir -m 0777 A B 131319714Sasomers atf_check ln -s A C 132319714Sasomers atf_check ln -shf B C 133319714Sasomers atf_check -o inline:'C: symbolic link to B\n' file C 134319714Sasomers} 135319714Sasomers 136319714Sasomersatf_test_case snf_flag_dir 137319714Sasomerssnf_flag_dir_head() { 138319714Sasomers atf_set "descr" "Verify that if the target directory is a symbolic " \ 139319714Sasomers "link, '-snf' option prevents following the link" 140319714Sasomers} 141319714Sasomers 142319714Sasomerssnf_flag_dir_body() 143319714Sasomers{ 144319714Sasomers atf_check mkdir -m 0777 A B 145319714Sasomers atf_check ln -s A C 146319714Sasomers atf_check ln -snf B C 147319714Sasomers atf_check -o inline:'C: symbolic link to B\n' file C 148319714Sasomers} 149319714Sasomers 150321094Sngieatf_test_case sF_flag 151321094SngiesF_flag_head() 152321094Sngie{ 153321094Sngie atf_set "descr" "Verify that if the target file already exists " \ 154321094Sngie "and is a directory, then '-sF' option removes " \ 155321094Sngie "it so that the link may occur" 156321094Sngie} 157321094Sngie 158321094SngiesF_flag_body() 159321094Sngie{ 160321094Sngie atf_check mkdir A B 161321094Sngie atf_check ln -sF A B 162321094Sngie atf_check -o inline:'Symbolic Link\n' stat -f %SHT B 163321094Sngie atf_check -o inline:'A\n' readlink B 164321094Sngie} 165321094Sngie 166319714Sasomersatf_test_case sf_flag 167319714Sasomerssf_flag_head() 168319714Sasomers{ 169319714Sasomers atf_set "descr" "Verify that if the target file already exists, " \ 170319714Sasomers "'-sf' option unlinks it and creates a symbolic link " \ 171319714Sasomers "to the source file" 172319714Sasomers} 173319714Sasomers 174319714Sasomerssf_flag_body() 175319714Sasomers{ 176321819Sasomers set_umask 177319714Sasomers atf_check touch A B 178319714Sasomers atf_check ln -sf A B 179319714Sasomers atf_check -o inline:'B: symbolic link to A\n' file B 180319714Sasomers} 181319714Sasomers 182319714Sasomersatf_test_case s_flag 183319714Sasomerss_flag_head() 184319714Sasomers{ 185319714Sasomers atf_set "descr" "Verify that '-s' option creates a symbolic link" 186319714Sasomers} 187319714Sasomers 188319714Sasomerss_flag_body() 189319714Sasomers{ 190319714Sasomers set_umask 191319714Sasomers atf_check touch A 192319714Sasomers atf_check ln -s A B 193319714Sasomers atf_check -o inline:'B: symbolic link to A\n' file B 194319714Sasomers} 195319714Sasomers 196319714Sasomersatf_test_case s_flag_broken 197319714Sasomerss_flag_broken_head() 198319714Sasomers{ 199319714Sasomers atf_set "descr" "Verify that if the source file does not exists, '-s' " \ 200319714Sasomers "option creates a broken symbolic link to the source file" 201319714Sasomers} 202319714Sasomers 203319714Sasomerss_flag_broken_body() 204319714Sasomers{ 205319714Sasomers atf_check ln -s A B 206319714Sasomers atf_check -o inline:'B: broken symbolic link to A\n' file B 207319714Sasomers} 208319714Sasomers 209319714Sasomersatf_test_case sw_flag 210319714Sasomerssw_flag_head() 211319714Sasomers{ 212319714Sasomers atf_set "descr" "Verify that '-sw' option produces a warning if the " \ 213319714Sasomers "source of a symbolic link does not currently exist" 214319714Sasomers} 215319714Sasomers 216319714Sasomerssw_flag_body() 217319714Sasomers{ 218319714Sasomers atf_check -s exit:0 -e inline:'ln: warning: A: No such file or directory\n' \ 219319714Sasomers ln -sw A B 220319714Sasomers atf_check -o inline:'B: broken symbolic link to A\n' file B 221319714Sasomers} 222319714Sasomers 223319714Sasomersatf_init_test_cases() 224319714Sasomers{ 225319714Sasomers atf_add_test_case L_flag 226319714Sasomers atf_add_test_case P_flag 227319714Sasomers atf_add_test_case f_flag 228319714Sasomers atf_add_test_case target_exists_hard 229319714Sasomers atf_add_test_case target_exists_symbolic 230319714Sasomers atf_add_test_case shf_flag_dir 231319714Sasomers atf_add_test_case snf_flag_dir 232321094Sngie atf_add_test_case sF_flag 233319714Sasomers atf_add_test_case sf_flag 234319714Sasomers atf_add_test_case s_flag 235319714Sasomers atf_add_test_case s_flag_broken 236319714Sasomers atf_add_test_case sw_flag 237319714Sasomers} 238