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_test_case link_argc
218link_argc_head() {
219	atf_set "descr" "Verify that link(1) requires exactly two arguments"
220}
221link_argc_body() {
222	atf_check -s exit:1 -e match:"usage: link" \
223	    link foo
224	atf_check -s exit:1 -e match:"No such file" \
225	    link foo bar
226	atf_check -s exit:1 -e match:"No such file" \
227	    link -- foo bar
228	atf_check -s exit:1 -e match:"usage: link" \
229	    link foo bar baz
230}
231
232atf_test_case link_basic
233link_basic_head() {
234	atf_set "descr" "Verify that link(1) creates a link"
235}
236link_basic_body() {
237	touch foo
238	atf_check link foo bar
239	atf_check_same_file foo bar
240	rm bar
241	ln -s foo bar
242	atf_check link bar baz
243	atf_check_same_file foo baz
244}
245
246atf_test_case link_eexist
247link_eexist_head() {
248	atf_set "descr" "Verify that link(1) fails if the target exists"
249}
250link_eexist_body() {
251	touch foo bar
252	atf_check -s exit:1 -e match:"bar.*exists" \
253	    link foo bar
254	ln -s non-existent baz
255	atf_check -s exit:1 -e match:"baz.*exists" \
256	    link foo baz
257}
258
259atf_test_case link_eisdir
260link_eisdir_head() {
261	atf_set "descr" "Verify that link(1) fails if the source is a directory"
262}
263link_eisdir_body() {
264	mkdir foo
265	atf_check -s exit:1 -e match:"foo.*directory" \
266	    link foo bar
267	ln -s foo bar
268	atf_check -s exit:1 -e match:"bar.*directory" \
269	    link bar baz
270}
271
272atf_init_test_cases()
273{
274	atf_add_test_case L_flag
275	atf_add_test_case P_flag
276	atf_add_test_case f_flag
277	atf_add_test_case target_exists_hard
278	atf_add_test_case target_exists_symbolic
279	atf_add_test_case shf_flag_dir
280	atf_add_test_case snf_flag_dir
281	atf_add_test_case sF_flag
282	atf_add_test_case sf_flag
283	atf_add_test_case sfF_flag
284	atf_add_test_case s_flag
285	atf_add_test_case s_flag_broken
286	atf_add_test_case sw_flag
287	atf_add_test_case link_argc
288	atf_add_test_case link_basic
289	atf_add_test_case link_eexist
290	atf_add_test_case link_eisdir
291}
292