Add tests for utility.h and sysincludes.h macros

BUG=chromium-os:17564
TEST=make && make runtests

Change-Id: Id0a834c2b234d5f9cbbb37a69426e2b97c734d0a
Reviewed-on: http://gerrit.chromium.org/gerrit/6608
Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-by: Stefan Reinauer <reinauer@google.com>
Tested-by: Randall Spangler <rspangler@chromium.org>
This commit is contained in:
Randall Spangler
2011-08-24 14:16:01 -07:00
parent 1f5d53f7bd
commit f02bbb4635
4 changed files with 58 additions and 4 deletions

View File

@@ -46,7 +46,7 @@
/* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and /* Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and
* [lsw] forming the most and least signficant 16-bit words. * [lsw] forming the most and least signficant 16-bit words.
*/ */
#define CombineUint16Pair(msw,lsw) (((msw) << 16) | \ #define CombineUint16Pair(msw,lsw) (((uint32_t)(msw) << 16) | \
(((lsw)) & 0xFFFF)) (((lsw)) & 0xFFFF))
/* Return the minimum of (a) or (b). */ /* Return the minimum of (a) or (b). */
#define Min(a, b) (((a) < (b)) ? (a) : (b)) #define Min(a, b) (((a) < (b)) ? (a) : (b))

View File

@@ -375,11 +375,12 @@ typedef struct VbSharedDataHeader {
} __attribute__((packed)) VbSharedDataHeader; } __attribute__((packed)) VbSharedDataHeader;
/* Size of VbSharedDataheader for each older version */ /* Size of VbSharedDataheader for each version */
// TODO: crossystem needs not to // TODO: crossystem needs not to
// fail if called on a v1 system where sizeof(VbSharedDataHeader) was smaller // fail if called on a v1 system where sizeof(VbSharedDataHeader) was smaller
#define VB_SHARED_DATA_HEADER_SIZE_V1 1072 #define VB_SHARED_DATA_HEADER_SIZE_V1 1072
#define VB_SHARED_DATA_HEADER_SIZE_V2 1096
#define VB_SHARED_DATA_VERSION 2 /* Version for struct_version */ #define VB_SHARED_DATA_VERSION 2 /* Version for struct_version */

View File

@@ -2,7 +2,7 @@
* Use of this source code is governed by a BSD-style license that can be * Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file. * found in the LICENSE file.
* *
* Tests for utility functions. * Tests for utility functions
*/ */
#include <stdio.h> #include <stdio.h>
@@ -16,6 +16,37 @@
#include "vboot_common.h" #include "vboot_common.h"
/* Test utility.h and sysincludes.h macros */
static void MacrosTest(void) {
int64_t a = -10, b = -20;
uint64_t u = UINT64_C(0xABCD00000000);
uint64_t v = UINT64_C(0xABCD000000);
TEST_EQ(CombineUint16Pair(1, 2), 0x00010002, "CombineUint16Pair");
TEST_EQ(CombineUint16Pair(0xFFFE, 0xFFFF), 0xFFFEFFFF,
"CombineUint16Pair 2");
TEST_EQ(CombineUint16Pair(-4, -16), 0xFFFCFFF0,
"CombineUint16Pair big negative");
TEST_EQ(CombineUint16Pair(0x10003, 0x10004), 0x00030004,
"CombineUint16Pair overflow");
TEST_EQ(Min(1, 2), 1, "Min 1");
TEST_EQ(Min(4, 3), 3, "Min 2");
TEST_EQ(Min(5, 5), 5, "Min 5");
TEST_EQ(Min(a, b), b, "Min uint64 1");
TEST_EQ(Min(b, a), b, "Min uint64 2");
TEST_EQ(Min(b, b), b, "Min uint64 same");
TEST_EQ(UINT64_RSHIFT(u, 8), v, "UINT64_RSHIFT 8");
TEST_EQ(UINT64_RSHIFT(u, 0), u, "UINT64_RSHIFT 0");
TEST_EQ(UINT64_RSHIFT(u, 36), UINT64_C(0xABC), "UINT64_RSHIFT 36");
TEST_EQ(UINT64_MULT32(v, 0), 0, "UINT64_MULT32 0");
TEST_EQ(UINT64_MULT32(v, 1), v, "UINT64_MULT32 1");
TEST_EQ(UINT64_MULT32(v, 256), u, "UINT64_MULT32 256");
}
/* Test Memset */ /* Test Memset */
static void MemsetTest(void) { static void MemsetTest(void) {
char dest[128]; char dest[128];
@@ -56,6 +87,7 @@ __pragma(warning (disable: 4100))
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
int error_code = 0; int error_code = 0;
MacrosTest();
MemsetTest(); MemsetTest();
SafeMemcmpTest(); SafeMemcmpTest();

View File

@@ -21,12 +21,32 @@ static void StructPackingTest(void) {
"sizeof(VbKeyBlockHeader)"); "sizeof(VbKeyBlockHeader)");
TEST_EQ(EXPECTED_VBFIRMWAREPREAMBLEHEADER2_0_SIZE, TEST_EQ(EXPECTED_VBFIRMWAREPREAMBLEHEADER2_0_SIZE,
sizeof(VbFirmwarePreambleHeader2_0), sizeof(VbFirmwarePreambleHeader2_0),
"sizeof(VbFirmwarePreambleHeader)"); "sizeof(VbFirmwarePreambleHeader2_0)");
TEST_EQ(EXPECTED_VBFIRMWAREPREAMBLEHEADER2_1_SIZE, TEST_EQ(EXPECTED_VBFIRMWAREPREAMBLEHEADER2_1_SIZE,
sizeof(VbFirmwarePreambleHeader), sizeof(VbFirmwarePreambleHeader),
"sizeof(VbFirmwarePreambleHeader)"); "sizeof(VbFirmwarePreambleHeader)");
TEST_EQ(EXPECTED_VBKERNELPREAMBLEHEADER_SIZE, TEST_EQ(EXPECTED_VBKERNELPREAMBLEHEADER_SIZE,
sizeof(VbKernelPreambleHeader), "sizeof(VbKernelPreambleHeader)"); sizeof(VbKernelPreambleHeader), "sizeof(VbKernelPreambleHeader)");
TEST_EQ(VB_SHARED_DATA_HEADER_SIZE_V1,
(long)&((VbSharedDataHeader*)NULL)->recovery_reason,
"sizeof(VbSharedDataHeader) V1");
TEST_EQ(VB_SHARED_DATA_HEADER_SIZE_V2,
sizeof(VbSharedDataHeader),
"sizeof(VbSharedDataHeader) V2");
}
/* Test array sized macro */
static void ArraySizeTest(void) {
uint8_t arr1[12];
uint32_t arr2[7];
uint64_t arr3[9];
TEST_EQ(ARRAY_SIZE(arr1), 12, "ARRAYSIZE(uint8_t)");
TEST_EQ(ARRAY_SIZE(arr2), 7, "ARRAYSIZE(uint32_t)");
TEST_EQ(ARRAY_SIZE(arr3), 9, "ARRAYSIZE(uint64_t)");
} }
@@ -112,6 +132,7 @@ int main(int argc, char* argv[]) {
int error_code = 0; int error_code = 0;
StructPackingTest(); StructPackingTest();
ArraySizeTest();
VerifyHelperFunctions(); VerifyHelperFunctions();
if (!gTestSuccess) if (!gTestSuccess)