From 334ca7d814ac354ab09fe77b56e800d1be15526f Mon Sep 17 00:00:00 2001 From: Daniele Di Proietto Date: Tue, 4 Jul 2023 14:21:11 +0000 Subject: [PATCH 1/2] shared_lib: Add helpers to serialize packed repeated fields This creates small wrappers to serialize packed repeated fields. Bug: 237053538 Change-Id: I70298ef6319392de986884abd979f89d2815a0a6 --- include/perfetto/public/BUILD.gn | 1 + include/perfetto/public/pb_macros.h | 40 ++++++- include/perfetto/public/pb_msg.h | 32 +++--- include/perfetto/public/pb_packed.h | 168 ++++++++++++++++++++++++++++ include/perfetto/public/pb_utils.h | 26 +++++ 5 files changed, 248 insertions(+), 19 deletions(-) create mode 100644 include/perfetto/public/pb_packed.h diff --git a/include/perfetto/public/BUILD.gn b/include/perfetto/public/BUILD.gn index 77b7773481..1b416f0678 100644 --- a/include/perfetto/public/BUILD.gn +++ b/include/perfetto/public/BUILD.gn @@ -28,6 +28,7 @@ source_set("public") { "pb_decoder.h", "pb_macros.h", "pb_msg.h", + "pb_packed.h", "producer.h", "stream_writer.h", "te_category_macros.h", diff --git a/include/perfetto/public/pb_macros.h b/include/perfetto/public/pb_macros.h index c45a3eec94..03724a5fdb 100644 --- a/include/perfetto/public/pb_macros.h +++ b/include/perfetto/public/pb_macros.h @@ -17,9 +17,10 @@ #ifndef INCLUDE_PERFETTO_PUBLIC_PB_MACROS_H_ #define INCLUDE_PERFETTO_PUBLIC_PB_MACROS_H_ -#include "perfetto/public/compiler.h" // IWYU pragma: export -#include "perfetto/public/pb_msg.h" // IWYU pragma: export -#include "perfetto/public/pb_utils.h" // IWYU pragma: export +#include "perfetto/public/compiler.h" // IWYU pragma: export +#include "perfetto/public/pb_msg.h" // IWYU pragma: export +#include "perfetto/public/pb_packed.h" // IWYU pragma: export +#include "perfetto/public/pb_utils.h" // IWYU pragma: export // This header contains macros that define types and accessors for protobuf // messages. @@ -173,6 +174,24 @@ PerfettoPbMsgEndNested(&msg->msg); \ } +#define PERFETTO_I_PB_FIELD_PACKED(PROTO, C_TYPE, NAME, NUM) \ + static inline void PERFETTO_I_PB_SETTER_NAME(PROTO, NAME)( \ + struct PROTO * msg, const void* data, size_t len) { \ + PerfettoPbMsgAppendType2Field( \ + &msg->msg, NUM, PERFETTO_STATIC_CAST(const uint8_t*, data), len); \ + } \ + static inline void PERFETTO_I_PB_SETTER_BEGIN_NAME(PROTO, NAME)( \ + struct PROTO * msg, struct PerfettoPbPackedMsg##C_TYPE * nested) { \ + struct PerfettoPbMsg* nested_msg = \ + PERFETTO_REINTERPRET_CAST(struct PerfettoPbMsg*, nested); \ + PerfettoPbMsgBeginNested(&msg->msg, nested_msg, NUM); \ + } \ + static inline void PERFETTO_I_PB_SETTER_END_NAME(PROTO, NAME)( \ + struct PROTO * msg, struct PerfettoPbPackedMsg##C_TYPE * nested) { \ + (void)nested; \ + PerfettoPbMsgEndNested(&msg->msg); \ + } \ + #define PERFETTO_I_PB_NUM_FIELD(PROTO, NAME, NUM) \ enum { PERFETTO_I_PB_NUM_FIELD_NAME(PROTO, NAME) = NUM } @@ -218,8 +237,8 @@ // `PROTO_end_NAME(struct PROTO*, struct CTYPE* nested)` that allows to // begin and end a nested submessage. `*nested` doesn't need to be // initialized. -// * `STRING`: for bytes, string and repeated packed field types. `CTYPE` -// should be `const char *`. Generates multiple accessors: +// * `STRING`: for bytes and string field types. `CTYPE` should be +// `const char *`. Generates multiple accessors: // * PROTO_set_cstr_NAME(struct PROTO*, const char*): Sets the value of the // field by copying from a null terminated string. // * PROTO_set_NAME(struct PROTO*, const void*, size_t): Sets the value of @@ -229,6 +248,17 @@ // PROTO_end_NAME(struct PROTO*, struct PerfettoPbMsg* nested): // Begins (and ends) a nested submessage to allow users to generate part // of the length delimited buffer piece by piece. +// * `PACKED`: for packed repeated field types. `CTYPE` should be +// one of `PerfettoPbPacked*`. Generates multiple accessors: +// * PROTO_set_NAME(struct PROTO*, const void*, size_t): Sets the value of +// the field by copying from a buffer at an address with the specified +// size. +// * PROTO_begin_NAME(struct PROTO*, struct PerfettoPbPackedMsgCTYPE* +// nested) and +// PROTO_end_NAME(struct PROTO*, struct PerfettoPbPackedMsgCTYPE* +// nested): Begins (and ends) a packed helper nested submessage (of the +// right type) to allow users to push repeated entries one by one +// directly into the stream writer buffer. #define PERFETTO_PB_FIELD(PROTO, TYPE, C_TYPE, NAME, NUM) \ PERFETTO_I_PB_FIELD_##TYPE(PROTO, C_TYPE, NAME, NUM) \ PERFETTO_I_PB_NUM_FIELD(PROTO, NAME, NUM) diff --git a/include/perfetto/public/pb_msg.h b/include/perfetto/public/pb_msg.h index 0f201bb226..717a080d97 100644 --- a/include/perfetto/public/pb_msg.h +++ b/include/perfetto/public/pb_msg.h @@ -105,6 +105,22 @@ static inline void PerfettoPbMsgAppendVarInt(struct PerfettoPbMsg* msg, PERFETTO_STATIC_CAST(size_t, buf_end - buf)); } +static inline void PerfettoPbMsgAppendFixed64(struct PerfettoPbMsg* msg, + uint64_t value) { + uint8_t buf[8]; + PerfettoPbWriteFixed64(value, buf); + + PerfettoPbMsgAppendBytes(msg, buf, 8); +} + +static inline void PerfettoPbMsgAppendFixed32(struct PerfettoPbMsg* msg, + uint32_t value) { + uint8_t buf[4]; + PerfettoPbWriteFixed32(value, buf); + + PerfettoPbMsgAppendBytes(msg, buf, 4); +} + static inline void PerfettoPbMsgAppendType0Field(struct PerfettoPbMsg* msg, int32_t field_id, uint64_t value) { @@ -141,11 +157,7 @@ static inline void PerfettoPbMsgAppendFixed32Field(struct PerfettoPbMsg* msg, uint8_t buf[PERFETTO_PB_VARINT_MAX_SIZE_32 + 4]; buf_end = PerfettoPbWriteVarInt( PerfettoPbMakeTag(field_id, PERFETTO_PB_WIRE_TYPE_FIXED32), buf); - buf_end[0] = PERFETTO_STATIC_CAST(uint8_t, value); - buf_end[1] = PERFETTO_STATIC_CAST(uint8_t, value >> 8); - buf_end[2] = PERFETTO_STATIC_CAST(uint8_t, value >> 16); - buf_end[3] = PERFETTO_STATIC_CAST(uint8_t, value >> 24); - buf_end += 4; + buf_end = PerfettoPbWriteFixed32(value, buf_end); PerfettoPbMsgAppendBytes(msg, buf, PERFETTO_STATIC_CAST(size_t, buf_end - buf)); @@ -166,15 +178,7 @@ static inline void PerfettoPbMsgAppendFixed64Field(struct PerfettoPbMsg* msg, uint8_t buf[PERFETTO_PB_VARINT_MAX_SIZE_32 + 8]; buf_end = PerfettoPbWriteVarInt( PerfettoPbMakeTag(field_id, PERFETTO_PB_WIRE_TYPE_FIXED64), buf); - buf_end[0] = PERFETTO_STATIC_CAST(uint8_t, value); - buf_end[1] = PERFETTO_STATIC_CAST(uint8_t, value >> 8); - buf_end[2] = PERFETTO_STATIC_CAST(uint8_t, value >> 16); - buf_end[3] = PERFETTO_STATIC_CAST(uint8_t, value >> 24); - buf_end[4] = PERFETTO_STATIC_CAST(uint8_t, value >> 32); - buf_end[5] = PERFETTO_STATIC_CAST(uint8_t, value >> 40); - buf_end[6] = PERFETTO_STATIC_CAST(uint8_t, value >> 48); - buf_end[7] = PERFETTO_STATIC_CAST(uint8_t, value >> 56); - buf_end += 8; + buf_end = PerfettoPbWriteFixed64(value, buf_end); PerfettoPbMsgAppendBytes(msg, buf, PERFETTO_STATIC_CAST(size_t, buf_end - buf)); diff --git a/include/perfetto/public/pb_packed.h b/include/perfetto/public/pb_packed.h new file mode 100644 index 0000000000..2d9bb68c8f --- /dev/null +++ b/include/perfetto/public/pb_packed.h @@ -0,0 +1,168 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INCLUDE_PERFETTO_PUBLIC_PB_PACKED_H_ +#define INCLUDE_PERFETTO_PUBLIC_PB_PACKED_H_ + +#include +#include + +#include "perfetto/public/compiler.h" +#include "perfetto/public/pb_msg.h" +#include "perfetto/public/pb_utils.h" + +// This file provides a way of serializing packed repeated fields. All the +// strongly typed `struct PerfettoPbPackedMsg*` variants behave as protozero +// nested messages and allow zero-copy serialization. A protobuf message that +// has a packed repeated field provides begin and end operations that accept a +// PerfettoPbPackedMsg. The downside of this approach is that (like all +// protozero nested messages), it reserves 4 bytes to encode the length, so it +// might add overhead for lots of small messages. + +// *** +// Sample usage of PerfettoPbPackedMsg* +// *** +// ``` +// struct PerfettoPbPackedMsgUint64 f; +// PROTO_begin_FIELD_NAME(&msg, &f); +// PerfettoPbPackedMsgUint64Append(&f, 1); +// PerfettoPbPackedMsgUint64Append(&f, 2); +// PROTO_end_FIELD_NAME(&msg, &f); +// ``` + +// *** +// Implementations of struct PerfettoPbPackedMsg for all supported field types. +// *** +struct PerfettoPbPackedMsgUint64 { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgUint64Append( + struct PerfettoPbPackedMsgUint64* buf, + uint64_t value) { + PerfettoPbMsgAppendVarInt(&buf->msg, value); +} + +struct PerfettoPbPackedMsgUint32 { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgUint32Append( + struct PerfettoPbPackedMsgUint32* buf, + uint32_t value) { + PerfettoPbMsgAppendVarInt(&buf->msg, value); +} + +struct PerfettoPbPackedMsgInt64 { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgInt64Append( + struct PerfettoPbPackedMsgInt64* buf, + int64_t value) { + PerfettoPbMsgAppendVarInt(&buf->msg, PERFETTO_STATIC_CAST(uint64_t, value)); +} + +struct PerfettoPbPackedMsgInt32 { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgInt32Append( + struct PerfettoPbPackedMsgInt32* buf, + int32_t value) { + PerfettoPbMsgAppendVarInt(&buf->msg, PERFETTO_STATIC_CAST(uint64_t, value)); +} + +struct PerfettoPbPackedMsgSint64 { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgSint64Append( + struct PerfettoPbPackedMsgSint64* buf, + int64_t value) { + uint64_t encoded = PerfettoPbZigZagEncode64(value); + PerfettoPbMsgAppendVarInt(&buf->msg, encoded); +} + +struct PerfettoPbPackedMsgSint32 { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgSint32Append( + struct PerfettoPbPackedMsgSint32* buf, + int32_t value) { + uint64_t encoded = + PerfettoPbZigZagEncode64(PERFETTO_STATIC_CAST(int64_t, value)); + PerfettoPbMsgAppendVarInt(&buf->msg, encoded); +} + +struct PerfettoPbPackedMsgFixed64 { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgFixed64Append( + struct PerfettoPbPackedMsgFixed64* buf, + uint64_t value) { + PerfettoPbMsgAppendFixed64(&buf->msg, value); +} + +struct PerfettoPbPackedMsgFixed32 { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgFixed32Append( + struct PerfettoPbPackedMsgFixed32* buf, + uint32_t value) { + PerfettoPbMsgAppendFixed32(&buf->msg, value); +} + +struct PerfettoPbPackedMsgSfixed64 { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgSfixed64Append( + struct PerfettoPbPackedMsgSfixed64* buf, + int64_t value) { + uint64_t encoded; + memcpy(&encoded, &value, sizeof(encoded)); + PerfettoPbMsgAppendFixed64(&buf->msg, encoded); +} + +struct PerfettoPbPackedMsgSfixed32 { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgSfixed32Append( + struct PerfettoPbPackedMsgSfixed32* buf, + int32_t value) { + uint32_t encoded; + memcpy(&encoded, &value, sizeof(encoded)); + PerfettoPbMsgAppendFixed32(&buf->msg, encoded); +} + +struct PerfettoPbPackedMsgDouble { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgDoubleAppend( + struct PerfettoPbPackedMsgDouble* buf, + double value) { + uint64_t encoded; + memcpy(&encoded, &value, sizeof(encoded)); + PerfettoPbMsgAppendFixed64(&buf->msg, encoded); +} + +struct PerfettoPbPackedMsgFloat { + struct PerfettoPbMsg msg; +}; +static inline void PerfettoPbPackedMsgFloatAppend( + struct PerfettoPbPackedMsgFloat* buf, + float value) { + uint32_t encoded; + memcpy(&encoded, &value, sizeof(encoded)); + PerfettoPbMsgAppendFixed32(&buf->msg, encoded); +} + +#endif // INCLUDE_PERFETTO_PUBLIC_PB_PACKED_H_ diff --git a/include/perfetto/public/pb_utils.h b/include/perfetto/public/pb_utils.h index 32ea75912f..8c36389c57 100644 --- a/include/perfetto/public/pb_utils.h +++ b/include/perfetto/public/pb_utils.h @@ -61,6 +61,32 @@ static inline uint8_t* PerfettoPbWriteVarInt(uint64_t value, uint8_t* dst) { return dst; } +// Encodes `value` as a fixed32 (little endian) into `*dst`. +// +// `dst` must point into a buffer with at least 4 bytes of space. +static inline uint8_t* PerfettoPbWriteFixed32(uint32_t value, uint8_t* buf) { + buf[0] = PERFETTO_STATIC_CAST(uint8_t, value); + buf[1] = PERFETTO_STATIC_CAST(uint8_t, value >> 8); + buf[2] = PERFETTO_STATIC_CAST(uint8_t, value >> 16); + buf[3] = PERFETTO_STATIC_CAST(uint8_t, value >> 24); + return buf + 4; +} + +// Encodes `value` as a fixed32 (little endian) into `*dst`. +// +// `dst` must point into a buffer with at least 8 bytes of space. +static inline uint8_t* PerfettoPbWriteFixed64(uint64_t value, uint8_t* buf) { + buf[0] = PERFETTO_STATIC_CAST(uint8_t, value); + buf[1] = PERFETTO_STATIC_CAST(uint8_t, value >> 8); + buf[2] = PERFETTO_STATIC_CAST(uint8_t, value >> 16); + buf[3] = PERFETTO_STATIC_CAST(uint8_t, value >> 24); + buf[4] = PERFETTO_STATIC_CAST(uint8_t, value >> 32); + buf[5] = PERFETTO_STATIC_CAST(uint8_t, value >> 40); + buf[6] = PERFETTO_STATIC_CAST(uint8_t, value >> 48); + buf[7] = PERFETTO_STATIC_CAST(uint8_t, value >> 56); + return buf + 8; +} + // Parses a VarInt from the encoded buffer [start, end). |end| is STL-style and // points one byte past the end of buffer. // The parsed int value is stored in the output arg |value|. Returns a pointer From 2471cd8a71538e60242a00d21b730fe1641ec81d Mon Sep 17 00:00:00 2001 From: Daniele Di Proietto Date: Mon, 25 Sep 2023 15:57:50 +0000 Subject: [PATCH 2/2] shared_lib: Tests for protozero serialization These cover most of the protozero macros. The sample protos in this test have been generated from the schemas at `external/perfetto/src/protozero/test/example_proto/`. A future commit contains the script that generates the sample headers from the schema. Bug: 237053538 Change-Id: Iaae9dabd14c9989460a2025d3042deee37438f4e --- Android.bp | 6 + include/perfetto/public/abi/heap_buffer.h | 2 +- src/shared_lib/test/BUILD.gn | 1 + src/shared_lib/test/api_integrationtest.cc | 348 +++++++++++++ src/shared_lib/test/protos/BUILD.gn | 23 + src/shared_lib/test/protos/library.pzc.h | 61 +++ .../protos/library_internals/galaxies.pzc.h | 32 ++ .../test/protos/test_messages.pzc.h | 482 ++++++++++++++++++ src/shared_lib/test/protos/upper_import.pzc.h | 27 + src/shared_lib/test/utils.h | 68 +++ 10 files changed, 1049 insertions(+), 1 deletion(-) create mode 100644 src/shared_lib/test/protos/BUILD.gn create mode 100644 src/shared_lib/test/protos/library.pzc.h create mode 100644 src/shared_lib/test/protos/library_internals/galaxies.pzc.h create mode 100644 src/shared_lib/test/protos/test_messages.pzc.h create mode 100644 src/shared_lib/test/protos/upper_import.pzc.h diff --git a/Android.bp b/Android.bp index 3516a35648..7c44e67e97 100644 --- a/Android.bp +++ b/Android.bp @@ -2221,6 +2221,7 @@ cc_test { ":perfetto_src_shared_lib_intern_map", ":perfetto_src_shared_lib_shared_lib", ":perfetto_src_shared_lib_test_integrationtests", + ":perfetto_src_shared_lib_test_protos_protos", ":perfetto_src_shared_lib_test_utils", ":perfetto_src_trace_processor_containers_containers", ":perfetto_src_trace_processor_db_db", @@ -9726,6 +9727,11 @@ filegroup { ], } +// GN: //src/shared_lib/test/protos:protos +filegroup { + name: "perfetto_src_shared_lib_test_protos_protos", +} + // GN: //src/shared_lib/test:utils filegroup { name: "perfetto_src_shared_lib_test_utils", diff --git a/include/perfetto/public/abi/heap_buffer.h b/include/perfetto/public/abi/heap_buffer.h index 811ed0b72d..b5bc4ffbce 100644 --- a/include/perfetto/public/abi/heap_buffer.h +++ b/include/perfetto/public/abi/heap_buffer.h @@ -25,7 +25,7 @@ extern "C" { // A PerfettoHeapBuffer can be used to serialize protobuf data using the // PerfettoStreamWriter interface. Stores data on heap allocated buffers, which -// can be read back with PerfettoHeapBufferCopyContent(). +// can be read back with PerfettoHeapBufferCopyInto(). struct PerfettoHeapBuffer; diff --git a/src/shared_lib/test/BUILD.gn b/src/shared_lib/test/BUILD.gn index aa87956b0a..c7d495e6b7 100644 --- a/src/shared_lib/test/BUILD.gn +++ b/src/shared_lib/test/BUILD.gn @@ -48,6 +48,7 @@ if (enable_perfetto_integration_tests) { source_set("integrationtests") { testonly = true deps = [ + "protos", ":utils", "..:shared_lib", "../../../gn:default_deps", diff --git a/src/shared_lib/test/api_integrationtest.cc b/src/shared_lib/test/api_integrationtest.cc index 7a2889d6e7..f6eae8946a 100644 --- a/src/shared_lib/test/api_integrationtest.cc +++ b/src/shared_lib/test/api_integrationtest.cc @@ -40,6 +40,7 @@ #include "test/gtest_and_gmock.h" #include "src/shared_lib/reset_for_testing.h" +#include "src/shared_lib/test/protos/test_messages.pzc.h" #include "src/shared_lib/test/utils.h" // Tests for the perfetto shared library. @@ -47,7 +48,11 @@ namespace { using ::perfetto::shlib::test_utils::AllFieldsWithId; +using ::perfetto::shlib::test_utils::DoubleField; using ::perfetto::shlib::test_utils::FieldView; +using ::perfetto::shlib::test_utils::Fixed32Field; +using ::perfetto::shlib::test_utils::Fixed64Field; +using ::perfetto::shlib::test_utils::FloatField; using ::perfetto::shlib::test_utils::IdFieldView; using ::perfetto::shlib::test_utils::MsgField; using ::perfetto::shlib::test_utils::PbField; @@ -61,6 +66,7 @@ using ::testing::DoAll; using ::testing::ElementsAre; using ::testing::InSequence; using ::testing::NiceMock; +using ::testing::ResultOf; using ::testing::Return; using ::testing::SaveArg; using ::testing::UnorderedElementsAre; @@ -204,6 +210,348 @@ TEST(SharedLibProtobufTest, PerfettoPbDecoderIteratorExample) { EXPECT_EQ(n_payload_single_int, 1u); } +class SharedLibProtozeroSerializationTest : public testing::Test { + protected: + SharedLibProtozeroSerializationTest() { + hb = PerfettoHeapBufferCreate(&writer.writer); + } + + std::vector GetData() { + std::vector data; + size_t size = PerfettoStreamWriterGetWrittenSize(&writer.writer); + data.resize(size); + PerfettoHeapBufferCopyInto(hb, &writer.writer, data.data(), data.size()); + return data; + } + + ~SharedLibProtozeroSerializationTest() { + PerfettoHeapBufferDestroy(hb, &writer.writer); + } + + template + static std::vector ParsePackedVarInt(const std::string& data) { + std::vector ret; + const uint8_t* read_ptr = reinterpret_cast(data.data()); + const uint8_t* const end = read_ptr + data.size(); + while (read_ptr != end) { + uint64_t val; + const uint8_t* new_read_ptr = PerfettoPbParseVarInt(read_ptr, end, &val); + if (new_read_ptr == read_ptr) { + ADD_FAILURE(); + return ret; + } + read_ptr = new_read_ptr; + ret.push_back(static_cast(val)); + } + return ret; + } + + template + static std::vector ParsePackedFixed(const std::string& data) { + std::vector ret; + if (data.size() % sizeof(T)) { + ADD_FAILURE(); + return ret; + } + const uint8_t* read_ptr = reinterpret_cast(data.data()); + const uint8_t* end = read_ptr + data.size(); + while (read_ptr < end) { + ret.push_back(*reinterpret_cast(read_ptr)); + read_ptr += sizeof(T); + } + return ret; + } + + struct PerfettoPbMsgWriter writer; + struct PerfettoHeapBuffer* hb; +}; + +TEST_F(SharedLibProtozeroSerializationTest, SimpleFieldsNoNesting) { + struct protozero_test_protos_EveryField msg; + PerfettoPbMsgInit(&msg.msg, &writer); + + protozero_test_protos_EveryField_set_field_int32(&msg, -1); + protozero_test_protos_EveryField_set_field_int64(&msg, -333123456789ll); + protozero_test_protos_EveryField_set_field_uint32(&msg, 600); + protozero_test_protos_EveryField_set_field_uint64(&msg, 333123456789ll); + protozero_test_protos_EveryField_set_field_sint32(&msg, -5); + protozero_test_protos_EveryField_set_field_sint64(&msg, -9000); + protozero_test_protos_EveryField_set_field_fixed32(&msg, 12345); + protozero_test_protos_EveryField_set_field_fixed64(&msg, 444123450000ll); + protozero_test_protos_EveryField_set_field_sfixed32(&msg, -69999); + protozero_test_protos_EveryField_set_field_sfixed64(&msg, -200); + protozero_test_protos_EveryField_set_field_float(&msg, 3.14f); + protozero_test_protos_EveryField_set_field_double(&msg, 0.5555); + protozero_test_protos_EveryField_set_field_bool(&msg, true); + protozero_test_protos_EveryField_set_small_enum(&msg, + protozero_test_protos_TO_BE); + protozero_test_protos_EveryField_set_signed_enum( + &msg, protozero_test_protos_NEGATIVE); + protozero_test_protos_EveryField_set_big_enum(&msg, + protozero_test_protos_BEGIN); + protozero_test_protos_EveryField_set_cstr_field_string(&msg, "FizzBuzz"); + protozero_test_protos_EveryField_set_field_bytes(&msg, "\x11\x00\xBE\xEF", 4); + protozero_test_protos_EveryField_set_repeated_int32(&msg, 1); + protozero_test_protos_EveryField_set_repeated_int32(&msg, -1); + protozero_test_protos_EveryField_set_repeated_int32(&msg, 100); + protozero_test_protos_EveryField_set_repeated_int32(&msg, 2000000); + + EXPECT_THAT( + FieldView(GetData()), + ElementsAre( + PbField(protozero_test_protos_EveryField_field_int32_field_number, + VarIntField(static_cast(-1))), + PbField(protozero_test_protos_EveryField_field_int64_field_number, + VarIntField(static_cast(INT64_C(-333123456789)))), + PbField(protozero_test_protos_EveryField_field_uint32_field_number, + VarIntField(600)), + PbField(protozero_test_protos_EveryField_field_uint64_field_number, + VarIntField(UINT64_C(333123456789))), + PbField(protozero_test_protos_EveryField_field_sint32_field_number, + VarIntField(ResultOf( + [](uint64_t val) { + return PerfettoPbZigZagDecode32( + static_cast(val)); + }, + -5))), + PbField(protozero_test_protos_EveryField_field_sint64_field_number, + VarIntField(ResultOf(PerfettoPbZigZagDecode64, -9000))), + PbField(protozero_test_protos_EveryField_field_fixed32_field_number, + Fixed32Field(12345)), + PbField(protozero_test_protos_EveryField_field_fixed64_field_number, + Fixed64Field(UINT64_C(444123450000))), + PbField(protozero_test_protos_EveryField_field_sfixed32_field_number, + Fixed32Field(static_cast(-69999))), + PbField(protozero_test_protos_EveryField_field_sfixed64_field_number, + Fixed64Field(static_cast(-200))), + PbField(protozero_test_protos_EveryField_field_float_field_number, + FloatField(3.14f)), + PbField(protozero_test_protos_EveryField_field_double_field_number, + DoubleField(0.5555)), + PbField(protozero_test_protos_EveryField_field_bool_field_number, + VarIntField(true)), + PbField(protozero_test_protos_EveryField_small_enum_field_number, + VarIntField(protozero_test_protos_TO_BE)), + PbField(protozero_test_protos_EveryField_signed_enum_field_number, + VarIntField(protozero_test_protos_NEGATIVE)), + PbField(protozero_test_protos_EveryField_big_enum_field_number, + VarIntField(protozero_test_protos_BEGIN)), + PbField(protozero_test_protos_EveryField_field_string_field_number, + StringField("FizzBuzz")), + PbField(protozero_test_protos_EveryField_field_bytes_field_number, + StringField(std::string_view("\x11\x00\xBE\xEF", 4))), + PbField(protozero_test_protos_EveryField_repeated_int32_field_number, + VarIntField(1)), + PbField(protozero_test_protos_EveryField_repeated_int32_field_number, + VarIntField(static_cast(-1))), + PbField(protozero_test_protos_EveryField_repeated_int32_field_number, + VarIntField(100)), + PbField(protozero_test_protos_EveryField_repeated_int32_field_number, + VarIntField(2000000)))); +} + +TEST_F(SharedLibProtozeroSerializationTest, NestedMessages) { + struct protozero_test_protos_NestedA msg_a; + PerfettoPbMsgInit(&msg_a.msg, &writer); + + { + struct protozero_test_protos_NestedA_NestedB msg_b; + protozero_test_protos_NestedA_begin_repeated_a(&msg_a, &msg_b); + { + struct protozero_test_protos_NestedA_NestedB_NestedC msg_c; + protozero_test_protos_NestedA_NestedB_begin_value_b(&msg_b, &msg_c); + protozero_test_protos_NestedA_NestedB_NestedC_set_value_c(&msg_c, 321); + protozero_test_protos_NestedA_NestedB_end_value_b(&msg_b, &msg_c); + } + protozero_test_protos_NestedA_end_repeated_a(&msg_a, &msg_b); + } + { + struct protozero_test_protos_NestedA_NestedB msg_b; + protozero_test_protos_NestedA_begin_repeated_a(&msg_a, &msg_b); + protozero_test_protos_NestedA_end_repeated_a(&msg_a, &msg_b); + } + { + struct protozero_test_protos_NestedA_NestedB_NestedC msg_c; + protozero_test_protos_NestedA_begin_super_nested(&msg_a, &msg_c); + protozero_test_protos_NestedA_NestedB_NestedC_set_value_c(&msg_c, 1000); + protozero_test_protos_NestedA_end_super_nested(&msg_a, &msg_c); + } + + EXPECT_THAT( + FieldView(GetData()), + ElementsAre( + PbField( + protozero_test_protos_NestedA_repeated_a_field_number, + MsgField(ElementsAre(PbField( + protozero_test_protos_NestedA_NestedB_value_b_field_number, + MsgField(ElementsAre(PbField( + protozero_test_protos_NestedA_NestedB_NestedC_value_c_field_number, + VarIntField(321)))))))), + PbField(protozero_test_protos_NestedA_repeated_a_field_number, + MsgField(ElementsAre())), + PbField( + protozero_test_protos_NestedA_super_nested_field_number, + MsgField(ElementsAre(PbField( + protozero_test_protos_NestedA_NestedB_NestedC_value_c_field_number, + VarIntField(1000))))))); +} + +TEST_F(SharedLibProtozeroSerializationTest, PackedRepeatedMsgVarInt) { + struct protozero_test_protos_PackedRepeatedFields msg; + PerfettoPbMsgInit(&msg.msg, &writer); + + { + PerfettoPbPackedMsgInt32 f; + protozero_test_protos_PackedRepeatedFields_begin_field_int32(&msg, &f); + PerfettoPbPackedMsgInt32Append(&f, 42); + PerfettoPbPackedMsgInt32Append(&f, 255); + PerfettoPbPackedMsgInt32Append(&f, -1); + protozero_test_protos_PackedRepeatedFields_end_field_int32(&msg, &f); + } + + { + PerfettoPbPackedMsgInt64 f; + protozero_test_protos_PackedRepeatedFields_begin_field_int64(&msg, &f); + PerfettoPbPackedMsgInt64Append(&f, INT64_C(3000000000)); + PerfettoPbPackedMsgInt64Append(&f, INT64_C(-3000000000)); + protozero_test_protos_PackedRepeatedFields_end_field_int64(&msg, &f); + } + + { + PerfettoPbPackedMsgUint32 f; + protozero_test_protos_PackedRepeatedFields_begin_field_uint32(&msg, &f); + PerfettoPbPackedMsgUint32Append(&f, 42); + PerfettoPbPackedMsgUint32Append(&f, UINT32_C(3000000000)); + protozero_test_protos_PackedRepeatedFields_end_field_uint32(&msg, &f); + } + + { + PerfettoPbPackedMsgUint64 f; + protozero_test_protos_PackedRepeatedFields_begin_field_uint64(&msg, &f); + PerfettoPbPackedMsgUint64Append(&f, 42); + PerfettoPbPackedMsgUint64Append(&f, UINT64_C(5000000000)); + protozero_test_protos_PackedRepeatedFields_end_field_uint64(&msg, &f); + } + + { + PerfettoPbPackedMsgInt32 f; + protozero_test_protos_PackedRepeatedFields_begin_signed_enum(&msg, &f); + PerfettoPbPackedMsgInt32Append(&f, protozero_test_protos_POSITIVE); + PerfettoPbPackedMsgInt32Append(&f, protozero_test_protos_NEGATIVE); + protozero_test_protos_PackedRepeatedFields_end_signed_enum(&msg, &f); + } + + EXPECT_THAT( + FieldView(GetData()), + ElementsAre( + PbField( + protozero_test_protos_PackedRepeatedFields_field_int32_field_number, + StringField(ResultOf(ParsePackedVarInt, + ElementsAre(42, 255, -1)))), + PbField( + protozero_test_protos_PackedRepeatedFields_field_int64_field_number, + StringField(ResultOf( + ParsePackedVarInt, + ElementsAre(INT64_C(3000000000), INT64_C(-3000000000))))), + PbField( + protozero_test_protos_PackedRepeatedFields_field_uint32_field_number, + StringField(ResultOf(ParsePackedVarInt, + ElementsAre(42, UINT32_C(3000000000))))), + PbField( + protozero_test_protos_PackedRepeatedFields_field_uint64_field_number, + StringField(ResultOf(ParsePackedVarInt, + ElementsAre(42, UINT64_C(5000000000))))), + PbField( + protozero_test_protos_PackedRepeatedFields_signed_enum_field_number, + StringField( + ResultOf(ParsePackedVarInt, + ElementsAre(protozero_test_protos_POSITIVE, + protozero_test_protos_NEGATIVE)))))); +} + +TEST_F(SharedLibProtozeroSerializationTest, PackedRepeatedMsgFixed) { + struct protozero_test_protos_PackedRepeatedFields msg; + PerfettoPbMsgInit(&msg.msg, &writer); + + { + PerfettoPbPackedMsgFixed32 f; + protozero_test_protos_PackedRepeatedFields_begin_field_fixed32(&msg, &f); + PerfettoPbPackedMsgFixed32Append(&f, 42); + PerfettoPbPackedMsgFixed32Append(&f, UINT32_C(3000000000)); + protozero_test_protos_PackedRepeatedFields_end_field_fixed32(&msg, &f); + } + + { + PerfettoPbPackedMsgFixed64 f; + protozero_test_protos_PackedRepeatedFields_begin_field_fixed64(&msg, &f); + PerfettoPbPackedMsgFixed64Append(&f, 42); + PerfettoPbPackedMsgFixed64Append(&f, UINT64_C(5000000000)); + protozero_test_protos_PackedRepeatedFields_end_field_fixed64(&msg, &f); + } + + { + PerfettoPbPackedMsgSfixed32 f; + protozero_test_protos_PackedRepeatedFields_begin_field_sfixed32(&msg, &f); + PerfettoPbPackedMsgSfixed32Append(&f, 42); + PerfettoPbPackedMsgSfixed32Append(&f, 255); + PerfettoPbPackedMsgSfixed32Append(&f, -1); + protozero_test_protos_PackedRepeatedFields_end_field_sfixed32(&msg, &f); + } + + { + PerfettoPbPackedMsgSfixed64 f; + protozero_test_protos_PackedRepeatedFields_begin_field_sfixed64(&msg, &f); + PerfettoPbPackedMsgSfixed64Append(&f, INT64_C(3000000000)); + PerfettoPbPackedMsgSfixed64Append(&f, INT64_C(-3000000000)); + protozero_test_protos_PackedRepeatedFields_end_field_sfixed64(&msg, &f); + } + + { + PerfettoPbPackedMsgFloat f; + protozero_test_protos_PackedRepeatedFields_begin_field_float(&msg, &f); + PerfettoPbPackedMsgFloatAppend(&f, 3.14f); + PerfettoPbPackedMsgFloatAppend(&f, 42.1f); + protozero_test_protos_PackedRepeatedFields_end_field_float(&msg, &f); + } + + { + PerfettoPbPackedMsgDouble f; + protozero_test_protos_PackedRepeatedFields_begin_field_double(&msg, &f); + PerfettoPbPackedMsgDoubleAppend(&f, 3.14); + PerfettoPbPackedMsgDoubleAppend(&f, 42.1); + protozero_test_protos_PackedRepeatedFields_end_field_double(&msg, &f); + } + + EXPECT_THAT( + FieldView(GetData()), + ElementsAre( + PbField( + protozero_test_protos_PackedRepeatedFields_field_fixed32_field_number, + StringField(ResultOf(ParsePackedFixed, + ElementsAre(42, UINT32_C(3000000000))))), + PbField( + protozero_test_protos_PackedRepeatedFields_field_fixed64_field_number, + StringField(ResultOf(ParsePackedFixed, + ElementsAre(42, UINT64_C(5000000000))))), + PbField( + protozero_test_protos_PackedRepeatedFields_field_sfixed32_field_number, + StringField(ResultOf(ParsePackedFixed, + ElementsAre(42, 255, -1)))), + PbField( + protozero_test_protos_PackedRepeatedFields_field_sfixed64_field_number, + StringField(ResultOf( + ParsePackedFixed, + ElementsAre(INT64_C(3000000000), INT64_C(-3000000000))))), + PbField( + protozero_test_protos_PackedRepeatedFields_field_float_field_number, + StringField(ResultOf(ParsePackedFixed, + ElementsAre(3.14f, 42.1f)))), + PbField( + protozero_test_protos_PackedRepeatedFields_field_double_field_number, + StringField(ResultOf(ParsePackedFixed, + ElementsAre(3.14, 42.1)))))); +} + class SharedLibDataSourceTest : public testing::Test { protected: void SetUp() override { diff --git a/src/shared_lib/test/protos/BUILD.gn b/src/shared_lib/test/protos/BUILD.gn new file mode 100644 index 0000000000..434f4a5218 --- /dev/null +++ b/src/shared_lib/test/protos/BUILD.gn @@ -0,0 +1,23 @@ +# Copyright (C) 2023 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +source_set("protos") { + testonly = true + sources = [ + "library.pzc.h", + "library_internals/galaxies.pzc.h", + "test_messages.pzc.h", + "upper_import.pzc.h", + ] +} diff --git a/src/shared_lib/test/protos/library.pzc.h b/src/shared_lib/test/protos/library.pzc.h new file mode 100644 index 0000000000..9238101ba9 --- /dev/null +++ b/src/shared_lib/test/protos/library.pzc.h @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SRC_SHARED_LIB_TEST_PROTOS_LIBRARY_PZC_H_ +#define SRC_SHARED_LIB_TEST_PROTOS_LIBRARY_PZC_H_ + +#include +#include + +#include "perfetto/public/pb_macros.h" +#include "src/shared_lib/test/protos/library_internals/galaxies.pzc.h" + +PERFETTO_PB_ENUM_IN_MSG(protozero_test_protos_TransgalacticMessage, + MessageType){ + PERFETTO_PB_ENUM_IN_MSG_ENTRY(protozero_test_protos_TransgalacticMessage, + REGULAR) = 0, + PERFETTO_PB_ENUM_IN_MSG_ENTRY(protozero_test_protos_TransgalacticMessage, + EXPRESS) = 1, +}; + +PERFETTO_PB_MSG(protozero_test_protos_TransgalacticMessage); +PERFETTO_PB_FIELD(protozero_test_protos_TransgalacticMessage, + VARINT, + enum protozero_test_protos_Galaxy, + origin_galaxy, + 1); +PERFETTO_PB_FIELD(protozero_test_protos_TransgalacticMessage, + STRING, + const char*, + origin_planet, + 2); +PERFETTO_PB_FIELD(protozero_test_protos_TransgalacticMessage, + VARINT, + enum protozero_test_protos_Galaxy, + destination_galaxy, + 3); +PERFETTO_PB_FIELD(protozero_test_protos_TransgalacticMessage, + STRING, + const char*, + destination_planet, + 4); +PERFETTO_PB_FIELD(protozero_test_protos_TransgalacticMessage, + STRING, + const char*, + proto_message, + 5); + +#endif // SRC_SHARED_LIB_TEST_PROTOS_LIBRARY_PZC_H_ diff --git a/src/shared_lib/test/protos/library_internals/galaxies.pzc.h b/src/shared_lib/test/protos/library_internals/galaxies.pzc.h new file mode 100644 index 0000000000..0823642842 --- /dev/null +++ b/src/shared_lib/test/protos/library_internals/galaxies.pzc.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SRC_SHARED_LIB_TEST_PROTOS_LIBRARY_INTERNALS_GALAXIES_PZC_H_ +#define SRC_SHARED_LIB_TEST_PROTOS_LIBRARY_INTERNALS_GALAXIES_PZC_H_ + +#include +#include + +#include "perfetto/public/pb_macros.h" +#include "src/shared_lib/test/protos/upper_import.pzc.h" + +PERFETTO_PB_ENUM(protozero_test_protos_Galaxy){ + PERFETTO_PB_ENUM_ENTRY(protozero_test_protos_MILKY_WAY) = 1, + PERFETTO_PB_ENUM_ENTRY(protozero_test_protos_ANDROMEDA) = 2, + PERFETTO_PB_ENUM_ENTRY(protozero_test_protos_SUNFLOWER) = 3, +}; + +#endif // SRC_SHARED_LIB_TEST_PROTOS_LIBRARY_INTERNALS_GALAXIES_PZC_H_ diff --git a/src/shared_lib/test/protos/test_messages.pzc.h b/src/shared_lib/test/protos/test_messages.pzc.h new file mode 100644 index 0000000000..5f8b6b3b0a --- /dev/null +++ b/src/shared_lib/test/protos/test_messages.pzc.h @@ -0,0 +1,482 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SRC_SHARED_LIB_TEST_PROTOS_TEST_MESSAGES_PZC_H_ +#define SRC_SHARED_LIB_TEST_PROTOS_TEST_MESSAGES_PZC_H_ + +#include +#include + +#include "perfetto/public/pb_macros.h" +#include "src/shared_lib/test/protos/library.pzc.h" + +PERFETTO_PB_MSG_DECL(protozero_test_protos_EveryField); +PERFETTO_PB_MSG_DECL(protozero_test_protos_NestedA_NestedB); +PERFETTO_PB_MSG_DECL(protozero_test_protos_NestedA_NestedB_NestedC); +PERFETTO_PB_MSG_DECL(protozero_test_protos_TestVersioning_V1_Sub1_V1); +PERFETTO_PB_MSG_DECL(protozero_test_protos_TestVersioning_V2_Sub1_V2); +PERFETTO_PB_MSG_DECL(protozero_test_protos_TestVersioning_V2_Sub2_V2); +PERFETTO_PB_MSG_DECL(protozero_test_protos_TransgalacticMessage); + +PERFETTO_PB_ENUM(protozero_test_protos_SmallEnum){ + PERFETTO_PB_ENUM_ENTRY(protozero_test_protos_TO_BE) = 1, + PERFETTO_PB_ENUM_ENTRY(protozero_test_protos_NOT_TO_BE) = 0, +}; + +PERFETTO_PB_ENUM(protozero_test_protos_SignedEnum){ + PERFETTO_PB_ENUM_ENTRY(protozero_test_protos_POSITIVE) = 1, + PERFETTO_PB_ENUM_ENTRY(protozero_test_protos_NEUTRAL) = 0, + PERFETTO_PB_ENUM_ENTRY(protozero_test_protos_NEGATIVE) = -1, +}; + +PERFETTO_PB_ENUM(protozero_test_protos_BigEnum){ + PERFETTO_PB_ENUM_ENTRY(protozero_test_protos_BEGIN) = 10, + PERFETTO_PB_ENUM_ENTRY(protozero_test_protos_END) = 100500, +}; + +PERFETTO_PB_ENUM_IN_MSG(protozero_test_protos_TestVersioning_V2, Enumz_V2){ + PERFETTO_PB_ENUM_IN_MSG_ENTRY(protozero_test_protos_TestVersioning_V2, + ONE) = 1, + PERFETTO_PB_ENUM_IN_MSG_ENTRY(protozero_test_protos_TestVersioning_V2, + TWO) = 2, + PERFETTO_PB_ENUM_IN_MSG_ENTRY(protozero_test_protos_TestVersioning_V2, + THREE_V2) = 3, +}; + +PERFETTO_PB_ENUM_IN_MSG(protozero_test_protos_TestVersioning_V1, Enumz_V1){ + PERFETTO_PB_ENUM_IN_MSG_ENTRY(protozero_test_protos_TestVersioning_V1, + ONE) = 1, + PERFETTO_PB_ENUM_IN_MSG_ENTRY(protozero_test_protos_TestVersioning_V1, + TWO) = 2, +}; + +PERFETTO_PB_ENUM_IN_MSG(protozero_test_protos_EveryField, NestedEnum){ + PERFETTO_PB_ENUM_IN_MSG_ENTRY(protozero_test_protos_EveryField, PING) = 1, + PERFETTO_PB_ENUM_IN_MSG_ENTRY(protozero_test_protos_EveryField, PONG) = 2, +}; + +PERFETTO_PB_MSG(protozero_test_protos_TestVersioning_V2); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + VARINT, + int32_t, + root_int, + 1); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + VARINT, + enum protozero_test_protos_TestVersioning_V2_Enumz_V2, + enumz, + 2); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + STRING, + const char*, + root_string, + 3); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + STRING, + const char*, + rep_string, + 4); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + MSG, + protozero_test_protos_TestVersioning_V2_Sub1_V2, + sub1, + 5); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + MSG, + protozero_test_protos_TestVersioning_V2_Sub1_V2, + sub1_rep, + 6); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + MSG, + protozero_test_protos_TestVersioning_V2_Sub1_V2, + sub1_lazy, + 7); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + VARINT, + int32_t, + root_int_v2, + 10); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + MSG, + protozero_test_protos_TestVersioning_V2_Sub2_V2, + sub2, + 11); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + MSG, + protozero_test_protos_TestVersioning_V2_Sub2_V2, + sub2_rep, + 12); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2, + MSG, + protozero_test_protos_TestVersioning_V2_Sub2_V2, + sub2_lazy, + 13); + +PERFETTO_PB_MSG(protozero_test_protos_TestVersioning_V2_Sub2_V2); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2_Sub2_V2, + VARINT, + int32_t, + sub2_int, + 1); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2_Sub2_V2, + STRING, + const char*, + sub2_string, + 2); + +PERFETTO_PB_MSG(protozero_test_protos_TestVersioning_V2_Sub1_V2); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2_Sub1_V2, + VARINT, + int32_t, + sub1_int, + 1); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2_Sub1_V2, + STRING, + const char*, + sub1_string, + 2); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2_Sub1_V2, + VARINT, + int32_t, + sub1_int_v2, + 3); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V2_Sub1_V2, + STRING, + const char*, + sub1_string_v2, + 4); + +PERFETTO_PB_MSG(protozero_test_protos_TestVersioning_V1); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V1, + VARINT, + int32_t, + root_int, + 1); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V1, + VARINT, + enum protozero_test_protos_TestVersioning_V1_Enumz_V1, + enumz, + 2); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V1, + STRING, + const char*, + root_string, + 3); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V1, + STRING, + const char*, + rep_string, + 4); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V1, + MSG, + protozero_test_protos_TestVersioning_V1_Sub1_V1, + sub1, + 5); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V1, + MSG, + protozero_test_protos_TestVersioning_V1_Sub1_V1, + sub1_rep, + 6); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V1, + MSG, + protozero_test_protos_TestVersioning_V1_Sub1_V1, + sub1_lazy, + 7); + +PERFETTO_PB_MSG(protozero_test_protos_TestVersioning_V1_Sub1_V1); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V1_Sub1_V1, + VARINT, + int32_t, + sub1_int, + 1); +PERFETTO_PB_FIELD(protozero_test_protos_TestVersioning_V1_Sub1_V1, + STRING, + const char*, + sub1_string, + 2); + +PERFETTO_PB_MSG(protozero_test_protos_PackedRepeatedFields); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Int32, + field_int32, + 1); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Int64, + field_int64, + 4); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Uint32, + field_uint32, + 5); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Uint64, + field_uint64, + 6); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Fixed32, + field_fixed32, + 2); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Fixed64, + field_fixed64, + 9); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Sfixed32, + field_sfixed32, + 10); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Sfixed64, + field_sfixed64, + 3); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Float, + field_float, + 11); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Double, + field_double, + 12); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Int32, + small_enum, + 51); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Int32, + signed_enum, + 52); +PERFETTO_PB_FIELD(protozero_test_protos_PackedRepeatedFields, + PACKED, + Int32, + big_enum, + 53); + +PERFETTO_PB_MSG(protozero_test_protos_CamelCaseFields); +PERFETTO_PB_FIELD(protozero_test_protos_CamelCaseFields, + VARINT, + bool, + foo_bar_baz, + 1); +PERFETTO_PB_FIELD(protozero_test_protos_CamelCaseFields, + VARINT, + bool, + barbaz, + 2); +PERFETTO_PB_FIELD(protozero_test_protos_CamelCaseFields, + VARINT, + bool, + moomoo, + 3); +PERFETTO_PB_FIELD(protozero_test_protos_CamelCaseFields, + VARINT, + bool, + urlencoder, + 4); +PERFETTO_PB_FIELD(protozero_test_protos_CamelCaseFields, VARINT, bool, xmap, 5); +PERFETTO_PB_FIELD(protozero_test_protos_CamelCaseFields, + VARINT, + bool, + urle_nco__der, + 6); +PERFETTO_PB_FIELD(protozero_test_protos_CamelCaseFields, + VARINT, + bool, + __bigbang, + 7); +PERFETTO_PB_FIELD(protozero_test_protos_CamelCaseFields, VARINT, bool, u2, 8); +PERFETTO_PB_FIELD(protozero_test_protos_CamelCaseFields, + VARINT, + bool, + bangbig__, + 9); + +PERFETTO_PB_MSG(protozero_test_protos_NestedA); +PERFETTO_PB_FIELD(protozero_test_protos_NestedA, + MSG, + protozero_test_protos_NestedA_NestedB, + repeated_a, + 2); +PERFETTO_PB_FIELD(protozero_test_protos_NestedA, + MSG, + protozero_test_protos_NestedA_NestedB_NestedC, + super_nested, + 3); + +PERFETTO_PB_MSG(protozero_test_protos_NestedA_NestedB); +PERFETTO_PB_FIELD(protozero_test_protos_NestedA_NestedB, + MSG, + protozero_test_protos_NestedA_NestedB_NestedC, + value_b, + 1); + +PERFETTO_PB_MSG(protozero_test_protos_NestedA_NestedB_NestedC); +PERFETTO_PB_FIELD(protozero_test_protos_NestedA_NestedB_NestedC, + VARINT, + int32_t, + value_c, + 1); + +PERFETTO_PB_MSG(protozero_test_protos_EveryField); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + VARINT, + int32_t, + field_int32, + 1); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + VARINT, + int64_t, + field_int64, + 2); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + VARINT, + uint32_t, + field_uint32, + 3); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + VARINT, + uint64_t, + field_uint64, + 4); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + ZIGZAG, + int32_t, + field_sint32, + 5); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + ZIGZAG, + int64_t, + field_sint64, + 6); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + FIXED32, + uint32_t, + field_fixed32, + 7); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + FIXED64, + uint64_t, + field_fixed64, + 8); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + FIXED32, + int32_t, + field_sfixed32, + 9); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + FIXED64, + int64_t, + field_sfixed64, + 10); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + FIXED32, + float, + field_float, + 11); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + FIXED64, + double, + field_double, + 12); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + VARINT, + bool, + field_bool, + 13); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + MSG, + protozero_test_protos_EveryField, + field_nested, + 14); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + VARINT, + enum protozero_test_protos_SmallEnum, + small_enum, + 51); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + VARINT, + enum protozero_test_protos_SignedEnum, + signed_enum, + 52); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + VARINT, + enum protozero_test_protos_BigEnum, + big_enum, + 53); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + STRING, + const char*, + field_string, + 500); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + STRING, + const char*, + field_bytes, + 505); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + VARINT, + enum protozero_test_protos_EveryField_NestedEnum, + nested_enum, + 600); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + STRING, + const char*, + repeated_string, + 700); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + FIXED64, + uint64_t, + repeated_fixed64, + 701); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + FIXED32, + int32_t, + repeated_sfixed32, + 702); +PERFETTO_PB_FIELD(protozero_test_protos_EveryField, + VARINT, + int32_t, + repeated_int32, + 999); + +PERFETTO_PB_MSG(protozero_test_protos_TransgalacticParcel); +PERFETTO_PB_FIELD(protozero_test_protos_TransgalacticParcel, + MSG, + protozero_test_protos_TransgalacticMessage, + message, + 1); +PERFETTO_PB_FIELD(protozero_test_protos_TransgalacticParcel, + STRING, + const char*, + tracking_code, + 2); +PERFETTO_PB_FIELD(protozero_test_protos_TransgalacticParcel, + VARINT, + enum protozero_test_protos_TransgalacticMessage_MessageType, + message_type, + 3); + +#endif // SRC_SHARED_LIB_TEST_PROTOS_TEST_MESSAGES_PZC_H_ diff --git a/src/shared_lib/test/protos/upper_import.pzc.h b/src/shared_lib/test/protos/upper_import.pzc.h new file mode 100644 index 0000000000..37e55c6937 --- /dev/null +++ b/src/shared_lib/test/protos/upper_import.pzc.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2023 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SRC_SHARED_LIB_TEST_PROTOS_UPPER_IMPORT_PZC_H_ +#define SRC_SHARED_LIB_TEST_PROTOS_UPPER_IMPORT_PZC_H_ + +#include +#include + +#include "perfetto/public/pb_macros.h" + +PERFETTO_PB_MSG(protozero_test_protos_TrickyPublicImport); + +#endif // SRC_SHARED_LIB_TEST_PROTOS_UPPER_IMPORT_PZC_H_ diff --git a/src/shared_lib/test/utils.h b/src/shared_lib/test/utils.h index e68376dcb1..f6cfddcd50 100644 --- a/src/shared_lib/test/utils.h +++ b/src/shared_lib/test/utils.h @@ -342,6 +342,74 @@ auto VarIntField(M m) { testing::ResultOf(f, m)); } +// Matches a PerfettoPbDecoderField fixed64 field. Accepts an integer matcher +// +// Example: +// PerfettoPbDecoderField field = ... +// EXPECT_THAT(field, Fixed64Field(1))); +template +auto Fixed64Field(M m) { + auto f = [](const PerfettoPbDecoderField& field) { + return field.value.integer64; + }; + return testing::AllOf( + testing::Field(&PerfettoPbDecoderField::status, PERFETTO_PB_DECODER_OK), + testing::Field(&PerfettoPbDecoderField::wire_type, + PERFETTO_PB_WIRE_TYPE_FIXED64), + testing::ResultOf(f, m)); +} + +// Matches a PerfettoPbDecoderField fixed32 field. Accepts an integer matcher +// +// Example: +// PerfettoPbDecoderField field = ... +// EXPECT_THAT(field, Fixed32Field(1))); +template +auto Fixed32Field(M m) { + auto f = [](const PerfettoPbDecoderField& field) { + return field.value.integer32; + }; + return testing::AllOf( + testing::Field(&PerfettoPbDecoderField::status, PERFETTO_PB_DECODER_OK), + testing::Field(&PerfettoPbDecoderField::wire_type, + PERFETTO_PB_WIRE_TYPE_FIXED32), + testing::ResultOf(f, m)); +} + +// Matches a PerfettoPbDecoderField double field. Accepts an double matcher +// +// Example: +// PerfettoPbDecoderField field = ... +// EXPECT_THAT(field, DoubleField(1.0))); +template +auto DoubleField(M m) { + auto f = [](const PerfettoPbDecoderField& field) { + return field.value.double_val; + }; + return testing::AllOf( + testing::Field(&PerfettoPbDecoderField::status, PERFETTO_PB_DECODER_OK), + testing::Field(&PerfettoPbDecoderField::wire_type, + PERFETTO_PB_WIRE_TYPE_FIXED64), + testing::ResultOf(f, m)); +} + +// Matches a PerfettoPbDecoderField float field. Accepts a float matcher +// +// Example: +// PerfettoPbDecoderField field = ... +// EXPECT_THAT(field, FloatField(1.0))); +template +auto FloatField(M m) { + auto f = [](const PerfettoPbDecoderField& field) { + return field.value.float_val; + }; + return testing::AllOf( + testing::Field(&PerfettoPbDecoderField::status, PERFETTO_PB_DECODER_OK), + testing::Field(&PerfettoPbDecoderField::wire_type, + PERFETTO_PB_WIRE_TYPE_FIXED32), + testing::ResultOf(f, m)); +} + // Matches a PerfettoPbDecoderField submessage field. Accepts a container // matcher for the subfields. //