1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "hex_dump.h"
18 
19 #include "globals.h"
20 
21 #include "gtest/gtest.h"
22 
23 #include <stdint.h>
24 
25 namespace art {
26 
27 #if defined(__LP64__)
28 #define ZEROPREFIX "00000000"
29 #else
30 #define ZEROPREFIX
31 #endif
32 
TEST(HexDump,OneLine)33 TEST(HexDump, OneLine) {
34   const char* test_text = "0123456789abcdef";
35   std::ostringstream oss;
36   oss << HexDump(test_text, strlen(test_text), false, "");
37   EXPECT_STREQ(oss.str().c_str(),
38                ZEROPREFIX
39                "00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66  0123456789abcdef");
40 }
41 
TEST(HexDump,MultiLine)42 TEST(HexDump, MultiLine) {
43   const char* test_text = "0123456789abcdef0123456789ABCDEF";
44   std::ostringstream oss;
45   oss << HexDump(test_text, strlen(test_text), false, "");
46   EXPECT_STREQ(oss.str().c_str(),
47                ZEROPREFIX
48                "00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66  0123456789abcdef\n"
49                ZEROPREFIX
50                "00000010: 30 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46  0123456789ABCDEF");
51 }
52 
53 uint64_t g16byte_aligned_number __attribute__ ((aligned(16)));  // NOLINT(whitespace/parens)
TEST(HexDump,ShowActualAddresses)54 TEST(HexDump, ShowActualAddresses) {
55   g16byte_aligned_number = 0x6162636465666768;
56   std::ostringstream oss;
57   oss << HexDump(&g16byte_aligned_number, 8, true, "");
58   // Compare ignoring pointer.
59   EXPECT_STREQ(oss.str().c_str() + (kBitsPerIntPtrT / 4),
60                ": 68 67 66 65 64 63 62 61                          hgfedcba        ");
61 }
62 
TEST(HexDump,Prefix)63 TEST(HexDump, Prefix) {
64   const char* test_text = "0123456789abcdef";
65   std::ostringstream oss;
66   oss << HexDump(test_text, strlen(test_text), false, "test prefix: ");
67   EXPECT_STREQ(oss.str().c_str(),
68                "test prefix: " ZEROPREFIX "00000000: 30 31 32 33 34 35 36 37 38 39 61 62 63 64 65 66  "
69                "0123456789abcdef");
70 }
71 
72 }  // namespace art
73