aboutsummaryrefslogtreecommitdiff
path: root/pw_hex_dump/hex_dump_test.cc
blob: 1b102515e0ddd6295f0020610ac95698a1463a5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include "pw_hex_dump/hex_dump.h"

#include <array>
#include <cinttypes>
#include <cstdint>
#include <cstring>
#include <span>
#include <string_view>

#include "gtest/gtest.h"
#include "pw_log/log.h"

namespace pw::dump {
namespace {

std::array<const std::byte, 33> source_data = {
    std::byte(0xa4), std::byte(0xcc), std::byte(0x32), std::byte(0x62),
    std::byte(0x9b), std::byte(0x46), std::byte(0x38), std::byte(0x1a),
    std::byte(0x23), std::byte(0x1a), std::byte(0x2a), std::byte(0x7a),
    std::byte(0xbc), std::byte(0xe2), std::byte(0x40), std::byte(0xa0),
    std::byte(0xff), std::byte(0x33), std::byte(0xe5), std::byte(0x2b),
    std::byte(0x9e), std::byte(0x9f), std::byte(0x6b), std::byte(0x3c),
    std::byte(0xbe), std::byte(0x9b), std::byte(0x89), std::byte(0x3c),
    std::byte(0x7e), std::byte(0x4a), std::byte(0x7a), std::byte(0x48),
    std::byte(0x18)};

std::array<const std::byte, 15> short_string = {
    std::byte('m'),
    std::byte('y'),
    std::byte(' '),
    std::byte('t'),
    std::byte('e'),
    std::byte('s'),
    std::byte('t'),
    std::byte(' '),
    std::byte('s'),
    std::byte('t'),
    std::byte('r'),
    std::byte('i'),
    std::byte('n'),
    std::byte('g'),
    std::byte('\n'),
};

class HexDump : public ::testing::Test {
 protected:
  HexDump() { dumper_ = FormattedHexDumper(dest_, default_flags_); }

  // Sufficiently large destination buffer to hold line-by-line formatted hex
  // dump.
  std::array<char, 256> dest_ = {0};
  FormattedHexDumper dumper_;
  FormattedHexDumper::Flags default_flags_ = {
      .bytes_per_line = 16,
      .group_every = 1,
      .show_ascii = false,
      .show_header = false,
      .prefix_mode = FormattedHexDumper::AddressMode::kDisabled};
};

class SmallBuffer : public ::testing::Test {
 protected:
  SmallBuffer() {
    // Disable address prefix for most of the tests as it's platform-specific.
    dumper_ = FormattedHexDumper(dest_, default_flags_);
  }

  // Small destination buffer that should be inadequate in some cases.
  std::array<char, 7> dest_ = {0};
  FormattedHexDumper dumper_;
  FormattedHexDumper::Flags default_flags_ = {
      .bytes_per_line = 16,
      .group_every = 1,
      .show_ascii = false,
      .show_header = false,
      .prefix_mode = FormattedHexDumper::AddressMode::kDisabled};
};

// On platforms where uintptr_t is 32-bit this evaluates to a 10-byte string
// where hex_string is prefixed with "0x". On 64-bit targets, this expands to
// an 18-byte string with the significant bytes are zero padded.
#define EXPECTED_SIGNIFICANT_BYTES(hex_string)                    \
  sizeof(uintptr_t) == sizeof(uint64_t) ? "0x00000000" hex_string \
                                        : "0x" hex_string

TEST_F(HexDump, DumpAddr_ZeroSizeT) {
  constexpr const char* expected = EXPECTED_SIGNIFICANT_BYTES("00000000");
  size_t zero = 0;
  EXPECT_EQ(DumpAddr(dest_, zero), Status::Ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(HexDump, DumpAddr_NonzeroSizeT) {
  constexpr const char* expected = EXPECTED_SIGNIFICANT_BYTES("deadbeef");
  size_t nonzero = 0xDEADBEEF;
  EXPECT_TRUE(DumpAddr(dest_, nonzero).ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(HexDump, DumpAddr_ZeroPtr) {
  constexpr const char* expected = EXPECTED_SIGNIFICANT_BYTES("00000000");
  uintptr_t zero = 0;
  EXPECT_TRUE(DumpAddr(dest_, reinterpret_cast<const void*>(zero)).ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(HexDump, DumpAddr_NonzeroPtr) {
  constexpr const char* expected = EXPECTED_SIGNIFICANT_BYTES("deadbeef");
  uintptr_t nonzero = 0xDEADBEEF;
  EXPECT_TRUE(DumpAddr(dest_, reinterpret_cast<const void*>(nonzero)).ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_Defaults) {
  constexpr const char* expected =
      "a4 cc 32 62 9b 46 38 1a 23 1a 2a 7a bc e2 40 a0  ..2b.F8.#.*z..@.";
  default_flags_.show_ascii = true;
  dumper_ = FormattedHexDumper(dest_, default_flags_);
  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_DefaultHeader) {
  constexpr const char* expected =
      " 0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f";

  default_flags_.show_header = true;
  dumper_ = FormattedHexDumper(dest_, default_flags_);
  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_DumpEntireBuffer) {
  constexpr size_t kTestBytesPerLine = 8;

  default_flags_.bytes_per_line = kTestBytesPerLine;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  for (size_t i = 0; i < source_data.size(); i += kTestBytesPerLine) {
    EXPECT_TRUE(dumper_.DumpLine().ok());
  }
  EXPECT_EQ(dumper_.DumpLine(), Status::ResourceExhausted());
}

// This test is provided for convenience of debugging, as it actually logs the
// dump.
TEST_F(HexDump, FormattedHexDump_LogDump) {
  default_flags_.show_ascii = true;
  default_flags_.show_header = true;
  default_flags_.prefix_mode = FormattedHexDumper::AddressMode::kOffset;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  // Dump data.
  while (dumper_.DumpLine().ok()) {
    PW_LOG_INFO("%s", dest_.data());
  }
  EXPECT_EQ(dumper_.DumpLine(), Status::ResourceExhausted());
}

TEST_F(HexDump, FormattedHexDump_NoSpaces) {
  constexpr const char* expected = "a4cc32629b46381a231a2a7abce240a0";

  default_flags_.group_every = 0;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_SetGroupEveryByte) {
  constexpr const char* expected =
      "a4 cc 32 62 9b 46 38 1a 23 1a 2a 7a bc e2 40 a0";
  default_flags_.group_every = 1;
  dumper_ = FormattedHexDumper(dest_, default_flags_);
  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_SetGroupEveryThreeBytes) {
  constexpr const char* expected = "a4cc32 629b46 381a23 1a2a7a bce240 a0";

  default_flags_.group_every = 3;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_TwoLines) {
  constexpr const char* expected1 = "a4 cc 32 62 9b 46 38 1a";
  constexpr const char* expected2 = "23 1a 2a 7a bc e2 40 a0";

  default_flags_.bytes_per_line = 8;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  // Dump first line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected1, dest_.data());
  // Dump second line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected2, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_Ascii) {
  constexpr const char* expected1 = "6d 79 20 74 65 73 74 20  my test ";
  constexpr const char* expected2 = "73 74 72 69 6e 67 0a     string.";

  default_flags_.bytes_per_line = 8;
  default_flags_.show_ascii = true;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(short_string).ok());
  // Dump first line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected1, dest_.data());
  // Dump second line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected2, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_AsciiHeader) {
  constexpr const char* expected0 = " 0        4        Text";
  constexpr const char* expected1 = "6d792074 65737420  my test ";
  constexpr const char* expected2 = "73747269 6e670a    string.";

  default_flags_.bytes_per_line = 8;
  default_flags_.group_every = 4;
  default_flags_.show_ascii = true;
  default_flags_.show_header = true;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(short_string).ok());
  // Dump header.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected0, dest_.data());
  // Dump first line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected1, dest_.data());
  // Dump second line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected2, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_AsciiHeaderGroupEvery) {
  constexpr const char* expected0 = " 0  1  2  3  4  5  6  7  Text";
  constexpr const char* expected1 = "6d 79 20 74 65 73 74 20  my test ";
  constexpr const char* expected2 = "73 74 72 69 6e 67 0a     string.";

  default_flags_.bytes_per_line = 8;
  default_flags_.group_every = 1;
  default_flags_.show_ascii = true;
  default_flags_.show_header = true;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(short_string).ok());
  // Dump header.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected0, dest_.data());
  // Dump first line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected1, dest_.data());
  // Dump second line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected2, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_OffsetPrefix) {
  constexpr const char* expected1 = "0000";
  constexpr const char* expected2 = "0010";

  default_flags_.bytes_per_line = 16;
  default_flags_.prefix_mode = FormattedHexDumper::AddressMode::kOffset;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  // Dump first line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  // Truncate string to only contain the offset.
  dest_[strlen(expected1)] = '\0';
  EXPECT_STREQ(expected1, dest_.data());

  // Dump second line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  // Truncate string to only contain the offset.
  dest_[strlen(expected2)] = '\0';
  EXPECT_STREQ(expected2, dest_.data());
}

TEST_F(HexDump, FormattedHexDump_AbsolutePrefix) {
  constexpr size_t kTestBytesPerLine = 16;
  std::array<char, kHexAddrStringSize + 1> expected1;
  std::array<char, kHexAddrStringSize + 1> expected2;
  DumpAddr(expected1, source_data.data());
  DumpAddr(expected2, source_data.data() + kTestBytesPerLine);

  default_flags_.bytes_per_line = kTestBytesPerLine;
  default_flags_.prefix_mode = FormattedHexDumper::AddressMode::kAbsolute;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  // Dump first line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  // Truncate string to only contain the offset.
  dest_[kHexAddrStringSize] = '\0';
  EXPECT_STREQ(expected1.data(), dest_.data());

  // Dump second line.
  EXPECT_TRUE(dumper_.DumpLine().ok());
  // Truncate string to only contain the offset.
  dest_[kHexAddrStringSize] = '\0';
  EXPECT_STREQ(expected2.data(), dest_.data());
}

TEST_F(SmallBuffer, TinyHexDump) {
  constexpr const char* expected = "a4cc32";

  default_flags_.bytes_per_line = 3;
  default_flags_.group_every = 4;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_TRUE(dumper_.BeginDump(source_data).ok());
  EXPECT_TRUE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(SmallBuffer, TooManyBytesPerLine) {
  constexpr const char* expected = "";

  default_flags_.bytes_per_line = 13;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_EQ(dumper_.BeginDump(source_data), Status::FailedPrecondition());
  EXPECT_FALSE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(SmallBuffer, SpacesIncreaseBufferRequirement) {
  constexpr const char* expected = "";

  default_flags_.bytes_per_line = 3;
  default_flags_.group_every = 1;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_EQ(dumper_.BeginDump(source_data), Status::FailedPrecondition());
  EXPECT_FALSE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST_F(SmallBuffer, PrefixIncreasesBufferRequirement) {
  constexpr const char* expected = "";

  default_flags_.bytes_per_line = 3;
  default_flags_.prefix_mode = FormattedHexDumper::AddressMode::kOffset;
  dumper_ = FormattedHexDumper(dest_, default_flags_);

  EXPECT_EQ(dumper_.BeginDump(source_data), Status::FailedPrecondition());
  EXPECT_FALSE(dumper_.DumpLine().ok());
  EXPECT_STREQ(expected, dest_.data());
}

TEST(BadBuffer, ZeroSize) {
  char buffer[1] = {static_cast<char>(0xaf)};
  FormattedHexDumper dumper(std::span<char>(buffer, 0));
  EXPECT_EQ(dumper.BeginDump(source_data), Status::FailedPrecondition());
  EXPECT_EQ(dumper.DumpLine(), Status::FailedPrecondition());
  EXPECT_EQ(buffer[0], static_cast<char>(0xaf));
}

TEST(BadBuffer, NullPtrDest) {
  FormattedHexDumper dumper;
  EXPECT_EQ(dumper.SetLineBuffer(std::span<char>()), Status::InvalidArgument());
  EXPECT_EQ(dumper.BeginDump(source_data), Status::FailedPrecondition());
  EXPECT_EQ(dumper.DumpLine(), Status::FailedPrecondition());
}

TEST(BadBuffer, NullPtrSrc) {
  char buffer[24] = {static_cast<char>(0)};
  FormattedHexDumper dumper(buffer);
  EXPECT_EQ(dumper.BeginDump(ByteSpan(nullptr, 64)), Status::InvalidArgument());
  // Don't actually dump nullptr in this test as it could cause a crash.
}

}  // namespace
}  // namespace pw::dump