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. tst_test.sh 17 18setup() 19{ 20 ROD_SILENT mkdir -p tst_mv.old 21} 22 23creat_dirnfiles() 24{ 25 local numdirs=$2 26 local numfiles=$3 27 local dirname=$4 28 local dircnt=0 29 local fcnt=0 30 31 tst_res TINFO "Test #$1: Creating $numdirs directories." 32 tst_res TINFO "Test #$1: filling each dir with $numfiles files." 33 while [ $dircnt -lt $numdirs ] 34 do 35 dirname=$dirname/d.$dircnt 36 ROD_SILENT mkdir -p $dirname 37 38 fcnt=0 39 while [ $fcnt -lt $numfiles ] 40 do 41 ROD_SILENT touch $dirname/f.$fcnt 42 fcnt=$(($fcnt+1)) 43 done 44 dircnt=$(($dircnt+1)) 45 done 46} 47 48creat_expout() 49{ 50 local numdir=$1 51 local numfile=$2 52 local dirname=$3 53 local dircnt=0 54 local fcnt=0 55 56 echo "$dirname:" 1>>tst_mv.exp 57 echo "d.$dircnt" 1>>tst_mv.exp 58 while [ $dircnt -lt $numdirs ] 59 do 60 dirname=$dirname/d.$dircnt 61 dircnt=$(($dircnt+1)) 62 echo "$dirname:" 1>>tst_mv.exp 63 if [ $dircnt -lt $numdirs ]; then 64 echo "d.$dircnt" 1>>tst_mv.exp 65 fi 66 67 fcnt=0 68 while [ $fcnt -lt $numfiles ] 69 do 70 echo "f.$fcnt " 1>>tst_mv.exp 71 fcnt=$(($fcnt+1)) 72 done 73 printf "\n\n" 1>>tst_mv.exp 74 done 75} 76 77test1() 78{ 79 numdirs=10 80 numfiles=10 81 dircnt=0 82 fcnt=0 83 84 tst_res TINFO "Test #1: mv <dir1> <dir2> will move dir1 to dir2 and" \ 85 "all its contents" 86 87 creat_dirnfiles 1 $numdirs $numfiles tst_mv.old 88 89 mv tst_mv.old tst_mv.new > tst_mv.err 2>&1 90 if [ $? -ne 0 ]; then 91 cat tst_mv.err 92 tst_brk TFAIL "Test #1: 'mv tst_mv.old tst_mv.new' failed" 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 152tst_run 153