xref: /freebsd/usr.sbin/etcupdate/tests/conflicts_test.sh (revision 02e9120893770924227138ba49df1edb3896112a)
1#!/bin/sh
2#
3# Copyright (c) 2010 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# Various regression tests to run for the 'resolve' command.
30
31FAILED=no
32WORKDIR=work
33
34usage()
35{
36	echo "Usage: conflicts.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
62OLD=$WORKDIR/old
63NEW=$WORKDIR/current
64TEST=$WORKDIR/test
65
66# These tests deal with conflicts to a single file.  For each test, we
67# generate a conflict in /etc/login.conf.  Each resolve option is tested
68# to ensure it DTRT.
69build_login_conflict()
70{
71
72	rm -rf $OLD $NEW $TEST $CONFLICTS
73	mkdir -p $OLD/etc $NEW/etc $TEST/etc
74
75	# Generate a conflict in /etc/login.conf.
76	cat > $OLD/etc/login.conf <<EOF
77default:\\
78	:passwd_format=md5:
79EOF
80	cat > $NEW/etc/login.conf <<EOF
81default:\\
82	:passwd_format=md5:\\
83	:copyright=/etc/COPYRIGHT
84EOF
85	cat > $TEST/etc/login.conf <<EOF
86default:\\
87	:passwd_format=md5:\\
88        :welcome=/etc/motd:
89EOF
90
91	$COMMAND -r -d $WORKDIR -D $TEST >/dev/null
92}
93
94# This is used to verify special handling for /etc/mail/aliases and
95# the newaliases warning.
96build_aliases_conflict()
97{
98
99	rm -rf $OLD $NEW $TEST $CONFLICTS
100	mkdir -p $OLD/etc/mail $NEW/etc/mail $TEST/etc/mail
101
102	# Generate a conflict in /etc/mail/aliases
103	cat > $OLD/etc/mail/aliases <<EOF
104# root: me@my.domain
105
106# Basic system aliases -- these MUST be present
107MAILER-DAEMON: postmaster
108postmaster: root
109EOF
110	cat > $NEW/etc/mail/aliases <<EOF
111# root: me@my.domain
112
113# Basic system aliases -- these MUST be present
114MAILER-DAEMON: postmaster
115postmaster: root
116
117# General redirections for pseudo accounts
118_dhcp:  root
119_pflogd: root
120EOF
121	cat > $TEST/etc/mail/aliases <<EOF
122root: someone@example.com
123
124# Basic system aliases -- these MUST be present
125MAILER-DAEMON: postmaster
126postmaster: foo
127EOF
128
129	$COMMAND -r -d $WORKDIR -D $TEST >/dev/null
130}
131
132# $1 - relative path to file that should be missing from TEST
133missing()
134{
135	if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
136		echo "File $1 should be missing"
137		FAILED=yes
138	fi
139}
140
141# $1 - relative path to file that should be present in TEST
142present()
143{
144	if ! [ -e $TEST/$1 -o -L $TEST/$1 ]; then
145		echo "File $1 should be present"
146		FAILED=yes
147	fi
148}
149
150# $1 - relative path to regular file that should be present in TEST
151# $2 - optional string that should match file contents
152# $3 - optional MD5 of the flie contents, overrides $2 if present
153file()
154{
155	local contents sum
156
157	if ! [ -f $TEST/$1 ]; then
158		echo "File $1 should be a regular file"
159		FAILED=yes
160	elif [ $# -eq 2 ]; then
161		contents=`cat $TEST/$1`
162		if [ "$contents" != "$2" ]; then
163			echo "File $1 has wrong contents"
164			FAILED=yes
165		fi
166	elif [ $# -eq 3 ]; then
167		sum=`md5 -q $TEST/$1`
168		if [ "$sum" != "$3" ]; then
169			echo "File $1 has wrong contents"
170			FAILED=yes
171		fi
172	fi
173}
174
175# $1 - relative path to a regular file that should have a conflict
176# $2 - optional MD5 of the conflict file contents
177conflict()
178{
179	local sum
180
181	if ! [ -f $CONFLICTS/$1 ]; then
182		echo "File $1 missing conflict"
183		FAILED=yes
184	elif [ $# -gt 1 ]; then
185		sum=`md5 -q $CONFLICTS/$1`
186		if [ "$sum" != "$2" ]; then
187			echo "Conflict $1 has wrong contents"
188			FAILED=yes
189		fi
190	fi
191}
192
193# $1 - relative path to a regular file that should no longer have a conflict
194resolved()
195{
196	if [ -f $CONFLICTS/$1 ]; then
197		echo "Conflict $1 should be resolved"
198		FAILED=yes
199	fi
200}
201
202if [ `id -u` -ne 0 ]; then
203	echo "must be root"
204	exit 0
205fi
206
207if [ -r /etc/etcupdate.conf ]; then
208	echo "WARNING: /etc/etcupdate.conf settings may break some tests."
209fi
210
211# Test each of the following resolve options: 'p', 'mf', 'tf', 'r'.
212
213build_login_conflict
214
215# Verify that 'p' doesn't do anything.
216echo "Checking 'p':"
217echo 'p' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
218
219file /etc/login.conf "" 95de92ea3f1bb1bf4f612a8b5908cddd
220missing /etc/login.conf.db
221conflict /etc/login.conf
222
223# Verify that 'mf' removes the conflict, but does nothing else.
224echo "Checking 'mf':"
225echo 'mf' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
226
227file /etc/login.conf "" 95de92ea3f1bb1bf4f612a8b5908cddd
228missing /etc/login.conf.db
229resolved /etc/login.conf
230
231build_login_conflict
232
233# Verify that 'tf' installs the new version of the file.
234echo "Checking 'tf':"
235echo 'tf' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
236
237file /etc/login.conf "" 7774a0f9a3a372c7c109c32fd31c4b6b
238file /etc/login.conf.db
239resolved /etc/login.conf
240
241build_login_conflict
242
243# Verify that 'r' installs the resolved version of the file.  To
244# simulate this, manually edit the merged file so that it doesn't
245# contain conflict markers.
246echo "Checking 'r':"
247cat > $CONFLICTS/etc/login.conf <<EOF
248default:\\
249	:passwd_format=md5:\\
250	:copyright=/etc/COPYRIGHT\\
251        :welcome=/etc/motd:
252EOF
253
254echo 'r' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
255
256file /etc/login.conf "" 966e25984b9b63da8eaac8479dcb0d4d
257file /etc/login.conf.db
258resolved /etc/login.conf
259
260build_aliases_conflict
261
262# Verify that 'p' and 'mf' do not generate the newaliases warning.
263echo "Checking newalias warning for 'p'":
264echo 'p' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
265if [ $? -eq 0 ]; then
266	echo "+ Extra warning"
267	FAILED=yes
268fi
269echo "Checking newalias warning for 'mf'":
270echo 'mf' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
271if [ $? -eq 0 ]; then
272	echo "+ Extra warning"
273	FAILED=yes
274fi
275
276# Verify that 'tf' and 'r' do generate the newaliases warning.
277build_aliases_conflict
278echo "Checking newalias warning for 'tf'":
279echo 'tf' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
280if [ $? -ne 0 ]; then
281	echo "- Missing warning"
282	FAILED=yes
283fi
284
285build_aliases_conflict
286cp $TEST/etc/mail/aliases $CONFLICTS/etc/mail/aliases
287echo 'r' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
288if [ $? -ne 0 ]; then
289	echo "- Missing warning"
290	FAILED=yes
291fi
292
293[ "${FAILED}" = no ]
294