]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - usr.sbin/etcupdate/tests/preworld_test.sh
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / usr.sbin / etcupdate / tests / preworld_test.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2013 Hudson River Trading LLC
4 # Written by: John H. Baldwin <jhb@FreeBSD.org>
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 # SUCH DAMAGE.
27 #
28 # $FreeBSD$
29
30 # Regression tests for the pre-world (-p) mode 
31
32 FAILED=no
33 WORKDIR=work
34
35 usage()
36 {
37         echo "Usage: preworld.sh [-s script] [-w workdir]"
38         exit 1
39 }
40
41 # Allow the user to specify an alternate work directory or script.
42 COMMAND=etcupdate
43 while getopts "s:w:" option; do
44         case $option in
45                 s)
46                         COMMAND="sh $OPTARG"
47                         ;;
48                 w)
49                         WORKDIR=$OPTARG
50                         ;;
51                 *)
52                         echo
53                         usage
54                         ;;
55         esac
56 done
57 shift $((OPTIND - 1))
58 if [ $# -ne 0 ]; then
59         usage
60 fi
61
62 CONFLICTS=$WORKDIR/conflicts
63 SRC=$WORKDIR/src
64 OLD=$WORKDIR/current
65 TEST=$WORKDIR/test
66
67 build_trees()
68 {
69
70         # Populate trees with pre-world files and additional files
71         # that should not be touched.
72
73         rm -rf $SRC $OLD $TEST $CONFLICTS
74
75         # Create the "old" source tree as the starting point
76         mkdir -p $OLD/etc
77         cat >> $OLD/etc/master.passwd <<EOF
78 #
79 root::0:0::0:0:Charlie &:/root:/bin/csh
80 toor:*:0:0::0:0:Bourne-again Superuser:/root:
81 daemon:*:1:1::0:0:Owner of many system processes:/root:/usr/sbin/nologin
82 operator:*:2:5::0:0:System &:/:/usr/sbin/nologin
83 _dhcp:*:65:65::0:0:dhcp programs:/var/empty:/usr/sbin/nologin
84 uucp:*:66:66::0:0:UUCP pseudo-user:/var/spool/uucppublic:/usr/local/libexec/uucp/uucico
85 pop:*:68:6::0:0:Post Office Owner:/nonexistent:/usr/sbin/nologin
86 www:*:80:80::0:0:World Wide Web Owner:/nonexistent:/usr/sbin/nologin
87 hast:*:845:845::0:0:HAST unprivileged user:/var/empty:/usr/sbin/nologin
88 nobody:*:65534:65534::0:0:Unprivileged user:/nonexistent:/usr/sbin/nologin
89 EOF
90         cat >> $OLD/etc/group <<EOF
91 #
92 wheel:*:0:root
93 daemon:*:1:
94 kmem:*:2:
95 sys:*:3:
96 tty:*:4:
97 operator:*:5:root
98 _dhcp:*:65:
99 uucp:*:66:
100 dialer:*:68:
101 network:*:69:
102 www:*:80:
103 hast:*:845:
104 nogroup:*:65533:
105 nobody:*:65534:
106 EOF
107         cat >> $OLD/etc/inetd.conf <<EOF
108 # Yet another file
109 EOF
110
111         # Copy the "old" source tree to the test tree and make local
112         # modifications.
113         cp -R $OLD $TEST
114         sed -I "" -e 's/root::/root:<rpass>:/' $TEST/etc/master.passwd
115         cat >> $TEST/etc/master.passwd <<EOF
116 john:<password>:1001:1001::0:0:John Baldwin:/home/john:/bin/tcsh
117 messagebus:*:556:556::0:0:D-BUS Daemon User:/nonexistent:/usr/sbin/nologin
118 polkit:*:562:562::0:0:PolicyKit User:/nonexistent:/usr/sbin/nologin
119 haldaemon:*:560:560::0:0:HAL Daemon User:/nonexistent:/usr/sbin/nologin
120 EOF
121         awk '/wheel/ { printf "%s,john\n", $0; next } // { print }' \
122             $OLD/etc/group > $TEST/etc/group
123         cat >> $TEST/etc/group <<EOF
124 john:*:1001:
125 messagebus:*:556:
126 polkit:*:562:
127 haldaemon:*:560:
128 EOF
129         rm $TEST/etc/inetd.conf
130         touch $TEST/etc/localtime
131
132         # Copy the "old" source tree to the new source tree and
133         # make upstream modifications.
134         cp -R $OLD $SRC
135         sed -I "" -e '/:80:/i\
136 auditdistd:*:78:77::0:0:Auditdistd unprivileged user:/var/empty:/usr/sbin/nologin' \
137             $SRC/etc/master.passwd
138         sed -I "" -e '/:80:/i\
139 audit:*:77:' \
140             $SRC/etc/group
141         cat >> $SRC/etc/inetd.conf <<EOF
142 # Making this larger
143 EOF
144 }
145
146 # $1 - relative path to file that should be missing from TEST
147 missing()
148 {
149         if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
150                 echo "File $1 should be missing"
151                 FAILED=yes
152         fi
153 }
154
155 # $1 - relative path to file that should be present in TEST
156 present()
157 {
158         if ! [ -e $TEST/$1 -o -L $TEST/$1 ]; then
159                 echo "File $1 should be present"
160                 FAILED=yes
161         fi
162 }
163
164 # $1 - relative path to regular file that should be present in TEST
165 # $2 - optional string that should match file contents
166 # $3 - optional MD5 of the flie contents, overrides $2 if present
167 file()
168 {
169         local contents sum
170
171         if ! [ -f $TEST/$1 ]; then
172                 echo "File $1 should be a regular file"
173                 FAILED=yes
174         elif [ $# -eq 2 ]; then
175                 contents=`cat $TEST/$1`
176                 if [ "$contents" != "$2" ]; then
177                         echo "File $1 has wrong contents"
178                         FAILED=yes
179                 fi
180         elif [ $# -eq 3 ]; then
181                 sum=`md5 -q $TEST/$1`
182                 if [ "$sum" != "$3" ]; then
183                         echo "File $1 has wrong contents"
184                         FAILED=yes
185                 fi
186         fi
187 }
188
189 # $1 - relative path to a regular file that should have a conflict
190 # $2 - optional MD5 of the conflict file contents
191 conflict()
192 {
193         local sum
194
195         if ! [ -f $CONFLICTS/$1 ]; then
196                 echo "File $1 missing conflict"
197                 FAILED=yes
198         elif [ $# -gt 1 ]; then
199                 sum=`md5 -q $CONFLICTS/$1`
200                 if [ "$sum" != "$2" ]; then
201                         echo "Conflict $1 has wrong contents"
202                         FAILED=yes
203                 fi
204         fi
205 }
206
207 check_trees()
208 {
209
210         echo "Checking tree for correct results:"
211
212         file /etc/master.passwd "" 1385366e8b424d33d59b7d8a2bdb15d3
213         file /etc/group "" 21273f845f6ec0cda9188c4ddac9ed47
214         missing /etc/inetd.conf
215
216         # These should be auto-generated by pwd_mkdb
217         file /etc/passwd "" 9831537874bdc99adccaa2b0293248a1
218         file /etc/pwd.db
219         file /etc/spwd.db
220 }
221
222 if [ `id -u` -ne 0 ]; then
223         echo "must be root"
224         exit 0
225 fi
226
227 if [ -r /etc/etcupdate.conf ]; then
228         echo "WARNING: /etc/etcupdate.conf settings may break some tests."
229 fi
230
231 build_trees
232
233 $COMMAND -np -s $SRC -d $WORKDIR -D $TEST > $WORKDIR/testn.out
234
235 cat > $WORKDIR/correct.out <<EOF
236   M /etc/group
237   M /etc/master.passwd
238 EOF
239
240 echo "Differences for -n:"
241 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
242     || FAILED=yes
243
244 $COMMAND -p -s $SRC -d $WORKDIR -D $TEST > $WORKDIR/test.out
245
246 echo "Differences for real:"
247 diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
248     || FAILED=yes
249
250 check_trees
251
252 [ "${FAILED}" = no ]