1#! /bin/bash 2set -o pipefail 3 4CWD=$PWD 5SRC_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )" 6IGZIP="$SRC_DIR/igzip $@" 7TEST_DIR="/tmp/igzip_cli_test_$$/" 8TEST_FILE=$SRC_DIR/igzip 9DIFF="diff -q" 10 11mkdir -p $TEST_DIR 12cd $TEST_DIR 13 14cleanup () 15{ 16 cd $CWD 17 rm -rf $TEST_DIR 18 exit $1 19} 20 21clear_dir () 22{ 23 cd /tmp/ 24 rm -rf $TEST_DIR 25 mkdir -p $TEST_DIR 26 cd $TEST_DIR 27} 28 29pass_check() 30{ 31 if [ $1 -eq 0 ]; then 32 echo -e "\e[1;32mPass\e[0;39m: " $2 33 else 34 echo -e "\e[1;31mFail\e[0;39m: " $2 35 cleanup 1 36 fi 37} 38 39fail_check() 40{ 41 if [ $1 -ne 0 ]; then 42 echo -e "\e[1;32mPass\e[0;39m: " $2 43 else 44 echo -e "\e[1;31mFail\e[0;39m: " $2 45 cleanup 1 46 fi 47} 48 49file1=tmp 50file2=jnk 51file3=blah 52bad_file=not_a_file 53dir=this_is_a_directory 54 55default_suffix=".gz" 56ds=$default_suffix 57gzip_standard_suffixes=(".gz" ".z") 58bad_suffix=".bad" 59custom_suffix=".custom" 60 61# Test basic compression and decompression 62ret=0 63cp $TEST_FILE $file1 64$IGZIP $file1 && rm $file1 || ret=1 65for suffix in ${gzip_standard_suffixes[@]}; do 66 if [ "$ds" != "$suffix" ]; then 67 cp -u $file1$ds $file1$suffix 68 fi 69 $IGZIP -d $file1$suffix && $DIFF $file1 $TEST_FILE || ret=1 70 rm $file1 71done 72pass_check $ret "Basic compression and decompression" 73clear_dir 74 75# Test piping 76cat $TEST_FILE | $IGZIP | $IGZIP -d | $DIFF $TEST_FILE - || ret=1 77cat $TEST_FILE | $IGZIP - | $IGZIP -d - | $DIFF $TEST_FILE - || ret=1 78pass_check $ret "Piping compression and decompression" 79 80# Test multiple concatenated gzip files 81ret=0 82(for i in `seq 3`; do $IGZIP -c $TEST_FILE ; done) | $IGZIP -t || ret=1 83pass_check $ret "Multiple gzip concatenated files" 84 85if command -V md5sum >/dev/null 2>&1; then 86 sum1=$((for i in `seq 15`; do $IGZIP -c $TEST_FILE; done) | $IGZIP -cd | md5sum) 87 sum2=$((for i in `seq 15`; do cat $TEST_FILE; done) | md5sum) 88 [[ "$sum1" == "$sum2" ]] && ret=0 || ret=1 89 pass_check $ret "Multiple large gzip concat test" 90 clear_dir 91else 92 echo "Skip: Multiple large gzip concat test" 93fi 94 95 96#Test outifle options 97$IGZIP $TEST_FILE -o $file2$ds && $IGZIP $file2$ds -d -o $file1 && \ 98 test -f $file2$ds && test -f $file1 && $DIFF $TEST_FILE $file1 99pass_check $? "Setting outfile name" 100clear_dir 101 102# Not a file test 103ret=0 104$IGZIP $bad_file &> /dev/null && ret=1 105test -f $bad_file$ds && ret=1 106pass_check $ret "Bad file" 107clear_dir 108 109# Multiple files 110cp $TEST_FILE $file1 && cp $TEST_FILE $file2 && cp $TEST_FILE $file3 && \ 111 $IGZIP $file1 $file2 $file3 && rm $file1 && rm $file2 && rm $file3 && \ 112 $IGZIP -d $file1$ds $file2$ds $file3$ds && \ 113 $DIFF $TEST_FILE $file1 && $DIFF $TEST_FILE $file2 && $DIFF $TEST_FILE $file3 114pass_check $? "Multiple files compression and decompression" 115clear_dir 116 117# Multiple files, one doesn't exist 118ret=0 119cp $TEST_FILE $file1 && cp $TEST_FILE $file2 || ret=1 120$IGZIP $file1 $bad_file $file2 &> /dev/null && ret=1 121rm $file1 && rm $file2 || ret=1 122$IGZIP -d $file1$ds $bad_file$ds $file2$ds &> /dev/null && ret=1 123$DIFF $TEST_FILE $file1 && $DIFF $TEST_FILE $file2 || ret=1 124pass_check $ret "Multiple files with a bad file" 125clear_dir 126 127# Custom suffix test 128cp $TEST_FILE $file1 && $IGZIP -S $custom_suffix $file1 && rm $file1 && \ 129 $IGZIP -d -S $custom_suffix $file1$custom_suffix && $DIFF $TEST_FILE $file1 130pass_check $? "Custom suffix" 131 132# Bad suffix test 133ret=0 134cp $TEST_FILE $file1 && $IGZIP -S $bad_suffix $file1 && rm $file1 || ret=1 135$IGZIP -d $file1$custom_suffix &> /dev/null && ret=1 136pass_check $ret "Bad suffix" 137clear_dir 138 139# Remove file test 140ret=0 141cp $TEST_FILE $file1 && $IGZIP --rm $file1 || ret=1 142test -f $file1 && ret=1 143$IGZIP --rm -d $file1$ds || ret=1 144test -f $file1$ds && ret=1 145pass_check $ret "Remove file" 146clear_dir 147 148# Pass a directory negative test 149ret=0 150mkdir -p $dir || ret=0 151$IGZIP $dir &> /dev/null && ret=1 152clear_dir 153 154mkdir -p $dir$ds || ret=1 155$IGZIP -d $dir &> /dev/null && ret=1 156pass_check $ret "Compress/Decompress Directory without -r" 157clear_dir 158 159# Write permissions test 160cp $TEST_FILE $file1 161chmod 400 $file1 162chmod 500 $TEST_DIR 163$IGZIP $file1 &> /dev/null 164fail_check $? "don't have write permissions" 165chmod -R 700 $TEST_DIR 166clear_dir 167 168# Read permissions test 169cp $TEST_FILE $file1 170chmod 000 $file1 171$IGZIP $file1 &> /dev/null 172fail_check $? "don't have read permissions" 173clear_dir 174 175# File overwrite test -f 176ret=0 177cp $TEST_FILE $file1 && touch $file1$ds || ret=1 178yes | $IGZIP $file1 &> /dev/null && ret=1 179$IGZIP -f $file1 &> /dev/null && cp $file1$ds $file1 || ret=1 180yes | $IGZIP -d $file1 &> /dev/null && ret=1 181$IGZIP -df $file1$ds &> /dev/null && $DIFF $TEST_FILE $file1 || ret=1 182pass_check $ret "Existing file overwrite only with force" 183clear_dir 184 185# Quiet suppresses interactivity 186ret=0 187cp $TEST_FILE $file1 && touch $file1$ds || ret=1 188$IGZIP -q $file1 &> /dev/null && ret=1 189$IGZIP -dq $file1 &> /dev/null && ret=1 190pass_check $ret "Quiet will not overwrite" 191clear_dir 192 193# Input file and output file cannot be the same 194ret=0 195cp $TEST_FILE $file1 && $IGZIP $file1 -o $file1 &> /dev/null && ret=1 196$DIFF $TEST_FILE $file1 &> /dev/null || ret=1 197pass_check $ret "No in place compression" 198clear_dir 199 200# Input file and output file cannot be the same 201ret=0 202cp $TEST_FILE $file1 && $IGZIP $file1 -o $file1$ds &> /dev/null || ret=1 203$IGZIP -do $file1 $file1 &> /dev/null && ret=1 204$DIFF $TEST_FILE $file1 &> /dev/null || ret=1 205pass_check $ret "No in place decompression" 206clear_dir 207 208ret=0 209$IGZIP -n $TEST_FILE -o $file1$ds && $IGZIP -Nd $file1$ds && $DIFF $file1 $TEST_FILE || ret=1 210pass_check $ret "Decompress name with no-name info" 211clear_dir 212 213ret=0 214cp -p $TEST_FILE $file1 && sleep 1 &&\ 215$IGZIP -N $file1 -o $file1$ds && $IGZIP -Nfqd $file1$ds || ret=1 216TIME_ORIG=$(stat --printf=\("%Y\n"\) $TEST_FILE) 217TIME_NEW=$(stat --printf=\("%Y\n"\) $file1) 218if [ "$TIME_ORIG" != "$TIME_NEW" ] ; then 219 ret=1 220fi 221pass_check $ret "Decompress with name info" 222clear_dir 223 224ret=0 225cp -p $TEST_FILE $file1 && touch $file2\\ 226$IGZIP $file1 -o $file1$ds || ret=1 227$IGZIP -t $file1$ds || ret=1 228$IGZIP -t $file2 &> /dev/null && ret=1 229cp $file1$ds $file2 && $IGZIP -t $file1$ds || ret=1 230truncate -s -1 $file1$ds 231$IGZIP -t $file1$ds &> /dev/null && ret=1 232pass_check $ret "Test test" 233clear_dir 234 235# Large stream test with threading if enabled 236ret=0 237(for i in `seq 100`; do cat $TEST_FILE ; done) | $IGZIP -c -T 4 | $IGZIP -t || ret=1 238pass_check $ret "Large stream test" 239 240 241# Large stream tests with decompression and threading if enabled 242if command -V md5sum >/dev/null 2>&1 && command -V dd >/dev/null 2>&1; then 243 ret=0 244 dd if=<(for i in `seq 1000`; do cat $TEST_FILE; done) iflag=fullblock bs=1M count=201 2> out.stder | tee >(md5sum > out.sum1) \ 245 | $IGZIP -c -T 4 | $IGZIP -d | md5sum > out.sum2 \ 246 && $DIFF out.sum1 out.sum2 || ret=1 247 pass_check $ret "Large stream compress test" 248 clear_dir 249 250 if test -e /dev/urandom; then 251 ret=0 252 dd if=/dev/urandom iflag=fullblock bs=1M count=200 2> out.stder | tee >(md5sum > out.sum3) \ 253 | $IGZIP -c -T 2 | $IGZIP -d | md5sum > out.sum4 \ 254 && $DIFF out.sum3 out.sum4 || ret=1 255 pass_check $ret "Large stream random data test" 256 clear_dir 257 fi 258fi 259 260echo "Passed all cli checks" 261cleanup 0 262