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