Skip to content

Commit 1428f28

Browse files
author
Andreas Fuchs
committed
Add unit tests for some error cases
Adding a first unit test to some of the error cases and handling in code. This also adds a dependency on cmocka. Signed-off-by: Andreas Fuchs <[email protected]>
1 parent 6cfaeb7 commit 1428f28

8 files changed

+140
-24
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ test-driver
3737
man/*.1
3838
man/*.3
3939
man/*.7
40+
.dirstamp
41+
*~
42+
test/error_tpm2-tss-engine-common
43+
test/*.o

.travis.yml

+11
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,17 @@ addons:
3333
install:
3434
- git clean -xdf
3535
- mkdir -p installdir/usr/local/bin
36+
# CMocka
37+
- wget https://download.01.org/tpm2/cmocka-1.1.1.tar.xz
38+
- sha256sum cmocka-1.1.1.tar.xz | grep -q f02ef48a7039aa77191d525c5b1aee3f13286b77a13615d11bc1148753fc0389 || travis_terminate 1
39+
- tar -Jxvf cmocka-1.1.1.tar.xz
40+
- pushd cmocka-1.1.1
41+
- mkdir build
42+
- cd build
43+
- cmake ../ -DCMAKE_INSTALL_PREFIX=${PWD}/../../installdir/usr/local -DCMAKE_BUILD_TYPE=Release
44+
- make
45+
- make install
46+
- popd
3647
# OpenSSL 1.0.2 / 1.1.0
3748
- git clone --branch $OPENSSL_BRANCH --depth=1 https://github.com/openssl/openssl.git
3849
- pushd openssl

Makefile.am

+25-11
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,31 @@ tpm2tss_genkey_LDADD = $(AM_LDADD) libtpm2tss.la
9797
tpm2tss_genkey_LDFLAGS = $(AM_LDFLAGS) -ltss2-tcti-mssim -ltss2-tcti-device
9898

9999
### Tests ###
100-
TESTS = test/ecdsa.sh \
101-
test/ecdsa-emptyauth.sh \
102-
test/rand.sh \
103-
test/rsadecrypt.sh \
104-
test/rsasign.sh \
105-
test/failload.sh \
106-
test/failwrite.sh \
107-
test/rsasign_parent.sh \
108-
test/rsasign_persistent.sh \
109-
test/rsasign_persistent_emptyauth.sh
110-
EXTRA_DIST += $(TESTS)
100+
TESTS = $(TESTS_SHELL) $(TESTS_UNIT)
101+
#TESTS = $(TESTS_UNIT)
102+
103+
TESTS_SHELL = test/ecdsa.sh \
104+
test/ecdsa-emptyauth.sh \
105+
test/rand.sh \
106+
test/rsadecrypt.sh \
107+
test/rsasign.sh \
108+
test/failload.sh \
109+
test/failwrite.sh \
110+
test/rsasign_parent.sh \
111+
test/rsasign_persistent.sh \
112+
test/rsasign_persistent_emptyauth.sh
113+
EXTRA_DIST += $(TESTS_SHELL)
114+
115+
check_PROGRAMS = $(TESTS_UNIT)
116+
117+
TESTS_UNIT = test/error_tpm2-tss-engine-common
118+
119+
test_error_tpm2_tss_engine_common_CFLAGS = $(AM_CFLAGS) $(CMOCKA_CFLAGS)
120+
test_error_tpm2_tss_engine_common_LDADD = $(AM_LDADD) $(CMOCKA_LIBS)
121+
test_error_tpm2_tss_engine_common_LDFLAGS = $(AM_LDFLAGS) -Wl,--wrap=Esys_Initialize
122+
test_error_tpm2_tss_engine_common_SOURCES = test/error_tpm2-tss-engine-common.c \
123+
$(libtpm2tss_la_SOURCES)
124+
111125

112126
# Adding user and developer information
113127
EXTRA_DIST += \

bootstrap

-10
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,3 @@
33
set -e
44

55
autoreconf --install --sym
6-
7-
if [ "$USER" = "afuchs" ]; then
8-
./configure \
9-
--enable-debug \
10-
PKG_CONFIG_PATH=$PWD/../INSTALLDIR/usr/local/lib/pkgconfig \
11-
CFLAGS=-I$PWD/../INSTALLDIR/usr/local/include \
12-
LDFLAGS=-L$PWD/../INSTALLDIR/usr/local/lib \
13-
# --enable-debug
14-
make -j8
15-
fi

configure.ac

+4
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ AS_IF([test -z "$with_completionsdir"],
140140
[with_completionsdir=$datarootdir/bash-completion/completions])
141141
AC_SUBST(completionsdir, "$with_completionsdir")
142142

143+
m4_define([cmocka_min_version], [1.0])
144+
m4_define([cmocka_err], [Unit test enabled, but cmocka missing or version requirements not met. cmocka version must be >= cmocka_min_version])
145+
PKG_CHECK_MODULES([CMOCKA], [cmocka >= 1.0])
146+
143147
AC_OUTPUT
144148

145149
AC_MSG_RESULT([

test/error_tpm2-tss-engine-common.c

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* SPDX-License-Identifier: BSD-2 */
2+
/*******************************************************************************
3+
* Copyright 2019, Fraunhofer SIT sponsored by Infineon Technologies AG
4+
* All rights reserved.
5+
******************************************************************************/
6+
7+
#include "tpm2-tss-engine.h"
8+
#include "tpm2-tss-engine-common.h"
9+
10+
#include <execinfo.h>
11+
#include <stdio.h>
12+
#include <unistd.h>
13+
#include <setjmp.h>
14+
#include <cmocka.h>
15+
16+
TSS2_RC
17+
__wrap_Esys_Initialize()
18+
{
19+
printf("Esys_Initialize called\n");
20+
void* b[128];
21+
backtrace_symbols_fd(b, backtrace(b, sizeof(b)/sizeof(b[0])), STDOUT_FILENO);
22+
return -1;
23+
}
24+
25+
void
26+
check_tpm2tss_tpm2data_readtpm(void **state)
27+
{
28+
(void)(state);
29+
int i;
30+
i = tpm2tss_tpm2data_readtpm(0, NULL);
31+
assert_int_equal(i, 0);
32+
}
33+
34+
void
35+
check_tpm2tss_tpm2data_read(void **state)
36+
{
37+
(void)(state);
38+
int i;
39+
i = tpm2tss_tpm2data_read("", NULL);
40+
assert_int_equal(i, 0);
41+
}
42+
43+
void
44+
check_init_tpm_parent_via_api(void **state)
45+
{
46+
(void)(state);
47+
int i;
48+
i = tpm2tss_rsa_genkey(NULL, 0, NULL, NULL, 0);
49+
assert_int_equal(i, 0);
50+
}
51+
52+
void
53+
check_init_tpm_parent(void **state)
54+
{
55+
(void)(state);
56+
TSS2_RC r;
57+
ESYS_AUXCONTEXT e;
58+
ESYS_TR t;
59+
r = init_tpm_parent(&e, -1, &t);
60+
assert_int_not_equal(r, TSS2_RC_SUCCESS);
61+
}
62+
63+
void
64+
check_init_tpm_key(void **state)
65+
{
66+
(void)(state);
67+
int i;
68+
TSS2_RC r;
69+
i = tpm2tss_rsa_genkey(NULL, 0, NULL, NULL, 0);
70+
assert_int_equal(i, 0);
71+
72+
ESYS_AUXCONTEXT e;
73+
ESYS_TR t;
74+
TPM2_DATA td = { .privatetype = KEY_TYPE_HANDLE };
75+
r = init_tpm_key(&e, &t, &td);
76+
assert_int_not_equal(r, TSS2_RC_SUCCESS);
77+
//assert_int_equal(1, 0);
78+
}
79+
80+
int
81+
main(void)
82+
{
83+
const struct CMUnitTest tests[] = {
84+
cmocka_unit_test(check_tpm2tss_tpm2data_readtpm),
85+
cmocka_unit_test(check_tpm2tss_tpm2data_read),
86+
cmocka_unit_test(check_init_tpm_parent_via_api),
87+
cmocka_unit_test(check_init_tpm_parent),
88+
cmocka_unit_test(check_init_tpm_key),
89+
};
90+
91+
return cmocka_run_group_tests(tests, NULL, NULL);
92+
}

test/rsasign_parent.sh

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ cat ${DIR}/mykey.pub
3030

3131
echo "abc" | openssl pkeyutl -engine tpm2tss -keyform engine -inkey ${DIR}/mykey -sign -in ${DIR}/mydata.txt -out ${DIR}/mysig -passin stdin
3232

33+
# Release persistent HANDLE
34+
tpm2_evictcontrol -T mssim -a o -c ${HANDLE} -p ${HANDLE}
35+
3336
cat ${DIR}/mysig
3437

3538
#this is a workaround because -verify allways exits 1

test/rsasign_persistent_emptyauth.sh

+1-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ fi
4949
tpm2_readpublic -T mssim -c ${HANDLE} -o ${DIR}/mykey.pem -f pem
5050

5151
# Release persistent HANDLE
52-
tpm2_evictcontrol -T mssim -a o -c ${HANDLE}
53-
54-
tpm2_flushcontext -T mssim -t -l
52+
tpm2_evictcontrol -T mssim -a o -c ${HANDLE} -p ${HANDLE}
5553

5654
R="$(openssl pkeyutl -pubin -inkey ${DIR}/mykey.pem -verify -in ${DIR}/mydata.txt -sigfile ${DIR}/mysig || true)"
5755
if ! echo $R | grep "Signature Verified Successfully" >/dev/null; then

0 commit comments

Comments
 (0)