Randall Spangler | a3454fc | 2011-08-23 14:41:18 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | * |
Randall Spangler | f02bbb4 | 2011-08-24 14:16:01 -0700 | [diff] [blame^] | 5 | * Tests for utility functions |
Randall Spangler | a3454fc | 2011-08-23 14:41:18 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <stdio.h> |
| 9 | #include <stdlib.h> |
| 10 | #include <string.h> |
| 11 | |
| 12 | #define _STUB_IMPLEMENTATION_ /* So we can use memset() ourselves */ |
| 13 | |
| 14 | #include "test_common.h" |
| 15 | #include "utility.h" |
| 16 | #include "vboot_common.h" |
| 17 | |
| 18 | |
Randall Spangler | f02bbb4 | 2011-08-24 14:16:01 -0700 | [diff] [blame^] | 19 | /* Test utility.h and sysincludes.h macros */ |
| 20 | static void MacrosTest(void) { |
| 21 | int64_t a = -10, b = -20; |
| 22 | uint64_t u = UINT64_C(0xABCD00000000); |
| 23 | uint64_t v = UINT64_C(0xABCD000000); |
| 24 | |
| 25 | TEST_EQ(CombineUint16Pair(1, 2), 0x00010002, "CombineUint16Pair"); |
| 26 | TEST_EQ(CombineUint16Pair(0xFFFE, 0xFFFF), 0xFFFEFFFF, |
| 27 | "CombineUint16Pair 2"); |
| 28 | TEST_EQ(CombineUint16Pair(-4, -16), 0xFFFCFFF0, |
| 29 | "CombineUint16Pair big negative"); |
| 30 | TEST_EQ(CombineUint16Pair(0x10003, 0x10004), 0x00030004, |
| 31 | "CombineUint16Pair overflow"); |
| 32 | |
| 33 | TEST_EQ(Min(1, 2), 1, "Min 1"); |
| 34 | TEST_EQ(Min(4, 3), 3, "Min 2"); |
| 35 | TEST_EQ(Min(5, 5), 5, "Min 5"); |
| 36 | TEST_EQ(Min(a, b), b, "Min uint64 1"); |
| 37 | TEST_EQ(Min(b, a), b, "Min uint64 2"); |
| 38 | TEST_EQ(Min(b, b), b, "Min uint64 same"); |
| 39 | |
| 40 | TEST_EQ(UINT64_RSHIFT(u, 8), v, "UINT64_RSHIFT 8"); |
| 41 | TEST_EQ(UINT64_RSHIFT(u, 0), u, "UINT64_RSHIFT 0"); |
| 42 | TEST_EQ(UINT64_RSHIFT(u, 36), UINT64_C(0xABC), "UINT64_RSHIFT 36"); |
| 43 | |
| 44 | TEST_EQ(UINT64_MULT32(v, 0), 0, "UINT64_MULT32 0"); |
| 45 | TEST_EQ(UINT64_MULT32(v, 1), v, "UINT64_MULT32 1"); |
| 46 | TEST_EQ(UINT64_MULT32(v, 256), u, "UINT64_MULT32 256"); |
| 47 | } |
| 48 | |
| 49 | |
Randall Spangler | a3454fc | 2011-08-23 14:41:18 -0700 | [diff] [blame] | 50 | /* Test Memset */ |
| 51 | static void MemsetTest(void) { |
| 52 | char dest[128]; |
| 53 | char want[128]; |
| 54 | |
| 55 | memset(want, 0, 128); |
| 56 | memset(dest, 0, 128); |
| 57 | |
| 58 | /* Simple fill */ |
| 59 | memset(want, 123, 5); |
Randall Spangler | 1f5d53f | 2011-08-24 12:07:43 -0700 | [diff] [blame] | 60 | TEST_PTR_EQ(dest, Memset(dest, 123, 5), "Memset() returns dest"); |
Randall Spangler | a3454fc | 2011-08-23 14:41:18 -0700 | [diff] [blame] | 61 | TEST_EQ(0, memcmp(dest, want, 128), "Memset()"); |
| 62 | |
| 63 | /* Filling length 0 does nothing */ |
| 64 | Memset(dest, 42, 0); |
| 65 | TEST_EQ(0, memcmp(dest, want, 128), "Memset() size=0"); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | /* Test SafeMemcmp */ |
| 70 | static void SafeMemcmpTest(void) { |
| 71 | /* Zero-length strings are equal */ |
| 72 | TEST_EQ(0, SafeMemcmp("APPLE", "TIGER", 0), "SafeMemcmp() size=0"); |
| 73 | |
| 74 | /* Test equal arrays */ |
| 75 | TEST_EQ(0, SafeMemcmp("clonebob", "clonebob", 8), "SafeMemcmp() equal"); |
| 76 | /* Inequality past end of array doesn't affect result */ |
| 77 | TEST_EQ(0, SafeMemcmp("clonebob", "clonedan", 5), "SafeMemcmp() equal2"); |
| 78 | |
| 79 | TEST_EQ(1, SafeMemcmp("APPLE", "TIGER", 5), "SafeMemcmp() unequal"); |
| 80 | TEST_EQ(1, SafeMemcmp("APPLE", "APPLe", 5), "SafeMemcmp() unequal 2"); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | /* disable MSVC warnings on unused arguments */ |
| 85 | __pragma(warning (disable: 4100)) |
| 86 | |
| 87 | int main(int argc, char* argv[]) { |
| 88 | int error_code = 0; |
| 89 | |
Randall Spangler | f02bbb4 | 2011-08-24 14:16:01 -0700 | [diff] [blame^] | 90 | MacrosTest(); |
Randall Spangler | a3454fc | 2011-08-23 14:41:18 -0700 | [diff] [blame] | 91 | MemsetTest(); |
| 92 | SafeMemcmpTest(); |
| 93 | |
| 94 | if (!gTestSuccess) |
| 95 | error_code = 255; |
| 96 | |
| 97 | return error_code; |
| 98 | } |