Home | History | Annotate | Download | only in cp
      1 #!/bin/sh
      2 ################################################################################
      3 ##                                                                            ##
      4 ## Copyright (c) International Business Machines  Corp., 2001                 ##
      5 ##  Author:       Manoj Iyer, manjo (at] mail.utexas.edu                           ##
      6 ## Copyright (c) 2016 Cyril Hrubis <chrubis (at] suse.cz>                          ##
      7 ##                                                                            ##
      8 ## This program is free software;  you can redistribute it and#or modify      ##
      9 ## it under the terms of the GNU General Public License as published by       ##
     10 ## the Free Software Foundation; either version 2 of the License, or          ##
     11 ## (at your option) any later version.                                        ##
     12 ##                                                                            ##
     13 ## This program is distributed in the hope that it will be useful, but        ##
     14 ## WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ##
     15 ## or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License   ##
     16 ## for more details.                                                          ##
     17 ##                                                                            ##
     18 ## You should have received a copy of the GNU General Public License          ##
     19 ## along with this program;  if not, write to the Free Software Foundation,   ##
     20 ## Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA           ##
     21 ##                                                                            ##
     22 ################################################################################
     23 #
     24 # Tests basic cp functionality
     25 #
     26 TST_CNT=5
     27 TST_TESTFUNC=do_test
     28 TST_SETUP=setup
     29 TST_NEEDS_TMPDIR=1
     30 . tst_test.sh
     31 
     32 create_tree()
     33 {
     34 	local dirname=$1
     35 	local dircnt=$2
     36 	local filecnt=$3
     37 
     38 	tst_res TINFO "Creating $dircnt directories."
     39 	tst_res TINFO "Filling each dir with $filecnt files".
     40 	while [ $dircnt -gt 0 ]; do
     41 		dirname=$dirname/dir$dircnt
     42 	        ROD mkdir -p $dirname
     43 
     44 		local fcnt=0
     45 	        while [ $fcnt -lt $filecnt ]; do
     46 			ROD touch $dirname/file$fcnt
     47 			fcnt=$((fcnt+1))
     48 		done
     49 		dircnt=$((dircnt-1))
     50 	done
     51 }
     52 
     53 setup()
     54 {
     55 	create_tree "dir" 10 10
     56 	ROD echo LTP > file
     57 }
     58 
     59 compare_dirs()
     60 {
     61 	local src="$1"
     62 	local dst="$2"
     63 
     64 	if diff -r $src $dst; then
     65 		tst_res TPASS "Directories $src and $dst are equal"
     66 	else
     67 		tst_res TFAIL "Directories $src and $dst differs"
     68 		ls -R $src
     69 		echo
     70 		ls -R $dst
     71 	fi
     72 }
     73 
     74 compare_files()
     75 {
     76 	local src="$1"
     77 	local dst="$2"
     78 
     79 	if diff $src $dst; then
     80 		tst_res TPASS "Files $src and $dst are equal"
     81 	else
     82 		tst_res TFAIL "Files $src and $dst differs"
     83 	fi
     84 }
     85 
     86 cp_test()
     87 {
     88 	local args="$1"
     89 	local src="$2"
     90 	local dst="$3"
     91 	EXPECT_PASS cp $args $src $dst
     92 	if [ -f $src ]; then
     93 		compare_files $src $dst
     94 	else
     95 		compare_dirs $src $dst
     96 	fi
     97 	ROD rm -r $dst
     98 }
     99 
    100 do_test()
    101 {
    102 	case $1 in
    103 	1) cp_test ""  "file" "file_copy";;
    104 	2) cp_test -l  "file" "file_copy";;
    105 	3) cp_test -s  "file" "file_copy";;
    106 	4) cp_test -R  "dir"  "dir_copy";;
    107 	5) cp_test -lR "dir"  "dir_copy";;
    108 	esac
    109 }
    110 
    111 tst_run
    112