1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/util/protozero_to_text.h"
18
19 #include "perfetto/ext/base/string_utils.h"
20 #include "perfetto/protozero/scattered_heap_buffer.h"
21 #include "protos/perfetto/trace/track_event/chrome_compositor_scheduler_state.pbzero.h"
22 #include "protos/perfetto/trace/track_event/track_event.pbzero.h"
23 #include "src/protozero/test/example_proto/test_messages.pbzero.h"
24 #include "src/trace_processor/importers/track_event.descriptor.h"
25 #include "src/trace_processor/test_messages.descriptor.h"
26 #include "src/trace_processor/util/descriptors.h"
27 #include "test/gtest_and_gmock.h"
28
29 namespace perfetto {
30 namespace trace_processor {
31 namespace protozero_to_text {
32
33 namespace {
34
35 constexpr size_t kChunkSize = 42;
36
37 using ::protozero::test::protos::pbzero::EveryField;
38 using ::protozero::test::protos::pbzero::PackedRepeatedFields;
39 using ::testing::_;
40 using ::testing::ElementsAre;
41 using ::testing::Eq;
42 using ::testing::StartsWith;
43
TEST(ProtozeroToTextTest,TrackEventBasic)44 TEST(ProtozeroToTextTest, TrackEventBasic) {
45 using perfetto::protos::pbzero::TrackEvent;
46 protozero::HeapBuffered<TrackEvent> msg{kChunkSize, kChunkSize};
47 msg->set_track_uuid(4);
48 msg->set_timestamp_delta_us(3);
49 auto binary_proto = msg.SerializeAsArray();
50 EXPECT_EQ(
51 "track_uuid: 4\ntimestamp_delta_us: 3",
52 DebugTrackEventProtozeroToText(
53 ".perfetto.protos.TrackEvent",
54 protozero::ConstBytes{binary_proto.data(), binary_proto.size()}));
55 EXPECT_EQ(
56 "track_uuid: 4 timestamp_delta_us: 3",
57 ShortDebugTrackEventProtozeroToText(
58 ".perfetto.protos.TrackEvent",
59 protozero::ConstBytes{binary_proto.data(), binary_proto.size()}));
60 }
61
TEST(ProtozeroToTextTest,TrackEventNestedMsg)62 TEST(ProtozeroToTextTest, TrackEventNestedMsg) {
63 using perfetto::protos::pbzero::TrackEvent;
64 protozero::HeapBuffered<TrackEvent> msg{kChunkSize, kChunkSize};
65 msg->set_track_uuid(4);
66 auto* state = msg->set_cc_scheduler_state();
67 state->set_deadline_us(7);
68 auto* machine = state->set_state_machine();
69 auto* minor_state = machine->set_minor_state();
70 minor_state->set_commit_count(8);
71 state->set_observing_begin_frame_source(true);
72 msg->set_timestamp_delta_us(3);
73 auto binary_proto = msg.SerializeAsArray();
74
75 EXPECT_EQ(
76 R"(track_uuid: 4
77 cc_scheduler_state {
78 deadline_us: 7
79 state_machine {
80 minor_state {
81 commit_count: 8
82 }
83 }
84 observing_begin_frame_source: true
85 }
86 timestamp_delta_us: 3)",
87 DebugTrackEventProtozeroToText(
88 ".perfetto.protos.TrackEvent",
89 protozero::ConstBytes{binary_proto.data(), binary_proto.size()}));
90
91 EXPECT_EQ(
92 "track_uuid: 4 cc_scheduler_state { deadline_us: 7 state_machine { "
93 "minor_state { commit_count: 8 } } observing_begin_frame_source: true } "
94 "timestamp_delta_us: 3",
95 ShortDebugTrackEventProtozeroToText(
96 ".perfetto.protos.TrackEvent",
97 protozero::ConstBytes{binary_proto.data(), binary_proto.size()}));
98 }
99
TEST(ProtozeroToTextTest,TrackEventEnumNames)100 TEST(ProtozeroToTextTest, TrackEventEnumNames) {
101 using perfetto::protos::pbzero::TrackEvent;
102 protozero::HeapBuffered<TrackEvent> msg{kChunkSize, kChunkSize};
103 msg->set_type(TrackEvent::TYPE_SLICE_BEGIN);
104 auto binary_proto = msg.SerializeAsArray();
105 EXPECT_EQ(
106 "type: TYPE_SLICE_BEGIN",
107 DebugTrackEventProtozeroToText(
108 ".perfetto.protos.TrackEvent",
109 protozero::ConstBytes{binary_proto.data(), binary_proto.size()}));
110 EXPECT_EQ(
111 "type: TYPE_SLICE_BEGIN",
112 DebugTrackEventProtozeroToText(
113 ".perfetto.protos.TrackEvent",
114 protozero::ConstBytes{binary_proto.data(), binary_proto.size()}));
115 }
116
TEST(ProtozeroToTextTest,CustomDescriptorPoolBasic)117 TEST(ProtozeroToTextTest, CustomDescriptorPoolBasic) {
118 using perfetto::protos::pbzero::TrackEvent;
119 protozero::HeapBuffered<TrackEvent> msg{kChunkSize, kChunkSize};
120 msg->set_track_uuid(4);
121 msg->set_timestamp_delta_us(3);
122 auto binary_proto = msg.SerializeAsArray();
123 DescriptorPool pool;
124 auto status = pool.AddFromFileDescriptorSet(kTrackEventDescriptor.data(),
125 kTrackEventDescriptor.size());
126 ASSERT_TRUE(status.ok());
127 EXPECT_EQ("track_uuid: 4\ntimestamp_delta_us: 3",
128 ProtozeroToText(pool, ".perfetto.protos.TrackEvent", binary_proto,
129 kIncludeNewLines));
130 EXPECT_EQ("track_uuid: 4 timestamp_delta_us: 3",
131 ProtozeroToText(pool, ".perfetto.protos.TrackEvent", binary_proto,
132 kSkipNewLines));
133 }
134
TEST(ProtozeroToTextTest,CustomDescriptorPoolNestedMsg)135 TEST(ProtozeroToTextTest, CustomDescriptorPoolNestedMsg) {
136 using perfetto::protos::pbzero::TrackEvent;
137 protozero::HeapBuffered<TrackEvent> msg{kChunkSize, kChunkSize};
138 msg->set_track_uuid(4);
139 auto* state = msg->set_cc_scheduler_state();
140 state->set_deadline_us(7);
141 auto* machine = state->set_state_machine();
142 auto* minor_state = machine->set_minor_state();
143 minor_state->set_commit_count(8);
144 state->set_observing_begin_frame_source(true);
145 msg->set_timestamp_delta_us(3);
146 auto binary_proto = msg.SerializeAsArray();
147
148 DescriptorPool pool;
149 auto status = pool.AddFromFileDescriptorSet(kTrackEventDescriptor.data(),
150 kTrackEventDescriptor.size());
151 ASSERT_TRUE(status.ok());
152
153 EXPECT_EQ(
154 R"(track_uuid: 4
155 cc_scheduler_state {
156 deadline_us: 7
157 state_machine {
158 minor_state {
159 commit_count: 8
160 }
161 }
162 observing_begin_frame_source: true
163 }
164 timestamp_delta_us: 3)",
165 ProtozeroToText(pool, ".perfetto.protos.TrackEvent", binary_proto,
166 kIncludeNewLines));
167
168 EXPECT_EQ(
169 "track_uuid: 4 cc_scheduler_state { deadline_us: 7 state_machine { "
170 "minor_state { commit_count: 8 } } observing_begin_frame_source: true } "
171 "timestamp_delta_us: 3",
172 ProtozeroToText(pool, ".perfetto.protos.TrackEvent", binary_proto,
173 kSkipNewLines));
174 }
175
TEST(ProtozeroToTextTest,ProtozeroEnumToText)176 TEST(ProtozeroToTextTest, ProtozeroEnumToText) {
177 using perfetto::protos::pbzero::TrackEvent;
178 EXPECT_EQ("TYPE_SLICE_END",
179 ProtozeroEnumToText(".perfetto.protos.TrackEvent.Type",
180 TrackEvent::TYPE_SLICE_END));
181 }
182
183 // Sets up a descriptor pool with all the messages from
184 // "src/protozero/test/example_proto/test_messages.proto"
185 class ProtozeroToTextTestMessageTest : public testing::Test {
186 protected:
SetUp()187 void SetUp() override {
188 auto status = pool_.AddFromFileDescriptorSet(
189 kTestMessagesDescriptor.data(), kTestMessagesDescriptor.size());
190 ASSERT_TRUE(status.ok());
191 }
192
193 DescriptorPool pool_;
194 };
195
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntInt32)196 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntInt32) {
197 protozero::HeapBuffered<EveryField> msg;
198 msg->set_field_int32(42);
199
200 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
201 msg.SerializeAsArray(), kIncludeNewLines),
202 "field_int32: 42");
203 }
204
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntSint32)205 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntSint32) {
206 protozero::HeapBuffered<EveryField> msg;
207 msg->set_field_sint32(-42);
208
209 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
210 msg.SerializeAsArray(), kIncludeNewLines),
211 "field_sint32: -42");
212 }
213
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntUint32)214 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntUint32) {
215 protozero::HeapBuffered<EveryField> msg;
216 msg->set_field_uint32(3000000000);
217
218 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
219 msg.SerializeAsArray(), kIncludeNewLines),
220 "field_uint32: 3000000000");
221 }
222
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntInt64)223 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntInt64) {
224 protozero::HeapBuffered<EveryField> msg;
225 msg->set_field_int64(3000000000);
226
227 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
228 msg.SerializeAsArray(), kIncludeNewLines),
229 "field_int64: 3000000000");
230 }
231
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntSint64)232 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntSint64) {
233 protozero::HeapBuffered<EveryField> msg;
234 msg->set_field_sint64(-3000000000);
235
236 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
237 msg.SerializeAsArray(), kIncludeNewLines),
238 "field_sint64: -3000000000");
239 }
240
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntBool)241 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntBool) {
242 protozero::HeapBuffered<EveryField> msg;
243 msg->set_field_bool(true);
244
245 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
246 msg.SerializeAsArray(), kIncludeNewLines),
247 "field_bool: true");
248 }
249
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntSmallEnum)250 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntSmallEnum) {
251 protozero::HeapBuffered<EveryField> msg;
252 msg->set_small_enum(protozero::test::protos::pbzero::TO_BE);
253
254 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
255 msg.SerializeAsArray(), kIncludeNewLines),
256 "small_enum: TO_BE");
257 }
258
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntSignedEnum)259 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntSignedEnum) {
260 protozero::HeapBuffered<EveryField> msg;
261 msg->set_signed_enum(protozero::test::protos::pbzero::NEGATIVE);
262
263 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
264 msg.SerializeAsArray(), kIncludeNewLines),
265 "signed_enum: NEGATIVE");
266 }
267
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntBigEnum)268 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntBigEnum) {
269 protozero::HeapBuffered<EveryField> msg;
270 msg->set_big_enum(protozero::test::protos::pbzero::END);
271
272 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
273 msg.SerializeAsArray(), kIncludeNewLines),
274 "big_enum: END");
275 }
276
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntEnumUnknown)277 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntEnumUnknown) {
278 protozero::HeapBuffered<EveryField> msg;
279 msg->AppendVarInt(EveryField::kSmallEnumFieldNumber, 42);
280 ASSERT_EQ(EveryField::kSmallEnumFieldNumber, 51);
281
282 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
283 msg.SerializeAsArray(), kIncludeNewLines),
284 "51: 42");
285 }
286
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntUnknown)287 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntUnknown) {
288 protozero::HeapBuffered<EveryField> msg;
289 msg->AppendVarInt(/*field_id=*/9999, /*value=*/42);
290
291 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
292 msg.SerializeAsArray(), kIncludeNewLines),
293 "9999: 42");
294 }
295
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntMismatch)296 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntMismatch) {
297 protozero::HeapBuffered<EveryField> msg;
298 ASSERT_EQ(EveryField::kFieldStringFieldNumber, 500);
299 msg->AppendVarInt(EveryField::kFieldStringFieldNumber, 42);
300
301 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
302 msg.SerializeAsArray(), kIncludeNewLines),
303 "500: 42");
304 }
305
TEST_F(ProtozeroToTextTestMessageTest,FieldVarIntForPacked)306 TEST_F(ProtozeroToTextTestMessageTest, FieldVarIntForPacked) {
307 // Even though field_int32 has [packed = true], it still accepts a non-packed
308 // representation.
309 protozero::HeapBuffered<PackedRepeatedFields> msg;
310 msg->AppendVarInt(PackedRepeatedFields::kFieldInt32FieldNumber, 42);
311
312 EXPECT_EQ(
313 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
314 msg.SerializeAsArray(), kIncludeNewLines),
315 "field_int32: 42");
316 }
317
TEST_F(ProtozeroToTextTestMessageTest,FieldFixed32Signed)318 TEST_F(ProtozeroToTextTestMessageTest, FieldFixed32Signed) {
319 protozero::HeapBuffered<EveryField> msg;
320 msg->set_field_sfixed32(-42);
321
322 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
323 msg.SerializeAsArray(), kIncludeNewLines),
324 "field_sfixed32: -42");
325 }
326
TEST_F(ProtozeroToTextTestMessageTest,FieldFixed32Unsigned)327 TEST_F(ProtozeroToTextTestMessageTest, FieldFixed32Unsigned) {
328 protozero::HeapBuffered<EveryField> msg;
329 msg->set_field_fixed32(3000000000);
330
331 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
332 msg.SerializeAsArray(), kIncludeNewLines),
333 "field_fixed32: 3000000000");
334 }
335
TEST_F(ProtozeroToTextTestMessageTest,FieldFixed32Float)336 TEST_F(ProtozeroToTextTestMessageTest, FieldFixed32Float) {
337 protozero::HeapBuffered<EveryField> msg;
338 msg->set_field_float(24.125);
339
340 EXPECT_THAT(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
341 msg.SerializeAsArray(), kIncludeNewLines),
342 StartsWith("field_float: 24.125"));
343 }
344
TEST_F(ProtozeroToTextTestMessageTest,FieldFixed32Unknown)345 TEST_F(ProtozeroToTextTestMessageTest, FieldFixed32Unknown) {
346 protozero::HeapBuffered<EveryField> msg;
347 msg->AppendFixed<uint32_t>(/*field_id=*/9999, /*value=*/0x1);
348
349 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
350 msg.SerializeAsArray(), kIncludeNewLines),
351 "9999: 0x00000001");
352 }
353
TEST_F(ProtozeroToTextTestMessageTest,FieldFixed32Mismatch)354 TEST_F(ProtozeroToTextTestMessageTest, FieldFixed32Mismatch) {
355 protozero::HeapBuffered<EveryField> msg;
356 ASSERT_EQ(EveryField::kFieldStringFieldNumber, 500);
357 msg->AppendFixed<uint32_t>(EveryField::kFieldStringFieldNumber, 0x1);
358
359 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
360 msg.SerializeAsArray(), kIncludeNewLines),
361 "500: 0x00000001");
362 }
363
TEST_F(ProtozeroToTextTestMessageTest,FieldFixed64Signed)364 TEST_F(ProtozeroToTextTestMessageTest, FieldFixed64Signed) {
365 protozero::HeapBuffered<EveryField> msg;
366 msg->set_field_sfixed64(-42);
367
368 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
369 msg.SerializeAsArray(), kIncludeNewLines),
370 "field_sfixed64: -42");
371 }
372
TEST_F(ProtozeroToTextTestMessageTest,FieldFixed64Unsigned)373 TEST_F(ProtozeroToTextTestMessageTest, FieldFixed64Unsigned) {
374 protozero::HeapBuffered<EveryField> msg;
375 msg->set_field_fixed64(3000000000);
376
377 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
378 msg.SerializeAsArray(), kIncludeNewLines),
379 "field_fixed64: 3000000000");
380 }
381
TEST_F(ProtozeroToTextTestMessageTest,FieldFixed64Double)382 TEST_F(ProtozeroToTextTestMessageTest, FieldFixed64Double) {
383 protozero::HeapBuffered<EveryField> msg;
384 msg->set_field_double(24.125);
385
386 EXPECT_THAT(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
387 msg.SerializeAsArray(), kIncludeNewLines),
388 StartsWith("field_double: 24.125"));
389 }
390
TEST_F(ProtozeroToTextTestMessageTest,FieldFixed64Unknown)391 TEST_F(ProtozeroToTextTestMessageTest, FieldFixed64Unknown) {
392 protozero::HeapBuffered<EveryField> msg;
393 msg->AppendFixed<uint64_t>(/*field_id=*/9999, /*value=*/0x1);
394
395 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
396 msg.SerializeAsArray(), kIncludeNewLines),
397 "9999: 0x0000000000000001");
398 }
399
TEST_F(ProtozeroToTextTestMessageTest,FieldFixed64Mismatch)400 TEST_F(ProtozeroToTextTestMessageTest, FieldFixed64Mismatch) {
401 protozero::HeapBuffered<EveryField> msg;
402 ASSERT_EQ(EveryField::kFieldStringFieldNumber, 500);
403 msg->AppendFixed<uint64_t>(EveryField::kFieldStringFieldNumber, 0x1);
404
405 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
406 msg.SerializeAsArray(), kIncludeNewLines),
407 "500: 0x0000000000000001");
408 }
409
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedString)410 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedString) {
411 protozero::HeapBuffered<EveryField> msg;
412 msg->set_field_string("Hello");
413
414 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
415 msg.SerializeAsArray(), kIncludeNewLines),
416 R"(field_string: "Hello")");
417 }
418
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedBytes)419 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedBytes) {
420 protozero::HeapBuffered<EveryField> msg;
421 msg->set_field_bytes("Hello");
422
423 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
424 msg.SerializeAsArray(), kIncludeNewLines),
425 R"(field_bytes: "Hello")");
426 }
427
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedUnknown)428 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedUnknown) {
429 protozero::HeapBuffered<EveryField> msg;
430 msg->AppendString(9999, "Hello");
431
432 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
433 msg.SerializeAsArray(), kIncludeNewLines),
434 R"(9999: "Hello")");
435 }
436
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedMismatch)437 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedMismatch) {
438 protozero::HeapBuffered<EveryField> msg;
439 ASSERT_EQ(EveryField::kFieldBoolFieldNumber, 13);
440 msg->AppendString(EveryField::kFieldBoolFieldNumber, "Hello");
441
442 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
443 msg.SerializeAsArray(), kIncludeNewLines),
444 "# Packed type 8 not supported. Printing raw string.\n"
445 R"(13: "Hello")");
446 }
447
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedForNonPacked)448 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedForNonPacked) {
449 // Even though repeated_int32 doesn't have [packed = true], it still accepts a
450 // packed representation.
451 protozero::HeapBuffered<EveryField> msg;
452 protozero::PackedVarInt buf;
453 buf.Append<int32_t>(-42);
454 buf.Append<int32_t>(2147483647);
455 msg->AppendBytes(EveryField::kRepeatedInt32FieldNumber, buf.data(),
456 buf.size());
457
458 EXPECT_EQ(ProtozeroToText(pool_, ".protozero.test.protos.EveryField",
459 msg.SerializeAsArray(), kIncludeNewLines),
460 "repeated_int32: -42\nrepeated_int32: 2147483647");
461 }
462
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedVarIntInt32)463 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedVarIntInt32) {
464 protozero::HeapBuffered<PackedRepeatedFields> msg;
465 protozero::PackedVarInt buf;
466 buf.Append<int32_t>(-42);
467 buf.Append<int32_t>(2147483647);
468 msg->set_field_int32(buf);
469
470 EXPECT_EQ(
471 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
472 msg.SerializeAsArray(), kIncludeNewLines),
473 "field_int32: -42\nfield_int32: 2147483647");
474 }
475
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedVarIntInt64)476 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedVarIntInt64) {
477 protozero::HeapBuffered<PackedRepeatedFields> msg;
478 protozero::PackedVarInt buf;
479 buf.Append<int64_t>(-42);
480 buf.Append<int64_t>(3000000000);
481 msg->set_field_int64(buf);
482
483 EXPECT_EQ(
484 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
485 msg.SerializeAsArray(), kIncludeNewLines),
486 "field_int64: -42\nfield_int64: 3000000000");
487 }
488
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedVarIntUint32)489 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedVarIntUint32) {
490 protozero::HeapBuffered<PackedRepeatedFields> msg;
491 protozero::PackedVarInt buf;
492 buf.Append<uint32_t>(42);
493 buf.Append<uint32_t>(3000000000);
494 msg->set_field_uint32(buf);
495
496 EXPECT_EQ(
497 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
498 msg.SerializeAsArray(), kIncludeNewLines),
499 "field_uint32: 42\nfield_uint32: 3000000000");
500 }
501
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedVarIntUint64)502 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedVarIntUint64) {
503 protozero::HeapBuffered<PackedRepeatedFields> msg;
504 protozero::PackedVarInt buf;
505 buf.Append<uint64_t>(42);
506 buf.Append<uint64_t>(3000000000000);
507 msg->set_field_uint64(buf);
508
509 EXPECT_EQ(
510 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
511 msg.SerializeAsArray(), kIncludeNewLines),
512 "field_uint64: 42\nfield_uint64: 3000000000000");
513 }
514
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedFixed32Uint32)515 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedFixed32Uint32) {
516 protozero::HeapBuffered<PackedRepeatedFields> msg;
517 protozero::PackedFixedSizeInt<uint32_t> buf;
518 buf.Append(42);
519 buf.Append(3000000000);
520 msg->set_field_fixed32(buf);
521
522 EXPECT_EQ(
523 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
524 msg.SerializeAsArray(), kIncludeNewLines),
525 "field_fixed32: 42\nfield_fixed32: 3000000000");
526 }
527
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedFixed32Int32)528 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedFixed32Int32) {
529 protozero::HeapBuffered<PackedRepeatedFields> msg;
530 protozero::PackedFixedSizeInt<int32_t> buf;
531 buf.Append(-42);
532 buf.Append(42);
533 msg->set_field_sfixed32(buf);
534
535 EXPECT_EQ(
536 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
537 msg.SerializeAsArray(), kIncludeNewLines),
538 "field_sfixed32: -42\nfield_sfixed32: 42");
539 }
540
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedFixed32Float)541 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedFixed32Float) {
542 protozero::HeapBuffered<PackedRepeatedFields> msg;
543 protozero::PackedFixedSizeInt<float> buf;
544 buf.Append(-42);
545 buf.Append(42.125);
546 msg->set_field_float(buf);
547
548 std::string output =
549 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
550 msg.SerializeAsArray(), kIncludeNewLines);
551
552 EXPECT_THAT(base::SplitString(output, "\n"),
553 ElementsAre(StartsWith("field_float: -42"),
554 StartsWith("field_float: 42.125")));
555 }
556
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedFixed64Uint64)557 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedFixed64Uint64) {
558 protozero::HeapBuffered<PackedRepeatedFields> msg;
559 protozero::PackedFixedSizeInt<uint64_t> buf;
560 buf.Append(42);
561 buf.Append(3000000000000);
562 msg->set_field_fixed64(buf);
563
564 EXPECT_EQ(
565 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
566 msg.SerializeAsArray(), kIncludeNewLines),
567 "field_fixed64: 42\nfield_fixed64: 3000000000000");
568 }
569
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedFixed64Int64)570 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedFixed64Int64) {
571 protozero::HeapBuffered<PackedRepeatedFields> msg;
572 protozero::PackedFixedSizeInt<int64_t> buf;
573 buf.Append(-42);
574 buf.Append(3000000000000);
575 msg->set_field_sfixed64(buf);
576
577 EXPECT_EQ(
578 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
579 msg.SerializeAsArray(), kIncludeNewLines),
580 "field_sfixed64: -42\nfield_sfixed64: 3000000000000");
581 }
582
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedFixed64Double)583 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedFixed64Double) {
584 protozero::HeapBuffered<PackedRepeatedFields> msg;
585 protozero::PackedFixedSizeInt<double> buf;
586 buf.Append(-42);
587 buf.Append(42.125);
588 msg->set_field_double(buf);
589
590 EXPECT_THAT(
591 base::SplitString(
592 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
593 msg.SerializeAsArray(), kIncludeNewLines),
594 "\n"),
595 ElementsAre(StartsWith("field_double: -42"),
596 StartsWith("field_double: 42.125")));
597 }
598
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedFixedErrShort)599 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedFixedErrShort) {
600 protozero::HeapBuffered<PackedRepeatedFields> msg;
601 std::string buf;
602 buf = "\x01";
603 // buf does not contain enough data for a fixed 64
604 msg->AppendBytes(PackedRepeatedFields::kFieldFixed64FieldNumber, buf.data(),
605 buf.size());
606
607 // "protoc --decode", instead, returns an error on stderr and doesn't output
608 // anything at all.
609 EXPECT_EQ(
610 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
611 msg.SerializeAsArray(), kIncludeNewLines),
612 "# Packed decoding failure for field field_fixed64\n");
613 }
614
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedFixedGarbage)615 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedFixedGarbage) {
616 protozero::HeapBuffered<PackedRepeatedFields> msg;
617 protozero::PackedFixedSizeInt<uint64_t> buf;
618 buf.Append(42);
619 buf.Append(3000000000000);
620 std::string buf_and_garbage(reinterpret_cast<const char*>(buf.data()),
621 buf.size());
622 buf_and_garbage += "\x01";
623 // buf contains extra garbage
624 msg->AppendBytes(PackedRepeatedFields::kFieldFixed64FieldNumber,
625 buf_and_garbage.data(), buf_and_garbage.size());
626
627 // "protoc --decode", instead, returns an error on stderr and doesn't output
628 // anything at all.
629 EXPECT_EQ(
630 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
631 msg.SerializeAsArray(), kIncludeNewLines),
632 "# Packed decoding failure for field field_fixed64\n");
633 }
634
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedVarIntShort)635 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedVarIntShort) {
636 protozero::HeapBuffered<PackedRepeatedFields> msg;
637 std::string buf;
638 buf = "\xFF";
639 // for the varint to be valid, buf should contain another byte.
640 msg->AppendBytes(PackedRepeatedFields::kFieldInt32FieldNumber, buf.data(),
641 buf.size());
642
643 // "protoc --decode", instead, returns an error on stderr and doesn't output
644 // anything at all.
645 EXPECT_EQ(
646 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
647 msg.SerializeAsArray(), kIncludeNewLines),
648 "# Packed decoding failure for field field_int32\n");
649 }
650
TEST_F(ProtozeroToTextTestMessageTest,FieldLengthLimitedPackedVarIntGarbage)651 TEST_F(ProtozeroToTextTestMessageTest, FieldLengthLimitedPackedVarIntGarbage) {
652 protozero::HeapBuffered<PackedRepeatedFields> msg;
653 protozero::PackedVarInt buf;
654 buf.Append(42);
655 buf.Append(105);
656 std::string buf_and_garbage(reinterpret_cast<const char*>(buf.data()),
657 buf.size());
658 buf_and_garbage += "\xFF";
659 // buf contains extra garbage
660 msg->AppendBytes(PackedRepeatedFields::kFieldInt32FieldNumber,
661 buf_and_garbage.data(), buf_and_garbage.size());
662
663 // "protoc --decode", instead:
664 // * doesn't output anything.
665 // * returns an error on stderr.
666 EXPECT_EQ(
667 ProtozeroToText(pool_, ".protozero.test.protos.PackedRepeatedFields",
668 msg.SerializeAsArray(), kIncludeNewLines),
669 "field_int32: 42\n"
670 "field_int32: 105\n"
671 "# Packed decoding failure for field field_int32\n");
672 }
673
674 } // namespace
675 } // namespace protozero_to_text
676 } // namespace trace_processor
677 } // namespace perfetto
678