xref: /freebsd/usr.sbin/etcupdate/tests/tzsetup_test.sh (revision f72b4f7e149d82ef5b8ed7a3aa415fcd7603b350)
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# Various regression tests for the tzsetup handling in the 'update' command.
30
31FAILED=no
32WORKDIR=work
33
34usage()
35{
36	echo "Usage: tzsetup.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
66build_trees()
67{
68
69	# Build the base tree, but not /etc/localtime itself
70	local i j k
71
72	rm -rf $OLD $NEW $TEST $CONFLICTS
73	mkdir -p $OLD $NEW $TEST
74	mkdir -p $TEST/etc
75	mkdir -p $TEST/var/db
76	mkdir -p $TEST/usr/share/zoneinfo
77
78	# Create a dummy timezone file
79	echo "foo" > $TEST/usr/share/zoneinfo/foo
80
81}
82
83# $1 - relative path to file that should be missing from TEST
84missing()
85{
86	if [ -e $TEST/$1 -o -L $TEST/$1 ]; then
87		echo "File $1 should be missing"
88		FAILED=yes
89	fi
90}
91
92# $1 - relative path to file that should be a symlink in TEST
93# $2 - optional value of the link
94link()
95{
96	local val
97
98	if ! [ -L $TEST/$1 ]; then
99		echo "File $1 should be a link"
100		FAILED=yes
101	elif [ $# -gt 1 ]; then
102		val=`readlink $TEST/$1`
103		if [ "$val" != "$2" ]; then
104			echo "Link $1 should link to \"$2\""
105			FAILED=yes
106		fi
107	fi
108}
109
110# $1 - relative path to regular file that should be present in TEST
111# $2 - optional string that should match file contents
112# $3 - optional MD5 of the flie contents, overrides $2 if present
113file()
114{
115	local contents sum
116
117	if ! [ -f $TEST/$1 ]; then
118		echo "File $1 should be a regular file"
119		FAILED=yes
120	elif [ $# -eq 2 ]; then
121		contents=`cat $TEST/$1`
122		if [ "$contents" != "$2" ]; then
123			echo "File $1 has wrong contents"
124			FAILED=yes
125		fi
126	elif [ $# -eq 3 ]; then
127		sum=`md5 -q $TEST/$1`
128		if [ "$sum" != "$3" ]; then
129			echo "File $1 has wrong contents"
130			FAILED=yes
131		fi
132	fi
133}
134
135if [ `id -u` -ne 0 ]; then
136	echo "must be root"
137	exit 0
138fi
139
140if [ -r /etc/etcupdate.conf ]; then
141	echo "WARNING: /etc/etcupdate.conf settings may break some tests."
142fi
143
144# First, test for /etc/localtime not existing
145
146build_trees
147
148$COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
149
150cat > $WORKDIR/correct.out <<EOF
151EOF
152
153echo "Differences for no /etc/localtime with -n:"
154diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
155    || FAILED=yes
156
157$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
158
159echo "Differences for no /etc/localtime:"
160diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
161    || FAILED=yes
162
163missing /etc/localtime
164missing /var/db/zoneinfo
165
166# Second, test for /etc/localtime being a symlink
167
168build_trees
169ln -s /dev/null $TEST/etc/localtime
170
171$COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
172
173cat > $WORKDIR/correct.out <<EOF
174EOF
175
176echo "Differences for symlinked /etc/localtime with -n:"
177diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
178    || FAILED=yes
179
180$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
181
182echo "Differences for symlinked /etc/localtime:"
183diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
184    || FAILED=yes
185
186link /etc/localtime "/dev/null"
187missing /var/db/zoneinfo
188
189# Third, test for /etc/localtime as a file and a missing /var/db/zoneinfo
190
191build_trees
192echo "bar" > $TEST/etc/localtime
193
194$COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
195
196cat > $WORKDIR/correct.out <<EOF
197Warnings:
198  Needs update: /etc/localtime (required manual update via tzsetup(8))
199EOF
200
201echo "Differences for missing /var/db/zoneinfo with -n:"
202diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
203    || FAILED=yes
204
205$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
206
207echo "Differences for missing /var/db/zoneinfo:"
208diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
209    || FAILED=yes
210
211file /etc/localtime "bar"
212missing /var/db/zoneinfo
213
214# Finally, test the case where it should update /etc/localtime
215
216build_trees
217echo "bar" > $TEST/etc/localtime
218echo "foo" > $TEST/var/db/zoneinfo
219
220$COMMAND -nr -d $WORKDIR -D $TEST > $WORKDIR/testn.out
221
222cat > $WORKDIR/correct.out <<EOF
223EOF
224
225echo "Differences for real update with -n:"
226diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/testn.out \
227    || FAILED=yes
228
229$COMMAND -r -d $WORKDIR -D $TEST > $WORKDIR/test.out
230
231echo "Differences for real update:"
232diff -u -L "correct" $WORKDIR/correct.out -L "test" $WORKDIR/test.out \
233    || FAILED=yes
234
235# XXX tzsetup installs a symlink as of 5e16809c953f
236#file /etc/localtime "foo"
237#file /var/db/zoneinfo "foo"
238
239[ "${FAILED}" = no ]
240