diff options
Diffstat (limited to 'tests')
32 files changed, 797 insertions, 769 deletions
diff --git a/tests/cgptlib_test.c b/tests/cgptlib_test.c index 0336c8e7..a886174d 100644 --- a/tests/cgptlib_test.c +++ b/tests/cgptlib_test.c @@ -1299,7 +1299,7 @@ static void FreeEntry(GptEntry *e) /* Set up an entry. */ static void FillEntry(GptEntry *e, int is_kernel, - int priority, int successful, int tries) + int priority, int successful, int tries) { memcpy(&e->type, (is_kernel ? &guid_kernel : &guid_zero), sizeof(Guid)); SetEntryPriority(e, priority); diff --git a/tests/cgptlib_test.h b/tests/cgptlib_test.h index 8668c9c5..90e76fc8 100644 --- a/tests/cgptlib_test.h +++ b/tests/cgptlib_test.h @@ -9,8 +9,8 @@ #include "sysincludes.h" enum { - TEST_FAIL = -1, - TEST_OK = 0, + TEST_FAIL = -1, + TEST_OK = 0, }; #define TEST_CASE(func) #func, func @@ -18,13 +18,13 @@ typedef int (*test_func)(); #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0])) -#define EXPECT(expr) \ - do { \ - if (!(expr)) { \ - printf(COL_RED " fail " COL_STOP "in expression %s in %s() line %d\n",\ - #expr, __FUNCTION__, __LINE__); \ - return TEST_FAIL; \ - } \ - } while (0) +#define EXPECT(expr) do { \ + if (!(expr)) { \ + printf(COL_RED " fail " COL_STOP \ + "in expression %s in %s() line %d\n", \ + #expr, __FUNCTION__, __LINE__); \ + return TEST_FAIL; \ + } \ +} while (0) -#endif /* VBOOT_REFERENCE_CGPTLIB_TEST_H_ */ +#endif /* VBOOT_REFERENCE_CGPTLIB_TEST_H_ */ diff --git a/tests/crc32_test.c b/tests/crc32_test.c index 01eb5ac6..81bdba98 100644 --- a/tests/crc32_test.c +++ b/tests/crc32_test.c @@ -12,37 +12,37 @@ #define MAX_VECTOR_LEN 256 int TestCrc32TestVectors() { - struct { - uint8_t vector[MAX_VECTOR_LEN]; - int len; - uint32_t crc32; - } cases[] = { - {{0x00}, 1, 0xD202EF8D}, - {{0x00, 0x00, 0x00, 0x00}, 4, 0x2144DF1C}, - {{0x01, 0x01, 0x01, 0x01}, 4, 0xF626D399}, - {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, - 0x00,0x00,0x00,0x28,0x86,0x4d,0x7f,0x99}, 48, 0x923D6EFD}, - {{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, - 0x00,0x00,0x00,0x28,0xc5,0x5e,0x45,0x7a}, 48, 0x49A04D82}, - {{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a, - 0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14, - 0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e, - 0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28, - 0x00,0x00,0x00,0x28,0xbf,0x67,0x1e,0xd0}, 48, 0x688B3BFA}, - }; - int i; + struct { + uint8_t vector[MAX_VECTOR_LEN]; + int len; + uint32_t crc32; + } cases[] = { + {{0x00}, 1, 0xD202EF8D}, + {{0x00, 0x00, 0x00, 0x00}, 4, 0x2144DF1C}, + {{0x01, 0x01, 0x01, 0x01}, 4, 0xF626D399}, + {{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, + 0x00,0x00,0x00,0x28,0x86,0x4d,0x7f,0x99}, 48, 0x923D6EFD}, + {{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, + 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, + 0x00,0x00,0x00,0x28,0xc5,0x5e,0x45,0x7a}, 48, 0x49A04D82}, + {{0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a, + 0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14, + 0x15,0x16,0x17,0x18,0x19,0x1a,0x1b,0x1c,0x1d,0x1e, + 0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28, + 0x00,0x00,0x00,0x28,0xbf,0x67,0x1e,0xd0}, 48, 0x688B3BFA}, + }; + int i; - for (i = 0; i < ARRAY_SIZE(cases); ++i) { - uint32_t crc32; + for (i = 0; i < ARRAY_SIZE(cases); ++i) { + uint32_t crc32; - crc32 = Crc32(cases[i].vector, cases[i].len); - EXPECT(crc32 == cases[i].crc32); - } - return TEST_OK; + crc32 = Crc32(cases[i].vector, cases[i].len); + EXPECT(crc32 == cases[i].crc32); + } + return TEST_OK; } diff --git a/tests/sha_test_vectors.h b/tests/sha_test_vectors.h index 310f5703..9214799e 100644 --- a/tests/sha_test_vectors.h +++ b/tests/sha_test_vectors.h @@ -10,82 +10,82 @@ char* oneblock_msg = "abc"; char* multiblock_msg1 = "abcdbcdecdefdefgefghfghighijhijkijkl" - "jklmklmnlmnomnopnopq"; + "jklmklmnlmnomnopnopq"; char* multiblock_msg2= "abcdefghbcdefghicdefghijdefghijkefghi" - "jklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnop" - "qrsmnopqrstnopqrstu"; + "jklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnop" + "qrsmnopqrstnopqrstu"; char* long_msg; uint8_t sha1_results[][VB2_SHA1_DIGEST_SIZE] = { - { - 0xa9,0x99,0x3e,0x36,0x47,0x06,0x81,0x6a, - 0xba,0x3e,0x25,0x71,0x78,0x50,0xc2,0x6c, - 0x9c,0xd0,0xd8,0x9d - }, - { - 0x84,0x98,0x3e,0x44,0x1c,0x3b,0xd2,0x6e, - 0xba,0xae,0x4a,0xa1,0xf9,0x51,0x29,0xe5, - 0xe5,0x46,0x70,0xf1 - }, - { - 0x34,0xaa,0x97,0x3c,0xd4,0xc4,0xda,0xa4, - 0xf6,0x1e,0xeb,0x2b,0xdb,0xad,0x27,0x31, - 0x65,0x34,0x01,0x6f - } + { + 0xa9,0x99,0x3e,0x36,0x47,0x06,0x81,0x6a, + 0xba,0x3e,0x25,0x71,0x78,0x50,0xc2,0x6c, + 0x9c,0xd0,0xd8,0x9d + }, + { + 0x84,0x98,0x3e,0x44,0x1c,0x3b,0xd2,0x6e, + 0xba,0xae,0x4a,0xa1,0xf9,0x51,0x29,0xe5, + 0xe5,0x46,0x70,0xf1 + }, + { + 0x34,0xaa,0x97,0x3c,0xd4,0xc4,0xda,0xa4, + 0xf6,0x1e,0xeb,0x2b,0xdb,0xad,0x27,0x31, + 0x65,0x34,0x01,0x6f + } }; uint8_t sha256_results[][VB2_SHA256_DIGEST_SIZE] = { - { - 0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea, - 0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23, - 0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c, - 0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad - }, - { - 0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8, - 0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39, - 0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67, - 0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1 - }, - { - 0xcd,0xc7,0x6e,0x5c,0x99,0x14,0xfb,0x92, - 0x81,0xa1,0xc7,0xe2,0x84,0xd7,0x3e,0x67, - 0xf1,0x80,0x9a,0x48,0xa4,0x97,0x20,0x0e, - 0x04,0x6d,0x39,0xcc,0xc7,0x11,0x2c,0xd0 - } + { + 0xba,0x78,0x16,0xbf,0x8f,0x01,0xcf,0xea, + 0x41,0x41,0x40,0xde,0x5d,0xae,0x22,0x23, + 0xb0,0x03,0x61,0xa3,0x96,0x17,0x7a,0x9c, + 0xb4,0x10,0xff,0x61,0xf2,0x00,0x15,0xad + }, + { + 0x24,0x8d,0x6a,0x61,0xd2,0x06,0x38,0xb8, + 0xe5,0xc0,0x26,0x93,0x0c,0x3e,0x60,0x39, + 0xa3,0x3c,0xe4,0x59,0x64,0xff,0x21,0x67, + 0xf6,0xec,0xed,0xd4,0x19,0xdb,0x06,0xc1 + }, + { + 0xcd,0xc7,0x6e,0x5c,0x99,0x14,0xfb,0x92, + 0x81,0xa1,0xc7,0xe2,0x84,0xd7,0x3e,0x67, + 0xf1,0x80,0x9a,0x48,0xa4,0x97,0x20,0x0e, + 0x04,0x6d,0x39,0xcc,0xc7,0x11,0x2c,0xd0 + } }; uint8_t sha512_results[][VB2_SHA512_DIGEST_SIZE] = { - { - 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba, - 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31, - 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2, - 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a, - 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8, - 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd, - 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e, - 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f - }, - { - 0x8e,0x95,0x9b,0x75,0xda,0xe3,0x13,0xda, - 0x8c,0xf4,0xf7,0x28,0x14,0xfc,0x14,0x3f, - 0x8f,0x77,0x79,0xc6,0xeb,0x9f,0x7f,0xa1, - 0x72,0x99,0xae,0xad,0xb6,0x88,0x90,0x18, - 0x50,0x1d,0x28,0x9e,0x49,0x00,0xf7,0xe4, - 0x33,0x1b,0x99,0xde,0xc4,0xb5,0x43,0x3a, - 0xc7,0xd3,0x29,0xee,0xb6,0xdd,0x26,0x54, - 0x5e,0x96,0xe5,0x5b,0x87,0x4b,0xe9,0x09 - }, - { - 0xe7,0x18,0x48,0x3d,0x0c,0xe7,0x69,0x64, - 0x4e,0x2e,0x42,0xc7,0xbc,0x15,0xb4,0x63, - 0x8e,0x1f,0x98,0xb1,0x3b,0x20,0x44,0x28, - 0x56,0x32,0xa8,0x03,0xaf,0xa9,0x73,0xeb, - 0xde,0x0f,0xf2,0x44,0x87,0x7e,0xa6,0x0a, - 0x4c,0xb0,0x43,0x2c,0xe5,0x77,0xc3,0x1b, - 0xeb,0x00,0x9c,0x5c,0x2c,0x49,0xaa,0x2e, - 0x4e,0xad,0xb2,0x17,0xad,0x8c,0xc0,0x9b - } + { + 0xdd,0xaf,0x35,0xa1,0x93,0x61,0x7a,0xba, + 0xcc,0x41,0x73,0x49,0xae,0x20,0x41,0x31, + 0x12,0xe6,0xfa,0x4e,0x89,0xa9,0x7e,0xa2, + 0x0a,0x9e,0xee,0xe6,0x4b,0x55,0xd3,0x9a, + 0x21,0x92,0x99,0x2a,0x27,0x4f,0xc1,0xa8, + 0x36,0xba,0x3c,0x23,0xa3,0xfe,0xeb,0xbd, + 0x45,0x4d,0x44,0x23,0x64,0x3c,0xe8,0x0e, + 0x2a,0x9a,0xc9,0x4f,0xa5,0x4c,0xa4,0x9f + }, + { + 0x8e,0x95,0x9b,0x75,0xda,0xe3,0x13,0xda, + 0x8c,0xf4,0xf7,0x28,0x14,0xfc,0x14,0x3f, + 0x8f,0x77,0x79,0xc6,0xeb,0x9f,0x7f,0xa1, + 0x72,0x99,0xae,0xad,0xb6,0x88,0x90,0x18, + 0x50,0x1d,0x28,0x9e,0x49,0x00,0xf7,0xe4, + 0x33,0x1b,0x99,0xde,0xc4,0xb5,0x43,0x3a, + 0xc7,0xd3,0x29,0xee,0xb6,0xdd,0x26,0x54, + 0x5e,0x96,0xe5,0x5b,0x87,0x4b,0xe9,0x09 + }, + { + 0xe7,0x18,0x48,0x3d,0x0c,0xe7,0x69,0x64, + 0x4e,0x2e,0x42,0xc7,0xbc,0x15,0xb4,0x63, + 0x8e,0x1f,0x98,0xb1,0x3b,0x20,0x44,0x28, + 0x56,0x32,0xa8,0x03,0xaf,0xa9,0x73,0xeb, + 0xde,0x0f,0xf2,0x44,0x87,0x7e,0xa6,0x0a, + 0x4c,0xb0,0x43,0x2c,0xe5,0x77,0xc3,0x1b, + 0xeb,0x00,0x9c,0x5c,0x2c,0x49,0xaa,0x2e, + 0x4e,0xad,0xb2,0x17,0xad,0x8c,0xc0,0x9b + } }; -#endif /* VBOOT_REFERENCE_SHA_TEST_VECTORS_H_ */ +#endif /* VBOOT_REFERENCE_SHA_TEST_VECTORS_H_ */ diff --git a/tests/sha_tests.c b/tests/sha_tests.c index 40d3749f..1d78204a 100644 --- a/tests/sha_tests.c +++ b/tests/sha_tests.c @@ -13,86 +13,95 @@ #include "sha_test_vectors.h" int SHA1_tests(void) { - int i, success = 1; - uint8_t sha1_digest[SHA1_DIGEST_SIZE]; - uint8_t* test_inputs[3]; - test_inputs[0] = (uint8_t *) oneblock_msg; - test_inputs[1] = (uint8_t *) multiblock_msg1; - test_inputs[2] = (uint8_t *) long_msg; + int i, success = 1; + uint8_t sha1_digest[SHA1_DIGEST_SIZE]; + uint8_t* test_inputs[3]; + test_inputs[0] = (uint8_t *) oneblock_msg; + test_inputs[1] = (uint8_t *) multiblock_msg1; + test_inputs[2] = (uint8_t *) long_msg; - for (i = 0; i < 3; i++) { - internal_SHA1(test_inputs[i], strlen((char *)test_inputs[i]), - sha1_digest); - if (!memcmp(sha1_digest, sha1_results[i], SHA1_DIGEST_SIZE)) { - fprintf(stderr, "Test vector %d PASSED for SHA-1\n", i+1); - } - else { - fprintf(stderr, "Test vector %d FAILED for SHA-1\n", i+1); - success = 0; - } - } - return success; + for (i = 0; i < 3; i++) { + internal_SHA1(test_inputs[i], strlen((char *)test_inputs[i]), + sha1_digest); + if (!memcmp(sha1_digest, sha1_results[i], + SHA1_DIGEST_SIZE)) { + fprintf(stderr, "Test vector %d PASSED for SHA-1\n", + i+1); + } + else { + fprintf(stderr, "Test vector %d FAILED for SHA-1\n", + i+1); + success = 0; + } + } + return success; } int SHA256_tests(void) { - int i, success = 1; - uint8_t sha256_digest[SHA256_DIGEST_SIZE]; - uint8_t* test_inputs[3]; - test_inputs[0] = (uint8_t *) oneblock_msg; - test_inputs[1] = (uint8_t *) multiblock_msg1; - test_inputs[2] = (uint8_t *) long_msg; + int i, success = 1; + uint8_t sha256_digest[SHA256_DIGEST_SIZE]; + uint8_t* test_inputs[3]; + test_inputs[0] = (uint8_t *) oneblock_msg; + test_inputs[1] = (uint8_t *) multiblock_msg1; + test_inputs[2] = (uint8_t *) long_msg; - for (i = 0; i < 3; i++) { - internal_SHA256(test_inputs[i], strlen((char *)test_inputs[i]), - sha256_digest); - if (!memcmp(sha256_digest, sha256_results[i], SHA256_DIGEST_SIZE)) { - fprintf(stderr, "Test vector %d PASSED for SHA-256\n", i+1); - } - else { - fprintf(stderr, "Test vector %d FAILED for SHA-256\n", i+1); - success = 0; - } - } - return success; + for (i = 0; i < 3; i++) { + internal_SHA256(test_inputs[i], strlen((char *)test_inputs[i]), + sha256_digest); + if (!memcmp(sha256_digest, sha256_results[i], + SHA256_DIGEST_SIZE)) { + fprintf(stderr, "Test vector %d PASSED for SHA-256\n", + i+1); + } + else { + fprintf(stderr, "Test vector %d FAILED for SHA-256\n", + i+1); + success = 0; + } + } + return success; } int SHA512_tests(void) { - int i, success = 1; - uint8_t sha512_digest[SHA512_DIGEST_SIZE]; - uint8_t* test_inputs[3]; - test_inputs[0] = (uint8_t *) oneblock_msg; - test_inputs[1] = (uint8_t *) multiblock_msg2; - test_inputs[2] = (uint8_t *) long_msg; + int i, success = 1; + uint8_t sha512_digest[SHA512_DIGEST_SIZE]; + uint8_t* test_inputs[3]; + test_inputs[0] = (uint8_t *) oneblock_msg; + test_inputs[1] = (uint8_t *) multiblock_msg2; + test_inputs[2] = (uint8_t *) long_msg; - for (i = 0; i < 3; i++) { - internal_SHA512(test_inputs[i], strlen((char *)test_inputs[i]), - sha512_digest); - if (!memcmp(sha512_digest, sha512_results[i], SHA512_DIGEST_SIZE)) { - fprintf(stderr, "Test vector %d PASSED for SHA-512\n", i+1); - } - else { - fprintf(stderr, "Test vector %d FAILED for SHA-512\n", i+1); - success = 0; - } - } - return success; + for (i = 0; i < 3; i++) { + internal_SHA512(test_inputs[i], strlen((char *)test_inputs[i]), + sha512_digest); + if (!memcmp(sha512_digest, sha512_results[i], + SHA512_DIGEST_SIZE)) { + fprintf(stderr, "Test vector %d PASSED for SHA-512\n", + i+1); + } + else { + fprintf(stderr, "Test vector %d FAILED for SHA-512\n", + i+1); + success = 0; + } + } + return success; } int main(int argc, char* argv[]) { - int success = 1; - /* Initialize long_msg with 'a' x 1,000,000 */ - long_msg = (char *) malloc(1000001); - memset(long_msg, 'a', 1000000); - long_msg[1000000]=0; + int success = 1; + /* Initialize long_msg with 'a' x 1,000,000 */ + long_msg = (char *) malloc(1000001); + memset(long_msg, 'a', 1000000); + long_msg[1000000]=0; - if (!SHA1_tests()) - success = 0; - if (!SHA256_tests()) - success = 0; - if (!SHA512_tests()) - success = 0; + if (!SHA1_tests()) + success = 0; + if (!SHA256_tests()) + success = 0; + if (!SHA512_tests()) + success = 0; - free(long_msg); + free(long_msg); - return !success; + return !success; } diff --git a/tests/test_common.c b/tests/test_common.c index 972a3ffe..ddd81052 100644 --- a/tests/test_common.c +++ b/tests/test_common.c @@ -17,156 +17,159 @@ int gTestSuccess = 1; int test_eq(int result, int expected, const char *preamble, const char *desc, const char *comment) { - if (result == expected) { - fprintf(stderr, "%s: %s ... " COL_GREEN "PASSED\n" COL_STOP, - preamble, comment ? comment : desc); - return 1; - } else { - fprintf(stderr, "%s: %s ... " COL_RED "FAILED\n" COL_STOP, - preamble, comment ? comment : desc); - fprintf(stderr, " Expected: 0x%x (%d), got: 0x%x (%d)\n", - expected, expected, result, result); - gTestSuccess = 0; - return 0; - } + if (result == expected) { + fprintf(stderr, "%s: %s ... " COL_GREEN "PASSED\n" COL_STOP, + preamble, comment ? comment : desc); + return 1; + } else { + fprintf(stderr, "%s: %s ... " COL_RED "FAILED\n" COL_STOP, + preamble, comment ? comment : desc); + fprintf(stderr, " Expected: 0x%x (%d), got: 0x%x (%d)\n", + expected, expected, result, result); + gTestSuccess = 0; + return 0; + } } int test_neq(int result, int not_expected, const char *preamble, const char *desc, const char *comment) { - if (result != not_expected) { - fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, - preamble, desc, comment); - return 1; - } else { - fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, - preamble, desc, comment); - fprintf(stderr, " Didn't expect 0x%x (%d), but got it.\n", - not_expected, not_expected); - gTestSuccess = 0; - return 0; - } + if (result != not_expected) { + fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, + preamble, desc, comment); + return 1; + } else { + fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, + preamble, desc, comment); + fprintf(stderr, " Didn't expect 0x%x (%d), but got it.\n", + not_expected, not_expected); + gTestSuccess = 0; + return 0; + } } int test_ptr_eq(const void* result, const void* expected, const char *preamble, const char *desc, const char *comment) { - if (result == expected) { - fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, - preamble, desc, comment); - return 1; - } else { - fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, - preamble, desc, comment); - fprintf(stderr, " Expected: 0x%lx, got: 0x%lx\n", (long)expected, - (long)result); - gTestSuccess = 0; - return 0; - } + if (result == expected) { + fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, + preamble, desc, comment); + return 1; + } else { + fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, + preamble, desc, comment); + fprintf(stderr, " Expected: 0x%lx, got: 0x%lx\n", + (long)expected, (long)result); + gTestSuccess = 0; + return 0; + } } int test_ptr_neq(const void* result, const void* not_expected, const char *preamble, const char *desc, const char *comment) { - if (result != not_expected) { - fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, - preamble, desc, comment); - return 1; - } else { - fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, - preamble, desc, comment); - fprintf(stderr, " Didn't expect 0x%lx, but got it\n", - (long)not_expected); - gTestSuccess = 0; - return 0; - } + if (result != not_expected) { + fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, + preamble, desc, comment); + return 1; + } else { + fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, + preamble, desc, comment); + fprintf(stderr, " Didn't expect 0x%lx, but got it\n", + (long)not_expected); + gTestSuccess = 0; + return 0; + } } int test_str_eq(const char* result, const char* expected, const char *preamble, const char *desc, const char *comment) { - if (!result || !expected) { - fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, - preamble, desc, comment); - fprintf(stderr, " String compare with NULL\n"); - gTestSuccess = 0; - return 0; - } else if (!strcmp(result, expected)) { - fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, - preamble, desc, comment); - return 1; - } else { - fprintf(stderr, "%s " COL_RED "FAILED\n" COL_STOP, comment); - fprintf(stderr, " Expected: \"%s\", got: \"%s\"\n", expected, - result); - gTestSuccess = 0; - return 0; - } + if (!result || !expected) { + fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, + preamble, desc, comment); + fprintf(stderr, " String compare with NULL\n"); + gTestSuccess = 0; + return 0; + } else if (!strcmp(result, expected)) { + fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, + preamble, desc, comment); + return 1; + } else { + fprintf(stderr, "%s " COL_RED "FAILED\n" COL_STOP, comment); + fprintf(stderr, " Expected: \"%s\", got: \"%s\"\n", + expected, result); + gTestSuccess = 0; + return 0; + } } int test_str_neq(const char* result, const char* not_expected, const char *preamble, const char *desc, const char *comment) { - if (!result || !not_expected) { - fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, - preamble, desc, comment); - fprintf(stderr, " String compare with NULL\n"); - gTestSuccess = 0; - return 0; - } else if (strcmp(result, not_expected)) { - fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, - preamble, desc, comment); - return 1; - } else { - fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, - preamble, desc, comment); - fprintf(stderr, " Didn't expect: \"%s\", but got it\n", not_expected); - gTestSuccess = 0; - return 0; - } + if (!result || !not_expected) { + fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, + preamble, desc, comment); + fprintf(stderr, " String compare with NULL\n"); + gTestSuccess = 0; + return 0; + } else if (strcmp(result, not_expected)) { + fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, + preamble, desc, comment); + return 1; + } else { + fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, + preamble, desc, comment); + fprintf(stderr, " Didn't expect: \"%s\", but got it\n", + not_expected); + gTestSuccess = 0; + return 0; + } } int test_succ(int result, const char *preamble, const char *desc, const char *comment) { - if (result == 0) { - 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 SUCCESS, got: 0x%x (%d)\n", result, result); - gTestSuccess = 0; - } - return !result; + if (result == 0) { + 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 SUCCESS, got: 0x%x (%d)\n", + result, result); + gTestSuccess = 0; + } + return !result; } int test_true(int result, const char *preamble, const char *desc, const char *comment) { - if (result) { - fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, - preamble, desc, comment); - } else { - fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, - preamble, desc, comment); - fprintf(stderr, " Expected TRUE, got 0\n"); - gTestSuccess = 0; - } - return result; + if (result) { + fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, + preamble, desc, comment); + } else { + fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, + preamble, desc, comment); + fprintf(stderr, " Expected TRUE, got 0\n"); + gTestSuccess = 0; + } + return result; } int test_false(int result, const char *preamble, const char *desc, const char *comment) { - if (!result) { - fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, - preamble, desc, comment); - } else { - fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, - preamble, desc, comment); - fprintf(stderr, " Expected FALSE, got: 0x%lx\n", (long)result); - gTestSuccess = 0; - } - return !result; + if (!result) { + fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP, + preamble, desc, comment); + } else { + fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP, + preamble, desc, comment); + fprintf(stderr, " Expected FALSE, got: 0x%lx\n", + (long)result); + gTestSuccess = 0; + } + return !result; } diff --git a/tests/timer_utils.c b/tests/timer_utils.c index 015f6db3..25e7503c 100644 --- a/tests/timer_utils.c +++ b/tests/timer_utils.c @@ -6,19 +6,19 @@ #include "timer_utils.h" void StartTimer(ClockTimerState* ct) { - clock_gettime(CLOCK_REALTIME, &ct->start_time); + clock_gettime(CLOCK_REALTIME, &ct->start_time); } void StopTimer(ClockTimerState* ct) { - clock_gettime(CLOCK_REALTIME, &ct->end_time); + clock_gettime(CLOCK_REALTIME, &ct->end_time); } uint32_t GetDurationMsecs(ClockTimerState* ct) { - uint64_t start = ((uint64_t) ct->start_time.tv_sec * 1000000000 + - (uint64_t) ct->start_time.tv_nsec); - uint64_t end = ((uint64_t) ct->end_time.tv_sec * 1000000000 + - (uint64_t) ct->end_time.tv_nsec); - uint64_t duration_msecs = (end - start) / 1000000U; /* Nanoseconds -> - * Milliseconds. */ - return (uint32_t) duration_msecs; + uint64_t start = ((uint64_t) ct->start_time.tv_sec * 1000000000 + + (uint64_t) ct->start_time.tv_nsec); + uint64_t end = ((uint64_t) ct->end_time.tv_sec * 1000000000 + + (uint64_t) ct->end_time.tv_nsec); + uint64_t duration_msecs = (end - start) / 1000000U; /* Nanoseconds -> + * Milliseconds. */ + return (uint32_t) duration_msecs; } diff --git a/tests/timer_utils.h b/tests/timer_utils.h index 69380722..78099807 100644 --- a/tests/timer_utils.h +++ b/tests/timer_utils.h @@ -11,8 +11,8 @@ #include <time.h> typedef struct ClockTimer { - struct timespec start_time; - struct timespec end_time; + struct timespec start_time; + struct timespec end_time; } ClockTimerState; /* Start timer and update [ct]. */ diff --git a/tests/tlcl_tests.c b/tests/tlcl_tests.c index 55e897cb..9d67671e 100644 --- a/tests/tlcl_tests.c +++ b/tests/tlcl_tests.c @@ -77,7 +77,7 @@ VbError_t VbExTpmClose(void) } VbError_t VbExTpmSendReceive(const uint8_t *request, uint32_t request_length, - uint8_t *response, uint32_t *response_length) + uint8_t *response, uint32_t *response_length) { struct srcall *c = calls + ncalls++; diff --git a/tests/tpm_lite/lock.c b/tests/tpm_lite/lock.c index 2fd21b7e..bfc10e2e 100644 --- a/tests/tpm_lite/lock.c +++ b/tests/tpm_lite/lock.c @@ -15,15 +15,15 @@ int main(int argc, char** argv) { - TlclLibInit(); + TlclLibInit(); - TlclStartup(); - TlclSelfTestFull(); + TlclStartup(); + TlclSelfTestFull(); - TlclAssertPhysicalPresence(); + TlclAssertPhysicalPresence(); - TlclWriteLock(INDEX0); + TlclWriteLock(INDEX0); - printf("Locked 0x%x\n", INDEX0); - exit(0); + printf("Locked 0x%x\n", INDEX0); + exit(0); } diff --git a/tests/tpm_lite/readonly.c b/tests/tpm_lite/readonly.c index 97b90917..c3940e12 100644 --- a/tests/tpm_lite/readonly.c +++ b/tests/tpm_lite/readonly.c @@ -34,76 +34,82 @@ * of making this FUBAR). */ void InitializeSpaces(void) { - uint32_t zero = 0; - uint32_t perm = TPM_NV_PER_WRITE_STCLEAR | TPM_NV_PER_PPWRITE; - - printf("Initializing spaces\n"); - TlclSetNvLocked(); /* useful only the first time */ - - TlclDefineSpace(INDEX0, perm, 4); - TlclWrite(INDEX0, (uint8_t *) &zero, 4); - TlclDefineSpace(INDEX1, perm, 4); - TlclWrite(INDEX1, (uint8_t *) &zero, 4); - TlclDefineSpace(INDEX2, perm, 4); - TlclWrite(INDEX2, (uint8_t *) &zero, 4); - TlclDefineSpace(INDEX3, perm, 4); - TlclWrite(INDEX3, (uint8_t *) &zero, 4); - - perm = TPM_NV_PER_READ_STCLEAR | TPM_NV_PER_WRITE_STCLEAR | - TPM_NV_PER_PPWRITE; - TlclDefineSpace(INDEX_INITIALIZED, perm, 1); + uint32_t zero = 0; + uint32_t perm = TPM_NV_PER_WRITE_STCLEAR | TPM_NV_PER_PPWRITE; + + printf("Initializing spaces\n"); + TlclSetNvLocked(); /* useful only the first time */ + + TlclDefineSpace(INDEX0, perm, 4); + TlclWrite(INDEX0, (uint8_t *) &zero, 4); + TlclDefineSpace(INDEX1, perm, 4); + TlclWrite(INDEX1, (uint8_t *) &zero, 4); + TlclDefineSpace(INDEX2, perm, 4); + TlclWrite(INDEX2, (uint8_t *) &zero, 4); + TlclDefineSpace(INDEX3, perm, 4); + TlclWrite(INDEX3, (uint8_t *) &zero, 4); + + perm = (TPM_NV_PER_READ_STCLEAR | TPM_NV_PER_WRITE_STCLEAR | + TPM_NV_PER_PPWRITE); + TlclDefineSpace(INDEX_INITIALIZED, perm, 1); } void EnterRecoveryMode(void) { - printf("entering recovery mode"); - exit(0); + printf("entering recovery mode"); + exit(0); } int main(int argc, char** argv) { - uint8_t c; - uint32_t index_0, index_1, index_2, index_3; - - TlclLibInit(); - - TlclStartup(); - TlclSelfTestFull(); - - TlclAssertPhysicalPresence(); - - /* Checks if initialization has completed by trying to read-lock a space - * that's created at the end of initialization. - */ - if (TlclRead(INDEX_INITIALIZED, &c, 0) == TPM_E_BADINDEX) { - /* The initialization did not complete. - */ - InitializeSpaces(); - } - - /* Checks if spaces are OK or messed up. - */ - if (TlclRead(INDEX0, (uint8_t*) &index_0, sizeof(index_0)) != TPM_SUCCESS || - TlclRead(INDEX1, (uint8_t*) &index_1, sizeof(index_1)) != TPM_SUCCESS || - TlclRead(INDEX2, (uint8_t*) &index_2, sizeof(index_2)) != TPM_SUCCESS || - TlclRead(INDEX3, (uint8_t*) &index_3, sizeof(index_3)) != TPM_SUCCESS) { - EnterRecoveryMode(); - } - - /* Writes space, and locks it. Then attempts to write again. I really wish - * I could use the imperative. - */ - index_0 += 1; - if (TlclWrite(INDEX0, (uint8_t*) &index_0, sizeof(index_0) != TPM_SUCCESS)) { - error("could not write index 0\n"); - } - TlclWriteLock(INDEX0); - if (TlclWrite(INDEX0, (uint8_t*) &index_0, sizeof(index_0)) == TPM_SUCCESS) { - error("index 0 is not locked\n"); - } - - /* Done for now. - */ - printf("TEST SUCCEEDED\n"); - exit(0); + uint8_t c; + uint32_t index_0, index_1, index_2, index_3; + + TlclLibInit(); + + TlclStartup(); + TlclSelfTestFull(); + + TlclAssertPhysicalPresence(); + + /* Checks if initialization has completed by trying to read-lock a space + * that's created at the end of initialization. + */ + if (TlclRead(INDEX_INITIALIZED, &c, 0) == TPM_E_BADINDEX) { + /* The initialization did not complete. + */ + InitializeSpaces(); + } + + /* Checks if spaces are OK or messed up. + */ + if (TlclRead(INDEX0, (uint8_t*) &index_0, + sizeof(index_0)) != TPM_SUCCESS || + TlclRead(INDEX1, (uint8_t*) &index_1, + sizeof(index_1)) != TPM_SUCCESS || + TlclRead(INDEX2, (uint8_t*) &index_2, + sizeof(index_2)) != TPM_SUCCESS || + TlclRead(INDEX3, (uint8_t*) &index_3, + sizeof(index_3)) != TPM_SUCCESS) { + EnterRecoveryMode(); + } + + /* Writes space, and locks it. Then attempts to write again. I really wish + * I could use the imperative. + */ + index_0 += 1; + if (TlclWrite(INDEX0, (uint8_t*) &index_0, + sizeof(index_0) != TPM_SUCCESS)) { + error("could not write index 0\n"); + } + TlclWriteLock(INDEX0); + if (TlclWrite(INDEX0, (uint8_t*) &index_0, + sizeof(index_0)) == TPM_SUCCESS) { + error("index 0 is not locked\n"); + } + + /* Done for now. + */ + printf("TEST SUCCEEDED\n"); + exit(0); } diff --git a/tests/tpm_lite/startup.c b/tests/tpm_lite/startup.c index 532ba8dd..3d901f39 100644 --- a/tests/tpm_lite/startup.c +++ b/tests/tpm_lite/startup.c @@ -11,22 +11,22 @@ #include "tlcl.h" int main(int argc, char** argv) { - uint32_t result; - TlclLibInit(); - result = TlclStartup(); - if (result != 0) { - printf("tpm startup failed with 0x%x\n", result); - } - result = TlclGetFlags(NULL, NULL, NULL); - if (result != 0) { - printf("tpm getflags failed with 0x%x\n", result); - } - printf("executing SelfTestFull\n"); - TlclSelfTestFull(); - result = TlclGetFlags(NULL, NULL, NULL); - if (result != 0) { - printf("tpm getflags failed with 0x%x\n", result); - } - printf("TEST SUCCEEDED\n"); - return 0; + uint32_t result; + TlclLibInit(); + result = TlclStartup(); + if (result != 0) { + printf("tpm startup failed with 0x%x\n", result); + } + result = TlclGetFlags(NULL, NULL, NULL); + if (result != 0) { + printf("tpm getflags failed with 0x%x\n", result); + } + printf("executing SelfTestFull\n"); + TlclSelfTestFull(); + result = TlclGetFlags(NULL, NULL, NULL); + if (result != 0) { + printf("tpm getflags failed with 0x%x\n", result); + } + printf("TEST SUCCEEDED\n"); + return 0; } diff --git a/tests/tpm_lite/tlcl_tests.c b/tests/tpm_lite/tlcl_tests.c index f36021f9..c164f4fe 100644 --- a/tests/tpm_lite/tlcl_tests.c +++ b/tests/tpm_lite/tlcl_tests.c @@ -14,16 +14,16 @@ const char* resilient_startup = NULL; uint32_t TlclStartupIfNeeded(void) { - static char* null_getenv = "some string"; /* just a unique address */ - uint32_t result = TlclStartup(); - if (resilient_startup == NULL) { - resilient_startup = getenv("TLCL_RESILIENT_STARTUP"); - if (resilient_startup == NULL) { - resilient_startup = null_getenv; - } - } - if (resilient_startup == null_getenv) { - return result; - } - return result == TPM_E_INVALID_POSTINIT ? TPM_SUCCESS : result; + static char* null_getenv = "some string"; /* just a unique address */ + uint32_t result = TlclStartup(); + if (resilient_startup == NULL) { + resilient_startup = getenv("TLCL_RESILIENT_STARTUP"); + if (resilient_startup == NULL) { + resilient_startup = null_getenv; + } + } + if (resilient_startup == null_getenv) { + return result; + } + return result == TPM_E_INVALID_POSTINIT ? TPM_SUCCESS : result; } diff --git a/tests/tpm_lite/tlcl_tests.h b/tests/tpm_lite/tlcl_tests.h index 5e23ee39..2b6bd6b8 100644 --- a/tests/tpm_lite/tlcl_tests.h +++ b/tests/tpm_lite/tlcl_tests.h @@ -16,14 +16,14 @@ /* Prints error and returns on failure */ #define TPM_CHECK(tpm_command) TPM_EXPECT(tpm_command, TPM_SUCCESS) -#define TPM_EXPECT(tpm_command, expected_result) do { \ - uint32_t _result = (tpm_command); \ - uint32_t _exp = (expected_result); \ - if (_result != _exp) { \ - printf("TEST FAILED: line %d: " #tpm_command ": 0x%x" \ - " (expecting 0x%x)\n", __LINE__, _result, _exp); \ - return _result; \ - } \ +#define TPM_EXPECT(tpm_command, expected_result) do { \ + uint32_t _result = (tpm_command); \ + uint32_t _exp = (expected_result); \ + if (_result != _exp) { \ + printf("TEST FAILED: line %d: " #tpm_command ": 0x%x" \ + " (expecting 0x%x)\n", __LINE__, _result, _exp); \ + return _result; \ + } \ } while (0) diff --git a/tests/tpm_lite/tpmtest_earlyextend.c b/tests/tpm_lite/tpmtest_earlyextend.c index 837960c1..8bab8afd 100644 --- a/tests/tpm_lite/tpmtest_earlyextend.c +++ b/tests/tpm_lite/tpmtest_earlyextend.c @@ -14,13 +14,13 @@ #include "tlcl_tests.h" int main(int argc, char** argv) { - uint8_t value_in[20]; - uint8_t value_out[20]; + uint8_t value_in[20]; + uint8_t value_out[20]; - TlclLibInit(); - TPM_CHECK(TlclStartup()); - TPM_CHECK(TlclContinueSelfTest()); - TPM_CHECK(TlclExtend(1, value_in, value_out)); - printf("TEST SUCCEEDED\n"); - exit(0); + TlclLibInit(); + TPM_CHECK(TlclStartup()); + TPM_CHECK(TlclContinueSelfTest()); + TPM_CHECK(TlclExtend(1, value_in, value_out)); + printf("TEST SUCCEEDED\n"); + exit(0); } diff --git a/tests/tpm_lite/tpmtest_earlynvram.c b/tests/tpm_lite/tpmtest_earlynvram.c index fa458338..d5ff77f5 100644 --- a/tests/tpm_lite/tpmtest_earlynvram.c +++ b/tests/tpm_lite/tpmtest_earlynvram.c @@ -17,13 +17,13 @@ #define INDEX0 0xcafe int main(int argc, char** argv) { - uint32_t x; + uint32_t x; - TlclLibInit(); - TPM_CHECK(TlclStartup()); - TPM_CHECK(TlclContinueSelfTest()); - TPM_CHECK(TlclAssertPhysicalPresence()); - TPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x))); - printf("TEST SUCCEEDED\n"); - return 0; + TlclLibInit(); + TPM_CHECK(TlclStartup()); + TPM_CHECK(TlclContinueSelfTest()); + TPM_CHECK(TlclAssertPhysicalPresence()); + TPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x))); + printf("TEST SUCCEEDED\n"); + return 0; } diff --git a/tests/tpm_lite/tpmtest_earlynvram2.c b/tests/tpm_lite/tpmtest_earlynvram2.c index 2bd2d712..88f6a6b4 100644 --- a/tests/tpm_lite/tpmtest_earlynvram2.c +++ b/tests/tpm_lite/tpmtest_earlynvram2.c @@ -15,13 +15,13 @@ #include "utility.h" int main(int argc, char** argv) { - uint32_t x; + uint32_t x; - TlclLibInit(); - TPM_CHECK(TlclStartup()); - TPM_CHECK(TlclContinueSelfTest()); - TPM_CHECK(TlclAssertPhysicalPresence()); - TPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x))); - printf("TEST SUCCEEDED\n"); - return 0; + TlclLibInit(); + TPM_CHECK(TlclStartup()); + TPM_CHECK(TlclContinueSelfTest()); + TPM_CHECK(TlclAssertPhysicalPresence()); + TPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x))); + printf("TEST SUCCEEDED\n"); + return 0; } diff --git a/tests/tpm_lite/tpmtest_enable.c b/tests/tpm_lite/tpmtest_enable.c index 9163ca17..645cd467 100644 --- a/tests/tpm_lite/tpmtest_enable.c +++ b/tests/tpm_lite/tpmtest_enable.c @@ -14,21 +14,21 @@ int main(int argc, char** argv) { - uint8_t disable, deactivated; + uint8_t disable, deactivated; - TlclLibInit(); - TPM_CHECK(TlclStartupIfNeeded()); - TPM_CHECK(TlclSelfTestFull()); - TPM_CHECK(TlclAssertPhysicalPresence()); - TPM_CHECK(TlclGetFlags(&disable, &deactivated, NULL)); - printf("disable is %d, deactivated is %d\n", disable, deactivated); - TPM_CHECK(TlclSetEnable()); - TPM_CHECK(TlclSetDeactivated(0)); - TPM_CHECK(TlclGetFlags(&disable, &deactivated, NULL)); - printf("disable is %d, deactivated is %d\n", disable, deactivated); - if (disable == 1 || deactivated == 1) { - VbExError("failed to enable or activate"); - } - printf("TEST SUCCEEDED\n"); - return 0; + TlclLibInit(); + TPM_CHECK(TlclStartupIfNeeded()); + TPM_CHECK(TlclSelfTestFull()); + TPM_CHECK(TlclAssertPhysicalPresence()); + TPM_CHECK(TlclGetFlags(&disable, &deactivated, NULL)); + printf("disable is %d, deactivated is %d\n", disable, deactivated); + TPM_CHECK(TlclSetEnable()); + TPM_CHECK(TlclSetDeactivated(0)); + TPM_CHECK(TlclGetFlags(&disable, &deactivated, NULL)); + printf("disable is %d, deactivated is %d\n", disable, deactivated); + if (disable == 1 || deactivated == 1) { + VbExError("failed to enable or activate"); + } + printf("TEST SUCCEEDED\n"); + return 0; } diff --git a/tests/tpm_lite/tpmtest_fastenable.c b/tests/tpm_lite/tpmtest_fastenable.c index 821658ca..72989c1d 100644 --- a/tests/tpm_lite/tpmtest_fastenable.c +++ b/tests/tpm_lite/tpmtest_fastenable.c @@ -19,28 +19,28 @@ #include "tlcl_tests.h" int main(int argc, char** argv) { - uint8_t disable, deactivated; - int i; + uint8_t disable, deactivated; + int i; - TlclLibInit(); - TPM_CHECK(TlclStartupIfNeeded()); - TPM_CHECK(TlclSelfTestFull()); - TPM_CHECK(TlclAssertPhysicalPresence()); - TPM_CHECK(TlclGetFlags(&disable, &deactivated, NULL)); - printf("disable is %d, deactivated is %d\n", disable, deactivated); + TlclLibInit(); + TPM_CHECK(TlclStartupIfNeeded()); + TPM_CHECK(TlclSelfTestFull()); + TPM_CHECK(TlclAssertPhysicalPresence()); + TPM_CHECK(TlclGetFlags(&disable, &deactivated, NULL)); + printf("disable is %d, deactivated is %d\n", disable, deactivated); - for (i = 0; i < 2; i++) { - TPM_CHECK(TlclForceClear()); - TPM_CHECK(TlclGetFlags(&disable, &deactivated, NULL)); - printf("disable is %d, deactivated is %d\n", disable, deactivated); - VbAssert(disable == 1 && deactivated == 1); - 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); - } + for (i = 0; i < 2; i++) { + TPM_CHECK(TlclForceClear()); + TPM_CHECK(TlclGetFlags(&disable, &deactivated, NULL)); + printf("disable is %d, deactivated is %d\n", disable, deactivated); + VbAssert(disable == 1 && deactivated == 1); + 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); + } - printf("TEST SUCCEEDED\n"); - return 0; + printf("TEST SUCCEEDED\n"); + return 0; } diff --git a/tests/tpm_lite/tpmtest_globallock.c b/tests/tpm_lite/tpmtest_globallock.c index 96fad9df..2e728524 100644 --- a/tests/tpm_lite/tpmtest_globallock.c +++ b/tests/tpm_lite/tpmtest_globallock.c @@ -15,39 +15,41 @@ #include "tlcl_tests.h" int main(int argc, char** argv) { - uint32_t zero = 0; - uint32_t x; - - TlclLibInit(); - TPM_CHECK(TlclStartupIfNeeded()); - TPM_CHECK(TlclSelfTestFull()); - TPM_CHECK(TlclAssertPhysicalPresence()); - TPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x))); - TPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &zero, sizeof(uint32_t))); - TPM_CHECK(TlclRead(INDEX1, (uint8_t*) &x, sizeof(x))); - TPM_CHECK(TlclWrite(INDEX1, (uint8_t*) &zero, sizeof(uint32_t))); - TPM_CHECK(TlclSetGlobalLock()); - - // Verifies that write to index0 fails. - x = 1; - 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); - - // 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); - - // Turns off PP. - TlclLockPhysicalPresence(); - - // Verifies that write to index1 fails. - x = 3; - 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); - printf("TEST SUCCEEDED\n"); - exit(0); + uint32_t zero = 0; + uint32_t x; + + TlclLibInit(); + TPM_CHECK(TlclStartupIfNeeded()); + TPM_CHECK(TlclSelfTestFull()); + TPM_CHECK(TlclAssertPhysicalPresence()); + TPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x))); + TPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &zero, sizeof(uint32_t))); + TPM_CHECK(TlclRead(INDEX1, (uint8_t*) &x, sizeof(x))); + TPM_CHECK(TlclWrite(INDEX1, (uint8_t*) &zero, sizeof(uint32_t))); + TPM_CHECK(TlclSetGlobalLock()); + + // Verifies that write to index0 fails. + x = 1; + 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); + + // 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); + + // Turns off PP. + TlclLockPhysicalPresence(); + + // Verifies that write to index1 fails. + x = 3; + 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); + 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 c54e1178..522d999f 100644 --- a/tests/tpm_lite/tpmtest_redefine_unowned.c +++ b/tests/tpm_lite/tpmtest_redefine_unowned.c @@ -15,49 +15,49 @@ #include "tlcl_tests.h" int main(int argc, char** argv) { - uint32_t perm; - uint32_t x; + uint32_t perm; + uint32_t x; - TlclLibInit(); - TPM_CHECK(TlclStartupIfNeeded()); - TPM_CHECK(TlclSelfTestFull()); - TPM_CHECK(TlclAssertPhysicalPresence()); + TlclLibInit(); + TPM_CHECK(TlclStartupIfNeeded()); + TPM_CHECK(TlclSelfTestFull()); + TPM_CHECK(TlclAssertPhysicalPresence()); - VbAssert(!TlclIsOwned()); + VbAssert(!TlclIsOwned()); - /* Ensures spaces exist. */ - TPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x))); - TPM_CHECK(TlclRead(INDEX1, (uint8_t*) &x, sizeof(x))); + /* Ensures spaces exist. */ + TPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x))); + TPM_CHECK(TlclRead(INDEX1, (uint8_t*) &x, sizeof(x))); - /* Redefines spaces a couple of times. */ - perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK; - TPM_CHECK(TlclDefineSpace(INDEX0, perm, 2 * sizeof(uint32_t))); - TPM_CHECK(TlclDefineSpace(INDEX0, perm, sizeof(uint32_t))); + /* Redefines spaces a couple of times. */ + perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK; + TPM_CHECK(TlclDefineSpace(INDEX0, perm, 2 * sizeof(uint32_t))); + TPM_CHECK(TlclDefineSpace(INDEX0, perm, sizeof(uint32_t))); - perm = TPM_NV_PER_PPWRITE; - TPM_CHECK(TlclDefineSpace(INDEX1, perm, 2 * sizeof(uint32_t))); - TPM_CHECK(TlclDefineSpace(INDEX1, perm, sizeof(uint32_t))); + perm = TPM_NV_PER_PPWRITE; + TPM_CHECK(TlclDefineSpace(INDEX1, perm, 2 * sizeof(uint32_t))); + TPM_CHECK(TlclDefineSpace(INDEX1, perm, sizeof(uint32_t))); - // Sets the global lock. - TlclSetGlobalLock(); + // Sets the global lock. + TlclSetGlobalLock(); - // Verifies that index0 cannot be redefined. - TPM_EXPECT(TlclDefineSpace(INDEX0, perm, sizeof(uint32_t)), - TPM_E_AREA_LOCKED); + // Verifies that index0 cannot be redefined. + TPM_EXPECT(TlclDefineSpace(INDEX0, perm, sizeof(uint32_t)), + TPM_E_AREA_LOCKED); - // Checks that index1 can. - TPM_CHECK(TlclDefineSpace(INDEX1, perm, 2 * sizeof(uint32_t))); - TPM_CHECK(TlclDefineSpace(INDEX1, perm, sizeof(uint32_t))); + // Checks that index1 can. + TPM_CHECK(TlclDefineSpace(INDEX1, perm, 2 * sizeof(uint32_t))); + TPM_CHECK(TlclDefineSpace(INDEX1, perm, sizeof(uint32_t))); - // Turns off PP. - TlclLockPhysicalPresence(); + // Turns off PP. + TlclLockPhysicalPresence(); - // Verifies that neither index0 nor index1 can be redefined. - TPM_EXPECT(TlclDefineSpace(INDEX0, perm, sizeof(uint32_t)), - TPM_E_BAD_PRESENCE); - TPM_EXPECT(TlclDefineSpace(INDEX1, perm, sizeof(uint32_t)), - TPM_E_BAD_PRESENCE); + // Verifies that neither index0 nor index1 can be redefined. + TPM_EXPECT(TlclDefineSpace(INDEX0, perm, sizeof(uint32_t)), + TPM_E_BAD_PRESENCE); + TPM_EXPECT(TlclDefineSpace(INDEX1, perm, sizeof(uint32_t)), + TPM_E_BAD_PRESENCE); - printf("TEST SUCCEEDED\n"); - exit(0); + printf("TEST SUCCEEDED\n"); + exit(0); } diff --git a/tests/tpm_lite/tpmtest_spaceperm.c b/tests/tpm_lite/tpmtest_spaceperm.c index 71a65218..c2608e4b 100644 --- a/tests/tpm_lite/tpmtest_spaceperm.c +++ b/tests/tpm_lite/tpmtest_spaceperm.c @@ -18,19 +18,19 @@ #define PERMPP TPM_NV_PER_PPWRITE int main(int argc, char** argv) { - uint32_t perm; + uint32_t perm; - TlclLibInit(); - TPM_CHECK(TlclStartupIfNeeded()); - TPM_CHECK(TlclContinueSelfTest()); - TPM_CHECK(TlclAssertPhysicalPresence()); + TlclLibInit(); + TPM_CHECK(TlclStartupIfNeeded()); + TPM_CHECK(TlclContinueSelfTest()); + TPM_CHECK(TlclAssertPhysicalPresence()); - TPM_CHECK(TlclGetPermissions(INDEX0, &perm)); - VbAssert((perm & PERMPPGL) == PERMPPGL); + TPM_CHECK(TlclGetPermissions(INDEX0, &perm)); + VbAssert((perm & PERMPPGL) == PERMPPGL); - TPM_CHECK(TlclGetPermissions(INDEX1, &perm)); - VbAssert((perm & PERMPP) == PERMPP); + TPM_CHECK(TlclGetPermissions(INDEX1, &perm)); + VbAssert((perm & PERMPP) == PERMPP); - printf("TEST SUCCEEDED\n"); - exit(0); + printf("TEST SUCCEEDED\n"); + exit(0); } diff --git a/tests/tpm_lite/tpmtest_testsetup.c b/tests/tpm_lite/tpmtest_testsetup.c index fac1951e..ce360994 100644 --- a/tests/tpm_lite/tpmtest_testsetup.c +++ b/tests/tpm_lite/tpmtest_testsetup.c @@ -15,31 +15,31 @@ #include "utility.h" int main(int argc, char** argv) { - uint32_t perm; - uint32_t result; - uint32_t x; - - TlclLibInit(); - - TPM_CHECK(TlclStartupIfNeeded()); - TPM_CHECK(TlclSelfTestFull()); - TPM_CHECK(TlclAssertPhysicalPresence()); - TPM_CHECK(TlclForceClear()); - TPM_CHECK(TlclSetEnable()); - TPM_CHECK(TlclSetDeactivated(0)); - - result = TlclRead(INDEX0, (uint8_t*) &x, sizeof(x)); - if (result == TPM_E_BADINDEX) { - perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK; - TPM_CHECK(TlclDefineSpace(INDEX0, perm, sizeof(uint32_t))); - } - - result = TlclRead(INDEX1, (uint8_t*) &x, sizeof(x)); - if (result == TPM_E_BADINDEX) { - perm = TPM_NV_PER_PPWRITE; - TPM_CHECK(TlclDefineSpace(INDEX1, perm, sizeof(uint32_t))); - } - - printf("TEST SUCCEEDED\n"); - exit(0); + uint32_t perm; + uint32_t result; + uint32_t x; + + TlclLibInit(); + + TPM_CHECK(TlclStartupIfNeeded()); + TPM_CHECK(TlclSelfTestFull()); + TPM_CHECK(TlclAssertPhysicalPresence()); + TPM_CHECK(TlclForceClear()); + TPM_CHECK(TlclSetEnable()); + TPM_CHECK(TlclSetDeactivated(0)); + + result = TlclRead(INDEX0, (uint8_t*) &x, sizeof(x)); + if (result == TPM_E_BADINDEX) { + perm = TPM_NV_PER_PPWRITE | TPM_NV_PER_GLOBALLOCK; + TPM_CHECK(TlclDefineSpace(INDEX0, perm, sizeof(uint32_t))); + } + + result = TlclRead(INDEX1, (uint8_t*) &x, sizeof(x)); + if (result == TPM_E_BADINDEX) { + perm = TPM_NV_PER_PPWRITE; + TPM_CHECK(TlclDefineSpace(INDEX1, perm, sizeof(uint32_t))); + } + + printf("TEST SUCCEEDED\n"); + exit(0); } diff --git a/tests/tpm_lite/tpmtest_timing.c b/tests/tpm_lite/tpmtest_timing.c index 677b533b..d86b4eee 100644 --- a/tests/tpm_lite/tpmtest_timing.c +++ b/tests/tpm_lite/tpmtest_timing.c @@ -21,48 +21,48 @@ /* Runs [op] and ensures it returns success and doesn't run longer than * [time_limit] in milliseconds. */ -#define TTPM_CHECK(op, time_limit) do { \ - struct timeval before, after; \ - int time; \ - uint32_t __result; \ - gettimeofday(&before, NULL); \ - __result = op; \ - if (__result != TPM_SUCCESS) { \ - printf(#op ": error 0x%x\n", __result); \ - errors++; \ - } \ - gettimeofday(&after, NULL); \ - time = (int) ((after.tv_sec - before.tv_sec) * 1000 + \ - (after.tv_usec - before.tv_usec) / 1000); \ - printf(#op ": %d ms\n", time); \ - if (time > time_limit) { \ - printf(#op " exceeded " #time_limit " ms\n"); \ - time_limit_exceeded = 1; \ - } \ - } while (0) +#define TTPM_CHECK(op, time_limit) do { \ + struct timeval before, after; \ + int time; \ + uint32_t __result; \ + gettimeofday(&before, NULL); \ + __result = op; \ + if (__result != TPM_SUCCESS) { \ + printf(#op ": error 0x%x\n", __result); \ + errors++; \ + } \ + gettimeofday(&after, NULL); \ + time = (int) ((after.tv_sec - before.tv_sec) * 1000 + \ + (after.tv_usec - before.tv_usec) / 1000); \ + printf(#op ": %d ms\n", time); \ + if (time > time_limit) { \ + printf(#op " exceeded " #time_limit " ms\n"); \ + time_limit_exceeded = 1; \ + } \ +} while (0) int main(int argc, char** argv) { - uint32_t x; - uint8_t in[20], out[20]; - int time_limit_exceeded = 0; - int errors = 0; + uint32_t x; + uint8_t in[20], out[20]; + int time_limit_exceeded = 0; + int errors = 0; - TlclLibInit(); - TTPM_CHECK(0, 50); - TTPM_CHECK(TlclStartupIfNeeded(), 50); - TTPM_CHECK(TlclContinueSelfTest(), 100); - TTPM_CHECK(TlclSelfTestFull(), 1000); - TTPM_CHECK(TlclAssertPhysicalPresence(), 100); - TTPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x)), 100); - TTPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x)), 100); - TTPM_CHECK(TlclExtend(0, in, out), 200); - TTPM_CHECK(TlclSetGlobalLock(), 50); - TTPM_CHECK(TlclLockPhysicalPresence(), 100); - if (time_limit_exceeded || errors > 0) { - printf("TEST FAILED\n"); - exit(1); - } else { - printf("TEST SUCCEEDED\n"); - return 0; - } + TlclLibInit(); + TTPM_CHECK(0, 50); + TTPM_CHECK(TlclStartupIfNeeded(), 50); + TTPM_CHECK(TlclContinueSelfTest(), 100); + TTPM_CHECK(TlclSelfTestFull(), 1000); + TTPM_CHECK(TlclAssertPhysicalPresence(), 100); + TTPM_CHECK(TlclWrite(INDEX0, (uint8_t*) &x, sizeof(x)), 100); + TTPM_CHECK(TlclRead(INDEX0, (uint8_t*) &x, sizeof(x)), 100); + TTPM_CHECK(TlclExtend(0, in, out), 200); + TTPM_CHECK(TlclSetGlobalLock(), 50); + TTPM_CHECK(TlclLockPhysicalPresence(), 100); + if (time_limit_exceeded || errors > 0) { + printf("TEST FAILED\n"); + exit(1); + } else { + printf("TEST SUCCEEDED\n"); + return 0; + } } diff --git a/tests/tpm_lite/tpmtest_writelimit.c b/tests/tpm_lite/tpmtest_writelimit.c index 952033cb..66dcc036 100644 --- a/tests/tpm_lite/tpmtest_writelimit.c +++ b/tests/tpm_lite/tpmtest_writelimit.c @@ -17,40 +17,42 @@ #define TPM_MAX_NV_WRITES_NOOWNER 64 int main(int argc, char** argv) { - int i; - - uint32_t result; - - TlclLibInit(); - - TPM_CHECK(TlclStartupIfNeeded()); - TPM_CHECK(TlclSelfTestFull()); - TPM_CHECK(TlclAssertPhysicalPresence()); - TPM_CHECK(TlclForceClear()); - TPM_CHECK(TlclSetEnable()); - TPM_CHECK(TlclSetDeactivated(0)); - - for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) { - printf("writing %d\n", i); - if ((result = TlclWrite(INDEX0, (uint8_t*)&i, sizeof(i))) != TPM_SUCCESS) { - switch (result) { - case TPM_E_MAXNVWRITES: - VbAssert(i >= TPM_MAX_NV_WRITES_NOOWNER); - break; - default: - VbExError("unexpected error code %d (0x%x)\n", result, result); - } - } - } - - /* Reset write count */ - TPM_CHECK(TlclForceClear()); - TPM_CHECK(TlclSetEnable()); - TPM_CHECK(TlclSetDeactivated(0)); - - /* Try writing again. */ - TPM_CHECK(TlclWrite(INDEX0, (uint8_t*)&i, sizeof(i))); - - printf("TEST SUCCEEDED\n"); - exit(0); + int i; + + uint32_t result; + + TlclLibInit(); + + TPM_CHECK(TlclStartupIfNeeded()); + TPM_CHECK(TlclSelfTestFull()); + TPM_CHECK(TlclAssertPhysicalPresence()); + TPM_CHECK(TlclForceClear()); + TPM_CHECK(TlclSetEnable()); + TPM_CHECK(TlclSetDeactivated(0)); + + for (i = 0; i < TPM_MAX_NV_WRITES_NOOWNER + 2; i++) { + printf("writing %d\n", i); + if ((result = TlclWrite(INDEX0, (uint8_t*)&i, + sizeof(i))) != TPM_SUCCESS) { + switch (result) { + case TPM_E_MAXNVWRITES: + VbAssert(i >= TPM_MAX_NV_WRITES_NOOWNER); + break; + default: + VbExError("unexpected error code %d (0x%x)\n", + result, result); + } + } + } + + /* Reset write count */ + TPM_CHECK(TlclForceClear()); + TPM_CHECK(TlclSetEnable()); + TPM_CHECK(TlclSetDeactivated(0)); + + /* Try writing again. */ + TPM_CHECK(TlclWrite(INDEX0, (uint8_t*)&i, sizeof(i))); + + printf("TEST SUCCEEDED\n"); + exit(0); } diff --git a/tests/utility_string_tests.c b/tests/utility_string_tests.c index d7601931..add1e640 100644 --- a/tests/utility_string_tests.c +++ b/tests/utility_string_tests.c @@ -17,103 +17,109 @@ /* Test string concatenation */ static void StrncatTest(void) { - char dest[128]; - - /* Null inputs */ - TEST_EQ(0, StrnAppend(dest, NULL, sizeof(dest)), "StrnAppend('', null)"); - TEST_EQ(0, StrnAppend(NULL, "Hey!", sizeof(dest)), "StrnAppend(null, '')"); - - /* Empty <-- empty */ - *dest = 0; - TEST_EQ(0, StrnAppend(dest, "", sizeof(dest)), "StrnAppend('', '')"); - TEST_EQ(0, strcmp(dest, ""), "StrnAppend('', '') result"); - - /* Nonempty <-- empty */ - strcpy(dest, "Bob"); - TEST_EQ(3, StrnAppend(dest, "", sizeof(dest)), "StrnAppend(B, '')"); - TEST_EQ(0, strcmp(dest, "Bob"), "StrnAppend(B, '') result"); - - /* Empty <-- nonempty */ - *dest = 0; - TEST_EQ(5, StrnAppend(dest, "Alice", sizeof(dest)), "StrnAppend('', A)"); - TEST_EQ(0, strcmp(dest, "Alice"), "StrnAppend('', A) result"); - - /* Nonempty <-- nonempty */ - strcpy(dest, "Tigre"); - TEST_EQ(10, StrnAppend(dest, "Bunny", sizeof(dest)), "StrnAppend(T, B)"); - TEST_EQ(0, strcmp(dest, "TigreBunny"), "StrnAppend(T, B) result"); - - /* Test clipping */ - strcpy(dest, "YesI"); - TEST_EQ(7, StrnAppend(dest, "Can't", 8), "StrnAppend(Y, over)"); - TEST_EQ(0, strcmp(dest, "YesICan"), "StrnAppend(Y, over) result"); - - /* Test clipping if dest already overflows its claimed length */ - strcpy(dest, "BudgetDeficit"); - TEST_EQ(6, StrnAppend(dest, "Spending", 7), "StrnAppend(over, over)"); - TEST_EQ(0, strcmp(dest, "Budget"), "StrnAppend(over, over) result"); + char dest[128]; + + /* Null inputs */ + TEST_EQ(0, StrnAppend(dest, NULL, sizeof(dest)), + "StrnAppend('', null)"); + TEST_EQ(0, StrnAppend(NULL, "Hey!", sizeof(dest)), + "StrnAppend(null, '')"); + + /* Empty <-- empty */ + *dest = 0; + TEST_EQ(0, StrnAppend(dest, "", sizeof(dest)), "StrnAppend('', '')"); + TEST_EQ(0, strcmp(dest, ""), "StrnAppend('', '') result"); + + /* Nonempty <-- empty */ + strcpy(dest, "Bob"); + TEST_EQ(3, StrnAppend(dest, "", sizeof(dest)), "StrnAppend(B, '')"); + TEST_EQ(0, strcmp(dest, "Bob"), "StrnAppend(B, '') result"); + + /* Empty <-- nonempty */ + *dest = 0; + TEST_EQ(5, StrnAppend(dest, "Alice", sizeof(dest)), + "StrnAppend('', A)"); + TEST_EQ(0, strcmp(dest, "Alice"), "StrnAppend('', A) result"); + + /* Nonempty <-- nonempty */ + strcpy(dest, "Tigre"); + TEST_EQ(10, StrnAppend(dest, "Bunny", sizeof(dest)), + "StrnAppend(T, B)"); + TEST_EQ(0, strcmp(dest, "TigreBunny"), "StrnAppend(T, B) result"); + + /* Test clipping */ + strcpy(dest, "YesI"); + TEST_EQ(7, StrnAppend(dest, "Can't", 8), "StrnAppend(Y, over)"); + TEST_EQ(0, strcmp(dest, "YesICan"), "StrnAppend(Y, over) result"); + + /* Test clipping if dest already overflows its claimed length */ + strcpy(dest, "BudgetDeficit"); + TEST_EQ(6, StrnAppend(dest, "Spending", 7), "StrnAppend(over, over)"); + TEST_EQ(0, strcmp(dest, "Budget"), "StrnAppend(over, over) result"); } static void TestU64ToS(uint64_t value, uint32_t radix, uint32_t zero_pad_width, - const char *expect) { - char dest[UINT64_TO_STRING_MAX]; - - TEST_EQ(strlen(expect), - Uint64ToString(dest, sizeof(dest), value, radix, zero_pad_width), - "Uint64ToString"); - printf("Uint64ToString expect %s got %s\n", expect, dest); - TEST_EQ(0, strcmp(dest, expect), "Uint64ToString result"); + const char *expect) { + char dest[UINT64_TO_STRING_MAX]; + + TEST_EQ(strlen(expect), + Uint64ToString(dest, sizeof(dest), value, radix, + zero_pad_width), + "Uint64ToString"); + printf("Uint64ToString expect %s got %s\n", expect, dest); + TEST_EQ(0, strcmp(dest, expect), "Uint64ToString result"); } /* Test uint64 to string conversion */ static void Uint64ToStringTest(void) { - char dest[UINT64_TO_STRING_MAX]; - - /* Test invalid inputs */ - TEST_EQ(0, Uint64ToString(NULL, 8, 123, 10, 8), "Uint64ToString null dest"); - TestU64ToS(0, 1, 0, ""); - TestU64ToS(0, 37, 0, ""); - - /* Binary */ - TestU64ToS(0, 2, 0, "0"); - TestU64ToS(0x9A, 2, 0, "10011010"); - TestU64ToS(0x71, 2, 12, "000001110001"); - TestU64ToS( - ~0ULL, 2, 0, - "1111111111111111111111111111111111111111111111111111111111111111"); - - /* Decimal */ - TestU64ToS(0, 10, 0, "0"); - TestU64ToS(12345, 10, 0, "12345"); - TestU64ToS(67890, 10, 8, "00067890"); - TestU64ToS(~0ULL, 10, 0, "18446744073709551615"); - - /* Hex */ - TestU64ToS(0, 16, 0, "0"); - TestU64ToS(0x12345678, 16, 0, "12345678"); - TestU64ToS(0x9ABCDEF, 16, 8, "09abcdef"); - TestU64ToS(~0ULL, 16, 0, "ffffffffffffffff"); - - /* Zero pad corner cases */ - /* Don't pad if over length */ - TestU64ToS(0x1234567890ULL, 16, 8, "1234567890"); - /* Fail if padding won't fit in buffer */ - TEST_EQ(0, Uint64ToString(dest, 8, 123, 10, 8), "Uint64ToString bad pad"); - TEST_EQ(0, strcmp(dest, ""), "Uint64ToString bad pad result"); + char dest[UINT64_TO_STRING_MAX]; + + /* Test invalid inputs */ + TEST_EQ(0, Uint64ToString(NULL, 8, 123, 10, 8), + "Uint64ToString null dest"); + TestU64ToS(0, 1, 0, ""); + TestU64ToS(0, 37, 0, ""); + + /* Binary */ + TestU64ToS(0, 2, 0, "0"); + TestU64ToS(0x9A, 2, 0, "10011010"); + TestU64ToS(0x71, 2, 12, "000001110001"); + TestU64ToS(~0ULL, 2, 0, + "1111111111111111111111111111111111111111111111111111111111111111"); + + /* Decimal */ + TestU64ToS(0, 10, 0, "0"); + TestU64ToS(12345, 10, 0, "12345"); + TestU64ToS(67890, 10, 8, "00067890"); + TestU64ToS(~0ULL, 10, 0, "18446744073709551615"); + + /* Hex */ + TestU64ToS(0, 16, 0, "0"); + TestU64ToS(0x12345678, 16, 0, "12345678"); + TestU64ToS(0x9ABCDEF, 16, 8, "09abcdef"); + TestU64ToS(~0ULL, 16, 0, "ffffffffffffffff"); + + /* Zero pad corner cases */ + /* Don't pad if over length */ + TestU64ToS(0x1234567890ULL, 16, 8, "1234567890"); + /* Fail if padding won't fit in buffer */ + TEST_EQ(0, Uint64ToString(dest, 8, 123, 10, 8), + "Uint64ToString bad pad"); + TEST_EQ(0, strcmp(dest, ""), "Uint64ToString bad pad result"); } int main(int argc, char* argv[]) { - int error_code = 0; + int error_code = 0; - StrncatTest(); - Uint64ToStringTest(); + StrncatTest(); + Uint64ToStringTest(); - if (!gTestSuccess) - error_code = 255; + if (!gTestSuccess) + error_code = 255; - return error_code; + return error_code; } diff --git a/tests/vb21_host_key_tests.c b/tests/vb21_host_key_tests.c index a833a003..c90fd40e 100644 --- a/tests/vb21_host_key_tests.c +++ b/tests/vb21_host_key_tests.c @@ -281,7 +281,7 @@ static void public_key_tests(const struct alg_combo *combo, } static int test_algorithm(const struct alg_combo *combo, const char *keys_dir, - const char *temp_dir) + const char *temp_dir) { int rsa_bits = vb2_rsa_sig_size(combo->sig_alg) * 8; char *pemfile; diff --git a/tests/vboot_api_devmode_tests.c b/tests/vboot_api_devmode_tests.c index 7e67e07d..31428a4b 100644 --- a/tests/vboot_api_devmode_tests.c +++ b/tests/vboot_api_devmode_tests.c @@ -56,7 +56,7 @@ test_case_t test[] = { 0, 0, 1, { - {0, 0, 2000}, // off and return at 2 seconds + {0, 0, 2000}, // off and return at 2 seconds }}, { "VbBootDeveloperSoundTest( normal )", @@ -64,9 +64,9 @@ test_case_t test[] = { 0, 0, 3, { - {250, 400, 20000}, // first beep at 20 seconds - {250, 400, 20510}, // second beep shortly after - {0, 0, 30020}, // off and return at 30 seconds + {250, 400, 20000}, // first beep at 20 seconds + {250, 400, 20510}, // second beep shortly after + {0, 0, 30020}, // off and return at 30 seconds }}, // Now with some keypresses @@ -76,8 +76,8 @@ test_case_t test[] = { 4, 20400, // Ctrl-D between beeps 2, { - {250, 400, 20000}, // first beep at 20 seconds - {0, 0, 20400}, // sees Ctrl-D, sound off, return + {250, 400, 20000}, // first beep at 20 seconds + {0, 0, 20400}, // sees Ctrl-D, sound off, return }}, { "VbBootDeveloperSoundTest( normal, Ctrl-U not allowed )", @@ -85,11 +85,11 @@ test_case_t test[] = { 21, 10000, // Ctrl-U at 10 seconds 5, { - {120, 400, 10000}, // complains about Ctrl-U (one beep) - {120, 400, 10240}, // complains about Ctrl-U (two beeps) - {250, 400, 20000}, // starts first beep at 20 seconds - {250, 400, 20510}, // starts second beep - {0, 0, 30020}, // returns at 30 seconds + 360ms + {120, 400, 10000}, // complains about Ctrl-U (one beep) + {120, 400, 10240}, // complains about Ctrl-U (two beeps) + {250, 400, 20000}, // starts first beep at 20 seconds + {250, 400, 20510}, // starts second beep + {0, 0, 30020}, // returns at 30 seconds + 360ms }}, }; @@ -156,25 +156,25 @@ VbError_t VbExNvStorageWrite(const uint8_t* buf) } VbError_t VbExDiskGetInfo(VbDiskInfo** infos_ptr, uint32_t* count, - uint32_t disk_flags) + uint32_t disk_flags) { return VBERROR_UNKNOWN; } VbError_t VbExDiskFreeInfo(VbDiskInfo* infos, - VbExDiskHandle_t preserve_handle) + VbExDiskHandle_t preserve_handle) { return VBERROR_SUCCESS; } VbError_t VbExDiskRead(VbExDiskHandle_t handle, uint64_t lba_start, - uint64_t lba_count, void* buffer) + uint64_t lba_count, void* buffer) { return VBERROR_UNKNOWN; } VbError_t VbExDiskWrite(VbExDiskHandle_t handle, uint64_t lba_start, - uint64_t lba_count, const void* buffer) + uint64_t lba_count, const void* buffer) { return VBERROR_UNKNOWN; } diff --git a/tests/vboot_api_kernel2_tests.c b/tests/vboot_api_kernel2_tests.c index b9e05157..11c4c2f6 100644 --- a/tests/vboot_api_kernel2_tests.c +++ b/tests/vboot_api_kernel2_tests.c @@ -139,7 +139,7 @@ int VbExLegacy(void) } VbError_t VbExDiskGetInfo(VbDiskInfo **infos_ptr, uint32_t *count, - uint32_t disk_flags) + uint32_t disk_flags) { if (mock_num_disks_count < ARRAY_SIZE(mock_num_disks)) { if (mock_num_disks[mock_num_disks_count] == -1) @@ -153,7 +153,7 @@ VbError_t VbExDiskGetInfo(VbDiskInfo **infos_ptr, uint32_t *count, } VbError_t VbExDiskFreeInfo(VbDiskInfo *infos, - VbExDiskHandle_t preserve_handle) + VbExDiskHandle_t preserve_handle) { return VBERROR_SUCCESS; } @@ -218,7 +218,7 @@ static void VbUserConfirmsTest(void) mock_keypress[0] = ' '; shutdown_request_calls_left = 1; TEST_EQ(VbUserConfirms(&ctx, VB_CONFIRM_SPACE_MEANS_NO), 0, - "Space means no"); + "Space means no"); ResetMocks(); mock_keypress[0] = ' '; diff --git a/tests/vboot_api_kernel_tests.c b/tests/vboot_api_kernel_tests.c index 133a778f..06d7d695 100644 --- a/tests/vboot_api_kernel_tests.c +++ b/tests/vboot_api_kernel_tests.c @@ -263,7 +263,7 @@ int is_nonzero(const void *vptr, size_t count) /* Mocked verification functions */ VbError_t VbExDiskGetInfo(VbDiskInfo **infos_ptr, uint32_t *count, - uint32_t disk_flags) + uint32_t disk_flags) { int i; int num_disks = 0; @@ -311,7 +311,7 @@ VbError_t VbExDiskGetInfo(VbDiskInfo **infos_ptr, uint32_t *count, } VbError_t VbExDiskFreeInfo(VbDiskInfo *infos, - VbExDiskHandle_t preserve_handle) + VbExDiskHandle_t preserve_handle) { got_load_disk = (const char *)preserve_handle; VB2_DEBUG("%s(): got_load_disk = %s\n", __FUNCTION__, diff --git a/tests/vboot_detach_menu_tests.c b/tests/vboot_detach_menu_tests.c index f494dc91..264fbf86 100644 --- a/tests/vboot_detach_menu_tests.c +++ b/tests/vboot_detach_menu_tests.c @@ -2003,7 +2003,7 @@ static void VbTestLanguageMenu(void) TEST_EQ(screens_count, i, " no extra screens"); TEST_EQ(beeps_count, 0, " no beeps"); - printf("...done.\n"); + printf("...done.\n"); } static void VbNavigationTest(void) diff --git a/tests/vboot_kernel_tests.c b/tests/vboot_kernel_tests.c index c9907597..a5922eda 100644 --- a/tests/vboot_kernel_tests.c +++ b/tests/vboot_kernel_tests.c @@ -190,7 +190,7 @@ static void ResetMocks(void) /* Mocks */ VbError_t VbExDiskRead(VbExDiskHandle_t handle, uint64_t lba_start, - uint64_t lba_count, void *buffer) + uint64_t lba_count, void *buffer) { LOGCALL("VbExDiskRead(h, %d, %d)\n", (int)lba_start, (int)lba_count); @@ -352,11 +352,11 @@ static void ReadWriteGptTest(void) TEST_EQ(AllocAndReadGptData(handle, &g), 0, "AllocAndRead primary invalid"); TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.streaming_drive_sectors, - g.gpt_drive_sectors, 0, g.sector_bytes), - 1, "Primary header is invalid"); + g.gpt_drive_sectors, 0, g.sector_bytes), + 1, "Primary header is invalid"); TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.streaming_drive_sectors, g.gpt_drive_sectors, 0, g.sector_bytes), - 0, "Secondary header is valid"); + 0, "Secondary header is valid"); TEST_CALLS("VbExDiskRead(h, 1, 1)\n" "VbExDiskRead(h, 1023, 1)\n" "VbExDiskRead(h, 991, 32)\n"); @@ -372,10 +372,10 @@ static void ReadWriteGptTest(void) "AllocAndRead secondary invalid"); TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.streaming_drive_sectors, g.gpt_drive_sectors, 0, g.sector_bytes), - 0, "Primary header is valid"); + 0, "Primary header is valid"); TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.streaming_drive_sectors, g.gpt_drive_sectors, 0, g.sector_bytes), - 1, "Secondary header is invalid"); + 1, "Secondary header is invalid"); TEST_CALLS("VbExDiskRead(h, 1, 1)\n" "VbExDiskRead(h, 2, 32)\n" "VbExDiskRead(h, 1023, 1)\n"); @@ -392,10 +392,10 @@ static void ReadWriteGptTest(void) "AllocAndRead primary and secondary invalid"); TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.streaming_drive_sectors, g.gpt_drive_sectors, 0, g.sector_bytes), - 1, "Primary header is invalid"); + 1, "Primary header is invalid"); TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.streaming_drive_sectors, g.gpt_drive_sectors, 0, g.sector_bytes), - 1, "Secondary header is invalid"); + 1, "Secondary header is invalid"); TEST_CALLS("VbExDiskRead(h, 1, 1)\n" "VbExDiskRead(h, 1023, 1)\n"); WriteAndFreeGptData(handle, &g); @@ -423,7 +423,7 @@ static void ReadWriteGptTest(void) "VbExDiskWrite(h, 2, 32)\n"); TEST_EQ(CheckHeader(mock_gpt_primary, 0, g.streaming_drive_sectors, g.gpt_drive_sectors, 0, g.sector_bytes), - 0, "Fix Primary GPT: Primary header is valid"); + 0, "Fix Primary GPT: Primary header is valid"); /* * Invalidate secondary GPT header and check that it can be @@ -448,7 +448,7 @@ static void ReadWriteGptTest(void) "VbExDiskWrite(h, 991, 32)\n"); TEST_EQ(CheckHeader(mock_gpt_secondary, 1, g.streaming_drive_sectors, g.gpt_drive_sectors, 0, g.sector_bytes), - 0, "Fix Secondary GPT: Secondary header is valid"); + 0, "Fix Secondary GPT: Secondary header is valid"); /* Data which is changed is written */ ResetMocks(); |