xref: /freebsd/bin/ln/tests/ln_test.sh (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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#
27
28set_umask()
29{
30	if ! umask 022; then
31		atf_fail "setting umask failed"
32	fi
33}
34
35atf_test_case L_flag
36L_flag_head()
37{
38	atf_set "descr" "Verify that when creating a hard link to a " \
39			"symbolic link, '-L' option creates a hard" \
40			"link to the target of the symbolic link"
41}
42
43L_flag_body()
44{
45	set_umask
46	atf_check touch A
47	atf_check ln -s A B
48	atf_check ln -L B C
49	stat_A=$(stat -f %i A)
50	stat_C=$(stat -f %i C)
51	atf_check_equal "$stat_A" "$stat_C"
52	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
53	atf_check -o inline:'A\n' readlink 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	set_umask
102	atf_check touch A B
103	atf_check -s exit:1 -e inline:'ln: B: File exists\n' \
104		ln A B
105}
106
107atf_test_case target_exists_symbolic
108target_exists_symbolic_head()
109{
110	atf_set "descr" "Verify whether creating a symbolic link fails if " \
111			"the target file already exists"
112}
113
114target_exists_symbolic_body()
115{
116	set_umask
117	atf_check touch A B
118	atf_check -s exit:1 -e inline:'ln: B: File exists\n' \
119		ln -s A B
120}
121
122atf_test_case shf_flag_dir
123shf_flag_dir_head() {
124	atf_set "descr" "Verify that if the target directory is a symbolic " \
125			"link, '-shf' option prevents following the link"
126}
127
128shf_flag_dir_body()
129{
130	atf_check mkdir -m 0777 A B
131	atf_check ln -s A C
132	atf_check ln -shf B C
133	atf_check -o inline:'Symbolic Link\n' stat -f %SHT C
134	atf_check -o inline:'B\n' readlink C
135}
136
137atf_test_case snf_flag_dir
138snf_flag_dir_head() {
139	atf_set "descr" "Verify that if the target directory is a symbolic " \
140			"link, '-snf' option prevents following the link"
141}
142
143snf_flag_dir_body()
144{
145	atf_check mkdir -m 0777 A B
146	atf_check ln -s A C
147	atf_check ln -snf B C
148	atf_check -o inline:'Symbolic Link\n' stat -f %SHT C
149	atf_check -o inline:'B\n' readlink C
150}
151
152atf_test_case sF_flag
153sF_flag_head()
154{
155	atf_set "descr" "Verify that if the target file already exists " \
156			"and is a directory, then '-sF' option removes " \
157			"it so that the link may occur"
158}
159
160sF_flag_body()
161{
162	atf_check mkdir A B
163	atf_check ln -sF A B
164	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
165	atf_check -o inline:'A\n' readlink B
166}
167
168atf_test_case sf_flag
169sf_flag_head()
170{
171	atf_set "descr" "Verify that if the target file already exists, " \
172			"'-sf' option unlinks it and creates a symbolic link " \
173			"to the source file"
174}
175
176sf_flag_body()
177{
178	set_umask
179	atf_check touch A B
180	atf_check ln -sf A B
181	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
182	atf_check -o inline:'A\n' readlink B
183}
184
185atf_test_case s_flag
186s_flag_head()
187{
188	atf_set "descr" "Verify that '-s' option creates a symbolic link"
189}
190
191s_flag_body()
192{
193	set_umask
194	atf_check touch A
195	atf_check ln -s A B
196	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
197	atf_check -o inline:'A\n' readlink B
198}
199
200atf_test_case s_flag_broken
201s_flag_broken_head()
202{
203	atf_set "descr" "Verify that if the source file does not exists, '-s' " \
204			"option creates a broken symbolic link to the source file"
205}
206
207s_flag_broken_body()
208{
209	atf_check ln -s A B
210	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
211	atf_check -o inline:'A\n' readlink B
212}
213
214atf_test_case sw_flag
215sw_flag_head()
216{
217	atf_set "descr" "Verify that '-sw' option produces a warning if the " \
218			"source of a symbolic link does not currently exist"
219}
220
221sw_flag_body()
222{
223	atf_check -s exit:0 -e inline:'ln: warning: A: No such file or directory\n' \
224		ln -sw A B
225	atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
226	atf_check -o inline:'A\n' readlink B
227}
228
229atf_init_test_cases()
230{
231	atf_add_test_case L_flag
232	atf_add_test_case P_flag
233	atf_add_test_case f_flag
234	atf_add_test_case target_exists_hard
235	atf_add_test_case target_exists_symbolic
236	atf_add_test_case shf_flag_dir
237	atf_add_test_case snf_flag_dir
238	atf_add_test_case sF_flag
239	atf_add_test_case sf_flag
240	atf_add_test_case s_flag
241	atf_add_test_case s_flag_broken
242	atf_add_test_case sw_flag
243}
244