1#!/bin/sh 2################################################################################ 3## ## 4## Copyright (c) International Business Machines Corp., 2001 ## 5## ## 6## This program is free software; you can redistribute it and#or modify ## 7## it under the terms of the GNU General Public License as published by ## 8## the Free Software Foundation; either version 2 of the License, or ## 9## (at your option) any later version. ## 10## ## 11## This program is distributed in the hope that it will be useful, but ## 12## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ## 13## or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ## 14## for more details. ## 15## ## 16## You should have received a copy of the GNU General Public License ## 17## along with this program; if not, write to the Free Software ## 18## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA ## 19## ## 20################################################################################ 21# 22# File : cp_test.sh 23# 24# Description: Test basic functionality of cp command 25# - Test #1: cp -R can do a recursive copy 26# 27# Author: Manoj Iyer, manjo@mail.utexas.edu 28# 29# History: Jan 30 2003 - Created - Manoj Iyer. 30# Feb 03 2003 - Fixed expected output. 31# 32# Function: init 33# 34# Description: - Check if command cp is available. 35# - Create temprary directory, and temporary files. 36# - Initialize environment variables. 37# 38# Return - zero on success 39# - non zero on failure. return value from commands ($RC) 40init() 41{ 42 43 RC=0 # Return code from commands. 44 export TST_TOTAL=1 # total numner of tests in this file. 45 export TCID=cp # this is the init function. 46 export TST_COUNT=0 # init identifier, 47 48 if [ -z "$LTPTMP" ] && [ -z "$TMPBASE" ] 49 then 50 LTPTMP=/tmp 51 else 52 LTPTMP=$TMPBASE 53 fi 54 if [ -z "$LTPBIN" ] && [ -z "$LTPROOT" ] 55 then 56 LTPBIN=./ 57 else 58 LTPBIN=$LTPROOT/testcases/bin 59 fi 60 61 62 $LTPBIN/tst_resm TINFO "INIT: Inititalizing tests." 63 64 which cp > $LTPTMP/tst_cp.err 2>&1 || RC=$? 65 if [ $RC -ne 0 ] 66 then 67 $LTPBIN/tst_brk TBROK $LTPTMP/tst_cp.err NULL \ 68 "Test #1: cp command does not exist. Reason:" 69 return $RC 70 fi 71 72 mkdir -p $LTPTMP/tst_cp.tmp > $LTPTMP/tst_cp.err 2>&1 || RC=$? 73 if [ $RC -ne 0 ] 74 then 75 $LTPBIN/tst_brk TBROK $LTPTMP/tst_cp.err NULL \ 76 "Test #1: failed creating temp directory. Reason:" 77 return $RC 78 fi 79 return $RC 80} 81 82# Function: creat_dirnfiles 83# 84# Description: - create N directories and fill each with M files 85# 86# Input: $1 - test number 87# $2 - number of directories to create 88# $3 - number of file to create in each directory 89# $4 - name of the base directory 90# 91# Return - zero on success 92# - non zero on failure. return value ($RC) from commands 93creat_dirnfiles() 94{ 95 numdirs=$2 # number of directories to create 96 numfiles=$3 # number of file to create in each directory 97 dirname=$4 # name of the base directory 98 dircnt=0 # index into number of dirs created in loop 99 fcnt=0 # index into number of files created in loop 100 RC=0 # return value from commands 101 102 $LTPBIN/tst_resm TINFO "Test #$1: Creating $numdirs directories." 103 $LTPBIN/tst_resm TINFO "Test #$1: filling each dir with $numfiles files". 104 while [ $dircnt -lt $numdirs ] 105 do 106 dirname=$dirname/d.$dircnt 107 mkdir -p $dirname > $LTPTMP/tst_cp.err 2>&1 || RC=$? 108 if [ $RC -ne 0 ] 109 then 110 $LTPBIN/tst_brk TBROK $LTPTMP/tst_cp.err NULL \ 111 "Test #$1: while creating $numdirs dirs. Reason" 112 return $RC 113 fi 114 fcnt=0 115 while [ $fcnt -lt $numfiles ] 116 do 117 touch $dirname/f.$fcnt 118 if [ $RC -ne 0 ] 119 then 120 $LTPBIN/tst_brk TBROK $LTPTMP/tst_cp.err NULL \ 121 "Test #$1: while creating $numdirs dirs. Reason" 122 return $RC 123 fi 124 fcnt=$(($fcnt+1)) 125 done 126 dircnt=$(($dircnt+1)) 127 done 128 return $RC 129} 130 131 132# Function: creat_expout 133# 134# Description: - create expected output 135# 136# Input: $1 - number of directories to create 137# $2 - number of file to create in each directory 138# $3 - name of the base directory 139# 140# Return - zero on success 141# - non zero on failure. return value ($RC) from commands 142creat_expout() 143{ 144 numdir=$1 # number of directories to create 145 numfile=$2 # number of file to create in each directory 146 dirname=$3 # name of the base directory 147 dircnt=0 # index into dir created in loop 148 fcnt=0 # index into files created in loop 149 RC=0 # return code from commands 150 151 echo "$dirname:" 1>>$LTPTMP/tst_cp.exp 152 echo "d.$dircnt" 1>>$LTPTMP/tst_cp.exp 153 while [ $dircnt -lt $numdirs ] 154 do 155 dirname=$dirname/d.$dircnt 156 dircnt=$(($dircnt+1)) 157 echo "$dirname:" 1>>$LTPTMP/tst_cp.exp 158 if [ $dircnt -lt $numdirs ] 159 then 160 echo "d.$dircnt" 1>>$LTPTMP/tst_cp.exp 161 fi 162 fcnt=0 163 while [ $fcnt -lt $numfiles ] 164 do 165 echo "f.$fcnt " 1>>$LTPTMP/tst_cp.exp 166 fcnt=$(($fcnt+1)) 167 done 168 printf "\n\n" 1>>$LTPTMP/tst_cp.exp 169 done 170} 171 172# Function: test01 173# 174# Description - Test #1: Test that cp -R will copy will copy directories 175# recursively. 176# - create N directories and fill each with M files. 177# - cp -R dir1 to dir2 178# - list contents of dir2 and save it to file - actual output 179# - create expected output 180# - compare expected output with actual output. 181# 182# Return - zero on success 183# - non zero on failure. return value from commands ($RC) 184 185test01() 186{ 187 RC=0 # Return value from commands. 188 export TCID=cp01 # Name of the test case. 189 export TST_COUNT=1 # Test number. 190 numdirs=10 191 numfiles=10 192 dircnt=0 193 fcnt=0 194 195 $LTPBIN/tst_resm TINFO \ 196 "Test #1: cp -R will recursively cp contents of directory" 197 198 creat_dirnfiles 1 $numdirs $numfiles $LTPTMP/tst_cp.tmp || RC=$? 199 if [ $RC -ne 0 ] 200 then 201 return $RC 202 fi 203 204 cp -R $LTPTMP/tst_cp.tmp $LTPTMP/tst_cp.tmp1 > $LTPTMP/tst_cp.err 2>&1 || RC=$? 205 if [ $RC -ne 0 ] 206 then 207 $LTPBIN/tst_res TFAIL $LTPTMP/tst_cp.err \ 208 "Test #1: cp -R failed, cp command returned $RC. Reason:" 209 return $RC 210 fi 211 212 $LTPBIN/tst_resm TINFO "Test #1: creating output file" 213 ls -R $LTPTMP/tst_cp.tmp1 > $LTPTMP/tst_cp.out 2>&1 214 215 $LTPBIN/tst_resm TINFO "Test #1: creating expected output file" 216 creat_expout $numdirs $numfiles $LTPTMP/tst_cp.tmp1 217 218 $LTPBIN/tst_resm TINFO \ 219 "Test #1: comparing expected out and actual output file" 220 diff -w -B -q $LTPTMP/tst_cp.out $LTPTMP/tst_cp.exp > $LTPTMP/tst_cp.err 2>&1 \ 221 || RC=$? 222 if [ $RC -ne 0 ] 223 then 224 $LTPBIN/tst_res TFAIL $LTPTMP/tst_cp.err \ 225 "Test #1: cp -R failed. Reason:" 226 else 227 $LTPBIN/tst_resm TINFO "Test #1: expected same as actual" 228 $LTPBIN/tst_resm TPASS "Test #1: cp -R success" 229 fi 230 return $RC 231} 232 233 234# Function: main 235# 236# Description: - Execute all tests, report results. 237# 238# Exit: - zero on success 239# - non-zero on failure. 240 241 242TFAILCNT=0 # Set TFAILCNT to 0, increment on failure. 243RC=0 # Return code from tests. 244 245init || return $RC # Exit if initializing testcases fails. 246 247test01 || RC=$? 248if [ $RC -ne 0 ] 249then 250 TFAILCNT=$(($TFAILCNT+1)) 251fi 252 253 254rm -fr $LTPTMP/tst_cp.* 255 256exit $TFAILCNT 257