1a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * Use of this source code is governed by a BSD-style license that can be
3a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler * found in the LICENSE file.
4a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler *
5f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler * Tests for utility functions
6a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler */
7a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
80c3ba249abb1dc60f5ebabccf84ff13206440b83Bill Richardson#include <stdint.h>
9a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler#include <stdio.h>
10a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler#include <stdlib.h>
11a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler#include <string.h>
12a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
13a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler#define _STUB_IMPLEMENTATION_  /* So we can use memset() ourselves */
14a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
15a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler#include "test_common.h"
16a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler#include "utility.h"
17a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler#include "vboot_common.h"
18a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
19a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
20f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler/* Test utility.h and sysincludes.h macros */
21f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spanglerstatic void MacrosTest(void) {
22f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  int64_t a = -10, b = -20;
23ac8805e7e9bd40c03baf44e37b26f28d9b763ab5Gabe Black  uint64_t u = (0xABCD00000000ULL);
24ac8805e7e9bd40c03baf44e37b26f28d9b763ab5Gabe Black  uint64_t v = (0xABCD000000ULL);
25f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler
26f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  TEST_EQ(CombineUint16Pair(1, 2), 0x00010002, "CombineUint16Pair");
27f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  TEST_EQ(CombineUint16Pair(0xFFFE, 0xFFFF), 0xFFFEFFFF,
28f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler          "CombineUint16Pair 2");
29f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  TEST_EQ(CombineUint16Pair(-4, -16), 0xFFFCFFF0,
30f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler          "CombineUint16Pair big negative");
31f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  TEST_EQ(CombineUint16Pair(0x10003, 0x10004), 0x00030004,
32f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler          "CombineUint16Pair overflow");
33f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler
34f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  TEST_EQ(Min(1, 2), 1, "Min 1");
35f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  TEST_EQ(Min(4, 3), 3, "Min 2");
36f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  TEST_EQ(Min(5, 5), 5, "Min 5");
37f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  TEST_EQ(Min(a, b), b, "Min uint64 1");
38f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  TEST_EQ(Min(b, a), b, "Min uint64 2");
39f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  TEST_EQ(Min(b, b), b, "Min uint64 same");
40f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler
41ac8805e7e9bd40c03baf44e37b26f28d9b763ab5Gabe Black  TEST_EQ(u >> 8, v, "uint64_t >> 8");
42ac8805e7e9bd40c03baf44e37b26f28d9b763ab5Gabe Black  TEST_EQ(u >> 0, u, "uint64_t >> 0");
43ac8805e7e9bd40c03baf44e37b26f28d9b763ab5Gabe Black  TEST_EQ(u >> 36, (uint64_t)0xABC, "uint64_t >> 36");
44f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler
45ac8805e7e9bd40c03baf44e37b26f28d9b763ab5Gabe Black  TEST_EQ(v * (uint32_t)0, 0, "uint64_t * uint32_t 0");
46ac8805e7e9bd40c03baf44e37b26f28d9b763ab5Gabe Black  TEST_EQ(v * (uint32_t)1, v, "uint64_t * uint32_t 1");
47ac8805e7e9bd40c03baf44e37b26f28d9b763ab5Gabe Black  TEST_EQ(v * (uint32_t)256, u, "uint64_t * uint32_t 256");
48f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler}
49f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler
50f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler
51a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler/* Test SafeMemcmp */
52a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spanglerstatic void SafeMemcmpTest(void) {
53a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  /* Zero-length strings are equal */
54a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  TEST_EQ(0, SafeMemcmp("APPLE", "TIGER", 0), "SafeMemcmp() size=0");
55a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
56a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  /* Test equal arrays */
57a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  TEST_EQ(0, SafeMemcmp("clonebob", "clonebob", 8), "SafeMemcmp() equal");
58a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  /* Inequality past end of array doesn't affect result */
59a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  TEST_EQ(0, SafeMemcmp("clonebob", "clonedan", 5), "SafeMemcmp() equal2");
60a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
61a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  TEST_EQ(1, SafeMemcmp("APPLE", "TIGER", 5), "SafeMemcmp() unequal");
62a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  TEST_EQ(1, SafeMemcmp("APPLE", "APPLe", 5), "SafeMemcmp() unequal 2");
63a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler}
64a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
65a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
66a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spanglerint main(int argc, char* argv[]) {
67a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  int error_code = 0;
68a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
69f02bbb4635175d6c3ba8f6557802f37e20160533Randall Spangler  MacrosTest();
70a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  SafeMemcmpTest();
71a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
72a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  if (!gTestSuccess)
73a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler    error_code = 255;
74a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler
75a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler  return error_code;
76a3454fcaa415b2c99514c44eebee7325fe0d1f9fRandall Spangler}
77