1 // Copyright (c) 2012 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 
5 #include "binary_data_utils.h"
6 #include "compat/test.h"
7 
8 namespace quipper {
9 
10 namespace {
11 
12 const size_t kHexArraySize = 8;
13 
14 }  // namespace
15 
TEST(BinaryDataUtilsTest,MD5)16 TEST(BinaryDataUtilsTest, MD5) {
17   ASSERT_EQ(Md5Prefix(""), 0xd41d8cd98f00b204LL);
18   ASSERT_EQ(Md5Prefix("The quick brown fox jumps over the lazy dog."),
19             0xe4d909c290d0fb1cLL);
20 }
21 
TEST(BinaryDataUtilsTest,Align)22 TEST(BinaryDataUtilsTest, Align) {
23   EXPECT_EQ(12, Align<4>(10));
24   EXPECT_EQ(12, Align<4>(12));
25   EXPECT_EQ(16, Align<4>(13));
26   EXPECT_EQ(100, Align<4>(97));
27   EXPECT_EQ(100, Align<4>(100));
28   EXPECT_EQ(104, Align<8>(100));
29   EXPECT_EQ(112, Align<8>(108));
30   EXPECT_EQ(112, Align<8>(112));
31 
32   EXPECT_EQ(12, Align<uint32_t>(10));
33   EXPECT_EQ(112, Align<uint64_t>(112));
34 }
35 
TEST(BinaryDataUtilsTest,RawDataToHexString)36 TEST(BinaryDataUtilsTest, RawDataToHexString) {
37   u8 hex_number[kHexArraySize];
38   // Generate a sequence of bytes and check its hex string representation.
39   for (size_t i = 0; i < arraysize(hex_number); ++i) hex_number[i] = i << i;
40   EXPECT_EQ("0002081840a08080",
41             RawDataToHexString(hex_number, arraysize(hex_number)));
42 
43   // Change the first and last bytes and check the new hex string.
44   hex_number[0] = 0x8f;
45   hex_number[arraysize(hex_number) - 1] = 0x64;
46   EXPECT_EQ("8f02081840a08064",
47             RawDataToHexString(hex_number, arraysize(hex_number)));
48 }
49 
TEST(BinaryDataUtilsTest,StringToHex)50 TEST(BinaryDataUtilsTest, StringToHex) {
51   u8 output[kHexArraySize], expected[kHexArraySize];
52 
53   // Use the same tests as in RawDataToHexString, except reversed.
54   for (size_t i = 0; i < arraysize(expected); ++i) expected[i] = i << i;
55   EXPECT_TRUE(
56       HexStringToRawData("0002081840a08080", output, arraysize(output)));
57   for (size_t i = 0; i < arraysize(expected); ++i)
58     EXPECT_EQ(expected[i], output[i]);
59 
60   expected[0] = 0x8f;
61   expected[arraysize(expected) - 1] = 0x64;
62   EXPECT_TRUE(HexStringToRawData("8f02081840a080640123456789abcdef", output,
63                                  arraysize(output)));
64   for (size_t i = 0; i < arraysize(expected); ++i)
65     EXPECT_EQ(expected[i], output[i]);
66 }
67 
68 }  // namespace quipper
69