diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_common.c | 33 | ||||
-rw-r--r-- | tests/test_common.h | 20 | ||||
-rw-r--r-- | tests/tpm_lite/tpmtest_fastenable.c | 7 | ||||
-rw-r--r-- | tests/tpm_lite/tpmtest_globallock.c | 7 | ||||
-rw-r--r-- | tests/tpm_lite/tpmtest_redefine_unowned.c | 3 | ||||
-rw-r--r-- | tests/tpm_lite/tpmtest_spaceperm.c | 5 | ||||
-rw-r--r-- | tests/tpm_lite/tpmtest_writelimit.c | 5 | ||||
-rw-r--r-- | tests/vb2_common_tests.c | 18 |
8 files changed, 89 insertions, 9 deletions
diff --git a/tests/test_common.c b/tests/test_common.c index ddd81052..02cbc165 100644 --- a/tests/test_common.c +++ b/tests/test_common.c @@ -9,10 +9,13 @@ #include <stdio.h> #include <string.h> +#include "2common.h" #include "test_common.h" /* Global test success flag. */ int gTestSuccess = 1; +int gTestAbortArmed = 0; +jmp_buf gTestJmpEnv; int test_eq(int result, int expected, const char *preamble, const char *desc, const char *comment) @@ -173,3 +176,33 @@ int test_false(int result, } return !result; } + +int test_abort(int aborted, + const char *preamble, const char *desc, const char *comment) +{ + if (aborted) { + fprintf(stderr, "%s: %s ... " COL_GREEN "PASSED\n" COL_STOP, + preamble, comment ? comment : desc); + } else { + fprintf(stderr, "%s: %s ... " COL_RED "FAILED\n" COL_STOP, + preamble, comment ? comment : desc); + fprintf(stderr, " Expected ABORT, but did not get it\n"); + gTestSuccess = 0; + } + return aborted; +} + +void vb2ex_abort(void) +{ + /* + * If expecting an abort call, jump back to TEST_ABORT macro. + * Otherwise, force exit to ensure the test fails. + */ + if (gTestAbortArmed) { + longjmp(gTestJmpEnv, 1); + } else { + fprintf(stderr, COL_RED "Unexpected ABORT encountered, " + "exiting\n" COL_STOP); + exit(1); + } +} diff --git a/tests/test_common.h b/tests/test_common.h index 8d98f5cc..17689b71 100644 --- a/tests/test_common.h +++ b/tests/test_common.h @@ -6,6 +6,7 @@ #ifndef VBOOT_REFERENCE_TEST_COMMON_H_ #define VBOOT_REFERENCE_TEST_COMMON_H_ +#include <setjmp.h> #include <stdio.h> /* Used to get a line number as a constant string. Need to stringify it twice */ @@ -13,6 +14,8 @@ #define TOSTRING(x) STRINGIFY(x) extern int gTestSuccess; +extern int gTestAbortArmed; +extern jmp_buf gTestJmpEnv; /* Return 1 if result is equal to expected_result, else return 0. * Also update the global gTestSuccess flag if test fails. */ @@ -117,6 +120,23 @@ int test_succ(int result, #result " == 0", \ comment) +/* Return 1 if vb2ex_abort() was called, else return 0. + * Also update the global gTestSuccess flag if test fails. */ +int test_abort(int aborted, + const char *preamble, const char *desc, const char *comment); + +#define TEST_ABORT(call, comment) do { \ + gTestAbortArmed = 1; \ + int jumped = setjmp(gTestJmpEnv); \ + if (!jumped) \ + call; \ + gTestAbortArmed = 0; \ + test_abort(jumped, \ + __FILE__ ":" TOSTRING(__LINE__), \ + #call " causes abort", \ + comment); \ +} while (0) + /* ANSI Color coding sequences. * * Don't use \e as MSC does not recognize it as a valid escape sequence. diff --git a/tests/tpm_lite/tpmtest_fastenable.c b/tests/tpm_lite/tpmtest_fastenable.c index 72989c1d..a93dfd77 100644 --- a/tests/tpm_lite/tpmtest_fastenable.c +++ b/tests/tpm_lite/tpmtest_fastenable.c @@ -15,6 +15,7 @@ #include <stdio.h> #include "host_common.h" +#include "test_common.h" #include "tlcl.h" #include "tlcl_tests.h" @@ -33,12 +34,14 @@ int main(int argc, char** argv) { TPM_CHECK(TlclForceClear()); TPM_CHECK(TlclGetFlags(&disable, &deactivated, NULL)); printf("disable is %d, deactivated is %d\n", disable, deactivated); - VbAssert(disable == 1 && deactivated == 1); + TEST_EQ(disable, 1, "after ForceClear, disable"); + TEST_EQ(deactivated, 1, "after ForceClear, deactivated"); TPM_CHECK(TlclSetEnable()); TPM_CHECK(TlclSetDeactivated(0)); TPM_CHECK(TlclGetFlags(&disable, &deactivated, NULL)); printf("disable is %d, deactivated is %d\n", disable, deactivated); - VbAssert(disable == 0 && deactivated == 0); + TEST_EQ(disable, 0, "after SetEnable, enabled"); + TEST_EQ(deactivated, 0, "after SetDeactivated(0), activated"); } printf("TEST SUCCEEDED\n"); diff --git a/tests/tpm_lite/tpmtest_globallock.c b/tests/tpm_lite/tpmtest_globallock.c index 2e728524..14f16de0 100644 --- a/tests/tpm_lite/tpmtest_globallock.c +++ b/tests/tpm_lite/tpmtest_globallock.c @@ -11,6 +11,7 @@ #include <stdlib.h> #include "host_common.h" +#include "test_common.h" #include "tlcl.h" #include "tlcl_tests.h" @@ -33,13 +34,13 @@ int main(int argc, char** argv) { TPM_EXPECT(TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x)), TPM_E_AREA_LOCKED); TPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x))); - VbAssert(x == 0); + TEST_EQ(x, 0, "Read from INDEX0 should give 0"); // Verifies that write to index1 is still possible. x = 2; TPM_CHECK(TlclWrite(INDEX1, (uint8_t*) &x, sizeof(x))); TPM_CHECK(TlclRead(INDEX1, (uint8_t*) &x, sizeof(x))); - VbAssert(x == 2); + TEST_EQ(x, 0, "Read from INDEX1 should give 2"); // Turns off PP. TlclLockPhysicalPresence(); @@ -49,7 +50,7 @@ int main(int argc, char** argv) { TPM_EXPECT(TlclWrite(INDEX1, (uint8_t*) &x, sizeof(x)), TPM_E_BAD_PRESENCE); TPM_CHECK(TlclRead(INDEX1, (uint8_t*) &x, sizeof(x))); - VbAssert(x == 2); + TEST_EQ(x, 2, "Read from INDEX1 should give 2"); printf("TEST SUCCEEDED\n"); exit(0); } diff --git a/tests/tpm_lite/tpmtest_redefine_unowned.c b/tests/tpm_lite/tpmtest_redefine_unowned.c index 522d999f..0f89870d 100644 --- a/tests/tpm_lite/tpmtest_redefine_unowned.c +++ b/tests/tpm_lite/tpmtest_redefine_unowned.c @@ -11,6 +11,7 @@ #include <stdlib.h> #include "host_common.h" +#include "test_common.h" #include "tlcl.h" #include "tlcl_tests.h" @@ -23,7 +24,7 @@ int main(int argc, char** argv) { TPM_CHECK(TlclSelfTestFull()); TPM_CHECK(TlclAssertPhysicalPresence()); - VbAssert(!TlclIsOwned()); + TEST_FALSE(TlclIsOwned(), "TlclIsOwned should return false"); /* Ensures spaces exist. */ TPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x))); diff --git a/tests/tpm_lite/tpmtest_spaceperm.c b/tests/tpm_lite/tpmtest_spaceperm.c index c2608e4b..8c9232b2 100644 --- a/tests/tpm_lite/tpmtest_spaceperm.c +++ b/tests/tpm_lite/tpmtest_spaceperm.c @@ -11,6 +11,7 @@ #include <stdlib.h> #include "host_common.h" +#include "test_common.h" #include "tlcl.h" #include "tlcl_tests.h" @@ -26,10 +27,10 @@ int main(int argc, char** argv) { TPM_CHECK(TlclAssertPhysicalPresence()); TPM_CHECK(TlclGetPermissions(INDEX0, &perm)); - VbAssert((perm & PERMPPGL) == PERMPPGL); + TEST_NEQ(perm & PERMPPGL, 0, "INDEX0: PERMPPGL is not set"); TPM_CHECK(TlclGetPermissions(INDEX1, &perm)); - VbAssert((perm & PERMPP) == PERMPP); + TEST_NEQ(perm & PERMPP, 0, "INDEX1: PERMPP is not set"); printf("TEST SUCCEEDED\n"); exit(0); diff --git a/tests/tpm_lite/tpmtest_writelimit.c b/tests/tpm_lite/tpmtest_writelimit.c index cedcd831..75bae9f1 100644 --- a/tests/tpm_lite/tpmtest_writelimit.c +++ b/tests/tpm_lite/tpmtest_writelimit.c @@ -12,6 +12,7 @@ #include "2common.h" #include "host_common.h" +#include "test_common.h" #include "tlcl.h" #include "tlcl_tests.h" @@ -37,7 +38,9 @@ int main(int argc, char** argv) { sizeof(i))) != TPM_SUCCESS) { switch (result) { case TPM_E_MAXNVWRITES: - VbAssert(i >= TPM_MAX_NV_WRITES_NOOWNER); + TEST_TRUE(i >= TPM_MAX_NV_WRITES_NOOWNER, + "MAXNVWRITES should only occur after " + "MAX_NV_WRITES_NOOWNER reached"); break; default: VB2_DEBUG("unexpected error code %d (0x%x)\n", diff --git a/tests/vb2_common_tests.c b/tests/vb2_common_tests.c index 4a3025e9..2661cce9 100644 --- a/tests/vb2_common_tests.c +++ b/tests/vb2_common_tests.c @@ -274,6 +274,23 @@ static void test_helper_functions(void) } } +/* Helper for test_assert_die() below */ +static int _true_assertion_helper(void) +{ + VB2_ASSERT(2 + 2 == 4); + return 1; +} + +/** + * Test VB2_ASSERT and VB2_DIE macros + */ +static void test_assert_die(void) +{ + TEST_ABORT(VB2_DIE("die"), "DIE should abort"); + TEST_ABORT(VB2_ASSERT(2 + 2 == 5), "ASSERT false should abort"); + TEST_TRUE(_true_assertion_helper(), "ASSERT true should continue"); +} + int main(int argc, char* argv[]) { test_arithmetic(); @@ -283,6 +300,7 @@ int main(int argc, char* argv[]) test_align(); test_workbuf(); test_helper_functions(); + test_assert_die(); return gTestSuccess ? 0 : 255; } |