xref: /aosp_15_r20/external/toybox/tests/chown.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1*cf5a6c84SAndroid Build Coastguard Worker#!/bin/bash
2*cf5a6c84SAndroid Build Coastguard Worker
3*cf5a6c84SAndroid Build Coastguard Worker[ -f testing.sh ] && . testing.sh
4*cf5a6c84SAndroid Build Coastguard Worker
5*cf5a6c84SAndroid Build Coastguard Workerif [ "$(id -u)" -ne 0 ]
6*cf5a6c84SAndroid Build Coastguard Workerthen
7*cf5a6c84SAndroid Build Coastguard Worker  echo "$SHOWSKIP: chown (not root)"
8*cf5a6c84SAndroid Build Coastguard Worker  return 2>/dev/null
9*cf5a6c84SAndroid Build Coastguard Worker  exit
10*cf5a6c84SAndroid Build Coastguard Workerfi
11*cf5a6c84SAndroid Build Coastguard Worker
12*cf5a6c84SAndroid Build Coastguard Worker# We chown between user "root" and the last user in /etc/passwd,
13*cf5a6c84SAndroid Build Coastguard Worker# and group "root" and the last group in /etc/group.
14*cf5a6c84SAndroid Build Coastguard WorkerUSR="$(sed -n '$s/:.*//p' /etc/passwd)"
15*cf5a6c84SAndroid Build Coastguard WorkerGRP="$(sed -n '$s/:.*//p' /etc/group)"
16*cf5a6c84SAndroid Build Coastguard Worker# Or if that fails, we assume we're on Android...
17*cf5a6c84SAndroid Build Coastguard Worker: "${USR:=shell}"
18*cf5a6c84SAndroid Build Coastguard Worker: "${GRP:=daemon}"
19*cf5a6c84SAndroid Build Coastguard Worker
20*cf5a6c84SAndroid Build Coastguard Worker# Set up a little testing hierarchy
21*cf5a6c84SAndroid Build Coastguard Worker
22*cf5a6c84SAndroid Build Coastguard Workerrm -rf testdir &&
23*cf5a6c84SAndroid Build Coastguard Workermkdir testdir &&
24*cf5a6c84SAndroid Build Coastguard Workertouch testdir/file
25*cf5a6c84SAndroid Build Coastguard WorkerF=testdir/file
26*cf5a6c84SAndroid Build Coastguard Worker
27*cf5a6c84SAndroid Build Coastguard Worker# Wrapper to reset groups and return results
28*cf5a6c84SAndroid Build Coastguard Worker
29*cf5a6c84SAndroid Build Coastguard WorkerOUT="&& stat --format '%U %G' $F"
30*cf5a6c84SAndroid Build Coastguard Worker
31*cf5a6c84SAndroid Build Coastguard Worker#testing "name" "command" "result" "infile" "stdin"
32*cf5a6c84SAndroid Build Coastguard Worker
33*cf5a6c84SAndroid Build Coastguard Worker# Basic smoketest
34*cf5a6c84SAndroid Build Coastguard Workertesting "initial" "chown root:root $F $OUT" "root root\n" "" ""
35*cf5a6c84SAndroid Build Coastguard Workertesting "usr:grp" "chown $USR:$GRP $F $OUT" "$USR $GRP\n" "" ""
36*cf5a6c84SAndroid Build Coastguard Workertesting "root"    "chown root $F $OUT" "root $GRP\n" "" ""
37*cf5a6c84SAndroid Build Coastguard Worker# TODO: can we test "owner:"?
38*cf5a6c84SAndroid Build Coastguard Workertesting ":grp"    "chown root:root $F && chown :$GRP $F $OUT" \
39*cf5a6c84SAndroid Build Coastguard Worker    "root $GRP\n" "" ""
40*cf5a6c84SAndroid Build Coastguard Workertesting ":"       "chown $USR:$GRP $F && chown : $F $OUT" \
41*cf5a6c84SAndroid Build Coastguard Worker    "$USR $GRP\n" "" ""
42*cf5a6c84SAndroid Build Coastguard Worker
43*cf5a6c84SAndroid Build Coastguard Workerrm -rf testdir
44