• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 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 "binary_json_conformance_suite.h"
9 
10 #include <cassert>
11 #include <cctype>
12 #include <cstddef>
13 #include <cstdint>
14 #include <cstring>
15 #include <memory>
16 #include <string>
17 #include <type_traits>
18 #include <utility>
19 #include <vector>
20 
21 #include "absl/log/absl_check.h"
22 #include "absl/log/absl_log.h"
23 #include "absl/log/die_if_null.h"
24 #include "absl/status/status.h"
25 #include "absl/strings/str_cat.h"
26 #include "absl/strings/str_format.h"
27 #include "absl/strings/string_view.h"
28 #include "absl/strings/substitute.h"
29 #include "json/config.h"
30 #include "json/reader.h"
31 #include "json/value.h"
32 #include "conformance/conformance.pb.h"
33 #include "conformance_test.h"
34 #include "conformance/test_protos/test_messages_edition2023.pb.h"
35 #include "editions/golden/test_messages_proto2_editions.pb.h"
36 #include "editions/golden/test_messages_proto3_editions.pb.h"
37 #include "google/protobuf/endian.h"
38 #include "google/protobuf/json/json.h"
39 #include "google/protobuf/test_messages_proto2.pb.h"
40 #include "google/protobuf/test_messages_proto3.pb.h"
41 #include "google/protobuf/text_format.h"
42 #include "google/protobuf/unknown_field_set.h"
43 #include "google/protobuf/util/type_resolver_util.h"
44 #include "google/protobuf/wire_format_lite.h"
45 
46 using conformance::ConformanceRequest;
47 using conformance::ConformanceResponse;
48 using conformance::TestStatus;
49 using conformance::WireFormat;
50 using google::protobuf::Descriptor;
51 using google::protobuf::FieldDescriptor;
52 using google::protobuf::internal::WireFormatLite;
53 using google::protobuf::internal::little_endian::FromHost;
54 using google::protobuf::util::NewTypeResolverForDescriptorPool;
55 using protobuf_test_messages::editions::TestAllTypesEdition2023;
56 using protobuf_test_messages::proto2::TestAllTypesProto2;
57 using protobuf_test_messages::proto3::TestAllTypesProto3;
58 using TestAllTypesProto2Editions =
59     protobuf_test_messages::editions::proto2::TestAllTypesProto2;
60 using TestAllTypesProto3Editions =
61     protobuf_test_messages::editions::proto3::TestAllTypesProto3;
62 
63 namespace {
64 
65 constexpr absl::string_view kTypeUrlPrefix = "type.googleapis.com";
66 
67 // The number of repetitions to use for performance tests.
68 // Corresponds approx to 500KB wireformat bytes.
69 const size_t kPerformanceRepeatCount = 50000;
70 
GetTypeUrl(const Descriptor * message)71 std::string GetTypeUrl(const Descriptor* message) {
72   return absl::StrCat(kTypeUrlPrefix, "/", message->full_name());
73 }
74 
75 /* Routines for building arbitrary protos *************************************/
76 
77 // We would use CodedOutputStream except that we want more freedom to build
78 // arbitrary protos (even invalid ones).
79 
80 // The maximum number of bytes that it takes to encode a 64-bit varint.
81 #define VARINT_MAX_LEN 10
82 
vencode64(uint64_t val,int over_encoded_bytes,char * buf)83 size_t vencode64(uint64_t val, int over_encoded_bytes, char* buf) {
84   if (val == 0) {
85     buf[0] = 0;
86     return 1;
87   }
88   size_t i = 0;
89   while (val) {
90     uint8_t byte = val & 0x7fU;
91     val >>= 7;
92     if (val || over_encoded_bytes) byte |= 0x80U;
93     buf[i++] = byte;
94   }
95   while (over_encoded_bytes--) {
96     assert(i < 10);
97     uint8_t byte = over_encoded_bytes ? 0x80 : 0;
98     buf[i++] = byte;
99   }
100   return i;
101 }
102 
varint(uint64_t x)103 std::string varint(uint64_t x) {
104   char buf[VARINT_MAX_LEN];
105   size_t len = vencode64(x, 0, buf);
106   return std::string(buf, len);
107 }
108 
109 // Encodes a varint that is |extra| bytes longer than it needs to be, but still
110 // valid.
longvarint(uint64_t x,int extra)111 std::string longvarint(uint64_t x, int extra) {
112   char buf[VARINT_MAX_LEN];
113   size_t len = vencode64(x, extra, buf);
114   return std::string(buf, len);
115 }
116 
fixed32(void * data)117 std::string fixed32(void* data) {
118   uint32_t data_le;
119   std::memcpy(&data_le, data, 4);
120   data_le = FromHost(data_le);
121   return std::string(reinterpret_cast<char*>(&data_le), 4);
122 }
fixed64(void * data)123 std::string fixed64(void* data) {
124   uint64_t data_le;
125   std::memcpy(&data_le, data, 8);
126   data_le = FromHost(data_le);
127   return std::string(reinterpret_cast<char*>(&data_le), 8);
128 }
129 
delim(const std::string & buf)130 std::string delim(const std::string& buf) {
131   return absl::StrCat(varint(buf.size()), buf);
132 }
u32(uint32_t u32)133 std::string u32(uint32_t u32) { return fixed32(&u32); }
u64(uint64_t u64)134 std::string u64(uint64_t u64) { return fixed64(&u64); }
flt(float f)135 std::string flt(float f) { return fixed32(&f); }
dbl(double d)136 std::string dbl(double d) { return fixed64(&d); }
zz32(int32_t x)137 std::string zz32(int32_t x) {
138   return varint(WireFormatLite::ZigZagEncode32(x));
139 }
zz64(int64_t x)140 std::string zz64(int64_t x) {
141   return varint(WireFormatLite::ZigZagEncode64(x));
142 }
143 
tag(uint32_t fieldnum,char wire_type)144 std::string tag(uint32_t fieldnum, char wire_type) {
145   return varint((fieldnum << 3) | wire_type);
146 }
147 
tag(int fieldnum,char wire_type)148 std::string tag(int fieldnum, char wire_type) {
149   return tag(static_cast<uint32_t>(fieldnum), wire_type);
150 }
151 
field(uint32_t fieldnum,char wire_type,std::string content)152 std::string field(uint32_t fieldnum, char wire_type, std::string content) {
153   return absl::StrCat(tag(fieldnum, wire_type), content);
154 }
155 
group(uint32_t fieldnum,std::string content)156 std::string group(uint32_t fieldnum, std::string content) {
157   return absl::StrCat(tag(fieldnum, WireFormatLite::WIRETYPE_START_GROUP),
158                       content,
159                       tag(fieldnum, WireFormatLite::WIRETYPE_END_GROUP));
160 }
161 
len(uint32_t fieldnum,std::string content)162 std::string len(uint32_t fieldnum, std::string content) {
163   return absl::StrCat(tag(fieldnum, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
164                       delim(content));
165 }
166 
GetDefaultValue(FieldDescriptor::Type type)167 std::string GetDefaultValue(FieldDescriptor::Type type) {
168   switch (type) {
169     case FieldDescriptor::TYPE_INT32:
170     case FieldDescriptor::TYPE_INT64:
171     case FieldDescriptor::TYPE_UINT32:
172     case FieldDescriptor::TYPE_UINT64:
173     case FieldDescriptor::TYPE_ENUM:
174     case FieldDescriptor::TYPE_BOOL:
175       return varint(0);
176     case FieldDescriptor::TYPE_SINT32:
177       return zz32(0);
178     case FieldDescriptor::TYPE_SINT64:
179       return zz64(0);
180     case FieldDescriptor::TYPE_FIXED32:
181     case FieldDescriptor::TYPE_SFIXED32:
182       return u32(0);
183     case FieldDescriptor::TYPE_FIXED64:
184     case FieldDescriptor::TYPE_SFIXED64:
185       return u64(0);
186     case FieldDescriptor::TYPE_FLOAT:
187       return flt(0);
188     case FieldDescriptor::TYPE_DOUBLE:
189       return dbl(0);
190     case FieldDescriptor::TYPE_STRING:
191     case FieldDescriptor::TYPE_BYTES:
192     case FieldDescriptor::TYPE_MESSAGE:
193       return delim("");
194     default:
195       return "";
196   }
197   return "";
198 }
199 
GetNonDefaultValue(FieldDescriptor::Type type)200 std::string GetNonDefaultValue(FieldDescriptor::Type type) {
201   switch (type) {
202     case FieldDescriptor::TYPE_INT32:
203     case FieldDescriptor::TYPE_INT64:
204     case FieldDescriptor::TYPE_UINT32:
205     case FieldDescriptor::TYPE_UINT64:
206     case FieldDescriptor::TYPE_ENUM:
207     case FieldDescriptor::TYPE_BOOL:
208       return varint(1);
209     case FieldDescriptor::TYPE_SINT32:
210       return zz32(1);
211     case FieldDescriptor::TYPE_SINT64:
212       return zz64(1);
213     case FieldDescriptor::TYPE_FIXED32:
214     case FieldDescriptor::TYPE_SFIXED32:
215       return u32(1);
216     case FieldDescriptor::TYPE_FIXED64:
217     case FieldDescriptor::TYPE_SFIXED64:
218       return u64(1);
219     case FieldDescriptor::TYPE_FLOAT:
220       return flt(1);
221     case FieldDescriptor::TYPE_DOUBLE:
222       return dbl(1);
223     case FieldDescriptor::TYPE_STRING:
224     case FieldDescriptor::TYPE_BYTES:
225       return delim("a");
226     case FieldDescriptor::TYPE_MESSAGE:
227       return delim(
228           absl::StrCat(tag(1, WireFormatLite::WIRETYPE_VARINT), varint(1234)));
229     default:
230       return "";
231   }
232   return "";
233 }
234 
235 #define UNKNOWN_FIELD 666
236 
UpperCase(std::string str)237 std::string UpperCase(std::string str) {
238   for (size_t i = 0; i < str.size(); i++) {
239     str[i] = toupper(str[i]);
240   }
241   return str;
242 }
243 
IsProto3Default(FieldDescriptor::Type type,const std::string & binary_data)244 bool IsProto3Default(FieldDescriptor::Type type,
245                      const std::string& binary_data) {
246   switch (type) {
247     case FieldDescriptor::TYPE_DOUBLE:
248       return binary_data == dbl(0);
249     case FieldDescriptor::TYPE_FLOAT:
250       return binary_data == flt(0);
251     case FieldDescriptor::TYPE_BOOL:
252     case FieldDescriptor::TYPE_INT64:
253     case FieldDescriptor::TYPE_UINT64:
254     case FieldDescriptor::TYPE_INT32:
255     case FieldDescriptor::TYPE_UINT32:
256     case FieldDescriptor::TYPE_SINT32:
257     case FieldDescriptor::TYPE_SINT64:
258     case FieldDescriptor::TYPE_ENUM:
259       return binary_data == varint(0);
260     case FieldDescriptor::TYPE_FIXED64:
261     case FieldDescriptor::TYPE_SFIXED64:
262       return binary_data == u64(0);
263     case FieldDescriptor::TYPE_FIXED32:
264     case FieldDescriptor::TYPE_SFIXED32:
265       return binary_data == u32(0);
266     case FieldDescriptor::TYPE_STRING:
267     case FieldDescriptor::TYPE_BYTES:
268       return binary_data == delim("");
269     default:
270       return false;
271   }
272 }
273 
274 }  // anonymous namespace
275 
276 namespace google {
277 namespace protobuf {
278 
ParseJsonResponse(const ConformanceResponse & response,Message * test_message)279 bool BinaryAndJsonConformanceSuite::ParseJsonResponse(
280     const ConformanceResponse& response, Message* test_message) {
281   std::string binary_protobuf;
282   absl::Status status =
283       json::JsonToBinaryString(type_resolver_.get(), type_url_,
284                                response.json_payload(), &binary_protobuf);
285 
286   if (!status.ok()) {
287     ABSL_LOG(ERROR) << status;
288     return false;
289   }
290 
291   if (!test_message->ParseFromString(binary_protobuf)) {
292     ABSL_LOG(FATAL) << "INTERNAL ERROR: internal JSON->protobuf transcode "
293                     << "yielded unparseable proto.";
294     return false;
295   }
296 
297   return true;
298 }
299 
ParseResponse(const ConformanceResponse & response,const ConformanceRequestSetting & setting,Message * test_message)300 bool BinaryAndJsonConformanceSuite::ParseResponse(
301     const ConformanceResponse& response,
302     const ConformanceRequestSetting& setting, Message* test_message) {
303   const ConformanceRequest& request = setting.GetRequest();
304   WireFormat requested_output = request.requested_output_format();
305   const std::string& test_name = setting.GetTestName();
306   ConformanceLevel level = setting.GetLevel();
307 
308   TestStatus test;
309   test.set_name(test_name);
310   switch (response.result_case()) {
311     case ConformanceResponse::kProtobufPayload: {
312       if (requested_output != conformance::PROTOBUF) {
313         test.set_failure_message(absl::StrCat(
314             "Test was asked for ", WireFormatToString(requested_output),
315             " output but provided PROTOBUF instead."));
316         ReportFailure(test, level, request, response);
317         return false;
318       }
319 
320       if (!test_message->ParseFromString(response.protobuf_payload())) {
321         test.set_failure_message(
322             "Protobuf output we received from test was unparseable.");
323         ReportFailure(test, level, request, response);
324         return false;
325       }
326 
327       break;
328     }
329 
330     case ConformanceResponse::kJsonPayload: {
331       if (requested_output != conformance::JSON) {
332         test.set_failure_message(absl::StrCat(
333             "Test was asked for ", WireFormatToString(requested_output),
334             " output but provided JSON instead."));
335         ReportFailure(test, level, request, response);
336         return false;
337       }
338 
339       if (!ParseJsonResponse(response, test_message)) {
340         test.set_failure_message(
341             "JSON output we received from test was unparseable.");
342         ReportFailure(test, level, request, response);
343         return false;
344       }
345 
346       break;
347     }
348 
349     default:
350       ABSL_LOG(FATAL) << test_name
351                       << ": unknown payload type: " << response.result_case()
352                       << ", response: " << response;
353   }
354 
355   return true;
356 }
357 
RunSuiteImpl()358 void BinaryAndJsonConformanceSuite::RunSuiteImpl() {
359   type_resolver_.reset(NewTypeResolverForDescriptorPool(
360       kTypeUrlPrefix, DescriptorPool::generated_pool()));
361 
362   BinaryAndJsonConformanceSuiteImpl<TestAllTypesProto3>(
363       this, /*run_proto3_tests=*/true);
364   BinaryAndJsonConformanceSuiteImpl<TestAllTypesProto2>(
365       this, /*run_proto3_tests=*/false);
366   if (maximum_edition_ >= Edition::EDITION_2023) {
367     BinaryAndJsonConformanceSuiteImpl<TestAllTypesProto3Editions>(
368         this, /*run_proto3_tests=*/true);
369     BinaryAndJsonConformanceSuiteImpl<TestAllTypesProto2Editions>(
370         this, /*run_proto3_tests=*/false);
371     RunDelimitedFieldTests();
372   }
373 }
374 
RunDelimitedFieldTests()375 void BinaryAndJsonConformanceSuite::RunDelimitedFieldTests() {
376   TestAllTypesEdition2023 prototype;
377   SetTypeUrl(GetTypeUrl(TestAllTypesEdition2023::GetDescriptor()));
378 
379   RunValidProtobufTest<TestAllTypesEdition2023>(
380       absl::StrCat("ValidNonMessage"), REQUIRED,
381       field(1, WireFormatLite::WIRETYPE_VARINT, varint(99)),
382       R"pb(optional_int32: 99)pb");
383 
384   RunValidProtobufTest<TestAllTypesEdition2023>(
385       absl::StrCat("ValidLengthPrefixedField"), REQUIRED,
386       len(18, field(1, WireFormatLite::WIRETYPE_VARINT, varint(99))),
387       R"pb(optional_nested_message { a: 99 })pb");
388 
389   RunValidProtobufTest<TestAllTypesEdition2023>(
390       absl::StrCat("ValidMap.Integer"), REQUIRED,
391       len(56,
392           absl::StrCat(field(1, WireFormatLite::WIRETYPE_VARINT, varint(99)),
393                        field(2, WireFormatLite::WIRETYPE_VARINT, varint(87)))),
394       R"pb(map_int32_int32 { key: 99 value: 87 })pb");
395 
396   RunValidProtobufTest<TestAllTypesEdition2023>(
397       absl::StrCat("ValidMap.LengthPrefixed"), REQUIRED,
398       len(71, absl::StrCat(len(1, "a"),
399                            len(2, field(1, WireFormatLite::WIRETYPE_VARINT,
400                                         varint(87))))),
401       R"pb(map_string_nested_message {
402              key: "a"
403              value: { a: 87 }
404            })pb");
405 
406   RunValidProtobufTest<TestAllTypesEdition2023>(
407       absl::StrCat("ValidDelimitedField.GroupLike"), REQUIRED,
408       group(201, field(202, WireFormatLite::WIRETYPE_VARINT, varint(99))),
409       R"pb(groupliketype { group_int32: 99 })pb");
410 
411   RunValidProtobufTest<TestAllTypesEdition2023>(
412       absl::StrCat("ValidDelimitedField.NotGroupLike"), REQUIRED,
413       group(202, field(202, WireFormatLite::WIRETYPE_VARINT, varint(99))),
414       R"pb(delimited_field { group_int32: 99 })pb");
415 
416   // Note: extensions don't work with TypeResolver, which is used by
417   // binary->JSON tests.
418   RunValidBinaryProtobufTest<TestAllTypesEdition2023>(
419       absl::StrCat("ValidDelimitedExtension.GroupLike"), REQUIRED,
420       group(121, field(1, WireFormatLite::WIRETYPE_VARINT, varint(99))),
421       R"pb([protobuf_test_messages.editions.groupliketype] { c: 99 })pb");
422 
423   RunValidBinaryProtobufTest<TestAllTypesEdition2023>(
424       absl::StrCat("ValidDelimitedExtension.NotGroupLike"), REQUIRED,
425       group(122, field(1, WireFormatLite::WIRETYPE_VARINT, varint(99))),
426       R"pb([protobuf_test_messages.editions.delimited_ext] { c: 99 })pb");
427 }
428 
429 template <typename MessageType>
430 void BinaryAndJsonConformanceSuiteImpl<MessageType>::
ExpectParseFailureForProtoWithProtoVersion(const std::string & proto,const std::string & test_name,ConformanceLevel level)431     ExpectParseFailureForProtoWithProtoVersion(const std::string& proto,
432                                                const std::string& test_name,
433                                                ConformanceLevel level) {
434   MessageType prototype;
435   // We don't expect output, but if the program erroneously accepts the protobuf
436   // we let it send its response as this.  We must not leave it unspecified.
437   ConformanceRequestSetting setting(
438       level, conformance::PROTOBUF, conformance::PROTOBUF,
439       conformance::BINARY_TEST, prototype, test_name, proto);
440 
441   const ConformanceRequest& request = setting.GetRequest();
442   ConformanceResponse response;
443   std::string effective_test_name =
444       absl::StrCat(setting.ConformanceLevelToString(level), ".",
445                    setting.GetSyntaxIdentifier(), ".ProtobufInput.", test_name);
446 
447   if (!suite_.RunTest(effective_test_name, request, &response)) {
448     return;
449   }
450 
451   TestStatus test;
452   test.set_name(effective_test_name);
453   if (response.result_case() == ConformanceResponse::kParseError) {
454     suite_.ReportSuccess(test);
455   } else if (response.result_case() == ConformanceResponse::kSkipped) {
456     suite_.ReportSkip(test, request, response);
457   } else {
458     test.set_failure_message("Should have failed to parse, but didn't.");
459     suite_.ReportFailure(test, level, request, response);
460   }
461 }
462 
463 // Expect that this precise protobuf will cause a parse error.
464 template <typename MessageType>
ExpectParseFailureForProto(const std::string & proto,const std::string & test_name,ConformanceLevel level)465 void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForProto(
466     const std::string& proto, const std::string& test_name,
467     ConformanceLevel level) {
468   ExpectParseFailureForProtoWithProtoVersion(proto, test_name, level);
469 }
470 
471 // Expect that this protobuf will cause a parse error, even if it is followed
472 // by valid protobuf data.  We can try running this twice: once with this
473 // data verbatim and once with this data followed by some valid data.
474 //
475 // TODO: implement the second of these.
476 template <typename MessageType>
477 void BinaryAndJsonConformanceSuiteImpl<
ExpectHardParseFailureForProto(const std::string & proto,const std::string & test_name,ConformanceLevel level)478     MessageType>::ExpectHardParseFailureForProto(const std::string& proto,
479                                                  const std::string& test_name,
480                                                  ConformanceLevel level) {
481   return ExpectParseFailureForProto(proto, test_name, level);
482 }
483 
484 template <typename MessageType>
RunValidJsonTest(const std::string & test_name,ConformanceLevel level,const std::string & input_json,const std::string & equivalent_text_format)485 void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidJsonTest(
486     const std::string& test_name, ConformanceLevel level,
487     const std::string& input_json, const std::string& equivalent_text_format) {
488   MessageType prototype;
489   RunValidJsonTestWithMessage(test_name, level, input_json,
490                               equivalent_text_format, prototype);
491 }
492 
493 template <typename MessageType>
494 void BinaryAndJsonConformanceSuiteImpl<MessageType>::
RunValidJsonTestWithMessage(const std::string & test_name,ConformanceLevel level,const std::string & input_json,const std::string & equivalent_text_format,const Message & prototype)495     RunValidJsonTestWithMessage(const std::string& test_name,
496                                 ConformanceLevel level,
497                                 const std::string& input_json,
498                                 const std::string& equivalent_text_format,
499                                 const Message& prototype) {
500   ConformanceRequestSetting setting1(
501       level, conformance::JSON, conformance::PROTOBUF, conformance::JSON_TEST,
502       prototype, test_name, input_json);
503   suite_.RunValidInputTest(setting1, equivalent_text_format);
504   ConformanceRequestSetting setting2(level, conformance::JSON,
505                                      conformance::JSON, conformance::JSON_TEST,
506                                      prototype, test_name, input_json);
507   suite_.RunValidInputTest(setting2, equivalent_text_format);
508 }
509 
510 template <typename MessageType>
511 void BinaryAndJsonConformanceSuiteImpl<MessageType>::
RunValidJsonTestWithProtobufInput(const std::string & test_name,ConformanceLevel level,const MessageType & input,const std::string & equivalent_text_format)512     RunValidJsonTestWithProtobufInput(
513         const std::string& test_name, ConformanceLevel level,
514         const MessageType& input, const std::string& equivalent_text_format) {
515   ConformanceRequestSetting setting(
516       level, conformance::PROTOBUF, conformance::JSON, conformance::JSON_TEST,
517       input, test_name, input.SerializeAsString());
518   suite_.RunValidInputTest(setting, equivalent_text_format);
519 }
520 
521 template <typename MessageType>
522 void BinaryAndJsonConformanceSuiteImpl<MessageType>::
RunValidJsonIgnoreUnknownTest(const std::string & test_name,ConformanceLevel level,const std::string & input_json,const std::string & equivalent_text_format)523     RunValidJsonIgnoreUnknownTest(const std::string& test_name,
524                                   ConformanceLevel level,
525                                   const std::string& input_json,
526                                   const std::string& equivalent_text_format) {
527   MessageType prototype;
528   ConformanceRequestSetting setting(
529       level, conformance::JSON, conformance::PROTOBUF,
530       conformance::JSON_IGNORE_UNKNOWN_PARSING_TEST, prototype, test_name,
531       input_json);
532   suite_.RunValidInputTest(setting, equivalent_text_format);
533 }
534 
535 template <typename MessageType>
RunValidBinaryProtobufTest(const std::string & test_name,ConformanceLevel level,const std::string & input_protobuf,const std::string & equivalent_text_format)536 void BinaryAndJsonConformanceSuite::RunValidBinaryProtobufTest(
537     const std::string& test_name, ConformanceLevel level,
538     const std::string& input_protobuf,
539     const std::string& equivalent_text_format) {
540   MessageType prototype;
541 
542   ConformanceRequestSetting binary_to_binary(
543       level, conformance::PROTOBUF, conformance::PROTOBUF,
544       conformance::BINARY_TEST, prototype, test_name, input_protobuf);
545   RunValidInputTest(binary_to_binary, equivalent_text_format);
546 }
547 
548 template <typename MessageType>
RunValidProtobufTest(const std::string & test_name,ConformanceLevel level,const std::string & input_protobuf,const std::string & equivalent_text_format)549 void BinaryAndJsonConformanceSuite::RunValidProtobufTest(
550     const std::string& test_name, ConformanceLevel level,
551     const std::string& input_protobuf,
552     const std::string& equivalent_text_format) {
553   MessageType prototype;
554 
555   ConformanceRequestSetting binary_to_binary(
556       level, conformance::PROTOBUF, conformance::PROTOBUF,
557       conformance::BINARY_TEST, prototype, test_name, input_protobuf);
558   RunValidInputTest(binary_to_binary, equivalent_text_format);
559 
560   ConformanceRequestSetting binary_to_json(
561       level, conformance::PROTOBUF, conformance::JSON, conformance::BINARY_TEST,
562       prototype, test_name, input_protobuf);
563   RunValidInputTest(binary_to_json, equivalent_text_format);
564 }
565 
566 template <typename MessageType>
RunValidProtobufTest(const std::string & test_name,ConformanceLevel level,const std::string & input_protobuf,const std::string & equivalent_text_format)567 void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidProtobufTest(
568     const std::string& test_name, ConformanceLevel level,
569     const std::string& input_protobuf,
570     const std::string& equivalent_text_format) {
571   suite_.RunValidProtobufTest<MessageType>(test_name, level, input_protobuf,
572                                            equivalent_text_format);
573 }
574 
575 template <typename MessageType>
RunValidBinaryProtobufTest(const std::string & test_name,ConformanceLevel level,const std::string & input_protobuf)576 void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidBinaryProtobufTest(
577     const std::string& test_name, ConformanceLevel level,
578     const std::string& input_protobuf) {
579   RunValidBinaryProtobufTest(test_name, level, input_protobuf, input_protobuf);
580 }
581 
582 template <typename MessageType>
RunValidBinaryProtobufTest(const std::string & test_name,ConformanceLevel level,const std::string & input_protobuf,const std::string & expected_protobuf)583 void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidBinaryProtobufTest(
584     const std::string& test_name, ConformanceLevel level,
585     const std::string& input_protobuf, const std::string& expected_protobuf) {
586   MessageType prototype;
587   ConformanceRequestSetting setting(
588       level, conformance::PROTOBUF, conformance::PROTOBUF,
589       conformance::BINARY_TEST, prototype, test_name, input_protobuf);
590   suite_.RunValidBinaryInputTest(setting, expected_protobuf, true);
591 }
592 
593 template <typename MessageType>
594 void BinaryAndJsonConformanceSuiteImpl<MessageType>::
RunBinaryPerformanceMergeMessageWithField(const std::string & test_name,const std::string & field_proto)595     RunBinaryPerformanceMergeMessageWithField(const std::string& test_name,
596                                               const std::string& field_proto) {
597   std::string message_tag = tag(27, WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
598   std::string message_proto = absl::StrCat(message_tag, delim(field_proto));
599 
600   std::string proto;
601   for (size_t i = 0; i < kPerformanceRepeatCount; i++) {
602     proto.append(message_proto);
603   }
604 
605   std::string multiple_repeated_field_proto;
606   for (size_t i = 0; i < kPerformanceRepeatCount; i++) {
607     multiple_repeated_field_proto.append(field_proto);
608   }
609   std::string expected_proto =
610       absl::StrCat(message_tag, delim(multiple_repeated_field_proto));
611 
612   RunValidBinaryProtobufTest(test_name, RECOMMENDED, proto, expected_proto);
613 }
614 
615 template <typename MessageType>
616 void BinaryAndJsonConformanceSuiteImpl<MessageType>::
RunValidProtobufTestWithMessage(const std::string & test_name,ConformanceLevel level,const Message * input,const std::string & equivalent_text_format)617     RunValidProtobufTestWithMessage(const std::string& test_name,
618                                     ConformanceLevel level,
619                                     const Message* input,
620                                     const std::string& equivalent_text_format) {
621   RunValidProtobufTest(test_name, level, input->SerializeAsString(),
622                        equivalent_text_format);
623 }
624 
625 // According to proto JSON specification, JSON serializers follow more strict
626 // rules than parsers (e.g., a serializer must serialize int32 values as JSON
627 // numbers while the parser is allowed to accept them as JSON strings). This
628 // method allows strict checking on a proto JSON serializer by inspecting
629 
630 template <typename MessageType>  // the JSON output directly.
631 void BinaryAndJsonConformanceSuiteImpl<
RunValidJsonTestWithValidator(const std::string & test_name,ConformanceLevel level,const std::string & input_json,const Validator & validator)632     MessageType>::RunValidJsonTestWithValidator(const std::string& test_name,
633                                                 ConformanceLevel level,
634                                                 const std::string& input_json,
635                                                 const Validator& validator) {
636   MessageType prototype;
637   ConformanceRequestSetting setting(level, conformance::JSON, conformance::JSON,
638                                     conformance::JSON_TEST, prototype,
639                                     test_name, input_json);
640   const ConformanceRequest& request = setting.GetRequest();
641   ConformanceResponse response;
642   std::string effective_test_name = absl::StrCat(
643       setting.ConformanceLevelToString(level), ".",
644       setting.GetSyntaxIdentifier(), ".JsonInput.", test_name, ".Validator");
645 
646   if (!suite_.RunTest(effective_test_name, request, &response)) {
647     return;
648   }
649 
650   TestStatus test;
651   test.set_name(effective_test_name);
652   if (response.result_case() == ConformanceResponse::kSkipped) {
653     suite_.ReportSkip(test, request, response);
654     return;
655   }
656 
657   if (response.result_case() != ConformanceResponse::kJsonPayload) {
658     test.set_failure_message(absl::StrCat("Expected JSON payload but got type ",
659                                           response.result_case()));
660     suite_.ReportFailure(test, level, request, response);
661     return;
662   }
663   Json::CharReaderBuilder builder;
664   Json::Value value;
665   Json::String err;
666   const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
667   if (!reader->parse(
668           response.json_payload().c_str(),
669           response.json_payload().c_str() + response.json_payload().length(),
670           &value, &err)) {
671     test.set_failure_message(
672         absl::StrCat("JSON payload cannot be parsed as valid JSON: ", err));
673     suite_.ReportFailure(test, level, request, response);
674     return;
675   }
676   if (!validator(value)) {
677     test.set_failure_message("JSON payload validation failed.");
678     suite_.ReportFailure(test, level, request, response);
679     return;
680   }
681   suite_.ReportSuccess(test);
682 }
683 
684 template <typename MessageType>
ExpectParseFailureForJson(const std::string & test_name,ConformanceLevel level,const std::string & input_json)685 void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForJson(
686     const std::string& test_name, ConformanceLevel level,
687     const std::string& input_json) {
688   MessageType prototype;
689   // We don't expect output, but if the program erroneously accepts the protobuf
690   // we let it send its response as this.  We must not leave it unspecified.
691   ConformanceRequestSetting setting(level, conformance::JSON, conformance::JSON,
692                                     conformance::JSON_TEST, prototype,
693                                     test_name, input_json);
694   const ConformanceRequest& request = setting.GetRequest();
695   ConformanceResponse response;
696   std::string effective_test_name =
697       absl::StrCat(setting.ConformanceLevelToString(level), ".",
698                    SyntaxIdentifier(), ".JsonInput.", test_name);
699 
700   if (!suite_.RunTest(effective_test_name, request, &response)) {
701     return;
702   }
703 
704   TestStatus test;
705   test.set_name(effective_test_name);
706   if (response.result_case() == ConformanceResponse::kParseError) {
707     suite_.ReportSuccess(test);
708   } else if (response.result_case() == ConformanceResponse::kSkipped) {
709     suite_.ReportSkip(test, request, response);
710   } else {
711     test.set_failure_message("Should have failed to parse, but didn't.");
712     suite_.ReportFailure(test, level, request, response);
713   }
714 }
715 
716 template <typename MessageType>
717 void BinaryAndJsonConformanceSuiteImpl<MessageType>::
ExpectSerializeFailureForJson(const std::string & test_name,ConformanceLevel level,const std::string & text_format)718     ExpectSerializeFailureForJson(const std::string& test_name,
719                                   ConformanceLevel level,
720                                   const std::string& text_format) {
721   MessageType payload_message;
722   ABSL_CHECK(TextFormat::ParseFromString(text_format, &payload_message))
723       << "Failed to parse: " << text_format;
724 
725   MessageType prototype;
726   ConformanceRequestSetting setting(
727       level, conformance::PROTOBUF, conformance::JSON, conformance::JSON_TEST,
728       prototype, test_name, payload_message.SerializeAsString());
729   const ConformanceRequest& request = setting.GetRequest();
730   ConformanceResponse response;
731   std::string effective_test_name =
732       absl::StrCat(setting.ConformanceLevelToString(level), ".",
733                    SyntaxIdentifier(), ".", test_name, ".JsonOutput");
734 
735   if (!suite_.RunTest(effective_test_name, request, &response)) {
736     return;
737   }
738 
739   TestStatus test;
740   test.set_name(effective_test_name);
741   if (response.result_case() == ConformanceResponse::kSerializeError) {
742     suite_.ReportSuccess(test);
743   } else if (response.result_case() == ConformanceResponse::kSkipped) {
744     suite_.ReportSkip(test, request, response);
745   } else {
746     test.set_failure_message("Should have failed to serialize, but didn't.");
747     suite_.ReportFailure(test, level, request, response);
748   }
749 }
750 
751 template <typename MessageType>
TestPrematureEOFForType(FieldDescriptor::Type type)752 void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestPrematureEOFForType(
753     FieldDescriptor::Type type) {
754   // Incomplete values for each wire type.
755   static constexpr absl::string_view incompletes[6] = {
756       "\x80",     // VARINT
757       "abcdefg",  // 64BIT
758       "\x80",     // DELIMITED (partial length)
759       "",         // START_GROUP (no value required)
760       "",         // END_GROUP (no value required)
761       "abc"       // 32BIT
762   };
763 
764   const FieldDescriptor* field = GetFieldForType(type, false);
765   const FieldDescriptor* rep_field = GetFieldForType(type, true);
766   WireFormatLite::WireType wire_type = WireFormatLite::WireTypeForFieldType(
767       static_cast<WireFormatLite::FieldType>(type));
768   absl::string_view incomplete = incompletes[wire_type];
769   const std::string type_name =
770       UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type)));
771 
772   ExpectParseFailureForProto(
773       tag(field->number(), wire_type),
774       absl::StrCat("PrematureEofBeforeKnownNonRepeatedValue", type_name),
775       REQUIRED);
776 
777   ExpectParseFailureForProto(
778       tag(rep_field->number(), wire_type),
779       absl::StrCat("PrematureEofBeforeKnownRepeatedValue", type_name),
780       REQUIRED);
781 
782   ExpectParseFailureForProto(
783       tag(UNKNOWN_FIELD, wire_type),
784       absl::StrCat("PrematureEofBeforeUnknownValue", type_name), REQUIRED);
785 
786   ExpectParseFailureForProto(
787       absl::StrCat(tag(field->number(), wire_type), incomplete),
788       absl::StrCat("PrematureEofInsideKnownNonRepeatedValue", type_name),
789       REQUIRED);
790 
791   ExpectParseFailureForProto(
792       absl::StrCat(tag(rep_field->number(), wire_type), incomplete),
793       absl::StrCat("PrematureEofInsideKnownRepeatedValue", type_name),
794       REQUIRED);
795 
796   ExpectParseFailureForProto(
797       absl::StrCat(tag(UNKNOWN_FIELD, wire_type), incomplete),
798       absl::StrCat("PrematureEofInsideUnknownValue", type_name), REQUIRED);
799 
800   if (wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
801     ExpectParseFailureForProto(
802         absl::StrCat(tag(field->number(), wire_type), varint(1)),
803         absl::StrCat("PrematureEofInDelimitedDataForKnownNonRepeatedValue",
804                      type_name),
805         REQUIRED);
806 
807     ExpectParseFailureForProto(
808         absl::StrCat(tag(rep_field->number(), wire_type), varint(1)),
809         absl::StrCat("PrematureEofInDelimitedDataForKnownRepeatedValue",
810                      type_name),
811         REQUIRED);
812 
813     // EOF in the middle of delimited data for unknown value.
814     ExpectParseFailureForProto(
815         absl::StrCat(tag(UNKNOWN_FIELD, wire_type), varint(1)),
816         absl::StrCat("PrematureEofInDelimitedDataForUnknownValue", type_name),
817         REQUIRED);
818 
819     if (type == FieldDescriptor::TYPE_MESSAGE) {
820       // Submessage ends in the middle of a value.
821       std::string incomplete_submsg = absl::StrCat(
822           tag(WireFormatLite::TYPE_INT32, WireFormatLite::WIRETYPE_VARINT),
823           incompletes[WireFormatLite::WIRETYPE_VARINT]);
824       ExpectHardParseFailureForProto(
825           absl::StrCat(
826               tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
827               varint(incomplete_submsg.size()), incomplete_submsg),
828           absl::StrCat("PrematureEofInSubmessageValue", type_name), REQUIRED);
829     }
830   } else if (type != FieldDescriptor::TYPE_GROUP) {
831     // Non-delimited, non-group: eligible for packing.
832 
833     // Packed region ends in the middle of a value.
834     ExpectHardParseFailureForProto(
835         absl::StrCat(
836             tag(rep_field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
837             varint(incomplete.size()), incomplete),
838         absl::StrCat("PrematureEofInPackedFieldValue", type_name), REQUIRED);
839 
840     // EOF in the middle of packed region.
841     ExpectParseFailureForProto(
842         absl::StrCat(
843             tag(rep_field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
844             varint(1)),
845         absl::StrCat("PrematureEofInPackedField", type_name), REQUIRED);
846   }
847 }
848 
849 template <typename MessageType>
TestValidDataForType(FieldDescriptor::Type type,std::vector<std::pair<std::string,std::string>> values)850 void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForType(
851     FieldDescriptor::Type type,
852     std::vector<std::pair<std::string, std::string>> values) {
853   const std::string type_name =
854       UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type)));
855   WireFormatLite::WireType wire_type = WireFormatLite::WireTypeForFieldType(
856       static_cast<WireFormatLite::FieldType>(type));
857   const FieldDescriptor* field = GetFieldForType(type, false);
858   const FieldDescriptor* rep_field = GetFieldForType(type, true);
859 
860   // Test singular data for singular fields.
861   for (size_t i = 0; i < values.size(); i++) {
862     std::string proto =
863         absl::StrCat(tag(field->number(), wire_type), values[i].first);
864     // In proto3, default primitive fields should not be encoded.
865     std::string expected_proto =
866         run_proto3_tests_ && IsProto3Default(field->type(), values[i].second)
867             ? ""
868             : absl::StrCat(tag(field->number(), wire_type), values[i].second);
869     MessageType test_message;
870     test_message.MergeFromString(expected_proto);
871     std::string text;
872     TextFormat::PrintToString(test_message, &text);
873 
874     RunValidProtobufTest(
875         absl::StrCat("ValidDataScalar", type_name, "[", i, "]"), REQUIRED,
876         proto, text);
877     RunValidBinaryProtobufTest(
878         absl::StrCat("ValidDataScalarBinary", type_name, "[", i, "]"),
879         RECOMMENDED, proto, expected_proto);
880   }
881 
882   // Test repeated data for singular fields.
883   // For scalar message fields, repeated values are merged, which is tested
884   // separately.
885   if (type != FieldDescriptor::TYPE_MESSAGE) {
886     std::string proto;
887     for (size_t i = 0; i < values.size(); i++) {
888       proto += absl::StrCat(tag(field->number(), wire_type), values[i].first);
889     }
890     std::string expected_proto =
891         absl::StrCat(tag(field->number(), wire_type), values.back().second);
892     MessageType test_message;
893     test_message.MergeFromString(expected_proto);
894     std::string text;
895     TextFormat::PrintToString(test_message, &text);
896 
897     RunValidProtobufTest(absl::StrCat("RepeatedScalarSelectsLast", type_name),
898                          REQUIRED, proto, text);
899   }
900 
901   // Test repeated fields.
902   if (FieldDescriptor::IsTypePackable(type)) {
903     const FieldDescriptor* packed_field =
904         GetFieldForType(type, true, Packed::kTrue);
905     const FieldDescriptor* unpacked_field =
906         GetFieldForType(type, true, Packed::kFalse);
907 
908     std::string default_proto_packed;
909     std::string default_proto_unpacked;
910     std::string default_proto_packed_expected;
911     std::string default_proto_unpacked_expected;
912     std::string packed_proto_packed;
913     std::string packed_proto_unpacked;
914     std::string packed_proto_expected;
915     std::string unpacked_proto_packed;
916     std::string unpacked_proto_unpacked;
917     std::string unpacked_proto_expected;
918 
919     for (size_t i = 0; i < values.size(); i++) {
920       default_proto_unpacked +=
921           absl::StrCat(tag(rep_field->number(), wire_type), values[i].first);
922       default_proto_unpacked_expected +=
923           absl::StrCat(tag(rep_field->number(), wire_type), values[i].second);
924       default_proto_packed += values[i].first;
925       default_proto_packed_expected += values[i].second;
926       packed_proto_unpacked +=
927           absl::StrCat(tag(packed_field->number(), wire_type), values[i].first);
928       packed_proto_packed += values[i].first;
929       packed_proto_expected += values[i].second;
930       unpacked_proto_unpacked += absl::StrCat(
931           tag(unpacked_field->number(), wire_type), values[i].first);
932       unpacked_proto_packed += values[i].first;
933       unpacked_proto_expected += absl::StrCat(
934           tag(unpacked_field->number(), wire_type), values[i].second);
935     }
936     default_proto_packed = absl::StrCat(
937         tag(rep_field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
938         delim(default_proto_packed));
939     default_proto_packed_expected = absl::StrCat(
940         tag(rep_field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
941         delim(default_proto_packed_expected));
942     packed_proto_packed = absl::StrCat(
943         tag(packed_field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
944         delim(packed_proto_packed));
945     packed_proto_expected = absl::StrCat(
946         tag(packed_field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
947         delim(packed_proto_expected));
948     unpacked_proto_packed =
949         absl::StrCat(tag(unpacked_field->number(),
950                          WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
951                      delim(unpacked_proto_packed));
952 
953     MessageType test_message;
954     test_message.MergeFromString(default_proto_packed_expected);
955     std::string text;
956     TextFormat::PrintToString(test_message, &text);
957 
958     // Ensures both packed and unpacked data can be parsed.
959     RunValidProtobufTest(
960         absl::StrCat("ValidDataRepeated", type_name, ".UnpackedInput"),
961         REQUIRED, default_proto_unpacked, text);
962     RunValidProtobufTest(
963         absl::StrCat("ValidDataRepeated", type_name, ".PackedInput"), REQUIRED,
964         default_proto_packed, text);
965 
966     // proto2 should encode as unpacked by default and proto3 should encode as
967     // packed by default.
968     std::string expected_proto = rep_field->is_packed()
969                                      ? default_proto_packed_expected
970                                      : default_proto_unpacked_expected;
971     RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name,
972                                             ".UnpackedInput.DefaultOutput"),
973                                RECOMMENDED, default_proto_unpacked,
974                                expected_proto);
975     RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name,
976                                             ".PackedInput.DefaultOutput"),
977                                RECOMMENDED, default_proto_packed,
978                                expected_proto);
979     RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name,
980                                             ".UnpackedInput.PackedOutput"),
981                                RECOMMENDED, packed_proto_unpacked,
982                                packed_proto_expected);
983     RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name,
984                                             ".PackedInput.PackedOutput"),
985                                RECOMMENDED, packed_proto_packed,
986                                packed_proto_expected);
987     RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name,
988                                             ".UnpackedInput.UnpackedOutput"),
989                                RECOMMENDED, unpacked_proto_unpacked,
990                                unpacked_proto_expected);
991     RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name,
992                                             ".PackedInput.UnpackedOutput"),
993                                RECOMMENDED, unpacked_proto_packed,
994                                unpacked_proto_expected);
995   } else {
996     std::string proto;
997     std::string expected_proto;
998     for (size_t i = 0; i < values.size(); i++) {
999       proto +=
1000           absl::StrCat(tag(rep_field->number(), wire_type), values[i].first);
1001       expected_proto +=
1002           absl::StrCat(tag(rep_field->number(), wire_type), values[i].second);
1003     }
1004     MessageType test_message;
1005     test_message.MergeFromString(expected_proto);
1006     std::string text;
1007     TextFormat::PrintToString(test_message, &text);
1008 
1009     RunValidProtobufTest(absl::StrCat("ValidDataRepeated", type_name), REQUIRED,
1010                          proto, text);
1011   }
1012 }
1013 
1014 template <typename MessageType>
1015 void BinaryAndJsonConformanceSuiteImpl<
TestValidDataForRepeatedScalarMessage()1016     MessageType>::TestValidDataForRepeatedScalarMessage() {
1017   std::vector<std::string> values = {
1018       delim(absl::StrCat(
1019           tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1020           delim(absl::StrCat(
1021               tag(1, WireFormatLite::WIRETYPE_VARINT), varint(1234),
1022               tag(2, WireFormatLite::WIRETYPE_VARINT), varint(1234),
1023               tag(31, WireFormatLite::WIRETYPE_VARINT), varint(1234))))),
1024       delim(absl::StrCat(
1025           tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1026           delim(absl::StrCat(
1027               tag(1, WireFormatLite::WIRETYPE_VARINT), varint(4321),
1028               tag(3, WireFormatLite::WIRETYPE_VARINT), varint(4321),
1029               tag(31, WireFormatLite::WIRETYPE_VARINT), varint(4321))))),
1030   };
1031 
1032   const std::string expected =
1033       R"({
1034         corecursive: {
1035           optional_int32: 4321,
1036           optional_int64: 1234,
1037           optional_uint32: 4321,
1038           repeated_int32: [1234, 4321],
1039   }
1040       })";
1041 
1042   std::string proto;
1043   const FieldDescriptor* field =
1044       GetFieldForType(FieldDescriptor::TYPE_MESSAGE, false);
1045   for (size_t i = 0; i < values.size(); i++) {
1046     proto += absl::StrCat(
1047         tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1048         values[i]);
1049   }
1050 
1051   RunValidProtobufTest("RepeatedScalarMessageMerge", REQUIRED, proto,
1052                        absl::StrCat(field->name(), ": ", expected));
1053 }
1054 
1055 template <typename MessageType>
TestValidDataForMapType(FieldDescriptor::Type key_type,FieldDescriptor::Type value_type)1056 void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForMapType(
1057     FieldDescriptor::Type key_type, FieldDescriptor::Type value_type) {
1058   const std::string key_type_name =
1059       UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(key_type)));
1060   const std::string value_type_name =
1061       UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(value_type)));
1062   WireFormatLite::WireType key_wire_type = WireFormatLite::WireTypeForFieldType(
1063       static_cast<WireFormatLite::FieldType>(key_type));
1064   WireFormatLite::WireType value_wire_type =
1065       WireFormatLite::WireTypeForFieldType(
1066           static_cast<WireFormatLite::FieldType>(value_type));
1067 
1068   std::string key1_data =
1069       absl::StrCat(tag(1, key_wire_type), GetDefaultValue(key_type));
1070   std::string value1_data =
1071       absl::StrCat(tag(2, value_wire_type), GetDefaultValue(value_type));
1072   std::string key2_data =
1073       absl::StrCat(tag(1, key_wire_type), GetNonDefaultValue(key_type));
1074   std::string value2_data =
1075       absl::StrCat(tag(2, value_wire_type), GetNonDefaultValue(value_type));
1076 
1077   const FieldDescriptor* field = GetFieldForMapType(key_type, value_type);
1078 
1079   {
1080     // Tests map with default key and value.
1081     std::string proto = absl::StrCat(
1082         tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1083         delim(absl::StrCat(key1_data, value1_data)));
1084     MessageType test_message;
1085     test_message.MergeFromString(proto);
1086     std::string text;
1087     TextFormat::PrintToString(test_message, &text);
1088     RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name,
1089                                       value_type_name, ".Default"),
1090                          REQUIRED, proto, text);
1091   }
1092 
1093   {
1094     // Tests map with missing default key and value.
1095     std::string proto = absl::StrCat(
1096         tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1097         delim(""));
1098     MessageType test_message;
1099     test_message.MergeFromString(proto);
1100     std::string text;
1101     TextFormat::PrintToString(test_message, &text);
1102     RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name,
1103                                       value_type_name, ".MissingDefault"),
1104                          REQUIRED, proto, text);
1105   }
1106 
1107   {
1108     // Tests map with non-default key and value.
1109     std::string proto = absl::StrCat(
1110         tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1111         delim(absl::StrCat(key2_data, value2_data)));
1112     MessageType test_message;
1113     test_message.MergeFromString(proto);
1114     std::string text;
1115     TextFormat::PrintToString(test_message, &text);
1116     RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name,
1117                                       value_type_name, ".NonDefault"),
1118                          REQUIRED, proto, text);
1119   }
1120 
1121   {
1122     // Tests map with unordered key and value.
1123     std::string proto = absl::StrCat(
1124         tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1125         delim(absl::StrCat(value2_data, key2_data)));
1126     MessageType test_message;
1127     test_message.MergeFromString(proto);
1128     std::string text;
1129     TextFormat::PrintToString(test_message, &text);
1130     RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name,
1131                                       value_type_name, ".Unordered"),
1132                          REQUIRED, proto, text);
1133   }
1134 
1135   {
1136     // Tests map with duplicate key.
1137     std::string proto1 = absl::StrCat(
1138         tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1139         delim(absl::StrCat(key2_data, value1_data)));
1140     std::string proto2 = absl::StrCat(
1141         tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1142         delim(absl::StrCat(key2_data, value2_data)));
1143     std::string proto = absl::StrCat(proto1, proto2);
1144     MessageType test_message;
1145     test_message.MergeFromString(proto2);
1146     std::string text;
1147     TextFormat::PrintToString(test_message, &text);
1148     RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name,
1149                                       value_type_name, ".DuplicateKey"),
1150                          REQUIRED, proto, text);
1151   }
1152 
1153   {
1154     // Tests map with duplicate key in map entry.
1155     std::string proto = absl::StrCat(
1156         tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1157         delim(absl::StrCat(key1_data, key2_data, value2_data)));
1158     MessageType test_message;
1159     test_message.MergeFromString(proto);
1160     std::string text;
1161     TextFormat::PrintToString(test_message, &text);
1162     RunValidProtobufTest(
1163         absl::StrCat("ValidDataMap", key_type_name, value_type_name,
1164                      ".DuplicateKeyInMapEntry"),
1165         REQUIRED, proto, text);
1166   }
1167 
1168   {
1169     // Tests map with duplicate value in map entry.
1170     std::string proto = absl::StrCat(
1171         tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1172         delim(absl::StrCat(key2_data, value1_data, value2_data)));
1173     MessageType test_message;
1174     test_message.MergeFromString(proto);
1175     std::string text;
1176     TextFormat::PrintToString(test_message, &text);
1177     RunValidProtobufTest(
1178         absl::StrCat("ValidDataMap", key_type_name, value_type_name,
1179                      ".DuplicateValueInMapEntry"),
1180         REQUIRED, proto, text);
1181   }
1182 }
1183 
1184 template <typename MessageType>
1185 void BinaryAndJsonConformanceSuiteImpl<
TestOverwriteMessageValueMap()1186     MessageType>::TestOverwriteMessageValueMap() {
1187   std::string key_data = absl::StrCat(
1188       tag(1, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), delim(""));
1189   std::string field1_data =
1190       absl::StrCat(tag(1, WireFormatLite::WIRETYPE_VARINT), varint(1));
1191   std::string field2_data =
1192       absl::StrCat(tag(2, WireFormatLite::WIRETYPE_VARINT), varint(1));
1193   std::string field31_data =
1194       absl::StrCat(tag(31, WireFormatLite::WIRETYPE_VARINT), varint(1));
1195   std::string submsg1_data = delim(absl::StrCat(field1_data, field31_data));
1196   std::string submsg2_data = delim(absl::StrCat(field2_data, field31_data));
1197   std::string value1_data = absl::StrCat(
1198       tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1199       delim(absl::StrCat(tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1200                          submsg1_data)));
1201   std::string value2_data = absl::StrCat(
1202       tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1203       delim(absl::StrCat(tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1204                          submsg2_data)));
1205 
1206   const FieldDescriptor* field = GetFieldForMapType(
1207       FieldDescriptor::TYPE_STRING, FieldDescriptor::TYPE_MESSAGE);
1208 
1209   std::string proto1 = absl::StrCat(
1210       tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1211       delim(absl::StrCat(key_data, value1_data)));
1212   std::string proto2 = absl::StrCat(
1213       tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1214       delim(absl::StrCat(key_data, value2_data)));
1215   std::string proto = absl::StrCat(proto1, proto2);
1216   MessageType test_message;
1217   test_message.MergeFromString(proto2);
1218   std::string text;
1219   TextFormat::PrintToString(test_message, &text);
1220   RunValidProtobufTest("ValidDataMap.STRING.MESSAGE.MergeValue", REQUIRED,
1221                        proto, text);
1222 }
1223 
1224 template <typename MessageType>
TestValidDataForOneofType(FieldDescriptor::Type type)1225 void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForOneofType(
1226     FieldDescriptor::Type type) {
1227   const std::string type_name =
1228       UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type)));
1229   WireFormatLite::WireType wire_type = WireFormatLite::WireTypeForFieldType(
1230       static_cast<WireFormatLite::FieldType>(type));
1231 
1232   const FieldDescriptor* field = GetFieldForOneofType(type);
1233   const std::string default_value =
1234       absl::StrCat(tag(field->number(), wire_type), GetDefaultValue(type));
1235   const std::string non_default_value =
1236       absl::StrCat(tag(field->number(), wire_type), GetNonDefaultValue(type));
1237 
1238   {
1239     // Tests oneof with default value.
1240     const std::string& proto = default_value;
1241     MessageType test_message;
1242     test_message.MergeFromString(proto);
1243     std::string text;
1244     TextFormat::PrintToString(test_message, &text);
1245 
1246     RunValidProtobufTest(
1247         absl::StrCat("ValidDataOneof", type_name, ".DefaultValue"), REQUIRED,
1248         proto, text);
1249     RunValidBinaryProtobufTest(
1250         absl::StrCat("ValidDataOneofBinary", type_name, ".DefaultValue"),
1251         RECOMMENDED, proto, proto);
1252   }
1253 
1254   {
1255     // Tests oneof with non-default value.
1256     const std::string& proto = non_default_value;
1257     MessageType test_message;
1258     test_message.MergeFromString(proto);
1259     std::string text;
1260     TextFormat::PrintToString(test_message, &text);
1261 
1262     RunValidProtobufTest(
1263         absl::StrCat("ValidDataOneof", type_name, ".NonDefaultValue"), REQUIRED,
1264         proto, text);
1265     RunValidBinaryProtobufTest(
1266         absl::StrCat("ValidDataOneofBinary", type_name, ".NonDefaultValue"),
1267         RECOMMENDED, proto, proto);
1268   }
1269 
1270   {
1271     // Tests oneof with multiple values of the same field.
1272     const std::string proto = absl::StrCat(default_value, non_default_value);
1273     const std::string& expected_proto = non_default_value;
1274     MessageType test_message;
1275     test_message.MergeFromString(expected_proto);
1276     std::string text;
1277     TextFormat::PrintToString(test_message, &text);
1278 
1279     RunValidProtobufTest(absl::StrCat("ValidDataOneof", type_name,
1280                                       ".MultipleValuesForSameField"),
1281                          REQUIRED, proto, text);
1282     RunValidBinaryProtobufTest(absl::StrCat("ValidDataOneofBinary", type_name,
1283                                             ".MultipleValuesForSameField"),
1284                                RECOMMENDED, proto, expected_proto);
1285   }
1286 
1287   {
1288     // Tests oneof with multiple values of the different fields.
1289     const FieldDescriptor* other_field = GetFieldForOneofType(type, true);
1290     FieldDescriptor::Type other_type = other_field->type();
1291     WireFormatLite::WireType other_wire_type =
1292         WireFormatLite::WireTypeForFieldType(
1293             static_cast<WireFormatLite::FieldType>(other_type));
1294     const std::string other_value =
1295         absl::StrCat(tag(other_field->number(), other_wire_type),
1296                      GetDefaultValue(other_type));
1297 
1298     const std::string proto = absl::StrCat(other_value, non_default_value);
1299     const std::string& expected_proto = non_default_value;
1300     MessageType test_message;
1301     test_message.MergeFromString(expected_proto);
1302     std::string text;
1303     TextFormat::PrintToString(test_message, &text);
1304 
1305     RunValidProtobufTest(absl::StrCat("ValidDataOneof", type_name,
1306                                       ".MultipleValuesForDifferentField"),
1307                          REQUIRED, proto, text);
1308     RunValidBinaryProtobufTest(absl::StrCat("ValidDataOneofBinary", type_name,
1309                                             ".MultipleValuesForDifferentField"),
1310                                RECOMMENDED, proto, expected_proto);
1311   }
1312 }
1313 
1314 template <typename MessageType>
TestMergeOneofMessage()1315 void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestMergeOneofMessage() {
1316   std::string field1_data =
1317       absl::StrCat(tag(1, WireFormatLite::WIRETYPE_VARINT), varint(1));
1318   std::string field2a_data =
1319       absl::StrCat(tag(2, WireFormatLite::WIRETYPE_VARINT), varint(1));
1320   std::string field2b_data =
1321       absl::StrCat(tag(2, WireFormatLite::WIRETYPE_VARINT), varint(1));
1322   std::string field89_data =
1323       absl::StrCat(tag(89, WireFormatLite::WIRETYPE_VARINT), varint(1));
1324   std::string submsg1_data = absl::StrCat(
1325       tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1326       delim(absl::StrCat(field1_data, field2a_data, field89_data)));
1327   std::string submsg2_data =
1328       absl::StrCat(tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1329                    delim(absl::StrCat(field2b_data, field89_data)));
1330   std::string merged_data =
1331       absl::StrCat(tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1332                    delim(absl::StrCat(field1_data, field2b_data, field89_data,
1333                                       field89_data)));
1334 
1335   const FieldDescriptor* field =
1336       GetFieldForOneofType(FieldDescriptor::TYPE_MESSAGE);
1337 
1338   std::string proto1 = absl::StrCat(
1339       tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1340       delim(submsg1_data));
1341   std::string proto2 = absl::StrCat(
1342       tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1343       delim(submsg2_data));
1344   std::string proto = absl::StrCat(proto1, proto2);
1345   std::string expected_proto = absl::StrCat(
1346       tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
1347       delim(merged_data));
1348 
1349   MessageType test_message;
1350   test_message.MergeFromString(expected_proto);
1351   std::string text;
1352   TextFormat::PrintToString(test_message, &text);
1353   RunValidProtobufTest("ValidDataOneof.MESSAGE.Merge", REQUIRED, proto, text);
1354   RunValidBinaryProtobufTest("ValidDataOneofBinary.MESSAGE.Merge", RECOMMENDED,
1355                              proto, expected_proto);
1356 }
1357 
1358 template <typename MessageType>
TestIllegalTags()1359 void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestIllegalTags() {
1360   // field num 0 is illegal
1361   std::string nullfield[] = {"\1DEADBEEF", "\2\1\1", "\3\4", "\5DEAD"};
1362   for (int i = 0; i < 4; i++) {
1363     std::string name = "IllegalZeroFieldNum_Case_0";
1364     name.back() += i;
1365     ExpectParseFailureForProto(nullfield[i], name, REQUIRED);
1366   }
1367 }
1368 
1369 template <typename MessageType>
TestUnknownWireType()1370 void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestUnknownWireType() {
1371   for (uint8_t type : {0x6, 0x7}) {
1372     for (uint8_t field = 0; field < 4; ++field) {
1373       for (uint8_t value = 0; value < 4; ++value) {
1374         std::string name = absl::StrFormat("UnknownWireType%d_Field%d_Verion%d",
1375                                            type, field, value);
1376 
1377         char data[2];
1378         data[0] = (field << 3) | type;  // unknown wire type.
1379         data[1] = value;
1380         std::string proto = {data, 2};
1381         ExpectParseFailureForProto(proto, name, REQUIRED);
1382       }
1383     }
1384   }
1385 }
1386 
1387 template <typename MessageType>
TestOneofMessage()1388 void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestOneofMessage() {
1389   MessageType message;
1390   message.set_oneof_uint32(0);
1391   RunValidProtobufTestWithMessage("OneofZeroUint32", RECOMMENDED, &message,
1392                                   "oneof_uint32: 0");
1393   message.mutable_oneof_nested_message()->set_a(0);
1394   RunValidProtobufTestWithMessage("OneofZeroMessage", RECOMMENDED, &message,
1395                                   run_proto3_tests_
1396                                       ? "oneof_nested_message: {}"
1397                                       : "oneof_nested_message: {a: 0}");
1398   message.mutable_oneof_nested_message()->set_a(1);
1399   RunValidProtobufTestWithMessage("OneofZeroMessageSetTwice", RECOMMENDED,
1400                                   &message, "oneof_nested_message: {a: 1}");
1401   message.set_oneof_string("");
1402   RunValidProtobufTestWithMessage("OneofZeroString", RECOMMENDED, &message,
1403                                   "oneof_string: \"\"");
1404   message.set_oneof_bytes("");
1405   RunValidProtobufTestWithMessage("OneofZeroBytes", RECOMMENDED, &message,
1406                                   "oneof_bytes: \"\"");
1407   message.set_oneof_bool(false);
1408   RunValidProtobufTestWithMessage("OneofZeroBool", RECOMMENDED, &message,
1409                                   "oneof_bool: false");
1410   message.set_oneof_uint64(0);
1411   RunValidProtobufTestWithMessage("OneofZeroUint64", RECOMMENDED, &message,
1412                                   "oneof_uint64: 0");
1413   message.set_oneof_float(0.0f);
1414   RunValidProtobufTestWithMessage("OneofZeroFloat", RECOMMENDED, &message,
1415                                   "oneof_float: 0");
1416   message.set_oneof_double(0.0);
1417   RunValidProtobufTestWithMessage("OneofZeroDouble", RECOMMENDED, &message,
1418                                   "oneof_double: 0");
1419   message.set_oneof_enum(MessageType::FOO);
1420   RunValidProtobufTestWithMessage("OneofZeroEnum", RECOMMENDED, &message,
1421                                   "oneof_enum: FOO");
1422 }
1423 
1424 template <typename MessageType>
TestUnknownMessage()1425 void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestUnknownMessage() {
1426   MessageType message;
1427   message.ParseFromString("\xA8\x1F\x01");
1428   RunValidBinaryProtobufTest("UnknownVarint", REQUIRED,
1429                              message.SerializeAsString());
1430 }
1431 
1432 template <typename MessageType>
TestUnknownOrdering()1433 void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestUnknownOrdering() {
1434   // Implementations must preserve the ordering of different unknown fields for
1435   // the same field number.  This is because some field types will accept
1436   // multiple wire types for the same field.  For example, repeated primitive
1437   // fields will accept both length-prefixed (packed) and
1438   // varint/fixed32/fixed64 (unpacked) wire types, and reordering these could
1439   // reorder the elements of the repeated field.
1440   MessageType message;
1441   MessageType prototype;
1442   message.mutable_unknown_fields()->AddLengthDelimited(UNKNOWN_FIELD, "abc");
1443   message.mutable_unknown_fields()->AddVarint(UNKNOWN_FIELD, 123);
1444   message.mutable_unknown_fields()->AddLengthDelimited(UNKNOWN_FIELD, "def");
1445   message.mutable_unknown_fields()->AddVarint(UNKNOWN_FIELD, 456);
1446   std::string serialized = message.SerializeAsString();
1447 
1448   ConformanceRequestSetting setting(
1449       REQUIRED, conformance::PROTOBUF, conformance::PROTOBUF,
1450       conformance::BINARY_TEST, prototype, "UnknownOrdering", serialized);
1451   const ConformanceRequest& request = setting.GetRequest();
1452   ConformanceResponse response;
1453   if (!suite_.RunTest(setting.GetTestName(), request, &response)) {
1454     return;
1455   }
1456 
1457   MessageType response_message;
1458   TestStatus test;
1459   test.set_name(setting.GetTestName());
1460   if (response.result_case() == ConformanceResponse::kSkipped) {
1461     suite_.ReportSkip(test, request, response);
1462     return;
1463   }
1464 
1465   suite_.ParseResponse(response, setting, &response_message);
1466 
1467   const UnknownFieldSet& ufs = response_message.unknown_fields();
1468   if (ufs.field_count() != 4 || ufs.field(0).number() != UNKNOWN_FIELD ||
1469       ufs.field(1).number() != UNKNOWN_FIELD ||
1470       ufs.field(2).number() != UNKNOWN_FIELD ||
1471       ufs.field(3).number() != UNKNOWN_FIELD ||
1472       ufs.field(0).type() != UnknownField::Type::TYPE_LENGTH_DELIMITED ||
1473       ufs.field(1).type() != UnknownField::Type::TYPE_VARINT ||
1474       ufs.field(2).type() != UnknownField::Type::TYPE_LENGTH_DELIMITED ||
1475       ufs.field(3).type() != UnknownField::Type::TYPE_VARINT ||
1476       ufs.field(0).length_delimited() != "abc" ||
1477       ufs.field(1).varint() != 123 ||
1478       ufs.field(2).length_delimited() != "def" ||
1479       ufs.field(3).varint() != 456) {
1480     test.set_failure_message("Unknown field mismatch");
1481     suite_.ReportFailure(test, setting.GetLevel(), request, response);
1482   } else {
1483     suite_.ReportSuccess(test);
1484   }
1485 }
1486 
1487 template <typename MessageType>
1488 void BinaryAndJsonConformanceSuiteImpl<
TestBinaryPerformanceForAlternatingUnknownFields()1489     MessageType>::TestBinaryPerformanceForAlternatingUnknownFields() {
1490   std::string unknown_field_1 = absl::StrCat(
1491       tag(UNKNOWN_FIELD, WireFormatLite::WIRETYPE_VARINT), varint(1234));
1492   std::string unknown_field_2 = absl::StrCat(
1493       tag(UNKNOWN_FIELD + 1, WireFormatLite::WIRETYPE_VARINT), varint(5678));
1494   std::string proto;
1495   for (size_t i = 0; i < kPerformanceRepeatCount; i++) {
1496     proto.append(unknown_field_1);
1497     proto.append(unknown_field_2);
1498   }
1499 
1500   RunValidBinaryProtobufTest("TestBinaryPerformanceForAlternatingUnknownFields",
1501                              RECOMMENDED, proto);
1502 }
1503 
1504 template <typename MessageType>
1505 void BinaryAndJsonConformanceSuiteImpl<MessageType>::
TestBinaryPerformanceMergeMessageWithRepeatedFieldForType(FieldDescriptor::Type type)1506     TestBinaryPerformanceMergeMessageWithRepeatedFieldForType(
1507         FieldDescriptor::Type type) {
1508   const std::string type_name =
1509       UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type)));
1510   int field_number = GetFieldForType(type, true, Packed::kFalse)->number();
1511   std::string rep_field_proto = absl::StrCat(
1512       tag(field_number, WireFormatLite::WireTypeForFieldType(
1513                             static_cast<WireFormatLite::FieldType>(type))),
1514       GetNonDefaultValue(type));
1515 
1516   RunBinaryPerformanceMergeMessageWithField(
1517       absl::StrCat("TestBinaryPerformanceMergeMessageWithRepeatedFieldForType",
1518                    type_name),
1519       rep_field_proto);
1520 }
1521 
1522 template <typename MessageType>
1523 void BinaryAndJsonConformanceSuiteImpl<MessageType>::
TestBinaryPerformanceMergeMessageWithUnknownFieldForType(FieldDescriptor::Type type)1524     TestBinaryPerformanceMergeMessageWithUnknownFieldForType(
1525         FieldDescriptor::Type type) {
1526   const std::string type_name =
1527       UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type)));
1528   std::string unknown_field_proto = absl::StrCat(
1529       tag(UNKNOWN_FIELD, WireFormatLite::WireTypeForFieldType(
1530                              static_cast<WireFormatLite::FieldType>(type))),
1531       GetNonDefaultValue(type));
1532   RunBinaryPerformanceMergeMessageWithField(
1533       absl::StrCat("TestBinaryPerformanceMergeMessageWithUnknownFieldForType",
1534                    type_name),
1535       unknown_field_proto);
1536 }
1537 
1538 template <typename MessageType>
1539 BinaryAndJsonConformanceSuiteImpl<MessageType>::
BinaryAndJsonConformanceSuiteImpl(BinaryAndJsonConformanceSuite * suite,bool run_proto3_tests)1540     BinaryAndJsonConformanceSuiteImpl(BinaryAndJsonConformanceSuite* suite,
1541                                       bool run_proto3_tests)
1542     : suite_(*ABSL_DIE_IF_NULL(suite)), run_proto3_tests_(run_proto3_tests) {
1543   suite_.SetTypeUrl(GetTypeUrl(MessageType::GetDescriptor()));
1544   RunAllTests();
1545 }
1546 
1547 template <typename MessageType>
RunAllTests()1548 void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunAllTests() {
1549   if (!suite_.performance_) {
1550     for (int i = 1; i <= FieldDescriptor::MAX_TYPE; i++) {
1551       if (i == FieldDescriptor::TYPE_GROUP) continue;
1552       TestPrematureEOFForType(static_cast<FieldDescriptor::Type>(i));
1553     }
1554 
1555     TestIllegalTags();
1556 
1557     TestUnknownWireType();
1558 
1559     int64_t kInt64Min = -9223372036854775808ULL;
1560     int64_t kInt64Max = 9223372036854775807ULL;
1561     uint64_t kUint64Max = 18446744073709551615ULL;
1562     int32_t kInt32Max = 2147483647;
1563     int32_t kInt32Min = -2147483648;
1564     uint32_t kUint32Max = 4294967295UL;
1565 
1566     TestValidDataForType(
1567         FieldDescriptor::TYPE_DOUBLE,
1568         {
1569             {dbl(0), dbl(0)},
1570             {dbl(0.1), dbl(0.1)},
1571             {dbl(1.7976931348623157e+308), dbl(1.7976931348623157e+308)},
1572             {dbl(2.22507385850720138309e-308),
1573              dbl(2.22507385850720138309e-308)},
1574         });
1575     TestValidDataForType(
1576         FieldDescriptor::TYPE_FLOAT,
1577         {
1578             {flt(0), flt(0)},
1579             {flt(0.1), flt(0.1)},
1580             {flt(1.00000075e-36), flt(1.00000075e-36)},
1581             {flt(3.402823e+38), flt(3.402823e+38)},  // 3.40282347e+38
1582             {flt(1.17549435e-38f), flt(1.17549435e-38)},
1583         });
1584     TestValidDataForType(FieldDescriptor::TYPE_INT64,
1585                          {
1586                              {varint(0), varint(0)},
1587                              {varint(12345), varint(12345)},
1588                              {varint(kInt64Max), varint(kInt64Max)},
1589                              {varint(kInt64Min), varint(kInt64Min)},
1590                          });
1591     TestValidDataForType(FieldDescriptor::TYPE_UINT64,
1592                          {
1593                              {varint(0), varint(0)},
1594                              {varint(12345), varint(12345)},
1595                              {varint(kUint64Max), varint(kUint64Max)},
1596                          });
1597     TestValidDataForType(FieldDescriptor::TYPE_INT32,
1598                          {
1599                              {varint(0), varint(0)},
1600                              {varint(12345), varint(12345)},
1601                              {longvarint(12345, 2), varint(12345)},
1602                              {longvarint(12345, 7), varint(12345)},
1603                              {varint(kInt32Max), varint(kInt32Max)},
1604                              {varint(kInt32Min), varint(kInt32Min)},
1605                              {varint(1LL << 33), varint(0)},
1606                              {varint((1LL << 33) - 1), varint(-1)},
1607                              {varint(kInt64Max), varint(-1)},
1608                              {varint(kInt64Min + 1), varint(1)},
1609                          });
1610     TestValidDataForType(
1611         FieldDescriptor::TYPE_UINT32,
1612         {
1613             {varint(0), varint(0)},
1614             {varint(12345), varint(12345)},
1615             {longvarint(12345, 2), varint(12345)},
1616             {longvarint(12345, 7), varint(12345)},
1617             {varint(kUint32Max), varint(kUint32Max)},  // UINT32_MAX
1618             {varint(1LL << 33), varint(0)},
1619             {varint((1LL << 33) + 1), varint(1)},
1620             {varint((1LL << 33) - 1), varint((1LL << 32) - 1)},
1621             {varint(kInt64Max), varint((1LL << 32) - 1)},
1622             {varint(kInt64Min + 1), varint(1)},
1623         });
1624     TestValidDataForType(FieldDescriptor::TYPE_FIXED64,
1625                          {
1626                              {u64(0), u64(0)},
1627                              {u64(12345), u64(12345)},
1628                              {u64(kUint64Max), u64(kUint64Max)},
1629                          });
1630     TestValidDataForType(FieldDescriptor::TYPE_FIXED32,
1631                          {
1632                              {u32(0), u32(0)},
1633                              {u32(12345), u32(12345)},
1634                              {u32(kUint32Max), u32(kUint32Max)},  // UINT32_MAX
1635                          });
1636     TestValidDataForType(FieldDescriptor::TYPE_SFIXED64,
1637                          {
1638                              {u64(0), u64(0)},
1639                              {u64(12345), u64(12345)},
1640                              {u64(kInt64Max), u64(kInt64Max)},
1641                              {u64(kInt64Min), u64(kInt64Min)},
1642                          });
1643     TestValidDataForType(FieldDescriptor::TYPE_SFIXED32,
1644                          {
1645                              {u32(0), u32(0)},
1646                              {u32(12345), u32(12345)},
1647                              {u32(kInt32Max), u32(kInt32Max)},
1648                              {u32(kInt32Min), u32(kInt32Min)},
1649                          });
1650     // Bools should be serialized as 0 for false and 1 for true. Parsers should
1651     // also interpret any nonzero value as true.
1652     TestValidDataForType(FieldDescriptor::TYPE_BOOL,
1653                          {
1654                              {varint(0), varint(0)},
1655                              {varint(1), varint(1)},
1656                              {varint(-1), varint(1)},
1657                              {varint(12345678), varint(1)},
1658                              {varint(1LL << 33), varint(1)},
1659                              {varint(kInt64Max), varint(1)},
1660                              {varint(kInt64Min), varint(1)},
1661                          });
1662     TestValidDataForType(FieldDescriptor::TYPE_SINT32,
1663                          {
1664                              {zz32(0), zz32(0)},
1665                              {zz32(12345), zz32(12345)},
1666                              {zz32(kInt32Max), zz32(kInt32Max)},
1667                              {zz32(kInt32Min), zz32(kInt32Min)},
1668                              {zz64(kInt32Max + 2LL), zz32(1)},
1669                          });
1670     TestValidDataForType(FieldDescriptor::TYPE_SINT64,
1671                          {
1672                              {zz64(0), zz64(0)},
1673                              {zz64(12345), zz64(12345)},
1674                              {zz64(kInt64Max), zz64(kInt64Max)},
1675                              {zz64(kInt64Min), zz64(kInt64Min)},
1676                          });
1677     TestValidDataForType(
1678         FieldDescriptor::TYPE_STRING,
1679         {
1680             {delim(""), delim("")},
1681             {delim("Hello world!"), delim("Hello world!")},
1682             {delim("\'\"\?\\\a\b\f\n\r\t\v"),
1683              delim("\'\"\?\\\a\b\f\n\r\t\v")},       // escape
1684             {delim("谷歌"), delim("谷歌")},          // Google in Chinese
1685             {delim("\u8C37\u6B4C"), delim("谷歌")},  // unicode escape
1686             {delim("\u8c37\u6b4c"), delim("谷歌")},  // lowercase unicode
1687             {delim("\xF0\x9F\x98\x81"),
1688              delim("\xF0\x9F\x98\x81")},  // emoji: ��
1689         });
1690     TestValidDataForType(FieldDescriptor::TYPE_BYTES,
1691                          {
1692                              {delim(""), delim("")},
1693                              {delim("Hello world!"), delim("Hello world!")},
1694                              {delim("\x01\x02"), delim("\x01\x02")},
1695                              {delim("\xfb"), delim("\xfb")},
1696                          });
1697     TestValidDataForType(FieldDescriptor::TYPE_ENUM,
1698                          {
1699                              {varint(0), varint(0)},
1700                              {varint(1), varint(1)},
1701                              {varint(2), varint(2)},
1702                              {varint(-1), varint(-1)},
1703                              {varint(kInt64Max), varint(-1)},
1704                              {varint(kInt64Min + 1), varint(1)},
1705                          });
1706     TestValidDataForRepeatedScalarMessage();
1707     TestValidDataForType(
1708         FieldDescriptor::TYPE_MESSAGE,
1709         {
1710             {delim(""), delim("")},
1711             {delim(absl::StrCat(tag(1, WireFormatLite::WIRETYPE_VARINT),
1712                                 varint(1234))),
1713              delim(absl::StrCat(tag(1, WireFormatLite::WIRETYPE_VARINT),
1714                                 varint(1234)))},
1715         });
1716 
1717     TestValidDataForMapType(FieldDescriptor::TYPE_INT32,
1718                             FieldDescriptor::TYPE_INT32);
1719     TestValidDataForMapType(FieldDescriptor::TYPE_INT64,
1720                             FieldDescriptor::TYPE_INT64);
1721     TestValidDataForMapType(FieldDescriptor::TYPE_UINT32,
1722                             FieldDescriptor::TYPE_UINT32);
1723     TestValidDataForMapType(FieldDescriptor::TYPE_UINT64,
1724                             FieldDescriptor::TYPE_UINT64);
1725     TestValidDataForMapType(FieldDescriptor::TYPE_SINT32,
1726                             FieldDescriptor::TYPE_SINT32);
1727     TestValidDataForMapType(FieldDescriptor::TYPE_SINT64,
1728                             FieldDescriptor::TYPE_SINT64);
1729     TestValidDataForMapType(FieldDescriptor::TYPE_FIXED32,
1730                             FieldDescriptor::TYPE_FIXED32);
1731     TestValidDataForMapType(FieldDescriptor::TYPE_FIXED64,
1732                             FieldDescriptor::TYPE_FIXED64);
1733     TestValidDataForMapType(FieldDescriptor::TYPE_SFIXED32,
1734                             FieldDescriptor::TYPE_SFIXED32);
1735     TestValidDataForMapType(FieldDescriptor::TYPE_SFIXED64,
1736                             FieldDescriptor::TYPE_SFIXED64);
1737     TestValidDataForMapType(FieldDescriptor::TYPE_INT32,
1738                             FieldDescriptor::TYPE_FLOAT);
1739     TestValidDataForMapType(FieldDescriptor::TYPE_INT32,
1740                             FieldDescriptor::TYPE_DOUBLE);
1741     TestValidDataForMapType(FieldDescriptor::TYPE_BOOL,
1742                             FieldDescriptor::TYPE_BOOL);
1743     TestValidDataForMapType(FieldDescriptor::TYPE_STRING,
1744                             FieldDescriptor::TYPE_STRING);
1745     TestValidDataForMapType(FieldDescriptor::TYPE_STRING,
1746                             FieldDescriptor::TYPE_BYTES);
1747     TestValidDataForMapType(FieldDescriptor::TYPE_STRING,
1748                             FieldDescriptor::TYPE_ENUM);
1749     TestValidDataForMapType(FieldDescriptor::TYPE_STRING,
1750                             FieldDescriptor::TYPE_MESSAGE);
1751     // Additional test to check overwriting message value map.
1752     TestOverwriteMessageValueMap();
1753 
1754     TestValidDataForOneofType(FieldDescriptor::TYPE_UINT32);
1755     TestValidDataForOneofType(FieldDescriptor::TYPE_BOOL);
1756     TestValidDataForOneofType(FieldDescriptor::TYPE_UINT64);
1757     TestValidDataForOneofType(FieldDescriptor::TYPE_FLOAT);
1758     TestValidDataForOneofType(FieldDescriptor::TYPE_DOUBLE);
1759     TestValidDataForOneofType(FieldDescriptor::TYPE_STRING);
1760     TestValidDataForOneofType(FieldDescriptor::TYPE_BYTES);
1761     TestValidDataForOneofType(FieldDescriptor::TYPE_ENUM);
1762     TestValidDataForOneofType(FieldDescriptor::TYPE_MESSAGE);
1763     // Additional test to check merging oneof message.
1764     TestMergeOneofMessage();
1765 
1766     // TODO:
1767     // TestValidDataForType(FieldDescriptor::TYPE_GROUP
1768 
1769     // Unknown fields.
1770     // TODO: update this behavior when unknown field's behavior
1771     // changed in open source. Also delete
1772     // Required.Proto3.ProtobufInput.UnknownVarint.ProtobufOutput
1773     // from failure list of python_cpp python java
1774     TestUnknownMessage();
1775     TestUnknownOrdering();
1776     TestOneofMessage();
1777 
1778     RunJsonTests();
1779   }
1780   // Flag control performance tests to keep them internal and opt-in only
1781   if (suite_.performance_) {
1782     RunBinaryPerformanceTests();
1783     RunJsonPerformanceTests();
1784   }
1785 }
1786 
1787 template <typename MessageType>
1788 void BinaryAndJsonConformanceSuiteImpl<
RunBinaryPerformanceTests()1789     MessageType>::RunBinaryPerformanceTests() {
1790   TestBinaryPerformanceForAlternatingUnknownFields();
1791 
1792   TestBinaryPerformanceMergeMessageWithRepeatedFieldForType(
1793       FieldDescriptor::TYPE_BOOL);
1794   TestBinaryPerformanceMergeMessageWithRepeatedFieldForType(
1795       FieldDescriptor::TYPE_DOUBLE);
1796   TestBinaryPerformanceMergeMessageWithRepeatedFieldForType(
1797       FieldDescriptor::TYPE_FLOAT);
1798   TestBinaryPerformanceMergeMessageWithRepeatedFieldForType(
1799       FieldDescriptor::TYPE_UINT32);
1800   TestBinaryPerformanceMergeMessageWithRepeatedFieldForType(
1801       FieldDescriptor::TYPE_UINT64);
1802   TestBinaryPerformanceMergeMessageWithRepeatedFieldForType(
1803       FieldDescriptor::TYPE_STRING);
1804   TestBinaryPerformanceMergeMessageWithRepeatedFieldForType(
1805       FieldDescriptor::TYPE_BYTES);
1806 
1807   TestBinaryPerformanceMergeMessageWithUnknownFieldForType(
1808       FieldDescriptor::TYPE_BOOL);
1809   TestBinaryPerformanceMergeMessageWithUnknownFieldForType(
1810       FieldDescriptor::TYPE_DOUBLE);
1811   TestBinaryPerformanceMergeMessageWithUnknownFieldForType(
1812       FieldDescriptor::TYPE_FLOAT);
1813   TestBinaryPerformanceMergeMessageWithUnknownFieldForType(
1814       FieldDescriptor::TYPE_UINT32);
1815   TestBinaryPerformanceMergeMessageWithUnknownFieldForType(
1816       FieldDescriptor::TYPE_UINT64);
1817   TestBinaryPerformanceMergeMessageWithUnknownFieldForType(
1818       FieldDescriptor::TYPE_STRING);
1819   TestBinaryPerformanceMergeMessageWithUnknownFieldForType(
1820       FieldDescriptor::TYPE_BYTES);
1821 }
1822 
1823 template <typename MessageType>
RunJsonPerformanceTests()1824 void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonPerformanceTests() {
1825   TestJsonPerformanceMergeMessageWithRepeatedFieldForType(
1826       FieldDescriptor::TYPE_BOOL, "true");
1827   TestJsonPerformanceMergeMessageWithRepeatedFieldForType(
1828       FieldDescriptor::TYPE_DOUBLE, "123");
1829   TestJsonPerformanceMergeMessageWithRepeatedFieldForType(
1830       FieldDescriptor::TYPE_FLOAT, "123");
1831   TestJsonPerformanceMergeMessageWithRepeatedFieldForType(
1832       FieldDescriptor::TYPE_UINT32, "123");
1833   TestJsonPerformanceMergeMessageWithRepeatedFieldForType(
1834       FieldDescriptor::TYPE_UINT64, "123");
1835   TestJsonPerformanceMergeMessageWithRepeatedFieldForType(
1836       FieldDescriptor::TYPE_STRING, "\"foo\"");
1837   TestJsonPerformanceMergeMessageWithRepeatedFieldForType(
1838       FieldDescriptor::TYPE_BYTES, "\"foo\"");
1839 }
1840 
1841 // This is currently considered valid input by some languages but not others
1842 template <typename MessageType>
1843 void BinaryAndJsonConformanceSuiteImpl<MessageType>::
TestJsonPerformanceMergeMessageWithRepeatedFieldForType(FieldDescriptor::Type type,std::string field_value)1844     TestJsonPerformanceMergeMessageWithRepeatedFieldForType(
1845         FieldDescriptor::Type type, std::string field_value) {
1846   const std::string type_name =
1847       UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type)));
1848   const FieldDescriptor* field = GetFieldForType(type, true, Packed::kFalse);
1849   const absl::string_view field_name = field->name();
1850 
1851   std::string message_field =
1852       absl::StrCat("\"", field_name, "\": [", field_value, "]");
1853   std::string recursive_message =
1854       absl::StrCat("\"recursive_message\": { ", message_field, "}");
1855   std::string input = absl::StrCat("{", recursive_message);
1856   for (size_t i = 1; i < kPerformanceRepeatCount; i++) {
1857     absl::StrAppend(&input, ",", recursive_message);
1858   }
1859   absl::StrAppend(&input, "}");
1860 
1861   std::string textproto_message_field =
1862       absl::StrCat(field_name, ": ", field_value);
1863   std::string expected_textproto = "recursive_message { ";
1864   for (size_t i = 0; i < kPerformanceRepeatCount; i++) {
1865     absl::StrAppend(&expected_textproto, textproto_message_field, " ");
1866   }
1867   absl::StrAppend(&expected_textproto, "}");
1868   RunValidJsonTest(
1869       absl::StrCat("TestJsonPerformanceMergeMessageWithRepeatedFieldForType",
1870                    type_name),
1871       RECOMMENDED, input, expected_textproto);
1872 }
1873 
1874 template <typename MessageType>
RunJsonTests()1875 void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTests() {
1876   RunValidJsonTest("HelloWorld", REQUIRED,
1877                    "{\"optionalString\":\"Hello, World!\"}",
1878                    "optional_string: 'Hello, World!'");
1879 
1880   // NOTE: The spec for JSON support is still being sorted out, these may not
1881   // all be correct.
1882   RunJsonTestsForFieldNameConvention();
1883   RunJsonTestsForNonRepeatedTypes();
1884   RunJsonTestsForRepeatedTypes();
1885   RunJsonTestsForNullTypes();
1886 
1887   if (run_proto3_tests_) {
1888     RunJsonTestsForWrapperTypes();
1889     RunJsonTestsForFieldMask();
1890     RunJsonTestsForStruct();
1891     RunJsonTestsForValue();
1892     RunJsonTestsForAny();
1893   } else {
1894     // Currently Proto2 only, but should also be run on Proto3-optional.
1895     RunJsonTestsForStoresDefaultPrimitive();
1896   }
1897 
1898   RunJsonTestsForUnknownEnumStringValues();
1899 
1900   RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonNumber", REQUIRED,
1901                                 R"({"unknown": 1})", "");
1902   RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonString", REQUIRED,
1903                                 R"({"unknown": "a"})", "");
1904   RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonTrue", REQUIRED,
1905                                 R"({"unknown": true})", "");
1906   RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonFalse", REQUIRED,
1907                                 R"({"unknown": false})", "");
1908   RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonNull", REQUIRED,
1909                                 R"({"unknown": null})", "");
1910   RunValidJsonIgnoreUnknownTest("IgnoreUnknownJsonObject", REQUIRED,
1911                                 R"({"unknown": {"a": 1}})", "");
1912 
1913   ExpectParseFailureForJson("RejectTopLevelNull", REQUIRED, "null");
1914 }
1915 
1916 template <typename MessageType>
1917 void BinaryAndJsonConformanceSuiteImpl<
RunJsonTestsForStoresDefaultPrimitive()1918     MessageType>::RunJsonTestsForStoresDefaultPrimitive() {
1919   RunValidJsonTestWithValidator(
1920       "StoresDefaultPrimitive", REQUIRED,
1921       R"({
1922           "FieldName13": 0
1923         })",
1924       [](const Json::Value& value) { return value.isMember("FieldName13"); });
1925   std::vector<const FieldDescriptor*> extensions;
1926   MessageType::GetDescriptor()->file()->pool()->FindAllExtensions(
1927       MessageType::GetDescriptor(), &extensions);
1928   RunValidJsonTestWithValidator("FieldNameExtension", RECOMMENDED,
1929                                 absl::Substitute(R"({
1930           "[$0]": 1
1931         })",
1932                                                  extensions[0]->full_name()),
1933                                 [&](const Json::Value& value) {
1934                                   return value.isMember(absl::StrCat(
1935                                       "[", extensions[0]->full_name(), "]"));
1936                                 });
1937 }
1938 
1939 template <typename MessageType>
1940 void BinaryAndJsonConformanceSuiteImpl<
RunJsonTestsForUnknownEnumStringValues()1941     MessageType>::RunJsonTestsForUnknownEnumStringValues() {
1942   // Tests the handling of unknown enum values when encoded as string labels.
1943   // The expected behavior depends on whether unknown fields are ignored:
1944   // * when ignored, the parser should ignore the unknown enum string value.
1945   // * when not ignored, the parser should fail.
1946   struct TestCase {
1947     // Used in the test name.
1948     std::string enum_location;
1949     // JSON input which will contain the unknown field.
1950     std::string input_json;
1951   };
1952   const std::vector<TestCase> test_cases = {
1953       {"InOptionalField", R"json({
1954       "optional_nested_enum": "UNKNOWN_ENUM_VALUE"
1955     })json"},
1956       {"InRepeatedField", R"json({
1957       "repeated_nested_enum": ["UNKNOWN_ENUM_VALUE"]
1958     })json"},
1959       {"InMapValue", R"json({
1960       "map_string_nested_enum": {"key": "UNKNOWN_ENUM_VALUE"}
1961     })json"},
1962   };
1963   for (const TestCase& test_case : test_cases) {
1964     // Unknown enum string value is a parse failure when not ignoring unknown
1965     // fields.
1966     ExpectParseFailureForJson(
1967         absl::StrCat("RejectUnknownEnumStringValue", test_case.enum_location),
1968         RECOMMENDED, test_case.input_json);
1969     // Unknown enum string value is ignored when ignoring unknown fields.
1970     RunValidJsonIgnoreUnknownTest(
1971         absl::StrCat("IgnoreUnknownEnumStringValue", test_case.enum_location),
1972         RECOMMENDED, test_case.input_json, "");
1973   }
1974 
1975   // This test is similar to "InRepeatedField" from above, but it highlights the
1976   // potentially unexpected behavior in an array with mixed known and unknown
1977   // enum string values.
1978   RunValidJsonIgnoreUnknownTest("IgnoreUnknownEnumStringValueInRepeatedPart",
1979                                 RECOMMENDED,
1980                                 R"json({
1981     "repeated_nested_enum": [
1982       "FOO",
1983       "UNKNOWN_ENUM_VALUE",
1984       "FOO"
1985     ]})json",
1986                                 R"(
1987     repeated_nested_enum: FOO
1988     repeated_nested_enum: FOO
1989   )");
1990 
1991   // This test is similar to "InMapValue" from above with mixture of known and
1992   // unknown enum string values in the map.
1993   RunValidJsonIgnoreUnknownTest("IgnoreUnknownEnumStringValueInMapPart",
1994                                 RECOMMENDED,
1995                                 R"json({
1996     "map_string_nested_enum": {
1997       "key1": "FOO",
1998       "key2": "UNKNOWN_ENUM_VALUE"
1999     }})json",
2000                                 R"(
2001     map_string_nested_enum: {
2002       key: "key1"
2003       value: FOO
2004     }
2005   )");
2006 }
2007 
2008 template <typename MessageType>
2009 void BinaryAndJsonConformanceSuiteImpl<
RunJsonTestsForFieldNameConvention()2010     MessageType>::RunJsonTestsForFieldNameConvention() {
2011   RunValidJsonTest("FieldNameInSnakeCase", REQUIRED,
2012                    R"({
2013         "fieldname1": 1,
2014         "fieldName2": 2,
2015         "FieldName3": 3,
2016         "fieldName4": 4
2017       })",
2018                    R"(
2019         fieldname1: 1
2020         field_name2: 2
2021         _field_name3: 3
2022         field__name4_: 4
2023       )");
2024   RunValidJsonTest("FieldNameWithNumbers", REQUIRED,
2025                    R"({
2026         "field0name5": 5,
2027         "field0Name6": 6
2028       })",
2029                    R"(
2030         field0name5: 5
2031         field_0_name6: 6
2032       )");
2033   RunValidJsonTest("FieldNameWithMixedCases", REQUIRED,
2034                    R"({
2035         "fieldName7": 7,
2036         "FieldName8": 8,
2037         "fieldName9": 9,
2038         "FieldName10": 10,
2039         "FIELDNAME11": 11,
2040         "FIELDName12": 12
2041       })",
2042                    R"(
2043         fieldName7: 7
2044         FieldName8: 8
2045         field_Name9: 9
2046         Field_Name10: 10
2047         FIELD_NAME11: 11
2048         FIELD_name12: 12
2049       )");
2050   RunValidJsonTest("FieldNameWithDoubleUnderscores", RECOMMENDED,
2051                    R"({
2052         "FieldName13": 13,
2053         "FieldName14": 14,
2054         "fieldName15": 15,
2055         "fieldName16": 16,
2056         "fieldName17": 17,
2057         "FieldName18": 18
2058       })",
2059                    R"(
2060         __field_name13: 13
2061         __Field_name14: 14
2062         field__name15: 15
2063         field__Name16: 16
2064         field_name17__: 17
2065         Field_name18__: 18
2066       )");
2067   // Using the original proto field name in JSON is also allowed.
2068   RunValidJsonTest("OriginalProtoFieldName", REQUIRED,
2069                    R"({
2070         "fieldname1": 1,
2071         "field_name2": 2,
2072         "_field_name3": 3,
2073         "field__name4_": 4,
2074         "field0name5": 5,
2075         "field_0_name6": 6,
2076         "fieldName7": 7,
2077         "FieldName8": 8,
2078         "field_Name9": 9,
2079         "Field_Name10": 10,
2080         "FIELD_NAME11": 11,
2081         "FIELD_name12": 12,
2082         "__field_name13": 13,
2083         "__Field_name14": 14,
2084         "field__name15": 15,
2085         "field__Name16": 16,
2086         "field_name17__": 17,
2087         "Field_name18__": 18
2088       })",
2089                    R"(
2090         fieldname1: 1
2091         field_name2: 2
2092         _field_name3: 3
2093         field__name4_: 4
2094         field0name5: 5
2095         field_0_name6: 6
2096         fieldName7: 7
2097         FieldName8: 8
2098         field_Name9: 9
2099         Field_Name10: 10
2100         FIELD_NAME11: 11
2101         FIELD_name12: 12
2102         __field_name13: 13
2103         __Field_name14: 14
2104         field__name15: 15
2105         field__Name16: 16
2106         field_name17__: 17
2107         Field_name18__: 18
2108       )");
2109   // Field names can be escaped.
2110   RunValidJsonTest("FieldNameEscaped", REQUIRED, R"({"fieldn\u0061me1": 1})",
2111                    "fieldname1: 1");
2112   // String ends with escape character.
2113   ExpectParseFailureForJson("StringEndsWithEscapeChar", RECOMMENDED,
2114                             "{\"optionalString\": \"abc\\");
2115   // Field names must be quoted (or it's not valid JSON).
2116   ExpectParseFailureForJson("FieldNameNotQuoted", RECOMMENDED,
2117                             "{fieldname1: 1}");
2118   // Trailing comma is not allowed (not valid JSON).
2119   ExpectParseFailureForJson("TrailingCommaInAnObject", RECOMMENDED,
2120                             R"({"fieldname1":1,})");
2121   ExpectParseFailureForJson("TrailingCommaInAnObjectWithSpace", RECOMMENDED,
2122                             R"({"fieldname1":1 ,})");
2123   ExpectParseFailureForJson("TrailingCommaInAnObjectWithSpaceCommaSpace",
2124                             RECOMMENDED, R"({"fieldname1":1 , })");
2125   ExpectParseFailureForJson("TrailingCommaInAnObjectWithNewlines", RECOMMENDED,
2126                             R"({
2127         "fieldname1":1,
2128       })");
2129   // JSON doesn't support comments.
2130   ExpectParseFailureForJson("JsonWithComments", RECOMMENDED,
2131                             R"({
2132         // This is a comment.
2133         "fieldname1": 1
2134       })");
2135   // JSON spec says whitespace doesn't matter, so try a few spacings to be sure.
2136   RunValidJsonTest("OneLineNoSpaces", RECOMMENDED,
2137                    "{\"optionalInt32\":1,\"optionalInt64\":2}",
2138                    R"(
2139         optional_int32: 1
2140         optional_int64: 2
2141       )");
2142   RunValidJsonTest("OneLineWithSpaces", RECOMMENDED,
2143                    "{ \"optionalInt32\" : 1 , \"optionalInt64\" : 2 }",
2144                    R"(
2145         optional_int32: 1
2146         optional_int64: 2
2147       )");
2148   RunValidJsonTest("MultilineNoSpaces", RECOMMENDED,
2149                    "{\n\"optionalInt32\"\n:\n1\n,\n\"optionalInt64\"\n:\n2\n}",
2150                    R"(
2151         optional_int32: 1
2152         optional_int64: 2
2153       )");
2154   RunValidJsonTest(
2155       "MultilineWithSpaces", RECOMMENDED,
2156       "{\n  \"optionalInt32\"  :  1\n  ,\n  \"optionalInt64\"  :  2\n}\n",
2157       R"(
2158         optional_int32: 1
2159         optional_int64: 2
2160       )");
2161   // Missing comma between key/value pairs.
2162   ExpectParseFailureForJson("MissingCommaOneLine", RECOMMENDED,
2163                             "{ \"optionalInt32\": 1 \"optionalInt64\": 2 }");
2164   ExpectParseFailureForJson(
2165       "MissingCommaMultiline", RECOMMENDED,
2166       "{\n  \"optionalInt32\": 1\n  \"optionalInt64\": 2\n}");
2167   // Duplicated field names are not allowed.
2168   ExpectParseFailureForJson("FieldNameDuplicate", RECOMMENDED,
2169                             R"({
2170         "optionalNestedMessage": {a: 1},
2171         "optionalNestedMessage": {}
2172       })");
2173   ExpectParseFailureForJson("FieldNameDuplicateDifferentCasing1", RECOMMENDED,
2174                             R"({
2175         "optional_nested_message": {a: 1},
2176         "optionalNestedMessage": {}
2177       })");
2178   ExpectParseFailureForJson("FieldNameDuplicateDifferentCasing2", RECOMMENDED,
2179                             R"({
2180         "optionalNestedMessage": {a: 1},
2181         "optional_nested_message": {}
2182       })");
2183   // Serializers should use lowerCamelCase by default.
2184   RunValidJsonTestWithValidator("FieldNameInLowerCamelCase", REQUIRED,
2185                                 R"({
2186         "fieldname1": 1,
2187         "fieldName2": 2,
2188         "FieldName3": 3,
2189         "fieldName4": 4
2190       })",
2191                                 [](const Json::Value& value) {
2192                                   return value.isMember("fieldname1") &&
2193                                          value.isMember("fieldName2") &&
2194                                          value.isMember("FieldName3") &&
2195                                          value.isMember("fieldName4");
2196                                 });
2197   RunValidJsonTestWithValidator("FieldNameWithNumbers", REQUIRED,
2198                                 R"({
2199         "field0name5": 5,
2200         "field0Name6": 6
2201       })",
2202                                 [](const Json::Value& value) {
2203                                   return value.isMember("field0name5") &&
2204                                          value.isMember("field0Name6");
2205                                 });
2206   RunValidJsonTestWithValidator(
2207       "FieldNameWithMixedCases", REQUIRED,
2208       R"({
2209         "fieldName7": 7,
2210         "FieldName8": 8,
2211         "fieldName9": 9,
2212         "FieldName10": 10,
2213         "FIELDNAME11": 11,
2214         "FIELDName12": 12
2215       })",
2216       [](const Json::Value& value) {
2217         return value.isMember("fieldName7") && value.isMember("FieldName8") &&
2218                value.isMember("fieldName9") && value.isMember("FieldName10") &&
2219                value.isMember("FIELDNAME11") && value.isMember("FIELDName12");
2220       });
2221   RunValidJsonTestWithValidator(
2222       "FieldNameWithDoubleUnderscores", RECOMMENDED,
2223       R"({
2224         "FieldName13": 13,
2225         "FieldName14": 14,
2226         "fieldName15": 15,
2227         "fieldName16": 16,
2228         "fieldName17": 17,
2229         "FieldName18": 18
2230       })",
2231       [](const Json::Value& value) {
2232         return value.isMember("FieldName13") && value.isMember("FieldName14") &&
2233                value.isMember("fieldName15") && value.isMember("fieldName16") &&
2234                value.isMember("fieldName17") && value.isMember("FieldName18");
2235       });
2236 
2237   if (run_proto3_tests_) {
2238     RunValidJsonTestWithValidator("SkipsDefaultPrimitive", REQUIRED,
2239                                   R"({"FieldName13": 0})",
2240                                   [](const Json::Value& value) {
2241                                     return !value.isMember("FieldName13");
2242                                   });
2243   }
2244 }
2245 
2246 template <typename MessageType>
2247 void BinaryAndJsonConformanceSuiteImpl<
RunJsonTestsForNonRepeatedTypes()2248     MessageType>::RunJsonTestsForNonRepeatedTypes() {
2249   // Integer fields.
2250   RunValidJsonTest("Int32FieldMaxValue", REQUIRED,
2251                    R"({"optionalInt32": 2147483647})",
2252                    "optional_int32: 2147483647");
2253   RunValidJsonTest("Int32FieldMinValue", REQUIRED,
2254                    R"({"optionalInt32": -2147483648})",
2255                    "optional_int32: -2147483648");
2256   RunValidJsonTest("Uint32FieldMaxValue", REQUIRED,
2257                    R"({"optionalUint32": 4294967295})",
2258                    "optional_uint32: 4294967295");
2259   RunValidJsonTest("Int64FieldMaxValue", REQUIRED,
2260                    R"({"optionalInt64": "9223372036854775807"})",
2261                    "optional_int64: 9223372036854775807");
2262   RunValidJsonTest("Int64FieldMinValue", REQUIRED,
2263                    R"({"optionalInt64": "-9223372036854775808"})",
2264                    "optional_int64: -9223372036854775808");
2265   RunValidJsonTest("Uint64FieldMaxValue", REQUIRED,
2266                    R"({"optionalUint64": "18446744073709551615"})",
2267                    "optional_uint64: 18446744073709551615");
2268   // While not the largest Int64, this is the largest
2269   // Int64 which can be exactly represented within an
2270   // IEEE-754 64-bit float, which is the expected level
2271   // of interoperability guarantee. Larger values may
2272   // work in some implementations, but should not be
2273   // relied upon.
2274   RunValidJsonTest("Int64FieldMaxValueNotQuoted", REQUIRED,
2275                    R"({"optionalInt64": 9223372036854774784})",
2276                    "optional_int64: 9223372036854774784");
2277   RunValidJsonTest("Int64FieldMinValueNotQuoted", REQUIRED,
2278                    R"({"optionalInt64": -9223372036854775808})",
2279                    "optional_int64: -9223372036854775808");
2280   // Largest interoperable Uint64; see comment above
2281   // for Int64FieldMaxValueNotQuoted.
2282   RunValidJsonTest("Uint64FieldMaxValueNotQuoted", REQUIRED,
2283                    R"({"optionalUint64": 18446744073709549568})",
2284                    "optional_uint64: 18446744073709549568");
2285   // Values can be represented as JSON strings.
2286   RunValidJsonTest("Int32FieldStringValue", REQUIRED,
2287                    R"({"optionalInt32": "2147483647"})",
2288                    "optional_int32: 2147483647");
2289   RunValidJsonTest("Int32FieldStringValueEscaped", REQUIRED,
2290                    R"({"optionalInt32": "2\u003147483647"})",
2291                    "optional_int32: 2147483647");
2292 
2293   // Parsers reject out-of-bound integer values.
2294   ExpectParseFailureForJson("Int32FieldTooLarge", REQUIRED,
2295                             R"({"optionalInt32": 2147483648})");
2296   ExpectParseFailureForJson("Int32FieldTooSmall", REQUIRED,
2297                             R"({"optionalInt32": -2147483649})");
2298   ExpectParseFailureForJson("Uint32FieldTooLarge", REQUIRED,
2299                             R"({"optionalUint32": 4294967296})");
2300   ExpectParseFailureForJson("Int64FieldTooLarge", REQUIRED,
2301                             R"({"optionalInt64": "9223372036854775808"})");
2302   ExpectParseFailureForJson("Int64FieldTooSmall", REQUIRED,
2303                             R"({"optionalInt64": "-9223372036854775809"})");
2304   ExpectParseFailureForJson("Uint64FieldTooLarge", REQUIRED,
2305                             R"({"optionalUint64": "18446744073709551616"})");
2306   // Parser reject non-integer numeric values as well.
2307   ExpectParseFailureForJson("Int32FieldNotInteger", REQUIRED,
2308                             R"({"optionalInt32": 0.5})");
2309   ExpectParseFailureForJson("Uint32FieldNotInteger", REQUIRED,
2310                             R"({"optionalUint32": 0.5})");
2311   ExpectParseFailureForJson("Int64FieldNotInteger", REQUIRED,
2312                             R"({"optionalInt64": "0.5"})");
2313   ExpectParseFailureForJson("Uint64FieldNotInteger", REQUIRED,
2314                             R"({"optionalUint64": "0.5"})");
2315 
2316   // Integers but represented as float values are accepted.
2317   RunValidJsonTest("Int32FieldFloatTrailingZero", REQUIRED,
2318                    R"({"optionalInt32": 100000.000})",
2319                    "optional_int32: 100000");
2320   RunValidJsonTest("Int32FieldExponentialFormat", REQUIRED,
2321                    R"({"optionalInt32": 1e5})", "optional_int32: 100000");
2322   RunValidJsonTest("Int32FieldMaxFloatValue", REQUIRED,
2323                    R"({"optionalInt32": 2.147483647e9})",
2324                    "optional_int32: 2147483647");
2325   RunValidJsonTest("Int32FieldMinFloatValue", REQUIRED,
2326                    R"({"optionalInt32": -2.147483648e9})",
2327                    "optional_int32: -2147483648");
2328   RunValidJsonTest("Uint32FieldMaxFloatValue", REQUIRED,
2329                    R"({"optionalUint32": 4.294967295e9})",
2330                    "optional_uint32: 4294967295");
2331 
2332   // Parser reject non-numeric values.
2333   ExpectParseFailureForJson("Int32FieldNotNumber", REQUIRED,
2334                             R"({"optionalInt32": "3x3"})");
2335   ExpectParseFailureForJson("Uint32FieldNotNumber", REQUIRED,
2336                             R"({"optionalUint32": "3x3"})");
2337   ExpectParseFailureForJson("Int64FieldNotNumber", REQUIRED,
2338                             R"({"optionalInt64": "3x3"})");
2339   ExpectParseFailureForJson("Uint64FieldNotNumber", REQUIRED,
2340                             R"({"optionalUint64": "3x3"})");
2341   // JSON does not allow "+" on numeric values.
2342   ExpectParseFailureForJson("Int32FieldPlusSign", REQUIRED,
2343                             R"({"optionalInt32": +1})");
2344   // JSON doesn't allow leading 0s.
2345   ExpectParseFailureForJson("Int32FieldLeadingZero", REQUIRED,
2346                             R"({"optionalInt32": 01})");
2347   ExpectParseFailureForJson("Int32FieldNegativeWithLeadingZero", REQUIRED,
2348                             R"({"optionalInt32": -01})");
2349   // String values must follow the same syntax rule. Specifically leading
2350   // or trailing spaces are not allowed.
2351   ExpectParseFailureForJson("Int32FieldLeadingSpace", REQUIRED,
2352                             R"({"optionalInt32": " 1"})");
2353   ExpectParseFailureForJson("Int32FieldTrailingSpace", REQUIRED,
2354                             R"({"optionalInt32": "1 "})");
2355 
2356   // 64-bit values are serialized as strings.
2357   RunValidJsonTestWithValidator(
2358       "Int64FieldBeString", RECOMMENDED, R"({"optionalInt64": 1})",
2359       [](const Json::Value& value) {
2360         return value["optionalInt64"].type() == Json::stringValue &&
2361                value["optionalInt64"].asString() == "1";
2362       });
2363   RunValidJsonTestWithValidator(
2364       "Uint64FieldBeString", RECOMMENDED, R"({"optionalUint64": 1})",
2365       [](const Json::Value& value) {
2366         return value["optionalUint64"].type() == Json::stringValue &&
2367                value["optionalUint64"].asString() == "1";
2368       });
2369 
2370   // Bool fields.
2371   RunValidJsonTest("BoolFieldTrue", REQUIRED, R"({"optionalBool":true})",
2372                    "optional_bool: true");
2373   RunValidJsonTest("BoolFieldFalse", REQUIRED, R"({"optionalBool":false})",
2374                    "optional_bool: false");
2375 
2376   // Other forms are not allowed.
2377   ExpectParseFailureForJson("BoolFieldIntegerZero", RECOMMENDED,
2378                             R"({"optionalBool":0})");
2379   ExpectParseFailureForJson("BoolFieldIntegerOne", RECOMMENDED,
2380                             R"({"optionalBool":1})");
2381   ExpectParseFailureForJson("BoolFieldCamelCaseTrue", RECOMMENDED,
2382                             R"({"optionalBool":True})");
2383   ExpectParseFailureForJson("BoolFieldCamelCaseFalse", RECOMMENDED,
2384                             R"({"optionalBool":False})");
2385   ExpectParseFailureForJson("BoolFieldAllCapitalTrue", RECOMMENDED,
2386                             R"({"optionalBool":TRUE})");
2387   ExpectParseFailureForJson("BoolFieldAllCapitalFalse", RECOMMENDED,
2388                             R"({"optionalBool":FALSE})");
2389   ExpectParseFailureForJson("BoolFieldDoubleQuotedTrue", RECOMMENDED,
2390                             R"({"optionalBool":"true"})");
2391   ExpectParseFailureForJson("BoolFieldDoubleQuotedFalse", RECOMMENDED,
2392                             R"({"optionalBool":"false"})");
2393 
2394   // Float fields.
2395   RunValidJsonTest("FloatFieldMinPositiveValue", REQUIRED,
2396                    R"({"optionalFloat": 1.175494e-38})",
2397                    "optional_float: 1.175494e-38");
2398   RunValidJsonTest("FloatFieldMaxNegativeValue", REQUIRED,
2399                    R"({"optionalFloat": -1.175494e-38})",
2400                    "optional_float: -1.175494e-38");
2401   RunValidJsonTest("FloatFieldMaxPositiveValue", REQUIRED,
2402                    R"({"optionalFloat": 3.402823e+38})",
2403                    "optional_float: 3.402823e+38");
2404   RunValidJsonTest("FloatFieldMinNegativeValue", REQUIRED,
2405                    R"({"optionalFloat": 3.402823e+38})",
2406                    "optional_float: 3.402823e+38");
2407   // Values can be quoted.
2408   RunValidJsonTest("FloatFieldQuotedValue", REQUIRED,
2409                    R"({"optionalFloat": "1"})", "optional_float: 1");
2410   // Special values.
2411   RunValidJsonTest("FloatFieldNan", REQUIRED, R"({"optionalFloat": "NaN"})",
2412                    "optional_float: nan");
2413   RunValidJsonTest("FloatFieldInfinity", REQUIRED,
2414                    R"({"optionalFloat": "Infinity"})", "optional_float: inf");
2415   RunValidJsonTest("FloatFieldNegativeInfinity", REQUIRED,
2416                    R"({"optionalFloat": "-Infinity"})", "optional_float: -inf");
2417   // Non-canonical Nan will be correctly normalized.
2418   {
2419     MessageType message;
2420     // IEEE floating-point standard 32-bit quiet NaN:
2421     //   0111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
2422     message.set_optional_float(WireFormatLite::DecodeFloat(0x7FA12345));
2423     RunValidJsonTestWithProtobufInput("FloatFieldNormalizeQuietNan", REQUIRED,
2424                                       message, "optional_float: nan");
2425     // IEEE floating-point standard 64-bit signaling NaN:
2426     //   1111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
2427     message.set_optional_float(WireFormatLite::DecodeFloat(0xFFB54321));
2428     RunValidJsonTestWithProtobufInput("FloatFieldNormalizeSignalingNan",
2429                                       REQUIRED, message, "optional_float: nan");
2430   }
2431 
2432   // Special values must be quoted.
2433   ExpectParseFailureForJson("FloatFieldNanNotQuoted", RECOMMENDED,
2434                             R"({"optionalFloat": NaN})");
2435   ExpectParseFailureForJson("FloatFieldInfinityNotQuoted", RECOMMENDED,
2436                             R"({"optionalFloat": Infinity})");
2437   ExpectParseFailureForJson("FloatFieldNegativeInfinityNotQuoted", RECOMMENDED,
2438                             R"({"optionalFloat": -Infinity})");
2439   // Parsers should reject out-of-bound values.
2440   ExpectParseFailureForJson("FloatFieldTooSmall", REQUIRED,
2441                             R"({"optionalFloat": -3.502823e+38})");
2442   ExpectParseFailureForJson("FloatFieldTooLarge", REQUIRED,
2443                             R"({"optionalFloat": 3.502823e+38})");
2444 
2445   // Double fields.
2446   RunValidJsonTest("DoubleFieldMinPositiveValue", REQUIRED,
2447                    R"({"optionalDouble": 2.22507e-308})",
2448                    "optional_double: 2.22507e-308");
2449   RunValidJsonTest("DoubleFieldMaxNegativeValue", REQUIRED,
2450                    R"({"optionalDouble": -2.22507e-308})",
2451                    "optional_double: -2.22507e-308");
2452   RunValidJsonTest("DoubleFieldMaxPositiveValue", REQUIRED,
2453                    R"({"optionalDouble": 1.79769e+308})",
2454                    "optional_double: 1.79769e+308");
2455   RunValidJsonTest("DoubleFieldMinNegativeValue", REQUIRED,
2456                    R"({"optionalDouble": -1.79769e+308})",
2457                    "optional_double: -1.79769e+308");
2458   // Values can be quoted.
2459   RunValidJsonTest("DoubleFieldQuotedValue", REQUIRED,
2460                    R"({"optionalDouble": "1"})", "optional_double: 1");
2461   // Special values.
2462   RunValidJsonTest("DoubleFieldNan", REQUIRED, R"({"optionalDouble": "NaN"})",
2463                    "optional_double: nan");
2464   RunValidJsonTest("DoubleFieldInfinity", REQUIRED,
2465                    R"({"optionalDouble": "Infinity"})", "optional_double: inf");
2466   RunValidJsonTest("DoubleFieldNegativeInfinity", REQUIRED,
2467                    R"({"optionalDouble": "-Infinity"})",
2468                    "optional_double: -inf");
2469   // Non-canonical Nan will be correctly normalized.
2470   {
2471     MessageType message;
2472     message.set_optional_double(
2473         WireFormatLite::DecodeDouble(int64_t{0x7FFA123456789ABC}));
2474     RunValidJsonTestWithProtobufInput("DoubleFieldNormalizeQuietNan", REQUIRED,
2475                                       message, "optional_double: nan");
2476     message.set_optional_double(
2477         WireFormatLite::DecodeDouble(uint64_t{0xFFFBCBA987654321}));
2478     RunValidJsonTestWithProtobufInput("DoubleFieldNormalizeSignalingNan",
2479                                       REQUIRED, message,
2480                                       "optional_double: nan");
2481   }
2482 
2483   // Special values must be quoted.
2484   ExpectParseFailureForJson("DoubleFieldNanNotQuoted", RECOMMENDED,
2485                             R"({"optionalDouble": NaN})");
2486   ExpectParseFailureForJson("DoubleFieldInfinityNotQuoted", RECOMMENDED,
2487                             R"({"optionalDouble": Infinity})");
2488   ExpectParseFailureForJson("DoubleFieldNegativeInfinityNotQuoted", RECOMMENDED,
2489                             R"({"optionalDouble": -Infinity})");
2490 
2491   // Parsers should reject out-of-bound values.
2492   ExpectParseFailureForJson("DoubleFieldTooSmall", REQUIRED,
2493                             R"({"optionalDouble": -1.89769e+308})");
2494   ExpectParseFailureForJson("DoubleFieldTooLarge", REQUIRED,
2495                             R"({"optionalDouble": +1.89769e+308})");
2496 
2497   // Enum fields.
2498   RunValidJsonTest("EnumField", REQUIRED, R"({"optionalNestedEnum": "FOO"})",
2499                    "optional_nested_enum: FOO");
2500 
2501   // Enum fields with alias
2502   if (run_proto3_tests_) {
2503     RunValidJsonTest("EnumFieldWithAlias", REQUIRED,
2504                      R"({"optionalAliasedEnum": "ALIAS_BAZ"})",
2505                      "optional_aliased_enum: ALIAS_BAZ");
2506     RunValidJsonTest("EnumFieldWithAliasUseAlias", REQUIRED,
2507                      R"({"optionalAliasedEnum": "MOO"})",
2508                      "optional_aliased_enum: ALIAS_BAZ");
2509     RunValidJsonTest("EnumFieldWithAliasLowerCase", REQUIRED,
2510                      R"({"optionalAliasedEnum": "moo"})",
2511                      "optional_aliased_enum: ALIAS_BAZ");
2512     RunValidJsonTest("EnumFieldWithAliasDifferentCase", REQUIRED,
2513                      R"({"optionalAliasedEnum": "bAz"})",
2514                      "optional_aliased_enum: ALIAS_BAZ");
2515   }
2516 
2517   // Enum values must be represented as strings.
2518   ExpectParseFailureForJson("EnumFieldNotQuoted", REQUIRED,
2519                             R"({"optionalNestedEnum": FOO})");
2520   // Numeric values are allowed.
2521   RunValidJsonTest("EnumFieldNumericValueZero", REQUIRED,
2522                    R"({"optionalNestedEnum": 0})", "optional_nested_enum: FOO");
2523   RunValidJsonTest("EnumFieldNumericValueNonZero", REQUIRED,
2524                    R"({"optionalNestedEnum": 1})", "optional_nested_enum: BAR");
2525 
2526   if (run_proto3_tests_) {
2527     // Unknown enum values are represented as numeric values.
2528     RunValidJsonTestWithValidator(
2529         "EnumFieldUnknownValue", REQUIRED, R"({"optionalNestedEnum": 123})",
2530         [](const Json::Value& value) {
2531           return value["optionalNestedEnum"].type() == Json::intValue &&
2532                  value["optionalNestedEnum"].asInt() == 123;
2533         });
2534   }
2535 
2536   // String fields.
2537   RunValidJsonTest("StringField", REQUIRED,
2538                    R"({"optionalString": "Hello world!"})",
2539                    R"(optional_string: "Hello world!")");
2540   RunValidJsonTest("StringFieldUnicode", REQUIRED,
2541                    // Google in Chinese.
2542                    R"({"optionalString": "谷歌"})",
2543                    R"(optional_string: "谷歌")");
2544   RunValidJsonTest("StringFieldEscape", REQUIRED,
2545                    R"({"optionalString": "\"\\\/\b\f\n\r\t"})",
2546                    R"(optional_string: "\"\\/\b\f\n\r\t")");
2547   RunValidJsonTest("StringFieldUnicodeEscape", REQUIRED,
2548                    R"({"optionalString": "\u8C37\u6B4C"})",
2549                    R"(optional_string: "谷歌")");
2550   RunValidJsonTest("StringFieldUnicodeEscapeWithLowercaseHexLetters", REQUIRED,
2551                    R"({"optionalString": "\u8c37\u6b4c"})",
2552                    R"(optional_string: "谷歌")");
2553   RunValidJsonTest(
2554       "StringFieldSurrogatePair", REQUIRED,
2555       // The character is an emoji: grinning face with smiling eyes. ��
2556       R"({"optionalString": "\uD83D\uDE01"})",
2557       R"(optional_string: "\xF0\x9F\x98\x81")");
2558   RunValidJsonTest("StringFieldEmbeddedNull", REQUIRED,
2559                    R"({"optionalString": "Hello\u0000world!"})",
2560                    R"(optional_string: "Hello\000world!")");
2561 
2562   // Unicode escapes must start with "\u" (lowercase u).
2563   ExpectParseFailureForJson("StringFieldUppercaseEscapeLetter", RECOMMENDED,
2564                             R"({"optionalString": "\U8C37\U6b4C"})");
2565   ExpectParseFailureForJson("StringFieldInvalidEscape", RECOMMENDED,
2566                             R"({"optionalString": "\uXXXX\u6B4C"})");
2567   ExpectParseFailureForJson("StringFieldUnterminatedEscape", RECOMMENDED,
2568                             R"({"optionalString": "\u8C3"})");
2569   ExpectParseFailureForJson("StringFieldUnpairedHighSurrogate", RECOMMENDED,
2570                             R"({"optionalString": "\uD800"})");
2571   ExpectParseFailureForJson("StringFieldUnpairedLowSurrogate", RECOMMENDED,
2572                             R"({"optionalString": "\uDC00"})");
2573   ExpectParseFailureForJson("StringFieldSurrogateInWrongOrder", RECOMMENDED,
2574                             R"({"optionalString": "\uDE01\uD83D"})");
2575   ExpectParseFailureForJson("StringFieldNotAString", REQUIRED,
2576                             R"({"optionalString": 12345})");
2577 
2578   // Bytes fields.
2579   RunValidJsonTest("BytesField", REQUIRED, R"({"optionalBytes": "AQI="})",
2580                    R"(optional_bytes: "\x01\x02")");
2581   RunValidJsonTest("BytesFieldBase64Url", RECOMMENDED,
2582                    R"({"optionalBytes": "-_"})", R"(optional_bytes: "\xfb")");
2583 
2584   // Message fields.
2585   RunValidJsonTest("MessageField", REQUIRED,
2586                    R"({"optionalNestedMessage": {"a": 1234}})",
2587                    "optional_nested_message: {a: 1234}");
2588 
2589   // Oneof fields.
2590   ExpectParseFailureForJson("OneofFieldDuplicate", REQUIRED,
2591                             R"({"oneofUint32": 1, "oneofString": "test"})");
2592   RunValidJsonTest("OneofFieldNullFirst", REQUIRED,
2593                    R"({"oneofUint32": null, "oneofString": "test"})",
2594                    "oneof_string: \"test\"");
2595   RunValidJsonTest("OneofFieldNullSecond", REQUIRED,
2596                    R"({"oneofString": "test", "oneofUint32": null})",
2597                    "oneof_string: \"test\"");
2598   RunValidJsonTest("OneofZeroUint32", RECOMMENDED, R"({"oneofUint32": 0})",
2599                    "oneof_uint32: 0");
2600   RunValidJsonTest("OneofZeroMessage", RECOMMENDED,
2601                    R"({"oneofNestedMessage": {}})", "oneof_nested_message: {}");
2602   RunValidJsonTest("OneofZeroString", RECOMMENDED, R"({"oneofString": ""})",
2603                    "oneof_string: \"\"");
2604   RunValidJsonTest("OneofZeroBytes", RECOMMENDED, R"({"oneofBytes": ""})",
2605                    "oneof_bytes: \"\"");
2606   RunValidJsonTest("OneofZeroBool", RECOMMENDED, R"({"oneofBool": false})",
2607                    "oneof_bool: false");
2608   RunValidJsonTest("OneofZeroUint64", RECOMMENDED, R"({"oneofUint64": 0})",
2609                    "oneof_uint64: 0");
2610   RunValidJsonTest("OneofZeroFloat", RECOMMENDED, R"({"oneofFloat": 0.0})",
2611                    "oneof_float: 0");
2612   RunValidJsonTest("OneofZeroDouble", RECOMMENDED, R"({"oneofDouble": 0.0})",
2613                    "oneof_double: 0");
2614   RunValidJsonTest("OneofZeroEnum", RECOMMENDED, R"({"oneofEnum":"FOO"})",
2615                    "oneof_enum: FOO");
2616 
2617   // Map fields.
2618   RunValidJsonTest("Int32MapField", REQUIRED,
2619                    R"({"mapInt32Int32": {"1": 2, "3": 4}})",
2620                    "map_int32_int32: {key: 1 value: 2}"
2621                    "map_int32_int32: {key: 3 value: 4}");
2622   ExpectParseFailureForJson("Int32MapFieldKeyNotQuoted", RECOMMENDED,
2623                             R"({"mapInt32Int32": {1: 2, 3: 4}})");
2624   RunValidJsonTest("Uint32MapField", REQUIRED,
2625                    R"({"mapUint32Uint32": {"1": 2, "3": 4}})",
2626                    "map_uint32_uint32: {key: 1 value: 2}"
2627                    "map_uint32_uint32: {key: 3 value: 4}");
2628   ExpectParseFailureForJson("Uint32MapFieldKeyNotQuoted", RECOMMENDED,
2629                             R"({"mapUint32Uint32": {1: 2, 3: 4}})");
2630   RunValidJsonTest("Int64MapField", REQUIRED,
2631                    R"({"mapInt64Int64": {"1": 2, "3": 4}})",
2632                    "map_int64_int64: {key: 1 value: 2}"
2633                    "map_int64_int64: {key: 3 value: 4}");
2634   ExpectParseFailureForJson("Int64MapFieldKeyNotQuoted", RECOMMENDED,
2635                             R"({"mapInt64Int64": {1: 2, 3: 4}})");
2636   RunValidJsonTest("Uint64MapField", REQUIRED,
2637                    R"({"mapUint64Uint64": {"1": 2, "3": 4}})",
2638                    "map_uint64_uint64: {key: 1 value: 2}"
2639                    "map_uint64_uint64: {key: 3 value: 4}");
2640   ExpectParseFailureForJson("Uint64MapFieldKeyNotQuoted", RECOMMENDED,
2641                             R"({"mapUint64Uint64": {1: 2, 3: 4}})");
2642   RunValidJsonTest("BoolMapField", REQUIRED,
2643                    R"({"mapBoolBool": {"true": true, "false": false}})",
2644                    "map_bool_bool: {key: true value: true}"
2645                    "map_bool_bool: {key: false value: false}");
2646   ExpectParseFailureForJson("BoolMapFieldKeyNotQuoted", RECOMMENDED,
2647                             R"({"mapBoolBool": {true: true, false: false}})");
2648   RunValidJsonTest("MessageMapField", REQUIRED,
2649                    R"({
2650         "mapStringNestedMessage": {
2651           "hello": {"a": 1234},
2652           "world": {"a": 5678}
2653   }
2654       })",
2655                    R"(
2656         map_string_nested_message: {
2657           key: "hello"
2658           value: {a: 1234}
2659   }
2660         map_string_nested_message: {
2661           key: "world"
2662           value: {a: 5678}
2663   }
2664       )");
2665   // Since Map keys are represented as JSON strings, escaping should be allowed.
2666   RunValidJsonTest("Int32MapEscapedKey", REQUIRED,
2667                    R"({"mapInt32Int32": {"\u0031": 2}})",
2668                    "map_int32_int32: {key: 1 value: 2}");
2669   RunValidJsonTest("Int64MapEscapedKey", REQUIRED,
2670                    R"({"mapInt64Int64": {"\u0031": 2}})",
2671                    "map_int64_int64: {key: 1 value: 2}");
2672   RunValidJsonTest("BoolMapEscapedKey", REQUIRED,
2673                    R"({"mapBoolBool": {"tr\u0075e": true}})",
2674                    "map_bool_bool: {key: true value: true}");
2675 
2676   // http://www.rfc-editor.org/rfc/rfc7159.txt says strings have to use double
2677   // quotes.
2678   ExpectParseFailureForJson("StringFieldSingleQuoteKey", RECOMMENDED,
2679                             R"({'optionalString': "Hello world!"})");
2680   ExpectParseFailureForJson("StringFieldSingleQuoteValue", RECOMMENDED,
2681                             R"({"optionalString": 'Hello world!'})");
2682   ExpectParseFailureForJson("StringFieldSingleQuoteBoth", RECOMMENDED,
2683                             R"({'optionalString': 'Hello world!'})");
2684 }
2685 
2686 template <typename MessageType>
2687 void BinaryAndJsonConformanceSuiteImpl<
RunJsonTestsForRepeatedTypes()2688     MessageType>::RunJsonTestsForRepeatedTypes() {
2689   // Repeated fields.
2690   RunValidJsonTest("PrimitiveRepeatedField", REQUIRED,
2691                    R"({"repeatedInt32": [1, 2, 3, 4]})",
2692                    "repeated_int32: [1, 2, 3, 4]");
2693   RunValidJsonTest("EnumRepeatedField", REQUIRED,
2694                    R"({"repeatedNestedEnum": ["FOO", "BAR", "BAZ"]})",
2695                    "repeated_nested_enum: [FOO, BAR, BAZ]");
2696   RunValidJsonTest("StringRepeatedField", REQUIRED,
2697                    R"({"repeatedString": ["Hello", "world"]})",
2698                    R"(repeated_string: ["Hello", "world"])");
2699   RunValidJsonTest("BytesRepeatedField", REQUIRED,
2700                    R"({"repeatedBytes": ["AAEC", "AQI="]})",
2701                    R"(repeated_bytes: ["\x00\x01\x02", "\x01\x02"])");
2702   RunValidJsonTest("MessageRepeatedField", REQUIRED,
2703                    R"({"repeatedNestedMessage": [{"a": 1234}, {"a": 5678}]})",
2704                    "repeated_nested_message: {a: 1234}"
2705                    "repeated_nested_message: {a: 5678}");
2706 
2707   // Repeated field elements are of incorrect type.
2708   ExpectParseFailureForJson(
2709       "RepeatedFieldWrongElementTypeExpectingIntegersGotBool", REQUIRED,
2710       R"({"repeatedInt32": [1, false, 3, 4]})");
2711   ExpectParseFailureForJson(
2712       "RepeatedFieldWrongElementTypeExpectingIntegersGotString", REQUIRED,
2713       R"({"repeatedInt32": [1, 2, "name", 4]})");
2714   ExpectParseFailureForJson(
2715       "RepeatedFieldWrongElementTypeExpectingIntegersGotMessage", REQUIRED,
2716       R"({"repeatedInt32": [1, 2, 3, {"a": 4}]})");
2717   ExpectParseFailureForJson(
2718       "RepeatedFieldWrongElementTypeExpectingStringsGotInt", REQUIRED,
2719       R"({"repeatedString": ["1", 2, "3", "4"]})");
2720   ExpectParseFailureForJson(
2721       "RepeatedFieldWrongElementTypeExpectingStringsGotBool", REQUIRED,
2722       R"({"repeatedString": ["1", "2", false, "4"]})");
2723   ExpectParseFailureForJson(
2724       "RepeatedFieldWrongElementTypeExpectingStringsGotMessage", REQUIRED,
2725       R"({"repeatedString": ["1", 2, "3", {"a": 4}]})");
2726   ExpectParseFailureForJson(
2727       "RepeatedFieldWrongElementTypeExpectingMessagesGotInt", REQUIRED,
2728       R"({"repeatedNestedMessage": [{"a": 1}, 2]})");
2729   ExpectParseFailureForJson(
2730       "RepeatedFieldWrongElementTypeExpectingMessagesGotBool", REQUIRED,
2731       R"({"repeatedNestedMessage": [{"a": 1}, false]})");
2732   ExpectParseFailureForJson(
2733       "RepeatedFieldWrongElementTypeExpectingMessagesGotString", REQUIRED,
2734       R"({"repeatedNestedMessage": [{"a": 1}, "2"]})");
2735   // Trailing comma in the repeated field is not allowed.
2736   ExpectParseFailureForJson("RepeatedFieldTrailingComma", RECOMMENDED,
2737                             R"({"repeatedInt32": [1, 2, 3, 4,]})");
2738   ExpectParseFailureForJson("RepeatedFieldTrailingCommaWithSpace", RECOMMENDED,
2739                             "{\"repeatedInt32\": [1, 2, 3, 4 ,]}");
2740   ExpectParseFailureForJson("RepeatedFieldTrailingCommaWithSpaceCommaSpace",
2741                             RECOMMENDED,
2742                             "{\"repeatedInt32\": [1, 2, 3, 4 , ]}");
2743   ExpectParseFailureForJson(
2744       "RepeatedFieldTrailingCommaWithNewlines", RECOMMENDED,
2745       "{\"repeatedInt32\": [\n  1,\n  2,\n  3,\n  4,\n]}");
2746 }
2747 
2748 template <typename MessageType>
2749 void BinaryAndJsonConformanceSuiteImpl<
RunJsonTestsForNullTypes()2750     MessageType>::RunJsonTestsForNullTypes() {
2751   // "null" is accepted for all fields types.
2752   RunValidJsonTest("AllFieldAcceptNull", REQUIRED,
2753                    R"({
2754         "optionalInt32": null,
2755         "optionalInt64": null,
2756         "optionalUint32": null,
2757         "optionalUint64": null,
2758         "optionalSint32": null,
2759         "optionalSint64": null,
2760         "optionalFixed32": null,
2761         "optionalFixed64": null,
2762         "optionalSfixed32": null,
2763         "optionalSfixed64": null,
2764         "optionalFloat": null,
2765         "optionalDouble": null,
2766         "optionalBool": null,
2767         "optionalString": null,
2768         "optionalBytes": null,
2769         "optionalNestedEnum": null,
2770         "optionalNestedMessage": null,
2771         "repeatedInt32": null,
2772         "repeatedInt64": null,
2773         "repeatedUint32": null,
2774         "repeatedUint64": null,
2775         "repeatedSint32": null,
2776         "repeatedSint64": null,
2777         "repeatedFixed32": null,
2778         "repeatedFixed64": null,
2779         "repeatedSfixed32": null,
2780         "repeatedSfixed64": null,
2781         "repeatedFloat": null,
2782         "repeatedDouble": null,
2783         "repeatedBool": null,
2784         "repeatedString": null,
2785         "repeatedBytes": null,
2786         "repeatedNestedEnum": null,
2787         "repeatedNestedMessage": null,
2788         "mapInt32Int32": null,
2789         "mapBoolBool": null,
2790         "mapStringNestedMessage": null
2791       })",
2792                    "");
2793 
2794   // Repeated field elements cannot be null.
2795   ExpectParseFailureForJson("RepeatedFieldPrimitiveElementIsNull", RECOMMENDED,
2796                             R"({"repeatedInt32": [1, null, 2]})");
2797   ExpectParseFailureForJson(
2798       "RepeatedFieldMessageElementIsNull", RECOMMENDED,
2799       R"({"repeatedNestedMessage": [{"a":1}, null, {"a":2}]})");
2800   // Map field keys cannot be null.
2801   ExpectParseFailureForJson("MapFieldKeyIsNull", RECOMMENDED,
2802                             R"({"mapInt32Int32": {null: 1}})");
2803   // Map field values cannot be null.
2804   ExpectParseFailureForJson("MapFieldValueIsNull", RECOMMENDED,
2805                             R"({"mapInt32Int32": {"0": null}})");
2806 }
2807 
2808 template <typename MessageType>
2809 void BinaryAndJsonConformanceSuiteImpl<
RunJsonTestsForWrapperTypes()2810     MessageType>::RunJsonTestsForWrapperTypes() {
2811   RunValidJsonTest("OptionalBoolWrapper", REQUIRED,
2812                    R"({"optionalBoolWrapper": false})",
2813                    "optional_bool_wrapper: {value: false}");
2814   RunValidJsonTest("OptionalInt32Wrapper", REQUIRED,
2815                    R"({"optionalInt32Wrapper": 0})",
2816                    "optional_int32_wrapper: {value: 0}");
2817   RunValidJsonTest("OptionalUint32Wrapper", REQUIRED,
2818                    R"({"optionalUint32Wrapper": 0})",
2819                    "optional_uint32_wrapper: {value: 0}");
2820   RunValidJsonTest("OptionalInt64Wrapper", REQUIRED,
2821                    R"({"optionalInt64Wrapper": 0})",
2822                    "optional_int64_wrapper: {value: 0}");
2823   RunValidJsonTest("OptionalUint64Wrapper", REQUIRED,
2824                    R"({"optionalUint64Wrapper": 0})",
2825                    "optional_uint64_wrapper: {value: 0}");
2826   RunValidJsonTest("OptionalFloatWrapper", REQUIRED,
2827                    R"({"optionalFloatWrapper": 0})",
2828                    "optional_float_wrapper: {value: 0}");
2829   RunValidJsonTest("OptionalDoubleWrapper", REQUIRED,
2830                    R"({"optionalDoubleWrapper": 0})",
2831                    "optional_double_wrapper: {value: 0}");
2832   RunValidJsonTest("OptionalStringWrapper", REQUIRED,
2833                    R"({"optionalStringWrapper": ""})",
2834                    R"(optional_string_wrapper: {value: ""})");
2835   RunValidJsonTest("OptionalBytesWrapper", REQUIRED,
2836                    R"({"optionalBytesWrapper": ""})",
2837                    R"(optional_bytes_wrapper: {value: ""})");
2838   RunValidJsonTest("OptionalWrapperTypesWithNonDefaultValue", REQUIRED,
2839                    R"({
2840         "optionalBoolWrapper": true,
2841         "optionalInt32Wrapper": 1,
2842         "optionalUint32Wrapper": 1,
2843         "optionalInt64Wrapper": "1",
2844         "optionalUint64Wrapper": "1",
2845         "optionalFloatWrapper": 1,
2846         "optionalDoubleWrapper": 1,
2847         "optionalStringWrapper": "1",
2848         "optionalBytesWrapper": "AQI="
2849       })",
2850                    R"(
2851         optional_bool_wrapper: {value: true}
2852         optional_int32_wrapper: {value: 1}
2853         optional_uint32_wrapper: {value: 1}
2854         optional_int64_wrapper: {value: 1}
2855         optional_uint64_wrapper: {value: 1}
2856         optional_float_wrapper: {value: 1}
2857         optional_double_wrapper: {value: 1}
2858         optional_string_wrapper: {value: "1"}
2859         optional_bytes_wrapper: {value: "\x01\x02"}
2860       )");
2861   RunValidJsonTest("RepeatedBoolWrapper", REQUIRED,
2862                    R"({"repeatedBoolWrapper": [true, false]})",
2863                    "repeated_bool_wrapper: {value: true}"
2864                    "repeated_bool_wrapper: {value: false}");
2865   RunValidJsonTest("RepeatedInt32Wrapper", REQUIRED,
2866                    R"({"repeatedInt32Wrapper": [0, 1]})",
2867                    "repeated_int32_wrapper: {value: 0}"
2868                    "repeated_int32_wrapper: {value: 1}");
2869   RunValidJsonTest("RepeatedUint32Wrapper", REQUIRED,
2870                    R"({"repeatedUint32Wrapper": [0, 1]})",
2871                    "repeated_uint32_wrapper: {value: 0}"
2872                    "repeated_uint32_wrapper: {value: 1}");
2873   RunValidJsonTest("RepeatedInt64Wrapper", REQUIRED,
2874                    R"({"repeatedInt64Wrapper": [0, 1]})",
2875                    "repeated_int64_wrapper: {value: 0}"
2876                    "repeated_int64_wrapper: {value: 1}");
2877   RunValidJsonTest("RepeatedUint64Wrapper", REQUIRED,
2878                    R"({"repeatedUint64Wrapper": [0, 1]})",
2879                    "repeated_uint64_wrapper: {value: 0}"
2880                    "repeated_uint64_wrapper: {value: 1}");
2881   RunValidJsonTest("RepeatedFloatWrapper", REQUIRED,
2882                    R"({"repeatedFloatWrapper": [0, 1]})",
2883                    "repeated_float_wrapper: {value: 0}"
2884                    "repeated_float_wrapper: {value: 1}");
2885   RunValidJsonTest("RepeatedDoubleWrapper", REQUIRED,
2886                    R"({"repeatedDoubleWrapper": [0, 1]})",
2887                    "repeated_double_wrapper: {value: 0}"
2888                    "repeated_double_wrapper: {value: 1}");
2889   RunValidJsonTest("RepeatedStringWrapper", REQUIRED,
2890                    R"({"repeatedStringWrapper": ["", "AQI="]})",
2891                    R"(
2892         repeated_string_wrapper: {value: ""}
2893         repeated_string_wrapper: {value: "AQI="}
2894       )");
2895   RunValidJsonTest("RepeatedBytesWrapper", REQUIRED,
2896                    R"({"repeatedBytesWrapper": ["", "AQI="]})",
2897                    R"(
2898         repeated_bytes_wrapper: {value: ""}
2899         repeated_bytes_wrapper: {value: "\x01\x02"}
2900       )");
2901   RunValidJsonTest("WrapperTypesWithNullValue", REQUIRED,
2902                    R"({
2903         "optionalBoolWrapper": null,
2904         "optionalInt32Wrapper": null,
2905         "optionalUint32Wrapper": null,
2906         "optionalInt64Wrapper": null,
2907         "optionalUint64Wrapper": null,
2908         "optionalFloatWrapper": null,
2909         "optionalDoubleWrapper": null,
2910         "optionalStringWrapper": null,
2911         "optionalBytesWrapper": null,
2912         "repeatedBoolWrapper": null,
2913         "repeatedInt32Wrapper": null,
2914         "repeatedUint32Wrapper": null,
2915         "repeatedInt64Wrapper": null,
2916         "repeatedUint64Wrapper": null,
2917         "repeatedFloatWrapper": null,
2918         "repeatedDoubleWrapper": null,
2919         "repeatedStringWrapper": null,
2920         "repeatedBytesWrapper": null
2921       })",
2922                    "");
2923 
2924   // Duration
2925   RunValidJsonTest(
2926       "DurationMinValue", REQUIRED,
2927       R"({"optionalDuration": "-315576000000.999999999s"})",
2928       "optional_duration: {seconds: -315576000000 nanos: -999999999}");
2929   RunValidJsonTest(
2930       "DurationMaxValue", REQUIRED,
2931       R"({"optionalDuration": "315576000000.999999999s"})",
2932       "optional_duration: {seconds: 315576000000 nanos: 999999999}");
2933   RunValidJsonTest("DurationRepeatedValue", REQUIRED,
2934                    R"({"repeatedDuration": ["1.5s", "-1.5s"]})",
2935                    "repeated_duration: {seconds: 1 nanos: 500000000}"
2936                    "repeated_duration: {seconds: -1 nanos: -500000000}");
2937   RunValidJsonTest("DurationNull", REQUIRED, R"({"optionalDuration": null})",
2938                    "");
2939   RunValidJsonTest("DurationNegativeSeconds", REQUIRED,
2940                    R"({"optionalDuration": "-5s"})",
2941                    "optional_duration: {seconds: -5 nanos: 0}");
2942   RunValidJsonTest("DurationNegativeNanos", REQUIRED,
2943                    R"({"optionalDuration": "-0.5s"})",
2944                    "optional_duration: {seconds: 0 nanos: -500000000}");
2945 
2946   ExpectParseFailureForJson("DurationMissingS", REQUIRED,
2947                             R"({"optionalDuration": "1"})");
2948   ExpectParseFailureForJson(
2949       "DurationJsonInputTooSmall", REQUIRED,
2950       R"({"optionalDuration": "-315576000001.000000000s"})");
2951   ExpectParseFailureForJson(
2952       "DurationJsonInputTooLarge", REQUIRED,
2953       R"({"optionalDuration": "315576000001.000000000s"})");
2954   ExpectSerializeFailureForJson(
2955       "DurationProtoInputTooSmall", REQUIRED,
2956       "optional_duration: {seconds: -315576000001 nanos: 0}");
2957   ExpectSerializeFailureForJson(
2958       "DurationProtoInputTooLarge", REQUIRED,
2959       "optional_duration: {seconds: 315576000001 nanos: 0}");
2960 
2961   RunValidJsonTestWithValidator(
2962       "DurationHasZeroFractionalDigit", RECOMMENDED,
2963       R"({"optionalDuration": "1.000000000s"})", [](const Json::Value& value) {
2964         return value["optionalDuration"].asString() == "1s";
2965       });
2966   RunValidJsonTestWithValidator(
2967       "DurationHas3FractionalDigits", RECOMMENDED,
2968       R"({"optionalDuration": "1.010000000s"})", [](const Json::Value& value) {
2969         return value["optionalDuration"].asString() == "1.010s";
2970       });
2971   RunValidJsonTestWithValidator(
2972       "DurationHas6FractionalDigits", RECOMMENDED,
2973       R"({"optionalDuration": "1.000010000s"})", [](const Json::Value& value) {
2974         return value["optionalDuration"].asString() == "1.000010s";
2975       });
2976   RunValidJsonTestWithValidator(
2977       "DurationHas9FractionalDigits", RECOMMENDED,
2978       R"({"optionalDuration": "1.000000010s"})", [](const Json::Value& value) {
2979         return value["optionalDuration"].asString() == "1.000000010s";
2980       });
2981 
2982   // Timestamp
2983   RunValidJsonTest("TimestampMinValue", REQUIRED,
2984                    R"({"optionalTimestamp": "0001-01-01T00:00:00Z"})",
2985                    "optional_timestamp: {seconds: -62135596800}");
2986   RunValidJsonTest(
2987       "TimestampMaxValue", REQUIRED,
2988       R"({"optionalTimestamp": "9999-12-31T23:59:59.999999999Z"})",
2989       "optional_timestamp: {seconds: 253402300799 nanos: 999999999}");
2990   RunValidJsonTest(
2991       "TimestampRepeatedValue", REQUIRED,
2992       R"({
2993         "repeatedTimestamp": [
2994           "0001-01-01T00:00:00Z",
2995           "9999-12-31T23:59:59.999999999Z"
2996   ]
2997       })",
2998       "repeated_timestamp: {seconds: -62135596800}"
2999       "repeated_timestamp: {seconds: 253402300799 nanos: 999999999}");
3000   RunValidJsonTest("TimestampLeap", REQUIRED,
3001                    R"({"optionalTimestamp": "1993-02-10T00:00:00.000Z"})",
3002                    "optional_timestamp: {seconds: 729302400}");
3003   RunValidJsonTest("TimestampWithPositiveOffset", REQUIRED,
3004                    R"({"optionalTimestamp": "1970-01-01T08:00:01+08:00"})",
3005                    "optional_timestamp: {seconds: 1}");
3006   RunValidJsonTest("TimestampWithNegativeOffset", REQUIRED,
3007                    R"({"optionalTimestamp": "1969-12-31T16:00:01-08:00"})",
3008                    "optional_timestamp: {seconds: 1}");
3009   RunValidJsonTest("TimestampNull", REQUIRED, R"({"optionalTimestamp": null})",
3010                    "");
3011 
3012   ExpectParseFailureForJson("TimestampJsonInputTooSmall", REQUIRED,
3013                             R"({"optionalTimestamp": "0000-01-01T00:00:00Z"})");
3014   ExpectParseFailureForJson(
3015       "TimestampJsonInputTooLarge", REQUIRED,
3016       R"({"optionalTimestamp": "10000-01-01T00:00:00Z"})");
3017   ExpectParseFailureForJson("TimestampJsonInputMissingZ", REQUIRED,
3018                             R"({"optionalTimestamp": "0001-01-01T00:00:00"})");
3019   ExpectParseFailureForJson("TimestampJsonInputMissingT", REQUIRED,
3020                             R"({"optionalTimestamp": "0001-01-01 00:00:00Z"})");
3021   ExpectParseFailureForJson("TimestampJsonInputLowercaseZ", REQUIRED,
3022                             R"({"optionalTimestamp": "0001-01-01T00:00:00z"})");
3023   ExpectParseFailureForJson("TimestampJsonInputLowercaseT", REQUIRED,
3024                             R"({"optionalTimestamp": "0001-01-01t00:00:00Z"})");
3025   ExpectSerializeFailureForJson("TimestampProtoInputTooSmall", REQUIRED,
3026                                 "optional_timestamp: {seconds: -62135596801}");
3027   ExpectSerializeFailureForJson("TimestampProtoInputTooLarge", REQUIRED,
3028                                 "optional_timestamp: {seconds: 253402300800}");
3029   RunValidJsonTestWithValidator(
3030       "TimestampZeroNormalized", RECOMMENDED,
3031       R"({"optionalTimestamp": "1969-12-31T16:00:00-08:00"})",
3032       [](const Json::Value& value) {
3033         return value["optionalTimestamp"].asString() == "1970-01-01T00:00:00Z";
3034       });
3035   RunValidJsonTestWithValidator(
3036       "TimestampHasZeroFractionalDigit", RECOMMENDED,
3037       R"({"optionalTimestamp": "1970-01-01T00:00:00.000000000Z"})",
3038       [](const Json::Value& value) {
3039         return value["optionalTimestamp"].asString() == "1970-01-01T00:00:00Z";
3040       });
3041   RunValidJsonTestWithValidator(
3042       "TimestampHas3FractionalDigits", RECOMMENDED,
3043       R"({"optionalTimestamp": "1970-01-01T00:00:00.010000000Z"})",
3044       [](const Json::Value& value) {
3045         return value["optionalTimestamp"].asString() ==
3046                "1970-01-01T00:00:00.010Z";
3047       });
3048   RunValidJsonTestWithValidator(
3049       "TimestampHas6FractionalDigits", RECOMMENDED,
3050       R"({"optionalTimestamp": "1970-01-01T00:00:00.000010000Z"})",
3051       [](const Json::Value& value) {
3052         return value["optionalTimestamp"].asString() ==
3053                "1970-01-01T00:00:00.000010Z";
3054       });
3055   RunValidJsonTestWithValidator(
3056       "TimestampHas9FractionalDigits", RECOMMENDED,
3057       R"({"optionalTimestamp": "1970-01-01T00:00:00.000000010Z"})",
3058       [](const Json::Value& value) {
3059         return value["optionalTimestamp"].asString() ==
3060                "1970-01-01T00:00:00.000000010Z";
3061       });
3062 }
3063 
3064 template <typename MessageType>
3065 void BinaryAndJsonConformanceSuiteImpl<
RunJsonTestsForFieldMask()3066     MessageType>::RunJsonTestsForFieldMask() {
3067   RunValidJsonTest("FieldMask", REQUIRED,
3068                    R"({"optionalFieldMask": "foo,barBaz"})",
3069                    R"(optional_field_mask: {paths: "foo" paths: "bar_baz"})");
3070   RunValidJsonTest("EmptyFieldMask", REQUIRED, R"({"optionalFieldMask": ""})",
3071                    R"(optional_field_mask: {})");
3072   ExpectParseFailureForJson("FieldMaskInvalidCharacter", RECOMMENDED,
3073                             R"({"optionalFieldMask": "foo,bar_bar"})");
3074   ExpectSerializeFailureForJson("FieldMaskPathsDontRoundTrip", RECOMMENDED,
3075                                 R"(optional_field_mask: {paths: "fooBar"})");
3076   ExpectSerializeFailureForJson("FieldMaskNumbersDontRoundTrip", RECOMMENDED,
3077                                 R"(optional_field_mask: {paths: "foo_3_bar"})");
3078   ExpectSerializeFailureForJson("FieldMaskTooManyUnderscore", RECOMMENDED,
3079                                 R"(optional_field_mask: {paths: "foo__bar"})");
3080 }
3081 
3082 template <typename MessageType>
RunJsonTestsForStruct()3083 void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTestsForStruct() {
3084   RunValidJsonTest("Struct", REQUIRED,
3085                    R"({
3086         "optionalStruct": {
3087           "nullValue": null,
3088           "intValue": 1234,
3089           "boolValue": true,
3090           "doubleValue": 1234.5678,
3091           "stringValue": "Hello world!",
3092           "listValue": [1234, "5678"],
3093           "objectValue": {
3094             "value": 0
3095     }
3096   }
3097       })",
3098                    R"(
3099         optional_struct: {
3100           fields: {
3101             key: "nullValue"
3102             value: {null_value: NULL_VALUE}
3103     }
3104           fields: {
3105             key: "intValue"
3106             value: {number_value: 1234}
3107     }
3108           fields: {
3109             key: "boolValue"
3110             value: {bool_value: true}
3111     }
3112           fields: {
3113             key: "doubleValue"
3114             value: {number_value: 1234.5678}
3115     }
3116           fields: {
3117             key: "stringValue"
3118             value: {string_value: "Hello world!"}
3119     }
3120           fields: {
3121             key: "listValue"
3122             value: {
3123               list_value: {
3124                 values: {
3125                   number_value: 1234
3126           }
3127                 values: {
3128                   string_value: "5678"
3129           }
3130         }
3131       }
3132     }
3133           fields: {
3134             key: "objectValue"
3135             value: {
3136               struct_value: {
3137                 fields: {
3138                   key: "value"
3139                   value: {
3140                     number_value: 0
3141             }
3142           }
3143         }
3144       }
3145     }
3146   }
3147       )");
3148   RunValidJsonTest("StructWithEmptyListValue", REQUIRED,
3149                    R"({
3150         "optionalStruct": {
3151           "listValue": []
3152   }
3153       })",
3154                    R"(
3155         optional_struct: {
3156           fields: {
3157             key: "listValue"
3158             value: {
3159               list_value: {
3160         }
3161       }
3162     }
3163   }
3164       )");
3165 }
3166 
3167 template <typename MessageType>
RunJsonTestsForValue()3168 void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTestsForValue() {
3169   RunValidJsonTest("ValueAcceptInteger", REQUIRED, R"({"optionalValue": 1})",
3170                    "optional_value: { number_value: 1}");
3171   RunValidJsonTest("ValueAcceptFloat", REQUIRED, R"({"optionalValue": 1.5})",
3172                    "optional_value: { number_value: 1.5}");
3173   RunValidJsonTest("ValueAcceptBool", REQUIRED, R"({"optionalValue": false})",
3174                    "optional_value: { bool_value: false}");
3175   RunValidJsonTest("ValueAcceptNull", REQUIRED, R"({"optionalValue": null})",
3176                    "optional_value: { null_value: NULL_VALUE}");
3177   RunValidJsonTest("ValueAcceptString", REQUIRED,
3178                    R"({"optionalValue": "hello"})",
3179                    R"(optional_value: { string_value: "hello"})");
3180   RunValidJsonTest("ValueAcceptList", REQUIRED,
3181                    R"({"optionalValue": [0, "hello"]})",
3182                    R"(
3183         optional_value: {
3184           list_value: {
3185             values: {
3186               number_value: 0
3187       }
3188             values: {
3189               string_value: "hello"
3190       }
3191     }
3192   }
3193       )");
3194   RunValidJsonTest("ValueAcceptObject", REQUIRED,
3195                    R"({"optionalValue": {"value": 1}})",
3196                    R"(
3197         optional_value: {
3198           struct_value: {
3199             fields: {
3200               key: "value"
3201               value: {
3202                 number_value: 1
3203         }
3204       }
3205     }
3206   }
3207       )");
3208   RunValidJsonTest("RepeatedValue", REQUIRED,
3209                    R"({
3210         "repeatedValue": [["a"]]
3211       })",
3212                    R"(
3213         repeated_value: [
3214   {
3215             list_value: {
3216               values: [
3217                 { string_value: "a"}
3218         ]
3219       }
3220     }
3221   ]
3222       )");
3223   RunValidJsonTest("RepeatedListValue", REQUIRED,
3224                    R"({
3225         "repeatedListValue": [["a"]]
3226       })",
3227                    R"(
3228         repeated_list_value: [
3229   {
3230             values: [
3231               { string_value: "a"}
3232       ]
3233     }
3234   ]
3235       )");
3236   RunValidJsonTestWithValidator("NullValueInOtherOneofOldFormat", RECOMMENDED,
3237                                 R"({"oneofNullValue": "NULL_VALUE"})",
3238                                 [](const Json::Value& value) {
3239                                   return (value.isMember("oneofNullValue") &&
3240                                           value["oneofNullValue"].isNull());
3241                                 });
3242   RunValidJsonTestWithValidator("NullValueInOtherOneofNewFormat", RECOMMENDED,
3243                                 R"({"oneofNullValue": null})",
3244                                 [](const Json::Value& value) {
3245                                   return (value.isMember("oneofNullValue") &&
3246                                           value["oneofNullValue"].isNull());
3247                                 });
3248   RunValidJsonTestWithValidator(
3249       "NullValueInNormalMessage", RECOMMENDED, R"({"optionalNullValue": null})",
3250       [](const Json::Value& value) { return value.empty(); });
3251   ExpectSerializeFailureForJson("ValueRejectNanNumberValue", RECOMMENDED,
3252                                 "optional_value: { number_value: nan}");
3253   ExpectSerializeFailureForJson("ValueRejectInfNumberValue", RECOMMENDED,
3254                                 "optional_value: { number_value: inf}");
3255 }
3256 
3257 template <typename MessageType>
RunJsonTestsForAny()3258 void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonTestsForAny() {
3259   std::string type_url = GetTypeUrl(MessageType::GetDescriptor());
3260   RunValidJsonTest("Any", REQUIRED,
3261                    absl::Substitute(R"({
3262         "optionalAny": {
3263           "@type": "$0",
3264           "optionalInt32": 12345
3265   }
3266       })",
3267                                     GetTypeUrl(MessageType::GetDescriptor())),
3268                    absl::Substitute(R"(
3269         optional_any: {
3270           [$0] {
3271             optional_int32: 12345
3272           }
3273         }
3274       )",
3275                                     type_url));
3276   RunValidJsonTest("AnyNested", REQUIRED,
3277                    absl::Substitute(R"({
3278         "optionalAny": {
3279           "@type": "type.googleapis.com/google.protobuf.Any",
3280           "value": {
3281             "@type": "$0",
3282             "optionalInt32": 12345
3283     }
3284   }
3285       })",
3286                                     type_url),
3287                    absl::Substitute(R"(
3288         optional_any: {
3289           [type.googleapis.com/google.protobuf.Any] {
3290             [$0] {
3291               optional_int32: 12345
3292             }
3293           }
3294         }
3295       )",
3296                                     type_url));
3297   // The special "@type" tag is not required to appear first.
3298   RunValidJsonTest("AnyUnorderedTypeTag", REQUIRED,
3299                    absl::Substitute(R"({
3300         "optionalAny": {
3301           "optionalInt32": 12345,
3302           "@type": "$0"
3303         }
3304       })",
3305                                     type_url),
3306                    absl::Substitute(R"(
3307         optional_any: {
3308           [$0] {
3309             optional_int32: 12345
3310           }
3311         }
3312       )",
3313                                     type_url));
3314   // Well-known types in Any.
3315   RunValidJsonTest("AnyWithInt32ValueWrapper", REQUIRED,
3316                    R"({
3317         "optionalAny": {
3318           "@type": "type.googleapis.com/google.protobuf.Int32Value",
3319           "value": 12345
3320   }
3321       })",
3322                    R"(
3323         optional_any: {
3324     [type.googleapis.com/google.protobuf.Int32Value] {
3325             value: 12345
3326     }
3327   }
3328       )");
3329   RunValidJsonTest("AnyWithDuration", REQUIRED,
3330                    R"({
3331         "optionalAny": {
3332           "@type": "type.googleapis.com/google.protobuf.Duration",
3333           "value": "1.5s"
3334   }
3335       })",
3336                    R"(
3337         optional_any: {
3338     [type.googleapis.com/google.protobuf.Duration] {
3339             seconds: 1
3340             nanos: 500000000
3341     }
3342   }
3343       )");
3344   RunValidJsonTest("AnyWithTimestamp", REQUIRED,
3345                    R"({
3346         "optionalAny": {
3347           "@type": "type.googleapis.com/google.protobuf.Timestamp",
3348           "value": "1970-01-01T00:00:00Z"
3349   }
3350       })",
3351                    R"(
3352         optional_any: {
3353     [type.googleapis.com/google.protobuf.Timestamp] {
3354             seconds: 0
3355             nanos: 0
3356     }
3357   }
3358       )");
3359   RunValidJsonTest("AnyWithFieldMask", REQUIRED,
3360                    R"({
3361         "optionalAny": {
3362           "@type": "type.googleapis.com/google.protobuf.FieldMask",
3363           "value": "foo,barBaz"
3364   }
3365       })",
3366                    R"(
3367         optional_any: {
3368     [type.googleapis.com/google.protobuf.FieldMask] {
3369             paths: ["foo", "bar_baz"]
3370     }
3371   }
3372       )");
3373   RunValidJsonTest("AnyWithStruct", REQUIRED,
3374                    R"({
3375         "optionalAny": {
3376           "@type": "type.googleapis.com/google.protobuf.Struct",
3377           "value": {
3378             "foo": 1
3379     }
3380   }
3381       })",
3382                    R"(
3383         optional_any: {
3384     [type.googleapis.com/google.protobuf.Struct] {
3385             fields: {
3386               key: "foo"
3387               value: {
3388                 number_value: 1
3389         }
3390       }
3391     }
3392   }
3393       )");
3394   RunValidJsonTest("AnyWithValueForJsonObject", REQUIRED,
3395                    R"({
3396         "optionalAny": {
3397           "@type": "type.googleapis.com/google.protobuf.Value",
3398           "value": {
3399             "foo": 1
3400     }
3401   }
3402       })",
3403                    R"(
3404         optional_any: {
3405     [type.googleapis.com/google.protobuf.Value] {
3406             struct_value: {
3407               fields: {
3408                 key: "foo"
3409                 value: {
3410                   number_value: 1
3411           }
3412         }
3413       }
3414     }
3415   }
3416       )");
3417   RunValidJsonTest("AnyWithValueForInteger", REQUIRED,
3418                    R"({
3419         "optionalAny": {
3420           "@type": "type.googleapis.com/google.protobuf.Value",
3421           "value": 1
3422   }
3423       })",
3424                    R"(
3425         optional_any: {
3426     [type.googleapis.com/google.protobuf.Value] {
3427             number_value: 1
3428     }
3429   }
3430       )");
3431 }
3432 
3433 template <typename MessageType>
3434 const FieldDescriptor*
GetFieldForType(FieldDescriptor::Type type,bool repeated,Packed packed) const3435 BinaryAndJsonConformanceSuiteImpl<MessageType>::GetFieldForType(
3436     FieldDescriptor::Type type, bool repeated, Packed packed) const {
3437   const Descriptor* d = MessageType::GetDescriptor();
3438   for (int i = 0; i < d->field_count(); i++) {
3439     const FieldDescriptor* f = d->field(i);
3440     if (f->type() == type && f->is_repeated() == repeated) {
3441       if ((packed == Packed::kTrue && !f->is_packed()) ||
3442           (packed == Packed::kFalse && f->is_packed())) {
3443         continue;
3444       }
3445       return f;
3446     }
3447   }
3448 
3449   absl::string_view packed_string = "";
3450   const absl::string_view repeated_string =
3451       repeated ? "Repeated " : "Singular ";
3452   if (packed == Packed::kTrue) {
3453     packed_string = "Packed ";
3454   }
3455   if (packed == Packed::kFalse) {
3456     packed_string = "Unpacked ";
3457   }
3458   ABSL_LOG(FATAL) << "Couldn't find field with type: " << repeated_string
3459                   << packed_string << FieldDescriptor::TypeName(type) << " for "
3460                   << d->full_name();
3461   return nullptr;
3462 }
3463 
3464 template <typename MessageType>
3465 const FieldDescriptor*
GetFieldForMapType(FieldDescriptor::Type key_type,FieldDescriptor::Type value_type) const3466 BinaryAndJsonConformanceSuiteImpl<MessageType>::GetFieldForMapType(
3467     FieldDescriptor::Type key_type, FieldDescriptor::Type value_type) const {
3468   const Descriptor* d = MessageType::GetDescriptor();
3469   for (int i = 0; i < d->field_count(); i++) {
3470     const FieldDescriptor* f = d->field(i);
3471     if (f->is_map()) {
3472       const Descriptor* map_entry = f->message_type();
3473       const FieldDescriptor* key = map_entry->field(0);
3474       const FieldDescriptor* value = map_entry->field(1);
3475       if (key->type() == key_type && value->type() == value_type) {
3476         return f;
3477       }
3478     }
3479   }
3480 
3481   ABSL_LOG(FATAL) << "Couldn't find map field with type: "
3482                   << FieldDescriptor::TypeName(key_type) << " and "
3483                   << FieldDescriptor::TypeName(key_type) << " for "
3484                   << d->full_name();
3485   return nullptr;
3486 }
3487 
3488 template <typename MessageType>
3489 const FieldDescriptor*
GetFieldForOneofType(FieldDescriptor::Type type,bool exclusive) const3490 BinaryAndJsonConformanceSuiteImpl<MessageType>::GetFieldForOneofType(
3491     FieldDescriptor::Type type, bool exclusive) const {
3492   const Descriptor* d = MessageType::GetDescriptor();
3493   for (int i = 0; i < d->field_count(); i++) {
3494     const FieldDescriptor* f = d->field(i);
3495     if (f->containing_oneof() && ((f->type() == type) ^ exclusive)) {
3496       return f;
3497     }
3498   }
3499 
3500   ABSL_LOG(FATAL) << "Couldn't find oneof field with type: "
3501                   << FieldDescriptor::TypeName(type) << " for "
3502                   << d->full_name();
3503   return nullptr;
3504 }
3505 
3506 template <typename MessageType>
SyntaxIdentifier() const3507 std::string BinaryAndJsonConformanceSuiteImpl<MessageType>::SyntaxIdentifier()
3508     const {
3509   if (std::is_same<MessageType, TestAllTypesProto2>::value) {
3510     return "Proto2";
3511   } else if (std::is_same<MessageType, TestAllTypesProto3>::value) {
3512     return "Proto3";
3513   } else if (std::is_same<MessageType, TestAllTypesProto2Editions>::value) {
3514     return "Editions_Proto2";
3515   } else {
3516     return "Editions_Proto3";
3517   }
3518 }
3519 
3520 }  // namespace protobuf
3521 }  // namespace google
3522