1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google Inc. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "google/protobuf/descriptor.pb.h"
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11 #include "google/protobuf/descriptor.h"
12 #include "editions/golden/test_messages_proto2_editions.pb.h"
13 #include "editions/golden/test_messages_proto3_editions.pb.h"
14 #include "editions/proto/test_editions_default_features.pb.h"
15 #include "google/protobuf/test_textproto.h"
16
17 // These tests provide some basic minimal coverage that protos work as expected.
18 // Full coverage will come as we migrate test protos to editions.
19
20 namespace google {
21 namespace protobuf {
22 namespace {
23
24 using ::protobuf_editions_test::EditionsDefaultMessage;
25 using ::protobuf_test_messages::editions::proto2::TestAllRequiredTypesProto2;
26 using ::protobuf_test_messages::editions::proto2::TestAllTypesProto2;
27 using ::protobuf_test_messages::editions::proto3::TestAllTypesProto3;
28 using ::testing::NotNull;
29
TEST(Generated,Parsing)30 TEST(Generated, Parsing) {
31 TestAllTypesProto2 message = ParseTextOrDie(R"pb(
32 Data { group_int32: 9 }
33 )pb");
34
35 EXPECT_EQ(message.data().group_int32(), 9);
36 }
37
TEST(Generated,GeneratedMethods)38 TEST(Generated, GeneratedMethods) {
39 TestAllTypesProto3 message;
40 message.set_optional_int32(9);
41
42 EXPECT_THAT(message, EqualsProto(R"pb(optional_int32: 9)pb"));
43 }
44
TEST(Generated,ExplicitPresence)45 TEST(Generated, ExplicitPresence) {
46 const FieldDescriptor* field =
47 TestAllTypesProto2::descriptor()->FindFieldByName("default_int32");
48 ASSERT_THAT(field, NotNull());
49 EXPECT_TRUE(field->has_presence());
50 }
51
TEST(Generated,RequiredPresence)52 TEST(Generated, RequiredPresence) {
53 const FieldDescriptor* field =
54 TestAllRequiredTypesProto2::descriptor()->FindFieldByName(
55 "required_int32");
56 ASSERT_THAT(field, NotNull());
57 EXPECT_TRUE(field->has_presence());
58 EXPECT_TRUE(field->is_required());
59 EXPECT_EQ(field->label(), FieldDescriptor::LABEL_REQUIRED);
60 }
61
TEST(Generated,ImplicitPresence)62 TEST(Generated, ImplicitPresence) {
63 const FieldDescriptor* field =
64 TestAllTypesProto3::descriptor()->FindFieldByName("optional_int32");
65 ASSERT_THAT(field, NotNull());
66 EXPECT_FALSE(field->has_presence());
67 }
68
TEST(Generated,ClosedEnum)69 TEST(Generated, ClosedEnum) {
70 const EnumDescriptor* enm =
71 TestAllTypesProto2::descriptor()->FindEnumTypeByName("NestedEnum");
72 ASSERT_THAT(enm, NotNull());
73 EXPECT_TRUE(enm->is_closed());
74 }
75
TEST(Generated,OpenEnum)76 TEST(Generated, OpenEnum) {
77 const EnumDescriptor* enm =
78 TestAllTypesProto3::descriptor()->FindEnumTypeByName("NestedEnum");
79 ASSERT_THAT(enm, NotNull());
80 EXPECT_FALSE(enm->is_closed());
81 }
82
TEST(Generated,PackedRepeated)83 TEST(Generated, PackedRepeated) {
84 const FieldDescriptor* field =
85 TestAllTypesProto2::descriptor()->FindFieldByName("packed_int32");
86 ASSERT_THAT(field, NotNull());
87 EXPECT_TRUE(field->is_packed());
88 }
89
TEST(Generated,ExpandedRepeated)90 TEST(Generated, ExpandedRepeated) {
91 const FieldDescriptor* field =
92 TestAllTypesProto2::descriptor()->FindFieldByName("repeated_int32");
93 ASSERT_THAT(field, NotNull());
94 EXPECT_FALSE(field->is_packed());
95 }
96
TEST(Generated,DoesNotEnforceUtf8)97 TEST(Generated, DoesNotEnforceUtf8) {
98 const FieldDescriptor* field =
99 TestAllTypesProto2::descriptor()->FindFieldByName("optional_string");
100 ASSERT_THAT(field, NotNull());
101 EXPECT_FALSE(field->requires_utf8_validation());
102 }
103
TEST(Generated,EnforceUtf8)104 TEST(Generated, EnforceUtf8) {
105 const FieldDescriptor* field =
106 TestAllTypesProto3::descriptor()->FindFieldByName("optional_string");
107 ASSERT_THAT(field, NotNull());
108 EXPECT_TRUE(field->requires_utf8_validation());
109 }
110
TEST(Generated,DelimitedEncoding)111 TEST(Generated, DelimitedEncoding) {
112 const FieldDescriptor* field =
113 TestAllTypesProto2::descriptor()->FindFieldByName("data");
114 ASSERT_THAT(field, NotNull());
115 EXPECT_EQ(field->type(), FieldDescriptor::TYPE_GROUP);
116 }
117
TEST(Generated,LengthPrefixedEncoding)118 TEST(Generated, LengthPrefixedEncoding) {
119 const FieldDescriptor* field =
120 TestAllTypesProto2::descriptor()->FindFieldByName(
121 "optional_nested_message");
122 ASSERT_THAT(field, NotNull());
123 EXPECT_EQ(field->type(), FieldDescriptor::TYPE_MESSAGE);
124 }
125
TEST(Generated,EditionDefaults2023)126 TEST(Generated, EditionDefaults2023) {
127 const Descriptor* desc = EditionsDefaultMessage::descriptor();
128 EXPECT_TRUE(desc->FindFieldByName("int32_field")->has_presence());
129 EXPECT_TRUE(
130 desc->FindFieldByName("string_field")->requires_utf8_validation());
131 EXPECT_FALSE(desc->FindFieldByName("enum_field")
132 ->legacy_enum_field_treated_as_closed());
133 EXPECT_FALSE(desc->FindFieldByName("enum_field")->enum_type()->is_closed());
134 EXPECT_TRUE(desc->FindFieldByName("repeated_int32_field")->is_packed());
135 EXPECT_EQ(desc->FindFieldByName("sub_message_field")->type(),
136 FieldDescriptor::TYPE_MESSAGE);
137 }
138
TEST(Generated,EditionDefaults2023InternalFeatures)139 TEST(Generated, EditionDefaults2023InternalFeatures) {
140 EXPECT_THAT(internal::InternalFeatureHelper::GetFeatures(
141 *EditionsDefaultMessage::descriptor()),
142 google::protobuf::EqualsProto(R"pb(
143 field_presence: EXPLICIT
144 enum_type: OPEN
145 repeated_field_encoding: PACKED
146 utf8_validation: VERIFY
147 message_encoding: LENGTH_PREFIXED
148 json_format: ALLOW
149 [pb.cpp] {
150 legacy_closed_enum: false
151 string_type: STRING
152 enum_name_uses_string_view: false
153 }
154 )pb"));
155 }
156
157 } // namespace
158 } // namespace protobuf
159 } // namespace google
160