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