Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Internal change. #16540

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 53 additions & 37 deletions src/google/protobuf/descriptor_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4060,18 +4060,24 @@ class ValidationErrorTest : public testing::Test {
}


// Add file_proto to the DescriptorPool. Expect errors to be produced which
// match the given error text.
void BuildFileWithErrors(const FileDescriptorProto& file_proto,
const std::string& expected_errors) {
MockErrorCollector error_collector;
EXPECT_TRUE(pool_.BuildFileCollectingErrors(file_proto, &error_collector) ==
nullptr);
EXPECT_EQ(expected_errors, error_collector.text_);
}

// Parse file_text as a FileDescriptorProto in text format and add it
// to the DescriptorPool. Expect errors to be produced which match the
// given error text.
void BuildFileWithErrors(const std::string& file_text,
const std::string& expected_errors) {
FileDescriptorProto file_proto;
ASSERT_TRUE(TextFormat::ParseFromString(file_text, &file_proto));

MockErrorCollector error_collector;
EXPECT_TRUE(pool_.BuildFileCollectingErrors(file_proto, &error_collector) ==
nullptr);
EXPECT_EQ(expected_errors, error_collector.text_);
BuildFileWithErrors(file_proto, expected_errors);
}

// Parse file_text as a FileDescriptorProto in text format and add it
Expand Down Expand Up @@ -6423,7 +6429,7 @@ TEST_F(ValidationErrorTest, MapEntryExtensionRange) {
" start: 10 end: 20 "
"} ",
file_proto.mutable_message_type(0)->mutable_nested_type(0));
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryExtension) {
Expand All @@ -6434,7 +6440,7 @@ TEST_F(ValidationErrorTest, MapEntryExtension) {
" name: 'foo_ext' extendee: '.Bar' number: 5"
"} ",
file_proto.mutable_message_type(0)->mutable_nested_type(0));
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryNestedType) {
Expand All @@ -6445,7 +6451,7 @@ TEST_F(ValidationErrorTest, MapEntryNestedType) {
" name: 'Bar' "
"} ",
file_proto.mutable_message_type(0)->mutable_nested_type(0));
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryEnumTypes) {
Expand All @@ -6457,7 +6463,7 @@ TEST_F(ValidationErrorTest, MapEntryEnumTypes) {
" value { name: 'BAR_BAR' number:0 } "
"} ",
file_proto.mutable_message_type(0)->mutable_nested_type(0));
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryExtraField) {
Expand All @@ -6471,7 +6477,7 @@ TEST_F(ValidationErrorTest, MapEntryExtraField) {
" number: 3 "
"} ",
file_proto.mutable_message_type(0)->mutable_nested_type(0));
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryMessageName) {
Expand All @@ -6481,15 +6487,15 @@ TEST_F(ValidationErrorTest, MapEntryMessageName) {
"OtherMapEntry");
file_proto.mutable_message_type(0)->mutable_field(0)->set_type_name(
"OtherMapEntry");
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryNoneRepeatedMapEntry) {
FileDescriptorProto file_proto;
FillValidMapEntry(&file_proto);
file_proto.mutable_message_type(0)->mutable_field(0)->set_label(
FieldDescriptorProto::LABEL_OPTIONAL);
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryDifferentContainingType) {
Expand All @@ -6499,7 +6505,7 @@ TEST_F(ValidationErrorTest, MapEntryDifferentContainingType) {
// the validation.
file_proto.mutable_message_type()->AddAllocated(
file_proto.mutable_message_type(0)->mutable_nested_type()->ReleaseLast());
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryKeyName) {
Expand All @@ -6509,7 +6515,7 @@ TEST_F(ValidationErrorTest, MapEntryKeyName) {
file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
0);
key->set_name("Key");
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryKeyLabel) {
Expand All @@ -6519,7 +6525,7 @@ TEST_F(ValidationErrorTest, MapEntryKeyLabel) {
file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
0);
key->set_label(FieldDescriptorProto::LABEL_REQUIRED);
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryKeyNumber) {
Expand All @@ -6529,7 +6535,7 @@ TEST_F(ValidationErrorTest, MapEntryKeyNumber) {
file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
0);
key->set_number(3);
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryValueName) {
Expand All @@ -6539,7 +6545,7 @@ TEST_F(ValidationErrorTest, MapEntryValueName) {
file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
1);
value->set_name("Value");
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryValueLabel) {
Expand All @@ -6549,7 +6555,7 @@ TEST_F(ValidationErrorTest, MapEntryValueLabel) {
file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
1);
value->set_label(FieldDescriptorProto::LABEL_REQUIRED);
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryValueNumber) {
Expand All @@ -6559,7 +6565,7 @@ TEST_F(ValidationErrorTest, MapEntryValueNumber) {
file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
1);
value->set_number(3);
BuildFileWithErrors(file_proto.DebugString(), kMapEntryErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryKeyTypeFloat) {
Expand All @@ -6569,7 +6575,7 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeFloat) {
file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
0);
key->set_type(FieldDescriptorProto::TYPE_FLOAT);
BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryKeyTypeErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryKeyTypeDouble) {
Expand All @@ -6579,7 +6585,7 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeDouble) {
file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
0);
key->set_type(FieldDescriptorProto::TYPE_DOUBLE);
BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryKeyTypeErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryKeyTypeBytes) {
Expand All @@ -6589,7 +6595,7 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeBytes) {
file_proto.mutable_message_type(0)->mutable_nested_type(0)->mutable_field(
0);
key->set_type(FieldDescriptorProto::TYPE_BYTES);
BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryKeyTypeErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryKeyTypeEnum) {
Expand All @@ -6605,14 +6611,14 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeEnum) {
EnumValueDescriptorProto* enum_value_proto = enum_proto->add_value();
enum_value_proto->set_name("BAR_VALUE0");
enum_value_proto->set_number(0);
BuildFileWithErrors(file_proto.DebugString(),
BuildFileWithErrors(file_proto,
"foo.proto: Foo.foo_map: TYPE: Key in map fields cannot "
"be enum types.\n");
// Enum keys are not allowed in proto3 as well.
// Get rid of extensions for proto3 to make it proto3 compatible.
file_proto.mutable_message_type()->RemoveLast();
file_proto.set_syntax("proto3");
BuildFileWithErrors(file_proto.DebugString(),
BuildFileWithErrors(file_proto,
"foo.proto: Foo.foo_map: TYPE: Key in map fields cannot "
"be enum types.\n");
}
Expand All @@ -6625,7 +6631,7 @@ TEST_F(ValidationErrorTest, MapEntryKeyTypeMessage) {
0);
key->clear_type();
key->set_type_name(".Bar");
BuildFileWithErrors(file_proto.DebugString(), kMapEntryKeyTypeErrorMessage);
BuildFileWithErrors(file_proto, kMapEntryKeyTypeErrorMessage);
}

TEST_F(ValidationErrorTest, MapEntryConflictsWithField) {
Expand All @@ -6640,7 +6646,7 @@ TEST_F(ValidationErrorTest, MapEntryConflictsWithField) {
"}",
file_proto.mutable_message_type(0));
BuildFileWithErrors(
file_proto.DebugString(),
file_proto,
"foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
"\"Foo\".\n"
"foo.proto: Foo.foo_map: TYPE: \"FooMapEntry\" is not defined.\n"
Expand All @@ -6657,7 +6663,7 @@ TEST_F(ValidationErrorTest, MapEntryConflictsWithMessage) {
"}",
file_proto.mutable_message_type(0));
BuildFileWithErrors(
file_proto.DebugString(),
file_proto,
"foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
"\"Foo\".\n"
"foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
Expand All @@ -6674,7 +6680,7 @@ TEST_F(ValidationErrorTest, MapEntryConflictsWithEnum) {
"}",
file_proto.mutable_message_type(0));
BuildFileWithErrors(
file_proto.DebugString(),
file_proto,
"foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
"\"Foo\".\n"
"foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
Expand Down Expand Up @@ -6860,7 +6866,7 @@ TEST_F(ValidationErrorTest, MapEntryConflictsWithOneof) {
"} ",
file_proto.mutable_message_type(0));
BuildFileWithErrors(
file_proto.DebugString(),
file_proto,
"foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
"\"Foo\".\n"
"foo.proto: Foo.foo_map: TYPE: \"FooMapEntry\" is not defined.\n"
Expand Down Expand Up @@ -7518,7 +7524,9 @@ TEST_F(FeaturesTest, Proto2Features) {
// Check round-trip consistency.
FileDescriptorProto proto;
file->CopyTo(&proto);
EXPECT_THAT(proto, EqualsProto(file_proto.DebugString()));
std::string file_textproto;
google::protobuf::TextFormat::PrintToString(file_proto, &file_textproto);
EXPECT_THAT(proto, EqualsProto(file_textproto));
}

TEST_F(FeaturesTest, Proto3Features) {
Expand Down Expand Up @@ -7588,7 +7596,9 @@ TEST_F(FeaturesTest, Proto3Features) {
// Check round-trip consistency.
FileDescriptorProto proto;
file->CopyTo(&proto);
EXPECT_THAT(proto, EqualsProto(file_proto.DebugString()));
std::string file_textproto;
google::protobuf::TextFormat::PrintToString(file_proto, &file_textproto);
EXPECT_THAT(proto, EqualsProto(file_textproto));
}

TEST_F(FeaturesTest, Proto2Proto3EnumFeatures) {
Expand Down Expand Up @@ -9778,7 +9788,7 @@ TEST_F(FeaturesTest, InvalidFieldMapImplicit) {
proto.set_name("foo.proto");

BuildDescriptorMessagesInTestPool();
BuildFileWithErrors(proto.DebugString(),
BuildFileWithErrors(proto,
"foo.proto: Foo.bar: NAME: Repeated fields can't specify "
"field presence.\n");
}
Expand Down Expand Up @@ -10021,7 +10031,7 @@ TEST_F(FeaturesTest, InvalidFieldMapWithMessageEncoding) {

BuildDescriptorMessagesInTestPool();
BuildFileWithErrors(
proto.DebugString(),
proto,
"foo.proto: Foo.bar: NAME: Only message fields can specify message "
"encoding.\n");
}
Expand Down Expand Up @@ -11819,9 +11829,12 @@ TEST_F(DatabaseBackedPoolTest, UnittestProto) {
TEST_F(DatabaseBackedPoolTest, FeatureResolution) {
FileDescriptorProto proto;
FileDescriptorProto::descriptor()->file()->CopyTo(&proto);
AddToDatabase(&database_, proto.DebugString());
std::string text_proto;
google::protobuf::TextFormat::PrintToString(proto, &text_proto);
AddToDatabase(&database_, text_proto);
pb::TestFeatures::descriptor()->file()->CopyTo(&proto);
AddToDatabase(&database_, proto.DebugString());
google::protobuf::TextFormat::PrintToString(proto, &text_proto);
AddToDatabase(&database_, text_proto);
AddToDatabase(&database_, R"pb(
name: "features.proto"
syntax: "editions"
Expand Down Expand Up @@ -11867,9 +11880,12 @@ TEST_F(DatabaseBackedPoolTest, FeatureResolution) {
TEST_F(DatabaseBackedPoolTest, FeatureLifetimeError) {
FileDescriptorProto proto;
FileDescriptorProto::descriptor()->file()->CopyTo(&proto);
AddToDatabase(&database_, proto.DebugString());
std::string text_proto;
google::protobuf::TextFormat::PrintToString(proto, &text_proto);
AddToDatabase(&database_, text_proto);
pb::TestFeatures::descriptor()->file()->CopyTo(&proto);
AddToDatabase(&database_, proto.DebugString());
google::protobuf::TextFormat::PrintToString(proto, &text_proto);
AddToDatabase(&database_, text_proto);
AddToDatabase(&database_, R"pb(
name: "features.proto"
syntax: "editions"
Expand Down
Loading