#!/bin/bash
#

#################################################################################
#										#
#			TPM2 regression test					#
#			     Written by Ken Goldman				#
#		       IBM Thomas J. Watson Research Center			#
#	$Id: reg.sh 703 2016-07-28 17:21:46Z kgoldman $				#
#										#
# (c) Copyright IBM Corporation 2014						#
# 										#
# All rights reserved.								#
# 										#
# Redistribution and use in source and binary forms, with or without		#
# modification, are permitted provided that the following conditions are	#
# met:										#
# 										#
# Redistributions of source code must retain the above copyright notice,	#
# this list of conditions and the following disclaimer.				#
# 										#
# Redistributions in binary form must reproduce the above copyright		#
# notice, this list of conditions and the following disclaimer in the		#
# documentation and/or other materials provided with the distribution.		#
# 										#
# Neither the names of the IBM Corporation nor the names of its			#
# contributors may be used to endorse or promote products derived from		#
# this software without specific prior written permission.			#
# 										#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS		#
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT		#
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR		#
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT		#
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,	#
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT		#
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,		#
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY		#
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT		#
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE		#
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.		#
#										#
#################################################################################

# handles are
# 80000000 platform hierarchy primary storage key
#    password pps
# storage key under primary
#    password sto
#    storepriv.bin
# signing key under primary
#    password sig
#    signpriv.bin
# RSA encryption key under primary
#    password dec
#    decpriv.bin

# at test entry and exit, there is a platform primary key at 80000000 and
# storage and signing keys under them, ready to load.
# The exception is the last test case, which rolls the seeds.

# This is a namespace prefix 
# For the basic tarball, PREFIX is set to ./   (the current directory)

PREFIX=./

# The rpm release prefixes all the utility names with tss, so PREFIX is set to tss

# PREFIX=tss

printUsage ()
{
    echo ""
    echo ""
    echo "-h help"
    echo "-a all tests"
    echo "-1 random number generator"
    echo "-2 PCR"
    echo "-3 primary keys"
    echo "-4 HMAC session - no bind or salt"
    echo "-5 HMAC session - bind"
    echo "-6 HMAC session - salt"
    echo "-7 Hierarchy"
    echo "-8 Storage"
    echo "-9 Object Change Auth"
    echo "-10 Encrypt and decrypt sessions"
    echo "-11 Sign"
    echo "-12 NV"
    echo "-13 Evict control"
    echo "-14 RSA encrypt decrypt"
    echo "-15 AES encrypt decrypt"
    echo "-16 HMAC and Hash"
    echo "-17 Attestation"
    echo "-18 Policy"
    echo "-19 Context"
    echo "-20 Clocks and Timers"
    echo "-21 DA logic"
    echo "-22 Unseal"
    echo "-23 Duplication"
    echo "-24 ECC"
    echo "-25 Credential"
    echo "-26 Shutdown (only run for simulator)"
    echo ""
    echo ""
    echo "-30 Tests under development (not part of all)"
    echo ""
    echo "-31 Change seed"
}

checkSuccess()
{
if [ $1 -ne 0 ]; then
    echo " ERROR:"
    cat run.out
    exit 255
else
    echo " INFO:"
fi

}

# FIXME should not increment past 254

checkWarning()
{
if [ $1 -ne 0 ]; then
    echo " WARN: $2"
    ((WARN++))
else
    echo " INFO:"
fi

}

checkFailure()
{
if [ $1 -eq 0 ]; then
    echo " ERROR:"
    cat run.out
    exit 255
else
    echo " INFO:"
fi

}

cleanup()
{
# stdout
    rm -f run.out
# general purpose keys
    rm -f storepriv.bin
    rm -f storepub.bin
    rm -f signpriv.bin
    rm -f signpub.bin
    rm -f signeccpriv.bin
    rm -f signeccpub.bin
    rm -f signrpriv.bin
    rm -f signrpub.bin
    rm -f derpriv.bin
    rm -f derpub.bin
    rm -f despriv.bin
    rm -f despub.bin
    rm -f khprivsha1.bin
    rm -f khpubsha1.bin
    rm -f khprivsha256.bin
    rm -f khpubsha256.bin
    rm -f khpubsha384.bin
    rm -f khprivsha384.bin
# misc
    rm -f dec.bin
    rm -f enc.bin
    rm -f msg.bin
    rm -f noncetpm.bin
    rm -f policyapproved.bin
    rm -f pssig.bin
    rm -f sig.bin
    rm -f tkt.bin
    rm -f tmp.bin
    rm -f tmp1.bin
    rm -f tmp2.bin
    rm -f tmpsha1.bin
    rm -f tmpsha256.bin
    rm -f tmpsha384.bin
    rm -f tmppriv.bin
    rm -f tmppub.bin
    rm -f tmpspriv.bin
    rm -f tmpspub.bin
    rm -f to.bin
    rm -f zero.bin
}

export -f checkSuccess
export -f checkWarning
export -f checkFailure
export WARN
export PREFIX

main ()
{
    RC=0
    I=0
    ((WARN=0))

    if [ "$1" == "-h" ]; then
	printUsage
	exit 0
    else
	# the MS simulator needs power up and startup
	if [ -z ${TPM_INTERFACE_TYPE} ] || [ ${TPM_INTERFACE_TYPE} == "socsim" ];  then
	    if [ -z ${TPM_SERVER_TYPE} ] || [ ${TPM_SERVER_TYPE} == "mssim" ]; then
		./regtests/inittpm.sh
	    fi
	fi
    	RC=$?
	if [ $RC -eq 255 ]; then
	    exit 255
	fi
	./regtests/initkeys.sh
	RC=$?
	if [ $RC -eq 255 ]; then
	    exit 255
	fi
	((WARN=$RC))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-1" ]; then
    	./regtests/testrng.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-2" ]; then
    	./regtests/testpcr.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-3" ]; then
    	./regtests/testprimary.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-4" ]; then
    	./regtests/testhmacsession.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-5" ]; then
    	./regtests/testbind.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-6" ]; then
    	./regtests/testsalt.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-7" ]; then
    	./regtests/testhierarchy.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-8" ]; then
    	./regtests/teststorage.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-9" ]; then
    	./regtests/testchangeauth.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-10" ]; then
    	./regtests/testencsession.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-11" ]; then
    	./regtests/testsign.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-12" ]; then
    	./regtests/testnv.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-13" ]; then
    	./regtests/testevict.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-14" ]; then
    	./regtests/testrsa.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-15" ]; then
    	./regtests/testaes.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-16" ]; then
    	./regtests/testhmac.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-17" ]; then
    	./regtests/testattest.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
	((WARN=$RC))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-18" ]; then
    	./regtests/testpolicy.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-19" ]; then
    	./regtests/testcontext.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-20" ]; then
    	./regtests/testclocks.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-21" ]; then
    	./regtests/testda.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-22" ]; then
    	./regtests/testunseal.sh
    	RC=$?
    	if [ $RC -eq 255 ]; then
    	    exit 255
    	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-23" ]; then
    	./regtests/testdup.sh
    	RC=$?
	if [ $RC -eq 255 ]; then
	    exit 255
	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-24" ]; then
    	./regtests/testecc.sh
    	RC=$?
	if [ $RC -eq 255 ]; then
	    exit 255
	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-25" ]; then
    	./regtests/testcredential.sh
    	RC=$?
	if [ $RC -eq 255 ]; then
	    exit 255
	fi
	((I++))
    fi
    if [ "$1" == "-a" ] || [ "$1" == "-26" ]; then
	# the MS simulator supports power cycling
	if [ -z ${TPM_INTERFACE_TYPE} ] || [ ${TPM_INTERFACE_TYPE} == "socsim" ];  then
	    if [ -z ${TPM_SERVER_TYPE} ] || [ ${TPM_SERVER_TYPE} == "mssim" ]; then
		./regtests/testshutdown.sh
	    fi
	fi
   	RC=$?
	if [ $RC -eq 255 ]; then
	    exit 255
	fi
	((I++))
    fi
    if [ "$1" == "-30" ]; then
     	./regtests/testdevel.sh
     	RC=$?
     	if [ $RC -eq 255 ]; then
     	    exit 255
     	fi
     	((I++))
     	((WARN=$RC))
    fi
# this must be the last test
    if [ "$1" == "-a" ] || [ "$1" == "-31" ]; then
    	./regtests/testchangeseed.sh
    	RC=$?
	if [ $RC -eq 255 ]; then
	    exit 255
	fi
	((I++))
    fi
    if [ $RC -eq 255 ]; then
	echo ""
	echo "Failed"
	echo ""
	exit 255
    else
	# -0 is a debug mode that initializes and does not clean up
	if [ "$1" != "-0" ]; then
	    ${PREFIX}flushcontext -ha 80000000
	    cleanup
	fi

	echo ""
	echo "Success - ${I} Tests ${WARN} Warnings"
	echo ""
    fi
}


main "$@"
