]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/TEST-21-SYSUSERS/test.sh
Merge pull request #13102 from mbiebl/nologin-path
[thirdparty/systemd.git] / test / TEST-21-SYSUSERS / test.sh
1 #!/bin/bash
2 set -e
3 TEST_DESCRIPTION="Sysuser-related tests"
4
5 . $TEST_BASE_DIR/test-functions
6
7 test_setup() {
8 mkdir -p $TESTDIR/etc/sysusers.d $TESTDIR/usr/lib/sysusers.d $TESTDIR/tmp
9 }
10
11 prepare_testdir() {
12 rm -f $TESTDIR/etc/*{passwd,group,shadow}
13 for i in $1.initial-{passwd,group,shadow}; do
14 test -f $i && cp $i $TESTDIR/etc/${i#*.initial-}
15 done
16 return 0
17 }
18
19 preprocess() {
20 in="$1"
21
22 # see meson.build how to extract this. gcc -E was used before to
23 # get this value from config.h, however the autopkgtest fails with
24 # it
25 SYSTEM_UID_MAX=$(awk 'BEGIN { uid=999 } /^\s*SYS_UID_MAX\s+/ { uid=$2 } END { print uid }' /etc/login.defs)
26
27 # we can't rely on config.h to get the nologin path, as autopkgtest
28 # uses pre-compiled binaries, so extract it from the systemd-sysusers
29 # binary which we are about to execute
30 NOLOGIN=$(strings $(type -p systemd-sysusers) | grep nologin)
31
32 sed -e "s/SYSTEM_UID_MAX/${SYSTEM_UID_MAX}/g" \
33 -e "s#NOLOGIN#${NOLOGIN}#g" "$in"
34 }
35
36 compare() {
37 if ! diff -u $TESTDIR/etc/passwd <(preprocess ${1%.*}.expected-passwd); then
38 echo "**** Unexpected output for $f"
39 exit 1
40 fi
41
42 if ! diff -u $TESTDIR/etc/group <(preprocess ${1%.*}.expected-group); then
43 echo "**** Unexpected output for $f $2"
44 exit 1
45 fi
46 }
47
48 test_run() {
49 # ensure our build of systemd-sysusers is run
50 PATH=${BUILD_DIR}:$PATH
51
52 rm -f $TESTDIR/etc/sysusers.d/* $TESTDIR/usr/lib/sysusers.d/*
53
54 # happy tests
55 for f in test-*.input; do
56 echo "*** Running $f"
57 prepare_testdir ${f%.input}
58 cp $f $TESTDIR/usr/lib/sysusers.d/test.conf
59 systemd-sysusers --root=$TESTDIR
60
61 compare $f ""
62 done
63
64 for f in test-*.input; do
65 echo "*** Running $f on stdin"
66 prepare_testdir ${f%.input}
67 touch $TESTDIR/etc/sysusers.d/test.conf
68 cat $f | systemd-sysusers --root=$TESTDIR -
69
70 compare $f "on stdin"
71 done
72
73 for f in test-*.input; do
74 echo "*** Running $f on stdin with --replace"
75 prepare_testdir ${f%.input}
76 touch $TESTDIR/etc/sysusers.d/test.conf
77 # this overrides test.conf which is masked on disk
78 cat $f | systemd-sysusers --root=$TESTDIR --replace=/etc/sysusers.d/test.conf -
79 # this should be ignored
80 cat test-1.input | systemd-sysusers --root=$TESTDIR --replace=/usr/lib/sysusers.d/test.conf -
81
82 compare $f "on stdin with --replace"
83 done
84
85 # test --inline
86 echo "*** Testing --inline"
87 prepare_testdir
88 # copy a random file to make sure it is ignored
89 cp $f $TESTDIR/etc/sysusers.d/confuse.conf
90 systemd-sysusers --root=$TESTDIR --inline \
91 "u u1 222 - - /bin/zsh" \
92 "g g1 111"
93
94 compare inline "(--inline)"
95
96 # test --replace
97 echo "*** Testing --inline with --replace"
98 prepare_testdir
99 # copy a random file to make sure it is ignored
100 cp $f $TESTDIR/etc/sysusers.d/confuse.conf
101 systemd-sysusers --root=$TESTDIR \
102 --inline \
103 --replace=/etc/sysusers.d/confuse.conf \
104 "u u1 222 - - /bin/zsh" \
105 "g g1 111"
106
107 compare inline "(--inline --replace=…)"
108
109 rm -f $TESTDIR/etc/sysusers.d/* $TESTDIR/usr/lib/sysusers.d/*
110
111 # tests for error conditions
112 for f in unhappy-*.input; do
113 echo "*** Running test $f"
114 prepare_testdir ${f%.input}
115 cp $f $TESTDIR/usr/lib/sysusers.d/test.conf
116 systemd-sysusers --root=$TESTDIR 2> /dev/null
117 journalctl -t systemd-sysusers -o cat | tail -n1 > $TESTDIR/tmp/err
118 if ! diff -u $TESTDIR/tmp/err ${f%.*}.expected-err; then
119 echo "**** Unexpected error output for $f"
120 cat $TESTDIR/tmp/err
121 exit 1
122 fi
123 done
124 }
125
126 do_test "$@"