2020-09-26 12:58:24 +03:00
#!/usr/bin/env bash
2021-10-01 14:04:32 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2020-09-26 12:58:24 +03:00
set -e
SYSUSERS = " ${ 1 :- systemd -sysusers } "
2023-03-29 03:10:15 +03:00
# shellcheck disable=SC1090
[ -e " $( dirname " $0 " ) /../systemd-runtest.env " ] && . " $( dirname " $0 " ) /../systemd-runtest.env "
2020-09-26 12:58:24 +03:00
SYSTEMD_TEST_DATA = ${ SYSTEMD_TEST_DATA :- @SYSTEMD_TEST_DATA@ }
SOURCE = $SYSTEMD_TEST_DATA /test-sysusers
TESTDIR = $( mktemp --tmpdir --directory "test-sysusers.XXXXXXXXXX" )
2023-03-29 03:10:15 +03:00
# shellcheck disable=SC2064
2020-09-26 12:58:24 +03:00
trap " rm -rf ' $TESTDIR ' " EXIT INT QUIT PIPE
prepare_testdir( ) {
2023-03-29 03:10:15 +03:00
mkdir -p " $TESTDIR /etc/sysusers.d/ "
mkdir -p " $TESTDIR /usr/lib/sysusers.d/ "
rm -f " $TESTDIR " /etc/*{ passwd,group,shadow}
2020-09-26 12:58:24 +03:00
for i in $1 .initial-{ passwd,group,shadow} ; do
2023-03-29 03:10:15 +03:00
test -f " $i " && cp " $i " " $TESTDIR /etc/ ${ i #*.initial- } "
2020-09-26 12:58:24 +03:00
done
return 0
}
2023-03-29 03:10:15 +03:00
# shellcheck disable=SC2050
2020-09-25 18:16:06 +03:00
[ @SYSTEM_UID_MAX@ -lt @SYSTEM_GID_MAX@ ] && system_guid_max = @SYSTEM_UID_MAX@ || system_guid_max = @SYSTEM_GID_MAX@
2020-09-26 12:58:24 +03:00
preprocess( ) {
2020-09-25 18:16:06 +03:00
m = ${ 2 :- $system_guid_max }
2023-03-29 03:10:15 +03:00
# shellcheck disable=SC2140
2020-09-25 18:16:06 +03:00
sed -e " s/SYSTEM_UGID_MAX/ $m /g;
s#NOLOGIN#@NOLOGIN@#g" " $1 "
2020-09-26 12:58:24 +03:00
}
compare( ) {
2023-03-29 03:10:15 +03:00
if ! diff -u " $TESTDIR /etc/passwd " <( preprocess " $1 .expected-passwd " " $3 " ) ; then
2024-01-21 08:21:12 +03:00
echo >& 2 " **** Unexpected output for $f $2 "
2020-09-26 12:58:24 +03:00
exit 1
fi
2023-03-29 03:10:15 +03:00
if ! diff -u " $TESTDIR /etc/group " <( preprocess " $1 .expected-group " " $3 " ) ; then
2024-01-21 08:21:12 +03:00
echo >& 2 " **** Unexpected output for $f $2 "
2020-09-26 12:58:24 +03:00
exit 1
fi
}
2023-03-29 03:10:15 +03:00
rm -f " $TESTDIR " /etc/sysusers.d/* " $TESTDIR " /usr/lib/sysusers.d/*
2020-09-26 12:58:24 +03:00
# happy tests
2023-03-29 03:10:15 +03:00
for f in $( find " $SOURCE " /test-*.input | sort -V) ; do
2020-09-26 12:58:24 +03:00
echo " *** Running $f "
2023-03-29 03:10:15 +03:00
prepare_testdir " ${ f %.input } "
cp " $f " " $TESTDIR /usr/lib/sysusers.d/test.conf "
$SYSUSERS --root= " $TESTDIR "
2020-09-26 12:58:24 +03:00
2023-03-29 03:10:15 +03:00
compare " ${ f %.* } " ""
2020-09-26 12:58:24 +03:00
done
2023-03-29 03:10:15 +03:00
for f in $( find " $SOURCE " /test-*.input | sort -V) ; do
2020-09-26 12:58:24 +03:00
echo " *** Running $f on stdin "
2023-03-29 03:10:15 +03:00
prepare_testdir " ${ f %.input } "
touch " $TESTDIR /etc/sysusers.d/test.conf "
$SYSUSERS --root= " $TESTDIR " - <" $f "
2020-09-26 12:58:24 +03:00
2023-03-29 03:10:15 +03:00
compare " ${ f %.* } " "on stdin"
2020-09-26 12:58:24 +03:00
done
2023-03-29 03:10:15 +03:00
for f in $( find " $SOURCE " /test-*.input | sort -V) ; do
2020-09-26 12:58:24 +03:00
echo " *** Running $f on stdin with --replace "
2023-03-29 03:10:15 +03:00
prepare_testdir " ${ f %.input } "
touch " $TESTDIR /etc/sysusers.d/test.conf "
2020-09-26 12:58:24 +03:00
# this overrides test.conf which is masked on disk
2023-03-29 03:10:15 +03:00
$SYSUSERS --root= " $TESTDIR " --replace= /etc/sysusers.d/test.conf - <" $f "
2020-09-26 12:58:24 +03:00
# this should be ignored
2023-03-29 03:10:15 +03:00
$SYSUSERS --root= " $TESTDIR " --replace= /usr/lib/sysusers.d/test.conf - <" $SOURCE /test-1.input "
2020-09-26 12:58:24 +03:00
2023-03-29 03:10:15 +03:00
compare " ${ f %.* } " "on stdin with --replace"
2020-09-26 12:58:24 +03:00
done
# test --inline
echo "*** Testing --inline"
2023-03-29 03:10:15 +03:00
prepare_testdir " $SOURCE /inline "
2020-09-26 12:58:24 +03:00
# copy a random file to make sure it is ignored
2023-03-29 03:10:15 +03:00
cp " $f " " $TESTDIR /etc/sysusers.d/confuse.conf "
$SYSUSERS --root= " $TESTDIR " --inline \
2020-09-26 12:58:24 +03:00
"u u1 222 - - /bin/zsh" \
"g g1 111"
2023-03-29 03:10:15 +03:00
compare " $SOURCE /inline " "(--inline)"
2020-09-26 12:58:24 +03:00
# test --replace
echo "*** Testing --inline with --replace"
2023-03-29 03:10:15 +03:00
prepare_testdir " $SOURCE /inline "
2020-09-26 12:58:24 +03:00
# copy a random file to make sure it is ignored
2023-03-29 03:10:15 +03:00
cp " $f " " $TESTDIR /etc/sysusers.d/confuse.conf "
$SYSUSERS --root= " $TESTDIR " \
2020-09-26 12:58:24 +03:00
--inline \
--replace= /etc/sysusers.d/confuse.conf \
"u u1 222 - - /bin/zsh" \
"g g1 111"
2023-03-29 03:10:15 +03:00
compare " $SOURCE /inline " "(--inline --replace=…)"
2020-09-26 12:58:24 +03:00
2023-01-31 18:41:16 +03:00
echo "*** Testing --inline with no /etc"
2023-03-29 03:10:15 +03:00
rm -rf " ${ TESTDIR : ? } /etc "
$SYSUSERS --root= " $TESTDIR " --inline \
2023-01-31 18:41:16 +03:00
"u u1 222 - - /bin/zsh" \
"g g1 111"
2023-03-29 03:10:15 +03:00
compare " $SOURCE /inline " "(--inline)"
2023-01-31 18:41:16 +03:00
2023-03-29 03:10:15 +03:00
rm -f " $TESTDIR " /etc/sysusers.d/* " $TESTDIR " /usr/lib/sysusers.d/*
2020-09-26 12:58:24 +03:00
2023-03-29 03:10:15 +03:00
cat >" $TESTDIR /etc/login.defs " <<EOF
2020-09-25 18:16:06 +03:00
SYS_UID_MIN abcd
SYS_UID_MAX abcd
SYS_GID_MIN abcd
SYS_GID_MAX abcd
SYS_UID_MIN 401
SYS_UID_MAX 555
SYS_GID_MIN 405
SYS_GID_MAX 666
SYS_UID_MIN abcd
SYS_UID_MAX abcd
SYS_GID_MIN abcd
SYS_GID_MAX abcd
SYS_UID_MIN999
SYS_UID_MAX999
SYS_GID_MIN999
SYS_GID_MAX999
EOF
2023-03-29 03:10:15 +03:00
for f in $( find " $SOURCE " /test-*.input | sort -V) ; do
2020-09-25 18:16:06 +03:00
echo " *** Running $f (with login.defs) "
2023-03-29 03:10:15 +03:00
prepare_testdir " ${ f %.input } "
cp " $f " " $TESTDIR /usr/lib/sysusers.d/test.conf "
$SYSUSERS --root= " $TESTDIR "
2020-09-25 18:16:06 +03:00
2023-03-29 03:10:15 +03:00
# shellcheck disable=SC2050
2020-09-25 18:16:06 +03:00
[ @ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES@ = 1 ] && bound = 555 || bound = $system_guid_max
2023-06-06 00:48:06 +03:00
compare " ${ f %.* } " "(with login.defs)" " $bound "
2020-09-25 18:16:06 +03:00
done
2023-03-29 03:10:15 +03:00
rm -f " $TESTDIR " /etc/sysusers.d/* " $TESTDIR " /usr/lib/sysusers.d/*
2020-09-25 18:16:06 +03:00
2023-03-29 03:10:15 +03:00
mv " $TESTDIR /etc/login.defs " " $TESTDIR /etc/login.defs.moved "
ln -s ../../../../../etc/login.defs.moved " $TESTDIR /etc/login.defs "
2020-09-25 18:16:06 +03:00
2023-03-29 03:10:15 +03:00
for f in $( find " $SOURCE " /test-*.input | sort -V) ; do
2020-09-25 18:16:06 +03:00
echo " *** Running $f (with login.defs symlinked) "
2023-03-29 03:10:15 +03:00
prepare_testdir " ${ f %.input } "
cp " $f " " $TESTDIR /usr/lib/sysusers.d/test.conf "
$SYSUSERS --root= " $TESTDIR "
2020-09-25 18:16:06 +03:00
2023-03-29 03:10:15 +03:00
# shellcheck disable=SC2050
2020-09-25 18:16:06 +03:00
[ @ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES@ = 1 ] && bound = 555 || bound = $system_guid_max
2023-06-06 00:48:06 +03:00
compare " ${ f %.* } " "(with login.defs symlinked)" " $bound "
2020-09-25 18:16:06 +03:00
done
2023-03-29 03:10:15 +03:00
rm -f " $TESTDIR " /etc/sysusers.d/* " $TESTDIR " /usr/lib/sysusers.d/*
2020-09-25 18:16:06 +03:00
2020-09-26 12:58:24 +03:00
# tests for error conditions
2023-03-29 03:10:15 +03:00
for f in $( find " $SOURCE " /unhappy-*.input | sort -V) ; do
2020-09-26 12:58:24 +03:00
echo " *** Running test $f "
2023-03-29 03:10:15 +03:00
prepare_testdir " ${ f %.input } "
cp " $f " " $TESTDIR /usr/lib/sysusers.d/test.conf "
2024-02-19 16:35:07 +03:00
SYSTEMD_LOG_LEVEL = info $SYSUSERS --root= " $TESTDIR " 2>& 1 | tail -n1 | sed -r 's/^[^:]+:[^:]+://' >" $TESTDIR /err "
2023-03-29 03:10:15 +03:00
if ! diff -u " $TESTDIR /err " " ${ f %.* } .expected-err " ; then
2024-01-21 08:21:12 +03:00
echo >& 2 " **** Unexpected error output for $f "
cat >& 2 " $TESTDIR /err "
2020-09-26 12:58:24 +03:00
exit 1
fi
done