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 : mkdir_test.sh 23# 24# Description: Test basic functionality of mkdir command 25# - Test #1: mkdir -p can make parent directories as needed 26# 27# Author: Manoj Iyer, manjo@mail.utexas.edu 28# 29# History: Feb 03 2003 - Created - Manoj Iyer. 30# 31# Function: init 32# 33# Description: - Check if command mkdir is available. 34# - Create temprary directory, and temporary files. 35# - Initialize environment variables. 36# 37# Return - zero on success 38# - non zero on failure. return value from commands ($RC) 39init() 40{ 41 42 RC=0 # Return code from commands. 43 export TST_TOTAL=1 # total numner of tests in this file. 44 export TCID=mkdir # this is the init function. 45 export TST_COUNT=0 # init identifier, 46 47 if [ -z "$LTPTMP" ] && [ -z "$TMPBASE" ] 48 then 49 LTPTMP=/tmp 50 else 51 LTPTMP=$TMPBASE 52 fi 53 if [ -z "$LTPBIN" ] && [ -z "$LTPROOT" ] 54 then 55 LTPBIN=./ 56 else 57 LTPBIN=$LTPROOT/testcases/bin 58 fi 59 60 61 $LTPBIN/tst_resm TINFO "INIT: Inititalizing tests." 62 63 which mkdir > $LTPTMP/tst_mkdir.err 2>&1 || RC=$? 64 if [ $RC -ne 0 ] 65 then 66 $LTPBIN/tst_brk TBROK $LTPTMP/tst_mkdir.err NULL \ 67 "Test #1: mkdir command does not exist. Reason:" 68 return $RC 69 fi 70 71 mkdir -p $LTPTMP/tst_mkdir.tmp > $LTPTMP/tst_mkdir.err 2>&1 || RC=$? 72 if [ $RC -ne 0 ] 73 then 74 $LTPBIN/tst_brk TBROK $LTPTMP/tst_mkdir.err NULL \ 75 "Test #1: failed creating temp directory. Reason:" 76 return $RC 77 fi 78 return $RC 79} 80 81 82# Function: creat_expout 83# 84# Description: - create expected output 85# 86# Input: $1 - number of directories to create 87# $2 - number of file to create in each directory 88# $3 - name of the base directory 89# 90# Return - zero on success 91# - non zero on failure. return value ($RC) from commands 92creat_expout() 93{ 94 numdir=$1 # number of directories to create 95 numfile=$2 # number of file to create in each directory 96 dirname=$3 # name of the base directory 97 dircnt=0 # index into dir created in loop 98 fcnt=0 # index into files created in loop 99 RC=0 # return code from commands 100 101 echo "$dirname:" 1>>$LTPTMP/tst_mkdir.exp 102 echo "d.$dircnt" 1>>$LTPTMP/tst_mkdir.exp 103 while [ $dircnt -lt $numdirs ] 104 do 105 dirname=$dirname/d.$dircnt 106 dircnt=$(($dircnt+1)) 107 echo "$dirname:" 1>>$LTPTMP/tst_mkdir.exp 108 if [ $dircnt -lt $numdirs ] 109 then 110 echo "d.$dircnt" 1>>$LTPTMP/tst_mkdir.exp 111 fi 112 fcnt=0 113 while [ $fcnt -lt $numfiles ] 114 do 115 echo "f.$fcnt " 1>>$LTPTMP/tst_mkdir.exp 116 fcnt=$(($fcnt+1)) 117 done 118 printf "\n\n" 1>>$LTPTMP/tst_mkdir.exp 119 done 120} 121 122# Function: test01 123# 124# Description - Test #1: Test that mkdir -p creates parent directories as 125# needed 126# - create N directories and fill each with M files. 127# - mkdir -p dir 128# - list contents of dir and save it to file - actual output 129# - create expected output 130# - compare expected output with actual output. 131# 132# Return - zero on success 133# - non zero on failure. return value from commands ($RC) 134 135test01() 136{ 137 RC=0 # Return value from commands. 138 export TCID=mkdir01 # Name of the test case. 139 export TST_COUNT=1 # Test number. 140 numdirs=10 141 numfiles=10 142 dircnt=0 143 fcnt=0 144 145 $LTPBIN/tst_resm TINFO \ 146 "Test #1: mkdir -p will recursively mkdir contents of directory" 147 148 dirname=$LTPTMP/tst_mkdir.tmp 149 $LTPBIN/tst_resm TINFO "Test #1: Creating $numdirs directories." 150 $LTPBIN/tst_resm TINFO "Test #1: filling each dir with $numfiles files". 151 while [ $dircnt -lt $numdirs ] 152 do 153 dirname=$dirname/d.$dircnt 154 mkdir -p $dirname > $LTPTMP/tst_mkdir.err 2>&1 || RC=$? 155 if [ $RC -ne 0 ] 156 then 157 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mkdir.err \ 158 "Test #1: mkdir -p $dirname failed. Reason:" 159 return $RC 160 fi 161 fcnt=0 162 while [ $fcnt -lt $numfiles ] 163 do 164 touch $dirname/f.$fcnt 165 if [ $RC -ne 0 ] 166 then 167 $LTPBIN/tst_brk TBROK $LTPTMP/tst_mkdir.err NULL \ 168 "Test #1: while creating $numdirs dirs. Reason" 169 return $RC 170 fi 171 fcnt=$(($fcnt+1)) 172 done 173 dircnt=$(($dircnt+1)) 174 done 175 176 $LTPBIN/tst_resm TINFO "Test #1: creating output file" 177 ls -R $LTPTMP/tst_mkdir.tmp > $LTPTMP/tst_mkdir.out 2>&1 178 179 $LTPBIN/tst_resm TINFO "Test #1: creating expected output file" 180 creat_expout $numdirs $numfiles $LTPTMP/tst_mkdir.tmp 181 182 $LTPBIN/tst_resm TINFO \ 183 "Test #1: comparing expected out and actual output file" 184 diff -w -B -q $LTPTMP/tst_mkdir.out $LTPTMP/tst_mkdir.exp \ 185 > $LTPTMP/tst_mkdir.err 2>&1 || RC=$? 186 if [ $RC -ne 0 ] 187 then 188 $LTPBIN/tst_res TFAIL $LTPTMP/tst_mkdir.err \ 189 "Test #1: mkdir -p failed. Reason:" 190 else 191 $LTPBIN/tst_resm TINFO "Test #1: expected same as actual" 192 $LTPBIN/tst_resm TPASS "Test #1: mkdir -R success" 193 fi 194 return $RC 195} 196 197 198# Function: main 199# 200# Description: - Execute all tests, report results. 201# 202# Exit: - zero on success 203# - non-zero on failure. 204 205 206TFAILCNT=0 # Set TFAILCNT to 0, increment on failure. 207RC=0 # Return code from tests. 208 209init || return $RC # Exit if initializing testcases fails. 210 211test01 || RC=$? 212if [ $RC -ne 0 ] 213then 214 TFAILCNT=$(($TFAILCNT+1)) 215fi 216 217 218rm -fr $LTPTMP/tst_mkdir.* 219 220exit $TFAILCNT 221