xref: /freebsd/bin/ln/tests/ln_test.sh (revision 8ce85300c792ecfcb9a307577e3af96ea9a4544c)
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 D D/A
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	atf_check ln -sfF A D/
179	atf_check_symlink_to A D/A
180	atf_check ln -sfF ../A .
181	atf_check_symlink_to ../A A
182}
183
184atf_test_case s_flag
185s_flag_head()
186{
187	atf_set "descr" "Verify that '-s' option creates a symbolic link"
188}
189s_flag_body()
190{
191	atf_check touch A
192	atf_check ln -s A B
193	atf_check_symlink_to A B
194}
195
196atf_test_case s_flag_broken
197s_flag_broken_head()
198{
199	atf_set "descr" "Verify that if the source file does not exists, '-s' " \
200			"option creates a broken symbolic link to the source file"
201}
202s_flag_broken_body()
203{
204	atf_check ln -s A B
205	atf_check_symlink_to A B
206}
207
208atf_test_case sw_flag
209sw_flag_head()
210{
211	atf_set "descr" "Verify that '-sw' option produces a warning if the " \
212			"source of a symbolic link does not currently exist"
213}
214sw_flag_body()
215{
216	atf_check -s exit:0 -e inline:'ln: warning: A: No such file or directory\n' \
217	    ln -sw A B
218	atf_check_symlink_to A B
219}
220
221atf_test_case link_argc
222link_argc_head() {
223	atf_set "descr" "Verify that link(1) requires exactly two arguments"
224}
225link_argc_body() {
226	atf_check -s exit:1 -e match:"usage: link" \
227	    link foo
228	atf_check -s exit:1 -e match:"No such file" \
229	    link foo bar
230	atf_check -s exit:1 -e match:"No such file" \
231	    link -- foo bar
232	atf_check -s exit:1 -e match:"usage: link" \
233	    link foo bar baz
234}
235
236atf_test_case link_basic
237link_basic_head() {
238	atf_set "descr" "Verify that link(1) creates a link"
239}
240link_basic_body() {
241	touch foo
242	atf_check link foo bar
243	atf_check_same_file foo bar
244	rm bar
245	ln -s foo bar
246	atf_check link bar baz
247	atf_check_same_file foo baz
248}
249
250atf_test_case link_eexist
251link_eexist_head() {
252	atf_set "descr" "Verify that link(1) fails if the target exists"
253}
254link_eexist_body() {
255	touch foo bar
256	atf_check -s exit:1 -e match:"bar.*exists" \
257	    link foo bar
258	ln -s non-existent baz
259	atf_check -s exit:1 -e match:"baz.*exists" \
260	    link foo baz
261}
262
263atf_test_case link_eisdir
264link_eisdir_head() {
265	atf_set "descr" "Verify that link(1) fails if the source is a directory"
266}
267link_eisdir_body() {
268	mkdir foo
269	atf_check -s exit:1 -e match:"foo.*directory" \
270	    link foo bar
271	ln -s foo bar
272	atf_check -s exit:1 -e match:"bar.*directory" \
273	    link bar baz
274}
275
276atf_init_test_cases()
277{
278	atf_add_test_case L_flag
279	atf_add_test_case P_flag
280	atf_add_test_case f_flag
281	atf_add_test_case target_exists_hard
282	atf_add_test_case target_exists_symbolic
283	atf_add_test_case shf_flag_dir
284	atf_add_test_case snf_flag_dir
285	atf_add_test_case sF_flag
286	atf_add_test_case sf_flag
287	atf_add_test_case sfF_flag
288	atf_add_test_case s_flag
289	atf_add_test_case s_flag_broken
290	atf_add_test_case sw_flag
291	atf_add_test_case link_argc
292	atf_add_test_case link_basic
293	atf_add_test_case link_eexist
294	atf_add_test_case link_eisdir
295}
296