OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/tracing/core/proto_utils.h" | 5 #include "components/tracing/core/proto_utils.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/macros.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 namespace tracing { | 13 namespace tracing { |
13 namespace v2 { | 14 namespace v2 { |
14 namespace proto { | 15 namespace proto { |
15 namespace { | 16 namespace { |
16 | 17 |
17 template <typename T> | 18 struct VarIntExpectation { |
18 bool CheckWriteVarInt(const char* expected, size_t length, T value) { | 19 const char* encoded; |
19 uint8_t buf[32]; | 20 size_t encoded_size; |
20 uint8_t* res = WriteVarInt<T>(value, buf); | 21 uint64_t int_value; |
21 if (static_cast<size_t>(res - buf) != length) | 22 }; |
22 return false; | |
23 return memcmp(expected, buf, length) == 0; | |
24 } | |
25 | 23 |
26 #define EXPECT_VARINT32_EQ(expected, expected_length, value) \ | 24 const VarIntExpectation kVarIntExpectations[] = { |
27 EXPECT_PRED3(CheckWriteVarInt<uint32_t>, expected, expected_length, value) | 25 {"\x00", 1, 0}, |
| 26 {"\x01", 1, 0x1}, |
| 27 {"\x7f", 1, 0x7F}, |
| 28 {"\xFF\x01", 2, 0xFF}, |
| 29 {"\xFF\x7F", 2, 0x3FFF}, |
| 30 {"\x80\x80\x01", 3, 0x4000}, |
| 31 {"\xFF\xFF\x7F", 3, 0x1FFFFF}, |
| 32 {"\x80\x80\x80\x01", 4, 0x200000}, |
| 33 {"\xFF\xFF\xFF\x7F", 4, 0xFFFFFFF}, |
| 34 {"\x80\x80\x80\x80\x01", 5, 0x10000000}, |
| 35 {"\xFF\xFF\xFF\xFF\x0F", 5, 0xFFFFFFFF}, |
| 36 {"\x80\x80\x80\x80\x10", 5, 0x100000000}, |
| 37 {"\xFF\xFF\xFF\xFF\x7F", 5, 0x7FFFFFFFF}, |
| 38 {"\x80\x80\x80\x80\x80\x01", 6, 0x800000000}, |
| 39 {"\xFF\xFF\xFF\xFF\xFF\x7F", 6, 0x3FFFFFFFFFF}, |
| 40 {"\x80\x80\x80\x80\x80\x80\x01", 7, 0x40000000000}, |
| 41 {"\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 7, 0x1FFFFFFFFFFFF}, |
| 42 {"\x80\x80\x80\x80\x80\x80\x80\x01", 8, 0x2000000000000}, |
| 43 {"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 8, 0xFFFFFFFFFFFFFF}, |
| 44 {"\x80\x80\x80\x80\x80\x80\x80\x80\x01", 9, 0x100000000000000}, |
| 45 {"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 9, 0x7FFFFFFFFFFFFFFF}, |
| 46 {"\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 10, 0x8000000000000000}, |
| 47 {"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01", 10, 0xFFFFFFFFFFFFFFFF}, |
| 48 }; |
28 | 49 |
29 #define EXPECT_VARINT64_EQ(expected, expected_length, value) \ | 50 struct FieldExpectation { |
30 EXPECT_PRED3(CheckWriteVarInt<uint64_t>, expected, expected_length, value) | 51 const char* encoded; |
| 52 size_t encoded_size; |
| 53 uint32_t id; |
| 54 FieldType type; |
| 55 uint64_t int_value; |
| 56 }; |
31 | 57 |
32 TEST(ProtoUtilsTest, Serialization) { | 58 const FieldExpectation kFieldExpectations[] = { |
| 59 {"\x08\x00", 2, 1, kFieldTypeVarInt, 0}, |
| 60 {"\x08\x42", 2, 1, kFieldTypeVarInt, 0x42}, |
| 61 {"\xF8\x07\x42", 3, 127, kFieldTypeVarInt, 0x42}, |
| 62 {"\x90\x4D\xFF\xFF\xFF\xFF\x0F", 7, 1234, kFieldTypeVarInt, 0xFFFFFFFF}, |
| 63 {"\x7D\x42\x00\x00\x00", 5, 15, kFieldTypeFixed32, 0x42}, |
| 64 {"\x95\x4D\x78\x56\x34\x12", 6, 1234, kFieldTypeFixed32, 0x12345678}, |
| 65 {"\x79\x42\x00\x00\x00\x00\x00\x00\x00", 9, 15, kFieldTypeFixed64, 0x42}, |
| 66 {"\x91\x4D\x08\x07\x06\x05\x04\x03\x02\x01", 10, 1234, kFieldTypeFixed64, |
| 67 0x0102030405060708}, |
| 68 {"\x0A\x00", 2, 1, kFieldTypeLengthDelimited, 0}, |
| 69 {"\x0A\x04|abc", 6, 1, kFieldTypeLengthDelimited, 4}, |
| 70 {"\x92\x4D\x04|abc", 7, 1234, kFieldTypeLengthDelimited, 4}, |
| 71 {"\x92\x4D\x83\x01|abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd" |
| 72 "efghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx", |
| 73 135, 1234, kFieldTypeLengthDelimited, 131}, |
| 74 }; |
| 75 |
| 76 TEST(ProtoUtilsTest, FieldPreambleEncoding) { |
33 // According to C++ standard, right shift of negative value has | 77 // According to C++ standard, right shift of negative value has |
34 // implementation-defined resulting value. | 78 // implementation-defined resulting value. |
35 if ((static_cast<int32_t>(0x80000000u) >> 31) != -1) | 79 if ((static_cast<int32_t>(0x80000000u) >> 31) != -1) |
36 FAIL() << "Platform has unsupported negative number format or arithmetic"; | 80 FAIL() << "Platform has unsupported negative number format or arithmetic"; |
37 | 81 |
38 EXPECT_EQ(0x08u, MakeTagVarInt(1)); | 82 EXPECT_EQ(0x08u, MakeTagVarInt(1)); |
39 EXPECT_EQ(0x09u, MakeTagFixed<uint64_t>(1)); | 83 EXPECT_EQ(0x09u, MakeTagFixed<uint64_t>(1)); |
40 EXPECT_EQ(0x0Au, MakeTagLengthDelimited(1)); | 84 EXPECT_EQ(0x0Au, MakeTagLengthDelimited(1)); |
41 EXPECT_EQ(0x0Du, MakeTagFixed<uint32_t>(1)); | 85 EXPECT_EQ(0x0Du, MakeTagFixed<uint32_t>(1)); |
42 | 86 |
43 EXPECT_EQ(0x03F8u, MakeTagVarInt(0x7F)); | 87 EXPECT_EQ(0x03F8u, MakeTagVarInt(0x7F)); |
44 EXPECT_EQ(0x03F9u, MakeTagFixed<int64_t>(0x7F)); | 88 EXPECT_EQ(0x03F9u, MakeTagFixed<int64_t>(0x7F)); |
45 EXPECT_EQ(0x03FAu, MakeTagLengthDelimited(0x7F)); | 89 EXPECT_EQ(0x03FAu, MakeTagLengthDelimited(0x7F)); |
46 EXPECT_EQ(0x03FDu, MakeTagFixed<int32_t>(0x7F)); | 90 EXPECT_EQ(0x03FDu, MakeTagFixed<int32_t>(0x7F)); |
47 | 91 |
48 EXPECT_EQ(0x0400u, MakeTagVarInt(0x80)); | 92 EXPECT_EQ(0x0400u, MakeTagVarInt(0x80)); |
49 EXPECT_EQ(0x0401u, MakeTagFixed<double>(0x80)); | 93 EXPECT_EQ(0x0401u, MakeTagFixed<double>(0x80)); |
50 EXPECT_EQ(0x0402u, MakeTagLengthDelimited(0x80)); | 94 EXPECT_EQ(0x0402u, MakeTagLengthDelimited(0x80)); |
51 EXPECT_EQ(0x0405u, MakeTagFixed<float>(0x80)); | 95 EXPECT_EQ(0x0405u, MakeTagFixed<float>(0x80)); |
52 | 96 |
53 EXPECT_EQ(0x01FFF8u, MakeTagVarInt(0x3fff)); | 97 EXPECT_EQ(0x01FFF8u, MakeTagVarInt(0x3fff)); |
54 EXPECT_EQ(0x01FFF9u, MakeTagFixed<int64_t>(0x3fff)); | 98 EXPECT_EQ(0x01FFF9u, MakeTagFixed<int64_t>(0x3fff)); |
55 EXPECT_EQ(0x01FFFAu, MakeTagLengthDelimited(0x3fff)); | 99 EXPECT_EQ(0x01FFFAu, MakeTagLengthDelimited(0x3fff)); |
56 EXPECT_EQ(0x01FFFDu, MakeTagFixed<int32_t>(0x3fff)); | 100 EXPECT_EQ(0x01FFFDu, MakeTagFixed<int32_t>(0x3fff)); |
57 | 101 |
58 EXPECT_EQ(0x020000u, MakeTagVarInt(0x4000)); | 102 EXPECT_EQ(0x020000u, MakeTagVarInt(0x4000)); |
59 EXPECT_EQ(0x020001u, MakeTagFixed<int64_t>(0x4000)); | 103 EXPECT_EQ(0x020001u, MakeTagFixed<int64_t>(0x4000)); |
60 EXPECT_EQ(0x020002u, MakeTagLengthDelimited(0x4000)); | 104 EXPECT_EQ(0x020002u, MakeTagLengthDelimited(0x4000)); |
61 EXPECT_EQ(0x020005u, MakeTagFixed<int32_t>(0x4000)); | 105 EXPECT_EQ(0x020005u, MakeTagFixed<int32_t>(0x4000)); |
| 106 } |
62 | 107 |
| 108 TEST(ProtoUtilsTest, ZigZagEncoding) { |
63 EXPECT_EQ(0u, ZigZagEncode(0)); | 109 EXPECT_EQ(0u, ZigZagEncode(0)); |
64 EXPECT_EQ(1u, ZigZagEncode(-1)); | 110 EXPECT_EQ(1u, ZigZagEncode(-1)); |
65 EXPECT_EQ(2u, ZigZagEncode(1)); | 111 EXPECT_EQ(2u, ZigZagEncode(1)); |
66 EXPECT_EQ(3u, ZigZagEncode(-2)); | 112 EXPECT_EQ(3u, ZigZagEncode(-2)); |
67 EXPECT_EQ(4294967293u, ZigZagEncode(-2147483647)); | 113 EXPECT_EQ(4294967293u, ZigZagEncode(-2147483647)); |
68 EXPECT_EQ(4294967294u, ZigZagEncode(2147483647)); | 114 EXPECT_EQ(4294967294u, ZigZagEncode(2147483647)); |
69 EXPECT_EQ(std::numeric_limits<uint32_t>::max(), | 115 EXPECT_EQ(std::numeric_limits<uint32_t>::max(), |
70 ZigZagEncode(std::numeric_limits<int32_t>::min())); | 116 ZigZagEncode(std::numeric_limits<int32_t>::min())); |
71 EXPECT_EQ(std::numeric_limits<uint64_t>::max(), | 117 EXPECT_EQ(std::numeric_limits<uint64_t>::max(), |
72 ZigZagEncode(std::numeric_limits<int64_t>::min())); | 118 ZigZagEncode(std::numeric_limits<int64_t>::min())); |
| 119 } |
73 | 120 |
74 EXPECT_VARINT32_EQ("\x00", 1, 0); | 121 TEST(ProtoUtilsTest, VarIntEncoding) { |
75 EXPECT_VARINT32_EQ("\x00", 1, 0); | 122 for (size_t i = 0; i < arraysize(kVarIntExpectations); ++i) { |
76 EXPECT_VARINT32_EQ("\x01", 1, 0x1); | 123 const VarIntExpectation& exp = kVarIntExpectations[i]; |
77 EXPECT_VARINT32_EQ("\x7f", 1, 0x7F); | 124 uint8_t buf[32]; |
78 EXPECT_VARINT32_EQ("\xFF\x01", 2, 0xFF); | 125 uint8_t* res = WriteVarInt<uint64_t>(exp.int_value, buf); |
79 EXPECT_VARINT32_EQ("\xFF\x7F", 2, 0x3FFF); | 126 ASSERT_EQ(exp.encoded_size, static_cast<size_t>(res - buf)); |
80 EXPECT_VARINT32_EQ("\x80\x80\x01", 3, 0x4000); | 127 ASSERT_EQ(0, memcmp(buf, exp.encoded, exp.encoded_size)); |
81 EXPECT_VARINT32_EQ("\xFF\xFF\x7F", 3, 0x1FFFFF); | |
82 EXPECT_VARINT32_EQ("\x80\x80\x80\x01", 4, 0x200000); | |
83 EXPECT_VARINT32_EQ("\xFF\xFF\xFF\x7F", 4, 0xFFFFFFF); | |
84 EXPECT_VARINT32_EQ("\x80\x80\x80\x80\x01", 5, 0x10000000); | |
85 EXPECT_VARINT32_EQ("\xFF\xFF\xFF\xFF\x0F", 5, 0xFFFFFFFF); | |
86 | 128 |
87 EXPECT_VARINT64_EQ("\x00", 1, 0); | 129 if (exp.int_value <= std::numeric_limits<uint32_t>::max()) { |
88 EXPECT_VARINT64_EQ("\x01", 1, 0x1); | 130 uint8_t* res = WriteVarInt<uint32_t>(exp.int_value, buf); |
89 EXPECT_VARINT64_EQ("\x7f", 1, 0x7F); | 131 ASSERT_EQ(exp.encoded_size, static_cast<size_t>(res - buf)); |
90 EXPECT_VARINT64_EQ("\xFF\x01", 2, 0xFF); | 132 ASSERT_EQ(0, memcmp(buf, exp.encoded, exp.encoded_size)); |
91 EXPECT_VARINT64_EQ("\xFF\x7F", 2, 0x3FFF); | 133 } |
92 EXPECT_VARINT64_EQ("\x80\x80\x01", 3, 0x4000); | 134 } |
93 EXPECT_VARINT64_EQ("\xFF\xFF\x7F", 3, 0x1FFFFF); | 135 } |
94 EXPECT_VARINT64_EQ("\x80\x80\x80\x01", 4, 0x200000); | |
95 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\x7F", 4, 0xFFFFFFF); | |
96 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x01", 5, 0x10000000); | |
97 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\x0F", 5, 0xFFFFFFFF); | |
98 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x10", 5, 0x100000000); | |
99 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\x7F", 5, 0x7FFFFFFFF); | |
100 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x80\x01", 6, 0x800000000); | |
101 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\xFF\x7F", 6, 0x3FFFFFFFFFF); | |
102 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x80\x80\x01", 7, 0x40000000000); | |
103 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 7, 0x1FFFFFFFFFFFF); | |
104 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x80\x80\x80\x01", 8, 0x2000000000000); | |
105 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 8, 0xFFFFFFFFFFFFFF); | |
106 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x80\x80\x80\x80\x01", 9, | |
107 0x100000000000000); | |
108 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x7F", 9, | |
109 0x7FFFFFFFFFFFFFFF); | |
110 EXPECT_VARINT64_EQ("\x80\x80\x80\x80\x80\x80\x80\x80\x80\x01", 10, | |
111 0x8000000000000000); | |
112 EXPECT_VARINT64_EQ("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01", 10, | |
113 0xFFFFFFFFFFFFFFFF); | |
114 | 136 |
| 137 TEST(ProtoUtilsTest, RedundantVarIntEncoding) { |
115 uint8_t buf[kMessageLengthFieldSize]; | 138 uint8_t buf[kMessageLengthFieldSize]; |
116 | 139 |
117 WriteRedundantVarInt(0, buf); | 140 WriteRedundantVarInt(0, buf); |
118 EXPECT_EQ(0, memcmp("\x80\x80\x80\x00", buf, sizeof(buf))); | 141 EXPECT_EQ(0, memcmp("\x80\x80\x80\x00", buf, sizeof(buf))); |
119 | 142 |
120 WriteRedundantVarInt(1, buf); | 143 WriteRedundantVarInt(1, buf); |
121 EXPECT_EQ(0, memcmp("\x81\x80\x80\x00", buf, sizeof(buf))); | 144 EXPECT_EQ(0, memcmp("\x81\x80\x80\x00", buf, sizeof(buf))); |
122 | 145 |
123 WriteRedundantVarInt(0x80, buf); | 146 WriteRedundantVarInt(0x80, buf); |
124 EXPECT_EQ(0, memcmp("\x80\x81\x80\x00", buf, sizeof(buf))); | 147 EXPECT_EQ(0, memcmp("\x80\x81\x80\x00", buf, sizeof(buf))); |
125 | 148 |
126 WriteRedundantVarInt(0x332211, buf); | 149 WriteRedundantVarInt(0x332211, buf); |
127 EXPECT_EQ(0, memcmp("\x91\xC4\xCC\x01", buf, sizeof(buf))); | 150 EXPECT_EQ(0, memcmp("\x91\xC4\xCC\x01", buf, sizeof(buf))); |
128 | 151 |
129 // Largest allowed length. | 152 // Largest allowed length. |
130 WriteRedundantVarInt(0x0FFFFFFF, buf); | 153 WriteRedundantVarInt(0x0FFFFFFF, buf); |
131 EXPECT_EQ(0, memcmp("\xFF\xFF\xFF\x7F", buf, sizeof(buf))); | 154 EXPECT_EQ(0, memcmp("\xFF\xFF\xFF\x7F", buf, sizeof(buf))); |
132 } | 155 } |
133 | 156 |
| 157 TEST(ProtoUtilsTest, VarIntDecoding) { |
| 158 for (size_t i = 0; i < arraysize(kVarIntExpectations); ++i) { |
| 159 const VarIntExpectation& exp = kVarIntExpectations[i]; |
| 160 uint64_t value = std::numeric_limits<uint64_t>::max(); |
| 161 const uint8_t* res = ParseVarInt( |
| 162 reinterpret_cast<const uint8_t*>(exp.encoded), |
| 163 reinterpret_cast<const uint8_t*>(exp.encoded + exp.encoded_size), |
| 164 &value); |
| 165 ASSERT_EQ(reinterpret_cast<const void*>(exp.encoded + exp.encoded_size), |
| 166 reinterpret_cast<const void*>(res)); |
| 167 ASSERT_EQ(exp.int_value, value); |
| 168 } |
| 169 } |
| 170 |
| 171 TEST(ProtoUtilsTest, FieldDecoding) { |
| 172 for (size_t i = 0; i < arraysize(kFieldExpectations); ++i) { |
| 173 const FieldExpectation& exp = kFieldExpectations[i]; |
| 174 FieldType field_type = kFieldTypeVarInt; |
| 175 uint32_t field_id = std::numeric_limits<uint32_t>::max(); |
| 176 uint64_t field_intvalue = std::numeric_limits<uint64_t>::max(); |
| 177 const uint8_t* res = ParseField( |
| 178 reinterpret_cast<const uint8_t*>(exp.encoded), |
| 179 reinterpret_cast<const uint8_t*>(exp.encoded + exp.encoded_size), |
| 180 &field_id, &field_type, &field_intvalue); |
| 181 ASSERT_EQ(reinterpret_cast<const void*>(exp.encoded + exp.encoded_size), |
| 182 reinterpret_cast<const void*>(res)); |
| 183 ASSERT_EQ(exp.id, field_id); |
| 184 ASSERT_EQ(exp.type, field_type); |
| 185 ASSERT_EQ(exp.int_value, field_intvalue); |
| 186 } |
| 187 } |
| 188 |
134 } // namespace | 189 } // namespace |
135 } // namespace proto | 190 } // namespace proto |
136 } // namespace v2 | 191 } // namespace v2 |
137 } // namespace tracing | 192 } // namespace tracing |
OLD | NEW |