2 changes: 1 addition & 1 deletion .ci/travis-build-and-run-tests.sh
Expand Up @@ -122,7 +122,7 @@ popd
pushd ./test/system

# Run the tests on ALL device TCTIs configuration
PATH=$(pwd)/../../build/tools:${PATH} ./test_all.sh
PATH=$(pwd)/../../build/tools:${PATH} ./test.sh --plain

# done go back to tpm2-tools directory
popd
Expand Down
32 changes: 13 additions & 19 deletions test/system/README.md
@@ -1,25 +1,19 @@
**these are some scripts for the testing of TPM (Trusted Platform Module) 2.0 tools **
# Testing Framework

Below are script instructions:
##1: integration testing
test_smoking.sh is a quick & brief testing for all tpm-tools.
test_all.sh runs all separated .sh.
The command **test.sh** can be used to run the test scripts. Invoking
**test.sh** will run the full test suite. For example:

##2: separated testing
There are some separated .sh for each of tpm2-tool. Name convention is test_tpm2_xxx.sh.
```
./test.sh
```

##3: algorithm testing
Named as test_algs_tpm2_XXX.sh, test all algorithms involved in the parameters of tpm2-tools.
A part of test_tpm2_XXX_all.sh also contain algorithm testing.
One can also run individual test scripts by invoking **test.sh** with an
argument of the test file name(s) to run, for example:

##4: others
test_tpm2_XXX_func.sh for adding some test cases in furture.
```
./test.sh test_tpm2_hmac.sh test_tpm2_hash.sh
```

##Condition and Operation instructions:
1.TPM is initialized
2.install tpm2-tools
3.must start resourcemgr before run all test scripts
4.clean up all log files
5.run xxx.sh for automatic test
6.results are printed to stdout and an exit code of 0 indicates success.
**NOTE: That the tools must be avialble on *PATH* **

**NOTE: That the test script should be run against the tpm simulator with a resource manager**
58 changes: 53 additions & 5 deletions test/system/test.sh
Expand Up @@ -55,6 +55,19 @@ mag=$'\e[1;35m'
cyn=$'\e[1;36m'
end=$'\e[0m'

# Set the default to print in a prety output
PRETTY=true

clear_colors() {
red=''
grn=''
yel=''
blu=''
mag=''
cyn=''
end=''
}

test_wrapper() {

./$1 &
Expand All @@ -63,13 +76,37 @@ test_wrapper() {
spin='-\|/'
i=0
while kill -0 $pid 2>/dev/null; do
i=$(( (i+1) %4 ))
printf "\r${yel}${spin:$i:1}${end}"
sleep .1
if [ "$PRETTY" == true ]; then
i=$(( (i+1) %4 ))
printf "\r${yel}${spin:$i:1}${end}"
sleep .1
fi
done

wait $pid
if [ $? -eq 0 ]; then
rc=$?

failed_checks=0
# check for leftover files and fail if present.
leftovers=`git ls-files -o`
if [ "$leftovers" != "" ]; then
printf "Test left files around, found: %s\n" "$leftovers"
failed_checks=1
fi

# check for persistent handles
leftovers=`tpm2_listpersistent`
if [ "$leftovers" != "" ]; then
printf "Test left peristent objects loaded, found: %s\n" "$leftovers"
failed_checks=1
fi

if [ $failed_checks -ne 0 ]; then
# set the $? variable to not be 0!
false
fi

if [ $rc -eq 0 ]; then
printf "\r${grn}$1 ... PASSED${end}\n"
let "pass++"
else
Expand All @@ -80,7 +117,7 @@ test_wrapper() {
}

# Get a list of test scripts, all tests should begin with test_tpm2_ and
# be a shell script. This avoids adding test_all.sh
# be a shell script.
tests=`ls test_tpm2_*.sh`

# Building with asan on clang, the leak sanitizier
Expand All @@ -95,13 +132,24 @@ if [ "$ASAN_ENABLED" == "true" ]; then
tests=`echo $tests | grep -v test_tpm2_getmanufec.sh`
fi

while true; do
case "$1" in
-p | --plain ) PRETTY=false; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done

# If command line arguments are provided, assume it is
# the test suite to execute.
# IE: test_tpm2_getrandom.sh
if [ "$#" -gt 0 ]; then
tests="$@"
fi

if [ "$PRETTY" != true ]; then
clear_colors
fi

for t in $tests; do
test_wrapper $t;
Expand Down
84 changes: 0 additions & 84 deletions test/system/test_all.sh

This file was deleted.

2 changes: 1 addition & 1 deletion test/system/test_tpm2_evictcontrol.sh
Expand Up @@ -39,7 +39,7 @@ trap onerror ERR

cleanup() {
rm -f primary.ctx decrypt.ctx key.pub key.priv key.name decrypt.out \
encrypt.out secret.dat &>/dev/null
encrypt.out secret.dat key.ctx
}
trap cleanup EXIT

Expand Down
2 changes: 1 addition & 1 deletion test/system/test_tpm2_hmac.sh
Expand Up @@ -55,7 +55,7 @@ trap onerror ERR

cleanup() {
rm -f $file_primary_key_ctx $file_hmac_key_pub $file_hmac_key_priv \
$file_hmac_key_name $file_hmac_output
$file_hmac_key_name $file_hmac_output evict.log
if [ "$1" == "all" ]; then
rm -f $file_hmac_key_ctx $file_input_data
fi
Expand Down
97 changes: 45 additions & 52 deletions test/system/test_tpm2_load.sh
Expand Up @@ -4,30 +4,30 @@
# Copyright (c) 2016, Intel Corporation
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
#
# 2. 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.
#
# 3. Neither the name of Intel 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
# 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.
#;**********************************************************************;
alg_primary_obj=0x000B
Expand All @@ -47,50 +47,43 @@ file_load_output=load_"$file_load_key_ctx"
Handle_parent=0x81010018
Handle_ek_load=0x81010017

fail()
{
echo "$1 test fail, please check the environment or parameters!"
# echo ""$1" fail" >>test_encryptdecrypt_error.log
exit 1
}
Pass()
{
echo ""$1" pass" >>test_getpubak_pass.log
onerror() {
echo "$BASH_COMMAND on line ${BASH_LINENO[0]} failed: $?"
exit 1
}
trap onerror ERR

cleanup() {

if [ "$1" != "keep_ctx" ]; then
rm -f $file_primary_key_ctx
fi

rm -f $file_load_key_pub $file_load_key_priv $file_load_key_name $file_load_key_ctx

rm $file_primary_key_ctx $file_load_key_pub $file_load_key_priv $file_load_key_name $file_load_key_ctx $file_load_output -rf
tpm2_evictcontrol -Q -Ao -H $Handle_parent 2>/dev/null || true

}
trap cleanup EXIT

cleanup

tpm2_takeownership -c

tpm2_createprimary -A e -g $alg_primary_obj -G $alg_primary_key -C $file_primary_key_ctx
if [ $? != 0 ];then
fail createprimary
fi
tpm2_create -g $alg_create_obj -G $alg_create_key -u $file_load_key_pub -r $file_load_key_priv -c $file_primary_key_ctx
if [ $? != 0 ];then
fail create
fi
tpm2_createprimary -Q -A e -g $alg_primary_obj -G $alg_primary_key -C $file_primary_key_ctx

tpm2_create -Q -g $alg_create_obj -G $alg_create_key -u $file_load_key_pub -r $file_load_key_priv -c $file_primary_key_ctx

tpm2_load -c $file_primary_key_ctx -u $file_load_key_pub -r $file_load_key_priv -n $file_load_key_name -C $file_load_key_ctx
if [ $? != 0 ];then
fail load
fi
tpm2_load -Q -c $file_primary_key_ctx -u $file_load_key_pub -r $file_load_key_priv -n $file_load_key_name -C $file_load_key_ctx

#####handle test

rm $file_load_key_pub $file_load_key_priv $file_load_key_name $file_load_key_ctx $file_load_output -rf
tpm2_evictcontrol -A o -c $file_primary_key_ctx -S $Handle_parent
if [ $? != 0 ];then
fail evict
fi
tpm2_create -H $Handle_parent -g $alg_create_obj -G $alg_create_key -u $file_load_key_pub -r $file_load_key_priv
if [ $? != 0 ];then
fail create
fi
tpm2_load -H $Handle_parent -u $file_load_key_pub -r $file_load_key_priv -n $file_load_key_name -C $file_load_key_ctx
if [ $? != 0 ];then
fail load
fi
echo "load test OK!"
cleanup keep_ctx

tpm2_evictcontrol -Q -A o -c $file_primary_key_ctx -S $Handle_parent

tpm2_create -Q -H $Handle_parent -g $alg_create_obj -G $alg_create_key -u $file_load_key_pub -r $file_load_key_priv

tpm2_load -Q -H $Handle_parent -u $file_load_key_pub -r $file_load_key_priv -n $file_load_key_name -C $file_load_key_ctx

exit 0