1#!/bin/bash
2
3[ -f testing.sh ] && . testing.sh
4
5if [ "$(id -u)" -ne 0 ]
6then
7  echo "$SHOWSKIP: chgrp (not root)"
8  return 2>/dev/null
9  exit
10fi
11
12# We chgrp between "root" and the last group in /etc/group.
13GRP="$(sed -n '$s/:.*//p' /etc/group)"
14# Or if that fails, assume we're on Android and pick a well-known group.
15: "${GRP:=shell}"
16
17# Set up a little testing hierarchy
18
19rm -rf testdir &&
20mkdir -p testdir/dir/dir/dir testdir/dir2 &&
21touch testdir/dir/file &&
22ln -s ../dir/dir testdir/dir2/dir &&
23ln -s ../dir/file testdir/dir2/file || exit 1
24
25# Wrapper to reset groups and return results
26
27IN="cd testdir && chgrp -R $GRP dir dir2 &&"
28OUT="&& cd .. && echo \$(ls -lR testdir | awk '{print \$4}')"
29
30# The groups returned by $OUT are, in order:
31# dir dir2 dir/dir dir/file dir/dir/dir dir2/dir dir2/file
32
33#testing "name" "command" "result" "infile" "stdin"
34
35# Basic smoketest
36testing "dir" "$IN chgrp root dir $OUT" \
37	"root $GRP $GRP $GRP $GRP $GRP $GRP\n" "" ""
38testing "file" "$IN chgrp root dir/file $OUT" \
39	"$GRP $GRP $GRP root $GRP $GRP $GRP\n" "" ""
40testing "dir and file" "$IN chgrp root dir dir/file $OUT" \
41	"root $GRP $GRP root $GRP $GRP $GRP\n" "" ""
42
43# symlinks (affect target, not symlink)
44testing "symlink->file" "$IN chgrp root dir2/file $OUT" \
45	"$GRP $GRP $GRP root $GRP $GRP $GRP\n" "" ""
46testing "symlink->dir" "$IN chgrp root dir2/dir $OUT" \
47	"$GRP $GRP root $GRP $GRP $GRP $GRP\n" "" ""
48testing "-h symlink->dir" "$IN chgrp -h root dir2/dir $OUT" \
49	"$GRP $GRP $GRP $GRP $GRP root $GRP\n" "" ""
50
51# What does -h do (affect symlink, not target)
52testing "-h symlink->file" "$IN chgrp -h root dir2/file $OUT" \
53	"$GRP $GRP $GRP $GRP $GRP $GRP root\n" "" ""
54testing "-h symlink->dir" "$IN chgrp -h root dir2/dir $OUT" \
55	"$GRP $GRP $GRP $GRP $GRP root $GRP\n" "" ""
56
57# chgrp -R (note, -h is implied by -R)
58
59testing "-R dir" "$IN chgrp -R root dir $OUT" \
60	"root $GRP root root root $GRP $GRP\n" "" ""
61testing "-R dir2" "$IN chgrp -R root dir2 $OUT" \
62	"$GRP root $GRP $GRP $GRP root root\n" "" ""
63testing "-R symlink->dir" "$IN chgrp -R root dir2/dir $OUT" \
64	"$GRP $GRP $GRP $GRP $GRP root $GRP\n" "" ""
65testing "-R symlink->file" "$IN chgrp -R root dir2/file $OUT" \
66	"$GRP $GRP $GRP $GRP $GRP $GRP root\n" "" ""
67
68# chgrp -RP (same as -R by itself)
69
70testing "-RP dir2" "$IN chgrp -RP root dir2 $OUT" \
71	"$GRP root $GRP $GRP $GRP root root\n" "" ""
72testing "-RP symlink->dir" "$IN chgrp -RP root dir2/dir $OUT" \
73	"$GRP $GRP $GRP $GRP $GRP root $GRP\n" "" ""
74testing "-RP symlink->file" "$IN chgrp -RP root dir2/file $OUT" \
75	"$GRP $GRP $GRP $GRP $GRP $GRP root\n" "" ""
76
77# chgrp -RH (change target but only recurse through symlink->dir on cmdline)
78
79testing "-RH dir2" "$IN chgrp -RH root dir2 $OUT" \
80	"$GRP root root root $GRP $GRP $GRP\n" "" ""
81testing "-RH symlink->dir" "$IN chgrp -RH root dir2/dir $OUT" \
82	"$GRP $GRP root $GRP root $GRP $GRP\n" "" ""
83testing "-RH symlink->file" "$IN chgrp -RH root dir2/file $OUT" \
84	"$GRP $GRP $GRP root $GRP $GRP $GRP\n" "" ""
85
86# chgrp -RL (change target and always recurse through symlink->dir)
87
88testing "-RL dir2" "$IN chgrp -RL root dir2 $OUT" \
89	"$GRP root root root root $GRP $GRP\n" "" ""
90testing "-RL symlink->dir" "$IN chgrp -RL root dir2/dir $OUT" \
91	"$GRP $GRP root $GRP root $GRP $GRP\n" "" ""
92testing "-RL symlink->file" "$IN chgrp -RL root dir2/file $OUT" \
93	"$GRP $GRP $GRP root $GRP $GRP $GRP\n" "" ""
94
95# -HLP are NOPs without -R
96testing "-H without -R" "$IN chgrp -H root dir2/dir $OUT" \
97	"$GRP $GRP root $GRP $GRP $GRP $GRP\n" "" ""
98testing "-L without -R" "$IN chgrp -L root dir2/dir $OUT" \
99	"$GRP $GRP root $GRP $GRP $GRP $GRP\n" "" ""
100testing "-P without -R" "$IN chgrp -P root dir2/dir $OUT" \
101	"$GRP $GRP root $GRP $GRP $GRP $GRP\n" "" ""
102
103rm -rf testdir
104