blob: ff4540c8a3407e7be7af7356622d3f962dd95ecf [file] [log] [blame]
subrata_modakc8649602008-08-27 11:29:55 +00001#!/bin/sh
iyermanojfccc1432003-01-07 23:13:33 +00002################################################################################
Garrett Cooperbe744da2010-02-24 20:15:46 -08003## ##
4## Copyright (c) International Business Machines Corp., 2001 ##
5## ##
6## This program is free software; you can redistribute it and#or modify ##
7## it under the terms of the GNU General Public License as published by ##
8## the Free Software Foundation; either version 2 of the License, or ##
9## (at your option) any later version. ##
10## ##
11## This program is distributed in the hope that it will be useful, but ##
12## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
13## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ##
14## for more details. ##
15## ##
16## You should have received a copy of the GNU General Public License ##
17## along with this program; if not, write to the Free Software ##
18## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ##
19## ##
iyermanojfccc1432003-01-07 23:13:33 +000020################################################################################
21#
Garrett Cooperbe744da2010-02-24 20:15:46 -080022# File : mail_tests.sh
iyermanojfccc1432003-01-07 23:13:33 +000023#
iyermanoj5b006802003-01-10 17:17:31 +000024# Description: Tests basic functions of mail system. The aim of the test is to
Garrett Cooperbe744da2010-02-24 20:15:46 -080025# make sure that certain basic functionality of mail is expected
26# to work as per man page. There are 4 - 5 operations that are
27# done on a regular basis wrt mail. ie.
iyermanojfccc1432003-01-07 23:13:33 +000028#
Garrett Cooperbe744da2010-02-24 20:15:46 -080029# mail sent to an user@domain - received by that user@domain
30# mail is sent to nosuchuser@domain - mail delivery failure
31# mail is sent to user@nosuchdomain - mail delivery failure
32# mail to user1@domain and cc user2@domain - mail rec by both
33# mail to user1@domain and bcc user2@domain - mail rec by both
iyermanojfccc1432003-01-07 23:13:33 +000034#
Garrett Cooperbe744da2010-02-24 20:15:46 -080035# Author: Manoj Iyer, manjo@mail.utexas.edu
36#
37# History: Jan 07 2003 - Created - Manoj Iyer.
38# Jan 09 2003 - Added Test #2 #3 #4 and #5.
39# Jan 10 2002 - Fixed various bugs I had introduced in the test.
40# - Added SETUP and CLEANUP sections
iyermanojfccc1432003-01-07 23:13:33 +000041#
iyermanojfccc1432003-01-07 23:13:33 +000042
iyermanojd91b9242003-01-09 23:08:26 +000043export TST_TOTAL=5
iyermanojfccc1432003-01-07 23:13:33 +000044
Garrett Cooperbe744da2010-02-24 20:15:46 -080045LTPTMP=${TMPBASE:-/tmp}
iyermanojfccc1432003-01-07 23:13:33 +000046
Garrett Cooperbe744da2010-02-24 20:15:46 -080047if [ -z "$LTPBIN" -a -z "$LTPROOT" ]; then
48 LTPBIN=./
iyermanojfccc1432003-01-07 23:13:33 +000049else
Garrett Cooperbe744da2010-02-24 20:15:46 -080050 LTPBIN=$LTPROOT/testcases/bin
iyermanojfccc1432003-01-07 23:13:33 +000051fi
52
Rishikesh K Rajak77312d82010-02-17 18:45:56 +053053isHeirloomMail=0
54checkHeirloomMail()
55{
Garrett Cooperbe744da2010-02-24 20:15:46 -080056 if [ $# -eq 1 -a -f $1 ] && grep "Heirloom" $1; then
57 isHeirloomMail=1
58 fi
Rishikesh K Rajak77312d82010-02-17 18:45:56 +053059}
60
iyermanojd91b9242003-01-09 23:08:26 +000061RC=0
Garrett Cooperbe744da2010-02-24 20:15:46 -080062export TCID=mail_tests::setup
iyermanoj5b006802003-01-10 17:17:31 +000063export TST_COUNT=1
iyermanojd91b9242003-01-09 23:08:26 +000064
Garrett Cooperbe744da2010-02-24 20:15:46 -080065if ! type mail > /dev/null 2>&1; then
66 tst_resm TCONF "mail isn't installed"
67 exit 0
68fi
69
70cat > $LTPTMP/tst_mail.in <<EOF
71This is a test email.
72EOF
73
74if [ $? -ne 0 ] ; then
75 tst_resm TBROK "couldn't create a temporary message"
subrata_modake764b282009-08-23 06:03:44 +000076fi
77
iyermanojd91b9242003-01-09 23:08:26 +000078# check if the user mail_test exists on this system.
79# if not add that user mail_test, will removed before exiting test.
Rishikesh K Rajak2a276cf2010-04-15 12:06:23 +053080id -u mail_test >/dev/null 2>&1
81if [ $? -ne 0 ] ; then
Garrett Cooperbe744da2010-02-24 20:15:46 -080082 tst_resm TINFO "INIT: Adding temporary user mail_test"
83 useradd -m -s /sbin/nologin mail_test > $LTPTMP/tst_mail.out 2>&1
84 if [ $? -ne 0 ]; then
85 tst_brk TBROK $LTPTMP/tst_mail.out NULL \
86 "Test INIT: Failed adding user mail_test. Reason:"
87 exit 1
88 fi
iyermanojd91b9242003-01-09 23:08:26 +000089fi
Garrett Cooperbe744da2010-02-24 20:15:46 -080090
91trap
92
93tst_resm TINFO "INIT: Removing all mails for mail_test and root"
subrata_modak39552a02008-10-21 06:54:15 +000094echo "d*" | mail -u mail_test > /dev/null 2>&1
95echo "d*" | mail -u root > /dev/null 2>&1
iyermanojd91b9242003-01-09 23:08:26 +000096
iyermanojfccc1432003-01-07 23:13:33 +000097# Set return code RC variable to 0, it will be set with a non-zero return code
98# in case of error. Set TFAILCNT to 0, increment if there occures a failure.
99
100TFAILCNT=0
101RC=0
102RC1=0
103RC2=0
104
105# Test #1
106# Test that mail user@domain will send a mail to that user at that domain.
107
Garrett Cooperbe744da2010-02-24 20:15:46 -0800108export TCID=mail_tests::mail01
iyermanojfccc1432003-01-07 23:13:33 +0000109export TST_COUNT=1
110
Garrett Cooperbe744da2010-02-24 20:15:46 -0800111tst_resm TINFO "Test #1: mail root@localhost will send mail to root"
112tst_resm TINFO "Test #1: user on local machine."
iyermanojfccc1432003-01-07 23:13:33 +0000113
114mail -s "Test" root@localhost < $LTPTMP/tst_mail.in \
Garrett Cooperbe744da2010-02-24 20:15:46 -0800115 > $LTPTMP/tst_mail.out 2>&1
116if [ $? -ne 0 ]; then
117 tst_res TFAIL $LTPTMP/tst_mail.out \
118 "Test #1: mail command failed. Reason: "
119 : $(( TFAILCNT += 1 ))
iyermanojfccc1432003-01-07 23:13:33 +0000120else
Garrett Cooperbe744da2010-02-24 20:15:46 -0800121 # check if root received a new email with Test as subject
122 # but wait for the mail to arrive.
iyermanojfccc1432003-01-07 23:13:33 +0000123
Garrett Cooperbe744da2010-02-24 20:15:46 -0800124 sleep 10
125 echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
126 mailsub=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
127 if [ "x$mailsub" != x0 ]; then
128 tst_resm TPASS \
129 "Test #1: Mail was sent to root & was received"
130 else
131 tst_res TFAIL $LTPTMP/tst_mail.res \
132 "Test #1: Mail sent to root, but was not received"
133 : $(( TFAILCNT += 1 ))
134 fi
135
136fi
iyermanojd91b9242003-01-09 23:08:26 +0000137
138# Test #2
139# Test that mail user@bad-domain will result in a warning from the mailer
robbiew7829e1e2004-03-23 16:11:21 +0000140# daemon that the domain does not exist.
iyermanojd91b9242003-01-09 23:08:26 +0000141
Garrett Cooperbe744da2010-02-24 20:15:46 -0800142export TCID=mail_tests::mail02
iyermanojd91b9242003-01-09 23:08:26 +0000143export TST_COUNT=2
144RC=0
145RC1=0
146RC2=0
147
Garrett Cooperbe744da2010-02-24 20:15:46 -0800148tst_resm TINFO "Test #2: mail user@bad-domain will result in failure"
149tst_resm TINFO "Test #2: to deliver the mail. Mailer daemon should"
150tst_resm TINFO "Test #2: report this failure."
iyermanojd91b9242003-01-09 23:08:26 +0000151
robbiew9f219da2003-12-09 17:58:03 +0000152# Don't use underscores in domain names (they're illegal)...
153mail -s "Test" root@thisdomaindoesnotexist < $LTPTMP/tst_mail.in \
Garrett Cooperbe744da2010-02-24 20:15:46 -0800154 > $LTPTMP/tst_mail.out 2>&1
155if [ $? -ne 0 ]; then
156 tst_res TFAIL $LTPTMP/tst_mail.out \
157 "Test #2: mail command failed. Reason:"
158 : $(( TFAILCNT += 1 ))
iyermanojd91b9242003-01-09 23:08:26 +0000159else
Garrett Cooperbe744da2010-02-24 20:15:46 -0800160 # check if Mailer-Deamon reported any delivery failure.
161 # but wait for the mail to arrive first, sleep 5.
162 sleep 5
163 echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
164 checkHeirloomMail $LTPTMP/tst_mail.res
165 if [ $isHeirloomMail -eq 0 ]; then
166 RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3, "Mailer-Daemon")}' \
167 $LTPTMP/tst_mail.res)
168 else
169 RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3 $4 $5, "MailDeliverySubsys")}' \
170 $LTPTMP/tst_mail.res)
171 fi
172
173 ##################################################################
174 # In this testcase, mail will get "Returnedmail:", while mailx will
175 # get "UndeliveredMailReturned:".
176 # Either of mail and mailx may be linked to another.
177 # For example,
178 # /bin/mail -> /bin/mailx
179 # or
180 # /bin/mailx -> /bin/mail
181 ##################################################################
182 if [ $isHeirloomMail -eq 0 ]; then
183 RC2=$(awk '/^>N/ {print match($9 $10, "Returnedmail:")}' \
184 $LTPTMP/tst_mail.res)
185 RC3=$(awk '/^>N/ {print match($9 $10, "UndeliveredMail")}' \
186 $LTPTMP/tst_mail.res)
187 else
188 RC2=$(awk '/^>N/ {print match($11 $12, "Returnedmail:")}' \
189 $LTPTMP/tst_mail.res)
190 RC3=$(awk '/^>N/ {print match($11 $12, "UndeliveredMail")}' \
191 $LTPTMP/tst_mail.res)
192 fi
193 if [ -z "$RC1" -a -z "$RC2" -a -z "$RC3" ]; then
194 tst_res TFAIL $LTPTMP/tst_mail.res \
195 "Test #2: No new mail for root. Reason:"
196 : $(( TFAILCNT += 1 ))
197 else
198
199 if [ $RC1 -ne 0 -a $RC2 -ne 0 ] || [ $RC1 -ne 0 -a $RC3 -ne 0 ]; then
200 tst_resm TPASS \
201 "Test #2: Mailer-Deamon reported delivery failure"
202 else
203 tst_res TFAIL $LTPTMP/tst_mail.res \
204 "Test #2: Mailer-Deamon failed to report delivery failure. Reason:"
205 : $(( TFAILCNT += 1 ))
206 fi
207
208 fi
209
210
211fi
iyermanojd91b9242003-01-09 23:08:26 +0000212
213# Test #3
Garrett Cooperbe744da2010-02-24 20:15:46 -0800214# Test that mail non_existent_user@localhost will result in delivery failure.
iyermanojd91b9242003-01-09 23:08:26 +0000215# Mailer-Deamon will report this failure.
216
Garrett Cooperbe744da2010-02-24 20:15:46 -0800217export TCID=mail_tests::mail03
iyermanojd91b9242003-01-09 23:08:26 +0000218export TST_COUNT=3
219RC=0
220RC1=0
221RC2=0
222
Garrett Cooperbe744da2010-02-24 20:15:46 -0800223tst_resm TINFO "Test #3: mail non_existent_user@localhost will fail"
224tst_resm TINFO "Test #3: to deliver the mail. Mailer daemon should"
225tst_resm TINFO "Test #3: report this failure."
iyermanojd91b9242003-01-09 23:08:26 +0000226
Garrett Cooperbe744da2010-02-24 20:15:46 -0800227mail -s "Test" non_existent_user@localhost < $LTPTMP/tst_mail.in > \
228 $LTPTMP/tst_mail.out 2>&1
229if [ $? -ne 0 ]; then
230 tst_res TFAIL $LTPTMP/tst_mail.out \
231 "Test #3: mail command failed. Reason: "
232 : $(( TFAILCNT += 1 ))
iyermanojd91b9242003-01-09 23:08:26 +0000233else
Garrett Cooperbe744da2010-02-24 20:15:46 -0800234 # check if Mailer-Deamon reported any delivery failure.
235 # but wait for the mail to arrive first, sleep 5.
236 sleep 5
237 echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
238 checkHeirloomMail $LTPTMP/tst_mail.res
239 if [ $isHeirloomMail -eq 0 ]; then
240 RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3, "Mailer-Daemon")}' \
241 $LTPTMP/tst_mail.res)
242 else
243 RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3 $4 $5, "MailDeliverySubsys")}' \
244 $LTPTMP/tst_mail.res)
245 fi
246 ##################################################################
247 # In this testcase, mail will get "Returnedmail:", while mailx will
248 # get "UndeliveredMailReturned:".
249 # Either of mail and mailx may be linked to another.
250 # For example,
251 # /bin/mail -> /bin/mailx
252 # or
253 # /bin/mailx -> /bin/mail
254 #################################################################
255 if [ $isHeirloomMail -eq 0 ]; then
256 RC2=$(awk '/^>N/ {print match($9 $10, "Returnedmail:")}' \
257 $LTPTMP/tst_mail.res)
258 RC3=$(awk '/^>N/ {print match($9 $10, "UndeliveredMail")}' \
259 $LTPTMP/tst_mail.res)
260 else
261 RC2=$(awk '/^>N/ {print match($11 $12, "Returnedmail:")}' \
262 $LTPTMP/tst_mail.res)
263 RC3=$(awk '/^>N/ {print match($11 $12, "UndeliveredMail")}' \
264 $LTPTMP/tst_mail.res)
265 fi
subrata_modak8eb5a852009-11-16 13:25:48 +0000266fi
Garrett Cooperbe744da2010-02-24 20:15:46 -0800267if [ -z "$RC1" -a -z "$RC2" -a -z "$RC3" ]; then
268
269 tst_res TFAIL $LTPTMP/tst_mail.res \
270 "Test #3: No new mail for root. Reason:"
271 : $(( TFAILCNT += 1 ))
subrata_modak8eb5a852009-11-16 13:25:48 +0000272
subrata_modake764b282009-08-23 06:03:44 +0000273else
Garrett Cooperbe744da2010-02-24 20:15:46 -0800274 if [ $RC1 -ne 0 -a $RC2 -ne 0 ] || [ $RC1 -ne 0 -a $RC3 -ne 0 ]; then
275 tst_resm TPASS \
276 "Test #3: Mailer-Daemon reported delivery failure"
277 else
278 tst_res TFAIL $LTPTMP/tst_mail.res \
279 "Test #3: Mailer-Daemon failed to report delivery failure. Reason:"
280 : $(( TFAILCNT += 1 ))
281 fi
subrata_modake764b282009-08-23 06:03:44 +0000282fi
iyermanojd91b9242003-01-09 23:08:26 +0000283
284# Test #4
285# Test that mail -c user@domain option will carbon copy that user.
286
Garrett Cooperbe744da2010-02-24 20:15:46 -0800287export TCID=mail_tests::mail04
iyermanojd91b9242003-01-09 23:08:26 +0000288export TST_COUNT=4
289RC=0
290
Garrett Cooperbe744da2010-02-24 20:15:46 -0800291tst_resm TINFO "Test #4: Test that mail -c user@domain will"
292tst_resm TINFO "Test #4: carbon copy user@domain"
iyermanojd91b9242003-01-09 23:08:26 +0000293# send mail to root and carbon copy mail_test
294mail -s "Test" root@localhost -c mail_test@localhost < \
Garrett Cooperbe744da2010-02-24 20:15:46 -0800295 $LTPTMP/tst_mail.in > $LTPTMP/tst_mail.out 2>&1
296if [ $? -ne 0 ]; then
297 tst_res TFAIL $LTPTMP/tst_mail.out \
298 "Test #4: mail command failed. Reason:"
299 : $(( TFAILCNT += 1 ))
iyermanojd91b9242003-01-09 23:08:26 +0000300else
Garrett Cooperbe744da2010-02-24 20:15:46 -0800301 # Check if mail_test received the mail and
302 # also if root received the main copy of the email.
303 sleep 5
304 echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
305 RC1=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
306 echo "d" | mail -u mail_test > $LTPTMP/tst_mail.res 2>&1
307 RC2=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
308
309 if [ "x$RC1" != x0 -a "x$RC2" != x0 ]; then
310 tst_resm TPASS \
311 "Test #4: Mail was carbon copied to user mail_test"
312 else
313 tst_res TFAIL $LTPTMP/tst_mail.res \
314 "Test #4: mail failed to carbon copy user mail_test. Reason:"
315 : $(( TFAILCNT += 1 ))
316 fi
317
subrata_modake764b282009-08-23 06:03:44 +0000318fi
iyermanojd91b9242003-01-09 23:08:26 +0000319
320# Test #5
321# Test that mail -b user@domain option will blind carbon copy that user.
322
Garrett Cooperbe744da2010-02-24 20:15:46 -0800323export TCID=mail_tests::mail05
iyermanojd91b9242003-01-09 23:08:26 +0000324export TST_COUNT=5
325RC=0
326
Garrett Cooperbe744da2010-02-24 20:15:46 -0800327tst_resm TINFO "Test #5: Test that mail -b user@domain will"
328tst_resm TINFO "Test #5: blind carbon copy user@domain"
iyermanojd91b9242003-01-09 23:08:26 +0000329
330# send mail to root and carbon copy mail_test
Rishikesh K Rajak2a276cf2010-04-15 12:06:23 +0530331mail -s "Test" root@localhost -b mail_test@localhost < \
Garrett Cooperbe744da2010-02-24 20:15:46 -0800332 $LTPTMP/tst_mail.in > $LTPTMP/tst_mail.out 2>&1
333if [ $? -ne 0 ]; then
334 tst_res TFAIL $LTPTMP/tst_mail.out \
335 "Test #5: mail command failed. Reason:"
336 : $(( TFAILCNT += 1 ))
iyermanojd91b9242003-01-09 23:08:26 +0000337else
Garrett Cooperbe744da2010-02-24 20:15:46 -0800338 # Check if mail_test received the mail and
339 # also if root received the main copy of the email.
340 sleep 5
341 echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
342 RC1=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
343 echo "d" | mail -u mail_test > $LTPTMP/tst_mail.res 2>&1
344 RC2=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
345
346 if [ "x$RC1" != x0 -a "x$RC2" != x0 ]; then
347 tst_resm TPASS \
Rishikesh K Rajak2a276cf2010-04-15 12:06:23 +0530348 "Test #5: Mail was blind carbon copied to user mail_test"
Garrett Cooperbe744da2010-02-24 20:15:46 -0800349 else
350 tst_res TFAIL $LTPTMP/tst_mail.res \
Rishikesh K Rajak2a276cf2010-04-15 12:06:23 +0530351 "Test #5: mail failed to blind carbon copy user mail_test. Reason:"
Garrett Cooperbe744da2010-02-24 20:15:46 -0800352 : $(( TFAILCNT += 1 ))
353 fi
354
subrata_modake764b282009-08-23 06:03:44 +0000355fi
iyermanojd91b9242003-01-09 23:08:26 +0000356
iyermanojfccc1432003-01-07 23:13:33 +0000357#CLEANUP & EXIT
358# remove all the temporary files created by this test.
Garrett Cooperbe744da2010-02-24 20:15:46 -0800359export TCID=mail_tests::cleanup
iyermanoj5b006802003-01-10 17:17:31 +0000360export TST_COUNT=1
361
Garrett Cooperbe744da2010-02-24 20:15:46 -0800362tst_resm TINFO "Test CLEAN: Removing temporary files from $LTPTMP"
iyermanojd91b9242003-01-09 23:08:26 +0000363rm -fr $LTPTMP/tst_mail*
364
Garrett Cooperbe744da2010-02-24 20:15:46 -0800365tst_resm TINFO "Test CLEAN: Removing temporary user mail_test"
subrata_modak39552a02008-10-21 06:54:15 +0000366userdel -r mail_test > /dev/null 2>&1
iyermanojfccc1432003-01-07 23:13:33 +0000367
368exit $TFAILCNT