]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - tools/regression/usr.sbin/etcupdate/conflicts.sh
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / tools / regression / usr.sbin / etcupdate / conflicts.sh
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
32 WORKDIR=work
33
34 usage()
35 {
36         echo "Usage: tests.sh [-w workdir]"
37         exit 1
38 }
39
40 # Allow the user to specify an alternate work directory.
41 while getopts "w:" option; do
42         case $option in
43                 w)
44                         WORKDIR=$OPTARG
45                         ;;
46                 *)
47                         echo
48                         usage
49                         ;;
50         esac
51 done
52 shift $((OPTIND - 1))
53 if [ $# -ne 0 ]; then
54         usage
55 fi
56
57 CONFLICTS=$WORKDIR/conflicts
58 OLD=$WORKDIR/old
59 NEW=$WORKDIR/current
60 TEST=$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.
65 build_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
73 default:\\
74         :passwd_format=md5:
75 EOF
76         cat > $NEW/etc/login.conf <<EOF
77 default:\\
78         :passwd_format=md5:\\
79         :copyright=/etc/COPYRIGHT
80 EOF
81         cat > $TEST/etc/login.conf <<EOF
82 default:\\
83         :passwd_format=md5:\\
84         :welcome=/etc/motd:
85 EOF
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.
92 build_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
103 MAILER-DAEMON: postmaster
104 postmaster: root
105 EOF
106         cat > $NEW/etc/mail/aliases <<EOF
107 # root: me@my.domain
108
109 # Basic system aliases -- these MUST be present
110 MAILER-DAEMON: postmaster
111 postmaster: root
112
113 # General redirections for pseudo accounts
114 _dhcp:  root
115 _pflogd: root
116 EOF
117         cat > $TEST/etc/mail/aliases <<EOF
118 root: someone@example.com
119
120 # Basic system aliases -- these MUST be present
121 MAILER-DAEMON: postmaster
122 postmaster: foo
123 EOF
124
125         etcupdate -r -d $WORKDIR -D $TEST >/dev/null
126 }
127
128 # $1 - relative path to file that should be missing from TEST
129 missing()
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
137 present()
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
147 file()
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
168 conflict()
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
183 resolved()
184 {
185         if [ -f $CONFLICTS/$1 ]; then
186                 echo "Conflict $1 should be resolved"
187         fi
188 }
189
190 if [ `id -u` -ne 0 ]; then
191         echo "must be root"
192 fi
193
194 if [ -r /etc/etcupdate.conf ]; then
195         echo "WARNING: /etc/etcupdate.conf settings may break some tests."
196 fi
197
198 # Test each of the following resolve options: 'p', 'mf', 'tf', 'r'.
199
200 build_login_conflict
201
202 # Verify that 'p' doesn't do anything.
203 echo "Checking 'p':"
204 echo 'p' | etcupdate resolve -d $WORKDIR -D $TEST >/dev/null
205
206 file /etc/login.conf "" 95de92ea3f1bb1bf4f612a8b5908cddd
207 missing /etc/login.conf.db
208 conflict /etc/login.conf
209
210 # Verify that 'mf' removes the conflict, but does nothing else.
211 echo "Checking 'mf':"
212 echo 'mf' | etcupdate resolve -d $WORKDIR -D $TEST >/dev/null
213
214 file /etc/login.conf "" 95de92ea3f1bb1bf4f612a8b5908cddd
215 missing /etc/login.conf.db
216 resolved /etc/login.conf
217
218 build_login_conflict
219
220 # Verify that 'tf' installs the new version of the file.
221 echo "Checking 'tf':"
222 echo 'tf' | etcupdate resolve -d $WORKDIR -D $TEST >/dev/null
223
224 file /etc/login.conf "" 7774a0f9a3a372c7c109c32fd31c4b6b
225 file /etc/login.conf.db
226 resolved /etc/login.conf
227
228 build_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.
233 echo "Checking 'r':"
234 cat > $CONFLICTS/etc/login.conf <<EOF
235 default:\\
236         :passwd_format=md5:\\
237         :copyright=/etc/COPYRIGHT\\
238         :welcome=/etc/motd:
239 EOF
240
241 echo 'r' | etcupdate resolve -d $WORKDIR -D $TEST >/dev/null
242
243 file /etc/login.conf "" 966e25984b9b63da8eaac8479dcb0d4d
244 file /etc/login.conf.db
245 resolved /etc/login.conf
246
247 build_aliases_conflict
248
249 # Verify that 'p' and 'mf' do not generate the newaliases warning.
250 echo "Checking newalias warning for 'p'":
251 echo 'p' | etcupdate resolve -d $WORKDIR -D $TEST | grep -q newalias
252 if [ $? -eq 0 ]; then
253         echo "+ Extra warning"
254 fi
255 echo "Checking newalias warning for 'mf'":
256 echo 'mf' | etcupdate resolve -d $WORKDIR -D $TEST | grep -q newalias
257 if [ $? -eq 0 ]; then
258         echo "+ Extra warning"
259 fi
260
261 # Verify that 'tf' and 'r' do generate the newaliases warning.
262 build_aliases_conflict
263 echo "Checking newalias warning for 'tf'":
264 echo 'tf' | etcupdate resolve -d $WORKDIR -D $TEST | grep -q newalias
265 if [ $? -ne 0 ]; then
266         echo "- Missing warning"
267 fi
268
269 build_aliases_conflict
270 cp $TEST/etc/mail/aliases $CONFLICTS/etc/mail/aliases
271 echo 'r' | etcupdate resolve -d $WORKDIR -D $TEST | grep -q newalias
272 if [ $? -ne 0 ]; then
273         echo "- Missing warning"
274 fi