Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: components/tracing/core/proto_zero_message_unittest.cc

Issue 2228563002: Tracing V2: Proto message and utils improvements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: style Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_zero_message.h" 5 #include "components/tracing/core/proto_zero_message.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <memory> 8 #include <memory>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 for (uint32_t i = 1; i <= 128; ++i) 80 for (uint32_t i = 1; i <= 128; ++i)
81 msg->AppendBytes(i, kTestBytes, sizeof(kTestBytes)); 81 msg->AppendBytes(i, kTestBytes, sizeof(kTestBytes));
82 82
83 if (depth < ProtoZeroMessage::kMaxNestingDepth) { 83 if (depth < ProtoZeroMessage::kMaxNestingDepth) {
84 auto* nested_msg = 84 auto* nested_msg =
85 msg->BeginNestedMessage<FakeChildMessage>(1 + depth * 10); 85 msg->BeginNestedMessage<FakeChildMessage>(1 + depth * 10);
86 BuildNestedMessages(depth + 1, nested_msg); 86 BuildNestedMessages(depth + 1, nested_msg);
87 } 87 }
88 88
89 for (uint32_t i = 129; i <= 256; ++i) 89 for (uint32_t i = 129; i <= 256; ++i)
90 msg->AppendVarIntU32(i, 42); 90 msg->AppendVarInt(i, 42);
91 91
92 if ((depth & 2) == 0) 92 if ((depth & 2) == 0)
93 msg->Finalize(); 93 msg->Finalize();
94 } 94 }
95 95
96 private: 96 private:
97 std::unique_ptr<FakeScatteredBuffer> buffer_; 97 std::unique_ptr<FakeScatteredBuffer> buffer_;
98 std::unique_ptr<ScatteredStreamWriter> stream_writer_; 98 std::unique_ptr<ScatteredStreamWriter> stream_writer_;
99 std::vector<std::unique_ptr<uint8_t[]>> messages_; 99 std::vector<std::unique_ptr<uint8_t[]>> messages_;
100 size_t readback_pos_; 100 size_t readback_pos_;
101 }; 101 };
102 102
103 TEST_F(ProtoZeroMessageTest, BasicTypesNoNesting) { 103 TEST_F(ProtoZeroMessageTest, BasicTypesNoNesting) {
104 ProtoZeroMessage* msg = NewMessage(); 104 ProtoZeroMessage* msg = NewMessage();
105 msg->AppendVarIntU32(1 /* field_id */, 0); 105 msg->AppendVarInt(1 /* field_id */, 0);
106 msg->AppendVarIntU32(2 /* field_id */, std::numeric_limits<uint32_t>::max()); 106 msg->AppendVarInt(2 /* field_id */, std::numeric_limits<uint32_t>::max());
107 msg->AppendVarIntU64(3 /* field_id */, 42); 107 msg->AppendVarInt(3 /* field_id */, 42);
108 msg->AppendVarIntU64(4 /* field_id */, std::numeric_limits<uint64_t>::max()); 108 msg->AppendVarInt(4 /* field_id */, std::numeric_limits<uint64_t>::max());
109 msg->AppendFloat(5 /* field_id */, 3.1415f); 109 msg->AppendFixed(5 /* field_id */, 3.1415f /* float */);
110 msg->AppendDouble(6 /* field_id */, 3.14159265358979323846); 110 msg->AppendFixed(6 /* field_id */, 3.14159265358979323846 /* double */);
111 msg->AppendBytes(7 /* field_id */, kTestBytes, sizeof(kTestBytes)); 111 msg->AppendBytes(7 /* field_id */, kTestBytes, sizeof(kTestBytes));
112 112
113 // Field ids > 16 are expected to be varint encoded (preamble > 1 byte) 113 // Field ids > 16 are expected to be varint encoded (preamble > 1 byte)
114 msg->AppendString(257 /* field_id */, "0123456789abcdefABCDEF"); 114 msg->AppendString(257 /* field_id */, "0123456789abcdefABCDEF");
115 msg->AppendSignedVarInt(3 /* field_id */, -21);
115 116
116 EXPECT_EQ(72u, msg->Finalize()); 117 EXPECT_EQ(74u, msg->Finalize());
117 EXPECT_EQ(72u, GetNumSerializedBytes()); 118 EXPECT_EQ(74u, GetNumSerializedBytes());
118 119
119 // These lines match the serialization of the Append* calls above. 120 // These lines match the serialization of the Append* calls above.
120 ASSERT_EQ("0800", GetNextSerializedBytes(2)); 121 ASSERT_EQ("0800", GetNextSerializedBytes(2));
121 ASSERT_EQ("10FFFFFFFF0F", GetNextSerializedBytes(6)); 122 ASSERT_EQ("10FFFFFFFF0F", GetNextSerializedBytes(6));
122 ASSERT_EQ("182A", GetNextSerializedBytes(2)); 123 ASSERT_EQ("182A", GetNextSerializedBytes(2));
123 ASSERT_EQ("20FFFFFFFFFFFFFFFFFF01", GetNextSerializedBytes(11)); 124 ASSERT_EQ("20FFFFFFFFFFFFFFFFFF01", GetNextSerializedBytes(11));
124 ASSERT_EQ("2D560E4940", GetNextSerializedBytes(5)); 125 ASSERT_EQ("2D560E4940", GetNextSerializedBytes(5));
125 ASSERT_EQ("31182D4454FB210940", GetNextSerializedBytes(9)); 126 ASSERT_EQ("31182D4454FB210940", GetNextSerializedBytes(9));
126 ASSERT_EQ("3A0A00000000420142FF4200", GetNextSerializedBytes(12)); 127 ASSERT_EQ("3A0A00000000420142FF4200", GetNextSerializedBytes(12));
127 ASSERT_EQ("8A101630313233343536373839616263646566414243444546", 128 ASSERT_EQ("8A101630313233343536373839616263646566414243444546",
128 GetNextSerializedBytes(25)); 129 GetNextSerializedBytes(25));
130 ASSERT_EQ("1829", GetNextSerializedBytes(2));
129 } 131 }
130 132
131 TEST_F(ProtoZeroMessageTest, NestedMessagesSimple) { 133 TEST_F(ProtoZeroMessageTest, NestedMessagesSimple) {
132 ProtoZeroMessage* root_msg = NewMessage(); 134 ProtoZeroMessage* root_msg = NewMessage();
133 root_msg->AppendVarIntU32(1 /* field_id */, 1); 135 root_msg->AppendVarInt(1 /* field_id */, 1);
134 136
135 FakeChildMessage* nested_msg = 137 FakeChildMessage* nested_msg =
136 root_msg->BeginNestedMessage<FakeChildMessage>(128 /* field_id */); 138 root_msg->BeginNestedMessage<FakeChildMessage>(128 /* field_id */);
137 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(nested_msg) % sizeof(void*)); 139 ASSERT_EQ(0u, reinterpret_cast<uintptr_t>(nested_msg) % sizeof(void*));
138 nested_msg->AppendVarIntU32(2 /* field_id */, 2); 140 nested_msg->AppendVarInt(2 /* field_id */, 2);
139 141
140 nested_msg = 142 nested_msg =
141 root_msg->BeginNestedMessage<FakeChildMessage>(129 /* field_id */); 143 root_msg->BeginNestedMessage<FakeChildMessage>(129 /* field_id */);
142 nested_msg->AppendVarIntU32(4 /* field_id */, 2); 144 nested_msg->AppendVarInt(4 /* field_id */, 2);
143 145
144 root_msg->AppendVarIntU32(5 /* field_id */, 3); 146 root_msg->AppendVarInt(5 /* field_id */, 3);
145 147
146 // The expected size of the root message is supposed to be 20 bytes: 148 // The expected size of the root message is supposed to be 20 bytes:
147 // 2 bytes for the varint field (id: 1) (1 for preamble and one for payload) 149 // 2 bytes for the varint field (id: 1) (1 for preamble and one for payload)
148 // 6 bytes for the preamble of the 1st nested message (2 for id, 4 for size) 150 // 6 bytes for the preamble of the 1st nested message (2 for id, 4 for size)
149 // 2 bytes for the varint field (id: 2) of the 1st nested message 151 // 2 bytes for the varint field (id: 2) of the 1st nested message
150 // 6 bytes for the premable of the 2nd nested message 152 // 6 bytes for the premable of the 2nd nested message
151 // 2 bytes for the varint field (id: 4) of the 2nd nested message. 153 // 2 bytes for the varint field (id: 4) of the 2nd nested message.
152 // 2 bytes for the last varint (id : 5) field of the root message. 154 // 2 bytes for the last varint (id : 5) field of the root message.
153 // Test also that finalization is idempontent and Finalize() can be safely 155 // Test also that finalization is idempontent and Finalize() can be safely
154 // called more than once without side effects. 156 // called more than once without side effects.
(...skipping 13 matching lines...) Expand all
168 ASSERT_EQ("2803", GetNextSerializedBytes(2)); 170 ASSERT_EQ("2803", GetNextSerializedBytes(2));
169 } 171 }
170 172
171 // Checks that the size field of root and nested messages is properly written 173 // Checks that the size field of root and nested messages is properly written
172 // on finalization. 174 // on finalization.
173 TEST_F(ProtoZeroMessageTest, BackfillSizeOnFinalization) { 175 TEST_F(ProtoZeroMessageTest, BackfillSizeOnFinalization) {
174 ProtoZeroMessage* root_msg = NewMessage(); 176 ProtoZeroMessage* root_msg = NewMessage();
175 uint8_t root_msg_size[proto::kMessageLengthFieldSize] = {}; 177 uint8_t root_msg_size[proto::kMessageLengthFieldSize] = {};
176 root_msg->set_size_field( 178 root_msg->set_size_field(
177 {&root_msg_size[0], &root_msg_size[proto::kMessageLengthFieldSize]}); 179 {&root_msg_size[0], &root_msg_size[proto::kMessageLengthFieldSize]});
178 root_msg->AppendVarIntU32(1, 0x42); 180 root_msg->AppendVarInt(1, 0x42);
179 181
180 FakeChildMessage* nested_msg_1 = 182 FakeChildMessage* nested_msg_1 =
181 root_msg->BeginNestedMessage<FakeChildMessage>(2); 183 root_msg->BeginNestedMessage<FakeChildMessage>(2);
182 nested_msg_1->AppendVarIntU32(3, 0x43); 184 nested_msg_1->AppendVarInt(3, 0x43);
183 185
184 FakeChildMessage* nested_msg_2 = 186 FakeChildMessage* nested_msg_2 =
185 nested_msg_1->BeginNestedMessage<FakeChildMessage>(4); 187 nested_msg_1->BeginNestedMessage<FakeChildMessage>(4);
186 uint8_t buf200[200]; 188 uint8_t buf200[200];
187 memset(buf200, 0x42, sizeof(buf200)); 189 memset(buf200, 0x42, sizeof(buf200));
188 nested_msg_2->AppendBytes(5, buf200, sizeof(buf200)); 190 nested_msg_2->AppendBytes(5, buf200, sizeof(buf200));
189 191
190 root_msg->inc_size_already_written(6); 192 root_msg->inc_size_already_written(6);
191 193
192 // The value returned by Finalize() should be == the full size of |root_msg|. 194 // The value returned by Finalize() should be == the full size of |root_msg|.
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 #endif 296 #endif
295 297
296 // Test also the behavior of handle with non-root (nested) messages. 298 // Test also the behavior of handle with non-root (nested) messages.
297 299
298 ContiguousMemoryRange size_msg_2; 300 ContiguousMemoryRange size_msg_2;
299 { 301 {
300 auto* nested_msg_1 = NewMessage()->BeginNestedMessage<FakeChildMessage>(3); 302 auto* nested_msg_1 = NewMessage()->BeginNestedMessage<FakeChildMessage>(3);
301 ProtoZeroMessageHandle<FakeChildMessage> child_handle_1(nested_msg_1); 303 ProtoZeroMessageHandle<FakeChildMessage> child_handle_1(nested_msg_1);
302 ContiguousMemoryRange size_msg_1 = nested_msg_1->size_field(); 304 ContiguousMemoryRange size_msg_1 = nested_msg_1->size_field();
303 memset(size_msg_1.begin, 0, size_msg_1.size()); 305 memset(size_msg_1.begin, 0, size_msg_1.size());
304 child_handle_1->AppendVarIntU32(1, 0x11); 306 child_handle_1->AppendVarInt(1, 0x11);
305 307
306 auto* nested_msg_2 = NewMessage()->BeginNestedMessage<FakeChildMessage>(2); 308 auto* nested_msg_2 = NewMessage()->BeginNestedMessage<FakeChildMessage>(2);
307 size_msg_2 = nested_msg_2->size_field(); 309 size_msg_2 = nested_msg_2->size_field();
308 memset(size_msg_2.begin, 0, size_msg_2.size()); 310 memset(size_msg_2.begin, 0, size_msg_2.size());
309 ProtoZeroMessageHandle<FakeChildMessage> child_handle_2(nested_msg_2); 311 ProtoZeroMessageHandle<FakeChildMessage> child_handle_2(nested_msg_2);
310 child_handle_2->AppendVarIntU32(2, 0xFF); 312 child_handle_2->AppendVarInt(2, 0xFF);
311 313
312 // |nested_msg_1| should not be finalized yet. 314 // |nested_msg_1| should not be finalized yet.
313 ASSERT_EQ(0u, size_msg_1.begin[0]); 315 ASSERT_EQ(0u, size_msg_1.begin[0]);
314 316
315 // This move should cause |nested_msg_1| to be finalized, but not 317 // This move should cause |nested_msg_1| to be finalized, but not
316 // |nested_msg_2|, which will be finalized only after the current scope. 318 // |nested_msg_2|, which will be finalized only after the current scope.
317 child_handle_1 = std::move(child_handle_2); 319 child_handle_1 = std::move(child_handle_2);
318 ASSERT_EQ(0x82u, size_msg_1.begin[0]); 320 ASSERT_EQ(0x82u, size_msg_1.begin[0]);
319 ASSERT_EQ(0u, size_msg_2.begin[0]); 321 ASSERT_EQ(0u, size_msg_2.begin[0]);
320 } 322 }
321 ASSERT_EQ(0x83u, size_msg_2.begin[0]); 323 ASSERT_EQ(0x83u, size_msg_2.begin[0]);
322 } 324 }
323 325
324 } // namespace v2 326 } // namespace v2
325 } // namespace tracing 327 } // namespace tracing
OLDNEW
« components/tracing/core/proto_utils.h ('K') | « components/tracing/core/proto_zero_message.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698