[Midnightbsd-cvs] src [11199] trunk/bin/ln/tests: add tests

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sun Jul 1 11:53:26 EDT 2018


Revision: 11199
          http://svnweb.midnightbsd.org/src/?rev=11199
Author:   laffer1
Date:     2018-07-01 11:53:25 -0400 (Sun, 01 Jul 2018)
Log Message:
-----------
add tests

Modified Paths:
--------------
    trunk/bin/ln/Makefile
    trunk/bin/ln/ln.1
    trunk/bin/ln/ln.c
    trunk/bin/ln/symlink.7

Added Paths:
-----------
    trunk/bin/ln/tests/
    trunk/bin/ln/tests/Makefile
    trunk/bin/ln/tests/ln_test.sh

Property Changed:
----------------
    trunk/bin/ln/ln.1
    trunk/bin/ln/symlink.7

Modified: trunk/bin/ln/Makefile
===================================================================
--- trunk/bin/ln/Makefile	2018-07-01 15:51:25 UTC (rev 11198)
+++ trunk/bin/ln/Makefile	2018-07-01 15:53:25 UTC (rev 11199)
@@ -1,7 +1,9 @@
+# $MidnightBSD$
 #	@(#)Makefile	8.2 (Berkeley) 5/31/93
-# $FreeBSD: src/bin/ln/Makefile,v 1.11 2001/12/04 01:57:44 obrien Exp $
-# $MidnightBSD$ 
+# $FreeBSD: stable/10/bin/ln/Makefile 321093 2017-07-17 21:12:02Z ngie $
 
+.include <bsd.own.mk>
+
 PROG=	ln
 MAN=	ln.1 symlink.7
 
@@ -8,4 +10,8 @@
 LINKS=	${BINDIR}/ln ${BINDIR}/link
 MLINKS=	ln.1 link.1
 
+.if ${MK_TESTS} != "no"
+SUBDIR+= 	tests
+.endif
+
 .include <bsd.prog.mk>

Modified: trunk/bin/ln/ln.1
===================================================================
--- trunk/bin/ln/ln.1	2018-07-01 15:51:25 UTC (rev 11198)
+++ trunk/bin/ln/ln.1	2018-07-01 15:53:25 UTC (rev 11199)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
 .\"-
 .\" Copyright (c) 1980, 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -30,10 +31,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"	@(#)ln.1	8.2 (Berkeley) 12/30/93
-.\" $FreeBSD: /repoman/r/ncvs/src/bin/ln/ln.1,v 1.30.2.1 2006/03/18 21:49:43 glebius Exp $
-.\" $MidnightBSD: src/bin/ln/ln.1,v 1.4 2007/07/26 20:13:00 laffer1 Exp $
+.\" $FreeBSD: stable/10/bin/ln/ln.1 321091 2017-07-17 21:10:08Z ngie $
 .\"
-.Dd July 17, 2009
+.Dd June 12, 2017
 .Dt LN 1
 .Os
 .Sh NAME
@@ -88,14 +88,18 @@
 or
 .Fl i
 options.
-If none is specified,
+If neither
 .Fl f
+nor
+.Fl i
+is specified,
+.Fl f
 is implied.
 The
 .Fl F
 option is a no-op unless
 .Fl s
-option is specified.
+is specified.
 .It Fl L
 When creating a hard link to a symbolic link,
 create a hard link to the target of the symbolic link.
@@ -213,6 +217,70 @@
 which performs a
 .Xr link 2
 operation using the two passed arguments.
+.Sh EXAMPLES
+Create a symbolic link named
+.Pa /home/src
+and point it to
+.Pa /usr/src :
+.Pp
+.Dl # ln -s /usr/src /home/src
+.Pp
+Hard link
+.Pa /usr/local/bin/fooprog
+to file
+.Pa /usr/local/bin/fooprog-1.0 :
+.Pp
+.Dl # ln /usr/local/bin/fooprog-1.0 /usr/local/bin/fooprog
+.Pp
+As an exercise, try the following commands:
+.Bd -literal -offset indent
+# ls -i /bin/[
+11553 /bin/[
+# ls -i /bin/test
+11553 /bin/test
+.Ed
+.Pp
+Note that both files have the same inode; that is,
+.Pa /bin/[
+is essentially an alias for the
+.Xr test 1
+command.
+This hard link exists so
+.Xr test 1
+may be invoked from shell scripts, for example, using the
+.Li "if [ ]"
+construct.
+.Pp
+In the next example, the second call to
+.Nm
+removes the original
+.Pa foo
+and creates a replacement pointing to
+.Pa baz :
+.Bd -literal -offset indent
+# mkdir bar baz
+# ln -s bar foo
+# ln -shf baz foo
+.Ed
+.Pp
+Without the
+.Fl h
+option, this would instead leave
+.Pa foo
+pointing to
+.Pa bar
+and inside
+.Pa foo
+create a new symlink
+.Pa baz
+pointing to itself.
+This results from directory-walking.
+.Pp
+An easy rule to remember is that the argument order for
+.Nm
+is the same as for
+.Xr cp 1 :
+The first argument needs to exist, the second one is created.
 .Sh COMPATIBILITY
 The
 .Fl h ,


Property changes on: trunk/bin/ln/ln.1
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/bin/ln/ln.c
===================================================================
--- trunk/bin/ln/ln.c	2018-07-01 15:51:25 UTC (rev 11198)
+++ trunk/bin/ln/ln.c	2018-07-01 15:53:25 UTC (rev 11199)
@@ -1,4 +1,4 @@
-/* $MidnightBSD: src/bin/ln/ln.c,v 1.4 2007/07/26 20:13:00 laffer1 Exp $ */
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1987, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -40,7 +40,7 @@
 #endif /* not lint */
 #endif
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: /repoman/r/ncvs/src/bin/ln/ln.c,v 1.33.2.1 2006/03/18 21:49:43 glebius Exp $");
+__FBSDID("$FreeBSD: stable/10/bin/ln/ln.c 321094 2017-07-17 21:13:43Z ngie $");
 
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -55,19 +55,19 @@
 #include <string.h>
 #include <unistd.h>
 
-int	fflag;				/* Unlink existing files. */
-int	Fflag;				/* Remove empty directories also. */
-int	hflag;				/* Check new name for symlink first. */
-int	iflag;				/* Interactive mode. */
-int	Pflag;				/* Create hard links to symlinks. */
-int	sflag;				/* Symbolic, not hard, link. */
-int	vflag;				/* Verbose output. */
-int	wflag;				/* Warn if symlink target does not
+static int	fflag;			/* Unlink existing files. */
+static int	Fflag;			/* Remove empty directories also. */
+static int	hflag;			/* Check new name for symlink first. */
+static int	iflag;			/* Interactive mode. */
+static int	Pflag;			/* Create hard links to symlinks. */
+static int	sflag;			/* Symbolic, not hard, link. */
+static int	vflag;			/* Verbose output. */
+static int	wflag;			/* Warn if symlink target does not
 					 * exist, and -f is not enabled. */
-char	linkch;
+static char	linkch;
 
-int	linkit(const char *, const char *, int);
-void	usage(void);
+static int	linkit(const char *, const char *, int);
+static void	usage(void);
 
 int
 main(int argc, char *argv[])
@@ -220,7 +220,7 @@
 	return sb1.st_dev == sb2.st_dev && sb1.st_ino == sb2.st_ino;
 }
 
-int
+static int
 linkit(const char *source, const char *target, int isdir)
 {
 	struct stat sb;
@@ -246,11 +246,11 @@
 
 	/*
 	 * If the target is a directory (and not a symlink if hflag),
-	 * append the source's name.
+	 * append the source's name, unless Fflag is set.
 	 */
-	if (isdir ||
+	if (!Fflag && (isdir ||
 	    (lstat(target, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
-	    (!hflag && stat(target, &sb) == 0 && S_ISDIR(sb.st_mode))) {
+	    (!hflag && stat(target, &sb) == 0 && S_ISDIR(sb.st_mode)))) {
 		if (strlcpy(bbuf, source, sizeof(bbuf)) >= sizeof(bbuf) ||
 		    (p = basename(bbuf)) == NULL ||
 		    snprintf(path, sizeof(path), "%s/%s", target, p) >=
@@ -348,7 +348,7 @@
 	return (0);
 }
 
-void
+static void
 usage(void)
 {
 	(void)fprintf(stderr, "%s\n%s\n%s\n",

Modified: trunk/bin/ln/symlink.7
===================================================================
--- trunk/bin/ln/symlink.7	2018-07-01 15:51:25 UTC (rev 11198)
+++ trunk/bin/ln/symlink.7	2018-07-01 15:53:25 UTC (rev 11199)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
 .\"-
 .\" Copyright (c) 1992, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
@@ -27,10 +28,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"	@(#)symlink.7	8.3 (Berkeley) 3/31/94
-.\" $FreeBSD: src/bin/ln/symlink.7,v 1.30 2005/02/13 22:25:09 ru Exp $
-.\" $MidnightBSD: src/bin/ln/symlink.7,v 1.2 2006/07/19 13:48:59 laffer1 Exp $
+.\" $FreeBSD: stable/10/bin/ln/symlink.7 279866 2015-03-10 22:23:56Z jilles $
 .\"
-.Dd April 25, 2010
+.Dd February 16, 2015
 .Dt SYMLINK 7
 .Os
 .Sh NAME
@@ -147,6 +147,7 @@
 unless given the
 .Dv AT_SYMLINK_NOFOLLOW
 flag:
+.Xr chflagsat 2 ,
 .Xr fchmodat 2 ,
 .Xr fchownat 2
 and
@@ -220,7 +221,7 @@
 .Dq Li slink
 itself.
 .Pp
-There are four exceptions to this rule.
+There are five exceptions to this rule.
 The
 .Xr mv 1
 and
@@ -263,13 +264,12 @@
 .Pp
 The
 .Xr file 1
-command is also an exception to this rule.
-The
-.Xr file 1
-command does not follow symbolic links named as argument by default.
-The
-.Xr file 1
-command does follow symbolic links named as argument if
+and
+.Xr stat 1
+commands are also exceptions to this rule.
+These
+commands do not follow symbolic links named as argument by default,
+but do follow symbolic links named as argument if the
 .Fl L
 option is specified.
 .Pp


Property changes on: trunk/bin/ln/symlink.7
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: trunk/bin/ln/tests/Makefile
===================================================================
--- trunk/bin/ln/tests/Makefile	                        (rev 0)
+++ trunk/bin/ln/tests/Makefile	2018-07-01 15:53:25 UTC (rev 11199)
@@ -0,0 +1,6 @@
+# $MidnightBSD$
+# $FreeBSD: stable/10/bin/ln/tests/Makefile 319714 2017-06-08 19:09:55Z asomers $
+
+ATF_TESTS_SH+=  ln_test
+
+.include <bsd.test.mk>


Property changes on: trunk/bin/ln/tests/Makefile
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: trunk/bin/ln/tests/ln_test.sh
===================================================================
--- trunk/bin/ln/tests/ln_test.sh	                        (rev 0)
+++ trunk/bin/ln/tests/ln_test.sh	2018-07-01 15:53:25 UTC (rev 11199)
@@ -0,0 +1,237 @@
+#
+# 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.
+#
+# $FreeBSD: stable/10/bin/ln/tests/ln_test.sh 321819 2017-07-31 21:42:18Z asomers $
+# $MidnightBSD$
+
+set_umask()
+{
+        if ! umask 022; then
+                atf_fail "setting umask failed"
+        fi
+}
+
+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()
+{
+        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:'B: symbolic link to A\n' file 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()
+{
+        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_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()
+{
+        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_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()
+{
+        set_umask
+        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()
+{
+        set_umask
+        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 -o inline:'C: symbolic link to B\n' file 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 -o inline:'C: symbolic link to B\n' file 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 -o inline:'Symbolic Link\n' stat -f %SHT B
+	atf_check -o inline:'A\n' readlink 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()
+{
+        set_umask
+        atf_check touch A B
+        atf_check ln -sf A B
+        atf_check -o inline:'B: symbolic link to A\n' file B
+}
+
+atf_test_case s_flag
+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:'B: symbolic link to A\n' file 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 -o inline:'B: broken symbolic link to A\n' file 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 -o inline:'B: broken symbolic link to A\n' file B
+}
+
+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 s_flag
+        atf_add_test_case s_flag_broken
+        atf_add_test_case sw_flag
+}


Property changes on: trunk/bin/ln/tests/ln_test.sh
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property


More information about the Midnightbsd-cvs mailing list