aboutsummaryrefslogtreecommitdiff
path: root/bin/ln/tests/ln_test.sh
diff options
context:
space:
mode:
Diffstat (limited to 'bin/ln/tests/ln_test.sh')
-rw-r--r--bin/ln/tests/ln_test.sh151
1 files changed, 99 insertions, 52 deletions
diff --git a/bin/ln/tests/ln_test.sh b/bin/ln/tests/ln_test.sh
index e1f5b47c262e..82bc556842d8 100644
--- a/bin/ln/tests/ln_test.sh
+++ b/bin/ln/tests/ln_test.sh
@@ -1,4 +1,6 @@
#
+# SPDX-License-Identifier: BSD-2-Clause
+#
# Copyright 2017 Shivansh Rai
# All rights reserved.
#
@@ -23,14 +25,15 @@
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
-# $FreeBSD$
-#
-set_umask()
+atf_check_same_file()
+{
+ atf_check_equal "$(stat -f %d,%i "$1")" "$(stat -f %d,%i "$2")"
+}
+
+atf_check_symlink_to()
{
- if ! umask 022; then
- atf_fail "setting umask failed"
- fi
+ atf_check -o inline:"$1\n" readlink "$2"
}
atf_test_case L_flag
@@ -40,18 +43,13 @@ L_flag_head()
"symbolic link, '-L' option creates a hard" \
"link to the target of the symbolic link"
}
-
L_flag_body()
{
- set_umask
atf_check touch A
atf_check ln -s A B
atf_check ln -L B C
- stat_A=$(stat -f %i A)
- stat_C=$(stat -f %i C)
- atf_check_equal "$stat_A" "$stat_C"
- atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
- atf_check -o inline:'A\n' readlink B
+ atf_check_same_file A C
+ atf_check_symlink_to A B
}
atf_test_case P_flag
@@ -61,16 +59,12 @@ P_flag_head()
"symbolic link, '-P' option creates a hard " \
"link to the symbolic link itself"
}
-
P_flag_body()
{
- set_umask
atf_check touch A
atf_check ln -s A B
atf_check ln -P B C
- stat_B=$(stat -f %i B)
- stat_C=$(stat -f %i C)
- atf_check_equal "$stat_B" "$stat_C"
+ atf_check_same_file B C
}
atf_test_case f_flag
@@ -79,15 +73,11 @@ 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()
{
- set_umask
atf_check touch A B
atf_check ln -f A B
- stat_A=$(stat -f %i A)
- stat_B=$(stat -f %i B)
- atf_check_equal "$stat_A" "$stat_B"
+ atf_check_same_file A B
}
atf_test_case target_exists_hard
@@ -96,13 +86,11 @@ target_exists_hard_head()
atf_set "descr" "Verify whether creating a hard link fails if the " \
"target file already exists"
}
-
target_exists_hard_body()
{
- set_umask
atf_check touch A B
atf_check -s exit:1 -e inline:'ln: B: File exists\n' \
- ln A B
+ ln A B
}
atf_test_case target_exists_symbolic
@@ -111,13 +99,11 @@ target_exists_symbolic_head()
atf_set "descr" "Verify whether creating a symbolic link fails if " \
"the target file already exists"
}
-
target_exists_symbolic_body()
{
- set_umask
atf_check touch A B
atf_check -s exit:1 -e inline:'ln: B: File exists\n' \
- ln -s A B
+ ln -s A B
}
atf_test_case shf_flag_dir
@@ -125,13 +111,12 @@ 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 -o inline:'Symbolic Link\n' stat -f %SHT C
+ atf_check test -L C
atf_check -o inline:'B\n' readlink C
}
@@ -140,14 +125,12 @@ 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 -o inline:'Symbolic Link\n' stat -f %SHT C
- atf_check -o inline:'B\n' readlink C
+ atf_check_symlink_to B C
}
atf_test_case sF_flag
@@ -157,13 +140,11 @@ sF_flag_head()
"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 -o inline:'Symbolic Link\n' stat -f %SHT B
- atf_check -o inline:'A\n' readlink B
+ atf_check_symlink_to A B
}
atf_test_case sf_flag
@@ -173,14 +154,27 @@ sf_flag_head()
"'-sf' option unlinks it and creates a symbolic link " \
"to the source file"
}
-
sf_flag_body()
{
- set_umask
atf_check touch A B
atf_check ln -sf A B
- atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
- atf_check -o inline:'A\n' readlink 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
@@ -188,14 +182,11 @@ s_flag_head()
{
atf_set "descr" "Verify that '-s' option creates a symbolic link"
}
-
s_flag_body()
{
- set_umask
atf_check touch A
atf_check ln -s A B
- atf_check -o inline:'Symbolic Link\n' stat -f %SHT B
- atf_check -o inline:'A\n' readlink B
+ atf_check_symlink_to A B
}
atf_test_case s_flag_broken
@@ -204,12 +195,10 @@ 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 -o inline:'Symbolic Link\n' stat -f %SHT B
- atf_check -o inline:'A\n' readlink B
+ atf_check_symlink_to A B
}
atf_test_case sw_flag
@@ -218,13 +207,66 @@ 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 -o inline:'Symbolic Link\n' stat -f %SHT B
- atf_check -o inline:'A\n' readlink B
+ 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()
@@ -238,7 +280,12 @@ atf_init_test_cases()
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
}