• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
30TST_ID=mv01
31TST_CNT=2
32TST_SETUP=setup
33TST_TESTFUNC=test
34TST_NEEDS_TMPDIR=1
35. tst_test.sh
36
37setup()
38{
39	ROD_SILENT mkdir -p tst_mv.old
40}
41
42creat_dirnfiles()
43{
44	local numdirs=$2
45	local numfiles=$3
46	local dirname=$4
47	local dircnt=0
48	local fcnt=0
49
50	tst_res TINFO "Test #$1: Creating $numdirs directories."
51	tst_res TINFO "Test #$1: filling each dir with $numfiles files."
52	while [ $dircnt -lt $numdirs ]
53	do
54		dirname=$dirname/d.$dircnt
55		ROD_SILENT mkdir -p $dirname
56
57		fcnt=0
58		while [ $fcnt -lt $numfiles ]
59		do
60			ROD_SILENT touch $dirname/f.$fcnt
61			fcnt=$(($fcnt+1))
62		done
63		dircnt=$(($dircnt+1))
64	done
65}
66
67creat_expout()
68{
69	local numdir=$1
70	local numfile=$2
71	local dirname=$3
72	local dircnt=0
73	local fcnt=0
74
75	echo "$dirname:"  1>>tst_mv.exp
76	echo "d.$dircnt"  1>>tst_mv.exp
77	while [ $dircnt -lt $numdirs ]
78	do
79		dirname=$dirname/d.$dircnt
80		dircnt=$(($dircnt+1))
81		echo "$dirname:"  1>>tst_mv.exp
82		if [ $dircnt -lt $numdirs ]; then
83			echo "d.$dircnt" 1>>tst_mv.exp
84		fi
85
86		fcnt=0
87		while [ $fcnt -lt $numfiles ]
88		do
89			echo "f.$fcnt " 1>>tst_mv.exp
90			fcnt=$(($fcnt+1))
91		done
92		printf "\n\n" 1>>tst_mv.exp
93	done
94}
95
96test1()
97{
98	numdirs=10
99	numfiles=10
100	dircnt=0
101	fcnt=0
102
103	tst_res TINFO "Test #1: mv <dir1> <dir2> will move dir1 to dir2 and" \
104		       "all its contents"
105
106	creat_dirnfiles 1 $numdirs $numfiles tst_mv.old
107
108	mv tst_mv.old tst_mv.new > tst_mv.err 2>&1
109	if [ $? -ne 0 ]; then
110		cat tst_mv.err
111		tst_brk TFAIL "Test #1: 'mv tst_mv.old tst_mv.new' failed"
112	fi
113
114	tst_res TINFO "Test #1: creating output file"
115	ls -R tst_mv.new > tst_mv.out 2>&1
116
117	tst_res TINFO "Test #1: creating expected output file"
118	creat_expout $numdirs $numfiles tst_mv.new
119
120	tst_res TINFO "Test #1: comparing expected out and actual output file"
121	diff -w -B -q tst_mv.out tst_mv.exp > tst_mv.err 2>&1
122	if [ $? -ne 0 ]; then
123		cat tst_mv.err
124		tst_res TFAIL "Test #1: mv failed."
125	else
126		tst_res TINFO "Test #1: expected same as actual"
127		if [ -f tst_mv.old ]; then
128			tst_res TFAIL "Test #1: mv did not delete old" \
129				       "directory"
130		else
131			tst_res TPASS "Test #1: mv success"
132		fi
133	fi
134}
135
136test2()
137{
138	tst_res TINFO "Test #2: mv -b <file1> <file2> will move dir1 to dir2"
139
140	ROD_SILENT touch tmpfile1 tmpfile2
141
142	MD5_old=$(md5sum tmpfile2 | awk '{print $1}')
143	if [ $? -ne 0 ]; then
144		tst_brk TBROK "Test #2: can't get the MD5 message of file2."
145	fi
146
147	if [ -f "tmpfile2~" ]; then
148		tst_brk TBROK "Test #2: file tmpfile2~ should not exists."
149	fi
150
151	mv -b tmpfile1 tmpfile2
152	if [ $? -ne 0 ]; then
153		tst_brk TBROK "Test #2: 'mv -b tmpfile1 tmpfile2' failed."
154	fi
155
156	# if 'mv -b file1 file2' succeed, there will be "tmpfile2~" file.
157
158	MD5_backup=$(md5sum tmpfile2 | awk '{print $1}')
159	if [ $? -ne 0 ]; then
160		tst_brk TBROK "Test #2: can not get the MD5 message of" \
161			       "backup file2."
162	fi
163
164	if [ "$MD5_old" = "$MD5_backup" -a -f "tmpfile2~" ]; then
165		tst_res TPASS "Test #2: mv -b success"
166	else
167		tst_res TFAIL "Test #2: mv -b failed"
168	fi
169}
170
171tst_run
172