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