1#!/bin/sh
2################################################################################
3##                                                                            ##
4## Copyright (c) International Business Machines  Corp., 2001                 ##
5## Author:       Manoj Iyer, manjo@mail.utexas.edu                            ##
6##                                                                            ##
7## This program is free software;  you can redistribute it and#or modify      ##
8## it under the terms of the GNU General Public License as published by       ##
9## the Free Software Foundation; either version 2 of the License, or          ##
10## (at your option) any later version.                                        ##
11##                                                                            ##
12## This program is distributed in the hope that it will be useful, but        ##
13## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
14## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
15## for more details.                                                          ##
16##                                                                            ##
17## You should have received a copy of the GNU General Public License          ##
18## along with this program;  if not, write to the Free Software Foundation,   ##
19## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA           ##
20##                                                                            ##
21################################################################################
22#
23# Description:  Test basic functionality of mv command
24#		- Test #1:  mv <dir1> <dir2>
25#		  move dir1 to dir2 and all its contents.
26#		- Test #2:  mv -b <file1> <file2>
27#		  move file1 to file2 and backup the file2.
28#
29
30TCID=mv01
31TST_TOTAL=2
32. test.sh
33
34setup()
35{
36	tst_check_cmds mv
37
38	tst_tmpdir
39
40	tst_resm TINFO "INIT: Inititalizing tests."
41
42	ROD_SILENT mkdir -p tst_mv.old
43}
44
45cleanup()
46{
47	tst_rmdir
48}
49
50creat_dirnfiles()
51{
52	local numdirs=$2
53	local numfiles=$3
54	local dirname=$4
55	local dircnt=0
56	local fcnt=0
57
58	tst_resm TINFO "Test #$1: Creating $numdirs directories."
59	tst_resm TINFO "Test #$1: filling each dir with $numfiles files."
60	while [ $dircnt -lt $numdirs ]
61	do
62		dirname=$dirname/d.$dircnt
63		ROD_SILENT mkdir -p $dirname
64
65		fcnt=0
66		while [ $fcnt -lt $numfiles ]
67		do
68			ROD_SILENT touch $dirname/f.$fcnt
69			fcnt=$(($fcnt+1))
70		done
71		dircnt=$(($dircnt+1))
72	done
73}
74
75creat_expout()
76{
77	local numdir=$1
78	local numfile=$2
79	local dirname=$3
80	local dircnt=0
81	local fcnt=0
82
83	echo "$dirname:"  1>>tst_mv.exp
84	echo "d.$dircnt"  1>>tst_mv.exp
85	while [ $dircnt -lt $numdirs ]
86	do
87		dirname=$dirname/d.$dircnt
88		dircnt=$(($dircnt+1))
89		echo "$dirname:"  1>>tst_mv.exp
90		if [ $dircnt -lt $numdirs ]; then
91			echo "d.$dircnt" 1>>tst_mv.exp
92		fi
93
94		fcnt=0
95		while [ $fcnt -lt $numfiles ]
96		do
97			echo "f.$fcnt " 1>>tst_mv.exp
98			fcnt=$(($fcnt+1))
99		done
100		printf "\n\n" 1>>tst_mv.exp
101	done
102}
103
104test01()
105{
106	numdirs=10
107	numfiles=10
108	dircnt=0
109	fcnt=0
110
111	tst_resm TINFO "Test #1: mv <dir1> <dir2> will move dir1 to dir2 and" \
112		       "all its contents"
113
114	creat_dirnfiles 1 $numdirs $numfiles tst_mv.old
115
116	mv tst_mv.old tst_mv.new > tst_mv.err 2>&1
117	if [ $? -ne 0 ]; then
118		cat tst_mv.err
119		tst_brkm TFAIL "Test #1: 'mv tst_mv.old tst_mv.new' failed"
120	fi
121
122	tst_resm TINFO "Test #1: creating output file"
123	ls -R tst_mv.new > tst_mv.out 2>&1
124
125	tst_resm TINFO "Test #1: creating expected output file"
126	creat_expout $numdirs $numfiles tst_mv.new
127
128	tst_resm TINFO "Test #1: comparing expected out and actual output file"
129	diff -w -B -q tst_mv.out tst_mv.exp > tst_mv.err 2>&1
130	if [ $? -ne 0 ]; then
131		cat tst_mv.err
132		tst_resm TFAIL "Test #1: mv failed."
133	else
134		tst_resm TINFO "Test #1: expected same as actual"
135		if [ -f tst_mv.old ]; then
136			tst_resm TFAIL "Test #1: mv did not delete old" \
137				       "directory"
138		else
139			tst_resm TPASS "Test #1: mv success"
140		fi
141	fi
142}
143
144test02()
145{
146	tst_resm TINFO "Test #2: mv -b <file1> <file2> will move dir1 to dir2"
147
148	ROD_SILENT touch tmpfile1 tmpfile2
149
150	MD5_old=$(md5sum tmpfile2 | awk '{print $1}')
151	if [ $? -ne 0 ]; then
152		tst_brkm TBROK "Test #2: can't get the MD5 message of file2."
153	fi
154
155	if [ -f "tmpfile2~" ]; then
156		tst_brkm TBROK "Test #2: file tmpfile2~ should not exists."
157	fi
158
159	mv -b tmpfile1 tmpfile2
160	if [ $? -ne 0 ]; then
161		tst_brkm TBROK "Test #2: 'mv -b tmpfile1 tmpfile2' failed."
162	fi
163
164	# if 'mv -b file1 file2' succeed, there will be "tmpfile2~" file.
165
166	MD5_backup=$(md5sum tmpfile2 | awk '{print $1}')
167	if [ $? -ne 0 ]; then
168		tst_brkm TBROK "Test #2: can not get the MD5 message of" \
169			       "backup file2."
170	fi
171
172	if [ "$MD5_old" == "$MD5_backup" -a -f "tmpfile2~" ]; then
173		tst_resm TPASS "Test #2: mv -b success"
174	else
175		tst_resm TFAIL "Test #2: mv -b failed"
176	fi
177}
178
179setup
180TST_CLEANUP=cleanup
181
182test01
183test02
184
185tst_exit
186