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