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