#!/usr/bin/bash

##
#   run_claes TESTS
##

#-----------------------------------------------------------#
red() {
     echo -en "\\0033[1;31m"
     echo "$1"
     echo -en "\\0033[0;39m"
}

green() {
     echo -en "\\0033[1;32m"
     echo "$1"
     echo -en "\\0033[0;39m"

}

orange() {
     echo -en "\\0033[1;33m"
     echo "$1"
     echo -en "\\0033[0;39m"

}
#-----------------------------------------------------------#

/bin/mkdir -p claes
echo "Preparing claes for canned password"
cp /bin/claes claes/aes; /bin/sed -i '98s/^/ASKPASS = "echo deadbeefdead"/' claes/aes
cd claes
cp ../fixed/bsd .
rm -f err log

DESC="\nENcrypt a gpg format file produced by clAES : bsd ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f bsd.asc
./aes -debug bsd  >> log 2>> err 
echo "--> : $?"  
if ls -l bsd.asc ; then green "OK"; else red "FAILED"; fi

DESC="\nDEcrypt a aes-gpg encrypted file with clAES : bsd.asc ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
echo echo y | mv -f bsd bsd.orig; echo -n "y"| ./aes -debug -decrypt bsd.asc  >> log 2>> err 
echo "--> : $?"  
if diff bsd bsd.orig ; then green "OK"; else red "FAILED"; fi

# CMS tests
DESC="\nENcrypt a CMS formated file produced by clAES : bsd ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f bsd.cms
./aes -debug -cms bsd  >> log 2>> err 
echo "--> : $?"  
if ls -l bsd.asc ; then green "OK"; else red "FAILED"; fi

DESC="\nDEcrypt a CMS encrypted file with clAES : bsd.cms ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
echo echo y | mv -f bsd bsd.orig; echo -n "y"| ./aes -debug -cms -decrypt bsd.cms  >> log 2>> err 
echo "--> : $?"  
if diff bsd bsd.orig ; then green "OK"; else red "FAILED"; fi

# openssl tests
DESC="\nENcrypt a raw OpenSSL formated file produced by clAES 256 bits : bsd ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f bsd.asc
./aes -debug -openssl bsd  >> log 2>> err 
echo "--> : $?"  
if ls -l bsd.asc ; then green "OK"; else red "FAILED"; fi

DESC="\nDEcrypt a raw OpenSSL encrypted file 256 bits : bsd.asc ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
echo echo y | mv -f bsd bsd.orig; echo -n "y"| ./aes -debug -openssl -decrypt bsd.asc  >> log 2>> err 
echo "--> : $?"  
if diff bsd bsd.orig ; then green "OK"; else red "FAILED"; fi

DESC="\nENcrypt a raw OpenSSL formated file produced by clAES 128 bits : bsd ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f bsd.asc
./aes -debug -openssl -128 bsd  >> log 2>> err 
echo "--> : $?"  
if ls -l bsd.asc ; then green "OK"; else red "FAILED"; fi

DESC="\nDEcrypt a raw OpenSSL encrypted file 128 bits : bsd.asc ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
echo echo y | mv -f bsd bsd.orig; echo -n "y"| ./aes -debug -openssl -128 -decrypt bsd.asc  >> log 2>> err 
echo "--> : $?"  
if diff bsd bsd.orig ; then green "OK"; else red "FAILED"; fi


### GPG2 TESTS

DESC="\nencrypt file using gpg2 with AES-128 (no compression): bsd ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f gpg2-AES-128.asc gpg2-AES-128
echo "gpg -ca -z 0 --cipher-algo AES --batch --passphrase deadbeefdead -o ./gpg2-AES-128.asc ./bsd"
gpg -ca -z 0 --cipher-algo AES --batch --passphrase deadbeefdead -o ./gpg2-AES-128.asc ./bsd
echo "--> : $?"  
if ls -l  gpg2-AES-128.asc; then green "gpg2 encryption OK"; else red "gpg2 encryption FAILED"; fi

DESC="\nDEcrypt a aes-gpg encrypted file with clAES : gpg2-AES-128.asc ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./aes -debug -decrypt gpg2-AES-128.asc >> log 2>> err
echo "--> : $?"  
if diff gpg2-AES-128 bsd.orig ; then green "clAES decryption OK"; else red "clAES decryption FAILED"; fi


DESC="\nencrypt file using gpg2 with AES-256 (no compression): bsd ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f gpg2-AES-256.asc gpg2-AES-256
echo "gpg -ca -z 0 --cipher-algo AES256 --batch --passphrase deadbeefdead -o ./gpg2-AES-256.asc ./bsd"
gpg -ca -z 0 --cipher-algo AES256 --batch --passphrase deadbeefdead -o ./gpg2-AES-256.asc ./bsd
echo "--> : $?"  
if ls -l  gpg2-AES-256.asc; then green "gpg2 encryption OK"; else red "gpg2 encryption FAILED"; fi

DESC="\nDEcrypt a aes-gpg encrypted file with clAES : gpg2-AES-256.asc ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./aes -debug -decrypt gpg2-AES-256.asc >> log 2>> err
echo "--> : $?"  
if diff gpg2-AES-256 bsd.orig ; then green "clAES decryption OK"; else red "clAES decryption FAILED"; fi

DESC="\nencrypt file using gpg2 with AES-256 (BINARY output): bsd ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f gpg2-AES-256.asc gpg2-AES-256
echo "gpg -c -z 0 --cipher-algo AES256 --batch --passphrase deadbeefdead -o ./gpg2-AES-256.asc ./bsd"
gpg -c -z 0 --cipher-algo AES256 --batch --passphrase deadbeefdead -o ./gpg2-AES-256.asc ./bsd
echo "--> : $?"  
if ls -l  gpg2-AES-256.asc; then green "gpg2 encryption OK"; else red "gpg2 encryption FAILED"; fi

DESC="\nDEcrypt a aes-gpg encrypted file with clAES : gpg2-AES-256.asc ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./aes -debug -decrypt gpg2-AES-256.asc >> log 2>> err
echo "--> : $?"  
if diff gpg2-AES-256 bsd.orig ; then green "clAES decryption OK"; else red "clAES decryption FAILED"; fi


### OPENSSL tests


DESC="\nencrypt file using openssl with AES-256: bsd ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f ossl-AES-256.asc ossl-AES-256
echo "openssl aes-256-cbc  -pbkdf2 -a -p -k deadbeefdead -in ./bsd -out ossl-AES-256.asc"
openssl aes-256-cbc  -pbkdf2 -a -p -k deadbeefdead -in ./bsd -out ossl-AES-256.asc
echo "--> : $?"  
if ls -l  ossl-AES-256.asc; then green "OpenSSL encryption OK"; else red "OpenSSL encryption FAILED"; fi

DESC="\ndecrypt ossl-AES-256.asc  with clAES ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./aes -debug -openssl -decrypt ossl-AES-256.asc >> log 2>> err
echo "--> : $?"  
if diff ossl-AES-256 bsd.orig ; then green "clAES decryption OK"; else red "clAES decryption FAILED"; fi

DESC="\nencrypt file using openssl with AES-128: bsd ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f ossl-AES-128.asc ossl-AES-128
echo "openssl aes-128-cbc  -pbkdf2 -a -p -k deadbeefdead -in ./bsd -out ossl-AES-128.asc"
openssl aes-128-cbc  -pbkdf2 -a -p -k deadbeefdead -in ./bsd -out ossl-AES-128.asc
echo "--> : $?"  
if ls -l  ossl-AES-128.asc; then green "OpenSSL encryption OK"; else red "OpenSSL encryption FAILED"; fi

DESC="\ndecrypt ossl-AES-128.asc  with clAES ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./aes -debug -openssl -128 -decrypt ossl-AES-128.asc >> log 2>> err
echo "--> : $?"  
if diff ossl-AES-128 bsd.orig ; then green "clAES decryption OK"; else red "clAES decryption FAILED"; fi


DESC="\nencrypt file using openssl with AES-256 (BINARY output): bsd ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f ossl-AES-256.asc ossl-AES-256
echo "openssl aes-256-cbc  -pbkdf2 -p -k deadbeefdead -in ./bsd -out ossl-AES-256.asc"
openssl aes-256-cbc  -pbkdf2 -p -k deadbeefdead -in ./bsd -out ossl-AES-256.asc
echo "--> : $?"  
if ls -l  ossl-AES-256.asc; then green "OpenSSL encryption OK"; else red "OpenSSL encryption FAILED"; fi

DESC="\ndecrypt ossl-AES-256.asc  with clAES ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
./aes -debug -openssl -decrypt ossl-AES-256.asc >> log 2>> err
echo "--> : $?"  
if diff ossl-AES-128 bsd.orig ; then green "clAES decryption OK"; else red "clAES decryption FAILED"; fi


DESC="\nENcrypt a HUGE random file produced by clAES : huge ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
rm -f huge huge.asc
dd if=/dev/urandom of=huge bs=1M count=20
./aes -debug huge  >> log 2>> err 
echo "--> : $?"  
if ls -l huge.asc ; then green " encrypting a huge file OK"; else red "encrypting a huge file FAILED"; fi

DESC="\nDEcrypt a HUGE encrypted file with clAES : huge.asc ... "
echo -e "------ ${DESC}" >> log; echo -e "------ ${DESC}" >> err; echo -e ${DESC}
echo echo y | mv -f huge huge.orig; echo -n "y"| ./aes -debug -decrypt huge.asc  >> log 2>> err 
echo "--> : $?"  
if diff huge huge.orig ; then green "OK"; else red "FAILED"; fi

echo
orange "claes tests finished."
echo

ls -l
