~sircmpwn/ctools

ref: 51b43b1ef945563db4700e7fae744bfa6bdd3e23 ctools/test/chmod -rw-r--r-- 1.6 KiB
51b43b1eDrew DeVault chmod wip test 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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