2424#include " absl/status/status.h"
2525#include " absl/strings/str_cat.h"
2626#include " absl/strings/string_view.h"
27+ #include " absl/strings/substitute.h"
2728#include " json/json.h"
2829#include " conformance/conformance.pb.h"
2930#include " conformance_test.h"
31+ #include " google/protobuf/editions/golden/test_messages_proto2_editions.pb.h"
32+ #include " google/protobuf/editions/golden/test_messages_proto3_editions.pb.h"
3033#include " google/protobuf/endian.h"
3134#include " google/protobuf/json/json.h"
3235#include " google/protobuf/test_messages_proto2.pb.h"
@@ -47,6 +50,10 @@ using google::protobuf::internal::little_endian::FromHost;
4750using google::protobuf::util::NewTypeResolverForDescriptorPool;
4851using protobuf_test_messages::proto2::TestAllTypesProto2;
4952using protobuf_test_messages::proto3::TestAllTypesProto3;
53+ using TestAllTypesProto2Editions =
54+ protobuf_test_messages::editions::proto2::TestAllTypesProto2;
55+ using TestAllTypesProto3Editions =
56+ protobuf_test_messages::editions::proto3::TestAllTypesProto3;
5057
5158namespace {
5259
@@ -325,12 +332,17 @@ bool BinaryAndJsonConformanceSuite::ParseResponse(
325332void BinaryAndJsonConformanceSuite::RunSuiteImpl () {
326333 type_resolver_.reset (NewTypeResolverForDescriptorPool (
327334 kTypeUrlPrefix , DescriptorPool::generated_pool ()));
328- type_url_ = GetTypeUrl (TestAllTypesProto3::descriptor ());
329335
330336 BinaryAndJsonConformanceSuiteImpl<TestAllTypesProto3>(
331337 this , /* run_proto3_tests=*/ true );
332338 BinaryAndJsonConformanceSuiteImpl<TestAllTypesProto2>(
333339 this , /* run_proto3_tests=*/ false );
340+ if (maximum_edition_ >= Edition::EDITION_2023) {
341+ BinaryAndJsonConformanceSuiteImpl<TestAllTypesProto3Editions>(
342+ this , /* run_proto3_tests=*/ true );
343+ BinaryAndJsonConformanceSuiteImpl<TestAllTypesProto2Editions>(
344+ this , /* run_proto3_tests=*/ false );
345+ }
334346}
335347
336348template <typename MessageType>
@@ -413,8 +425,7 @@ template <typename MessageType>
413425void BinaryAndJsonConformanceSuiteImpl<MessageType>::
414426 RunValidJsonTestWithProtobufInput (
415427 const std::string& test_name, ConformanceLevel level,
416- const TestAllTypesProto3& input,
417- const std::string& equivalent_text_format) {
428+ const MessageType& input, const std::string& equivalent_text_format) {
418429 ConformanceRequestSetting setting (
419430 level, conformance::PROTOBUF, conformance::JSON, conformance::JSON_TEST,
420431 input, test_name, input.SerializeAsString ());
@@ -427,7 +438,7 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::
427438 ConformanceLevel level,
428439 const std::string& input_json,
429440 const std::string& equivalent_text_format) {
430- TestAllTypesProto3 prototype;
441+ MessageType prototype;
431442 ConformanceRequestSetting setting (
432443 level, conformance::JSON, conformance::PROTOBUF,
433444 conformance::JSON_IGNORE_UNKNOWN_PARSING_TEST, prototype, test_name,
@@ -560,16 +571,17 @@ template <typename MessageType>
560571void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForJson(
561572 const std::string& test_name, ConformanceLevel level,
562573 const std::string& input_json) {
563- TestAllTypesProto3 prototype;
574+ MessageType prototype;
564575 // We don't expect output, but if the program erroneously accepts the protobuf
565576 // we let it send its response as this. We must not leave it unspecified.
566577 ConformanceRequestSetting setting (level, conformance::JSON, conformance::JSON,
567578 conformance::JSON_TEST, prototype,
568579 test_name, input_json);
569580 const ConformanceRequest& request = setting.GetRequest ();
570581 ConformanceResponse response;
571- std::string effective_test_name = absl::StrCat (
572- setting.ConformanceLevelToString (level), " .Proto3.JsonInput." , test_name);
582+ std::string effective_test_name =
583+ absl::StrCat (setting.ConformanceLevelToString (level), " ." ,
584+ SyntaxIdentifier (), " .JsonInput." , test_name);
573585
574586 suite_.RunTest (effective_test_name, request, &response);
575587 if (response.result_case () == ConformanceResponse::kParseError ) {
@@ -587,18 +599,19 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::
587599 ExpectSerializeFailureForJson (const std::string& test_name,
588600 ConformanceLevel level,
589601 const std::string& text_format) {
590- TestAllTypesProto3 payload_message;
602+ MessageType payload_message;
591603 ABSL_CHECK (TextFormat::ParseFromString (text_format, &payload_message))
592604 << " Failed to parse: " << text_format;
593605
594- TestAllTypesProto3 prototype;
606+ MessageType prototype;
595607 ConformanceRequestSetting setting (
596608 level, conformance::PROTOBUF, conformance::JSON, conformance::JSON_TEST,
597609 prototype, test_name, payload_message.SerializeAsString ());
598610 const ConformanceRequest& request = setting.GetRequest ();
599611 ConformanceResponse response;
600- std::string effective_test_name = absl::StrCat (
601- setting.ConformanceLevelToString (level), " ." , test_name, " .JsonOutput" );
612+ std::string effective_test_name =
613+ absl::StrCat (setting.ConformanceLevelToString (level), " ." ,
614+ SyntaxIdentifier (), " ." , test_name, " .JsonOutput" );
602615
603616 suite_.RunTest (effective_test_name, request, &response);
604617 if (response.result_case () == ConformanceResponse::kSerializeError ) {
@@ -1330,6 +1343,7 @@ BinaryAndJsonConformanceSuiteImpl<MessageType>::
13301343 BinaryAndJsonConformanceSuiteImpl (BinaryAndJsonConformanceSuite* suite,
13311344 bool run_proto3_tests)
13321345 : suite_(*ABSL_DIE_IF_NULL (suite)), run_proto3_tests_(run_proto3_tests) {
1346+ suite_.SetTypeUrl (GetTypeUrl (MessageType::GetDescriptor ()));
13331347 RunAllTests ();
13341348}
13351349
@@ -1666,15 +1680,18 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTests() {
16661680 "FieldName13": 0
16671681 })" ,
16681682 [](const Json::Value& value) { return value.isMember (" FieldName13" ); });
1669- RunValidJsonTestWithValidator (
1670- " FieldNameExtension" , RECOMMENDED,
1671- R"( {
1672- "[protobuf_test_messages.proto2.extension_int32]": 1
1683+ std::vector<const FieldDescriptor*> extensions;
1684+ MessageType::GetDescriptor ()->file ()->pool ()->FindAllExtensions (
1685+ MessageType::GetDescriptor (), &extensions);
1686+ RunValidJsonTestWithValidator (" FieldNameExtension" , RECOMMENDED,
1687+ absl::Substitute (R"( {
1688+ "[$0]": 1
16731689 })" ,
1674- [](const Json::Value& value) {
1675- return value.isMember (
1676- " [protobuf_test_messages.proto2.extension_int32]" );
1677- });
1690+ extensions[0 ]->full_name ()),
1691+ [&](const Json::Value& value) {
1692+ return value.isMember (absl::StrCat (
1693+ " [" , extensions[0 ]->full_name (), " ]" ));
1694+ });
16781695 return ;
16791696 }
16801697 RunValidJsonTest (" HelloWorld" , REQUIRED,
@@ -2175,7 +2192,7 @@ void BinaryAndJsonConformanceSuiteImpl<
21752192 R"( {"optionalFloat": "-Infinity"})" , " optional_float: -inf" );
21762193 // Non-canonical Nan will be correctly normalized.
21772194 {
2178- TestAllTypesProto3 message;
2195+ MessageType message;
21792196 // IEEE floating-point standard 32-bit quiet NaN:
21802197 // 0111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
21812198 message.set_optional_float (WireFormatLite::DecodeFloat (0x7FA12345 ));
@@ -2227,7 +2244,7 @@ void BinaryAndJsonConformanceSuiteImpl<
22272244 " optional_double: -inf" );
22282245 // Non-canonical Nan will be correctly normalized.
22292246 {
2230- TestAllTypesProto3 message;
2247+ MessageType message;
22312248 message.set_optional_double (
22322249 WireFormatLite::DecodeDouble (int64_t {0x7FFA123456789ABC }));
22332250 RunValidJsonTestWithProtobufInput (" DoubleFieldNormalizeQuietNan" , REQUIRED,
@@ -3008,54 +3025,61 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTestsForValue() {
30083025
30093026template <typename MessageType>
30103027void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTestsForAny() {
3028+ std::string type_url = GetTypeUrl (MessageType::GetDescriptor ());
30113029 RunValidJsonTest (" Any" , REQUIRED,
3012- R"( {
3030+ absl::Substitute ( R"( {
30133031 "optionalAny": {
3014- "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3 ",
3032+ "@type": "$0 ",
30153033 "optionalInt32": 12345
30163034 }
30173035 })" ,
3018- R"(
3036+ GetTypeUrl (MessageType::GetDescriptor ())),
3037+ absl::Substitute (R"(
30193038 optional_any: {
3020- [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3 ] {
3039+ [$0 ] {
30213040 optional_int32: 12345
3022- }
3023- }
3024- )" );
3041+ }
3042+ }
3043+ )" ,
3044+ type_url));
30253045 RunValidJsonTest (" AnyNested" , REQUIRED,
3026- R"( {
3046+ absl::Substitute ( R"( {
30273047 "optionalAny": {
30283048 "@type": "type.googleapis.com/google.protobuf.Any",
30293049 "value": {
3030- "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3 ",
3050+ "@type": "$0 ",
30313051 "optionalInt32": 12345
30323052 }
30333053 }
30343054 })" ,
3035- R"(
3055+ type_url),
3056+ absl::Substitute (R"(
30363057 optional_any: {
3037- [type.googleapis.com/google.protobuf.Any] {
3038- [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3 ] {
3058+ [type.googleapis.com/google.protobuf.Any] {
3059+ [$0 ] {
30393060 optional_int32: 12345
3040- }
3041- }
3042- }
3043- )" );
3061+ }
3062+ }
3063+ }
3064+ )" ,
3065+ type_url));
30443066 // The special "@type" tag is not required to appear first.
30453067 RunValidJsonTest (" AnyUnorderedTypeTag" , REQUIRED,
3046- R"( {
3068+ absl::Substitute ( R"( {
30473069 "optionalAny": {
30483070 "optionalInt32": 12345,
3049- "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3 "
3050- }
3071+ "@type": "$0 "
3072+ }
30513073 })" ,
3052- R"(
3074+ type_url),
3075+ absl::Substitute (R"(
30533076 optional_any: {
3054- [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3 ] {
3077+ [$0 ] {
30553078 optional_int32: 12345
3056- }
3057- }
3058- )" );
3079+ }
3080+ }
3081+ )" ,
3082+ type_url));
30593083 // Well-known types in Any.
30603084 RunValidJsonTest (" AnyWithInt32ValueWrapper" , REQUIRED,
30613085 R"( {
@@ -3253,8 +3277,13 @@ std::string BinaryAndJsonConformanceSuiteImpl<MessageType>::SyntaxIdentifier()
32533277 const {
32543278 if constexpr (std::is_same<MessageType, TestAllTypesProto2>::value) {
32553279 return " Proto2" ;
3256- } else {
3280+ } else if constexpr (std::is_same<MessageType, TestAllTypesProto3>::value) {
32573281 return " Proto3" ;
3282+ } else if constexpr (std::is_same<MessageType,
3283+ TestAllTypesProto2Editions>::value) {
3284+ return " Editions_Proto2" ;
3285+ } else {
3286+ return " Editions_Proto3" ;
32583287 }
32593288}
32603289
0 commit comments