• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 #include <stdarg.h>
32 #include <string>
33 #include <fstream>
34 
35 #include "conformance.pb.h"
36 #include "conformance_test.h"
37 #include <google/protobuf/stubs/common.h>
38 #include <google/protobuf/stubs/stringprintf.h>
39 #include <google/protobuf/text_format.h>
40 #include <google/protobuf/util/json_util.h>
41 #include <google/protobuf/util/field_comparator.h>
42 #include <google/protobuf/util/message_differencer.h>
43 #include <google/protobuf/util/type_resolver_util.h>
44 #include <google/protobuf/wire_format_lite.h>
45 
46 #include "third_party/jsoncpp/json.h"
47 
48 using conformance::ConformanceRequest;
49 using conformance::ConformanceResponse;
50 using conformance::TestAllTypes;
51 using conformance::WireFormat;
52 using google::protobuf::Descriptor;
53 using google::protobuf::FieldDescriptor;
54 using google::protobuf::internal::WireFormatLite;
55 using google::protobuf::TextFormat;
56 using google::protobuf::util::DefaultFieldComparator;
57 using google::protobuf::util::JsonToBinaryString;
58 using google::protobuf::util::MessageDifferencer;
59 using google::protobuf::util::NewTypeResolverForDescriptorPool;
60 using google::protobuf::util::Status;
61 using std::string;
62 
63 namespace {
64 
65 static const char kTypeUrlPrefix[] = "type.googleapis.com";
66 
GetTypeUrl(const Descriptor * message)67 static string GetTypeUrl(const Descriptor* message) {
68   return string(kTypeUrlPrefix) + "/" + message->full_name();
69 }
70 
71 /* Routines for building arbitrary protos *************************************/
72 
73 // We would use CodedOutputStream except that we want more freedom to build
74 // arbitrary protos (even invalid ones).
75 
76 const string empty;
77 
cat(const string & a,const string & b,const string & c=empty,const string & d=empty,const string & e=empty,const string & f=empty,const string & g=empty,const string & h=empty,const string & i=empty,const string & j=empty,const string & k=empty,const string & l=empty)78 string cat(const string& a, const string& b,
79            const string& c = empty,
80            const string& d = empty,
81            const string& e = empty,
82            const string& f = empty,
83            const string& g = empty,
84            const string& h = empty,
85            const string& i = empty,
86            const string& j = empty,
87            const string& k = empty,
88            const string& l = empty) {
89   string ret;
90   ret.reserve(a.size() + b.size() + c.size() + d.size() + e.size() + f.size() +
91               g.size() + h.size() + i.size() + j.size() + k.size() + l.size());
92   ret.append(a);
93   ret.append(b);
94   ret.append(c);
95   ret.append(d);
96   ret.append(e);
97   ret.append(f);
98   ret.append(g);
99   ret.append(h);
100   ret.append(i);
101   ret.append(j);
102   ret.append(k);
103   ret.append(l);
104   return ret;
105 }
106 
107 // The maximum number of bytes that it takes to encode a 64-bit varint.
108 #define VARINT_MAX_LEN 10
109 
vencode64(uint64_t val,char * buf)110 size_t vencode64(uint64_t val, char *buf) {
111   if (val == 0) { buf[0] = 0; return 1; }
112   size_t i = 0;
113   while (val) {
114     uint8_t byte = val & 0x7fU;
115     val >>= 7;
116     if (val) byte |= 0x80U;
117     buf[i++] = byte;
118   }
119   return i;
120 }
121 
varint(uint64_t x)122 string varint(uint64_t x) {
123   char buf[VARINT_MAX_LEN];
124   size_t len = vencode64(x, buf);
125   return string(buf, len);
126 }
127 
128 // TODO: proper byte-swapping for big-endian machines.
fixed32(void * data)129 string fixed32(void *data) { return string(static_cast<char*>(data), 4); }
fixed64(void * data)130 string fixed64(void *data) { return string(static_cast<char*>(data), 8); }
131 
delim(const string & buf)132 string delim(const string& buf) { return cat(varint(buf.size()), buf); }
uint32(uint32_t u32)133 string uint32(uint32_t u32) { return fixed32(&u32); }
uint64(uint64_t u64)134 string uint64(uint64_t u64) { return fixed64(&u64); }
flt(float f)135 string flt(float f) { return fixed32(&f); }
dbl(double d)136 string dbl(double d) { return fixed64(&d); }
zz32(int32_t x)137 string zz32(int32_t x) { return varint(WireFormatLite::ZigZagEncode32(x)); }
zz64(int64_t x)138 string zz64(int64_t x) { return varint(WireFormatLite::ZigZagEncode64(x)); }
139 
tag(uint32_t fieldnum,char wire_type)140 string tag(uint32_t fieldnum, char wire_type) {
141   return varint((fieldnum << 3) | wire_type);
142 }
143 
submsg(uint32_t fn,const string & buf)144 string submsg(uint32_t fn, const string& buf) {
145   return cat( tag(fn, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), delim(buf) );
146 }
147 
148 #define UNKNOWN_FIELD 666
149 
GetFieldNumberForType(FieldDescriptor::Type type,bool repeated)150 uint32_t GetFieldNumberForType(FieldDescriptor::Type type, bool repeated) {
151   const Descriptor* d = TestAllTypes().GetDescriptor();
152   for (int i = 0; i < d->field_count(); i++) {
153     const FieldDescriptor* f = d->field(i);
154     if (f->type() == type && f->is_repeated() == repeated) {
155       return f->number();
156     }
157   }
158   GOOGLE_LOG(FATAL) << "Couldn't find field with type " << (int)type;
159   return 0;
160 }
161 
UpperCase(string str)162 string UpperCase(string str) {
163   for (int i = 0; i < str.size(); i++) {
164     str[i] = toupper(str[i]);
165   }
166   return str;
167 }
168 
169 }  // anonymous namespace
170 
171 namespace google {
172 namespace protobuf {
173 
ReportSuccess(const string & test_name)174 void ConformanceTestSuite::ReportSuccess(const string& test_name) {
175   if (expected_to_fail_.erase(test_name) != 0) {
176     StringAppendF(&output_,
177                   "ERROR: test %s is in the failure list, but test succeeded.  "
178                   "Remove it from the failure list.\n",
179                   test_name.c_str());
180     unexpected_succeeding_tests_.insert(test_name);
181   }
182   successes_++;
183 }
184 
ReportFailure(const string & test_name,const ConformanceRequest & request,const ConformanceResponse & response,const char * fmt,...)185 void ConformanceTestSuite::ReportFailure(const string& test_name,
186                                          const ConformanceRequest& request,
187                                          const ConformanceResponse& response,
188                                          const char* fmt, ...) {
189   if (expected_to_fail_.erase(test_name) == 1) {
190     expected_failures_++;
191     if (!verbose_)
192       return;
193   } else {
194     StringAppendF(&output_, "ERROR, test=%s: ", test_name.c_str());
195     unexpected_failing_tests_.insert(test_name);
196   }
197   va_list args;
198   va_start(args, fmt);
199   StringAppendV(&output_, fmt, args);
200   va_end(args);
201   StringAppendF(&output_, " request=%s, response=%s\n",
202                 request.ShortDebugString().c_str(),
203                 response.ShortDebugString().c_str());
204 }
205 
ReportSkip(const string & test_name,const ConformanceRequest & request,const ConformanceResponse & response)206 void ConformanceTestSuite::ReportSkip(const string& test_name,
207                                       const ConformanceRequest& request,
208                                       const ConformanceResponse& response) {
209   if (verbose_) {
210     StringAppendF(&output_, "SKIPPED, test=%s request=%s, response=%s\n",
211                   test_name.c_str(), request.ShortDebugString().c_str(),
212                   response.ShortDebugString().c_str());
213   }
214   skipped_.insert(test_name);
215 }
216 
RunTest(const string & test_name,const ConformanceRequest & request,ConformanceResponse * response)217 void ConformanceTestSuite::RunTest(const string& test_name,
218                                    const ConformanceRequest& request,
219                                    ConformanceResponse* response) {
220   if (test_names_.insert(test_name).second == false) {
221     GOOGLE_LOG(FATAL) << "Duplicated test name: " << test_name;
222   }
223 
224   string serialized_request;
225   string serialized_response;
226   request.SerializeToString(&serialized_request);
227 
228   runner_->RunTest(test_name, serialized_request, &serialized_response);
229 
230   if (!response->ParseFromString(serialized_response)) {
231     response->Clear();
232     response->set_runtime_error("response proto could not be parsed.");
233   }
234 
235   if (verbose_) {
236     StringAppendF(&output_, "conformance test: name=%s, request=%s, response=%s\n",
237                   test_name.c_str(),
238                   request.ShortDebugString().c_str(),
239                   response->ShortDebugString().c_str());
240   }
241 }
242 
RunValidInputTest(const string & test_name,const string & input,WireFormat input_format,const string & equivalent_text_format,WireFormat requested_output)243 void ConformanceTestSuite::RunValidInputTest(
244     const string& test_name, const string& input, WireFormat input_format,
245     const string& equivalent_text_format, WireFormat requested_output) {
246   TestAllTypes reference_message;
247   GOOGLE_CHECK(
248       TextFormat::ParseFromString(equivalent_text_format, &reference_message))
249           << "Failed to parse data for test case: " << test_name
250           << ", data: " << equivalent_text_format;
251 
252   ConformanceRequest request;
253   ConformanceResponse response;
254 
255   switch (input_format) {
256     case conformance::PROTOBUF:
257       request.set_protobuf_payload(input);
258       break;
259 
260     case conformance::JSON:
261       request.set_json_payload(input);
262       break;
263 
264     default:
265       GOOGLE_LOG(FATAL) << "Unspecified input format";
266   }
267 
268   request.set_requested_output_format(requested_output);
269 
270   RunTest(test_name, request, &response);
271 
272   TestAllTypes test_message;
273 
274   switch (response.result_case()) {
275     case ConformanceResponse::kParseError:
276     case ConformanceResponse::kRuntimeError:
277     case ConformanceResponse::kSerializeError:
278       ReportFailure(test_name, request, response,
279                     "Failed to parse JSON input or produce JSON output.");
280       return;
281 
282     case ConformanceResponse::kSkipped:
283       ReportSkip(test_name, request, response);
284       return;
285 
286     case ConformanceResponse::kJsonPayload: {
287       if (requested_output != conformance::JSON) {
288         ReportFailure(
289             test_name, request, response,
290             "Test was asked for protobuf output but provided JSON instead.");
291         return;
292       }
293       string binary_protobuf;
294       Status status =
295           JsonToBinaryString(type_resolver_.get(), type_url_,
296                              response.json_payload(), &binary_protobuf);
297       if (!status.ok()) {
298         ReportFailure(test_name, request, response,
299                       "JSON output we received from test was unparseable.");
300         return;
301       }
302 
303       if (!test_message.ParseFromString(binary_protobuf)) {
304         ReportFailure(test_name, request, response,
305                       "INTERNAL ERROR: internal JSON->protobuf transcode "
306                       "yielded unparseable proto.");
307         return;
308       }
309 
310       break;
311     }
312 
313     case ConformanceResponse::kProtobufPayload: {
314       if (requested_output != conformance::PROTOBUF) {
315         ReportFailure(
316             test_name, request, response,
317             "Test was asked for JSON output but provided protobuf instead.");
318         return;
319       }
320 
321       if (!test_message.ParseFromString(response.protobuf_payload())) {
322         ReportFailure(test_name, request, response,
323                       "Protobuf output we received from test was unparseable.");
324         return;
325       }
326 
327       break;
328     }
329 
330     default:
331       GOOGLE_LOG(FATAL) << test_name << ": unknown payload type: "
332                         << response.result_case();
333   }
334 
335   MessageDifferencer differencer;
336   DefaultFieldComparator field_comparator;
337   field_comparator.set_treat_nan_as_equal(true);
338   differencer.set_field_comparator(&field_comparator);
339   string differences;
340   differencer.ReportDifferencesToString(&differences);
341 
342   if (differencer.Compare(reference_message, test_message)) {
343     ReportSuccess(test_name);
344   } else {
345     ReportFailure(test_name, request, response,
346                   "Output was not equivalent to reference message: %s.",
347                   differences.c_str());
348   }
349 }
350 
351 // Expect that this precise protobuf will cause a parse error.
ExpectParseFailureForProto(const string & proto,const string & test_name)352 void ConformanceTestSuite::ExpectParseFailureForProto(
353     const string& proto, const string& test_name) {
354   ConformanceRequest request;
355   ConformanceResponse response;
356   request.set_protobuf_payload(proto);
357   string effective_test_name = "ProtobufInput." + test_name;
358 
359   // We don't expect output, but if the program erroneously accepts the protobuf
360   // we let it send its response as this.  We must not leave it unspecified.
361   request.set_requested_output_format(conformance::PROTOBUF);
362 
363   RunTest(effective_test_name, request, &response);
364   if (response.result_case() == ConformanceResponse::kParseError) {
365     ReportSuccess(effective_test_name);
366   } else if (response.result_case() == ConformanceResponse::kSkipped) {
367     ReportSkip(effective_test_name, request, response);
368   } else {
369     ReportFailure(effective_test_name, request, response,
370                   "Should have failed to parse, but didn't.");
371   }
372 }
373 
374 // Expect that this protobuf will cause a parse error, even if it is followed
375 // by valid protobuf data.  We can try running this twice: once with this
376 // data verbatim and once with this data followed by some valid data.
377 //
378 // TODO(haberman): implement the second of these.
ExpectHardParseFailureForProto(const string & proto,const string & test_name)379 void ConformanceTestSuite::ExpectHardParseFailureForProto(
380     const string& proto, const string& test_name) {
381   return ExpectParseFailureForProto(proto, test_name);
382 }
383 
RunValidJsonTest(const string & test_name,const string & input_json,const string & equivalent_text_format)384 void ConformanceTestSuite::RunValidJsonTest(
385     const string& test_name, const string& input_json,
386     const string& equivalent_text_format) {
387   RunValidInputTest("JsonInput." + test_name + ".ProtobufOutput", input_json,
388                     conformance::JSON, equivalent_text_format,
389                     conformance::PROTOBUF);
390   RunValidInputTest("JsonInput." + test_name + ".JsonOutput", input_json,
391                     conformance::JSON, equivalent_text_format,
392                     conformance::JSON);
393 }
394 
RunValidJsonTestWithProtobufInput(const string & test_name,const TestAllTypes & input,const string & equivalent_text_format)395 void ConformanceTestSuite::RunValidJsonTestWithProtobufInput(
396     const string& test_name, const TestAllTypes& input,
397     const string& equivalent_text_format) {
398   RunValidInputTest("ProtobufInput." + test_name + ".JsonOutput",
399                     input.SerializeAsString(), conformance::PROTOBUF,
400                     equivalent_text_format, conformance::JSON);
401 }
402 
403 // According to proto3 JSON specification, JSON serializers follow more strict
404 // rules than parsers (e.g., a serializer must serialize int32 values as JSON
405 // numbers while the parser is allowed to accept them as JSON strings). This
406 // method allows strict checking on a proto3 JSON serializer by inspecting
407 // the JSON output directly.
RunValidJsonTestWithValidator(const string & test_name,const string & input_json,const Validator & validator)408 void ConformanceTestSuite::RunValidJsonTestWithValidator(
409     const string& test_name, const string& input_json,
410     const Validator& validator) {
411   ConformanceRequest request;
412   ConformanceResponse response;
413   request.set_json_payload(input_json);
414   request.set_requested_output_format(conformance::JSON);
415 
416   string effective_test_name = "JsonInput." + test_name + ".Validator";
417 
418   RunTest(effective_test_name, request, &response);
419 
420   if (response.result_case() == ConformanceResponse::kSkipped) {
421     ReportSkip(effective_test_name, request, response);
422     return;
423   }
424 
425   if (response.result_case() != ConformanceResponse::kJsonPayload) {
426     ReportFailure(effective_test_name, request, response,
427                   "Expected JSON payload but got type %d.",
428                   response.result_case());
429     return;
430   }
431   Json::Reader reader;
432   Json::Value value;
433   if (!reader.parse(response.json_payload(), value)) {
434     ReportFailure(effective_test_name, request, response,
435                   "JSON payload cannot be parsed as valid JSON: %s",
436                   reader.getFormattedErrorMessages().c_str());
437     return;
438   }
439   if (!validator(value)) {
440     ReportFailure(effective_test_name, request, response,
441                   "JSON payload validation failed.");
442     return;
443   }
444   ReportSuccess(effective_test_name);
445 }
446 
ExpectParseFailureForJson(const string & test_name,const string & input_json)447 void ConformanceTestSuite::ExpectParseFailureForJson(
448     const string& test_name, const string& input_json) {
449   ConformanceRequest request;
450   ConformanceResponse response;
451   request.set_json_payload(input_json);
452   string effective_test_name = "JsonInput." + test_name;
453 
454   // We don't expect output, but if the program erroneously accepts the protobuf
455   // we let it send its response as this.  We must not leave it unspecified.
456   request.set_requested_output_format(conformance::JSON);
457 
458   RunTest(effective_test_name, request, &response);
459   if (response.result_case() == ConformanceResponse::kParseError) {
460     ReportSuccess(effective_test_name);
461   } else if (response.result_case() == ConformanceResponse::kSkipped) {
462     ReportSkip(effective_test_name, request, response);
463   } else {
464     ReportFailure(effective_test_name, request, response,
465                   "Should have failed to parse, but didn't.");
466   }
467 }
468 
ExpectSerializeFailureForJson(const string & test_name,const string & text_format)469 void ConformanceTestSuite::ExpectSerializeFailureForJson(
470     const string& test_name, const string& text_format) {
471   TestAllTypes payload_message;
472   GOOGLE_CHECK(
473       TextFormat::ParseFromString(text_format, &payload_message))
474           << "Failed to parse: " << text_format;
475 
476   ConformanceRequest request;
477   ConformanceResponse response;
478   request.set_protobuf_payload(payload_message.SerializeAsString());
479   string effective_test_name = test_name + ".JsonOutput";
480   request.set_requested_output_format(conformance::JSON);
481 
482   RunTest(effective_test_name, request, &response);
483   if (response.result_case() == ConformanceResponse::kSerializeError) {
484     ReportSuccess(effective_test_name);
485   } else if (response.result_case() == ConformanceResponse::kSkipped) {
486     ReportSkip(effective_test_name, request, response);
487   } else {
488     ReportFailure(effective_test_name, request, response,
489                   "Should have failed to serialize, but didn't.");
490   }
491 }
492 
TestPrematureEOFForType(FieldDescriptor::Type type)493 void ConformanceTestSuite::TestPrematureEOFForType(FieldDescriptor::Type type) {
494   // Incomplete values for each wire type.
495   static const string incompletes[6] = {
496     string("\x80"),     // VARINT
497     string("abcdefg"),  // 64BIT
498     string("\x80"),     // DELIMITED (partial length)
499     string(),           // START_GROUP (no value required)
500     string(),           // END_GROUP (no value required)
501     string("abc")       // 32BIT
502   };
503 
504   uint32_t fieldnum = GetFieldNumberForType(type, false);
505   uint32_t rep_fieldnum = GetFieldNumberForType(type, true);
506   WireFormatLite::WireType wire_type = WireFormatLite::WireTypeForFieldType(
507       static_cast<WireFormatLite::FieldType>(type));
508   const string& incomplete = incompletes[wire_type];
509   const string type_name =
510       UpperCase(string(".") + FieldDescriptor::TypeName(type));
511 
512   ExpectParseFailureForProto(
513       tag(fieldnum, wire_type),
514       "PrematureEofBeforeKnownNonRepeatedValue" + type_name);
515 
516   ExpectParseFailureForProto(
517       tag(rep_fieldnum, wire_type),
518       "PrematureEofBeforeKnownRepeatedValue" + type_name);
519 
520   ExpectParseFailureForProto(
521       tag(UNKNOWN_FIELD, wire_type),
522       "PrematureEofBeforeUnknownValue" + type_name);
523 
524   ExpectParseFailureForProto(
525       cat( tag(fieldnum, wire_type), incomplete ),
526       "PrematureEofInsideKnownNonRepeatedValue" + type_name);
527 
528   ExpectParseFailureForProto(
529       cat( tag(rep_fieldnum, wire_type), incomplete ),
530       "PrematureEofInsideKnownRepeatedValue" + type_name);
531 
532   ExpectParseFailureForProto(
533       cat( tag(UNKNOWN_FIELD, wire_type), incomplete ),
534       "PrematureEofInsideUnknownValue" + type_name);
535 
536   if (wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
537     ExpectParseFailureForProto(
538         cat( tag(fieldnum, wire_type), varint(1) ),
539         "PrematureEofInDelimitedDataForKnownNonRepeatedValue" + type_name);
540 
541     ExpectParseFailureForProto(
542         cat( tag(rep_fieldnum, wire_type), varint(1) ),
543         "PrematureEofInDelimitedDataForKnownRepeatedValue" + type_name);
544 
545     // EOF in the middle of delimited data for unknown value.
546     ExpectParseFailureForProto(
547         cat( tag(UNKNOWN_FIELD, wire_type), varint(1) ),
548         "PrematureEofInDelimitedDataForUnknownValue" + type_name);
549 
550     if (type == FieldDescriptor::TYPE_MESSAGE) {
551       // Submessage ends in the middle of a value.
552       string incomplete_submsg =
553           cat( tag(WireFormatLite::TYPE_INT32, WireFormatLite::WIRETYPE_VARINT),
554                 incompletes[WireFormatLite::WIRETYPE_VARINT] );
555       ExpectHardParseFailureForProto(
556           cat( tag(fieldnum, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
557                varint(incomplete_submsg.size()),
558                incomplete_submsg ),
559           "PrematureEofInSubmessageValue" + type_name);
560     }
561   } else if (type != FieldDescriptor::TYPE_GROUP) {
562     // Non-delimited, non-group: eligible for packing.
563 
564     // Packed region ends in the middle of a value.
565     ExpectHardParseFailureForProto(
566         cat( tag(rep_fieldnum, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
567              varint(incomplete.size()),
568              incomplete ),
569         "PrematureEofInPackedFieldValue" + type_name);
570 
571     // EOF in the middle of packed region.
572     ExpectParseFailureForProto(
573         cat( tag(rep_fieldnum, WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
574              varint(1) ),
575         "PrematureEofInPackedField" + type_name);
576   }
577 }
578 
SetFailureList(const string & filename,const vector<string> & failure_list)579 void ConformanceTestSuite::SetFailureList(const string& filename,
580                                           const vector<string>& failure_list) {
581   failure_list_filename_ = filename;
582   expected_to_fail_.clear();
583   std::copy(failure_list.begin(), failure_list.end(),
584             std::inserter(expected_to_fail_, expected_to_fail_.end()));
585 }
586 
CheckSetEmpty(const set<string> & set_to_check,const std::string & write_to_file,const std::string & msg)587 bool ConformanceTestSuite::CheckSetEmpty(const set<string>& set_to_check,
588                                          const std::string& write_to_file,
589                                          const std::string& msg) {
590   if (set_to_check.empty()) {
591     return true;
592   } else {
593     StringAppendF(&output_, "\n");
594     StringAppendF(&output_, "%s\n\n", msg.c_str());
595     for (set<string>::const_iterator iter = set_to_check.begin();
596          iter != set_to_check.end(); ++iter) {
597       StringAppendF(&output_, "  %s\n", iter->c_str());
598     }
599     StringAppendF(&output_, "\n");
600 
601     if (!write_to_file.empty()) {
602       std::ofstream os(write_to_file);
603       if (os) {
604         for (set<string>::const_iterator iter = set_to_check.begin();
605              iter != set_to_check.end(); ++iter) {
606           os << *iter << "\n";
607         }
608       } else {
609         StringAppendF(&output_, "Failed to open file: %s\n",
610                       write_to_file.c_str());
611       }
612     }
613 
614     return false;
615   }
616 }
617 
RunSuite(ConformanceTestRunner * runner,std::string * output)618 bool ConformanceTestSuite::RunSuite(ConformanceTestRunner* runner,
619                                     std::string* output) {
620   runner_ = runner;
621   successes_ = 0;
622   expected_failures_ = 0;
623   skipped_.clear();
624   test_names_.clear();
625   unexpected_failing_tests_.clear();
626   unexpected_succeeding_tests_.clear();
627   type_resolver_.reset(NewTypeResolverForDescriptorPool(
628       kTypeUrlPrefix, DescriptorPool::generated_pool()));
629   type_url_ = GetTypeUrl(TestAllTypes::descriptor());
630 
631   output_ = "\nCONFORMANCE TEST BEGIN ====================================\n\n";
632 
633   for (int i = 1; i <= FieldDescriptor::MAX_TYPE; i++) {
634     if (i == FieldDescriptor::TYPE_GROUP) continue;
635     TestPrematureEOFForType(static_cast<FieldDescriptor::Type>(i));
636   }
637 
638   RunValidJsonTest("HelloWorld", "{\"optionalString\":\"Hello, World!\"}",
639                    "optional_string: 'Hello, World!'");
640 
641   // Test field name conventions.
642   RunValidJsonTest(
643       "FieldNameInSnakeCase",
644       R"({
645         "fieldname1": 1,
646         "fieldName2": 2,
647         "FieldName3": 3
648       })",
649       R"(
650         fieldname1: 1
651         field_name2: 2
652         _field_name3: 3
653       )");
654   RunValidJsonTest(
655       "FieldNameWithNumbers",
656       R"({
657         "field0name5": 5,
658         "field0Name6": 6
659       })",
660       R"(
661         field0name5: 5
662         field_0_name6: 6
663       )");
664   RunValidJsonTest(
665       "FieldNameWithMixedCases",
666       R"({
667         "fieldName7": 7,
668         "fieldName8": 8,
669         "fieldName9": 9,
670         "fieldName10": 10,
671         "fIELDNAME11": 11,
672         "fIELDName12": 12
673       })",
674       R"(
675         fieldName7: 7
676         FieldName8: 8
677         field_Name9: 9
678         Field_Name10: 10
679         FIELD_NAME11: 11
680         FIELD_name12: 12
681       )");
682   // Using the original proto field name in JSON is also allowed.
683   RunValidJsonTest(
684       "OriginalProtoFieldName",
685       R"({
686         "fieldname1": 1,
687         "field_name2": 2,
688         "_field_name3": 3,
689         "field0name5": 5,
690         "field_0_name6": 6,
691         "fieldName7": 7,
692         "FieldName8": 8,
693         "field_Name9": 9,
694         "Field_Name10": 10,
695         "FIELD_NAME11": 11,
696         "FIELD_name12": 12
697       })",
698       R"(
699         fieldname1: 1
700         field_name2: 2
701         _field_name3: 3
702         field0name5: 5
703         field_0_name6: 6
704         fieldName7: 7
705         FieldName8: 8
706         field_Name9: 9
707         Field_Name10: 10
708         FIELD_NAME11: 11
709         FIELD_name12: 12
710       )");
711   // Field names can be escaped.
712   RunValidJsonTest(
713       "FieldNameEscaped",
714       R"({"fieldn\u0061me1": 1})",
715       "fieldname1: 1");
716   // Field names must be quoted (or it's not valid JSON).
717   ExpectParseFailureForJson(
718       "FieldNameNotQuoted",
719       "{fieldname1: 1}");
720   // Trailing comma is not allowed (not valid JSON).
721   ExpectParseFailureForJson(
722       "TrailingCommaInAnObject",
723       R"({"fieldname1":1,})");
724   // JSON doesn't support comments.
725   ExpectParseFailureForJson(
726       "JsonWithComments",
727       R"({
728         // This is a comment.
729         "fieldname1": 1
730       })");
731   // Duplicated field names are not allowed.
732   ExpectParseFailureForJson(
733       "FieldNameDuplicate",
734       R"({
735         "optionalNestedMessage": {a: 1},
736         "optionalNestedMessage": {}
737       })");
738   ExpectParseFailureForJson(
739       "FieldNameDuplicateDifferentCasing1",
740       R"({
741         "optional_nested_message": {a: 1},
742         "optionalNestedMessage": {}
743       })");
744   ExpectParseFailureForJson(
745       "FieldNameDuplicateDifferentCasing2",
746       R"({
747         "optionalNestedMessage": {a: 1},
748         "optional_nested_message": {}
749       })");
750   // Serializers should use lowerCamelCase by default.
751   RunValidJsonTestWithValidator(
752       "FieldNameInLowerCamelCase",
753       R"({
754         "fieldname1": 1,
755         "fieldName2": 2,
756         "FieldName3": 3
757       })",
758       [](const Json::Value& value) {
759         return value.isMember("fieldname1") &&
760             value.isMember("fieldName2") &&
761             value.isMember("FieldName3");
762       });
763   RunValidJsonTestWithValidator(
764       "FieldNameWithNumbers",
765       R"({
766         "field0name5": 5,
767         "field0Name6": 6
768       })",
769       [](const Json::Value& value) {
770         return value.isMember("field0name5") &&
771             value.isMember("field0Name6");
772       });
773   RunValidJsonTestWithValidator(
774       "FieldNameWithMixedCases",
775       R"({
776         "fieldName7": 7,
777         "fieldName8": 8,
778         "fieldName9": 9,
779         "fieldName10": 10,
780         "fIELDNAME11": 11,
781         "fIELDName12": 12
782       })",
783       [](const Json::Value& value) {
784         return value.isMember("fieldName7") &&
785             value.isMember("fieldName8") &&
786             value.isMember("fieldName9") &&
787             value.isMember("fieldName10") &&
788             value.isMember("fIELDNAME11") &&
789             value.isMember("fIELDName12");
790       });
791 
792   // Integer fields.
793   RunValidJsonTest(
794       "Int32FieldMaxValue",
795       R"({"optionalInt32": 2147483647})",
796       "optional_int32: 2147483647");
797   RunValidJsonTest(
798       "Int32FieldMinValue",
799       R"({"optionalInt32": -2147483648})",
800       "optional_int32: -2147483648");
801   RunValidJsonTest(
802       "Uint32FieldMaxValue",
803       R"({"optionalUint32": 4294967295})",
804       "optional_uint32: 4294967295");
805   RunValidJsonTest(
806       "Int64FieldMaxValue",
807       R"({"optionalInt64": "9223372036854775807"})",
808       "optional_int64: 9223372036854775807");
809   RunValidJsonTest(
810       "Int64FieldMinValue",
811       R"({"optionalInt64": "-9223372036854775808"})",
812       "optional_int64: -9223372036854775808");
813   RunValidJsonTest(
814       "Uint64FieldMaxValue",
815       R"({"optionalUint64": "18446744073709551615"})",
816       "optional_uint64: 18446744073709551615");
817   RunValidJsonTest(
818       "Int64FieldMaxValueNotQuoted",
819       R"({"optionalInt64": 9223372036854775807})",
820       "optional_int64: 9223372036854775807");
821   RunValidJsonTest(
822       "Int64FieldMinValueNotQuoted",
823       R"({"optionalInt64": -9223372036854775808})",
824       "optional_int64: -9223372036854775808");
825   RunValidJsonTest(
826       "Uint64FieldMaxValueNotQuoted",
827       R"({"optionalUint64": 18446744073709551615})",
828       "optional_uint64: 18446744073709551615");
829   // Values can be represented as JSON strings.
830   RunValidJsonTest(
831       "Int32FieldStringValue",
832       R"({"optionalInt32": "2147483647"})",
833       "optional_int32: 2147483647");
834   RunValidJsonTest(
835       "Int32FieldStringValueEscaped",
836       R"({"optionalInt32": "2\u003147483647"})",
837       "optional_int32: 2147483647");
838 
839   // Parsers reject out-of-bound integer values.
840   ExpectParseFailureForJson(
841       "Int32FieldTooLarge",
842       R"({"optionalInt32": 2147483648})");
843   ExpectParseFailureForJson(
844       "Int32FieldTooSmall",
845       R"({"optionalInt32": -2147483649})");
846   ExpectParseFailureForJson(
847       "Uint32FieldTooLarge",
848       R"({"optionalUint32": 4294967296})");
849   ExpectParseFailureForJson(
850       "Int64FieldTooLarge",
851       R"({"optionalInt64": "9223372036854775808"})");
852   ExpectParseFailureForJson(
853       "Int64FieldTooSmall",
854       R"({"optionalInt64": "-9223372036854775809"})");
855   ExpectParseFailureForJson(
856       "Uint64FieldTooLarge",
857       R"({"optionalUint64": "18446744073709551616"})");
858   // Parser reject non-integer numeric values as well.
859   ExpectParseFailureForJson(
860       "Int32FieldNotInteger",
861       R"({"optionalInt32": 0.5})");
862   ExpectParseFailureForJson(
863       "Uint32FieldNotInteger",
864       R"({"optionalUint32": 0.5})");
865   ExpectParseFailureForJson(
866       "Int64FieldNotInteger",
867       R"({"optionalInt64": "0.5"})");
868   ExpectParseFailureForJson(
869       "Uint64FieldNotInteger",
870       R"({"optionalUint64": "0.5"})");
871 
872   // Integers but represented as float values are accepted.
873   RunValidJsonTest(
874       "Int32FieldFloatTrailingZero",
875       R"({"optionalInt32": 100000.000})",
876       "optional_int32: 100000");
877   RunValidJsonTest(
878       "Int32FieldExponentialFormat",
879       R"({"optionalInt32": 1e5})",
880       "optional_int32: 100000");
881   RunValidJsonTest(
882       "Int32FieldMaxFloatValue",
883       R"({"optionalInt32": 2.147483647e9})",
884       "optional_int32: 2147483647");
885   RunValidJsonTest(
886       "Int32FieldMinFloatValue",
887       R"({"optionalInt32": -2.147483648e9})",
888       "optional_int32: -2147483648");
889   RunValidJsonTest(
890       "Uint32FieldMaxFloatValue",
891       R"({"optionalUint32": 4.294967295e9})",
892       "optional_uint32: 4294967295");
893 
894   // Parser reject non-numeric values.
895   ExpectParseFailureForJson(
896       "Int32FieldNotNumber",
897       R"({"optionalInt32": "3x3"})");
898   ExpectParseFailureForJson(
899       "Uint32FieldNotNumber",
900       R"({"optionalUint32": "3x3"})");
901   ExpectParseFailureForJson(
902       "Int64FieldNotNumber",
903       R"({"optionalInt64": "3x3"})");
904   ExpectParseFailureForJson(
905       "Uint64FieldNotNumber",
906       R"({"optionalUint64": "3x3"})");
907   // JSON does not allow "+" on numric values.
908   ExpectParseFailureForJson(
909       "Int32FieldPlusSign",
910       R"({"optionalInt32": +1})");
911   // JSON doesn't allow leading 0s.
912   ExpectParseFailureForJson(
913       "Int32FieldLeadingZero",
914       R"({"optionalInt32": 01})");
915   ExpectParseFailureForJson(
916       "Int32FieldNegativeWithLeadingZero",
917       R"({"optionalInt32": -01})");
918   // String values must follow the same syntax rule. Specifically leading
919   // or traling spaces are not allowed.
920   ExpectParseFailureForJson(
921       "Int32FieldLeadingSpace",
922       R"({"optionalInt32": " 1"})");
923   ExpectParseFailureForJson(
924       "Int32FieldTrailingSpace",
925       R"({"optionalInt32": "1 "})");
926 
927   // 64-bit values are serialized as strings.
928   RunValidJsonTestWithValidator(
929       "Int64FieldBeString",
930       R"({"optionalInt64": 1})",
931       [](const Json::Value& value) {
932         return value["optionalInt64"].type() == Json::stringValue &&
933             value["optionalInt64"].asString() == "1";
934       });
935   RunValidJsonTestWithValidator(
936       "Uint64FieldBeString",
937       R"({"optionalUint64": 1})",
938       [](const Json::Value& value) {
939         return value["optionalUint64"].type() == Json::stringValue &&
940             value["optionalUint64"].asString() == "1";
941       });
942 
943   // Bool fields.
944   RunValidJsonTest(
945       "BoolFieldTrue",
946       R"({"optionalBool":true})",
947       "optional_bool: true");
948   RunValidJsonTest(
949       "BoolFieldFalse",
950       R"({"optionalBool":false})",
951       "optional_bool: false");
952 
953   // Other forms are not allowed.
954   ExpectParseFailureForJson(
955       "BoolFieldIntegerZero",
956       R"({"optionalBool":0})");
957   ExpectParseFailureForJson(
958       "BoolFieldIntegerOne",
959       R"({"optionalBool":1})");
960   ExpectParseFailureForJson(
961       "BoolFieldCamelCaseTrue",
962       R"({"optionalBool":True})");
963   ExpectParseFailureForJson(
964       "BoolFieldCamelCaseFalse",
965       R"({"optionalBool":False})");
966   ExpectParseFailureForJson(
967       "BoolFieldAllCapitalTrue",
968       R"({"optionalBool":TRUE})");
969   ExpectParseFailureForJson(
970       "BoolFieldAllCapitalFalse",
971       R"({"optionalBool":FALSE})");
972   ExpectParseFailureForJson(
973       "BoolFieldDoubleQuotedTrue",
974       R"({"optionalBool":"true"})");
975   ExpectParseFailureForJson(
976       "BoolFieldDoubleQuotedFalse",
977       R"({"optionalBool":"false"})");
978 
979   // Float fields.
980   RunValidJsonTest(
981       "FloatFieldMinPositiveValue",
982       R"({"optionalFloat": 1.175494e-38})",
983       "optional_float: 1.175494e-38");
984   RunValidJsonTest(
985       "FloatFieldMaxNegativeValue",
986       R"({"optionalFloat": -1.175494e-38})",
987       "optional_float: -1.175494e-38");
988   RunValidJsonTest(
989       "FloatFieldMaxPositiveValue",
990       R"({"optionalFloat": 3.402823e+38})",
991       "optional_float: 3.402823e+38");
992   RunValidJsonTest(
993       "FloatFieldMinNegativeValue",
994       R"({"optionalFloat": 3.402823e+38})",
995       "optional_float: 3.402823e+38");
996   // Values can be quoted.
997   RunValidJsonTest(
998       "FloatFieldQuotedValue",
999       R"({"optionalFloat": "1"})",
1000       "optional_float: 1");
1001   // Special values.
1002   RunValidJsonTest(
1003       "FloatFieldNan",
1004       R"({"optionalFloat": "NaN"})",
1005       "optional_float: nan");
1006   RunValidJsonTest(
1007       "FloatFieldInfinity",
1008       R"({"optionalFloat": "Infinity"})",
1009       "optional_float: inf");
1010   RunValidJsonTest(
1011       "FloatFieldNegativeInfinity",
1012       R"({"optionalFloat": "-Infinity"})",
1013       "optional_float: -inf");
1014   // Non-cannonical Nan will be correctly normalized.
1015   {
1016     TestAllTypes message;
1017     // IEEE floating-point standard 32-bit quiet NaN:
1018     //   0111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
1019     message.set_optional_float(
1020         WireFormatLite::DecodeFloat(0x7FA12345));
1021     RunValidJsonTestWithProtobufInput(
1022         "FloatFieldNormalizeQuietNan", message,
1023         "optional_float: nan");
1024     // IEEE floating-point standard 64-bit signaling NaN:
1025     //   1111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
1026     message.set_optional_float(
1027         WireFormatLite::DecodeFloat(0xFFB54321));
1028     RunValidJsonTestWithProtobufInput(
1029         "FloatFieldNormalizeSignalingNan", message,
1030         "optional_float: nan");
1031   }
1032 
1033   // Special values must be quoted.
1034   ExpectParseFailureForJson(
1035       "FloatFieldNanNotQuoted",
1036       R"({"optionalFloat": NaN})");
1037   ExpectParseFailureForJson(
1038       "FloatFieldInfinityNotQuoted",
1039       R"({"optionalFloat": Infinity})");
1040   ExpectParseFailureForJson(
1041       "FloatFieldNegativeInfinityNotQuoted",
1042       R"({"optionalFloat": -Infinity})");
1043   // Parsers should reject out-of-bound values.
1044   ExpectParseFailureForJson(
1045       "FloatFieldTooSmall",
1046       R"({"optionalFloat": -3.502823e+38})");
1047   ExpectParseFailureForJson(
1048       "FloatFieldTooLarge",
1049       R"({"optionalFloat": 3.502823e+38})");
1050 
1051   // Double fields.
1052   RunValidJsonTest(
1053       "DoubleFieldMinPositiveValue",
1054       R"({"optionalDouble": 2.22507e-308})",
1055       "optional_double: 2.22507e-308");
1056   RunValidJsonTest(
1057       "DoubleFieldMaxNegativeValue",
1058       R"({"optionalDouble": -2.22507e-308})",
1059       "optional_double: -2.22507e-308");
1060   RunValidJsonTest(
1061       "DoubleFieldMaxPositiveValue",
1062       R"({"optionalDouble": 1.79769e+308})",
1063       "optional_double: 1.79769e+308");
1064   RunValidJsonTest(
1065       "DoubleFieldMinNegativeValue",
1066       R"({"optionalDouble": -1.79769e+308})",
1067       "optional_double: -1.79769e+308");
1068   // Values can be quoted.
1069   RunValidJsonTest(
1070       "DoubleFieldQuotedValue",
1071       R"({"optionalDouble": "1"})",
1072       "optional_double: 1");
1073   // Speical values.
1074   RunValidJsonTest(
1075       "DoubleFieldNan",
1076       R"({"optionalDouble": "NaN"})",
1077       "optional_double: nan");
1078   RunValidJsonTest(
1079       "DoubleFieldInfinity",
1080       R"({"optionalDouble": "Infinity"})",
1081       "optional_double: inf");
1082   RunValidJsonTest(
1083       "DoubleFieldNegativeInfinity",
1084       R"({"optionalDouble": "-Infinity"})",
1085       "optional_double: -inf");
1086   // Non-cannonical Nan will be correctly normalized.
1087   {
1088     TestAllTypes message;
1089     message.set_optional_double(
1090         WireFormatLite::DecodeDouble(0x7FFA123456789ABCLL));
1091     RunValidJsonTestWithProtobufInput(
1092         "DoubleFieldNormalizeQuietNan", message,
1093         "optional_double: nan");
1094     message.set_optional_double(
1095         WireFormatLite::DecodeDouble(0xFFFBCBA987654321LL));
1096     RunValidJsonTestWithProtobufInput(
1097         "DoubleFieldNormalizeSignalingNan", message,
1098         "optional_double: nan");
1099   }
1100 
1101   // Special values must be quoted.
1102   ExpectParseFailureForJson(
1103       "DoubleFieldNanNotQuoted",
1104       R"({"optionalDouble": NaN})");
1105   ExpectParseFailureForJson(
1106       "DoubleFieldInfinityNotQuoted",
1107       R"({"optionalDouble": Infinity})");
1108   ExpectParseFailureForJson(
1109       "DoubleFieldNegativeInfinityNotQuoted",
1110       R"({"optionalDouble": -Infinity})");
1111 
1112   // Parsers should reject out-of-bound values.
1113   ExpectParseFailureForJson(
1114       "DoubleFieldTooSmall",
1115       R"({"optionalDouble": -1.89769e+308})");
1116   ExpectParseFailureForJson(
1117       "DoubleFieldTooLarge",
1118       R"({"optionalDouble": +1.89769e+308})");
1119 
1120   // Enum fields.
1121   RunValidJsonTest(
1122       "EnumField",
1123       R"({"optionalNestedEnum": "FOO"})",
1124       "optional_nested_enum: FOO");
1125   // Enum values must be represented as strings.
1126   ExpectParseFailureForJson(
1127       "EnumFieldNotQuoted",
1128       R"({"optionalNestedEnum": FOO})");
1129   // Numeric values are allowed.
1130   RunValidJsonTest(
1131       "EnumFieldNumericValueZero",
1132       R"({"optionalNestedEnum": 0})",
1133       "optional_nested_enum: FOO");
1134   RunValidJsonTest(
1135       "EnumFieldNumericValueNonZero",
1136       R"({"optionalNestedEnum": 1})",
1137       "optional_nested_enum: BAR");
1138   // Unknown enum values are represented as numeric values.
1139   RunValidJsonTestWithValidator(
1140       "EnumFieldUnknownValue",
1141       R"({"optionalNestedEnum": 123})",
1142       [](const Json::Value& value) {
1143         return value["optionalNestedEnum"].type() == Json::intValue &&
1144             value["optionalNestedEnum"].asInt() == 123;
1145       });
1146 
1147   // String fields.
1148   RunValidJsonTest(
1149       "StringField",
1150       R"({"optionalString": "Hello world!"})",
1151       "optional_string: \"Hello world!\"");
1152   RunValidJsonTest(
1153       "StringFieldUnicode",
1154       // Google in Chinese.
1155       R"({"optionalString": "谷歌"})",
1156       R"(optional_string: "谷歌")");
1157   RunValidJsonTest(
1158       "StringFieldEscape",
1159       R"({"optionalString": "\"\\\/\b\f\n\r\t"})",
1160       R"(optional_string: "\"\\/\b\f\n\r\t")");
1161   RunValidJsonTest(
1162       "StringFieldUnicodeEscape",
1163       R"({"optionalString": "\u8C37\u6B4C"})",
1164       R"(optional_string: "谷歌")");
1165   RunValidJsonTest(
1166       "StringFieldUnicodeEscapeWithLowercaseHexLetters",
1167       R"({"optionalString": "\u8c37\u6b4c"})",
1168       R"(optional_string: "谷歌")");
1169   RunValidJsonTest(
1170       "StringFieldSurrogatePair",
1171       // The character is an emoji: grinning face with smiling eyes. ��
1172       R"({"optionalString": "\uD83D\uDE01"})",
1173       R"(optional_string: "\xF0\x9F\x98\x81")");
1174 
1175   // Unicode escapes must start with "\u" (lowercase u).
1176   ExpectParseFailureForJson(
1177       "StringFieldUppercaseEscapeLetter",
1178       R"({"optionalString": "\U8C37\U6b4C"})");
1179   ExpectParseFailureForJson(
1180       "StringFieldInvalidEscape",
1181       R"({"optionalString": "\uXXXX\u6B4C"})");
1182   ExpectParseFailureForJson(
1183       "StringFieldUnterminatedEscape",
1184       R"({"optionalString": "\u8C3"})");
1185   ExpectParseFailureForJson(
1186       "StringFieldUnpairedHighSurrogate",
1187       R"({"optionalString": "\uD800"})");
1188   ExpectParseFailureForJson(
1189       "StringFieldUnpairedLowSurrogate",
1190       R"({"optionalString": "\uDC00"})");
1191   ExpectParseFailureForJson(
1192       "StringFieldSurrogateInWrongOrder",
1193       R"({"optionalString": "\uDE01\uD83D"})");
1194   ExpectParseFailureForJson(
1195       "StringFieldNotAString",
1196       R"({"optionalString": 12345})");
1197 
1198   // Bytes fields.
1199   RunValidJsonTest(
1200       "BytesField",
1201       R"({"optionalBytes": "AQI="})",
1202       R"(optional_bytes: "\x01\x02")");
1203   ExpectParseFailureForJson(
1204       "BytesFieldNoPadding",
1205       R"({"optionalBytes": "AQI"})");
1206   ExpectParseFailureForJson(
1207       "BytesFieldInvalidBase64Characters",
1208       R"({"optionalBytes": "-_=="})");
1209 
1210   // Message fields.
1211   RunValidJsonTest(
1212       "MessageField",
1213       R"({"optionalNestedMessage": {"a": 1234}})",
1214       "optional_nested_message: {a: 1234}");
1215 
1216   // Oneof fields.
1217   ExpectParseFailureForJson(
1218       "OneofFieldDuplicate",
1219       R"({"oneofUint32": 1, "oneofString": "test"})");
1220 
1221   // Repeated fields.
1222   RunValidJsonTest(
1223       "PrimitiveRepeatedField",
1224       R"({"repeatedInt32": [1, 2, 3, 4]})",
1225       "repeated_int32: [1, 2, 3, 4]");
1226   RunValidJsonTest(
1227       "EnumRepeatedField",
1228       R"({"repeatedNestedEnum": ["FOO", "BAR", "BAZ"]})",
1229       "repeated_nested_enum: [FOO, BAR, BAZ]");
1230   RunValidJsonTest(
1231       "StringRepeatedField",
1232       R"({"repeatedString": ["Hello", "world"]})",
1233       R"(repeated_string: ["Hello", "world"])");
1234   RunValidJsonTest(
1235       "BytesRepeatedField",
1236       R"({"repeatedBytes": ["AAEC", "AQI="]})",
1237       R"(repeated_bytes: ["\x00\x01\x02", "\x01\x02"])");
1238   RunValidJsonTest(
1239       "MessageRepeatedField",
1240       R"({"repeatedNestedMessage": [{"a": 1234}, {"a": 5678}]})",
1241       "repeated_nested_message: {a: 1234}"
1242       "repeated_nested_message: {a: 5678}");
1243 
1244   // Repeated field elements are of incorrect type.
1245   ExpectParseFailureForJson(
1246       "RepeatedFieldWrongElementTypeExpectingIntegersGotBool",
1247       R"({"repeatedInt32": [1, false, 3, 4]})");
1248   ExpectParseFailureForJson(
1249       "RepeatedFieldWrongElementTypeExpectingIntegersGotString",
1250       R"({"repeatedInt32": [1, 2, "name", 4]})");
1251   ExpectParseFailureForJson(
1252       "RepeatedFieldWrongElementTypeExpectingIntegersGotMessage",
1253       R"({"repeatedInt32": [1, 2, 3, {"a": 4}]})");
1254   ExpectParseFailureForJson(
1255       "RepeatedFieldWrongElementTypeExpectingStringsGotInt",
1256       R"({"repeatedString": ["1", 2, "3", "4"]})");
1257   ExpectParseFailureForJson(
1258       "RepeatedFieldWrongElementTypeExpectingStringsGotBool",
1259       R"({"repeatedString": ["1", "2", false, "4"]})");
1260   ExpectParseFailureForJson(
1261       "RepeatedFieldWrongElementTypeExpectingStringsGotMessage",
1262       R"({"repeatedString": ["1", 2, "3", {"a": 4}]})");
1263   ExpectParseFailureForJson(
1264       "RepeatedFieldWrongElementTypeExpectingMessagesGotInt",
1265       R"({"repeatedNestedMessage": [{"a": 1}, 2]})");
1266   ExpectParseFailureForJson(
1267       "RepeatedFieldWrongElementTypeExpectingMessagesGotBool",
1268       R"({"repeatedNestedMessage": [{"a": 1}, false]})");
1269   ExpectParseFailureForJson(
1270       "RepeatedFieldWrongElementTypeExpectingMessagesGotString",
1271       R"({"repeatedNestedMessage": [{"a": 1}, "2"]})");
1272   // Trailing comma in the repeated field is not allowed.
1273   ExpectParseFailureForJson(
1274       "RepeatedFieldTrailingComma",
1275       R"({"repeatedInt32": [1, 2, 3, 4,]})");
1276 
1277   // Map fields.
1278   RunValidJsonTest(
1279       "Int32MapField",
1280       R"({"mapInt32Int32": {"1": 2, "3": 4}})",
1281       "map_int32_int32: {key: 1 value: 2}"
1282       "map_int32_int32: {key: 3 value: 4}");
1283   ExpectParseFailureForJson(
1284       "Int32MapFieldKeyNotQuoted",
1285       R"({"mapInt32Int32": {1: 2, 3: 4}})");
1286   RunValidJsonTest(
1287       "Uint32MapField",
1288       R"({"mapUint32Uint32": {"1": 2, "3": 4}})",
1289       "map_uint32_uint32: {key: 1 value: 2}"
1290       "map_uint32_uint32: {key: 3 value: 4}");
1291   ExpectParseFailureForJson(
1292       "Uint32MapFieldKeyNotQuoted",
1293       R"({"mapUint32Uint32": {1: 2, 3: 4}})");
1294   RunValidJsonTest(
1295       "Int64MapField",
1296       R"({"mapInt64Int64": {"1": 2, "3": 4}})",
1297       "map_int64_int64: {key: 1 value: 2}"
1298       "map_int64_int64: {key: 3 value: 4}");
1299   ExpectParseFailureForJson(
1300       "Int64MapFieldKeyNotQuoted",
1301       R"({"mapInt64Int64": {1: 2, 3: 4}})");
1302   RunValidJsonTest(
1303       "Uint64MapField",
1304       R"({"mapUint64Uint64": {"1": 2, "3": 4}})",
1305       "map_uint64_uint64: {key: 1 value: 2}"
1306       "map_uint64_uint64: {key: 3 value: 4}");
1307   ExpectParseFailureForJson(
1308       "Uint64MapFieldKeyNotQuoted",
1309       R"({"mapUint64Uint64": {1: 2, 3: 4}})");
1310   RunValidJsonTest(
1311       "BoolMapField",
1312       R"({"mapBoolBool": {"true": true, "false": false}})",
1313       "map_bool_bool: {key: true value: true}"
1314       "map_bool_bool: {key: false value: false}");
1315   ExpectParseFailureForJson(
1316       "BoolMapFieldKeyNotQuoted",
1317       R"({"mapBoolBool": {true: true, false: false}})");
1318   RunValidJsonTest(
1319       "MessageMapField",
1320       R"({
1321         "mapStringNestedMessage": {
1322           "hello": {"a": 1234},
1323           "world": {"a": 5678}
1324         }
1325       })",
1326       R"(
1327         map_string_nested_message: {
1328           key: "hello"
1329           value: {a: 1234}
1330         }
1331         map_string_nested_message: {
1332           key: "world"
1333           value: {a: 5678}
1334         }
1335       )");
1336   // Since Map keys are represented as JSON strings, escaping should be allowed.
1337   RunValidJsonTest(
1338       "Int32MapEscapedKey",
1339       R"({"mapInt32Int32": {"\u0031": 2}})",
1340       "map_int32_int32: {key: 1 value: 2}");
1341   RunValidJsonTest(
1342       "Int64MapEscapedKey",
1343       R"({"mapInt64Int64": {"\u0031": 2}})",
1344       "map_int64_int64: {key: 1 value: 2}");
1345   RunValidJsonTest(
1346       "BoolMapEscapedKey",
1347       R"({"mapBoolBool": {"tr\u0075e": true}})",
1348       "map_bool_bool: {key: true value: true}");
1349 
1350   // "null" is accepted for all fields types.
1351   RunValidJsonTest(
1352       "AllFieldAcceptNull",
1353       R"({
1354         "optionalInt32": null,
1355         "optionalInt64": null,
1356         "optionalUint32": null,
1357         "optionalUint64": null,
1358         "optionalBool": null,
1359         "optionalString": null,
1360         "optionalBytes": null,
1361         "optionalNestedEnum": null,
1362         "optionalNestedMessage": null,
1363         "repeatedInt32": null,
1364         "repeatedInt64": null,
1365         "repeatedUint32": null,
1366         "repeatedUint64": null,
1367         "repeatedBool": null,
1368         "repeatedString": null,
1369         "repeatedBytes": null,
1370         "repeatedNestedEnum": null,
1371         "repeatedNestedMessage": null,
1372         "mapInt32Int32": null,
1373         "mapBoolBool": null,
1374         "mapStringNestedMessage": null
1375       })",
1376       "");
1377 
1378   // Repeated field elements cannot be null.
1379   ExpectParseFailureForJson(
1380       "RepeatedFieldPrimitiveElementIsNull",
1381       R"({"repeatedInt32": [1, null, 2]})");
1382   ExpectParseFailureForJson(
1383       "RepeatedFieldMessageElementIsNull",
1384       R"({"repeatedNestedMessage": [{"a":1}, null, {"a":2}]})");
1385   // Map field keys cannot be null.
1386   ExpectParseFailureForJson(
1387       "MapFieldKeyIsNull",
1388       R"({"mapInt32Int32": {null: 1}})");
1389   // Map field values cannot be null.
1390   ExpectParseFailureForJson(
1391       "MapFieldValueIsNull",
1392       R"({"mapInt32Int32": {"0": null}})");
1393 
1394   // Wrapper types.
1395   RunValidJsonTest(
1396       "OptionalBoolWrapper",
1397       R"({"optionalBoolWrapper": false})",
1398       "optional_bool_wrapper: {value: false}");
1399   RunValidJsonTest(
1400       "OptionalInt32Wrapper",
1401       R"({"optionalInt32Wrapper": 0})",
1402       "optional_int32_wrapper: {value: 0}");
1403   RunValidJsonTest(
1404       "OptionalUint32Wrapper",
1405       R"({"optionalUint32Wrapper": 0})",
1406       "optional_uint32_wrapper: {value: 0}");
1407   RunValidJsonTest(
1408       "OptionalInt64Wrapper",
1409       R"({"optionalInt64Wrapper": 0})",
1410       "optional_int64_wrapper: {value: 0}");
1411   RunValidJsonTest(
1412       "OptionalUint64Wrapper",
1413       R"({"optionalUint64Wrapper": 0})",
1414       "optional_uint64_wrapper: {value: 0}");
1415   RunValidJsonTest(
1416       "OptionalFloatWrapper",
1417       R"({"optionalFloatWrapper": 0})",
1418       "optional_float_wrapper: {value: 0}");
1419   RunValidJsonTest(
1420       "OptionalDoubleWrapper",
1421       R"({"optionalDoubleWrapper": 0})",
1422       "optional_double_wrapper: {value: 0}");
1423   RunValidJsonTest(
1424       "OptionalStringWrapper",
1425       R"({"optionalStringWrapper": ""})",
1426       R"(optional_string_wrapper: {value: ""})");
1427   RunValidJsonTest(
1428       "OptionalBytesWrapper",
1429       R"({"optionalBytesWrapper": ""})",
1430       R"(optional_bytes_wrapper: {value: ""})");
1431   RunValidJsonTest(
1432       "OptionalWrapperTypesWithNonDefaultValue",
1433       R"({
1434         "optionalBoolWrapper": true,
1435         "optionalInt32Wrapper": 1,
1436         "optionalUint32Wrapper": 1,
1437         "optionalInt64Wrapper": "1",
1438         "optionalUint64Wrapper": "1",
1439         "optionalFloatWrapper": 1,
1440         "optionalDoubleWrapper": 1,
1441         "optionalStringWrapper": "1",
1442         "optionalBytesWrapper": "AQI="
1443       })",
1444       R"(
1445         optional_bool_wrapper: {value: true}
1446         optional_int32_wrapper: {value: 1}
1447         optional_uint32_wrapper: {value: 1}
1448         optional_int64_wrapper: {value: 1}
1449         optional_uint64_wrapper: {value: 1}
1450         optional_float_wrapper: {value: 1}
1451         optional_double_wrapper: {value: 1}
1452         optional_string_wrapper: {value: "1"}
1453         optional_bytes_wrapper: {value: "\x01\x02"}
1454       )");
1455   RunValidJsonTest(
1456       "RepeatedBoolWrapper",
1457       R"({"repeatedBoolWrapper": [true, false]})",
1458       "repeated_bool_wrapper: {value: true}"
1459       "repeated_bool_wrapper: {value: false}");
1460   RunValidJsonTest(
1461       "RepeatedInt32Wrapper",
1462       R"({"repeatedInt32Wrapper": [0, 1]})",
1463       "repeated_int32_wrapper: {value: 0}"
1464       "repeated_int32_wrapper: {value: 1}");
1465   RunValidJsonTest(
1466       "RepeatedUint32Wrapper",
1467       R"({"repeatedUint32Wrapper": [0, 1]})",
1468       "repeated_uint32_wrapper: {value: 0}"
1469       "repeated_uint32_wrapper: {value: 1}");
1470   RunValidJsonTest(
1471       "RepeatedInt64Wrapper",
1472       R"({"repeatedInt64Wrapper": [0, 1]})",
1473       "repeated_int64_wrapper: {value: 0}"
1474       "repeated_int64_wrapper: {value: 1}");
1475   RunValidJsonTest(
1476       "RepeatedUint64Wrapper",
1477       R"({"repeatedUint64Wrapper": [0, 1]})",
1478       "repeated_uint64_wrapper: {value: 0}"
1479       "repeated_uint64_wrapper: {value: 1}");
1480   RunValidJsonTest(
1481       "RepeatedFloatWrapper",
1482       R"({"repeatedFloatWrapper": [0, 1]})",
1483       "repeated_float_wrapper: {value: 0}"
1484       "repeated_float_wrapper: {value: 1}");
1485   RunValidJsonTest(
1486       "RepeatedDoubleWrapper",
1487       R"({"repeatedDoubleWrapper": [0, 1]})",
1488       "repeated_double_wrapper: {value: 0}"
1489       "repeated_double_wrapper: {value: 1}");
1490   RunValidJsonTest(
1491       "RepeatedStringWrapper",
1492       R"({"repeatedStringWrapper": ["", "AQI="]})",
1493       R"(
1494         repeated_string_wrapper: {value: ""}
1495         repeated_string_wrapper: {value: "AQI="}
1496       )");
1497   RunValidJsonTest(
1498       "RepeatedBytesWrapper",
1499       R"({"repeatedBytesWrapper": ["", "AQI="]})",
1500       R"(
1501         repeated_bytes_wrapper: {value: ""}
1502         repeated_bytes_wrapper: {value: "\x01\x02"}
1503       )");
1504   RunValidJsonTest(
1505       "WrapperTypesWithNullValue",
1506       R"({
1507         "optionalBoolWrapper": null,
1508         "optionalInt32Wrapper": null,
1509         "optionalUint32Wrapper": null,
1510         "optionalInt64Wrapper": null,
1511         "optionalUint64Wrapper": null,
1512         "optionalFloatWrapper": null,
1513         "optionalDoubleWrapper": null,
1514         "optionalStringWrapper": null,
1515         "optionalBytesWrapper": null,
1516         "repeatedBoolWrapper": null,
1517         "repeatedInt32Wrapper": null,
1518         "repeatedUint32Wrapper": null,
1519         "repeatedInt64Wrapper": null,
1520         "repeatedUint64Wrapper": null,
1521         "repeatedFloatWrapper": null,
1522         "repeatedDoubleWrapper": null,
1523         "repeatedStringWrapper": null,
1524         "repeatedBytesWrapper": null
1525       })",
1526       "");
1527 
1528   // Duration
1529   RunValidJsonTest(
1530       "DurationMinValue",
1531       R"({"optionalDuration": "-315576000000.999999999s"})",
1532       "optional_duration: {seconds: -315576000000 nanos: -999999999}");
1533   RunValidJsonTest(
1534       "DurationMaxValue",
1535       R"({"optionalDuration": "315576000000.999999999s"})",
1536       "optional_duration: {seconds: 315576000000 nanos: 999999999}");
1537   RunValidJsonTest(
1538       "DurationRepeatedValue",
1539       R"({"repeatedDuration": ["1.5s", "-1.5s"]})",
1540       "repeated_duration: {seconds: 1 nanos: 500000000}"
1541       "repeated_duration: {seconds: -1 nanos: -500000000}");
1542 
1543   ExpectParseFailureForJson(
1544       "DurationMissingS",
1545       R"({"optionalDuration": "1"})");
1546   ExpectParseFailureForJson(
1547       "DurationJsonInputTooSmall",
1548       R"({"optionalDuration": "-315576000001.000000000s"})");
1549   ExpectParseFailureForJson(
1550       "DurationJsonInputTooLarge",
1551       R"({"optionalDuration": "315576000001.000000000s"})");
1552   ExpectSerializeFailureForJson(
1553       "DurationProtoInputTooSmall",
1554       "optional_duration: {seconds: -315576000001 nanos: 0}");
1555   ExpectSerializeFailureForJson(
1556       "DurationProtoInputTooLarge",
1557       "optional_duration: {seconds: 315576000001 nanos: 0}");
1558 
1559   RunValidJsonTestWithValidator(
1560       "DurationHasZeroFractionalDigit",
1561       R"({"optionalDuration": "1.000000000s"})",
__anon613fe1920802(const Json::Value& value) 1562       [](const Json::Value& value) {
1563         return value["optionalDuration"].asString() == "1s";
1564       });
1565   RunValidJsonTestWithValidator(
1566       "DurationHas3FractionalDigits",
1567       R"({"optionalDuration": "1.010000000s"})",
__anon613fe1920902(const Json::Value& value) 1568       [](const Json::Value& value) {
1569         return value["optionalDuration"].asString() == "1.010s";
1570       });
1571   RunValidJsonTestWithValidator(
1572       "DurationHas6FractionalDigits",
1573       R"({"optionalDuration": "1.000010000s"})",
__anon613fe1920a02(const Json::Value& value) 1574       [](const Json::Value& value) {
1575         return value["optionalDuration"].asString() == "1.000010s";
1576       });
1577   RunValidJsonTestWithValidator(
1578       "DurationHas9FractionalDigits",
1579       R"({"optionalDuration": "1.000000010s"})",
__anon613fe1920b02(const Json::Value& value) 1580       [](const Json::Value& value) {
1581         return value["optionalDuration"].asString() == "1.000000010s";
1582       });
1583 
1584   // Timestamp
1585   RunValidJsonTest(
1586       "TimestampMinValue",
1587       R"({"optionalTimestamp": "0001-01-01T00:00:00Z"})",
1588       "optional_timestamp: {seconds: -62135596800}");
1589   RunValidJsonTest(
1590       "TimestampMaxValue",
1591       R"({"optionalTimestamp": "9999-12-31T23:59:59.999999999Z"})",
1592       "optional_timestamp: {seconds: 253402300799 nanos: 999999999}");
1593   RunValidJsonTest(
1594       "TimestampRepeatedValue",
1595       R"({
1596         "repeatedTimestamp": [
1597           "0001-01-01T00:00:00Z",
1598           "9999-12-31T23:59:59.999999999Z"
1599         ]
1600       })",
1601       "repeated_timestamp: {seconds: -62135596800}"
1602       "repeated_timestamp: {seconds: 253402300799 nanos: 999999999}");
1603   RunValidJsonTest(
1604       "TimestampWithPositiveOffset",
1605       R"({"optionalTimestamp": "1970-01-01T08:00:00+08:00"})",
1606       "optional_timestamp: {seconds: 0}");
1607   RunValidJsonTest(
1608       "TimestampWithNegativeOffset",
1609       R"({"optionalTimestamp": "1969-12-31T16:00:00-08:00"})",
1610       "optional_timestamp: {seconds: 0}");
1611 
1612   ExpectParseFailureForJson(
1613       "TimestampJsonInputTooSmall",
1614       R"({"optionalTimestamp": "0000-01-01T00:00:00Z"})");
1615   ExpectParseFailureForJson(
1616       "TimestampJsonInputTooLarge",
1617       R"({"optionalTimestamp": "10000-01-01T00:00:00Z"})");
1618   ExpectParseFailureForJson(
1619       "TimestampJsonInputMissingZ",
1620       R"({"optionalTimestamp": "0001-01-01T00:00:00"})");
1621   ExpectParseFailureForJson(
1622       "TimestampJsonInputMissingT",
1623       R"({"optionalTimestamp": "0001-01-01 00:00:00Z"})");
1624   ExpectParseFailureForJson(
1625       "TimestampJsonInputLowercaseZ",
1626       R"({"optionalTimestamp": "0001-01-01T00:00:00z"})");
1627   ExpectParseFailureForJson(
1628       "TimestampJsonInputLowercaseT",
1629       R"({"optionalTimestamp": "0001-01-01t00:00:00Z"})");
1630   ExpectSerializeFailureForJson(
1631       "TimestampProtoInputTooSmall",
1632       "optional_timestamp: {seconds: -62135596801}");
1633   ExpectSerializeFailureForJson(
1634       "TimestampProtoInputTooLarge",
1635       "optional_timestamp: {seconds: 253402300800}");
1636   RunValidJsonTestWithValidator(
1637       "TimestampZeroNormalized",
1638       R"({"optionalTimestamp": "1969-12-31T16:00:00-08:00"})",
__anon613fe1920c02(const Json::Value& value) 1639       [](const Json::Value& value) {
1640         return value["optionalTimestamp"].asString() ==
1641             "1970-01-01T00:00:00Z";
1642       });
1643   RunValidJsonTestWithValidator(
1644       "TimestampHasZeroFractionalDigit",
1645       R"({"optionalTimestamp": "1970-01-01T00:00:00.000000000Z"})",
__anon613fe1920d02(const Json::Value& value) 1646       [](const Json::Value& value) {
1647         return value["optionalTimestamp"].asString() ==
1648             "1970-01-01T00:00:00Z";
1649       });
1650   RunValidJsonTestWithValidator(
1651       "TimestampHas3FractionalDigits",
1652       R"({"optionalTimestamp": "1970-01-01T00:00:00.010000000Z"})",
__anon613fe1920e02(const Json::Value& value) 1653       [](const Json::Value& value) {
1654         return value["optionalTimestamp"].asString() ==
1655             "1970-01-01T00:00:00.010Z";
1656       });
1657   RunValidJsonTestWithValidator(
1658       "TimestampHas6FractionalDigits",
1659       R"({"optionalTimestamp": "1970-01-01T00:00:00.000010000Z"})",
__anon613fe1920f02(const Json::Value& value) 1660       [](const Json::Value& value) {
1661         return value["optionalTimestamp"].asString() ==
1662             "1970-01-01T00:00:00.000010Z";
1663       });
1664   RunValidJsonTestWithValidator(
1665       "TimestampHas9FractionalDigits",
1666       R"({"optionalTimestamp": "1970-01-01T00:00:00.000000010Z"})",
__anon613fe1921002(const Json::Value& value) 1667       [](const Json::Value& value) {
1668         return value["optionalTimestamp"].asString() ==
1669             "1970-01-01T00:00:00.000000010Z";
1670       });
1671 
1672   // FieldMask
1673   RunValidJsonTest(
1674       "FieldMask",
1675       R"({"optionalFieldMask": "foo,barBaz"})",
1676       R"(optional_field_mask: {paths: "foo" paths: "bar_baz"})");
1677   ExpectParseFailureForJson(
1678       "FieldMaskInvalidCharacter",
1679       R"({"optionalFieldMask": "foo,bar_bar"})");
1680   ExpectSerializeFailureForJson(
1681       "FieldMaskPathsDontRoundTrip",
1682       R"(optional_field_mask: {paths: "fooBar"})");
1683   ExpectSerializeFailureForJson(
1684       "FieldMaskNumbersDontRoundTrip",
1685       R"(optional_field_mask: {paths: "foo_3_bar"})");
1686   ExpectSerializeFailureForJson(
1687       "FieldMaskTooManyUnderscore",
1688       R"(optional_field_mask: {paths: "foo__bar"})");
1689 
1690   // Struct
1691   RunValidJsonTest(
1692       "Struct",
1693       R"({
1694         "optionalStruct": {
1695           "nullValue": null,
1696           "intValue": 1234,
1697           "boolValue": true,
1698           "doubleValue": 1234.5678,
1699           "stringValue": "Hello world!",
1700           "listValue": [1234, "5678"],
1701           "objectValue": {
1702             "value": 0
1703           }
1704         }
1705       })",
1706       R"(
1707         optional_struct: {
1708           fields: {
1709             key: "nullValue"
1710             value: {null_value: NULL_VALUE}
1711           }
1712           fields: {
1713             key: "intValue"
1714             value: {number_value: 1234}
1715           }
1716           fields: {
1717             key: "boolValue"
1718             value: {bool_value: true}
1719           }
1720           fields: {
1721             key: "doubleValue"
1722             value: {number_value: 1234.5678}
1723           }
1724           fields: {
1725             key: "stringValue"
1726             value: {string_value: "Hello world!"}
1727           }
1728           fields: {
1729             key: "listValue"
1730             value: {
1731               list_value: {
1732                 values: {
1733                   number_value: 1234
1734                 }
1735                 values: {
1736                   string_value: "5678"
1737                 }
1738               }
1739             }
1740           }
1741           fields: {
1742             key: "objectValue"
1743             value: {
1744               struct_value: {
1745                 fields: {
1746                   key: "value"
1747                   value: {
1748                     number_value: 0
1749                   }
1750                 }
1751               }
1752             }
1753           }
1754         }
1755       )");
1756   // Value
1757   RunValidJsonTest(
1758       "ValueAcceptInteger",
1759       R"({"optionalValue": 1})",
1760       "optional_value: { number_value: 1}");
1761   RunValidJsonTest(
1762       "ValueAcceptFloat",
1763       R"({"optionalValue": 1.5})",
1764       "optional_value: { number_value: 1.5}");
1765   RunValidJsonTest(
1766       "ValueAcceptBool",
1767       R"({"optionalValue": false})",
1768       "optional_value: { bool_value: false}");
1769   RunValidJsonTest(
1770       "ValueAcceptNull",
1771       R"({"optionalValue": null})",
1772       "optional_value: { null_value: NULL_VALUE}");
1773   RunValidJsonTest(
1774       "ValueAcceptString",
1775       R"({"optionalValue": "hello"})",
1776       R"(optional_value: { string_value: "hello"})");
1777   RunValidJsonTest(
1778       "ValueAcceptList",
1779       R"({"optionalValue": [0, "hello"]})",
1780       R"(
1781         optional_value: {
1782           list_value: {
1783             values: {
1784               number_value: 0
1785             }
1786             values: {
1787               string_value: "hello"
1788             }
1789           }
1790         }
1791       )");
1792   RunValidJsonTest(
1793       "ValueAcceptObject",
1794       R"({"optionalValue": {"value": 1}})",
1795       R"(
1796         optional_value: {
1797           struct_value: {
1798             fields: {
1799               key: "value"
1800               value: {
1801                 number_value: 1
1802               }
1803             }
1804           }
1805         }
1806       )");
1807 
1808   // Any
1809   RunValidJsonTest(
1810       "Any",
1811       R"({
1812         "optionalAny": {
1813           "@type": "type.googleapis.com/conformance.TestAllTypes",
1814           "optionalInt32": 12345
1815         }
1816       })",
1817       R"(
1818         optional_any: {
1819           [type.googleapis.com/conformance.TestAllTypes] {
1820             optional_int32: 12345
1821           }
1822         }
1823       )");
1824   RunValidJsonTest(
1825       "AnyNested",
1826       R"({
1827         "optionalAny": {
1828           "@type": "type.googleapis.com/google.protobuf.Any",
1829           "value": {
1830             "@type": "type.googleapis.com/conformance.TestAllTypes",
1831             "optionalInt32": 12345
1832           }
1833         }
1834       })",
1835       R"(
1836         optional_any: {
1837           [type.googleapis.com/google.protobuf.Any] {
1838             [type.googleapis.com/conformance.TestAllTypes] {
1839               optional_int32: 12345
1840             }
1841           }
1842         }
1843       )");
1844   // The special "@type" tag is not required to appear first.
1845   RunValidJsonTest(
1846       "AnyUnorderedTypeTag",
1847       R"({
1848         "optionalAny": {
1849           "optionalInt32": 12345,
1850           "@type": "type.googleapis.com/conformance.TestAllTypes"
1851         }
1852       })",
1853       R"(
1854         optional_any: {
1855           [type.googleapis.com/conformance.TestAllTypes] {
1856             optional_int32: 12345
1857           }
1858         }
1859       )");
1860   // Well-known types in Any.
1861   RunValidJsonTest(
1862       "AnyWithInt32ValueWrapper",
1863       R"({
1864         "optionalAny": {
1865           "@type": "type.googleapis.com/google.protobuf.Int32Value",
1866           "value": 12345
1867         }
1868       })",
1869       R"(
1870         optional_any: {
1871           [type.googleapis.com/google.protobuf.Int32Value] {
1872             value: 12345
1873           }
1874         }
1875       )");
1876   RunValidJsonTest(
1877       "AnyWithDuration",
1878       R"({
1879         "optionalAny": {
1880           "@type": "type.googleapis.com/google.protobuf.Duration",
1881           "value": "1.5s"
1882         }
1883       })",
1884       R"(
1885         optional_any: {
1886           [type.googleapis.com/google.protobuf.Duration] {
1887             seconds: 1
1888             nanos: 500000000
1889           }
1890         }
1891       )");
1892   RunValidJsonTest(
1893       "AnyWithTimestamp",
1894       R"({
1895         "optionalAny": {
1896           "@type": "type.googleapis.com/google.protobuf.Timestamp",
1897           "value": "1970-01-01T00:00:00Z"
1898         }
1899       })",
1900       R"(
1901         optional_any: {
1902           [type.googleapis.com/google.protobuf.Timestamp] {
1903             seconds: 0
1904             nanos: 0
1905           }
1906         }
1907       )");
1908   RunValidJsonTest(
1909       "AnyWithFieldMask",
1910       R"({
1911         "optionalAny": {
1912           "@type": "type.googleapis.com/google.protobuf.FieldMask",
1913           "value": "foo,barBaz"
1914         }
1915       })",
1916       R"(
1917         optional_any: {
1918           [type.googleapis.com/google.protobuf.FieldMask] {
1919             paths: ["foo", "bar_baz"]
1920           }
1921         }
1922       )");
1923   RunValidJsonTest(
1924       "AnyWithStruct",
1925       R"({
1926         "optionalAny": {
1927           "@type": "type.googleapis.com/google.protobuf.Struct",
1928           "value": {
1929             "foo": 1
1930           }
1931         }
1932       })",
1933       R"(
1934         optional_any: {
1935           [type.googleapis.com/google.protobuf.Struct] {
1936             fields: {
1937               key: "foo"
1938               value: {
1939                 number_value: 1
1940               }
1941             }
1942           }
1943         }
1944       )");
1945   RunValidJsonTest(
1946       "AnyWithValueForJsonObject",
1947       R"({
1948         "optionalAny": {
1949           "@type": "type.googleapis.com/google.protobuf.Value",
1950           "value": {
1951             "foo": 1
1952           }
1953         }
1954       })",
1955       R"(
1956         optional_any: {
1957           [type.googleapis.com/google.protobuf.Value] {
1958             struct_value: {
1959               fields: {
1960                 key: "foo"
1961                 value: {
1962                   number_value: 1
1963                 }
1964               }
1965             }
1966           }
1967         }
1968       )");
1969   RunValidJsonTest(
1970       "AnyWithValueForInteger",
1971       R"({
1972         "optionalAny": {
1973           "@type": "type.googleapis.com/google.protobuf.Value",
1974           "value": 1
1975         }
1976       })",
1977       R"(
1978         optional_any: {
1979           [type.googleapis.com/google.protobuf.Value] {
1980             number_value: 1
1981           }
1982         }
1983       )");
1984 
1985   bool ok = true;
1986   if (!CheckSetEmpty(expected_to_fail_, "nonexistent_tests.txt",
1987                      "These tests were listed in the failure list, but they "
1988                      "don't exist.  Remove them from the failure list by "
1989                      "running:\n"
1990                      "  ./update_failure_list.py " + failure_list_filename_ +
1991                      " --remove nonexistent_tests.txt")) {
1992     ok = false;
1993   }
1994   if (!CheckSetEmpty(unexpected_failing_tests_, "failing_tests.txt",
1995                      "These tests failed.  If they can't be fixed right now, "
1996                      "you can add them to the failure list so the overall "
1997                      "suite can succeed.  Add them to the failure list by "
1998                      "running:\n"
1999                      "  ./update_failure_list.py " + failure_list_filename_ +
2000                      " --add failing_tests.txt")) {
2001     ok = false;
2002   }
2003   if (!CheckSetEmpty(unexpected_succeeding_tests_, "succeeding_tests.txt",
2004                      "These tests succeeded, even though they were listed in "
2005                      "the failure list.  Remove them from the failure list "
2006                      "by running:\n"
2007                      "  ./update_failure_list.py " + failure_list_filename_ +
2008                      " --remove succeeding_tests.txt")) {
2009     ok = false;
2010   }
2011 
2012   if (verbose_) {
2013     CheckSetEmpty(skipped_, "",
2014                   "These tests were skipped (probably because support for some "
2015                   "features is not implemented)");
2016   }
2017 
2018   StringAppendF(&output_,
2019                 "CONFORMANCE SUITE %s: %d successes, %d skipped, "
2020                 "%d expected failures, %d unexpected failures.\n",
2021                 ok ? "PASSED" : "FAILED", successes_, skipped_.size(),
2022                 expected_failures_, unexpected_failing_tests_.size());
2023   StringAppendF(&output_, "\n");
2024 
2025   output->assign(output_);
2026 
2027   return ok;
2028 }
2029 
2030 }  // namespace protobuf
2031 }  // namespace google
2032