aboutsummaryrefslogtreecommitdiff
path: root/bin/ln/tests/ln_test.sh
blob: 82bc556842d85a05a935e458d40d33e754585c30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright 2017 Shivansh Rai
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

atf_check_same_file()
{
	atf_check_equal "$(stat -f %d,%i "$1")" "$(stat -f %d,%i "$2")"
}

atf_check_symlink_to()
{
	atf_check -o inline:"$1\n" readlink "$2"
}

atf_test_case L_flag
L_flag_head()
{
	atf_set "descr" "Verify that when creating a hard link to a " \
			"symbolic link, '-L' option creates a hard" \
			"link to the target of the symbolic link"
}
L_flag_body()
{
	atf_check touch A
	atf_check ln -s A B
	atf_check ln -L B C
	atf_check_same_file A C
	atf_check_symlink_to A B
}

atf_test_case P_flag
P_flag_head()
{
	atf_set "descr" "Verify that when creating a hard link to a " \
			"symbolic link, '-P' option creates a hard " \
			"link to the symbolic link itself"
}
P_flag_body()
{
	atf_check touch A
	atf_check ln -s A B
	atf_check ln -P B C
	atf_check_same_file B C
}

atf_test_case f_flag
f_flag_head()
{
	atf_set "descr" "Verify that if the target file already exists, " \
			"'-f' option unlinks it so that link may occur"
}
f_flag_body()
{
	atf_check touch A B
	atf_check ln -f A B
	atf_check_same_file A B
}

atf_test_case target_exists_hard
target_exists_hard_head()
{
	atf_set "descr" "Verify whether creating a hard link fails if the " \
			"target file already exists"
}
target_exists_hard_body()
{
	atf_check touch A B
	atf_check -s exit:1 -e inline:'ln: B: File exists\n' \
	    ln A B
}

atf_test_case target_exists_symbolic
target_exists_symbolic_head()
{
	atf_set "descr" "Verify whether creating a symbolic link fails if " \
			"the target file already exists"
}
target_exists_symbolic_body()
{
	atf_check touch A B
	atf_check -s exit:1 -e inline:'ln: B: File exists\n' \
	    ln -s A B
}

atf_test_case shf_flag_dir
shf_flag_dir_head() {
	atf_set "descr" "Verify that if the target directory is a symbolic " \
			"link, '-shf' option prevents following the link"
}
shf_flag_dir_body()
{
	atf_check mkdir -m 0777 A B
	atf_check ln -s A C
	atf_check ln -shf B C
	atf_check test -L C
	atf_check -o inline:'B\n' readlink C
}

atf_test_case snf_flag_dir
snf_flag_dir_head() {
	atf_set "descr" "Verify that if the target directory is a symbolic " \
			"link, '-snf' option prevents following the link"
}
snf_flag_dir_body()
{
	atf_check mkdir -m 0777 A B
	atf_check ln -s A C
	atf_check ln -snf B C
	atf_check_symlink_to B C
}

atf_test_case sF_flag
sF_flag_head()
{
	atf_set "descr" "Verify that if the target file already exists " \
			"and is a directory, then '-sF' option removes " \
			"it so that the link may occur"
}
sF_flag_body()
{
	atf_check mkdir A B
	atf_check ln -sF A B
	atf_check_symlink_to A B
}

atf_test_case sf_flag
sf_flag_head()
{
	atf_set "descr" "Verify that if the target file already exists, " \
			"'-sf' option unlinks it and creates a symbolic link " \
			"to the source file"
}
sf_flag_body()
{
	atf_check touch A B
	atf_check ln -sf A B
	atf_check_symlink_to A B
}

atf_test_case sfF_flag
sfF_flag_head()
{
	atf_set "descr" "Verify that if the target file already exists " \
			"and is a symlink, then '-sfF' option removes " \
			"it so that the link may occur"
}
sfF_flag_body()
{
	atf_check mkdir A B C
	atf_check ln -sF A C
	atf_check_symlink_to A C
	atf_check ln -sfF B C
	atf_check_symlink_to B C
}

atf_test_case s_flag
s_flag_head()
{
	atf_set "descr" "Verify that '-s' option creates a symbolic link"
}
s_flag_body()
{
	atf_check touch A
	atf_check ln -s A B
	atf_check_symlink_to A B
}

atf_test_case s_flag_broken
s_flag_broken_head()
{
	atf_set "descr" "Verify that if the source file does not exists, '-s' " \
			"option creates a broken symbolic link to the source file"
}
s_flag_broken_body()
{
	atf_check ln -s A B
	atf_check_symlink_to A B
}

atf_test_case sw_flag
sw_flag_head()
{
	atf_set "descr" "Verify that '-sw' option produces a warning if the " \
			"source of a symbolic link does not currently exist"
}
sw_flag_body()
{
	atf_check -s exit:0 -e inline:'ln: warning: A: No such file or directory\n' \
	    ln -sw A B
	atf_check_symlink_to A B
}

atf_test_case link_argc
link_argc_head() {
	atf_set "descr" "Verify that link(1) requires exactly two arguments"
}
link_argc_body() {
	atf_check -s exit:1 -e match:"usage: link" \
	    link foo
	atf_check -s exit:1 -e match:"No such file" \
	    link foo bar
	atf_check -s exit:1 -e match:"No such file" \
	    link -- foo bar
	atf_check -s exit:1 -e match:"usage: link" \
	    link foo bar baz
}

atf_test_case link_basic
link_basic_head() {
	atf_set "descr" "Verify that link(1) creates a link"
}
link_basic_body() {
	touch foo
	atf_check link foo bar
	atf_check_same_file foo bar
	rm bar
	ln -s foo bar
	atf_check link bar baz
	atf_check_same_file foo baz
}

atf_test_case link_eexist
link_eexist_head() {
	atf_set "descr" "Verify that link(1) fails if the target exists"
}
link_eexist_body() {
	touch foo bar
	atf_check -s exit:1 -e match:"bar.*exists" \
	    link foo bar
	ln -s non-existent baz
	atf_check -s exit:1 -e match:"baz.*exists" \
	    link foo baz
}

atf_test_case link_eisdir
link_eisdir_head() {
	atf_set "descr" "Verify that link(1) fails if the source is a directory"
}
link_eisdir_body() {
	mkdir foo
	atf_check -s exit:1 -e match:"foo.*directory" \
	    link foo bar
	ln -s foo bar
	atf_check -s exit:1 -e match:"bar.*directory" \
	    link bar baz
}

atf_init_test_cases()
{
	atf_add_test_case L_flag
	atf_add_test_case P_flag
	atf_add_test_case f_flag
	atf_add_test_case target_exists_hard
	atf_add_test_case target_exists_symbolic
	atf_add_test_case shf_flag_dir
	atf_add_test_case snf_flag_dir
	atf_add_test_case sF_flag
	atf_add_test_case sf_flag
	atf_add_test_case sfF_flag
	atf_add_test_case s_flag
	atf_add_test_case s_flag_broken
	atf_add_test_case sw_flag
	atf_add_test_case link_argc
	atf_add_test_case link_basic
	atf_add_test_case link_eexist
	atf_add_test_case link_eisdir
}