~sircmpwn/ctools

af1632adc7c1d84ab4d6c8604c82bc5a30c0a7e1 — Drew DeVault 2 years ago 51b43b1
chmod: add conformance test
2 files changed, 43 insertions(+), 9 deletions(-)

M test/chmod
M test/meson.build
M test/chmod => test/chmod +42 -9
@@ 1,8 1,10 @@
#!/bin/sh
# vim: ft=txt :
# vim syntax breaks on this file due to weird arithmetic
. "$HARNESS" chmod

# TODO:
# - setuid/setguid/sticky
# - a+X behavior

assert_mode() (
	mode="$1"
	shift


@@ 12,7 14,7 @@ assert_mode() (
		base="$(basename "$path")"
		if [ "$(find "$dir" -name "$base" -perm "$mode")" != "$path" ]
		then
			printf "(%s:mode != %s) " "$path" "$mode"
			printf "(%s:mode != %o) " "$path" "$mode"
			return 1
		fi
	done


@@ 39,6 41,10 @@ octtodec() {

# should_handle_syms op baseline expected_result_fn
should_handle_syms() (
	op="$1"
	base="$2"
	check="$3"

	f="$TMPDIR"/symbolic
	touch "$f"



@@ 70,29 76,56 @@ should_handle_syms() (
			dec=$((dec&mask))
			oct=$(printf '%o' $dec)

			chmod 0 "$f"
			chmod $base "$f"

			chmod ${who}+${sym} "$f" || return 1
			assert_mode $oct "$f" || return 1
			chmod ${who}${op}${sym} "$f" || return 1
			assert_mode "$(printf '%o' $($check $dec))" "$f" || return 1

			mask=$((mask<<3))
		done
	done
	ret=$?

	rm "$TMPDIR"/map
	chmod 644 "$f"
	rm "$f"

	return $ret
)

should_handle_add_syms() (
	should_handle_syms "+" 000 echo
	should_handle_syms + 000 echo
)

expected_rem() (
	dec="$1"
	echo $((777-dec))
	echo $((511-dec))
)

should_handle_rem_syms() (
	should_handle_syms - 777 expected_rem
)

expected_clear() {
	dec="$1"
	dec=$((~dec))
	echo $((511&dec))
}

should_handle_clear_syms() (
	should_handle_syms = 777 expected_clear
)

should_handle_recursive() (
	mkdir -p "$TMPDIR"/recursive/foo/bar
	touch "$TMPDIR"/recursive/foo/bar/baz
	chmod -R 777 "$TMPDIR"/recursive
	assert_mode 777 "$TMPDIR"/recursive/foo/bar/baz
)

runtests \
	should_handle_recursive \
	should_handle_octal_modes \
	should_handle_add_syms
	should_handle_add_syms \
	should_handle_rem_syms \
	should_handle_clear_syms

M test/meson.build => test/meson.build +1 -0
@@ 5,6 5,7 @@ test_files = [
	'cat',
	'chgrp',
	'chown',
	'chmod',
	'cksum',
	'cmp',
	'false',