~sircmpwn/ctools

51b43b1ef945563db4700e7fae744bfa6bdd3e23 — Drew DeVault 2 years ago 48c580e
chmod wip test
1 files changed, 98 insertions(+), 0 deletions(-)

A test/chmod
A test/chmod => test/chmod +98 -0
@@ 0,0 1,98 @@
#!/bin/sh
# vim: ft=txt :
# vim syntax breaks on this file due to weird arithmetic
. "$HARNESS" chmod

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

should_handle_octal_modes() (
	f="$TMPDIR"/octal
	touch $f
	i=0
	while [ $i -le 511 ] # 0777 == 511
	do
		oct=$(printf '%o' $i)
		chmod $oct "$f" || return 1
		assert_mode $oct "$f" || return 1
		i=$((i+1))
	done
	rm $f
)

octtodec() {
	oct=$1
	awk 'BEGIN { printf "%d\n" '"0$oct"'; print }' | tail -n1
}

# should_handle_syms op baseline expected_result_fn
should_handle_syms() (
	f="$TMPDIR"/symbolic
	touch "$f"

	cat >"$TMPDIR"/map <<-EOF
	x 111
	w 222
	r 444
	wx 333
	xw 333
	rx 555
	xr 555
	rw 666
	wr 666
	rwx 777
	EOF

	cat "$TMPDIR"/map | while read testcase
	do
		mask=7
		for who in u:7 g:70 o:700 ug:77 uo:707 go:770 ugo:777
		do
			mask=$(echo "$who" | cut -d: -f2)
			mask=$(octtodec $mask)
			who=$(echo "$who" | cut -d: -f1)

			sym=$(echo "$testcase" | cut -d' ' -f1)
			oct=$(echo "$testcase" | cut -d' ' -f2)
			dec=$(octtodec $oct)
			dec=$((dec&mask))
			oct=$(printf '%o' $dec)

			chmod 0 "$f"

			chmod ${who}+${sym} "$f" || return 1
			assert_mode $oct "$f" || return 1

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

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

should_handle_add_syms() (
	should_handle_syms "+" 000 echo
)

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

runtests \
	should_handle_octal_modes \
	should_handle_add_syms