xref: /freebsd/usr.sbin/etcupdate/tests/preworld_test.sh (revision a03411e84728e9b267056fd31c7d1d9d1dc1b01e)
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
29# Regression tests for the pre-world (-p) mode
30
31FAILED=no
32WORKDIR=work
33
34usage()
35{
36	echo "Usage: preworld.sh [-s script] [-w workdir]"
37	exit 1
38}
39
40# Allow the user to specify an alternate work directory or script.
41COMMAND=etcupdate
42while getopts "s:w:" option; do
43	case $option in
44		s)
45			COMMAND="sh $OPTARG"
46			;;
47		w)
48			WORKDIR=$OPTARG
49			;;
50		*)
51			echo
52			usage
53			;;
54	esac
55done
56shift $((OPTIND - 1))
57if [ $# -ne 0 ]; then
58	usage
59fi
60
61CONFLICTS=$WORKDIR/conflicts
62SRC=$WORKDIR/src
63OLD=$WORKDIR/current
64TEST=$WORKDIR/test
65
66build_trees()
67{
68
69	# Populate trees with pre-world files and additional files
70	# that should not be touched.
71
72	rm -rf $SRC $OLD $TEST $CONFLICTS
73
74	# Create the "old" source tree as the starting point
75	mkdir -p $OLD/etc
76	cat >> $OLD/etc/master.passwd <<EOF
77#
78root::0:0::0:0:Charlie &:/root:/bin/csh
79toor:*:0:0::0:0:Bourne-again Superuser:/root:
80daemon:*:1:1::0:0:Owner of many system processes:/root:/usr/sbin/nologin
81operator:*:2:5::0:0:System &:/:/usr/sbin/nologin
82_dhcp:*:65:65::0:0:dhcp programs:/var/empty:/usr/sbin/nologin
83uucp:*:66:66::0:0:UUCP pseudo-user:/var/spool/uucppublic:/usr/local/libexec/uucp/uucico
84pop:*:68:6::0:0:Post Office Owner:/nonexistent:/usr/sbin/nologin
85www:*:80:80::0:0:World Wide Web Owner:/nonexistent:/usr/sbin/nologin
86hast:*:845:845::0:0:HAST unprivileged user:/var/empty:/usr/sbin/nologin
87nobody:*:65534:65534::0:0:Unprivileged user:/nonexistent:/usr/sbin/nologin
88EOF
89	cat >> $OLD/etc/group <<EOF
90#
91wheel:*:0:root
92daemon:*:1:
93kmem:*:2:
94sys:*:3:
95tty:*:4:
96operator:*:5:root
97_dhcp:*:65:
98uucp:*:66:
99dialer:*:68:
100network:*:69:
101www:*:80:
102hast:*:845:
103nogroup:*:65533:
104nobody:*:65534:
105EOF
106	cat >> $OLD/etc/inetd.conf <<EOF
107# Yet another file
108EOF
109
110	# Copy the "old" source tree to the test tree and make local
111	# modifications.
112	cp -R $OLD $TEST
113	sed -I "" -e 's/root::/root:<rpass>:/' $TEST/etc/master.passwd
114	cat >> $TEST/etc/master.passwd <<EOF
115john:<password>:1001:1001::0:0:John Baldwin:/home/john:/bin/tcsh
116messagebus:*:556:556::0:0:D-BUS Daemon User:/nonexistent:/usr/sbin/nologin
117polkit:*:562:562::0:0:PolicyKit User:/nonexistent:/usr/sbin/nologin
118haldaemon:*:560:560::0:0:HAL Daemon User:/nonexistent:/usr/sbin/nologin
119EOF
120	awk '/wheel/ { printf "%s,john\n", $0; next } // { print }' \
121	    $OLD/etc/group > $TEST/etc/group
122	cat >> $TEST/etc/group <<EOF
123john:*:1001:
124messagebus:*:556:
125polkit:*:562:
126haldaemon:*:560:
127EOF
128	rm $TEST/etc/inetd.conf
129	touch $TEST/etc/localtime
130
131	# Copy the "old" source tree to the new source tree and
132	# make upstream modifications.
133	cp -R $OLD $SRC
134	sed -I "" -e '/:80:/i\
135auditdistd:*:78:77::0:0:Auditdistd unprivileged user:/var/empty:/usr/sbin/nologin' \
136	    $SRC/etc/master.passwd
137	sed -I "" -e '/:80:/i\
138audit:*:77:' \
139	    $SRC/etc/group
140	cat >> $SRC/etc/inetd.conf <<EOF
141# Making this larger
142EOF
143}
144
145# $1 - relative path to file that should be missing from TEST
146missing()
147{
148	if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
149		echo "File $1 should be missing"
150		FAILED=yes
151	fi
152}
153
154# $1 - relative path to file that should be present in TEST
155present()
156{
157	if ! [ -e $TEST/$1 -o -L $TEST/$1 ]; then
158		echo "File $1 should be present"
159		FAILED=yes
160	fi
161}
162
163# $1 - relative path to regular file that should be present in TEST
164# $2 - optional string that should match file contents
165# $3 - optional MD5 of the flie contents, overrides $2 if present
166file()
167{
168	local contents sum
169
170	if ! [ -f $TEST/$1 ]; then
171		echo "File $1 should be a regular file"
172		FAILED=yes
173	elif [ $# -eq 2 ]; then
174		contents=`cat $TEST/$1`
175		if [ "$contents" != "$2" ]; then
176			echo "File $1 has wrong contents"
177			FAILED=yes
178		fi
179	elif [ $# -eq 3 ]; then
180		sum=`md5 -q $TEST/$1`
181		if [ "$sum" != "$3" ]; then
182			echo "File $1 has wrong contents"
183			FAILED=yes
184		fi
185	fi
186}
187
188# $1 - relative path to a regular file that should have a conflict
189# $2 - optional MD5 of the conflict file contents
190conflict()
191{
192	local sum
193
194	if ! [ -f $CONFLICTS/$1 ]; then
195		echo "File $1 missing conflict"
196		FAILED=yes
197	elif [ $# -gt 1 ]; then
198		sum=`md5 -q $CONFLICTS/$1`
199		if [ "$sum" != "$2" ]; then
200			echo "Conflict $1 has wrong contents"
201			FAILED=yes
202		fi
203	fi
204}
205
206check_trees()
207{
208
209	echo "Checking tree for correct results:"
210
211	file /etc/master.passwd "" 1385366e8b424d33d59b7d8a2bdb15d3
212	file /etc/group "" 21273f845f6ec0cda9188c4ddac9ed47
213	missing /etc/inetd.conf
214
215	# These should be auto-generated by pwd_mkdb
216	file /etc/passwd "" e4650d2727044b22d513e6a02d86bcfa
217	file /etc/pwd.db
218	file /etc/spwd.db
219}
220
221if [ `id -u` -ne 0 ]; then
222	echo "must be root"
223	exit 0
224fi
225
226if [ -r /etc/etcupdate.conf ]; then
227	echo "WARNING: /etc/etcupdate.conf settings may break some tests."
228fi
229
230build_trees
231
232$COMMAND -np -s $SRC -d $WORKDIR -D $TEST > $WORKDIR/testn.out
233
234cat > $WORKDIR/correct.out <<EOF
235  M /etc/group
236  M /etc/master.passwd
237EOF
238
239echo "Differences for -n:"
240diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
241    || FAILED=yes
242
243$COMMAND -p -s $SRC -d $WORKDIR -D $TEST > $WORKDIR/test.out
244
245echo "Differences for real:"
246diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
247    || FAILED=yes
248
249check_trees
250
251[ "${FAILED}" = no ]
252