~sircmpwn/ctools

9f111c815262d1418c6549ad184724389c3127be — Drew DeVault 2 years ago 9816f64
chown: add conformance tests
2 files changed, 219 insertions(+), 0 deletions(-)

A test/chown
M test/meson.build
A test/chown => test/chown +218 -0
@@ 0,0 1,218 @@
#!/bin/sh
. "$HARNESS" chown

auxgroup=${AUXGROUP:-}
mainuser=$(id -un)
maingroup="$(id -gn)"
for grp in $(id -Gn)
do
	if [ $grp != "$maingroup" ]
	then
		auxgroup=$grp
		break
	fi
done
[ -z "$auxgroup" ] && ! echo "Unable to select auxillary group for testing" >&2

assert_grp() (
	grp="$1"
	shift
	for path in $*
	do
		dir="$(dirname "$path")"
		base="$(basename "$path")"
		if [ "$(find "$dir" -name "$base" -group "$grp")" != "$path" ]
		then
			printf "(%s:group != %s) " "$path" "$grp"
			return 1
		fi
	done
)

assert_usr() (
	usr="$1"
	shift
	for path in $*
	do
		dir="$(dirname "$path")"
		base="$(basename "$path")"
		if [ "$(find "$dir" -name "$base" -user "$usr")" != "$path" ]
		then
			printf "(%s:user != %s) " "$path" "$usr"
			return 1
		fi
	done
)

should_handle_simple() (
	path="$TMPDIR"/simple
	touch "$path"
	assert_grp "$maingroup" "$path" || return 1
	chown "$mainuser":"$auxgroup" "$path" || return 1
	assert_grp "$auxgroup" "$path" || return 1
)

should_handle_mutex_options() (
	! chgrp -hR "$auxgroup" "$TMPDIR" 2>/dev/null
)

should_handle_several() (
	path_1="$TMPDIR"/several-1
	path_2="$TMPDIR"/several-2
	touch "$path_1" "$path_2"
	assert_grp "$maingroup" "$path_1" "$path_2" || return 1
	chown "$mainuser":"$auxgroup" "$path_1" "$path_2" || return 1
	assert_grp "$auxgroup" "$path_1" "$path_2" || return 1
)

should_handle_recursive() (
	mkdir -p "$TMPDIR"/recursive/subdir
	path_1="$TMPDIR"/recursive/recursive-1
	path_2="$TMPDIR"/recursive/subdir/recursive-2
	touch "$path_1" "$path_2"

	assert_grp "$maingroup" "$path_1" "$path_2" || return 1
	chown -R "$mainuser":"$auxgroup" "$TMPDIR"/recursive || return 1
	assert_grp "$auxgroup" "$path_1" "$path_2" || return 1
)

should_handle_recursive_Hflag() (
	# -H: If the -R option is specified and a symbolic link referencing a
	# file of type directory is specified on the command line, chgrp shall
	# change the group of the directory referenced by the symbolic link and
	# all files in the file hierarchy below it.
	dir_1="$TMPDIR"/recursive_hflag
	dir_2="$TMPDIR"/recursive_hflag2
	dir_3="$TMPDIR"/recursive_hflag3
	path_1="$dir_1"/file1
	path_2="$dir_2"/file2
	path_3="$dir_3"/file3
	explicit_link="$dir_1"/explicit
	implicit_link="$dir_1"/implicit
	mkdir -p "$dir_1" "$dir_2" "$dir_3"
	ln -s "$dir_2" "$explicit_link"
	ln -s "$dir_3" "$implicit_link"
	touch "$path_1" "$path_2" "$path_3"

	# Assert that initial state matches expectations
	assert_grp "$maingroup" "$path_1" "$path_2" "$path_3" \
		"$dir_1" "$dir_2" "$dir_3" \
		"$explicit_link" "$implicit_link" || return 1

	# If repeated, the last -HLP option is effective
	chown -RHLPH "$mainuser":"$auxgroup" "$dir_1" "$explicit_link" || return 1

	# Explicit link target and children updated
	assert_grp "$auxgroup" "$path_1" "$path_2" "$dir_2" || return 1
	# Explicit link unchanged
	assert_grp "$maingroup" "$explicit_link" || return 1
	# Implicit link target updated
	assert_grp "$auxgroup" "$dir_3" || return 1
	# Implicit link children unchanged
	assert_grp "$maingroup" "$path_3" || return 1
	# Implicit link unchanged
	assert_grp "$maingroup" "$implicit_link" || return 1
)

should_handle_recursive_Lflag() (
	# -L: If the -R option is specified and a symbolic link referencing a
	# file of type directory is specified on the command line or
	# encountered during the traversal of a file hierarchy, chgrp shall
	# change the group of the directory referenced by the symbolic link and
	# all files in the file hierarchy below it.
	dir_1="$TMPDIR"/recursive_lflag
	dir_2="$TMPDIR"/recursive_lflag2
	dir_3="$TMPDIR"/recursive_lflag3
	path_1="$dir_1"/file1
	path_2="$dir_2"/file2
	path_3="$dir_3"/file3
	explicit_link="$dir_1"/explicit
	implicit_link="$dir_1"/implicit
	mkdir -p "$dir_1" "$dir_2" "$dir_3"
	ln -s "$dir_2" "$explicit_link"
	ln -s "$dir_3" "$implicit_link"
	touch "$path_1" "$path_2" "$path_3"

	# Assert that initial state matches expectations
	assert_grp "$maingroup" "$path_1" "$path_2" "$path_3" \
		"$dir_1" "$dir_2" "$dir_3" \
		"$explicit_link" "$implicit_link" || return 1

	# If repeated, the last -HLP option is effective
	chown -RHLPL "$mainuser":"$auxgroup" "$dir_1" "$explicit_link" || return 1

	# Explicit link target and children updated
	assert_grp "$auxgroup" "$path_1" "$path_2" "$dir_2" || return 1
	# Explicit link unchanged
	assert_grp "$maingroup" "$explicit_link" || return 1
	# Implicit link target and children updated
	assert_grp "$auxgroup" "$path_3" "$dir_3" || return 1
	# Implicit link unchanged
	assert_grp "$maingroup" "$implicit_link" || return 1
)

should_handle_recursive_Pflag() (
	# -P: If the -R option is specified and a symbolic link is specified on
	# the command line or encountered during the traversal of a file
	# hierarchy, chgrp shall change the group ID of the symbolic link. The
	# chgrp utility shall not follow the symbolic link to any other part of
	# the file hierarchy.
	dir_1="$TMPDIR"/recursive_pflag
	dir_2="$TMPDIR"/recursive_pflag2
	dir_3="$TMPDIR"/recursive_pflag3
	path_1="$dir_1"/file1
	path_2="$dir_2"/file2
	path_3="$dir_3"/file3
	explicit_link="$dir_1"/explicit
	implicit_link="$dir_1"/implicit
	mkdir -p "$dir_1" "$dir_2" "$dir_3"
	ln -s "$dir_2" "$explicit_link"
	ln -s "$dir_3" "$implicit_link"
	touch "$path_1" "$path_2" "$path_3"

	# Assert that initial state matches expectations
	assert_grp "$maingroup" "$path_1" "$path_2" "$path_3" \
		"$dir_1" "$dir_2" "$dir_3" \
		"$explicit_link" "$implicit_link" || return 1

	# If repeated, the last -HLP option is effective
	chown -RHLPP "$mainuser":"$auxgroup" "$dir_1" "$explicit_link" || return 1

	# Explicit link target and children updated
	assert_grp "$auxgroup" "$path_1" || return 1
	# Explicit link updated
	assert_grp "$auxgroup" "$explicit_link" || return 1
	# Implicit link target and children unchanged
	assert_grp "$maingroup" "$dir_2" "$path_2" "$path_3" "$dir_3" || return 1
	# Implicit link updated
	assert_grp "$auxgroup" "$implicit_link" || return 1
)

should_handle_owner_change() (
	if [ -z "${ESCALATE:-}" ]
	then
		echo 'SKIP($ESCALATE undefined) '
		return
	fi
	if ! id nobody >/dev/null 2>&1
	then
		echo 'SKIP(No "nobody" user) '
		return
	fi
	path="$TMPDIR"/owner_change
	touch "$path"
	assert_usr "$mainuser" "$path" || return 1
	$ESCALATE chown nobody "$path" || return 1
	assert_usr nobody "$path" || return 1
	$ESCALATE chown "$mainuser" "$path" || return 1
)

runtests \
	should_handle_simple \
	should_handle_several \
	should_handle_mutex_options \
	should_handle_recursive \
	should_handle_recursive_Hflag \
	should_handle_recursive_Lflag \
	should_handle_recursive_Pflag \
	should_handle_owner_change

M test/meson.build => test/meson.build +1 -0
@@ 4,6 4,7 @@ test_files = [
	'basename',
	'cat',
	'chgrp',
	'chown',
]

foreach test_file : test_files