blob: 978f768c7729913634801fb58aa7e8656ed6f84f [file] [log] [blame]
Randall Spanglera3454fc2011-08-23 14:41:18 -07001/* 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 Spanglerf02bbb42011-08-24 14:16:01 -07005 * Tests for utility functions
Randall Spanglera3454fc2011-08-23 14:41:18 -07006 */
7
Bill Richardson0c3ba242013-03-29 11:09:30 -07008#include <stdint.h>
Randall Spanglera3454fc2011-08-23 14:41:18 -07009#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12
13#define _STUB_IMPLEMENTATION_ /* So we can use memset() ourselves */
14
15#include "test_common.h"
16#include "utility.h"
17#include "vboot_common.h"
18
19
Randall Spanglerf02bbb42011-08-24 14:16:01 -070020/* Test utility.h and sysincludes.h macros */
21static void MacrosTest(void) {
22 int64_t a = -10, b = -20;
Gabe Blackac8805e2013-03-16 04:03:40 -070023 uint64_t u = (0xABCD00000000ULL);
24 uint64_t v = (0xABCD000000ULL);
Randall Spanglerf02bbb42011-08-24 14:16:01 -070025
26 TEST_EQ(CombineUint16Pair(1, 2), 0x00010002, "CombineUint16Pair");
27 TEST_EQ(CombineUint16Pair(0xFFFE, 0xFFFF), 0xFFFEFFFF,
28 "CombineUint16Pair 2");
29 TEST_EQ(CombineUint16Pair(-4, -16), 0xFFFCFFF0,
30 "CombineUint16Pair big negative");
31 TEST_EQ(CombineUint16Pair(0x10003, 0x10004), 0x00030004,
32 "CombineUint16Pair overflow");
33
34 TEST_EQ(Min(1, 2), 1, "Min 1");
35 TEST_EQ(Min(4, 3), 3, "Min 2");
36 TEST_EQ(Min(5, 5), 5, "Min 5");
37 TEST_EQ(Min(a, b), b, "Min uint64 1");
38 TEST_EQ(Min(b, a), b, "Min uint64 2");
39 TEST_EQ(Min(b, b), b, "Min uint64 same");
40
Gabe Blackac8805e2013-03-16 04:03:40 -070041 TEST_EQ(u >> 8, v, "uint64_t >> 8");
42 TEST_EQ(u >> 0, u, "uint64_t >> 0");
43 TEST_EQ(u >> 36, (uint64_t)0xABC, "uint64_t >> 36");
Randall Spanglerf02bbb42011-08-24 14:16:01 -070044
Gabe Blackac8805e2013-03-16 04:03:40 -070045 TEST_EQ(v * (uint32_t)0, 0, "uint64_t * uint32_t 0");
46 TEST_EQ(v * (uint32_t)1, v, "uint64_t * uint32_t 1");
47 TEST_EQ(v * (uint32_t)256, u, "uint64_t * uint32_t 256");
Randall Spanglerf02bbb42011-08-24 14:16:01 -070048}
49
50
Randall Spanglera3454fc2011-08-23 14:41:18 -070051/* Test SafeMemcmp */
52static void SafeMemcmpTest(void) {
53 /* Zero-length strings are equal */
54 TEST_EQ(0, SafeMemcmp("APPLE", "TIGER", 0), "SafeMemcmp() size=0");
55
56 /* Test equal arrays */
57 TEST_EQ(0, SafeMemcmp("clonebob", "clonebob", 8), "SafeMemcmp() equal");
58 /* Inequality past end of array doesn't affect result */
59 TEST_EQ(0, SafeMemcmp("clonebob", "clonedan", 5), "SafeMemcmp() equal2");
60
61 TEST_EQ(1, SafeMemcmp("APPLE", "TIGER", 5), "SafeMemcmp() unequal");
62 TEST_EQ(1, SafeMemcmp("APPLE", "APPLe", 5), "SafeMemcmp() unequal 2");
63}
64
65
Randall Spanglera3454fc2011-08-23 14:41:18 -070066int main(int argc, char* argv[]) {
67 int error_code = 0;
68
Randall Spanglerf02bbb42011-08-24 14:16:01 -070069 MacrosTest();
Randall Spanglera3454fc2011-08-23 14:41:18 -070070 SafeMemcmpTest();
71
72 if (!gTestSuccess)
73 error_code = 255;
74
75 return error_code;
76}