From 7de0ad1adb0198ce75a36b39cca1f1be19fa85ae Mon Sep 17 00:00:00 2001 From: Mike Kruskal Date: Tue, 5 Mar 2024 17:00:10 -0800 Subject: [PATCH] Add a unittest locking down presence behavior for proto3 extensions PiperOrigin-RevId: 613016814 --- src/google/protobuf/BUILD.bazel | 1 + src/google/protobuf/extension_set_unittest.cc | 14 ++++++++++++++ .../protobuf/unittest_proto3_extensions.proto | 13 +++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/google/protobuf/unittest_proto3_extensions.proto diff --git a/src/google/protobuf/BUILD.bazel b/src/google/protobuf/BUILD.bazel index 5655b4b9bc..02825e7d69 100644 --- a/src/google/protobuf/BUILD.bazel +++ b/src/google/protobuf/BUILD.bazel @@ -828,6 +828,7 @@ filegroup( "unittest_proto3_arena.proto", "unittest_proto3_arena_lite.proto", "unittest_proto3_bad_macros.proto", + "unittest_proto3_extensions.proto", "unittest_proto3_lite.proto", "unittest_proto3_optional.proto", "unittest_retention.proto", diff --git a/src/google/protobuf/extension_set_unittest.cc b/src/google/protobuf/extension_set_unittest.cc index cbdddc7d11..019eec6da3 100644 --- a/src/google/protobuf/extension_set_unittest.cc +++ b/src/google/protobuf/extension_set_unittest.cc @@ -32,6 +32,7 @@ #include "google/protobuf/unittest.pb.h" #include "google/protobuf/unittest.pb.h" #include "google/protobuf/unittest_mset.pb.h" +#include "google/protobuf/unittest_proto3_extensions.pb.h" #include "google/protobuf/wire_format.h" #include "google/protobuf/wire_format_lite.h" @@ -1393,6 +1394,19 @@ TEST(ExtensionSetTest, Proto3PackedDynamicExtensions) { EXPECT_EQ(reserialized_options, "\xca\xb5\x18\x01\x01"); } +TEST(ExtensionSetTest, Proto3ExtensionPresenceSingular) { + using protobuf_unittest::Proto3FileExtensions; + FileDescriptorProto file; + + EXPECT_FALSE(file.options().HasExtension(Proto3FileExtensions::singular_int)); + EXPECT_EQ(file.options().GetExtension(Proto3FileExtensions::singular_int), 0); + + file.mutable_options()->SetExtension(Proto3FileExtensions::singular_int, 1); + + EXPECT_TRUE(file.options().HasExtension(Proto3FileExtensions::singular_int)); + EXPECT_EQ(file.options().GetExtension(Proto3FileExtensions::singular_int), 1); +} + TEST(ExtensionSetTest, BoolExtension) { unittest::TestAllExtensions msg; uint8_t wire_bytes[2] = {13 * 8, 42 /* out of bounds payload for bool */}; diff --git a/src/google/protobuf/unittest_proto3_extensions.proto b/src/google/protobuf/unittest_proto3_extensions.proto new file mode 100644 index 0000000000..928a84aae0 --- /dev/null +++ b/src/google/protobuf/unittest_proto3_extensions.proto @@ -0,0 +1,13 @@ +syntax = "proto3"; + +package protobuf_unittest; + +import "google/protobuf/descriptor.proto"; + +// For testing proto3 extension behaviors. +message Proto3FileExtensions { + extend google.protobuf.FileOptions { + int32 singular_int = 1001; + repeated int32 repeated_int = 1002; + } +}