• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) International Business Machines Corp., 2001
4# Author: Manoj Iyer <manjo@mail.utexas.edu>
5#
6# Test basic functionality of mv command
7# Test #1:  mv <dir1> <dir2>
8# move dir1 to dir2 and all its contents.
9# Test #2:  mv -b <file1> <file2>
10# move file1 to file2 and backup the file2.
11
12TST_CNT=2
13TST_SETUP=setup
14TST_TESTFUNC=test
15TST_NEEDS_TMPDIR=1
16
17setup()
18{
19	ROD_SILENT mkdir -p tst_mv.old
20}
21
22creat_dirnfiles()
23{
24	local numdirs=$2
25	local numfiles=$3
26	local dirname=$4
27	local dircnt=0
28	local fcnt=0
29
30	tst_res TINFO "Test #$1: Creating $numdirs directories."
31	tst_res TINFO "Test #$1: filling each dir with $numfiles files."
32	while [ $dircnt -lt $numdirs ]
33	do
34		dirname=$dirname/d.$dircnt
35		ROD_SILENT mkdir -p $dirname
36
37		fcnt=0
38		while [ $fcnt -lt $numfiles ]
39		do
40			ROD_SILENT touch $dirname/f.$fcnt
41			fcnt=$(($fcnt+1))
42		done
43		dircnt=$(($dircnt+1))
44	done
45}
46
47creat_expout()
48{
49	local numdir=$1
50	local numfile=$2
51	local dirname=$3
52	local dircnt=0
53	local fcnt=0
54
55	echo "$dirname:"  1>>tst_mv.exp
56	echo "d.$dircnt"  1>>tst_mv.exp
57	while [ $dircnt -lt $numdirs ]
58	do
59		dirname=$dirname/d.$dircnt
60		dircnt=$(($dircnt+1))
61		echo "$dirname:"  1>>tst_mv.exp
62		if [ $dircnt -lt $numdirs ]; then
63			echo "d.$dircnt" 1>>tst_mv.exp
64		fi
65
66		fcnt=0
67		while [ $fcnt -lt $numfiles ]
68		do
69			echo "f.$fcnt " 1>>tst_mv.exp
70			fcnt=$(($fcnt+1))
71		done
72		printf "\n\n" 1>>tst_mv.exp
73	done
74}
75
76test1()
77{
78	numdirs=10
79	numfiles=10
80	dircnt=0
81	fcnt=0
82
83	tst_res TINFO "Test #1: mv <dir1> <dir2> will move dir1 to dir2 and" \
84		       "all its contents"
85
86	creat_dirnfiles 1 $numdirs $numfiles tst_mv.old
87
88	mv tst_mv.old tst_mv.new > tst_mv.err 2>&1
89	if [ $? -ne 0 ]; then
90		cat tst_mv.err
91		tst_brk TFAIL "Test #1: 'mv tst_mv.old tst_mv.new' failed"
92	fi
93
94	tst_res TINFO "Test #1: creating output file"
95	ls -R tst_mv.new > tst_mv.out 2>&1
96
97	tst_res TINFO "Test #1: creating expected output file"
98	creat_expout $numdirs $numfiles tst_mv.new
99
100	tst_res TINFO "Test #1: comparing expected out and actual output file"
101	diff -w -B -q tst_mv.out tst_mv.exp > tst_mv.err 2>&1
102	if [ $? -ne 0 ]; then
103		cat tst_mv.err
104		tst_res TFAIL "Test #1: mv failed."
105	else
106		tst_res TINFO "Test #1: expected same as actual"
107		if [ -f tst_mv.old ]; then
108			tst_res TFAIL "Test #1: mv did not delete old" \
109				       "directory"
110		else
111			tst_res TPASS "Test #1: mv success"
112		fi
113	fi
114}
115
116test2()
117{
118	tst_res TINFO "Test #2: mv -b <file1> <file2> will move dir1 to dir2"
119
120	ROD_SILENT touch tmpfile1 tmpfile2
121
122	MD5_old=$(md5sum tmpfile2 | awk '{print $1}')
123	if [ $? -ne 0 ]; then
124		tst_brk TBROK "Test #2: can't get the MD5 message of file2."
125	fi
126
127	if [ -f "tmpfile2~" ]; then
128		tst_brk TBROK "Test #2: file tmpfile2~ should not exists."
129	fi
130
131	mv -b tmpfile1 tmpfile2
132	if [ $? -ne 0 ]; then
133		tst_brk TBROK "Test #2: 'mv -b tmpfile1 tmpfile2' failed."
134	fi
135
136	# if 'mv -b file1 file2' succeed, there will be "tmpfile2~" file.
137
138	MD5_backup=$(md5sum tmpfile2 | awk '{print $1}')
139	if [ $? -ne 0 ]; then
140		tst_brk TBROK "Test #2: can not get the MD5 message of" \
141			       "backup file2."
142	fi
143
144	if [ "$MD5_old" = "$MD5_backup" -a -f "tmpfile2~" ]; then
145		tst_res TPASS "Test #2: mv -b success"
146	else
147		tst_res TFAIL "Test #2: mv -b failed"
148	fi
149}
150
151. tst_test.sh
152tst_run
153