1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "pw_hex_dump/hex_dump.h"
16 
17 #include <array>
18 #include <cinttypes>
19 #include <cstdint>
20 #include <cstring>
21 #include <span>
22 #include <string_view>
23 
24 #include "gtest/gtest.h"
25 #include "pw_log/log.h"
26 
27 namespace pw::dump {
28 namespace {
29 
30 std::array<const std::byte, 33> source_data = {
31     std::byte(0xa4), std::byte(0xcc), std::byte(0x32), std::byte(0x62),
32     std::byte(0x9b), std::byte(0x46), std::byte(0x38), std::byte(0x1a),
33     std::byte(0x23), std::byte(0x1a), std::byte(0x2a), std::byte(0x7a),
34     std::byte(0xbc), std::byte(0xe2), std::byte(0x40), std::byte(0xa0),
35     std::byte(0xff), std::byte(0x33), std::byte(0xe5), std::byte(0x2b),
36     std::byte(0x9e), std::byte(0x9f), std::byte(0x6b), std::byte(0x3c),
37     std::byte(0xbe), std::byte(0x9b), std::byte(0x89), std::byte(0x3c),
38     std::byte(0x7e), std::byte(0x4a), std::byte(0x7a), std::byte(0x48),
39     std::byte(0x18)};
40 
41 std::array<const std::byte, 15> short_string = {
42     std::byte('m'),
43     std::byte('y'),
44     std::byte(' '),
45     std::byte('t'),
46     std::byte('e'),
47     std::byte('s'),
48     std::byte('t'),
49     std::byte(' '),
50     std::byte('s'),
51     std::byte('t'),
52     std::byte('r'),
53     std::byte('i'),
54     std::byte('n'),
55     std::byte('g'),
56     std::byte('\n'),
57 };
58 
59 class HexDump : public ::testing::Test {
60  protected:
HexDump()61   HexDump() { dumper_ = FormattedHexDumper(dest_, default_flags_); }
62 
63   // Sufficiently large destination buffer to hold line-by-line formatted hex
64   // dump.
65   std::array<char, 256> dest_ = {0};
66   FormattedHexDumper dumper_;
67   FormattedHexDumper::Flags default_flags_ = {
68       .bytes_per_line = 16,
69       .group_every = 1,
70       .show_ascii = false,
71       .show_header = false,
72       .prefix_mode = FormattedHexDumper::AddressMode::kDisabled};
73 };
74 
75 class SmallBuffer : public ::testing::Test {
76  protected:
SmallBuffer()77   SmallBuffer() {
78     // Disable address prefix for most of the tests as it's platform-specific.
79     dumper_ = FormattedHexDumper(dest_, default_flags_);
80   }
81 
82   // Small destination buffer that should be inadequate in some cases.
83   std::array<char, 7> dest_ = {0};
84   FormattedHexDumper dumper_;
85   FormattedHexDumper::Flags default_flags_ = {
86       .bytes_per_line = 16,
87       .group_every = 1,
88       .show_ascii = false,
89       .show_header = false,
90       .prefix_mode = FormattedHexDumper::AddressMode::kDisabled};
91 };
92 
93 // On platforms where uintptr_t is 32-bit this evaluates to a 10-byte string
94 // where hex_string is prefixed with "0x". On 64-bit targets, this expands to
95 // an 18-byte string with the significant bytes are zero padded.
96 #define EXPECTED_SIGNIFICANT_BYTES(hex_string)                    \
97   sizeof(uintptr_t) == sizeof(uint64_t) ? "0x00000000" hex_string \
98                                         : "0x" hex_string
99 
TEST_F(HexDump,DumpAddr_ZeroSizeT)100 TEST_F(HexDump, DumpAddr_ZeroSizeT) {
101   constexpr const char* expected = EXPECTED_SIGNIFICANT_BYTES("00000000");
102   size_t zero = 0;
103   EXPECT_EQ(DumpAddr(dest_, zero), OkStatus());
104   EXPECT_STREQ(expected, dest_.data());
105 }
106 
TEST_F(HexDump,DumpAddr_NonzeroSizeT)107 TEST_F(HexDump, DumpAddr_NonzeroSizeT) {
108   constexpr const char* expected = EXPECTED_SIGNIFICANT_BYTES("deadbeef");
109   size_t nonzero = 0xDEADBEEF;
110   EXPECT_TRUE(DumpAddr(dest_, nonzero).ok());
111   EXPECT_STREQ(expected, dest_.data());
112 }
113 
TEST_F(HexDump,DumpAddr_ZeroPtr)114 TEST_F(HexDump, DumpAddr_ZeroPtr) {
115   constexpr const char* expected = EXPECTED_SIGNIFICANT_BYTES("00000000");
116   uintptr_t zero = 0;
117   EXPECT_TRUE(DumpAddr(dest_, reinterpret_cast<const void*>(zero)).ok());
118   EXPECT_STREQ(expected, dest_.data());
119 }
120 
TEST_F(HexDump,DumpAddr_NonzeroPtr)121 TEST_F(HexDump, DumpAddr_NonzeroPtr) {
122   constexpr const char* expected = EXPECTED_SIGNIFICANT_BYTES("deadbeef");
123   uintptr_t nonzero = 0xDEADBEEF;
124   EXPECT_TRUE(DumpAddr(dest_, reinterpret_cast<const void*>(nonzero)).ok());
125   EXPECT_STREQ(expected, dest_.data());
126 }
127 
TEST_F(HexDump,FormattedHexDump_Defaults)128 TEST_F(HexDump, FormattedHexDump_Defaults) {
129   constexpr const char* expected =
130       "a4 cc 32 62 9b 46 38 1a 23 1a 2a 7a bc e2 40 a0  ..2b.F8.#.*z..@.";
131   default_flags_.show_ascii = true;
132   dumper_ = FormattedHexDumper(dest_, default_flags_);
133   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
134   EXPECT_TRUE(dumper_.DumpLine().ok());
135   EXPECT_STREQ(expected, dest_.data());
136 }
137 
TEST_F(HexDump,FormattedHexDump_DefaultHeader)138 TEST_F(HexDump, FormattedHexDump_DefaultHeader) {
139   constexpr const char* expected =
140       " 0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f";
141 
142   default_flags_.show_header = true;
143   dumper_ = FormattedHexDumper(dest_, default_flags_);
144   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
145   EXPECT_TRUE(dumper_.DumpLine().ok());
146   EXPECT_STREQ(expected, dest_.data());
147 }
148 
TEST_F(HexDump,FormattedHexDump_DumpEntireBuffer)149 TEST_F(HexDump, FormattedHexDump_DumpEntireBuffer) {
150   constexpr size_t kTestBytesPerLine = 8;
151 
152   default_flags_.bytes_per_line = kTestBytesPerLine;
153   dumper_ = FormattedHexDumper(dest_, default_flags_);
154 
155   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
156   for (size_t i = 0; i < source_data.size(); i += kTestBytesPerLine) {
157     EXPECT_TRUE(dumper_.DumpLine().ok());
158   }
159   EXPECT_EQ(dumper_.DumpLine(), Status::ResourceExhausted());
160 }
161 
162 // This test is provided for convenience of debugging, as it actually logs the
163 // dump.
TEST_F(HexDump,FormattedHexDump_LogDump)164 TEST_F(HexDump, FormattedHexDump_LogDump) {
165   default_flags_.show_ascii = true;
166   default_flags_.show_header = true;
167   default_flags_.prefix_mode = FormattedHexDumper::AddressMode::kOffset;
168   dumper_ = FormattedHexDumper(dest_, default_flags_);
169 
170   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
171   // Dump data.
172   while (dumper_.DumpLine().ok()) {
173     PW_LOG_INFO("%s", dest_.data());
174   }
175   EXPECT_EQ(dumper_.DumpLine(), Status::ResourceExhausted());
176 }
177 
TEST_F(HexDump,FormattedHexDump_NoSpaces)178 TEST_F(HexDump, FormattedHexDump_NoSpaces) {
179   constexpr const char* expected = "a4cc32629b46381a231a2a7abce240a0";
180 
181   default_flags_.group_every = 0;
182   dumper_ = FormattedHexDumper(dest_, default_flags_);
183 
184   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
185   EXPECT_TRUE(dumper_.DumpLine().ok());
186   EXPECT_STREQ(expected, dest_.data());
187 }
188 
TEST_F(HexDump,FormattedHexDump_SetGroupEveryByte)189 TEST_F(HexDump, FormattedHexDump_SetGroupEveryByte) {
190   constexpr const char* expected =
191       "a4 cc 32 62 9b 46 38 1a 23 1a 2a 7a bc e2 40 a0";
192   default_flags_.group_every = 1;
193   dumper_ = FormattedHexDumper(dest_, default_flags_);
194   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
195   EXPECT_TRUE(dumper_.DumpLine().ok());
196   EXPECT_STREQ(expected, dest_.data());
197 }
198 
TEST_F(HexDump,FormattedHexDump_SetGroupEveryThreeBytes)199 TEST_F(HexDump, FormattedHexDump_SetGroupEveryThreeBytes) {
200   constexpr const char* expected = "a4cc32 629b46 381a23 1a2a7a bce240 a0";
201 
202   default_flags_.group_every = 3;
203   dumper_ = FormattedHexDumper(dest_, default_flags_);
204 
205   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
206   EXPECT_TRUE(dumper_.DumpLine().ok());
207   EXPECT_STREQ(expected, dest_.data());
208 }
209 
TEST_F(HexDump,FormattedHexDump_TwoLines)210 TEST_F(HexDump, FormattedHexDump_TwoLines) {
211   constexpr const char* expected1 = "a4 cc 32 62 9b 46 38 1a";
212   constexpr const char* expected2 = "23 1a 2a 7a bc e2 40 a0";
213 
214   default_flags_.bytes_per_line = 8;
215   dumper_ = FormattedHexDumper(dest_, default_flags_);
216 
217   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
218   // Dump first line.
219   EXPECT_TRUE(dumper_.DumpLine().ok());
220   EXPECT_STREQ(expected1, dest_.data());
221   // Dump second line.
222   EXPECT_TRUE(dumper_.DumpLine().ok());
223   EXPECT_STREQ(expected2, dest_.data());
224 }
225 
TEST_F(HexDump,FormattedHexDump_Ascii)226 TEST_F(HexDump, FormattedHexDump_Ascii) {
227   constexpr const char* expected1 = "6d 79 20 74 65 73 74 20  my test ";
228   constexpr const char* expected2 = "73 74 72 69 6e 67 0a     string.";
229 
230   default_flags_.bytes_per_line = 8;
231   default_flags_.show_ascii = true;
232   dumper_ = FormattedHexDumper(dest_, default_flags_);
233 
234   EXPECT_TRUE(dumper_.BeginDump(short_string).ok());
235   // Dump first line.
236   EXPECT_TRUE(dumper_.DumpLine().ok());
237   EXPECT_STREQ(expected1, dest_.data());
238   // Dump second line.
239   EXPECT_TRUE(dumper_.DumpLine().ok());
240   EXPECT_STREQ(expected2, dest_.data());
241 }
242 
TEST_F(HexDump,FormattedHexDump_AsciiHeader)243 TEST_F(HexDump, FormattedHexDump_AsciiHeader) {
244   constexpr const char* expected0 = " 0        4        Text";
245   constexpr const char* expected1 = "6d792074 65737420  my test ";
246   constexpr const char* expected2 = "73747269 6e670a    string.";
247 
248   default_flags_.bytes_per_line = 8;
249   default_flags_.group_every = 4;
250   default_flags_.show_ascii = true;
251   default_flags_.show_header = true;
252   dumper_ = FormattedHexDumper(dest_, default_flags_);
253 
254   EXPECT_TRUE(dumper_.BeginDump(short_string).ok());
255   // Dump header.
256   EXPECT_TRUE(dumper_.DumpLine().ok());
257   EXPECT_STREQ(expected0, dest_.data());
258   // Dump first line.
259   EXPECT_TRUE(dumper_.DumpLine().ok());
260   EXPECT_STREQ(expected1, dest_.data());
261   // Dump second line.
262   EXPECT_TRUE(dumper_.DumpLine().ok());
263   EXPECT_STREQ(expected2, dest_.data());
264 }
265 
TEST_F(HexDump,FormattedHexDump_AsciiHeaderGroupEvery)266 TEST_F(HexDump, FormattedHexDump_AsciiHeaderGroupEvery) {
267   constexpr const char* expected0 = " 0  1  2  3  4  5  6  7  Text";
268   constexpr const char* expected1 = "6d 79 20 74 65 73 74 20  my test ";
269   constexpr const char* expected2 = "73 74 72 69 6e 67 0a     string.";
270 
271   default_flags_.bytes_per_line = 8;
272   default_flags_.group_every = 1;
273   default_flags_.show_ascii = true;
274   default_flags_.show_header = true;
275   dumper_ = FormattedHexDumper(dest_, default_flags_);
276 
277   EXPECT_TRUE(dumper_.BeginDump(short_string).ok());
278   // Dump header.
279   EXPECT_TRUE(dumper_.DumpLine().ok());
280   EXPECT_STREQ(expected0, dest_.data());
281   // Dump first line.
282   EXPECT_TRUE(dumper_.DumpLine().ok());
283   EXPECT_STREQ(expected1, dest_.data());
284   // Dump second line.
285   EXPECT_TRUE(dumper_.DumpLine().ok());
286   EXPECT_STREQ(expected2, dest_.data());
287 }
288 
TEST_F(HexDump,FormattedHexDump_OffsetPrefix)289 TEST_F(HexDump, FormattedHexDump_OffsetPrefix) {
290   constexpr const char* expected1 = "0000";
291   constexpr const char* expected2 = "0010";
292 
293   default_flags_.bytes_per_line = 16;
294   default_flags_.prefix_mode = FormattedHexDumper::AddressMode::kOffset;
295   dumper_ = FormattedHexDumper(dest_, default_flags_);
296 
297   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
298   // Dump first line.
299   EXPECT_TRUE(dumper_.DumpLine().ok());
300   // Truncate string to only contain the offset.
301   dest_[strlen(expected1)] = '\0';
302   EXPECT_STREQ(expected1, dest_.data());
303 
304   // Dump second line.
305   EXPECT_TRUE(dumper_.DumpLine().ok());
306   // Truncate string to only contain the offset.
307   dest_[strlen(expected2)] = '\0';
308   EXPECT_STREQ(expected2, dest_.data());
309 }
310 
TEST_F(HexDump,FormattedHexDump_AbsolutePrefix)311 TEST_F(HexDump, FormattedHexDump_AbsolutePrefix) {
312   constexpr size_t kTestBytesPerLine = 16;
313   std::array<char, kHexAddrStringSize + 1> expected1;
314   std::array<char, kHexAddrStringSize + 1> expected2;
315   DumpAddr(expected1, source_data.data());
316   DumpAddr(expected2, source_data.data() + kTestBytesPerLine);
317 
318   default_flags_.bytes_per_line = kTestBytesPerLine;
319   default_flags_.prefix_mode = FormattedHexDumper::AddressMode::kAbsolute;
320   dumper_ = FormattedHexDumper(dest_, default_flags_);
321 
322   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
323   // Dump first line.
324   EXPECT_TRUE(dumper_.DumpLine().ok());
325   // Truncate string to only contain the offset.
326   dest_[kHexAddrStringSize] = '\0';
327   EXPECT_STREQ(expected1.data(), dest_.data());
328 
329   // Dump second line.
330   EXPECT_TRUE(dumper_.DumpLine().ok());
331   // Truncate string to only contain the offset.
332   dest_[kHexAddrStringSize] = '\0';
333   EXPECT_STREQ(expected2.data(), dest_.data());
334 }
335 
TEST_F(SmallBuffer,TinyHexDump)336 TEST_F(SmallBuffer, TinyHexDump) {
337   constexpr const char* expected = "a4cc32";
338 
339   default_flags_.bytes_per_line = 3;
340   default_flags_.group_every = 4;
341   dumper_ = FormattedHexDumper(dest_, default_flags_);
342 
343   EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
344   EXPECT_TRUE(dumper_.DumpLine().ok());
345   EXPECT_STREQ(expected, dest_.data());
346 }
347 
TEST_F(SmallBuffer,TooManyBytesPerLine)348 TEST_F(SmallBuffer, TooManyBytesPerLine) {
349   constexpr const char* expected = "";
350 
351   default_flags_.bytes_per_line = 13;
352   dumper_ = FormattedHexDumper(dest_, default_flags_);
353 
354   EXPECT_EQ(dumper_.BeginDump(source_data), Status::FailedPrecondition());
355   EXPECT_FALSE(dumper_.DumpLine().ok());
356   EXPECT_STREQ(expected, dest_.data());
357 }
358 
TEST_F(SmallBuffer,SpacesIncreaseBufferRequirement)359 TEST_F(SmallBuffer, SpacesIncreaseBufferRequirement) {
360   constexpr const char* expected = "";
361 
362   default_flags_.bytes_per_line = 3;
363   default_flags_.group_every = 1;
364   dumper_ = FormattedHexDumper(dest_, default_flags_);
365 
366   EXPECT_EQ(dumper_.BeginDump(source_data), Status::FailedPrecondition());
367   EXPECT_FALSE(dumper_.DumpLine().ok());
368   EXPECT_STREQ(expected, dest_.data());
369 }
370 
TEST_F(SmallBuffer,PrefixIncreasesBufferRequirement)371 TEST_F(SmallBuffer, PrefixIncreasesBufferRequirement) {
372   constexpr const char* expected = "";
373 
374   default_flags_.bytes_per_line = 3;
375   default_flags_.prefix_mode = FormattedHexDumper::AddressMode::kOffset;
376   dumper_ = FormattedHexDumper(dest_, default_flags_);
377 
378   EXPECT_EQ(dumper_.BeginDump(source_data), Status::FailedPrecondition());
379   EXPECT_FALSE(dumper_.DumpLine().ok());
380   EXPECT_STREQ(expected, dest_.data());
381 }
382 
TEST(BadBuffer,ZeroSize)383 TEST(BadBuffer, ZeroSize) {
384   char buffer[1] = {static_cast<char>(0xaf)};
385   FormattedHexDumper dumper(std::span<char>(buffer, 0));
386   EXPECT_EQ(dumper.BeginDump(source_data), Status::FailedPrecondition());
387   EXPECT_EQ(dumper.DumpLine(), Status::FailedPrecondition());
388   EXPECT_EQ(buffer[0], static_cast<char>(0xaf));
389 }
390 
TEST(BadBuffer,NullPtrDest)391 TEST(BadBuffer, NullPtrDest) {
392   FormattedHexDumper dumper;
393   EXPECT_EQ(dumper.SetLineBuffer(std::span<char>()), Status::InvalidArgument());
394   EXPECT_EQ(dumper.BeginDump(source_data), Status::FailedPrecondition());
395   EXPECT_EQ(dumper.DumpLine(), Status::FailedPrecondition());
396 }
397 
TEST(BadBuffer,NullPtrSrc)398 TEST(BadBuffer, NullPtrSrc) {
399   char buffer[24] = {static_cast<char>(0)};
400   FormattedHexDumper dumper(buffer);
401   EXPECT_EQ(dumper.BeginDump(ByteSpan(nullptr, 64)), Status::InvalidArgument());
402   // Don't actually dump nullptr in this test as it could cause a crash.
403 }
404 
405 }  // namespace
406 }  // namespace pw::dump
407