#!/bin/sh
#
# Run a simple backup of the Bareos bin directory
#   then restore it.
#
TestName="secure-erase-test"
JobName=backup
. scripts/functions

scripts/cleanup
cp configs/secure-erase-test/*.conf bin
cp -p scripts/wipe.sh bin/wipe.sh

ln bin/wipe.sh bin/wipe-dir.sh
ln bin/wipe.sh bin/wipe-sd.sh
ln bin/wipe.sh bin/wipe-fd.sh

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp ${conf}/bareos-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >${conf}/bareos-dir.conf

change_jobname BackupClient1 $JobName
start_test

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
setdebug level=100 storage=File
label volume=TestVolume001 storage=File pool=Full
run job=$JobName yes
status storage=File
status storage=File
status storage=File
status storage=File
status storage=File
status storage=File
@sleep 1
status storage=File
status storage=File
status storage=File
status storage=File
status storage=File
@sleep 1
status storage=File
status storage=File
status storage=File
status storage=File
status storage=File
wait
messages
@#
@# now do a restore
@#
@$out $tmp/log2.out
restore where=$tmp/bareos-restores select all done
yes
wait
messages
quit
END_OF_DATA

run_bareos
check_for_zombie_jobs storage=File
stop_bareos

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log1.out
@#setdebug level=100 storage=File
run job=$JobName yes
wait
messages
@#
@# now do a restore
@#
@$out $tmp/log2.out
restore where=$tmp/bareos-restores select all done
yes
wait
messages
quit
END_OF_DATA

#
# Now do a second backup after making a few changes
#
touch ${cwd}/bin/bareos*
echo "test test" > ${cwd}/bin/testxxx
#

run_bareos
check_for_zombie_jobs storage=File
stop_bareos

check_two_logs
check_restore_bin_diff





# make the wipe command fail always
echo '#!/bin/sh
echo "wipe fails of file \"$1\""
exit 1' > bin/wipe.sh

# make a backup and restore with failing wipe command

cat <<END_OF_DATA >$tmp/bconcmds
@$out /dev/null
messages
@$out $tmp/log3.out
@#setdebug level=100 storage=File
run job=$JobName yes
wait
messages
@#
@# now do a restore
@#
@$out $tmp/log4.out
restore where=$tmp/bareos-restores select all done
yes
wait
messages
quit
END_OF_DATA

#
# Now do a second backup after making a few changes
#
touch ${cwd}/bin/bareos*
echo "test test" > ${cwd}/bin/testxxx
#

run_bareos
stop_bareos

check_two_logs






#
# check if "secure erase command" is in status output
# of all three daemons
#

grep "secure erase command" ${tmp}/dir.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, secure erase command found in dir output"
else
   estat=1
fi


grep "secure erase command" ${tmp}/fd.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, secure erase command found in fd output"
else
   estat=2
fi


grep "secure erase command" ${tmp}/sd.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, secure erase command  found in sd output"
else
   estat=3
fi

#
# check for secure erase of spool files
#
grep "SecureErase. executing.*data.*spool" ${tmp}/log1.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, secure erase command used to remove data spool file"
else
   estat=4
fi

grep "SecureErase: executing.*attr.*spool" ${tmp}/log1.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, secure erase command used to remove attr spool file"
else
   estat=5
fi

#
# check for secure erase of bsr files (director)
#
grep "SecureErase: executing.*bsr" ${tmp}/log2.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, secure erase command used to remove data spool file"
else
   estat=6
fi

#
# check for secure erase of existing files during restore (fd)
#
grep ".*-fd.*SecureErase: executing.*bareos-restores" ${tmp}/log2.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, secure erase command used to remove data spool file"
else
   estat=7
fi



#
# check if failing wipe creates fatal error on backup in sd
#
grep "\-sd.*Fatal error: SecureErase:" ${tmp}/log3.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, failing SecureErase command creates fatal error"
else
   estat=8
fi



#
# check if failing wipe creates fatal error on dir
#
grep "\-dir.*Fatal error: SecureErase:" ${tmp}/log4.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, failing SecureErase command creates fatal error"
else
   estat=9
fi

#
# check if failing wipe makes backup fail
#
grep "Termination:.**** Backup Error ***" ${tmp}/log3.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, failing SecureErase command makes full backup fail"
else
   estat=10
fi

#
# check if secure erase cmds are reported correctly in the job report
#
grep 'Dir Secure Erase Cmd.*wipe-dir.sh' tmp/log4.out ${tmp}/log3.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, Dir Secure Erase Command is correctly reported"
else
   estat=11
fi

grep 'SD  Secure Erase Cmd.*wipe-sd.sh' tmp/log4.out ${tmp}/log3.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, Sd Secure Erase Command is correctly reported"
else
   estat=12
fi

grep 'FD  Secure Erase Cmd.*wipe-fd.sh' tmp/log4.out ${tmp}/log3.out 2>&1 >/dev/null
if test $? -eq 0; then
   print_debug "OK, Fd Secure Erase Command is correctly reported"
else
   estat=13
fi


end_test
