xref: /freebsd/bin/ln/tests/ln_test.sh (revision f7c32ed617858bcd22f8d1b03199099d50125721)
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:'Symbolic Link\n' stat -f %SHT B
54	atf_check -o inline:'A\n' readlink B
55}
56
57atf_test_case P_flag
58P_flag_head()
59{
60	atf_set "descr" "Verify that when creating a hard link to a " \
61			"symbolic link, '-P' option creates a hard " \
62			"link to the symbolic link itself"
63}
64
65P_flag_body()
66{
67	set_umask
68	atf_check touch A
69	atf_check ln -s A B
70	atf_check ln -P B C
71	stat_B=$(stat -f %i B)
72	stat_C=$(stat -f %i C)
73	atf_check_equal "$stat_B" "$stat_C"
74}
75
76atf_test_case f_flag
77f_flag_head()
78{
79	atf_set "descr" "Verify that if the target file already exists, " \
80			"'-f' option unlinks it so that link may occur"
81}
82
83f_flag_body()
84{
85	set_umask
86	atf_check touch A B
87	atf_check ln -f A B
88	stat_A=$(stat -f %i A)
89	stat_B=$(stat -f %i B)
90	atf_check_equal "$stat_A" "$stat_B"
91}
92
93atf_test_case target_exists_hard
94target_exists_hard_head()
95{
96	atf_set "descr" "Verify whether creating a hard link fails if the " \
97			"target file already exists"
98}
99
100target_exists_hard_body()
101{
102	set_umask
103	atf_check touch A B
104	atf_check -s exit:1 -e inline:'ln: B: File exists\n' \
105		ln A B
106}
107
108atf_test_case target_exists_symbolic
109target_exists_symbolic_head()
110{
111	atf_set "descr" "Verify whether creating a symbolic link fails if " \
112			"the target file already exists"
113}
114
115target_exists_symbolic_body()
116{
117	set_umask
118	atf_check touch A B
119	atf_check -s exit:1 -e inline:'ln: B: File exists\n' \
120		ln -s A B
121}
122
123atf_test_case shf_flag_dir
124shf_flag_dir_head() {
125	atf_set "descr" "Verify that if the target directory is a symbolic " \
126			"link, '-shf' option prevents following the link"
127}
128
129shf_flag_dir_body()
130{
131	atf_check mkdir -m 0777 A B
132	atf_check ln -s A C
133	atf_check ln -shf B C
134	atf_check -o inline:'Symbolic Link\n' stat -f %SHT C
135	atf_check -o inline:'B\n' readlink C
136}
137
138atf_test_case snf_flag_dir
139snf_flag_dir_head() {
140	atf_set "descr" "Verify that if the target directory is a symbolic " \
141			"link, '-snf' option prevents following the link"
142}
143
144snf_flag_dir_body()
145{
146	atf_check mkdir -m 0777 A B
147	atf_check ln -s A C
148	atf_check ln -snf B C
149	atf_check -o inline:'Symbolic Link\n' stat -f %SHT C
150	atf_check -o inline:'B\n' readlink C
151}
152
153atf_test_case sF_flag
154sF_flag_head()
155{
156	atf_set "descr" "Verify that if the target file already exists " \
157			"and is a directory, then '-sF' option removes " \
158			"it so that the link may occur"
159}
160
161sF_flag_body()
162{
163	atf_check mkdir A B
164	atf_check ln -sF A B
165	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
166	atf_check -o inline:'A\n' readlink B
167}
168
169atf_test_case sf_flag
170sf_flag_head()
171{
172	atf_set "descr" "Verify that if the target file already exists, " \
173			"'-sf' option unlinks it and creates a symbolic link " \
174			"to the source file"
175}
176
177sf_flag_body()
178{
179	set_umask
180	atf_check touch A B
181	atf_check ln -sf A B
182	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
183	atf_check -o inline:'A\n' readlink B
184}
185
186atf_test_case s_flag
187s_flag_head()
188{
189	atf_set "descr" "Verify that '-s' option creates a symbolic link"
190}
191
192s_flag_body()
193{
194	set_umask
195	atf_check touch A
196	atf_check ln -s A B
197	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
198	atf_check -o inline:'A\n' readlink B
199}
200
201atf_test_case s_flag_broken
202s_flag_broken_head()
203{
204	atf_set "descr" "Verify that if the source file does not exists, '-s' " \
205			"option creates a broken symbolic link to the source file"
206}
207
208s_flag_broken_body()
209{
210	atf_check ln -s A B
211	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
212	atf_check -o inline:'A\n' readlink B
213}
214
215atf_test_case sw_flag
216sw_flag_head()
217{
218	atf_set "descr" "Verify that '-sw' option produces a warning if the " \
219			"source of a symbolic link does not currently exist"
220}
221
222sw_flag_body()
223{
224	atf_check -s exit:0 -e inline:'ln: warning: A: No such file or directory\n' \
225		ln -sw A B
226	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
227	atf_check -o inline:'A\n' readlink B
228}
229
230atf_init_test_cases()
231{
232	atf_add_test_case L_flag
233	atf_add_test_case P_flag
234	atf_add_test_case f_flag
235	atf_add_test_case target_exists_hard
236	atf_add_test_case target_exists_symbolic
237	atf_add_test_case shf_flag_dir
238	atf_add_test_case snf_flag_dir
239	atf_add_test_case sF_flag
240	atf_add_test_case sf_flag
241	atf_add_test_case s_flag
242	atf_add_test_case s_flag_broken
243	atf_add_test_case sw_flag
244}
245