summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2011-08-24 18:35:19 -0700
committerRandall Spangler <rspangler@chromium.org>2011-08-25 09:04:35 -0700
commitaeb8632258f240c9350716bdfe149d1f3c5c54a1 (patch)
treea8a9ed4ccbe41efe34ee680043dd7d42e424dc81
parent9462e3e4c405fa810bec5cea564eea89e9d4eb01 (diff)
downloadvboot-aeb8632258f240c9350716bdfe149d1f3c5c54a1.tar.gz
Add unit tests for tpm_bootmode
BUG=chromium-os:17564 TEST=make && make runtests Change-Id: I6437e41f55d5b705a3768d325999847c5753a13d Reviewed-on: http://gerrit.chromium.org/gerrit/6627 Reviewed-by: Stefan Reinauer <reinauer@google.com> Tested-by: Randall Spangler <rspangler@chromium.org>
-rw-r--r--tests/Makefile2
-rw-r--r--tests/tpm_bootmode_tests.c83
2 files changed, 85 insertions, 0 deletions
diff --git a/tests/Makefile b/tests/Makefile
index 235ba957..b81825bc 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -18,6 +18,7 @@ TEST_NAMES = cgptlib_test \
stateful_util_tests \
utility_string_tests \
utility_tests \
+ tpm_bootmode_tests \
vboot_common_tests \
vboot_common2_tests \
vboot_common3_tests \
@@ -94,6 +95,7 @@ runcryptotests:
runmisctests:
./run_vbutil_tests.sh
${BUILD_ROOT}/stateful_util_tests
+ ${BUILD_ROOT}/tpm_bootmode_tests
${BUILD_ROOT}/utility_string_tests
${BUILD_ROOT}/utility_tests
diff --git a/tests/tpm_bootmode_tests.c b/tests/tpm_bootmode_tests.c
new file mode 100644
index 00000000..3fd7a956
--- /dev/null
+++ b/tests/tpm_bootmode_tests.c
@@ -0,0 +1,83 @@
+/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * Tests for tpm_bootmode functions
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define _STUB_IMPLEMENTATION_ /* So we can use memset() ourselves */
+
+#include "test_common.h"
+#include "utility.h"
+#include "tpm_bootmode.h"
+
+extern const char* kBootStateSHA1Digests[];
+
+/* Last in_digest passed to TlclExtend() */
+static const uint8_t* last_in = NULL;
+
+/* Return value to pass for TlclExtend() */
+static uint32_t extend_returns = 0;
+
+/* Mocked TlclExtend() function for testing */
+uint32_t TlclExtend(int pcr_num, const uint8_t* in_digest,
+ uint8_t* out_digest) {
+
+ /* Should be using pcr 0 */
+ TEST_EQ(pcr_num, 0, "TlclExtend pcr_num");
+
+ last_in = in_digest;
+
+ return extend_returns;
+}
+
+
+/* Test setting TPM boot mode state */
+static void BootStateTest(void) {
+ int recdev;
+ int flags;
+ int index;
+ char what[128];
+
+ /* Test all permutations of developer and recovery mode */
+ for (recdev = 0; recdev < 4; recdev++) {
+ /* Exhaustively test all permutations of key block flags currently
+ * defined in vboot_struct.h (KEY_BLOCK_FLAG_*) */
+ for (flags = 0; flags < 16; flags++) {
+ index = recdev * 3;
+ if (6 == flags)
+ index += 2;
+ else if (7 == flags)
+ index += 1;
+
+ last_in = NULL;
+ TEST_EQ(SetTPMBootModeState(recdev & 2, recdev & 1, flags), 0,
+ "SetTPMBootModeState return");
+ snprintf(what, sizeof(what), "SetTPMBootModeState %d, 0x%x",
+ recdev, flags);
+ TEST_PTR_EQ(last_in, kBootStateSHA1Digests[index], what);
+ }
+ }
+
+ extend_returns = 1;
+ TEST_EQ(SetTPMBootModeState(0, 0, 0), 1, "SetTPMBootModeState error");
+}
+
+
+/* disable MSVC warnings on unused arguments */
+__pragma(warning (disable: 4100))
+
+int main(int argc, char* argv[]) {
+ int error_code = 0;
+
+ BootStateTest();
+
+ if (!gTestSuccess)
+ error_code = 255;
+
+ return error_code;
+}