blob: de4556da36fbd9176f33e7ef3bd72b24012d2fc5 [file] [log] [blame]
Avi Drissman8ba1bad2022-09-13 19:22:361// Copyright 2019 The Chromium Authors
Adam Langley4851c48f2019-04-11 17:18:472// 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 Langley08718f732019-04-22 22:21:336
Adam Langleyec76a6e2024-02-21 02:13:217#include "base/strings/stringprintf.h"
Adam Langley08718f732019-04-22 22:21:338#include "components/cbor/reader.h"
Adam Langley4851c48f2019-04-11 17:18:479#include "components/cbor/values.h"
10
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace cbor {
14
15TEST(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 Hamilton385541b2023-07-06 00:30:3837 map.emplace(8, cbor::Value(3.14));
38
Adam Langley4851c48f2019-04-11 17:18:4739 EXPECT_EQ(
40 "{1: 1, 2: -2, 3: \"test\", 4: h'01020304', 5: {5: true, 6: false}, 6: "
Russ Hamilton385541b2023-07-06 00:30:3841 "[1, 2, 3, \"foo\"], 7: \"es'cap\\\\in\\ng\", 8: 3.14}",
Adam Langley4851c48f2019-04-11 17:18:4742 DiagnosticWriter::Write(cbor::Value(map)));
43}
44
45TEST(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 Langleyec76a6e2024-02-21 02:13:2163 20u);
64}
65
66TEST(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 Langley4851c48f2019-04-11 17:18:47103}
104
Adam Langley08718f732019-04-22 22:21:33105TEST(CBORDiagnosticWriterTest, InvalidUTF8) {
106 static const uint8_t kInvalidUTF8[] = {0x62, 0xe2, 0x80};
107 cbor::Reader::Config config;
108 config.allow_invalid_utf8 = true;
Arthur Sonzognic571efb2024-01-26 20:26:18109 std::optional<cbor::Value> maybe_value =
Adam Langley08718f732019-04-22 22:21:33110 cbor::Reader::Read(kInvalidUTF8, config);
111
112 ASSERT_TRUE(maybe_value);
113 EXPECT_EQ("s'E280'", DiagnosticWriter::Write(*maybe_value));
114}
115
Adam Langley4851c48f2019-04-11 17:18:47116} // namespace cbor