From 2471cd8a71538e60242a00d21b730fe1641ec81d Mon Sep 17 00:00:00 2001 From: Daniele Di Proietto Date: Mon, 25 Sep 2023 15:57:50 +0000 Subject: [PATCH] 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. //