summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2011-08-24 15:38:20 -0700
committerRandall Spangler <rspangler@chromium.org>2011-08-24 15:47:58 -0700
commit68f6049f3f64fe395ae7e0e30e8a1b1729f34e69 (patch)
tree715bf1ef8fb8aa5651b7bd42906a75d953568923 /tests
parentf02bbb4635175d6c3ba8f6557802f37e20160533 (diff)
downloadvboot-68f6049f3f64fe395ae7e0e30e8a1b1729f34e69.tar.gz
Add missing tests for vboot_common
BUG=chromium-os:17564 TEST=make && make runtests Change-Id: I8dd6103eb60c8bc5af2abdd733d8f581984f27b9 Reviewed-on: http://gerrit.chromium.org/gerrit/6615 Reviewed-by: Bill Richardson <wfrichar@chromium.org> Tested-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/vboot_common_tests.c84
1 files changed, 81 insertions, 3 deletions
diff --git a/tests/vboot_common_tests.c b/tests/vboot_common_tests.c
index 9180958d..85b2d4ed 100644
--- a/tests/vboot_common_tests.c
+++ b/tests/vboot_common_tests.c
@@ -2,16 +2,19 @@
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
- * Tests for firmware image library.
+ * Tests for firmware vboot_common.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "test_common.h"
+#include "utility.h"
#include "vboot_common.h"
-/* Test struct packing */
+/* Test struct packing for vboot_struct.h structs which are passed
+ * between firmware and OS, or passed between different phases of
+ * firmware. */
static void StructPackingTest(void) {
TEST_EQ(EXPECTED_VBPUBLICKEY_SIZE, sizeof(VbPublicKey),
"sizeof(VbPublicKey)");
@@ -38,7 +41,7 @@ static void StructPackingTest(void) {
}
-/* Test array sized macro */
+/* Test array size macro */
static void ArraySizeTest(void) {
uint8_t arr1[12];
uint32_t arr2[7];
@@ -122,9 +125,82 @@ static void VerifyHelperFunctions(void) {
TEST_EQ(VerifySignatureInside(&s, 99, &s), 1,
"SignatureInside offset too big");
}
+}
+
+
+/* Public key utility functions */
+static void PublicKeyTest(void) {
+ VbPublicKey k[3];
+ VbPublicKey j[5];
+
+ /* Fill some bits of the public key data */
+ Memset(j, 0, sizeof(j));
+ Memset(k, 0x42, sizeof(k));
+ k[1].key_size = 12345;
+ k[2].key_version = 67;
+
+ PublicKeyInit(k, (uint8_t*)(k + 1), 2 * sizeof(VbPublicKey));
+ TEST_EQ(k->key_offset, sizeof(VbPublicKey), "PublicKeyInit key_offset");
+ TEST_EQ(k->key_size, 2 * sizeof(VbPublicKey), "PublicKeyInit key_size");
+ TEST_EQ(k->algorithm, kNumAlgorithms, "PublicKeyInit algorithm");
+ TEST_EQ(k->key_version, 0, "PublicKeyInit key_version");
+
+ /* Set algorithm and version, so we can tell if they get copied */
+ k->algorithm = 3;
+ k->key_version = 21;
+
+ /* Copying to a smaller destination should fail */
+ PublicKeyInit(j, (uint8_t*)(j + 1), 2 * sizeof(VbPublicKey) - 1);
+ TEST_NEQ(0, PublicKeyCopy(j, k), "PublicKeyCopy too small");
+
+ /* Copying to same or larger size should succeed */
+ PublicKeyInit(j, (uint8_t*)(j + 2), 2 * sizeof(VbPublicKey) + 1);
+ TEST_EQ(0, PublicKeyCopy(j, k), "PublicKeyCopy same");
+ /* Offset in destination shouldn't have been modified */
+ TEST_EQ(j->key_offset, 2 * sizeof(VbPublicKey), "PublicKeyCopy key_offset");
+ /* Size should have been reduced to match the source */
+ TEST_EQ(k->key_size, 2 * sizeof(VbPublicKey), "PublicKeyCopy key_size");
+ /* Other fields should have been copied */
+ TEST_EQ(k->algorithm, j->algorithm, "PublicKeyCopy algorithm");
+ TEST_EQ(k->key_version, j->key_version, "PublicKeyCopy key_version");
+ /* Data should have been copied */
+ TEST_EQ(0, Memcmp(GetPublicKeyData(k), GetPublicKeyData(j), k->key_size),
+ "PublicKeyCopy data");
+}
+
+/* VbSharedData utility tests */
+static void VbSharedDataTest(void) {
+ uint8_t buf[VB_SHARED_DATA_MIN_SIZE + 1];
+ VbSharedDataHeader* d = (VbSharedDataHeader*)buf;
+
+ TEST_NEQ(VBOOT_SUCCESS, VbSharedDataInit(d, sizeof(VbSharedDataHeader) - 1),
+ "VbSharedDataInit too small");
+ TEST_NEQ(VBOOT_SUCCESS, VbSharedDataInit(d, VB_SHARED_DATA_MIN_SIZE - 1),
+ "VbSharedDataInit too small 2");
+ TEST_NEQ(VBOOT_SUCCESS, VbSharedDataInit(NULL, VB_SHARED_DATA_MIN_SIZE),
+ "VbSharedDataInit null");
+
+ Memset(buf, 0x68, sizeof(buf));
+ TEST_EQ(VBOOT_SUCCESS, VbSharedDataInit(d, VB_SHARED_DATA_MIN_SIZE),
+ "VbSharedDataInit");
+ /* Check fields that should have been initialized */
+ TEST_EQ(d->magic, VB_SHARED_DATA_MAGIC, "VbSharedDataInit magic");
+ TEST_EQ(d->struct_version, VB_SHARED_DATA_VERSION,
+ "VbSharedDataInit version");
+ TEST_EQ(d->struct_size, sizeof(VbSharedDataHeader),
+ "VbSharedDataInit struct_size");
+ TEST_EQ(d->data_size, VB_SHARED_DATA_MIN_SIZE, "VbSharedDataInit data_size");
+ TEST_EQ(d->data_used, d->struct_size, "VbSharedDataInit data_used");
+ TEST_EQ(d->firmware_index, 0xFF, "VbSharedDataInit firmware index");
+ /* Sample some other fields to make sure they were zeroed */
+ TEST_EQ(d->flags, 0, "VbSharedDataInit firmware flags");
+ TEST_EQ(d->lk_call_count, 0, "VbSharedDataInit lk_call_count");
+ TEST_EQ(d->kernel_version_lowest, 0,
+ "VbSharedDataInit kernel_version_lowest");
}
+
/* disable MSVC warnings on unused arguments */
__pragma(warning (disable: 4100))
@@ -134,6 +210,8 @@ int main(int argc, char* argv[]) {
StructPackingTest();
ArraySizeTest();
VerifyHelperFunctions();
+ PublicKeyTest();
+ VbSharedDataTest();
if (!gTestSuccess)
error_code = 255;