Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Adam Langley | 4851c48f | 2019-04-11 17:18:47 | [diff] [blame] | 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 "components/cbor/diagnostic_writer.h" |
Adam Langley | 08718f73 | 2019-04-22 22:21:33 | [diff] [blame] | 6 | |
Adam Langley | ec76a6e | 2024-02-21 02:13:21 | [diff] [blame] | 7 | #include "base/strings/stringprintf.h" |
Adam Langley | 08718f73 | 2019-04-22 22:21:33 | [diff] [blame] | 8 | #include "components/cbor/reader.h" |
Adam Langley | 4851c48f | 2019-04-11 17:18:47 | [diff] [blame] | 9 | #include "components/cbor/values.h" |
| 10 | |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | namespace cbor { |
| 14 | |
| 15 | TEST(CBORDiagnosticWriterTest, Basic) { |
| 16 | Value::MapValue map; |
| 17 | map.emplace(1, 1); |
| 18 | map.emplace(2, -2); |
| 19 | map.emplace(3, "test"); |
| 20 | std::vector<uint8_t> bytes = {1, 2, 3, 4}; |
| 21 | map.emplace(4, std::move(bytes)); |
| 22 | |
| 23 | Value::MapValue submap; |
| 24 | submap.emplace(5, true); |
| 25 | submap.emplace(6, false); |
| 26 | map.emplace(5, cbor::Value(submap)); |
| 27 | |
| 28 | Value::ArrayValue array; |
| 29 | array.emplace_back(1); |
| 30 | array.emplace_back(2); |
| 31 | array.emplace_back(3); |
| 32 | array.emplace_back("foo"); |
| 33 | map.emplace(6, cbor::Value(array)); |
| 34 | |
| 35 | map.emplace(7, "es\'cap\\in\ng"); |
| 36 | |
Russ Hamilton | 385541b | 2023-07-06 00:30:38 | [diff] [blame] | 37 | map.emplace(8, cbor::Value(3.14)); |
| 38 | |
Adam Langley | 4851c48f | 2019-04-11 17:18:47 | [diff] [blame] | 39 | EXPECT_EQ( |
| 40 | "{1: 1, 2: -2, 3: \"test\", 4: h'01020304', 5: {5: true, 6: false}, 6: " |
Russ Hamilton | 385541b | 2023-07-06 00:30:38 | [diff] [blame] | 41 | "[1, 2, 3, \"foo\"], 7: \"es'cap\\\\in\\ng\", 8: 3.14}", |
Adam Langley | 4851c48f | 2019-04-11 17:18:47 | [diff] [blame] | 42 | DiagnosticWriter::Write(cbor::Value(map))); |
| 43 | } |
| 44 | |
| 45 | TEST(CBORDiagnosticWriterTest, SizeLimit) { |
| 46 | Value::ArrayValue array; |
| 47 | array.emplace_back(1); |
| 48 | array.emplace_back(2); |
| 49 | array.emplace_back(3); |
| 50 | EXPECT_EQ("[1, 2, 3]", DiagnosticWriter::Write(cbor::Value(array))); |
| 51 | // A limit of zero is set, but it's only rough, so a few bytes might be |
| 52 | // produced. |
| 53 | EXPECT_LT( |
| 54 | DiagnosticWriter::Write(cbor::Value(array), /*rough_max_output_bytes=*/0) |
| 55 | .size(), |
| 56 | 3u); |
| 57 | |
| 58 | std::vector<uint8_t> bytes; |
| 59 | bytes.resize(100); |
| 60 | EXPECT_LT( |
| 61 | DiagnosticWriter::Write(cbor::Value(bytes), /*rough_max_output_bytes=*/0) |
| 62 | .size(), |
Adam Langley | ec76a6e | 2024-02-21 02:13:21 | [diff] [blame] | 63 | 20u); |
| 64 | } |
| 65 | |
| 66 | TEST(CBORDiagnosticWriterTest, LargeBytestrings) { |
| 67 | constexpr struct { |
| 68 | size_t length; |
| 69 | bool should_be_truncated; |
| 70 | } kTestCases[] = { |
| 71 | {0, false}, |
| 72 | {1, false}, |
| 73 | // Just under the 87.5% threshold. |
| 74 | {56, false}, |
| 75 | // Just over the 87.5% threshold. |
| 76 | {57, true}, |
| 77 | // 100% of the output limit. |
| 78 | {64, true}, |
| 79 | // Over the output limit. |
| 80 | {65, true}, |
| 81 | }; |
| 82 | |
| 83 | for (const auto& test : kTestCases) { |
| 84 | SCOPED_TRACE(test.length); |
| 85 | |
| 86 | Value::ArrayValue array; |
| 87 | array.emplace_back(1); |
| 88 | array.emplace_back(std::vector<uint8_t>(test.length, 0)); |
| 89 | array.emplace_back(3); |
| 90 | |
| 91 | std::string expected; |
| 92 | if (test.should_be_truncated) { |
| 93 | expected = base::StringPrintf("[1, (%zu bytes), 3]", test.length); |
| 94 | } else { |
| 95 | expected = base::StringPrintf("[1, h'%s', 3]", |
| 96 | std::string(test.length * 2, '0').c_str()); |
| 97 | } |
| 98 | |
| 99 | EXPECT_EQ(expected, |
| 100 | DiagnosticWriter::Write(cbor::Value(array), |
| 101 | /*rough_max_output_bytes=*/128)); |
| 102 | } |
Adam Langley | 4851c48f | 2019-04-11 17:18:47 | [diff] [blame] | 103 | } |
| 104 | |
Adam Langley | 08718f73 | 2019-04-22 22:21:33 | [diff] [blame] | 105 | TEST(CBORDiagnosticWriterTest, InvalidUTF8) { |
| 106 | static const uint8_t kInvalidUTF8[] = {0x62, 0xe2, 0x80}; |
| 107 | cbor::Reader::Config config; |
| 108 | config.allow_invalid_utf8 = true; |
Arthur Sonzogni | c571efb | 2024-01-26 20:26:18 | [diff] [blame] | 109 | std::optional<cbor::Value> maybe_value = |
Adam Langley | 08718f73 | 2019-04-22 22:21:33 | [diff] [blame] | 110 | cbor::Reader::Read(kInvalidUTF8, config); |
| 111 | |
| 112 | ASSERT_TRUE(maybe_value); |
| 113 | EXPECT_EQ("s'E280'", DiagnosticWriter::Write(*maybe_value)); |
| 114 | } |
| 115 | |
Adam Langley | 4851c48f | 2019-04-11 17:18:47 | [diff] [blame] | 116 | } // namespace cbor |