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; |
petrcermak
2016/09/08 16:13:25
Do you really need the encoded size? Isn't it esse
Primiano Tucci (use gerrit)
2016/09/09 14:15:49
no, because of the \x00 unfortunately
| |
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; | |
petrcermak
2016/09/08 16:13:25
ditto
Primiano Tucci (use gerrit)
2016/09/09 14:15:49
ditto :P
| |
53 uint32_t id; | |
54 FieldType type; | |
55 uint64_t int_value; | |
56 }; | |
57 | |
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 }; | |
31 | 75 |
32 TEST(ProtoUtilsTest, Serialization) { | 76 TEST(ProtoUtilsTest, Serialization) { |
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)); |
(...skipping 23 matching lines...) Expand all Loading... | |
64 EXPECT_EQ(1u, ZigZagEncode(-1)); | 108 EXPECT_EQ(1u, ZigZagEncode(-1)); |
65 EXPECT_EQ(2u, ZigZagEncode(1)); | 109 EXPECT_EQ(2u, ZigZagEncode(1)); |
66 EXPECT_EQ(3u, ZigZagEncode(-2)); | 110 EXPECT_EQ(3u, ZigZagEncode(-2)); |
67 EXPECT_EQ(4294967293u, ZigZagEncode(-2147483647)); | 111 EXPECT_EQ(4294967293u, ZigZagEncode(-2147483647)); |
68 EXPECT_EQ(4294967294u, ZigZagEncode(2147483647)); | 112 EXPECT_EQ(4294967294u, ZigZagEncode(2147483647)); |
69 EXPECT_EQ(std::numeric_limits<uint32_t>::max(), | 113 EXPECT_EQ(std::numeric_limits<uint32_t>::max(), |
70 ZigZagEncode(std::numeric_limits<int32_t>::min())); | 114 ZigZagEncode(std::numeric_limits<int32_t>::min())); |
71 EXPECT_EQ(std::numeric_limits<uint64_t>::max(), | 115 EXPECT_EQ(std::numeric_limits<uint64_t>::max(), |
72 ZigZagEncode(std::numeric_limits<int64_t>::min())); | 116 ZigZagEncode(std::numeric_limits<int64_t>::min())); |
73 | 117 |
74 EXPECT_VARINT32_EQ("\x00", 1, 0); | 118 for (size_t i = 0; i < arraysize(kVarIntExpectations); ++i) { |
petrcermak
2016/09/08 16:13:25
I would put this into a separate test (it seems in
Primiano Tucci (use gerrit)
2016/09/09 14:15:49
Ok restructured the tests.
| |
75 EXPECT_VARINT32_EQ("\x00", 1, 0); | 119 const VarIntExpectation& exp = kVarIntExpectations[i]; |
76 EXPECT_VARINT32_EQ("\x01", 1, 0x1); | 120 uint8_t buf[32]; |
77 EXPECT_VARINT32_EQ("\x7f", 1, 0x7F); | 121 uint8_t* res = WriteVarInt<uint64_t>(exp.int_value, buf); |
78 EXPECT_VARINT32_EQ("\xFF\x01", 2, 0xFF); | 122 ASSERT_EQ(exp.encoded_size, static_cast<size_t>(res - buf)); |
79 EXPECT_VARINT32_EQ("\xFF\x7F", 2, 0x3FFF); | 123 ASSERT_EQ(0, memcmp(buf, exp.encoded, exp.encoded_size)); |
80 EXPECT_VARINT32_EQ("\x80\x80\x01", 3, 0x4000); | |
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 | 124 |
87 EXPECT_VARINT64_EQ("\x00", 1, 0); | 125 if (exp.int_value <= std::numeric_limits<uint32_t>::max()) { |
88 EXPECT_VARINT64_EQ("\x01", 1, 0x1); | 126 uint8_t* res = WriteVarInt<uint32_t>(exp.int_value, buf); |
89 EXPECT_VARINT64_EQ("\x7f", 1, 0x7F); | 127 ASSERT_EQ(exp.encoded_size, static_cast<size_t>(res - buf)); |
90 EXPECT_VARINT64_EQ("\xFF\x01", 2, 0xFF); | 128 ASSERT_EQ(0, memcmp(buf, exp.encoded, exp.encoded_size)); |
91 EXPECT_VARINT64_EQ("\xFF\x7F", 2, 0x3FFF); | 129 } |
92 EXPECT_VARINT64_EQ("\x80\x80\x01", 3, 0x4000); | 130 } |
93 EXPECT_VARINT64_EQ("\xFF\xFF\x7F", 3, 0x1FFFFF); | |
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 | 131 |
115 uint8_t buf[kMessageLengthFieldSize]; | 132 uint8_t buf[kMessageLengthFieldSize]; |
116 | 133 |
117 WriteRedundantVarInt(0, buf); | 134 WriteRedundantVarInt(0, buf); |
118 EXPECT_EQ(0, memcmp("\x80\x80\x80\x00", buf, sizeof(buf))); | 135 EXPECT_EQ(0, memcmp("\x80\x80\x80\x00", buf, sizeof(buf))); |
119 | 136 |
120 WriteRedundantVarInt(1, buf); | 137 WriteRedundantVarInt(1, buf); |
121 EXPECT_EQ(0, memcmp("\x81\x80\x80\x00", buf, sizeof(buf))); | 138 EXPECT_EQ(0, memcmp("\x81\x80\x80\x00", buf, sizeof(buf))); |
122 | 139 |
123 WriteRedundantVarInt(0x80, buf); | 140 WriteRedundantVarInt(0x80, buf); |
124 EXPECT_EQ(0, memcmp("\x80\x81\x80\x00", buf, sizeof(buf))); | 141 EXPECT_EQ(0, memcmp("\x80\x81\x80\x00", buf, sizeof(buf))); |
125 | 142 |
126 WriteRedundantVarInt(0x332211, buf); | 143 WriteRedundantVarInt(0x332211, buf); |
127 EXPECT_EQ(0, memcmp("\x91\xC4\xCC\x01", buf, sizeof(buf))); | 144 EXPECT_EQ(0, memcmp("\x91\xC4\xCC\x01", buf, sizeof(buf))); |
128 | 145 |
129 // Largest allowed length. | 146 // Largest allowed length. |
130 WriteRedundantVarInt(0x0FFFFFFF, buf); | 147 WriteRedundantVarInt(0x0FFFFFFF, buf); |
131 EXPECT_EQ(0, memcmp("\xFF\xFF\xFF\x7F", buf, sizeof(buf))); | 148 EXPECT_EQ(0, memcmp("\xFF\xFF\xFF\x7F", buf, sizeof(buf))); |
132 } | 149 } |
133 | 150 |
151 TEST(ProtoUtilsTest, Deserialization) { | |
152 // Test VarInt decoding. | |
153 for (size_t i = 0; i < arraysize(kVarIntExpectations); ++i) { | |
154 const VarIntExpectation& exp = kVarIntExpectations[i]; | |
155 uint64_t value = 0; | |
petrcermak
2016/09/08 16:13:25
perhaps use some value that you know can't be outp
Primiano Tucci (use gerrit)
2016/09/09 14:15:49
Done.
| |
156 const uint8_t* res = ParseVarInt( | |
157 reinterpret_cast<const uint8_t*>(exp.encoded), | |
158 reinterpret_cast<const uint8_t*>(exp.encoded + exp.encoded_size), | |
159 &value); | |
160 ASSERT_EQ(reinterpret_cast<const void*>(exp.encoded + exp.encoded_size), | |
161 reinterpret_cast<const void*>(res)); | |
162 ASSERT_EQ(exp.int_value, value); | |
163 } | |
164 | |
165 // Test field parsing. | |
petrcermak
2016/09/08 16:13:25
These seem to be two separate tests, so I'd put th
Primiano Tucci (use gerrit)
2016/09/09 14:15:49
Done.
| |
166 for (size_t i = 0; i < arraysize(kFieldExpectations); ++i) { | |
167 const FieldExpectation& exp = kFieldExpectations[i]; | |
168 FieldType field_type = kFieldTypeVarInt; | |
169 uint32_t field_id = 0; | |
petrcermak
2016/09/08 16:13:25
Maybe use some values that you know can't be "retu
Primiano Tucci (use gerrit)
2016/09/09 14:15:49
Done.
| |
170 uint64_t field_intvalue = 0; | |
171 const uint8_t* res = ParseField( | |
172 reinterpret_cast<const uint8_t*>(exp.encoded), | |
173 reinterpret_cast<const uint8_t*>(exp.encoded + exp.encoded_size), | |
174 &field_id, &field_type, &field_intvalue); | |
175 ASSERT_EQ(reinterpret_cast<const void*>(exp.encoded + exp.encoded_size), | |
176 reinterpret_cast<const void*>(res)); | |
177 ASSERT_EQ(exp.id, field_id); | |
178 ASSERT_EQ(exp.type, field_type); | |
179 ASSERT_EQ(exp.int_value, field_intvalue); | |
180 } | |
181 } | |
182 | |
134 } // namespace | 183 } // namespace |
135 } // namespace proto | 184 } // namespace proto |
136 } // namespace v2 | 185 } // namespace v2 |
137 } // namespace tracing | 186 } // namespace tracing |
OLD | NEW |