1# 2# SPDX-License-Identifier: BSD-2-Clause 3# 4# Copyright 2017 Shivansh Rai 5# All rights reserved. 6# 7# Redistribution and use in source and binary forms, with or without 8# modification, are permitted provided that the following conditions 9# are met: 10# 1. Redistributions of source code must retain the above copyright 11# notice, this list of conditions and the following disclaimer. 12# 2. Redistributions in binary form must reproduce the above copyright 13# notice, this list of conditions and the following disclaimer in the 14# documentation and/or other materials provided with the distribution. 15# 16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26# SUCH DAMAGE. 27# 28 29atf_check_same_file() 30{ 31 atf_check_equal "$(stat -f %d,%i "$1")" "$(stat -f %d,%i "$2")" 32} 33 34atf_check_symlink_to() 35{ 36 atf_check -o inline:"$1\n" readlink "$2" 37} 38 39atf_test_case L_flag 40L_flag_head() 41{ 42 atf_set "descr" "Verify that when creating a hard link to a " \ 43 "symbolic link, '-L' option creates a hard" \ 44 "link to the target of the symbolic link" 45} 46L_flag_body() 47{ 48 atf_check touch A 49 atf_check ln -s A B 50 atf_check ln -L B C 51 atf_check_same_file A C 52 atf_check_symlink_to A B 53} 54 55atf_test_case P_flag 56P_flag_head() 57{ 58 atf_set "descr" "Verify that when creating a hard link to a " \ 59 "symbolic link, '-P' option creates a hard " \ 60 "link to the symbolic link itself" 61} 62P_flag_body() 63{ 64 atf_check touch A 65 atf_check ln -s A B 66 atf_check ln -P B C 67 atf_check_same_file B C 68} 69 70atf_test_case f_flag 71f_flag_head() 72{ 73 atf_set "descr" "Verify that if the target file already exists, " \ 74 "'-f' option unlinks it so that link may occur" 75} 76f_flag_body() 77{ 78 atf_check touch A B 79 atf_check ln -f A B 80 atf_check_same_file A B 81} 82 83atf_test_case target_exists_hard 84target_exists_hard_head() 85{ 86 atf_set "descr" "Verify whether creating a hard link fails if the " \ 87 "target file already exists" 88} 89target_exists_hard_body() 90{ 91 atf_check touch A B 92 atf_check -s exit:1 -e inline:'ln: B: File exists\n' \ 93 ln A B 94} 95 96atf_test_case target_exists_symbolic 97target_exists_symbolic_head() 98{ 99 atf_set "descr" "Verify whether creating a symbolic link fails if " \ 100 "the target file already exists" 101} 102target_exists_symbolic_body() 103{ 104 atf_check touch A B 105 atf_check -s exit:1 -e inline:'ln: B: File exists\n' \ 106 ln -s A B 107} 108 109atf_test_case shf_flag_dir 110shf_flag_dir_head() { 111 atf_set "descr" "Verify that if the target directory is a symbolic " \ 112 "link, '-shf' option prevents following the link" 113} 114shf_flag_dir_body() 115{ 116 atf_check mkdir -m 0777 A B 117 atf_check ln -s A C 118 atf_check ln -shf B C 119 atf_check test -L C 120 atf_check -o inline:'B\n' readlink C 121} 122 123atf_test_case snf_flag_dir 124snf_flag_dir_head() { 125 atf_set "descr" "Verify that if the target directory is a symbolic " \ 126 "link, '-snf' option prevents following the link" 127} 128snf_flag_dir_body() 129{ 130 atf_check mkdir -m 0777 A B 131 atf_check ln -s A C 132 atf_check ln -snf B C 133 atf_check_symlink_to B C 134} 135 136atf_test_case sF_flag 137sF_flag_head() 138{ 139 atf_set "descr" "Verify that if the target file already exists " \ 140 "and is a directory, then '-sF' option removes " \ 141 "it so that the link may occur" 142} 143sF_flag_body() 144{ 145 atf_check mkdir A B 146 atf_check ln -sF A B 147 atf_check_symlink_to A B 148} 149 150atf_test_case sf_flag 151sf_flag_head() 152{ 153 atf_set "descr" "Verify that if the target file already exists, " \ 154 "'-sf' option unlinks it and creates a symbolic link " \ 155 "to the source file" 156} 157sf_flag_body() 158{ 159 atf_check touch A B 160 atf_check ln -sf A B 161 atf_check_symlink_to A B 162} 163 164atf_test_case sfF_flag 165sfF_flag_head() 166{ 167 atf_set "descr" "Verify that if the target file already exists " \ 168 "and is a symlink, then '-sfF' option removes " \ 169 "it so that the link may occur" 170} 171sfF_flag_body() 172{ 173 atf_check mkdir A B C 174 atf_check ln -sF A C 175 atf_check_symlink_to A C 176 atf_check ln -sfF B C 177 atf_check_symlink_to B C 178} 179 180atf_test_case s_flag 181s_flag_head() 182{ 183 atf_set "descr" "Verify that '-s' option creates a symbolic link" 184} 185s_flag_body() 186{ 187 atf_check touch A 188 atf_check ln -s A B 189 atf_check_symlink_to A B 190} 191 192atf_test_case s_flag_broken 193s_flag_broken_head() 194{ 195 atf_set "descr" "Verify that if the source file does not exists, '-s' " \ 196 "option creates a broken symbolic link to the source file" 197} 198s_flag_broken_body() 199{ 200 atf_check ln -s A B 201 atf_check_symlink_to A B 202} 203 204atf_test_case sw_flag 205sw_flag_head() 206{ 207 atf_set "descr" "Verify that '-sw' option produces a warning if the " \ 208 "source of a symbolic link does not currently exist" 209} 210sw_flag_body() 211{ 212 atf_check -s exit:0 -e inline:'ln: warning: A: No such file or directory\n' \ 213 ln -sw A B 214 atf_check_symlink_to A B 215} 216 217atf_init_test_cases() 218{ 219 atf_add_test_case L_flag 220 atf_add_test_case P_flag 221 atf_add_test_case f_flag 222 atf_add_test_case target_exists_hard 223 atf_add_test_case target_exists_symbolic 224 atf_add_test_case shf_flag_dir 225 atf_add_test_case snf_flag_dir 226 atf_add_test_case sF_flag 227 atf_add_test_case sf_flag 228 atf_add_test_case sfF_flag 229 atf_add_test_case s_flag 230 atf_add_test_case s_flag_broken 231 atf_add_test_case sw_flag 232} 233