1238423Sjhb#!/bin/sh
2238423Sjhb#
3283927Sjhb# Copyright (c) 2010 Hudson River Trading LLC
4238423Sjhb# Written by: John H. Baldwin <jhb@FreeBSD.org>
5238423Sjhb# All rights reserved.
6238423Sjhb#
7238423Sjhb# Redistribution and use in source and binary forms, with or without
8238423Sjhb# modification, are permitted provided that the following conditions
9238423Sjhb# are met:
10238423Sjhb# 1. Redistributions of source code must retain the above copyright
11238423Sjhb#    notice, this list of conditions and the following disclaimer.
12238423Sjhb# 2. Redistributions in binary form must reproduce the above copyright
13238423Sjhb#    notice, this list of conditions and the following disclaimer in the
14238423Sjhb#    documentation and/or other materials provided with the distribution.
15238423Sjhb#
16238423Sjhb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17238423Sjhb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18238423Sjhb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19238423Sjhb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20238423Sjhb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21238423Sjhb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22238423Sjhb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23238423Sjhb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24238423Sjhb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25238423Sjhb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26238423Sjhb# SUCH DAMAGE.
27238423Sjhb#
28238423Sjhb# $FreeBSD$
29238423Sjhb
30238423Sjhb# Various regression tests to run for the 'resolve' command.
31238423Sjhb
32263221SjmmvFAILED=no
33238423SjhbWORKDIR=work
34238423Sjhb
35238423Sjhbusage()
36238423Sjhb{
37258063Sjhb	echo "Usage: conflicts.sh [-s script] [-w workdir]"
38238423Sjhb	exit 1
39238423Sjhb}
40238423Sjhb
41258063Sjhb# Allow the user to specify an alternate work directory or script.
42258063SjhbCOMMAND=etcupdate
43258063Sjhbwhile getopts "s:w:" option; do
44238423Sjhb	case $option in
45258063Sjhb		s)
46258063Sjhb			COMMAND="sh $OPTARG"
47258063Sjhb			;;
48238423Sjhb		w)
49238423Sjhb			WORKDIR=$OPTARG
50238423Sjhb			;;
51238423Sjhb		*)
52238423Sjhb			echo
53238423Sjhb			usage
54238423Sjhb			;;
55238423Sjhb	esac
56238423Sjhbdone
57238423Sjhbshift $((OPTIND - 1))
58238423Sjhbif [ $# -ne 0 ]; then
59238423Sjhb	usage
60238423Sjhbfi
61238423Sjhb
62238423SjhbCONFLICTS=$WORKDIR/conflicts
63238423SjhbOLD=$WORKDIR/old
64238423SjhbNEW=$WORKDIR/current
65238423SjhbTEST=$WORKDIR/test
66238423Sjhb
67238423Sjhb# These tests deal with conflicts to a single file.  For each test, we
68238423Sjhb# generate a conflict in /etc/login.conf.  Each resolve option is tested
69238423Sjhb# to ensure it DTRT.
70238423Sjhbbuild_login_conflict()
71238423Sjhb{
72238423Sjhb
73238423Sjhb	rm -rf $OLD $NEW $TEST $CONFLICTS
74238423Sjhb	mkdir -p $OLD/etc $NEW/etc $TEST/etc
75238423Sjhb	
76238423Sjhb	# Generate a conflict in /etc/login.conf.
77238423Sjhb	cat > $OLD/etc/login.conf <<EOF
78238423Sjhbdefault:\\
79238423Sjhb	:passwd_format=md5:
80238423SjhbEOF
81238423Sjhb	cat > $NEW/etc/login.conf <<EOF
82238423Sjhbdefault:\\
83238423Sjhb	:passwd_format=md5:\\
84238423Sjhb	:copyright=/etc/COPYRIGHT
85238423SjhbEOF
86238423Sjhb	cat > $TEST/etc/login.conf <<EOF
87238423Sjhbdefault:\\
88238423Sjhb	:passwd_format=md5:\\
89238423Sjhb        :welcome=/etc/motd:
90238423SjhbEOF
91238423Sjhb
92258063Sjhb	$COMMAND -r -d $WORKDIR -D $TEST >/dev/null
93238423Sjhb}
94238423Sjhb
95238423Sjhb# This is used to verify special handling for /etc/mail/aliases and
96238423Sjhb# the newaliases warning.
97238423Sjhbbuild_aliases_conflict()
98238423Sjhb{
99238423Sjhb
100238423Sjhb	rm -rf $OLD $NEW $TEST $CONFLICTS
101238423Sjhb	mkdir -p $OLD/etc/mail $NEW/etc/mail $TEST/etc/mail
102238423Sjhb
103238423Sjhb	# Generate a conflict in /etc/mail/aliases
104238423Sjhb	cat > $OLD/etc/mail/aliases <<EOF
105238423Sjhb# root: me@my.domain
106238423Sjhb
107238423Sjhb# Basic system aliases -- these MUST be present
108238423SjhbMAILER-DAEMON: postmaster
109238423Sjhbpostmaster: root
110238423SjhbEOF
111238423Sjhb	cat > $NEW/etc/mail/aliases <<EOF
112238423Sjhb# root: me@my.domain
113238423Sjhb
114238423Sjhb# Basic system aliases -- these MUST be present
115238423SjhbMAILER-DAEMON: postmaster
116238423Sjhbpostmaster: root
117238423Sjhb
118238423Sjhb# General redirections for pseudo accounts
119238423Sjhb_dhcp:  root
120238423Sjhb_pflogd: root
121238423SjhbEOF
122238423Sjhb	cat > $TEST/etc/mail/aliases <<EOF
123238423Sjhbroot: someone@example.com
124238423Sjhb
125238423Sjhb# Basic system aliases -- these MUST be present
126238423SjhbMAILER-DAEMON: postmaster
127238423Sjhbpostmaster: foo
128238423SjhbEOF
129238423Sjhb
130258063Sjhb	$COMMAND -r -d $WORKDIR -D $TEST >/dev/null
131238423Sjhb}
132238423Sjhb
133238423Sjhb# $1 - relative path to file that should be missing from TEST
134238423Sjhbmissing()
135238423Sjhb{
136238423Sjhb	if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
137238423Sjhb		echo "File $1 should be missing"
138263221Sjmmv		FAILED=yes
139238423Sjhb	fi
140238423Sjhb}
141238423Sjhb
142238423Sjhb# $1 - relative path to file that should be present in TEST
143238423Sjhbpresent()
144238423Sjhb{
145238423Sjhb	if ! [ -e $TEST/$1 -o -L $TEST/$1 ]; then
146238423Sjhb		echo "File $1 should be present"
147263221Sjmmv		FAILED=yes
148238423Sjhb	fi
149238423Sjhb}
150238423Sjhb
151238423Sjhb# $1 - relative path to regular file that should be present in TEST
152238423Sjhb# $2 - optional string that should match file contents
153238423Sjhb# $3 - optional MD5 of the flie contents, overrides $2 if present
154238423Sjhbfile()
155238423Sjhb{
156238423Sjhb	local contents sum
157238423Sjhb
158238423Sjhb	if ! [ -f $TEST/$1 ]; then
159238423Sjhb		echo "File $1 should be a regular file"
160263221Sjmmv		FAILED=yes
161238423Sjhb	elif [ $# -eq 2 ]; then
162238423Sjhb		contents=`cat $TEST/$1`
163238423Sjhb		if [ "$contents" != "$2" ]; then
164238423Sjhb			echo "File $1 has wrong contents"
165263221Sjmmv			FAILED=yes
166238423Sjhb		fi
167238423Sjhb	elif [ $# -eq 3 ]; then
168238423Sjhb		sum=`md5 -q $TEST/$1`
169238423Sjhb		if [ "$sum" != "$3" ]; then
170238423Sjhb			echo "File $1 has wrong contents"
171263221Sjmmv			FAILED=yes
172238423Sjhb		fi
173238423Sjhb	fi
174238423Sjhb}
175238423Sjhb
176238423Sjhb# $1 - relative path to a regular file that should have a conflict
177238423Sjhb# $2 - optional MD5 of the conflict file contents
178238423Sjhbconflict()
179238423Sjhb{
180238423Sjhb	local sum
181238423Sjhb
182238423Sjhb	if ! [ -f $CONFLICTS/$1 ]; then
183238423Sjhb		echo "File $1 missing conflict"
184263221Sjmmv		FAILED=yes
185238423Sjhb	elif [ $# -gt 1 ]; then
186238423Sjhb		sum=`md5 -q $CONFLICTS/$1`
187238423Sjhb		if [ "$sum" != "$2" ]; then
188238423Sjhb			echo "Conflict $1 has wrong contents"
189263221Sjmmv			FAILED=yes
190238423Sjhb		fi
191238423Sjhb	fi
192238423Sjhb}
193238423Sjhb
194238423Sjhb# $1 - relative path to a regular file that should no longer have a conflict
195238423Sjhbresolved()
196238423Sjhb{
197238423Sjhb	if [ -f $CONFLICTS/$1 ]; then
198238423Sjhb		echo "Conflict $1 should be resolved"
199263221Sjmmv		FAILED=yes
200238423Sjhb	fi
201238423Sjhb}
202238423Sjhb
203238423Sjhbif [ `id -u` -ne 0 ]; then
204238423Sjhb	echo "must be root"
205263221Sjmmv	exit 0
206238423Sjhbfi
207238423Sjhb
208238423Sjhbif [ -r /etc/etcupdate.conf ]; then
209238423Sjhb	echo "WARNING: /etc/etcupdate.conf settings may break some tests."
210238423Sjhbfi
211238423Sjhb
212238423Sjhb# Test each of the following resolve options: 'p', 'mf', 'tf', 'r'.
213238423Sjhb
214238423Sjhbbuild_login_conflict
215238423Sjhb
216238423Sjhb# Verify that 'p' doesn't do anything.
217238423Sjhbecho "Checking 'p':"
218258063Sjhbecho 'p' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
219238423Sjhb
220238423Sjhbfile /etc/login.conf "" 95de92ea3f1bb1bf4f612a8b5908cddd
221238423Sjhbmissing /etc/login.conf.db
222238423Sjhbconflict /etc/login.conf
223238423Sjhb
224238423Sjhb# Verify that 'mf' removes the conflict, but does nothing else.
225238423Sjhbecho "Checking 'mf':"
226258063Sjhbecho 'mf' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
227238423Sjhb
228238423Sjhbfile /etc/login.conf "" 95de92ea3f1bb1bf4f612a8b5908cddd
229238423Sjhbmissing /etc/login.conf.db
230238423Sjhbresolved /etc/login.conf
231238423Sjhb
232238423Sjhbbuild_login_conflict
233238423Sjhb
234238423Sjhb# Verify that 'tf' installs the new version of the file.
235238423Sjhbecho "Checking 'tf':"
236258063Sjhbecho 'tf' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
237238423Sjhb
238238423Sjhbfile /etc/login.conf "" 7774a0f9a3a372c7c109c32fd31c4b6b
239238423Sjhbfile /etc/login.conf.db
240238423Sjhbresolved /etc/login.conf
241238423Sjhb
242238423Sjhbbuild_login_conflict
243238423Sjhb
244238423Sjhb# Verify that 'r' installs the resolved version of the file.  To
245238423Sjhb# simulate this, manually edit the merged file so that it doesn't
246238423Sjhb# contain conflict markers.
247238423Sjhbecho "Checking 'r':"
248238423Sjhbcat > $CONFLICTS/etc/login.conf <<EOF
249238423Sjhbdefault:\\
250238423Sjhb	:passwd_format=md5:\\
251238423Sjhb	:copyright=/etc/COPYRIGHT\\
252238423Sjhb        :welcome=/etc/motd:
253238423SjhbEOF
254238423Sjhb
255258063Sjhbecho 'r' | $COMMAND resolve -d $WORKDIR -D $TEST >/dev/null
256238423Sjhb
257238423Sjhbfile /etc/login.conf "" 966e25984b9b63da8eaac8479dcb0d4d
258238423Sjhbfile /etc/login.conf.db
259238423Sjhbresolved /etc/login.conf
260238423Sjhb
261238423Sjhbbuild_aliases_conflict
262238423Sjhb
263238423Sjhb# Verify that 'p' and 'mf' do not generate the newaliases warning.
264238423Sjhbecho "Checking newalias warning for 'p'":
265258063Sjhbecho 'p' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
266238423Sjhbif [ $? -eq 0 ]; then
267238423Sjhb	echo "+ Extra warning"
268263221Sjmmv	FAILED=yes
269238423Sjhbfi
270238423Sjhbecho "Checking newalias warning for 'mf'":
271258063Sjhbecho 'mf' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
272238423Sjhbif [ $? -eq 0 ]; then
273238423Sjhb	echo "+ Extra warning"
274263221Sjmmv	FAILED=yes
275238423Sjhbfi
276238423Sjhb
277238423Sjhb# Verify that 'tf' and 'r' do generate the newaliases warning.
278238423Sjhbbuild_aliases_conflict
279238423Sjhbecho "Checking newalias warning for 'tf'":
280258063Sjhbecho 'tf' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
281238423Sjhbif [ $? -ne 0 ]; then
282238423Sjhb	echo "- Missing warning"
283263221Sjmmv	FAILED=yes
284238423Sjhbfi
285238423Sjhb
286238423Sjhbbuild_aliases_conflict
287238423Sjhbcp $TEST/etc/mail/aliases $CONFLICTS/etc/mail/aliases
288258063Sjhbecho 'r' | $COMMAND resolve -d $WORKDIR -D $TEST | grep -q newalias
289238423Sjhbif [ $? -ne 0 ]; then
290238423Sjhb	echo "- Missing warning"
291263221Sjmmv	FAILED=yes
292238423Sjhbfi
293263221Sjmmv
294263221Sjmmv[ "${FAILED}" = no ]
295