[Midnightbsd-cvs] src [11354] trunk/bin/chmod: add tests

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Fri Jul 6 08:05:27 EDT 2018


Revision: 11354
          http://svnweb.midnightbsd.org/src/?rev=11354
Author:   laffer1
Date:     2018-07-06 08:05:26 -0400 (Fri, 06 Jul 2018)
Log Message:
-----------
add tests

Modified Paths:
--------------
    trunk/bin/chmod/Makefile
    trunk/bin/chmod/chmod.1
    trunk/bin/chmod/chmod.c

Added Paths:
-----------
    trunk/bin/chmod/tests/
    trunk/bin/chmod/tests/Makefile
    trunk/bin/chmod/tests/chmod_test.sh

Property Changed:
----------------
    trunk/bin/chmod/chmod.1

Modified: trunk/bin/chmod/Makefile
===================================================================
--- trunk/bin/chmod/Makefile	2018-07-06 12:04:38 UTC (rev 11353)
+++ trunk/bin/chmod/Makefile	2018-07-06 12:05:26 UTC (rev 11354)
@@ -1,7 +1,13 @@
+# $MidnightBSD$
 #	@(#)Makefile	8.1 (Berkeley) 5/31/93
-# $FreeBSD: src/bin/chmod/Makefile,v 1.10 2002/07/15 12:08:21 sheldonh Exp $
-# $MidnightBSD$
+# $FreeBSD: stable/10/bin/chmod/Makefile 321130 2017-07-18 16:27:10Z ngie $
 
+.include <bsd.own.mk>
+
 PROG=	chmod
 
+.if ${MK_TESTS} != "no"
+SUBDIR+=	tests
+.endif
+
 .include <bsd.prog.mk>

Modified: trunk/bin/chmod/chmod.1
===================================================================
--- trunk/bin/chmod/chmod.1	2018-07-06 12:04:38 UTC (rev 11353)
+++ trunk/bin/chmod/chmod.1	2018-07-06 12:05:26 UTC (rev 11354)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
 .\"-
 .\" Copyright (c) 1989, 1990, 1993, 1994
 .\"	The Regents of the University of California.  All rights reserved.
@@ -30,10 +31,9 @@
 .\" SUCH DAMAGE.
 .\"
 .\"	@(#)chmod.1	8.4 (Berkeley) 3/31/94
-.\" $FreeBSD: src/bin/chmod/chmod.1,v 1.39 2005/02/13 23:45:45 ru Exp $
-.\" $MidnightBSD: src/bin/chmod/chmod.1,v 1.4 2008/06/30 01:34:02 laffer1 Exp $
+.\" $FreeBSD: stable/10/bin/chmod/chmod.1 283875 2015-06-01 09:04:57Z smh $
 .\"
-.Dd January 26, 2009
+.Dd April 20, 2015
 .Dt CHMOD 1
 .Os
 .Sh NAME
@@ -64,9 +64,9 @@
 .It Fl H
 If the
 .Fl R
-option is specified, symbolic links on the command line are followed.
-(Symbolic links encountered in the tree traversal are not followed by
-default.)
+option is specified, symbolic links on the command line are followed
+and hence unaffected by the command.
+(Symbolic links encountered during tree traversal are not followed.)
 .It Fl h
 If the file is a symbolic link, change the mode of the link itself
 rather than the file that the link points to.
@@ -80,8 +80,12 @@
 option is specified, no symbolic links are followed.
 This is the default.
 .It Fl R
-Change the modes of the file hierarchies rooted in the files
+Change the modes of the file hierarchies rooted in the files,
 instead of just the files themselves.
+Beware of unintentionally matching the
+.Dq Pa ".."
+hard link to the parent directory when using wildcards like
+.Dq Li ".*" .
 .It Fl v
 Cause
 .Nm


Property changes on: trunk/bin/chmod/chmod.1
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/bin/chmod/chmod.c
===================================================================
--- trunk/bin/chmod/chmod.c	2018-07-06 12:04:38 UTC (rev 11353)
+++ trunk/bin/chmod/chmod.c	2018-07-06 12:05:26 UTC (rev 11354)
@@ -1,4 +1,4 @@
-/* $MidnightBSD: src/bin/chmod/chmod.c,v 1.3 2007/07/26 20:12:59 laffer1 Exp $ */
+/* $MidnightBSD$ */
 /*-
  * Copyright (c) 1989, 1993, 1994
  *	The Regents of the University of California.  All rights reserved.
@@ -27,7 +27,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  */
-/* $FreeBSD: src/bin/chmod/chmod.c,v 1.33 2005/01/10 08:39:20 imp Exp $ */
+
 #if 0
 #ifndef lint
 static char const copyright[] =
@@ -40,6 +40,7 @@
 #endif /* not lint */
 #endif
 #include <sys/cdefs.h>
+__FBSDID("$FreeBSD: stable/10/bin/chmod/chmod.c 306976 2016-10-10 16:07:23Z sevan $");
 
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -46,6 +47,7 @@
 
 #include <err.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <fts.h>
 #include <limits.h>
 #include <stdio.h>
@@ -62,7 +64,7 @@
 	FTS *ftsp;
 	FTSENT *p;
 	mode_t *set;
-	int Hflag, Lflag, Rflag, ch, error, fflag, fts_options, hflag, rval;
+	int Hflag, Lflag, Rflag, ch, fflag, fts_options, hflag, rval;
 	int vflag;
 	char *mode;
 	mode_t newmode;
@@ -90,12 +92,11 @@
 			break;
 		case 'h':
 			/*
-			 * In System V (and probably POSIX.2) the -h option
-			 * causes chmod to change the mode of the symbolic
-			 * link.  4.4BSD's symbolic links didn't have modes,
-			 * so it was an undocumented noop.  In FreeBSD 3.0,
-			 * lchmod(2) is introduced and this option does real
-			 * work.
+			 * In System V the -h option causes chmod to change
+			 * the mode of the symbolic link. 4.4BSD's symbolic
+			 * links didn't have modes, so it was an undocumented
+			 * noop.  In FreeBSD 3.0, lchmod(2) is introduced and
+			 * this option does real work.
 			 */
 			hflag = 1;
 			break;
@@ -126,18 +127,23 @@
 		usage();
 
 	if (Rflag) {
-		fts_options = FTS_PHYSICAL;
 		if (hflag)
-			errx(1,
-		"the -R and -h options may not be specified together.");
-		if (Hflag)
-			fts_options |= FTS_COMFOLLOW;
+			errx(1, "the -R and -h options may not be "
+			    "specified together.");
 		if (Lflag) {
-			fts_options &= ~FTS_PHYSICAL;
-			fts_options |= FTS_LOGICAL;
+			fts_options = FTS_LOGICAL;
+		} else {
+			fts_options = FTS_PHYSICAL;
+
+			if (Hflag) {
+				fts_options |= FTS_COMFOLLOW;
+			}
 		}
-	} else
-		fts_options = hflag ? FTS_PHYSICAL : FTS_LOGICAL;
+	} else if (hflag) {
+		fts_options = FTS_PHYSICAL;
+	} else {
+		fts_options = FTS_LOGICAL;
+	}
 
 	mode = *argv;
 	if ((set = setmode(mode)) == NULL)
@@ -146,12 +152,21 @@
 	if ((ftsp = fts_open(++argv, fts_options, 0)) == NULL)
 		err(1, "fts_open");
 	for (rval = 0; (p = fts_read(ftsp)) != NULL;) {
+		int atflag;
+
+		if ((fts_options & FTS_LOGICAL) ||
+		    ((fts_options & FTS_COMFOLLOW) &&
+		    p->fts_level == FTS_ROOTLEVEL))
+			atflag = 0;
+		else
+			atflag = AT_SYMLINK_NOFOLLOW;
+
 		switch (p->fts_info) {
 		case FTS_D:			/* Change it at FTS_DP. */
 			if (!Rflag)
 				fts_set(ftsp, p, FTS_SKIP);
 			continue;
-		case FTS_DNR:			/* Warn, chmod, continue. */
+		case FTS_DNR:			/* Warn, chmod. */
 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
 			rval = 1;
 			break;
@@ -160,16 +175,6 @@
 			warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
 			rval = 1;
 			continue;
-		case FTS_SL:			/* Ignore. */
-		case FTS_SLNONE:
-			/*
-			 * The only symlinks that end up here are ones that
-			 * don't point to anything and ones that we found
-			 * doing a physical walk.
-			 */
-			if (!hflag)
-				continue;
-			/* FALLTHROUGH */
 		default:
 			break;
 		}
@@ -182,32 +187,25 @@
 		if (may_have_nfs4acl(p, hflag) == 0 &&
 		    (newmode & ALLPERMS) == (p->fts_statp->st_mode & ALLPERMS))
 				continue;
-		if (hflag)
-			error = lchmod(p->fts_accpath, newmode);
-		else
-			error = chmod(p->fts_accpath, newmode);
-		if (error) {
-			if (!fflag) {
-				warn("%s", p->fts_path);
-				rval = 1;
-			}
-		} else {
-			if (vflag) {
-				(void)printf("%s", p->fts_path);
+		if (fchmodat(AT_FDCWD, p->fts_accpath, newmode, atflag) == -1
+		    && !fflag) {
+			warn("%s", p->fts_path);
+			rval = 1;
+		} else if (vflag) {
+			(void)printf("%s", p->fts_path);
 
-				if (vflag > 1) {
-					char m1[12], m2[12];
+			if (vflag > 1) {
+				char m1[12], m2[12];
 
-					strmode(p->fts_statp->st_mode, m1);
-					strmode((p->fts_statp->st_mode &
-					    S_IFMT) | newmode, m2);
-					(void)printf(": 0%o [%s] -> 0%o [%s]",
-					    p->fts_statp->st_mode, m1,
-					    (p->fts_statp->st_mode & S_IFMT) |
-					    newmode, m2);
-				}
-				(void)printf("\n");
+				strmode(p->fts_statp->st_mode, m1);
+				strmode((p->fts_statp->st_mode &
+				    S_IFMT) | newmode, m2);
+				(void)printf(": 0%o [%s] -> 0%o [%s]",
+				    p->fts_statp->st_mode, m1,
+				    (p->fts_statp->st_mode & S_IFMT) |
+				    newmode, m2);
 			}
+			(void)printf("\n");
 		}
 	}
 	if (errno)

Added: trunk/bin/chmod/tests/Makefile
===================================================================
--- trunk/bin/chmod/tests/Makefile	                        (rev 0)
+++ trunk/bin/chmod/tests/Makefile	2018-07-06 12:05:26 UTC (rev 11354)
@@ -0,0 +1,6 @@
+# $MidnightBSD$
+# $FreeBSD: stable/10/bin/chmod/tests/Makefile 319642 2017-06-07 05:33:56Z ngie $
+
+ATF_TESTS_SH+=	chmod_test
+
+.include <bsd.test.mk>


Property changes on: trunk/bin/chmod/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/chmod/tests/chmod_test.sh
===================================================================
--- trunk/bin/chmod/tests/chmod_test.sh	                        (rev 0)
+++ trunk/bin/chmod/tests/chmod_test.sh	2018-07-06 12:05:26 UTC (rev 11354)
@@ -0,0 +1,178 @@
+#
+# Copyright (c) 2017 Dell EMC
+# 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/chmod/tests/chmod_test.sh 322759 2017-08-21 17:20:31Z gjb $
+# $MidnightBSD$
+
+get_filesystem()
+{
+	local mountpoint=$1
+
+	df -T $mountpoint | tail -n 1 | cut -wf 2
+}
+
+atf_test_case RH_flag
+RH_flag_head()
+{
+	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
+			"affect symlinks specified via the arguments when -H " \
+			"is specified"
+}
+RH_flag_body()
+{
+	atf_check mkdir -m 0777 -p A/B
+	atf_check ln -s B A/C
+	atf_check chmod -h 0777 A/C
+	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
+	atf_check chmod -RH 0700 A
+	atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C
+	atf_check chmod -RH 0600 A/C
+	atf_check -o inline:'40700\n40600\n120700\n' stat -f '%p' A A/B A/C
+}
+
+atf_test_case RL_flag
+RL_flag_head()
+{
+	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
+			"affect symlinks specified via the arguments when -L " \
+			"is specified"
+}
+RL_flag_body()
+{
+	atf_check mkdir -m 0777 -p A/B
+	atf_check ln -s B A/C
+	atf_check chmod -h 0777 A/C
+	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
+	atf_check chmod -RL 0700 A
+	atf_check -o inline:'40700\n40700\n120777\n' stat -f '%p' A A/B A/C
+	atf_check chmod -RL 0600 A/C
+	atf_check -o inline:'40700\n40600\n120777\n' stat -f '%p' A A/B A/C
+}
+
+atf_test_case RP_flag
+RP_flag_head()
+{
+	atf_set	"descr" "Verify that setting modes recursively via -R doesn't " \
+			"affect symlinks specified via the arguments when -P " \
+			"is specified"
+}
+RP_flag_body()
+{
+	atf_check mkdir -m 0777 -p A/B
+	atf_check ln -s B A/C
+	atf_check chmod -h 0777 A/C
+	atf_check -o inline:'40755\n40777\n120777\n' stat -f '%p' A A/B A/C
+	atf_check chmod -RP 0700 A
+	atf_check -o inline:'40700\n40700\n120700\n' stat -f '%p' A A/B A/C
+	atf_check chmod -RP 0600 A/C
+	atf_check -o inline:'40700\n40700\n120600\n' stat -f '%p' A A/B A/C
+}
+
+atf_test_case f_flag cleanup
+f_flag_head()
+{
+	atf_set	"descr" "Verify that setting a mode for a file with -f " \
+			"doesn't emit an error message/exit with a non-zero " \
+			"code"
+}
+
+f_flag_body()
+{
+	atf_check truncate -s 0 foo bar
+	atf_check chmod 0750 foo bar
+	case "$(get_filesystem .)" in
+	zfs)
+		atf_expect_fail "ZFS doesn't support UF_IMMUTABLE; returns EPERM - bug 221189"
+		;;
+	esac
+	atf_check chflags uchg foo
+	atf_check -e not-empty -s not-exit:0 chmod 0700 foo bar
+	atf_check -o inline:'100750\n100700\n' stat -f '%p' foo bar
+	atf_check -s exit:0 chmod -f 0600 foo bar
+	atf_check -o inline:'100750\n100600\n' stat -f '%p' foo bar
+}
+
+f_flag_cleanup()
+{
+	chflags 0 foo || :
+}
+
+atf_test_case h_flag
+h_flag_head()
+{
+	atf_set	"descr" "Verify that setting a mode for a file with -f " \
+			"doesn't emit an error message/exit with a non-zero " \
+			"code"
+}
+
+h_flag_body()
+{
+	atf_check truncate -s 0 foo
+	atf_check chmod 0600 foo
+	atf_check -o inline:'100600\n' stat -f '%p' foo
+	umask 0077
+	atf_check ln -s foo bar
+	atf_check -o inline:'100600\n120700\n' stat -f '%p' foo bar
+	atf_check chmod -h 0500 bar
+	atf_check -o inline:'100600\n120500\n' stat -f '%p' foo bar
+	atf_check chmod 0660 bar
+	atf_check -o inline:'100660\n120500\n' stat -f '%p' foo bar
+}
+
+atf_test_case v_flag
+v_flag_head()
+{
+	atf_set	"descr" "Verify that setting a mode with -v emits the file when " \
+			"doesn't emit an error message/exit with a non-zero " \
+			"code"
+}
+v_flag_body()
+{
+	atf_check truncate -s 0 foo bar
+	atf_check chmod 0600 foo
+	atf_check chmod 0750 bar
+	case "$(get_filesystem .)" in
+	zfs)
+		atf_expect_fail "ZFS updates mode for foo unnecessarily - bug 221188"
+		;;
+	esac
+	atf_check -o 'inline:bar\n' chmod -v 0600 foo bar
+	atf_check chmod -v 0600 foo bar
+	for f in foo bar; do
+		echo "$f: 0100600 [-rw------- ] -> 0100700 [-rwx------ ]";
+	done > output.txt
+	atf_check -o file:output.txt chmod -vv 0700 foo bar
+	atf_check chmod -vv 0700 foo bar
+}
+
+atf_init_test_cases()
+{
+	atf_add_test_case RH_flag
+	atf_add_test_case RL_flag
+	atf_add_test_case RP_flag
+	atf_add_test_case f_flag
+	atf_add_test_case h_flag
+	atf_add_test_case v_flag
+}


Property changes on: trunk/bin/chmod/tests/chmod_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