xref: /aosp_15_r20/external/ltp/testcases/commands/mv/mv_tests.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) International Business Machines Corp., 2001
4# Author: Manoj Iyer <[email protected]>
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_res TFAIL "Test #1: 'mv tst_mv.old tst_mv.new' failed"
92		return
93	fi
94
95	tst_res TINFO "Test #1: creating output file"
96	ls -R tst_mv.new > tst_mv.out 2>&1
97
98	tst_res TINFO "Test #1: creating expected output file"
99	creat_expout $numdirs $numfiles tst_mv.new
100
101	tst_res TINFO "Test #1: comparing expected out and actual output file"
102	diff -w -B -q tst_mv.out tst_mv.exp > tst_mv.err 2>&1
103	if [ $? -ne 0 ]; then
104		cat tst_mv.err
105		tst_res TFAIL "Test #1: mv failed."
106	else
107		tst_res TINFO "Test #1: expected same as actual"
108		if [ -f tst_mv.old ]; then
109			tst_res TFAIL "Test #1: mv did not delete old" \
110				       "directory"
111		else
112			tst_res TPASS "Test #1: mv success"
113		fi
114	fi
115}
116
117test2()
118{
119	tst_res TINFO "Test #2: mv -b <file1> <file2> will move dir1 to dir2"
120
121	ROD_SILENT touch tmpfile1 tmpfile2
122
123	MD5_old=$(md5sum tmpfile2 | awk '{print $1}')
124	if [ $? -ne 0 ]; then
125		tst_brk TBROK "Test #2: can't get the MD5 message of file2."
126	fi
127
128	if [ -f "tmpfile2~" ]; then
129		tst_brk TBROK "Test #2: file tmpfile2~ should not exists."
130	fi
131
132	mv -b tmpfile1 tmpfile2
133	if [ $? -ne 0 ]; then
134		tst_brk TBROK "Test #2: 'mv -b tmpfile1 tmpfile2' failed."
135	fi
136
137	# if 'mv -b file1 file2' succeed, there will be "tmpfile2~" file.
138
139	MD5_backup=$(md5sum tmpfile2 | awk '{print $1}')
140	if [ $? -ne 0 ]; then
141		tst_brk TBROK "Test #2: can not get the MD5 message of" \
142			       "backup file2."
143	fi
144
145	if [ "$MD5_old" = "$MD5_backup" -a -f "tmpfile2~" ]; then
146		tst_res TPASS "Test #2: mv -b success"
147	else
148		tst_res TFAIL "Test #2: mv -b failed"
149	fi
150}
151
152. tst_test.sh
153tst_run
154