1#!/bin/sh
2################################################################################
3##										##
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA	##
19##										##
20################################################################################
21#
22# File :		mail_tests.sh
23#
24# Description:  Tests basic functions of mail system. The aim of the test is to
25#		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.
28#
29#		   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
34#
35# 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
41#
42
43export TST_TOTAL=5
44
45LTPTMP=${TMPBASE:-/tmp}
46
47if [ -z "$LTPBIN" -a -z "$LTPROOT" ]; then
48	LTPBIN=./
49else
50	LTPBIN=$LTPROOT/testcases/bin
51fi
52
53isHeirloomMail=0
54checkHeirloomMail()
55{
56	if [ $# -eq 1 -a -f $1 ] && grep "Heirloom" $1; then
57		isHeirloomMail=1
58	fi
59}
60
61RC=0
62export TCID=mail_tests::setup
63export TST_COUNT=1
64
65if ! 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"
76fi
77
78# check if the user mail_test exists on this system.
79# if not add that user mail_test, will removed before exiting test.
80if id -u mail_test >/dev/null 2>&1; then
81	tst_resm TINFO "INIT: Adding temporary user mail_test"
82	useradd -m -s /sbin/nologin mail_test > $LTPTMP/tst_mail.out 2>&1
83	if [ $? -ne 0 ]; then
84		tst_brk TBROK $LTPTMP/tst_mail.out NULL \
85			"Test INIT: Failed adding user mail_test. Reason:"
86		exit 1
87	fi
88fi
89
90trap
91
92tst_resm TINFO "INIT: Removing all mails for mail_test and root"
93echo "d*" | mail -u mail_test > /dev/null 2>&1
94echo "d*" | mail -u root > /dev/null 2>&1
95
96# Set return code RC variable to 0, it will be set with a non-zero return code
97# in case of error. Set TFAILCNT to 0, increment if there occures a failure.
98
99TFAILCNT=0
100RC=0
101RC1=0
102RC2=0
103
104# Test #1
105# Test that mail user@domain will send a mail to that user at that domain.
106
107export TCID=mail_tests::mail01
108export TST_COUNT=1
109
110tst_resm TINFO "Test #1: mail root@localhost will send mail to root"
111tst_resm TINFO "Test #1: user on local machine."
112
113mail -s "Test" root@localhost < $LTPTMP/tst_mail.in \
114	> $LTPTMP/tst_mail.out 2>&1
115if [ $? -ne 0 ]; then
116	tst_res TFAIL $LTPTMP/tst_mail.out \
117	    "Test #1: mail command failed. Reason: "
118	: $(( TFAILCNT += 1 ))
119else
120	# check if root received a new email with Test as subject
121	# but wait for the mail to arrive.
122
123	sleep 10
124	echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
125	mailsub=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
126	if [ "x$mailsub" != x0 ]; then
127		tst_resm TPASS \
128		    "Test #1: Mail was sent to root & was received"
129	else
130		tst_res TFAIL $LTPTMP/tst_mail.res \
131		    "Test #1: Mail sent to root, but was not received"
132		: $(( TFAILCNT += 1 ))
133	fi
134
135fi
136
137# Test #2
138# Test that mail user@bad-domain will result in a warning from the mailer
139# daemon that the domain does not exist.
140
141export TCID=mail_tests::mail02
142export TST_COUNT=2
143RC1=0
144RC2=0
145RC3=0
146RC4=0
147
148tst_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."
151
152tvar=${MACHTYPE%-*}
153tvar=${tvar#*-}
154
155# Don't use underscores in domain names (they're illegal)...
156mail -s "Test" root@thisdomaindoesnotexist < $LTPTMP/tst_mail.in \
157	> $LTPTMP/tst_mail.out 2>&1
158if [ $? -ne 0 ]; then
159	tst_res TFAIL $LTPTMP/tst_mail.out \
160	    "Test #2: mail command failed. Reason:"
161	: $(( TFAILCNT += 1 ))
162else
163	# check if Mailer-Deamon reported any delivery failure.
164	# but wait for the mail to arrive first, sleep 5.
165	sleep 5
166	echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
167	checkHeirloomMail $LTPTMP/tst_mail.res
168	if [ $isHeirloomMail -eq 0 ]; then
169		RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3, "Mailer-Daemon")}' \
170		    $LTPTMP/tst_mail.res)
171	else
172		RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3 $4 $5, "MailDelivery(Subsys|System)")}' \
173		    $LTPTMP/tst_mail.res)
174	fi
175
176	##################################################################
177	# In this testcase, mail will get "Returnedmail:", while mailx will
178	# get "UndeliveredMailReturned:".
179	# Either of mail and mailx may be linked to another.
180	# For example,
181	# /bin/mail -> /bin/mailx
182	# or
183	# /bin/mailx -> /bin/mail
184	##################################################################
185	if [ $isHeirloomMail -eq 0 ]; then
186		RC2=$(awk '/^>N/ {print match($9 $10, "Returnedmail:")}' \
187		    $LTPTMP/tst_mail.res)
188		RC3=$(awk '/^>N/ {print match($9 $10, "UndeliveredMail")}' \
189		    $LTPTMP/tst_mail.res)
190	else
191		RC2=$(awk '/^>N/ {print match($11 $12, "Returnedmail:")}' \
192		    $LTPTMP/tst_mail.res)
193		RC3=$(awk '/^>N/ {print match($11 $12, "UndeliveredMail")}' \
194		    $LTPTMP/tst_mail.res)
195	fi
196	if [ -z "$RC1" -a -z "$RC2" -a -z "$RC3" ]; then
197		RC4=$(awk '{print match($1 $2 $3, "Nomailfor")}' \
198        	    $LTPTMP/tst_mail.res)
199		if [ \( "$tvar" = "redhat" -o "$tvar" = "redhat-linux" \) -a -n "$RC4" ]; then
200			tst_resm TPASS \
201				"Test #2: No new mail for root as expected"
202		else
203			tst_res TFAIL $LTPTMP/tst_mail.res \
204			    "Test #2: No new mail for root. Reason:"
205			: $(( TFAILCNT += 1 ))
206		fi
207	else
208
209		if [ $RC1 -ne 0 -a $RC2 -ne 0 ] || [ $RC1 -ne 0 -a $RC3 -ne 0 ]; then
210			tst_resm TPASS \
211				"Test #2: Mailer-Deamon reported delivery failure"
212		else
213			tst_res TFAIL $LTPTMP/tst_mail.res \
214			"Test #2: Mailer-Deamon failed to report delivery failure. Reason:"
215			: $(( TFAILCNT += 1 ))
216		fi
217
218	fi
219
220
221fi
222
223# Test #3
224# Test that mail non_existent_user@localhost will result in delivery failure.
225# Mailer-Deamon will report this failure.
226
227export TCID=mail_tests::mail03
228export TST_COUNT=3
229RC=0
230RC1=0
231RC2=0
232
233tst_resm TINFO "Test #3: mail non_existent_user@localhost will fail"
234tst_resm TINFO "Test #3: to deliver the mail. Mailer daemon should"
235tst_resm TINFO "Test #3: report this failure."
236
237mail -s "Test" non_existent_user@localhost < $LTPTMP/tst_mail.in > \
238    $LTPTMP/tst_mail.out 2>&1
239if [ $? -ne 0 ]; then
240	tst_res TFAIL $LTPTMP/tst_mail.out \
241	    "Test #3: mail command failed. Reason: "
242	: $(( TFAILCNT += 1 ))
243else
244	# check if Mailer-Deamon reported any delivery failure.
245	# but wait for the mail to arrive first, sleep 5.
246	sleep 5
247	echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
248	checkHeirloomMail $LTPTMP/tst_mail.res
249	if [ $isHeirloomMail -eq 0 ]; then
250		RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3, "Mailer-Daemon")}' \
251		    $LTPTMP/tst_mail.res)
252	else
253		RC1=$(awk '/^>N/ {IGNORECASE=1; print match($3 $4 $5, "MailDelivery(Subsys|System)")}' \
254		    $LTPTMP/tst_mail.res)
255	fi
256	##################################################################
257	# In this testcase, mail will get "Returnedmail:", while mailx will
258	# get "UndeliveredMailReturned:".
259	# Either of mail and mailx may be linked to another.
260	# For example,
261	# /bin/mail -> /bin/mailx
262	# or
263	# /bin/mailx -> /bin/mail
264	#################################################################
265	if [ $isHeirloomMail -eq 0 ]; then
266		RC2=$(awk '/^>N/ {print match($9 $10, "Returnedmail:")}' \
267		    $LTPTMP/tst_mail.res)
268		RC3=$(awk '/^>N/ {print match($9 $10, "UndeliveredMail")}' \
269		    $LTPTMP/tst_mail.res)
270	else
271		RC2=$(awk '/^>N/ {print match($11 $12, "Returnedmail:")}' \
272		    $LTPTMP/tst_mail.res)
273		RC3=$(awk '/^>N/ {print match($11 $12, "UndeliveredMail")}' \
274		    $LTPTMP/tst_mail.res)
275	fi
276fi
277if [ -z "$RC1" -a -z "$RC2" -a -z "$RC3" ]; then
278
279	tst_res TFAIL $LTPTMP/tst_mail.res \
280	    "Test #3: No new mail for root. Reason:"
281	: $(( TFAILCNT += 1 ))
282
283else
284	if [ $RC1 -ne 0 -a $RC2 -ne 0 ] || [ $RC1 -ne 0 -a $RC3 -ne 0 ]; then
285		tst_resm TPASS \
286		    "Test #3: Mailer-Daemon reported delivery failure"
287	else
288		tst_res TFAIL $LTPTMP/tst_mail.res \
289		    "Test #3: Mailer-Daemon failed to report delivery failure. Reason:"
290		: $(( TFAILCNT += 1 ))
291	fi
292fi
293
294# Test #4
295# Test that mail -c user@domain option will carbon copy that user.
296
297export TCID=mail_tests::mail04
298export TST_COUNT=4
299RC=0
300
301tst_resm TINFO "Test #4: Test that mail -c user@domain will"
302tst_resm TINFO "Test #4: carbon copy user@domain"
303# send mail to root and carbon copy mail_test
304mail -s "Test" root@localhost -c mail_test@localhost < \
305    $LTPTMP/tst_mail.in > $LTPTMP/tst_mail.out 2>&1
306if [ $? -ne 0 ]; then
307	tst_res TFAIL $LTPTMP/tst_mail.out \
308	    "Test #4: mail command failed. Reason:"
309	: $(( TFAILCNT += 1 ))
310else
311	# Check if mail_test received the mail and
312	# also if root received the main copy of the email.
313	sleep 5
314	echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
315	RC1=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
316	echo "d" | mail -u mail_test > $LTPTMP/tst_mail.res 2>&1
317	RC2=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
318
319	if [ "x$RC1" != x0 -a "x$RC2" != x0 ]; then
320		tst_resm TPASS \
321		    "Test #4: Mail was carbon copied to user mail_test"
322	else
323		tst_res TFAIL $LTPTMP/tst_mail.res \
324		    "Test #4: mail failed to carbon copy user mail_test. Reason:"
325		: $(( TFAILCNT += 1 ))
326	fi
327
328fi
329
330# Test #5
331# Test that mail -b user@domain option will blind carbon copy that user.
332
333export TCID=mail_tests::mail05
334export TST_COUNT=5
335RC=0
336
337tst_resm TINFO "Test #5: Test that mail -b user@domain will"
338tst_resm TINFO "Test #5: blind carbon copy user@domain"
339
340# send mail to root and blind carbon copy mail_test
341mail -s "Test" root@localhost -c mail_test@localhost < \
342	$LTPTMP/tst_mail.in > $LTPTMP/tst_mail.out 2>&1
343if [ $? -ne 0 ]; then
344	tst_res TFAIL $LTPTMP/tst_mail.out \
345	    "Test #5: mail command failed. Reason:"
346	: $(( TFAILCNT += 1 ))
347else
348	# Check if mail_test received the mail and
349	# also if root received the main copy of the email.
350	sleep 5
351	echo "d" | mail -u root > $LTPTMP/tst_mail.res 2>&1
352	RC1=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
353	echo "d" | mail -u mail_test > $LTPTMP/tst_mail.res 2>&1
354	RC2=$(awk '/^>N/ {print match($9, "Test")}' $LTPTMP/tst_mail.res)
355
356	if [ "x$RC1" != x0 -a "x$RC2" != x0 ]; then
357		tst_resm TPASS \
358		    "Test #5: Mail was carbon copied to user mail_test"
359	else
360		tst_res TFAIL $LTPTMP/tst_mail.res \
361		    "Test #5: mail failed to carbon copy user mail_test. Reason:"
362		: $(( TFAILCNT += 1 ))
363	fi
364
365fi
366
367#CLEANUP & EXIT
368# remove all the temporary files created by this test.
369export TCID=mail_tests::cleanup
370export TST_COUNT=1
371
372tst_resm TINFO "Test CLEAN: Removing temporary files from $LTPTMP"
373rm -fr $LTPTMP/tst_mail*
374
375tst_resm TINFO "Test CLEAN: Removing temporary user mail_test"
376userdel -r mail_test > /dev/null 2>&1
377
378exit $TFAILCNT
379