1259134Sjhb#!/bin/sh
2259134Sjhb#
3283927Sjhb# Copyright (c) 2013 Hudson River Trading LLC
4259134Sjhb# Written by: John H. Baldwin <jhb@FreeBSD.org>
5259134Sjhb# All rights reserved.
6259134Sjhb#
7259134Sjhb# Redistribution and use in source and binary forms, with or without
8259134Sjhb# modification, are permitted provided that the following conditions
9259134Sjhb# are met:
10259134Sjhb# 1. Redistributions of source code must retain the above copyright
11259134Sjhb#    notice, this list of conditions and the following disclaimer.
12259134Sjhb# 2. Redistributions in binary form must reproduce the above copyright
13259134Sjhb#    notice, this list of conditions and the following disclaimer in the
14259134Sjhb#    documentation and/or other materials provided with the distribution.
15259134Sjhb#
16259134Sjhb# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17259134Sjhb# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18259134Sjhb# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19259134Sjhb# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20259134Sjhb# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21259134Sjhb# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22259134Sjhb# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23259134Sjhb# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24259134Sjhb# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25259134Sjhb# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26259134Sjhb# SUCH DAMAGE.
27259134Sjhb#
28259134Sjhb# $FreeBSD$
29259134Sjhb
30259134Sjhb# Various regression tests for the tzsetup handling in the 'update' command.
31259134Sjhb
32263221SjmmvFAILED=no
33259134SjhbWORKDIR=work
34259134Sjhb
35259134Sjhbusage()
36259134Sjhb{
37259134Sjhb	echo "Usage: tzsetup.sh [-s script] [-w workdir]"
38259134Sjhb	exit 1
39259134Sjhb}
40259134Sjhb
41259134Sjhb# Allow the user to specify an alternate work directory or script.
42259134SjhbCOMMAND=etcupdate
43259134Sjhbwhile getopts "s:w:" option; do
44259134Sjhb	case $option in
45259134Sjhb		s)
46259134Sjhb			COMMAND="sh $OPTARG"
47259134Sjhb			;;
48259134Sjhb		w)
49259134Sjhb			WORKDIR=$OPTARG
50259134Sjhb			;;
51259134Sjhb		*)
52259134Sjhb			echo
53259134Sjhb			usage
54259134Sjhb			;;
55259134Sjhb	esac
56259134Sjhbdone
57259134Sjhbshift $((OPTIND - 1))
58259134Sjhbif [ $# -ne 0 ]; then
59259134Sjhb	usage
60259134Sjhbfi
61259134Sjhb
62259134SjhbCONFLICTS=$WORKDIR/conflicts
63259134SjhbOLD=$WORKDIR/old
64259134SjhbNEW=$WORKDIR/current
65259134SjhbTEST=$WORKDIR/test
66259134Sjhb
67259134Sjhbbuild_trees()
68259134Sjhb{
69259134Sjhb
70259134Sjhb	# Build the base tree, but not /etc/localtime itself
71259134Sjhb	local i j k
72259134Sjhb
73259134Sjhb	rm -rf $OLD $NEW $TEST $CONFLICTS
74259134Sjhb	mkdir -p $OLD $NEW $TEST
75259134Sjhb	mkdir -p $TEST/etc
76259134Sjhb	mkdir -p $TEST/var/db
77259134Sjhb	mkdir -p $TEST/usr/share/zoneinfo
78259134Sjhb
79259134Sjhb	# Create a dummy timezone file
80259134Sjhb	echo "foo" > $TEST/usr/share/zoneinfo/foo
81259134Sjhb
82259134Sjhb}
83259134Sjhb
84259134Sjhb# $1 - relative path to file that should be missing from TEST
85259134Sjhbmissing()
86259134Sjhb{
87259134Sjhb	if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
88259134Sjhb		echo "File $1 should be missing"
89263221Sjmmv		FAILED=yes
90259134Sjhb	fi
91259134Sjhb}
92259134Sjhb
93259134Sjhb# $1 - relative path to file that should be a symlink in TEST
94259134Sjhb# $2 - optional value of the link
95259134Sjhblink()
96259134Sjhb{
97259134Sjhb	local val
98259134Sjhb
99259134Sjhb	if ! [ -L $TEST/$1 ]; then
100259134Sjhb		echo "File $1 should be a link"
101263221Sjmmv		FAILED=yes
102259134Sjhb	elif [ $# -gt 1 ]; then
103259134Sjhb		val=`readlink $TEST/$1`
104259134Sjhb		if [ "$val" != "$2" ]; then
105259134Sjhb			echo "Link $1 should link to \"$2\""
106263221Sjmmv			FAILED=yes
107259134Sjhb		fi
108259134Sjhb	fi
109259134Sjhb}
110259134Sjhb
111259134Sjhb# $1 - relative path to regular file that should be present in TEST
112259134Sjhb# $2 - optional string that should match file contents
113259134Sjhb# $3 - optional MD5 of the flie contents, overrides $2 if present
114259134Sjhbfile()
115259134Sjhb{
116259134Sjhb	local contents sum
117259134Sjhb
118259134Sjhb	if ! [ -f $TEST/$1 ]; then
119259134Sjhb		echo "File $1 should be a regular file"
120263221Sjmmv		FAILED=yes
121259134Sjhb	elif [ $# -eq 2 ]; then
122259134Sjhb		contents=`cat $TEST/$1`
123259134Sjhb		if [ "$contents" != "$2" ]; then
124259134Sjhb			echo "File $1 has wrong contents"
125263221Sjmmv			FAILED=yes
126259134Sjhb		fi
127259134Sjhb	elif [ $# -eq 3 ]; then
128259134Sjhb		sum=`md5 -q $TEST/$1`
129259134Sjhb		if [ "$sum" != "$3" ]; then
130259134Sjhb			echo "File $1 has wrong contents"
131263221Sjmmv			FAILED=yes
132259134Sjhb		fi
133259134Sjhb	fi
134259134Sjhb}
135259134Sjhb
136259134Sjhbif [ `id -u` -ne 0 ]; then
137259134Sjhb	echo "must be root"
138263221Sjmmv	exit 0
139259134Sjhbfi
140259134Sjhb
141259134Sjhbif [ -r /etc/etcupdate.conf ]; then
142259134Sjhb	echo "WARNING: /etc/etcupdate.conf settings may break some tests."
143259134Sjhbfi
144259134Sjhb
145259134Sjhb# First, test for /etc/localtime not existing
146259134Sjhb
147259134Sjhbbuild_trees
148259134Sjhb
149259134Sjhb$COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
150259134Sjhb
151259134Sjhbcat > $WORKDIR/correct.out <<EOF
152259134SjhbEOF
153259134Sjhb
154259134Sjhbecho "Differences for no /etc/localtime with -n:"
155263221Sjmmvdiff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
156263221Sjmmv    || FAILED=yes
157259134Sjhb
158259134Sjhb$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
159259134Sjhb
160259134Sjhbecho "Differences for no /etc/localtime:"
161263221Sjmmvdiff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
162263221Sjmmv    || FAILED=yes
163259134Sjhb
164259134Sjhbmissing /etc/localtime
165259134Sjhbmissing /var/db/zoneinfo
166259134Sjhb
167259134Sjhb# Second, test for /etc/localtime being a symlink
168259134Sjhb
169259134Sjhbbuild_trees
170259134Sjhbln -s /dev/null $TEST/etc/localtime
171259134Sjhb
172259134Sjhb$COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
173259134Sjhb
174259134Sjhbcat > $WORKDIR/correct.out <<EOF
175259134SjhbEOF
176259134Sjhb
177259134Sjhbecho "Differences for symlinked /etc/localtime with -n:"
178263221Sjmmvdiff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
179263221Sjmmv    || FAILED=yes
180259134Sjhb
181259134Sjhb$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
182259134Sjhb
183259134Sjhbecho "Differences for symlinked /etc/localtime:"
184263221Sjmmvdiff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
185263221Sjmmv    || FAILED=yes
186259134Sjhb
187259134Sjhblink /etc/localtime "/dev/null"
188259134Sjhbmissing /var/db/zoneinfo
189259134Sjhb
190259134Sjhb# Third, test for /etc/localtime as a file and a missing /var/db/zoneinfo
191259134Sjhb
192259134Sjhbbuild_trees
193259134Sjhbecho "bar" > $TEST/etc/localtime
194259134Sjhb
195259134Sjhb$COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
196259134Sjhb
197259134Sjhbcat > $WORKDIR/correct.out <<EOF
198259134SjhbWarnings:
199259134Sjhb  Needs update: /etc/localtime (required manual update via tzsetup(1))
200259134SjhbEOF
201259134Sjhb
202259134Sjhbecho "Differences for missing /var/db/zoneinfo with -n:"
203263221Sjmmvdiff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
204263221Sjmmv    || FAILED=yes
205259134Sjhb
206259134Sjhb$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
207259134Sjhb
208259134Sjhbecho "Differences for missing /var/db/zoneinfo:"
209263221Sjmmvdiff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
210263221Sjmmv    || FAILED=yes
211259134Sjhb
212259134Sjhbfile /etc/localtime "bar"
213259134Sjhbmissing /var/db/zoneinfo
214259134Sjhb
215259134Sjhb# Finally, test the case where it should update /etc/localtime
216259134Sjhb
217259134Sjhbbuild_trees
218259134Sjhbecho "bar" > $TEST/etc/localtime
219259134Sjhbecho "foo" > $TEST/var/db/zoneinfo
220259134Sjhb
221259134Sjhb$COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
222259134Sjhb
223259134Sjhbcat > $WORKDIR/correct.out <<EOF
224259134SjhbEOF
225259134Sjhb
226259134Sjhbecho "Differences for real update with -n:"
227263221Sjmmvdiff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
228263221Sjmmv    || FAILED=yes
229259134Sjhb
230259134Sjhb$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
231259134Sjhb
232259134Sjhbecho "Differences for real update:"
233263221Sjmmvdiff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
234263221Sjmmv    || FAILED=yes
235259134Sjhb
236259134Sjhbfile /etc/localtime "foo"
237259134Sjhbfile /var/db/zoneinfo "foo"
238263221Sjmmv
239263221Sjmmv[ "${FAILED}" = no ]
240