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 "binary_json_conformance_suite.h"
32 #include "conformance_test.h"
33 #include "third_party/jsoncpp/json.h"
34
35 #include <google/protobuf/test_messages_proto3.pb.h>
36 #include <google/protobuf/test_messages_proto2.pb.h>
37
38 #include <google/protobuf/stubs/common.h>
39 #include <google/protobuf/stubs/strutil.h>
40 #include <google/protobuf/text_format.h>
41 #include <google/protobuf/util/json_util.h>
42 #include <google/protobuf/util/type_resolver_util.h>
43 #include <google/protobuf/wire_format_lite.h>
44
45 using conformance::ConformanceRequest;
46 using conformance::ConformanceResponse;
47 using conformance::WireFormat;
48 using google::protobuf::Descriptor;
49 using google::protobuf::FieldDescriptor;
50 using google::protobuf::Message;
51 using google::protobuf::internal::WireFormatLite;
52 using google::protobuf::TextFormat;
53 using google::protobuf::util::NewTypeResolverForDescriptorPool;
54 using protobuf_test_messages::proto3::TestAllTypesProto3;
55 using protobuf_test_messages::proto2::TestAllTypesProto2;
56 using std::string;
57
58 namespace {
59
60 static const char kTypeUrlPrefix[] = "type.googleapis.com";
61
GetTypeUrl(const Descriptor * message)62 static string GetTypeUrl(const Descriptor* message) {
63 return string(kTypeUrlPrefix) + "/" + message->full_name();
64 }
65
66 /* Routines for building arbitrary protos *************************************/
67
68 // We would use CodedOutputStream except that we want more freedom to build
69 // arbitrary protos (even invalid ones).
70
71 const string empty;
72
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)73 string cat(const string& a, const string& b,
74 const string& c = empty,
75 const string& d = empty,
76 const string& e = empty,
77 const string& f = empty,
78 const string& g = empty,
79 const string& h = empty,
80 const string& i = empty,
81 const string& j = empty,
82 const string& k = empty,
83 const string& l = empty) {
84 string ret;
85 ret.reserve(a.size() + b.size() + c.size() + d.size() + e.size() + f.size() +
86 g.size() + h.size() + i.size() + j.size() + k.size() + l.size());
87 ret.append(a);
88 ret.append(b);
89 ret.append(c);
90 ret.append(d);
91 ret.append(e);
92 ret.append(f);
93 ret.append(g);
94 ret.append(h);
95 ret.append(i);
96 ret.append(j);
97 ret.append(k);
98 ret.append(l);
99 return ret;
100 }
101
102 // The maximum number of bytes that it takes to encode a 64-bit varint.
103 #define VARINT_MAX_LEN 10
104
vencode64(uint64_t val,int over_encoded_bytes,char * buf)105 size_t vencode64(uint64_t val, int over_encoded_bytes, char *buf) {
106 if (val == 0) { buf[0] = 0; return 1; }
107 size_t i = 0;
108 while (val) {
109 uint8_t byte = val & 0x7fU;
110 val >>= 7;
111 if (val || over_encoded_bytes) byte |= 0x80U;
112 buf[i++] = byte;
113 }
114 while (over_encoded_bytes--) {
115 assert(i < 10);
116 uint8_t byte = over_encoded_bytes ? 0x80 : 0;
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, 0, buf);
125 return string(buf, len);
126 }
127
128 // Encodes a varint that is |extra| bytes longer than it needs to be, but still
129 // valid.
longvarint(uint64_t x,int extra)130 string longvarint(uint64_t x, int extra) {
131 char buf[VARINT_MAX_LEN];
132 size_t len = vencode64(x, extra, buf);
133 return string(buf, len);
134 }
135
136 // TODO: proper byte-swapping for big-endian machines.
fixed32(void * data)137 string fixed32(void *data) { return string(static_cast<char*>(data), 4); }
fixed64(void * data)138 string fixed64(void *data) { return string(static_cast<char*>(data), 8); }
139
delim(const string & buf)140 string delim(const string& buf) { return cat(varint(buf.size()), buf); }
u32(uint32_t u32)141 string u32(uint32_t u32) { return fixed32(&u32); }
u64(uint64_t u64)142 string u64(uint64_t u64) { return fixed64(&u64); }
flt(float f)143 string flt(float f) { return fixed32(&f); }
dbl(double d)144 string dbl(double d) { return fixed64(&d); }
zz32(int32_t x)145 string zz32(int32_t x) { return varint(WireFormatLite::ZigZagEncode32(x)); }
zz64(int64_t x)146 string zz64(int64_t x) { return varint(WireFormatLite::ZigZagEncode64(x)); }
147
tag(uint32_t fieldnum,char wire_type)148 string tag(uint32_t fieldnum, char wire_type) {
149 return varint((fieldnum << 3) | wire_type);
150 }
151
submsg(uint32_t fn,const string & buf)152 string submsg(uint32_t fn, const string& buf) {
153 return cat( tag(fn, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), delim(buf) );
154 }
155
156 #define UNKNOWN_FIELD 666
157
GetFieldForType(FieldDescriptor::Type type,bool repeated,bool is_proto3)158 const FieldDescriptor* GetFieldForType(FieldDescriptor::Type type,
159 bool repeated, bool is_proto3) {
160
161 const Descriptor* d = is_proto3 ?
162 TestAllTypesProto3().GetDescriptor() : TestAllTypesProto2().GetDescriptor();
163 for (int i = 0; i < d->field_count(); i++) {
164 const FieldDescriptor* f = d->field(i);
165 if (f->type() == type && f->is_repeated() == repeated) {
166 return f;
167 }
168 }
169 GOOGLE_LOG(FATAL) << "Couldn't find field with type " << (int)type;
170 return nullptr;
171 }
172
UpperCase(string str)173 string UpperCase(string str) {
174 for (int i = 0; i < str.size(); i++) {
175 str[i] = toupper(str[i]);
176 }
177 return str;
178 }
179
NewTestMessage(bool is_proto3)180 std::unique_ptr<Message> NewTestMessage(bool is_proto3) {
181 std::unique_ptr<Message> prototype;
182 if (is_proto3) {
183 prototype.reset(new TestAllTypesProto3());
184 } else {
185 prototype.reset(new TestAllTypesProto2());
186 }
187 return prototype;
188 }
189
190 } // anonymous namespace
191
192 namespace google {
193 namespace protobuf {
194
ParseJsonResponse(const ConformanceResponse & response,Message * test_message)195 bool BinaryAndJsonConformanceSuite::ParseJsonResponse(
196 const ConformanceResponse& response,
197 Message* test_message) {
198 string binary_protobuf;
199 util::Status status =
200 JsonToBinaryString(type_resolver_.get(), type_url_,
201 response.json_payload(), &binary_protobuf);
202
203 if (!status.ok()) {
204 return false;
205 }
206
207 if (!test_message->ParseFromString(binary_protobuf)) {
208 GOOGLE_LOG(FATAL)
209 << "INTERNAL ERROR: internal JSON->protobuf transcode "
210 << "yielded unparseable proto.";
211 return false;
212 }
213
214 return true;
215 }
216
ParseResponse(const ConformanceResponse & response,const ConformanceRequestSetting & setting,Message * test_message)217 bool BinaryAndJsonConformanceSuite::ParseResponse(
218 const ConformanceResponse& response,
219 const ConformanceRequestSetting& setting,
220 Message* test_message) {
221 const ConformanceRequest& request = setting.GetRequest();
222 WireFormat requested_output = request.requested_output_format();
223 const string& test_name = setting.GetTestName();
224 ConformanceLevel level = setting.GetLevel();
225
226 switch (response.result_case()) {
227 case ConformanceResponse::kProtobufPayload: {
228 if (requested_output != conformance::PROTOBUF) {
229 ReportFailure(
230 test_name, level, request, response,
231 StrCat("Test was asked for ", WireFormatToString(requested_output),
232 " output but provided PROTOBUF instead.").c_str());
233 return false;
234 }
235
236 if (!test_message->ParseFromString(response.protobuf_payload())) {
237 ReportFailure(test_name, level, request, response,
238 "Protobuf output we received from test was unparseable.");
239 return false;
240 }
241
242 break;
243 }
244
245 case ConformanceResponse::kJsonPayload: {
246 if (requested_output != conformance::JSON) {
247 ReportFailure(
248 test_name, level, request, response,
249 StrCat("Test was asked for ", WireFormatToString(requested_output),
250 " output but provided JSON instead.").c_str());
251 return false;
252 }
253
254 if (!ParseJsonResponse(response, test_message)) {
255 ReportFailure(test_name, level, request, response,
256 "JSON output we received from test was unparseable.");
257 return false;
258 }
259
260 break;
261 }
262
263 default:
264 GOOGLE_LOG(FATAL) << test_name << ": unknown payload type: "
265 << response.result_case();
266 }
267
268 return true;
269 }
270
ExpectParseFailureForProtoWithProtoVersion(const string & proto,const string & test_name,ConformanceLevel level,bool is_proto3)271 void BinaryAndJsonConformanceSuite::ExpectParseFailureForProtoWithProtoVersion (
272 const string& proto, const string& test_name, ConformanceLevel level,
273 bool is_proto3) {
274 std::unique_ptr<Message> prototype = NewTestMessage(is_proto3);
275 // We don't expect output, but if the program erroneously accepts the protobuf
276 // we let it send its response as this. We must not leave it unspecified.
277 ConformanceRequestSetting setting(
278 level, conformance::PROTOBUF, conformance::PROTOBUF,
279 conformance::BINARY_TEST,
280 *prototype, test_name, proto);
281
282 const ConformanceRequest& request = setting.GetRequest();
283 ConformanceResponse response;
284 string effective_test_name =
285 StrCat(setting.ConformanceLevelToString(level),
286 (is_proto3 ? ".Proto3" : ".Proto2"),
287 ".ProtobufInput.", test_name);
288
289 RunTest(effective_test_name, request, &response);
290 if (response.result_case() == ConformanceResponse::kParseError) {
291 ReportSuccess(effective_test_name);
292 } else if (response.result_case() == ConformanceResponse::kSkipped) {
293 ReportSkip(effective_test_name, request, response);
294 } else {
295 ReportFailure(effective_test_name, level, request, response,
296 "Should have failed to parse, but didn't.");
297 }
298 }
299
300 // Expect that this precise protobuf will cause a parse error.
ExpectParseFailureForProto(const string & proto,const string & test_name,ConformanceLevel level)301 void BinaryAndJsonConformanceSuite::ExpectParseFailureForProto(
302 const string& proto, const string& test_name, ConformanceLevel level) {
303 ExpectParseFailureForProtoWithProtoVersion(proto, test_name, level, true);
304 ExpectParseFailureForProtoWithProtoVersion(proto, test_name, level, false);
305 }
306
307 // Expect that this protobuf will cause a parse error, even if it is followed
308 // by valid protobuf data. We can try running this twice: once with this
309 // data verbatim and once with this data followed by some valid data.
310 //
311 // TODO(haberman): implement the second of these.
ExpectHardParseFailureForProto(const string & proto,const string & test_name,ConformanceLevel level)312 void BinaryAndJsonConformanceSuite::ExpectHardParseFailureForProto(
313 const string& proto, const string& test_name, ConformanceLevel level) {
314 return ExpectParseFailureForProto(proto, test_name, level);
315 }
316
RunValidJsonTest(const string & test_name,ConformanceLevel level,const string & input_json,const string & equivalent_text_format)317 void BinaryAndJsonConformanceSuite::RunValidJsonTest(
318 const string& test_name, ConformanceLevel level, const string& input_json,
319 const string& equivalent_text_format) {
320 TestAllTypesProto3 prototype;
321 ConformanceRequestSetting setting1(
322 level, conformance::JSON, conformance::PROTOBUF,
323 conformance::JSON_TEST,
324 prototype, test_name, input_json);
325 RunValidInputTest(setting1, equivalent_text_format);
326 ConformanceRequestSetting setting2(
327 level, conformance::JSON, conformance::JSON,
328 conformance::JSON_TEST,
329 prototype, test_name, input_json);
330 RunValidInputTest(setting2, equivalent_text_format);
331 }
332
RunValidJsonTestWithProtobufInput(const string & test_name,ConformanceLevel level,const TestAllTypesProto3 & input,const string & equivalent_text_format)333 void BinaryAndJsonConformanceSuite::RunValidJsonTestWithProtobufInput(
334 const string& test_name, ConformanceLevel level, const TestAllTypesProto3& input,
335 const string& equivalent_text_format) {
336 ConformanceRequestSetting setting(
337 level, conformance::PROTOBUF, conformance::JSON,
338 conformance::JSON_TEST,
339 input, test_name, input.SerializeAsString());
340 RunValidInputTest(setting, equivalent_text_format);
341 }
342
RunValidJsonIgnoreUnknownTest(const string & test_name,ConformanceLevel level,const string & input_json,const string & equivalent_text_format)343 void BinaryAndJsonConformanceSuite::RunValidJsonIgnoreUnknownTest(
344 const string& test_name, ConformanceLevel level, const string& input_json,
345 const string& equivalent_text_format) {
346 TestAllTypesProto3 prototype;
347 ConformanceRequestSetting setting(
348 level, conformance::JSON, conformance::PROTOBUF,
349 conformance::JSON_IGNORE_UNKNOWN_PARSING_TEST,
350 prototype, test_name, input_json);
351 RunValidInputTest(setting, equivalent_text_format);
352 }
353
RunValidProtobufTest(const string & test_name,ConformanceLevel level,const string & input_protobuf,const string & equivalent_text_format,bool is_proto3)354 void BinaryAndJsonConformanceSuite::RunValidProtobufTest(
355 const string& test_name, ConformanceLevel level,
356 const string& input_protobuf, const string& equivalent_text_format,
357 bool is_proto3) {
358 std::unique_ptr<Message> prototype = NewTestMessage(is_proto3);
359
360 ConformanceRequestSetting setting1(
361 level, conformance::PROTOBUF, conformance::PROTOBUF,
362 conformance::BINARY_TEST,
363 *prototype, test_name, input_protobuf);
364 RunValidInputTest(setting1, equivalent_text_format);
365
366 if (is_proto3) {
367 ConformanceRequestSetting setting2(
368 level, conformance::PROTOBUF, conformance::JSON,
369 conformance::BINARY_TEST,
370 *prototype, test_name, input_protobuf);
371 RunValidInputTest(setting2, equivalent_text_format);
372 }
373 }
374
RunValidBinaryProtobufTest(const string & test_name,ConformanceLevel level,const string & input_protobuf,bool is_proto3)375 void BinaryAndJsonConformanceSuite::RunValidBinaryProtobufTest(
376 const string& test_name, ConformanceLevel level,
377 const string& input_protobuf, bool is_proto3) {
378 std::unique_ptr<Message> prototype = NewTestMessage(is_proto3);
379 ConformanceRequestSetting setting(
380 level, conformance::PROTOBUF, conformance::PROTOBUF,
381 conformance::BINARY_TEST,
382 *prototype, test_name, input_protobuf);
383 RunValidBinaryInputTest(setting, input_protobuf);
384 }
385
RunValidProtobufTestWithMessage(const string & test_name,ConformanceLevel level,const Message * input,const string & equivalent_text_format,bool is_proto3)386 void BinaryAndJsonConformanceSuite::RunValidProtobufTestWithMessage(
387 const string& test_name, ConformanceLevel level, const Message *input,
388 const string& equivalent_text_format, bool is_proto3) {
389 RunValidProtobufTest(test_name, level, input->SerializeAsString(),
390 equivalent_text_format, is_proto3);
391 }
392
393 // According to proto3 JSON specification, JSON serializers follow more strict
394 // rules than parsers (e.g., a serializer must serialize int32 values as JSON
395 // numbers while the parser is allowed to accept them as JSON strings). This
396 // method allows strict checking on a proto3 JSON serializer by inspecting
397 // the JSON output directly.
RunValidJsonTestWithValidator(const string & test_name,ConformanceLevel level,const string & input_json,const Validator & validator)398 void BinaryAndJsonConformanceSuite::RunValidJsonTestWithValidator(
399 const string& test_name, ConformanceLevel level, const string& input_json,
400 const Validator& validator) {
401 TestAllTypesProto3 prototype;
402 ConformanceRequestSetting setting(
403 level, conformance::JSON, conformance::JSON,
404 conformance::JSON_TEST,
405 prototype, test_name, input_json);
406 const ConformanceRequest& request = setting.GetRequest();
407 ConformanceResponse response;
408 string effective_test_name =
409 StrCat(setting.ConformanceLevelToString(level),
410 ".Proto3.JsonInput.",
411 test_name, ".Validator");
412
413 RunTest(effective_test_name, request, &response);
414
415 if (response.result_case() == ConformanceResponse::kSkipped) {
416 ReportSkip(effective_test_name, request, response);
417 return;
418 }
419
420 if (response.result_case() != ConformanceResponse::kJsonPayload) {
421 ReportFailure(effective_test_name, level, request, response,
422 "Expected JSON payload but got type %d.",
423 response.result_case());
424 return;
425 }
426 Json::Reader reader;
427 Json::Value value;
428 if (!reader.parse(response.json_payload(), value)) {
429 ReportFailure(effective_test_name, level, request, response,
430 "JSON payload cannot be parsed as valid JSON: %s",
431 reader.getFormattedErrorMessages().c_str());
432 return;
433 }
434 if (!validator(value)) {
435 ReportFailure(effective_test_name, level, request, response,
436 "JSON payload validation failed.");
437 return;
438 }
439 ReportSuccess(effective_test_name);
440 }
441
ExpectParseFailureForJson(const string & test_name,ConformanceLevel level,const string & input_json)442 void BinaryAndJsonConformanceSuite::ExpectParseFailureForJson(
443 const string& test_name, ConformanceLevel level, const string& input_json) {
444 TestAllTypesProto3 prototype;
445 // We don't expect output, but if the program erroneously accepts the protobuf
446 // we let it send its response as this. We must not leave it unspecified.
447 ConformanceRequestSetting setting(
448 level, conformance::JSON, conformance::JSON,
449 conformance::JSON_TEST,
450 prototype, test_name, input_json);
451 const ConformanceRequest& request = setting.GetRequest();
452 ConformanceResponse response;
453 string effective_test_name =
454 StrCat(setting.ConformanceLevelToString(level),
455 ".Proto3.JsonInput.", test_name);
456
457 RunTest(effective_test_name, request, &response);
458 if (response.result_case() == ConformanceResponse::kParseError) {
459 ReportSuccess(effective_test_name);
460 } else if (response.result_case() == ConformanceResponse::kSkipped) {
461 ReportSkip(effective_test_name, request, response);
462 } else {
463 ReportFailure(effective_test_name, level, request, response,
464 "Should have failed to parse, but didn't.");
465 }
466 }
467
ExpectSerializeFailureForJson(const string & test_name,ConformanceLevel level,const string & text_format)468 void BinaryAndJsonConformanceSuite::ExpectSerializeFailureForJson(
469 const string& test_name, ConformanceLevel level, const string& text_format) {
470 TestAllTypesProto3 payload_message;
471 GOOGLE_CHECK(
472 TextFormat::ParseFromString(text_format, &payload_message))
473 << "Failed to parse: " << text_format;
474
475 TestAllTypesProto3 prototype;
476 ConformanceRequestSetting setting(
477 level, conformance::PROTOBUF, conformance::JSON,
478 conformance::JSON_TEST,
479 prototype, test_name, payload_message.SerializeAsString());
480 const ConformanceRequest& request = setting.GetRequest();
481 ConformanceResponse response;
482 string effective_test_name =
483 StrCat(setting.ConformanceLevelToString(level),
484 ".", test_name, ".JsonOutput");
485
486 RunTest(effective_test_name, request, &response);
487 if (response.result_case() == ConformanceResponse::kSerializeError) {
488 ReportSuccess(effective_test_name);
489 } else if (response.result_case() == ConformanceResponse::kSkipped) {
490 ReportSkip(effective_test_name, request, response);
491 } else {
492 ReportFailure(effective_test_name, level, request, response,
493 "Should have failed to serialize, but didn't.");
494 }
495 }
496
497 //TODO: proto2?
TestPrematureEOFForType(FieldDescriptor::Type type)498 void BinaryAndJsonConformanceSuite::TestPrematureEOFForType(
499 FieldDescriptor::Type type) {
500 // Incomplete values for each wire type.
501 static const string incompletes[6] = {
502 string("\x80"), // VARINT
503 string("abcdefg"), // 64BIT
504 string("\x80"), // DELIMITED (partial length)
505 string(), // START_GROUP (no value required)
506 string(), // END_GROUP (no value required)
507 string("abc") // 32BIT
508 };
509
510 const FieldDescriptor* field = GetFieldForType(type, false, true);
511 const FieldDescriptor* rep_field = GetFieldForType(type, true, true);
512 WireFormatLite::WireType wire_type = WireFormatLite::WireTypeForFieldType(
513 static_cast<WireFormatLite::FieldType>(type));
514 const string& incomplete = incompletes[wire_type];
515 const string type_name =
516 UpperCase(string(".") + FieldDescriptor::TypeName(type));
517
518 ExpectParseFailureForProto(
519 tag(field->number(), wire_type),
520 "PrematureEofBeforeKnownNonRepeatedValue" + type_name, REQUIRED);
521
522 ExpectParseFailureForProto(
523 tag(rep_field->number(), wire_type),
524 "PrematureEofBeforeKnownRepeatedValue" + type_name, REQUIRED);
525
526 ExpectParseFailureForProto(
527 tag(UNKNOWN_FIELD, wire_type),
528 "PrematureEofBeforeUnknownValue" + type_name, REQUIRED);
529
530 ExpectParseFailureForProto(
531 cat( tag(field->number(), wire_type), incomplete ),
532 "PrematureEofInsideKnownNonRepeatedValue" + type_name, REQUIRED);
533
534 ExpectParseFailureForProto(
535 cat( tag(rep_field->number(), wire_type), incomplete ),
536 "PrematureEofInsideKnownRepeatedValue" + type_name, REQUIRED);
537
538 ExpectParseFailureForProto(
539 cat( tag(UNKNOWN_FIELD, wire_type), incomplete ),
540 "PrematureEofInsideUnknownValue" + type_name, REQUIRED);
541
542 if (wire_type == WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
543 ExpectParseFailureForProto(
544 cat( tag(field->number(), wire_type), varint(1) ),
545 "PrematureEofInDelimitedDataForKnownNonRepeatedValue" + type_name,
546 REQUIRED);
547
548 ExpectParseFailureForProto(
549 cat( tag(rep_field->number(), wire_type), varint(1) ),
550 "PrematureEofInDelimitedDataForKnownRepeatedValue" + type_name,
551 REQUIRED);
552
553 // EOF in the middle of delimited data for unknown value.
554 ExpectParseFailureForProto(
555 cat( tag(UNKNOWN_FIELD, wire_type), varint(1) ),
556 "PrematureEofInDelimitedDataForUnknownValue" + type_name, REQUIRED);
557
558 if (type == FieldDescriptor::TYPE_MESSAGE) {
559 // Submessage ends in the middle of a value.
560 string incomplete_submsg =
561 cat( tag(WireFormatLite::TYPE_INT32, WireFormatLite::WIRETYPE_VARINT),
562 incompletes[WireFormatLite::WIRETYPE_VARINT] );
563 ExpectHardParseFailureForProto(
564 cat( tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
565 varint(incomplete_submsg.size()),
566 incomplete_submsg ),
567 "PrematureEofInSubmessageValue" + type_name, REQUIRED);
568 }
569 } else if (type != FieldDescriptor::TYPE_GROUP) {
570 // Non-delimited, non-group: eligible for packing.
571
572 // Packed region ends in the middle of a value.
573 ExpectHardParseFailureForProto(
574 cat(tag(rep_field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
575 varint(incomplete.size()), incomplete),
576 "PrematureEofInPackedFieldValue" + type_name, REQUIRED);
577
578 // EOF in the middle of packed region.
579 ExpectParseFailureForProto(
580 cat(tag(rep_field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED),
581 varint(1)),
582 "PrematureEofInPackedField" + type_name, REQUIRED);
583 }
584 }
585
TestValidDataForType(FieldDescriptor::Type type,std::vector<std::pair<std::string,std::string>> values)586 void BinaryAndJsonConformanceSuite::TestValidDataForType(
587 FieldDescriptor::Type type,
588 std::vector<std::pair<std::string, std::string>> values) {
589 for (int is_proto3 = 0; is_proto3 < 2; is_proto3++) {
590 const string type_name =
591 UpperCase(string(".") + FieldDescriptor::TypeName(type));
592 WireFormatLite::WireType wire_type = WireFormatLite::WireTypeForFieldType(
593 static_cast<WireFormatLite::FieldType>(type));
594 const FieldDescriptor* field = GetFieldForType(type, false, is_proto3);
595 const FieldDescriptor* rep_field = GetFieldForType(type, true, is_proto3);
596
597 RunValidProtobufTest("ValidDataScalar" + type_name, REQUIRED,
598 cat(tag(field->number(), wire_type), values[0].first),
599 field->name() + ": " + values[0].second, is_proto3);
600
601 string proto;
602 string text = field->name() + ": " + values.back().second;
603 for (size_t i = 0; i < values.size(); i++) {
604 proto += cat(tag(field->number(), wire_type), values[i].first);
605 }
606 RunValidProtobufTest("RepeatedScalarSelectsLast" + type_name, REQUIRED,
607 proto, text, is_proto3);
608
609 proto.clear();
610 text.clear();
611
612 for (size_t i = 0; i < values.size(); i++) {
613 proto += cat(tag(rep_field->number(), wire_type), values[i].first);
614 text += rep_field->name() + ": " + values[i].second + " ";
615 }
616 RunValidProtobufTest("ValidDataRepeated" + type_name, REQUIRED,
617 proto, text, is_proto3);
618 }
619 }
620
621 // TODO: proto2?
TestIllegalTags()622 void BinaryAndJsonConformanceSuite::TestIllegalTags() {
623 // field num 0 is illegal
624 string nullfield[] = {
625 "\1DEADBEEF",
626 "\2\1\1",
627 "\3\4",
628 "\5DEAD"
629 };
630 for (int i = 0; i < 4; i++) {
631 string name = "IllegalZeroFieldNum_Case_0";
632 name.back() += i;
633 ExpectParseFailureForProto(nullfield[i], name, REQUIRED);
634 }
635 }
636 template <class MessageType>
TestOneofMessage(MessageType & message,bool is_proto3)637 void BinaryAndJsonConformanceSuite::TestOneofMessage (
638 MessageType &message, bool is_proto3) {
639 message.set_oneof_uint32(0);
640 RunValidProtobufTestWithMessage(
641 "OneofZeroUint32", RECOMMENDED, &message, "oneof_uint32: 0", is_proto3);
642 message.mutable_oneof_nested_message()->set_a(0);
643 RunValidProtobufTestWithMessage(
644 "OneofZeroMessage", RECOMMENDED, &message,
645 is_proto3 ? "oneof_nested_message: {}" : "oneof_nested_message: {a: 0}",
646 is_proto3);
647 message.mutable_oneof_nested_message()->set_a(1);
648 RunValidProtobufTestWithMessage(
649 "OneofZeroMessageSetTwice", RECOMMENDED, &message,
650 "oneof_nested_message: {a: 1}",
651 is_proto3);
652 message.set_oneof_string("");
653 RunValidProtobufTestWithMessage(
654 "OneofZeroString", RECOMMENDED, &message, "oneof_string: \"\"", is_proto3);
655 message.set_oneof_bytes("");
656 RunValidProtobufTestWithMessage(
657 "OneofZeroBytes", RECOMMENDED, &message, "oneof_bytes: \"\"", is_proto3);
658 message.set_oneof_bool(false);
659 RunValidProtobufTestWithMessage(
660 "OneofZeroBool", RECOMMENDED, &message, "oneof_bool: false", is_proto3);
661 message.set_oneof_uint64(0);
662 RunValidProtobufTestWithMessage(
663 "OneofZeroUint64", RECOMMENDED, &message, "oneof_uint64: 0", is_proto3);
664 message.set_oneof_float(0.0f);
665 RunValidProtobufTestWithMessage(
666 "OneofZeroFloat", RECOMMENDED, &message, "oneof_float: 0", is_proto3);
667 message.set_oneof_double(0.0);
668 RunValidProtobufTestWithMessage(
669 "OneofZeroDouble", RECOMMENDED, &message, "oneof_double: 0", is_proto3);
670 message.set_oneof_enum(MessageType::FOO);
671 RunValidProtobufTestWithMessage(
672 "OneofZeroEnum", RECOMMENDED, &message, "oneof_enum: FOO", is_proto3);
673 }
674
675 template <class MessageType>
TestUnknownMessage(MessageType & message,bool is_proto3)676 void BinaryAndJsonConformanceSuite::TestUnknownMessage(
677 MessageType& message, bool is_proto3) {
678 message.ParseFromString("\xA8\x1F\x01");
679 RunValidBinaryProtobufTest("UnknownVarint", REQUIRED,
680 message.SerializeAsString(), is_proto3);
681 }
682
RunSuiteImpl()683 void BinaryAndJsonConformanceSuite::RunSuiteImpl() {
684 // Hack to get the list of test failures based on whether
685 // GOOGLE3_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER is enabled or not.
686 conformance::FailureSet failure_set;
687 ConformanceRequest req;
688 ConformanceResponse res;
689 req.set_message_type(failure_set.GetTypeName());
690 req.set_protobuf_payload("");
691 req.set_requested_output_format(conformance::WireFormat::PROTOBUF);
692 RunTest("FindFailures", req, &res);
693 GOOGLE_CHECK(failure_set.MergeFromString(res.protobuf_payload()));
694 for (const string& failure : failure_set.failure()) {
695 AddExpectedFailedTest(failure);
696 }
697
698 type_resolver_.reset(NewTypeResolverForDescriptorPool(
699 kTypeUrlPrefix, DescriptorPool::generated_pool()));
700 type_url_ = GetTypeUrl(TestAllTypesProto3::descriptor());
701
702 for (int i = 1; i <= FieldDescriptor::MAX_TYPE; i++) {
703 if (i == FieldDescriptor::TYPE_GROUP) continue;
704 TestPrematureEOFForType(static_cast<FieldDescriptor::Type>(i));
705 }
706
707 TestIllegalTags();
708
709 int64 kInt64Min = -9223372036854775808ULL;
710 int64 kInt64Max = 9223372036854775807ULL;
711 uint64 kUint64Max = 18446744073709551615ULL;
712 int32 kInt32Max = 2147483647;
713 int32 kInt32Min = -2147483648;
714 uint32 kUint32Max = 4294967295UL;
715
716 TestValidDataForType(FieldDescriptor::TYPE_DOUBLE, {
717 {dbl(0.1), "0.1"},
718 {dbl(1.7976931348623157e+308), "1.7976931348623157e+308"},
719 {dbl(2.22507385850720138309e-308), "2.22507385850720138309e-308"}
720 });
721 TestValidDataForType(FieldDescriptor::TYPE_FLOAT, {
722 {flt(0.1), "0.1"},
723 {flt(1.00000075e-36), "1.00000075e-36"},
724 {flt(3.402823e+38), "3.402823e+38"}, // 3.40282347e+38
725 {flt(1.17549435e-38f), "1.17549435e-38"}
726 });
727 TestValidDataForType(FieldDescriptor::TYPE_INT64, {
728 {varint(12345), "12345"},
729 {varint(kInt64Max), std::to_string(kInt64Max)},
730 {varint(kInt64Min), std::to_string(kInt64Min)}
731 });
732 TestValidDataForType(FieldDescriptor::TYPE_UINT64, {
733 {varint(12345), "12345"},
734 {varint(kUint64Max), std::to_string(kUint64Max)},
735 {varint(0), "0"}
736 });
737 TestValidDataForType(FieldDescriptor::TYPE_INT32, {
738 {varint(12345), "12345"},
739 {longvarint(12345, 2), "12345"},
740 {longvarint(12345, 7), "12345"},
741 {varint(kInt32Max), std::to_string(kInt32Max)},
742 {varint(kInt32Min), std::to_string(kInt32Min)},
743 {varint(1LL << 33), std::to_string(static_cast<int32>(1LL << 33))},
744 {varint((1LL << 33) - 1),
745 std::to_string(static_cast<int32>((1LL << 33) - 1))},
746 });
747 TestValidDataForType(FieldDescriptor::TYPE_UINT32, {
748 {varint(12345), "12345"},
749 {longvarint(12345, 2), "12345"},
750 {longvarint(12345, 7), "12345"},
751 {varint(kUint32Max), std::to_string(kUint32Max)}, // UINT32_MAX
752 {varint(0), "0"},
753 {varint(1LL << 33), std::to_string(static_cast<uint32>(1LL << 33))},
754 {varint((1LL << 33) - 1),
755 std::to_string(static_cast<uint32>((1LL << 33) - 1))},
756 });
757 TestValidDataForType(FieldDescriptor::TYPE_FIXED64, {
758 {u64(12345), "12345"},
759 {u64(kUint64Max), std::to_string(kUint64Max)},
760 {u64(0), "0"}
761 });
762 TestValidDataForType(FieldDescriptor::TYPE_FIXED32, {
763 {u32(12345), "12345"},
764 {u32(kUint32Max), std::to_string(kUint32Max)}, // UINT32_MAX
765 {u32(0), "0"}
766 });
767 TestValidDataForType(FieldDescriptor::TYPE_SFIXED64, {
768 {u64(12345), "12345"},
769 {u64(kInt64Max), std::to_string(kInt64Max)},
770 {u64(kInt64Min), std::to_string(kInt64Min)}
771 });
772 TestValidDataForType(FieldDescriptor::TYPE_SFIXED32, {
773 {u32(12345), "12345"},
774 {u32(kInt32Max), std::to_string(kInt32Max)},
775 {u32(kInt32Min), std::to_string(kInt32Min)}
776 });
777 TestValidDataForType(FieldDescriptor::TYPE_BOOL, {
778 {varint(1), "true"},
779 {varint(0), "false"},
780 {varint(12345678), "true"}
781 });
782 TestValidDataForType(FieldDescriptor::TYPE_SINT32, {
783 {zz32(12345), "12345"},
784 {zz32(kInt32Max), std::to_string(kInt32Max)},
785 {zz32(kInt32Min), std::to_string(kInt32Min)}
786 });
787 TestValidDataForType(FieldDescriptor::TYPE_SINT64, {
788 {zz64(12345), "12345"},
789 {zz64(kInt64Max), std::to_string(kInt64Max)},
790 {zz64(kInt64Min), std::to_string(kInt64Min)}
791 });
792
793 // TODO(haberman):
794 // TestValidDataForType(FieldDescriptor::TYPE_STRING
795 // TestValidDataForType(FieldDescriptor::TYPE_GROUP
796 // TestValidDataForType(FieldDescriptor::TYPE_MESSAGE
797 // TestValidDataForType(FieldDescriptor::TYPE_BYTES
798 // TestValidDataForType(FieldDescriptor::TYPE_ENUM
799
800 RunValidJsonTest("HelloWorld", REQUIRED,
801 "{\"optionalString\":\"Hello, World!\"}",
802 "optional_string: 'Hello, World!'");
803
804 // NOTE: The spec for JSON support is still being sorted out, these may not
805 // all be correct.
806 // Test field name conventions.
807 RunValidJsonTest(
808 "FieldNameInSnakeCase", REQUIRED,
809 R"({
810 "fieldname1": 1,
811 "fieldName2": 2,
812 "FieldName3": 3,
813 "fieldName4": 4
814 })",
815 R"(
816 fieldname1: 1
817 field_name2: 2
818 _field_name3: 3
819 field__name4_: 4
820 )");
821 RunValidJsonTest(
822 "FieldNameWithNumbers", REQUIRED,
823 R"({
824 "field0name5": 5,
825 "field0Name6": 6
826 })",
827 R"(
828 field0name5: 5
829 field_0_name6: 6
830 )");
831 RunValidJsonTest(
832 "FieldNameWithMixedCases", REQUIRED,
833 R"({
834 "fieldName7": 7,
835 "FieldName8": 8,
836 "fieldName9": 9,
837 "FieldName10": 10,
838 "FIELDNAME11": 11,
839 "FIELDName12": 12
840 })",
841 R"(
842 fieldName7: 7
843 FieldName8: 8
844 field_Name9: 9
845 Field_Name10: 10
846 FIELD_NAME11: 11
847 FIELD_name12: 12
848 )");
849 RunValidJsonTest(
850 "FieldNameWithDoubleUnderscores", RECOMMENDED,
851 R"({
852 "FieldName13": 13,
853 "FieldName14": 14,
854 "fieldName15": 15,
855 "fieldName16": 16,
856 "fieldName17": 17,
857 "FieldName18": 18
858 })",
859 R"(
860 __field_name13: 13
861 __Field_name14: 14
862 field__name15: 15
863 field__Name16: 16
864 field_name17__: 17
865 Field_name18__: 18
866 )");
867 // Using the original proto field name in JSON is also allowed.
868 RunValidJsonTest(
869 "OriginalProtoFieldName", REQUIRED,
870 R"({
871 "fieldname1": 1,
872 "field_name2": 2,
873 "_field_name3": 3,
874 "field__name4_": 4,
875 "field0name5": 5,
876 "field_0_name6": 6,
877 "fieldName7": 7,
878 "FieldName8": 8,
879 "field_Name9": 9,
880 "Field_Name10": 10,
881 "FIELD_NAME11": 11,
882 "FIELD_name12": 12,
883 "__field_name13": 13,
884 "__Field_name14": 14,
885 "field__name15": 15,
886 "field__Name16": 16,
887 "field_name17__": 17,
888 "Field_name18__": 18
889 })",
890 R"(
891 fieldname1: 1
892 field_name2: 2
893 _field_name3: 3
894 field__name4_: 4
895 field0name5: 5
896 field_0_name6: 6
897 fieldName7: 7
898 FieldName8: 8
899 field_Name9: 9
900 Field_Name10: 10
901 FIELD_NAME11: 11
902 FIELD_name12: 12
903 __field_name13: 13
904 __Field_name14: 14
905 field__name15: 15
906 field__Name16: 16
907 field_name17__: 17
908 Field_name18__: 18
909 )");
910 // Field names can be escaped.
911 RunValidJsonTest(
912 "FieldNameEscaped", REQUIRED,
913 R"({"fieldn\u0061me1": 1})",
914 "fieldname1: 1");
915 // String ends with escape character.
916 ExpectParseFailureForJson(
917 "StringEndsWithEscapeChar", RECOMMENDED,
918 "{\"optionalString\": \"abc\\");
919 // Field names must be quoted (or it's not valid JSON).
920 ExpectParseFailureForJson(
921 "FieldNameNotQuoted", RECOMMENDED,
922 "{fieldname1: 1}");
923 // Trailing comma is not allowed (not valid JSON).
924 ExpectParseFailureForJson(
925 "TrailingCommaInAnObject", RECOMMENDED,
926 R"({"fieldname1":1,})");
927 ExpectParseFailureForJson(
928 "TrailingCommaInAnObjectWithSpace", RECOMMENDED,
929 R"({"fieldname1":1 ,})");
930 ExpectParseFailureForJson(
931 "TrailingCommaInAnObjectWithSpaceCommaSpace", RECOMMENDED,
932 R"({"fieldname1":1 , })");
933 ExpectParseFailureForJson(
934 "TrailingCommaInAnObjectWithNewlines", RECOMMENDED,
935 R"({
936 "fieldname1":1,
937 })");
938 // JSON doesn't support comments.
939 ExpectParseFailureForJson(
940 "JsonWithComments", RECOMMENDED,
941 R"({
942 // This is a comment.
943 "fieldname1": 1
944 })");
945 // JSON spec says whitespace doesn't matter, so try a few spacings to be sure.
946 RunValidJsonTest(
947 "OneLineNoSpaces", RECOMMENDED,
948 "{\"optionalInt32\":1,\"optionalInt64\":2}",
949 R"(
950 optional_int32: 1
951 optional_int64: 2
952 )");
953 RunValidJsonTest(
954 "OneLineWithSpaces", RECOMMENDED,
955 "{ \"optionalInt32\" : 1 , \"optionalInt64\" : 2 }",
956 R"(
957 optional_int32: 1
958 optional_int64: 2
959 )");
960 RunValidJsonTest(
961 "MultilineNoSpaces", RECOMMENDED,
962 "{\n\"optionalInt32\"\n:\n1\n,\n\"optionalInt64\"\n:\n2\n}",
963 R"(
964 optional_int32: 1
965 optional_int64: 2
966 )");
967 RunValidJsonTest(
968 "MultilineWithSpaces", RECOMMENDED,
969 "{\n \"optionalInt32\" : 1\n ,\n \"optionalInt64\" : 2\n}\n",
970 R"(
971 optional_int32: 1
972 optional_int64: 2
973 )");
974 // Missing comma between key/value pairs.
975 ExpectParseFailureForJson(
976 "MissingCommaOneLine", RECOMMENDED,
977 "{ \"optionalInt32\": 1 \"optionalInt64\": 2 }");
978 ExpectParseFailureForJson(
979 "MissingCommaMultiline", RECOMMENDED,
980 "{\n \"optionalInt32\": 1\n \"optionalInt64\": 2\n}");
981 // Duplicated field names are not allowed.
982 ExpectParseFailureForJson(
983 "FieldNameDuplicate", RECOMMENDED,
984 R"({
985 "optionalNestedMessage": {a: 1},
986 "optionalNestedMessage": {}
987 })");
988 ExpectParseFailureForJson(
989 "FieldNameDuplicateDifferentCasing1", RECOMMENDED,
990 R"({
991 "optional_nested_message": {a: 1},
992 "optionalNestedMessage": {}
993 })");
994 ExpectParseFailureForJson(
995 "FieldNameDuplicateDifferentCasing2", RECOMMENDED,
996 R"({
997 "optionalNestedMessage": {a: 1},
998 "optional_nested_message": {}
999 })");
1000 // Serializers should use lowerCamelCase by default.
1001 RunValidJsonTestWithValidator(
1002 "FieldNameInLowerCamelCase", REQUIRED,
1003 R"({
1004 "fieldname1": 1,
1005 "fieldName2": 2,
1006 "FieldName3": 3,
1007 "fieldName4": 4
1008 })",
1009 [](const Json::Value& value) {
1010 return value.isMember("fieldname1") &&
1011 value.isMember("fieldName2") &&
1012 value.isMember("FieldName3") &&
1013 value.isMember("fieldName4");
1014 });
1015 RunValidJsonTestWithValidator(
1016 "FieldNameWithNumbers", REQUIRED,
1017 R"({
1018 "field0name5": 5,
1019 "field0Name6": 6
1020 })",
1021 [](const Json::Value& value) {
1022 return value.isMember("field0name5") &&
1023 value.isMember("field0Name6");
1024 });
1025 RunValidJsonTestWithValidator(
1026 "FieldNameWithMixedCases", REQUIRED,
1027 R"({
1028 "fieldName7": 7,
1029 "FieldName8": 8,
1030 "fieldName9": 9,
1031 "FieldName10": 10,
1032 "FIELDNAME11": 11,
1033 "FIELDName12": 12
1034 })",
1035 [](const Json::Value& value) {
1036 return value.isMember("fieldName7") &&
1037 value.isMember("FieldName8") &&
1038 value.isMember("fieldName9") &&
1039 value.isMember("FieldName10") &&
1040 value.isMember("FIELDNAME11") &&
1041 value.isMember("FIELDName12");
1042 });
1043 RunValidJsonTestWithValidator(
1044 "FieldNameWithDoubleUnderscores", RECOMMENDED,
1045 R"({
1046 "FieldName13": 13,
1047 "FieldName14": 14,
1048 "fieldName15": 15,
1049 "fieldName16": 16,
1050 "fieldName17": 17,
1051 "FieldName18": 18
1052 })",
1053 [](const Json::Value& value) {
1054 return value.isMember("FieldName13") &&
1055 value.isMember("FieldName14") &&
1056 value.isMember("fieldName15") &&
1057 value.isMember("fieldName16") &&
1058 value.isMember("fieldName17") &&
1059 value.isMember("FieldName18");
1060 });
1061
1062 // Integer fields.
1063 RunValidJsonTest(
1064 "Int32FieldMaxValue", REQUIRED,
1065 R"({"optionalInt32": 2147483647})",
1066 "optional_int32: 2147483647");
1067 RunValidJsonTest(
1068 "Int32FieldMinValue", REQUIRED,
1069 R"({"optionalInt32": -2147483648})",
1070 "optional_int32: -2147483648");
1071 RunValidJsonTest(
1072 "Uint32FieldMaxValue", REQUIRED,
1073 R"({"optionalUint32": 4294967295})",
1074 "optional_uint32: 4294967295");
1075 RunValidJsonTest(
1076 "Int64FieldMaxValue", REQUIRED,
1077 R"({"optionalInt64": "9223372036854775807"})",
1078 "optional_int64: 9223372036854775807");
1079 RunValidJsonTest(
1080 "Int64FieldMinValue", REQUIRED,
1081 R"({"optionalInt64": "-9223372036854775808"})",
1082 "optional_int64: -9223372036854775808");
1083 RunValidJsonTest(
1084 "Uint64FieldMaxValue", REQUIRED,
1085 R"({"optionalUint64": "18446744073709551615"})",
1086 "optional_uint64: 18446744073709551615");
1087 // While not the largest Int64, this is the largest
1088 // Int64 which can be exactly represented within an
1089 // IEEE-754 64-bit float, which is the expected level
1090 // of interoperability guarantee. Larger values may
1091 // work in some implementations, but should not be
1092 // relied upon.
1093 RunValidJsonTest(
1094 "Int64FieldMaxValueNotQuoted", REQUIRED,
1095 R"({"optionalInt64": 9223372036854774784})",
1096 "optional_int64: 9223372036854774784");
1097 RunValidJsonTest(
1098 "Int64FieldMinValueNotQuoted", REQUIRED,
1099 R"({"optionalInt64": -9223372036854775808})",
1100 "optional_int64: -9223372036854775808");
1101 // Largest interoperable Uint64; see comment above
1102 // for Int64FieldMaxValueNotQuoted.
1103 RunValidJsonTest(
1104 "Uint64FieldMaxValueNotQuoted", REQUIRED,
1105 R"({"optionalUint64": 18446744073709549568})",
1106 "optional_uint64: 18446744073709549568");
1107 // Values can be represented as JSON strings.
1108 RunValidJsonTest(
1109 "Int32FieldStringValue", REQUIRED,
1110 R"({"optionalInt32": "2147483647"})",
1111 "optional_int32: 2147483647");
1112 RunValidJsonTest(
1113 "Int32FieldStringValueEscaped", REQUIRED,
1114 R"({"optionalInt32": "2\u003147483647"})",
1115 "optional_int32: 2147483647");
1116
1117 // Parsers reject out-of-bound integer values.
1118 ExpectParseFailureForJson(
1119 "Int32FieldTooLarge", REQUIRED,
1120 R"({"optionalInt32": 2147483648})");
1121 ExpectParseFailureForJson(
1122 "Int32FieldTooSmall", REQUIRED,
1123 R"({"optionalInt32": -2147483649})");
1124 ExpectParseFailureForJson(
1125 "Uint32FieldTooLarge", REQUIRED,
1126 R"({"optionalUint32": 4294967296})");
1127 ExpectParseFailureForJson(
1128 "Int64FieldTooLarge", REQUIRED,
1129 R"({"optionalInt64": "9223372036854775808"})");
1130 ExpectParseFailureForJson(
1131 "Int64FieldTooSmall", REQUIRED,
1132 R"({"optionalInt64": "-9223372036854775809"})");
1133 ExpectParseFailureForJson(
1134 "Uint64FieldTooLarge", REQUIRED,
1135 R"({"optionalUint64": "18446744073709551616"})");
1136 // Parser reject non-integer numeric values as well.
1137 ExpectParseFailureForJson(
1138 "Int32FieldNotInteger", REQUIRED,
1139 R"({"optionalInt32": 0.5})");
1140 ExpectParseFailureForJson(
1141 "Uint32FieldNotInteger", REQUIRED,
1142 R"({"optionalUint32": 0.5})");
1143 ExpectParseFailureForJson(
1144 "Int64FieldNotInteger", REQUIRED,
1145 R"({"optionalInt64": "0.5"})");
1146 ExpectParseFailureForJson(
1147 "Uint64FieldNotInteger", REQUIRED,
1148 R"({"optionalUint64": "0.5"})");
1149
1150 // Integers but represented as float values are accepted.
1151 RunValidJsonTest(
1152 "Int32FieldFloatTrailingZero", REQUIRED,
1153 R"({"optionalInt32": 100000.000})",
1154 "optional_int32: 100000");
1155 RunValidJsonTest(
1156 "Int32FieldExponentialFormat", REQUIRED,
1157 R"({"optionalInt32": 1e5})",
1158 "optional_int32: 100000");
1159 RunValidJsonTest(
1160 "Int32FieldMaxFloatValue", REQUIRED,
1161 R"({"optionalInt32": 2.147483647e9})",
1162 "optional_int32: 2147483647");
1163 RunValidJsonTest(
1164 "Int32FieldMinFloatValue", REQUIRED,
1165 R"({"optionalInt32": -2.147483648e9})",
1166 "optional_int32: -2147483648");
1167 RunValidJsonTest(
1168 "Uint32FieldMaxFloatValue", REQUIRED,
1169 R"({"optionalUint32": 4.294967295e9})",
1170 "optional_uint32: 4294967295");
1171
1172 // Parser reject non-numeric values.
1173 ExpectParseFailureForJson(
1174 "Int32FieldNotNumber", REQUIRED,
1175 R"({"optionalInt32": "3x3"})");
1176 ExpectParseFailureForJson(
1177 "Uint32FieldNotNumber", REQUIRED,
1178 R"({"optionalUint32": "3x3"})");
1179 ExpectParseFailureForJson(
1180 "Int64FieldNotNumber", REQUIRED,
1181 R"({"optionalInt64": "3x3"})");
1182 ExpectParseFailureForJson(
1183 "Uint64FieldNotNumber", REQUIRED,
1184 R"({"optionalUint64": "3x3"})");
1185 // JSON does not allow "+" on numric values.
1186 ExpectParseFailureForJson(
1187 "Int32FieldPlusSign", REQUIRED,
1188 R"({"optionalInt32": +1})");
1189 // JSON doesn't allow leading 0s.
1190 ExpectParseFailureForJson(
1191 "Int32FieldLeadingZero", REQUIRED,
1192 R"({"optionalInt32": 01})");
1193 ExpectParseFailureForJson(
1194 "Int32FieldNegativeWithLeadingZero", REQUIRED,
1195 R"({"optionalInt32": -01})");
1196 // String values must follow the same syntax rule. Specifically leading
1197 // or trailing spaces are not allowed.
1198 ExpectParseFailureForJson(
1199 "Int32FieldLeadingSpace", REQUIRED,
1200 R"({"optionalInt32": " 1"})");
1201 ExpectParseFailureForJson(
1202 "Int32FieldTrailingSpace", REQUIRED,
1203 R"({"optionalInt32": "1 "})");
1204
1205 // 64-bit values are serialized as strings.
1206 RunValidJsonTestWithValidator(
1207 "Int64FieldBeString", RECOMMENDED,
1208 R"({"optionalInt64": 1})",
1209 [](const Json::Value& value) {
1210 return value["optionalInt64"].type() == Json::stringValue &&
1211 value["optionalInt64"].asString() == "1";
1212 });
1213 RunValidJsonTestWithValidator(
1214 "Uint64FieldBeString", RECOMMENDED,
1215 R"({"optionalUint64": 1})",
1216 [](const Json::Value& value) {
1217 return value["optionalUint64"].type() == Json::stringValue &&
1218 value["optionalUint64"].asString() == "1";
1219 });
1220
1221 // Bool fields.
1222 RunValidJsonTest(
1223 "BoolFieldTrue", REQUIRED,
1224 R"({"optionalBool":true})",
1225 "optional_bool: true");
1226 RunValidJsonTest(
1227 "BoolFieldFalse", REQUIRED,
1228 R"({"optionalBool":false})",
1229 "optional_bool: false");
1230
1231 // Other forms are not allowed.
1232 ExpectParseFailureForJson(
1233 "BoolFieldIntegerZero", RECOMMENDED,
1234 R"({"optionalBool":0})");
1235 ExpectParseFailureForJson(
1236 "BoolFieldIntegerOne", RECOMMENDED,
1237 R"({"optionalBool":1})");
1238 ExpectParseFailureForJson(
1239 "BoolFieldCamelCaseTrue", RECOMMENDED,
1240 R"({"optionalBool":True})");
1241 ExpectParseFailureForJson(
1242 "BoolFieldCamelCaseFalse", RECOMMENDED,
1243 R"({"optionalBool":False})");
1244 ExpectParseFailureForJson(
1245 "BoolFieldAllCapitalTrue", RECOMMENDED,
1246 R"({"optionalBool":TRUE})");
1247 ExpectParseFailureForJson(
1248 "BoolFieldAllCapitalFalse", RECOMMENDED,
1249 R"({"optionalBool":FALSE})");
1250 ExpectParseFailureForJson(
1251 "BoolFieldDoubleQuotedTrue", RECOMMENDED,
1252 R"({"optionalBool":"true"})");
1253 ExpectParseFailureForJson(
1254 "BoolFieldDoubleQuotedFalse", RECOMMENDED,
1255 R"({"optionalBool":"false"})");
1256
1257 // Float fields.
1258 RunValidJsonTest(
1259 "FloatFieldMinPositiveValue", REQUIRED,
1260 R"({"optionalFloat": 1.175494e-38})",
1261 "optional_float: 1.175494e-38");
1262 RunValidJsonTest(
1263 "FloatFieldMaxNegativeValue", REQUIRED,
1264 R"({"optionalFloat": -1.175494e-38})",
1265 "optional_float: -1.175494e-38");
1266 RunValidJsonTest(
1267 "FloatFieldMaxPositiveValue", REQUIRED,
1268 R"({"optionalFloat": 3.402823e+38})",
1269 "optional_float: 3.402823e+38");
1270 RunValidJsonTest(
1271 "FloatFieldMinNegativeValue", REQUIRED,
1272 R"({"optionalFloat": 3.402823e+38})",
1273 "optional_float: 3.402823e+38");
1274 // Values can be quoted.
1275 RunValidJsonTest(
1276 "FloatFieldQuotedValue", REQUIRED,
1277 R"({"optionalFloat": "1"})",
1278 "optional_float: 1");
1279 // Special values.
1280 RunValidJsonTest(
1281 "FloatFieldNan", REQUIRED,
1282 R"({"optionalFloat": "NaN"})",
1283 "optional_float: nan");
1284 RunValidJsonTest(
1285 "FloatFieldInfinity", REQUIRED,
1286 R"({"optionalFloat": "Infinity"})",
1287 "optional_float: inf");
1288 RunValidJsonTest(
1289 "FloatFieldNegativeInfinity", REQUIRED,
1290 R"({"optionalFloat": "-Infinity"})",
1291 "optional_float: -inf");
1292 // Non-cannonical Nan will be correctly normalized.
1293 {
1294 TestAllTypesProto3 message;
1295 // IEEE floating-point standard 32-bit quiet NaN:
1296 // 0111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
1297 message.set_optional_float(
1298 WireFormatLite::DecodeFloat(0x7FA12345));
1299 RunValidJsonTestWithProtobufInput(
1300 "FloatFieldNormalizeQuietNan", REQUIRED, message,
1301 "optional_float: nan");
1302 // IEEE floating-point standard 64-bit signaling NaN:
1303 // 1111 1111 1xxx xxxx xxxx xxxx xxxx xxxx
1304 message.set_optional_float(
1305 WireFormatLite::DecodeFloat(0xFFB54321));
1306 RunValidJsonTestWithProtobufInput(
1307 "FloatFieldNormalizeSignalingNan", REQUIRED, message,
1308 "optional_float: nan");
1309 }
1310
1311 // Special values must be quoted.
1312 ExpectParseFailureForJson(
1313 "FloatFieldNanNotQuoted", RECOMMENDED,
1314 R"({"optionalFloat": NaN})");
1315 ExpectParseFailureForJson(
1316 "FloatFieldInfinityNotQuoted", RECOMMENDED,
1317 R"({"optionalFloat": Infinity})");
1318 ExpectParseFailureForJson(
1319 "FloatFieldNegativeInfinityNotQuoted", RECOMMENDED,
1320 R"({"optionalFloat": -Infinity})");
1321 // Parsers should reject out-of-bound values.
1322 ExpectParseFailureForJson(
1323 "FloatFieldTooSmall", REQUIRED,
1324 R"({"optionalFloat": -3.502823e+38})");
1325 ExpectParseFailureForJson(
1326 "FloatFieldTooLarge", REQUIRED,
1327 R"({"optionalFloat": 3.502823e+38})");
1328
1329 // Double fields.
1330 RunValidJsonTest(
1331 "DoubleFieldMinPositiveValue", REQUIRED,
1332 R"({"optionalDouble": 2.22507e-308})",
1333 "optional_double: 2.22507e-308");
1334 RunValidJsonTest(
1335 "DoubleFieldMaxNegativeValue", REQUIRED,
1336 R"({"optionalDouble": -2.22507e-308})",
1337 "optional_double: -2.22507e-308");
1338 RunValidJsonTest(
1339 "DoubleFieldMaxPositiveValue", REQUIRED,
1340 R"({"optionalDouble": 1.79769e+308})",
1341 "optional_double: 1.79769e+308");
1342 RunValidJsonTest(
1343 "DoubleFieldMinNegativeValue", REQUIRED,
1344 R"({"optionalDouble": -1.79769e+308})",
1345 "optional_double: -1.79769e+308");
1346 // Values can be quoted.
1347 RunValidJsonTest(
1348 "DoubleFieldQuotedValue", REQUIRED,
1349 R"({"optionalDouble": "1"})",
1350 "optional_double: 1");
1351 // Speical values.
1352 RunValidJsonTest(
1353 "DoubleFieldNan", REQUIRED,
1354 R"({"optionalDouble": "NaN"})",
1355 "optional_double: nan");
1356 RunValidJsonTest(
1357 "DoubleFieldInfinity", REQUIRED,
1358 R"({"optionalDouble": "Infinity"})",
1359 "optional_double: inf");
1360 RunValidJsonTest(
1361 "DoubleFieldNegativeInfinity", REQUIRED,
1362 R"({"optionalDouble": "-Infinity"})",
1363 "optional_double: -inf");
1364 // Non-cannonical Nan will be correctly normalized.
1365 {
1366 TestAllTypesProto3 message;
1367 message.set_optional_double(
1368 WireFormatLite::DecodeDouble(0x7FFA123456789ABCLL));
1369 RunValidJsonTestWithProtobufInput(
1370 "DoubleFieldNormalizeQuietNan", REQUIRED, message,
1371 "optional_double: nan");
1372 message.set_optional_double(
1373 WireFormatLite::DecodeDouble(0xFFFBCBA987654321LL));
1374 RunValidJsonTestWithProtobufInput(
1375 "DoubleFieldNormalizeSignalingNan", REQUIRED, message,
1376 "optional_double: nan");
1377 }
1378
1379 // Special values must be quoted.
1380 ExpectParseFailureForJson(
1381 "DoubleFieldNanNotQuoted", RECOMMENDED,
1382 R"({"optionalDouble": NaN})");
1383 ExpectParseFailureForJson(
1384 "DoubleFieldInfinityNotQuoted", RECOMMENDED,
1385 R"({"optionalDouble": Infinity})");
1386 ExpectParseFailureForJson(
1387 "DoubleFieldNegativeInfinityNotQuoted", RECOMMENDED,
1388 R"({"optionalDouble": -Infinity})");
1389
1390 // Parsers should reject out-of-bound values.
1391 ExpectParseFailureForJson(
1392 "DoubleFieldTooSmall", REQUIRED,
1393 R"({"optionalDouble": -1.89769e+308})");
1394 ExpectParseFailureForJson(
1395 "DoubleFieldTooLarge", REQUIRED,
1396 R"({"optionalDouble": +1.89769e+308})");
1397
1398 // Enum fields.
1399 RunValidJsonTest(
1400 "EnumField", REQUIRED,
1401 R"({"optionalNestedEnum": "FOO"})",
1402 "optional_nested_enum: FOO");
1403 // Enum fields with alias
1404 RunValidJsonTest(
1405 "EnumFieldWithAlias", REQUIRED,
1406 R"({"optionalAliasedEnum": "ALIAS_BAZ"})",
1407 "optional_aliased_enum: ALIAS_BAZ");
1408 RunValidJsonTest(
1409 "EnumFieldWithAliasUseAlias", REQUIRED,
1410 R"({"optionalAliasedEnum": "QUX"})",
1411 "optional_aliased_enum: ALIAS_BAZ");
1412 RunValidJsonTest(
1413 "EnumFieldWithAliasLowerCase", REQUIRED,
1414 R"({"optionalAliasedEnum": "qux"})",
1415 "optional_aliased_enum: ALIAS_BAZ");
1416 RunValidJsonTest(
1417 "EnumFieldWithAliasDifferentCase", REQUIRED,
1418 R"({"optionalAliasedEnum": "bAz"})",
1419 "optional_aliased_enum: ALIAS_BAZ");
1420 // Enum values must be represented as strings.
1421 ExpectParseFailureForJson(
1422 "EnumFieldNotQuoted", REQUIRED,
1423 R"({"optionalNestedEnum": FOO})");
1424 // Numeric values are allowed.
1425 RunValidJsonTest(
1426 "EnumFieldNumericValueZero", REQUIRED,
1427 R"({"optionalNestedEnum": 0})",
1428 "optional_nested_enum: FOO");
1429 RunValidJsonTest(
1430 "EnumFieldNumericValueNonZero", REQUIRED,
1431 R"({"optionalNestedEnum": 1})",
1432 "optional_nested_enum: BAR");
1433 // Unknown enum values are represented as numeric values.
1434 RunValidJsonTestWithValidator(
1435 "EnumFieldUnknownValue", REQUIRED,
1436 R"({"optionalNestedEnum": 123})",
1437 [](const Json::Value& value) {
1438 return value["optionalNestedEnum"].type() == Json::intValue &&
1439 value["optionalNestedEnum"].asInt() == 123;
1440 });
1441
1442 // String fields.
1443 RunValidJsonTest(
1444 "StringField", REQUIRED,
1445 R"({"optionalString": "Hello world!"})",
1446 "optional_string: \"Hello world!\"");
1447 RunValidJsonTest(
1448 "StringFieldUnicode", REQUIRED,
1449 // Google in Chinese.
1450 R"({"optionalString": "谷歌"})",
1451 R"(optional_string: "谷歌")");
1452 RunValidJsonTest(
1453 "StringFieldEscape", REQUIRED,
1454 R"({"optionalString": "\"\\\/\b\f\n\r\t"})",
1455 R"(optional_string: "\"\\/\b\f\n\r\t")");
1456 RunValidJsonTest(
1457 "StringFieldUnicodeEscape", REQUIRED,
1458 R"({"optionalString": "\u8C37\u6B4C"})",
1459 R"(optional_string: "谷歌")");
1460 RunValidJsonTest(
1461 "StringFieldUnicodeEscapeWithLowercaseHexLetters", REQUIRED,
1462 R"({"optionalString": "\u8c37\u6b4c"})",
1463 R"(optional_string: "谷歌")");
1464 RunValidJsonTest(
1465 "StringFieldSurrogatePair", REQUIRED,
1466 // The character is an emoji: grinning face with smiling eyes.
1467 R"({"optionalString": "\uD83D\uDE01"})",
1468 R"(optional_string: "\xF0\x9F\x98\x81")");
1469
1470 // Unicode escapes must start with "\u" (lowercase u).
1471 ExpectParseFailureForJson(
1472 "StringFieldUppercaseEscapeLetter", RECOMMENDED,
1473 R"({"optionalString": "\U8C37\U6b4C"})");
1474 ExpectParseFailureForJson(
1475 "StringFieldInvalidEscape", RECOMMENDED,
1476 R"({"optionalString": "\uXXXX\u6B4C"})");
1477 ExpectParseFailureForJson(
1478 "StringFieldUnterminatedEscape", RECOMMENDED,
1479 R"({"optionalString": "\u8C3"})");
1480 ExpectParseFailureForJson(
1481 "StringFieldUnpairedHighSurrogate", RECOMMENDED,
1482 R"({"optionalString": "\uD800"})");
1483 ExpectParseFailureForJson(
1484 "StringFieldUnpairedLowSurrogate", RECOMMENDED,
1485 R"({"optionalString": "\uDC00"})");
1486 ExpectParseFailureForJson(
1487 "StringFieldSurrogateInWrongOrder", RECOMMENDED,
1488 R"({"optionalString": "\uDE01\uD83D"})");
1489 ExpectParseFailureForJson(
1490 "StringFieldNotAString", REQUIRED,
1491 R"({"optionalString": 12345})");
1492
1493 // Bytes fields.
1494 RunValidJsonTest(
1495 "BytesField", REQUIRED,
1496 R"({"optionalBytes": "AQI="})",
1497 R"(optional_bytes: "\x01\x02")");
1498 RunValidJsonTest(
1499 "BytesFieldBase64Url", RECOMMENDED,
1500 R"({"optionalBytes": "-_"})",
1501 R"(optional_bytes: "\xfb")");
1502
1503 // Message fields.
1504 RunValidJsonTest(
1505 "MessageField", REQUIRED,
1506 R"({"optionalNestedMessage": {"a": 1234}})",
1507 "optional_nested_message: {a: 1234}");
1508
1509 // Oneof fields.
1510 ExpectParseFailureForJson(
1511 "OneofFieldDuplicate", REQUIRED,
1512 R"({"oneofUint32": 1, "oneofString": "test"})");
1513 // Ensure zero values for oneof make it out/backs.
1514 TestAllTypesProto3 messageProto3;
1515 TestAllTypesProto2 messageProto2;
1516 TestOneofMessage(messageProto3, true);
1517 TestOneofMessage(messageProto2, false);
1518 RunValidJsonTest(
1519 "OneofZeroUint32", RECOMMENDED,
1520 R"({"oneofUint32": 0})", "oneof_uint32: 0");
1521 RunValidJsonTest(
1522 "OneofZeroMessage", RECOMMENDED,
1523 R"({"oneofNestedMessage": {}})", "oneof_nested_message: {}");
1524 RunValidJsonTest(
1525 "OneofZeroString", RECOMMENDED,
1526 R"({"oneofString": ""})", "oneof_string: \"\"");
1527 RunValidJsonTest(
1528 "OneofZeroBytes", RECOMMENDED,
1529 R"({"oneofBytes": ""})", "oneof_bytes: \"\"");
1530 RunValidJsonTest(
1531 "OneofZeroBool", RECOMMENDED,
1532 R"({"oneofBool": false})", "oneof_bool: false");
1533 RunValidJsonTest(
1534 "OneofZeroUint64", RECOMMENDED,
1535 R"({"oneofUint64": 0})", "oneof_uint64: 0");
1536 RunValidJsonTest(
1537 "OneofZeroFloat", RECOMMENDED,
1538 R"({"oneofFloat": 0.0})", "oneof_float: 0");
1539 RunValidJsonTest(
1540 "OneofZeroDouble", RECOMMENDED,
1541 R"({"oneofDouble": 0.0})", "oneof_double: 0");
1542 RunValidJsonTest(
1543 "OneofZeroEnum", RECOMMENDED,
1544 R"({"oneofEnum":"FOO"})", "oneof_enum: FOO");
1545
1546 // Repeated fields.
1547 RunValidJsonTest(
1548 "PrimitiveRepeatedField", REQUIRED,
1549 R"({"repeatedInt32": [1, 2, 3, 4]})",
1550 "repeated_int32: [1, 2, 3, 4]");
1551 RunValidJsonTest(
1552 "EnumRepeatedField", REQUIRED,
1553 R"({"repeatedNestedEnum": ["FOO", "BAR", "BAZ"]})",
1554 "repeated_nested_enum: [FOO, BAR, BAZ]");
1555 RunValidJsonTest(
1556 "StringRepeatedField", REQUIRED,
1557 R"({"repeatedString": ["Hello", "world"]})",
1558 R"(repeated_string: ["Hello", "world"])");
1559 RunValidJsonTest(
1560 "BytesRepeatedField", REQUIRED,
1561 R"({"repeatedBytes": ["AAEC", "AQI="]})",
1562 R"(repeated_bytes: ["\x00\x01\x02", "\x01\x02"])");
1563 RunValidJsonTest(
1564 "MessageRepeatedField", REQUIRED,
1565 R"({"repeatedNestedMessage": [{"a": 1234}, {"a": 5678}]})",
1566 "repeated_nested_message: {a: 1234}"
1567 "repeated_nested_message: {a: 5678}");
1568
1569 // Repeated field elements are of incorrect type.
1570 ExpectParseFailureForJson(
1571 "RepeatedFieldWrongElementTypeExpectingIntegersGotBool", REQUIRED,
1572 R"({"repeatedInt32": [1, false, 3, 4]})");
1573 ExpectParseFailureForJson(
1574 "RepeatedFieldWrongElementTypeExpectingIntegersGotString", REQUIRED,
1575 R"({"repeatedInt32": [1, 2, "name", 4]})");
1576 ExpectParseFailureForJson(
1577 "RepeatedFieldWrongElementTypeExpectingIntegersGotMessage", REQUIRED,
1578 R"({"repeatedInt32": [1, 2, 3, {"a": 4}]})");
1579 ExpectParseFailureForJson(
1580 "RepeatedFieldWrongElementTypeExpectingStringsGotInt", REQUIRED,
1581 R"({"repeatedString": ["1", 2, "3", "4"]})");
1582 ExpectParseFailureForJson(
1583 "RepeatedFieldWrongElementTypeExpectingStringsGotBool", REQUIRED,
1584 R"({"repeatedString": ["1", "2", false, "4"]})");
1585 ExpectParseFailureForJson(
1586 "RepeatedFieldWrongElementTypeExpectingStringsGotMessage", REQUIRED,
1587 R"({"repeatedString": ["1", 2, "3", {"a": 4}]})");
1588 ExpectParseFailureForJson(
1589 "RepeatedFieldWrongElementTypeExpectingMessagesGotInt", REQUIRED,
1590 R"({"repeatedNestedMessage": [{"a": 1}, 2]})");
1591 ExpectParseFailureForJson(
1592 "RepeatedFieldWrongElementTypeExpectingMessagesGotBool", REQUIRED,
1593 R"({"repeatedNestedMessage": [{"a": 1}, false]})");
1594 ExpectParseFailureForJson(
1595 "RepeatedFieldWrongElementTypeExpectingMessagesGotString", REQUIRED,
1596 R"({"repeatedNestedMessage": [{"a": 1}, "2"]})");
1597 // Trailing comma in the repeated field is not allowed.
1598 ExpectParseFailureForJson(
1599 "RepeatedFieldTrailingComma", RECOMMENDED,
1600 R"({"repeatedInt32": [1, 2, 3, 4,]})");
1601 ExpectParseFailureForJson(
1602 "RepeatedFieldTrailingCommaWithSpace", RECOMMENDED,
1603 "{\"repeatedInt32\": [1, 2, 3, 4 ,]}");
1604 ExpectParseFailureForJson(
1605 "RepeatedFieldTrailingCommaWithSpaceCommaSpace", RECOMMENDED,
1606 "{\"repeatedInt32\": [1, 2, 3, 4 , ]}");
1607 ExpectParseFailureForJson(
1608 "RepeatedFieldTrailingCommaWithNewlines", RECOMMENDED,
1609 "{\"repeatedInt32\": [\n 1,\n 2,\n 3,\n 4,\n]}");
1610
1611 // Map fields.
1612 RunValidJsonTest(
1613 "Int32MapField", REQUIRED,
1614 R"({"mapInt32Int32": {"1": 2, "3": 4}})",
1615 "map_int32_int32: {key: 1 value: 2}"
1616 "map_int32_int32: {key: 3 value: 4}");
1617 ExpectParseFailureForJson(
1618 "Int32MapFieldKeyNotQuoted", RECOMMENDED,
1619 R"({"mapInt32Int32": {1: 2, 3: 4}})");
1620 RunValidJsonTest(
1621 "Uint32MapField", REQUIRED,
1622 R"({"mapUint32Uint32": {"1": 2, "3": 4}})",
1623 "map_uint32_uint32: {key: 1 value: 2}"
1624 "map_uint32_uint32: {key: 3 value: 4}");
1625 ExpectParseFailureForJson(
1626 "Uint32MapFieldKeyNotQuoted", RECOMMENDED,
1627 R"({"mapUint32Uint32": {1: 2, 3: 4}})");
1628 RunValidJsonTest(
1629 "Int64MapField", REQUIRED,
1630 R"({"mapInt64Int64": {"1": 2, "3": 4}})",
1631 "map_int64_int64: {key: 1 value: 2}"
1632 "map_int64_int64: {key: 3 value: 4}");
1633 ExpectParseFailureForJson(
1634 "Int64MapFieldKeyNotQuoted", RECOMMENDED,
1635 R"({"mapInt64Int64": {1: 2, 3: 4}})");
1636 RunValidJsonTest(
1637 "Uint64MapField", REQUIRED,
1638 R"({"mapUint64Uint64": {"1": 2, "3": 4}})",
1639 "map_uint64_uint64: {key: 1 value: 2}"
1640 "map_uint64_uint64: {key: 3 value: 4}");
1641 ExpectParseFailureForJson(
1642 "Uint64MapFieldKeyNotQuoted", RECOMMENDED,
1643 R"({"mapUint64Uint64": {1: 2, 3: 4}})");
1644 RunValidJsonTest(
1645 "BoolMapField", REQUIRED,
1646 R"({"mapBoolBool": {"true": true, "false": false}})",
1647 "map_bool_bool: {key: true value: true}"
1648 "map_bool_bool: {key: false value: false}");
1649 ExpectParseFailureForJson(
1650 "BoolMapFieldKeyNotQuoted", RECOMMENDED,
1651 R"({"mapBoolBool": {true: true, false: false}})");
1652 RunValidJsonTest(
1653 "MessageMapField", REQUIRED,
1654 R"({
1655 "mapStringNestedMessage": {
1656 "hello": {"a": 1234},
1657 "world": {"a": 5678}
1658 }
1659 })",
1660 R"(
1661 map_string_nested_message: {
1662 key: "hello"
1663 value: {a: 1234}
1664 }
1665 map_string_nested_message: {
1666 key: "world"
1667 value: {a: 5678}
1668 }
1669 )");
1670 // Since Map keys are represented as JSON strings, escaping should be allowed.
1671 RunValidJsonTest(
1672 "Int32MapEscapedKey", REQUIRED,
1673 R"({"mapInt32Int32": {"\u0031": 2}})",
1674 "map_int32_int32: {key: 1 value: 2}");
1675 RunValidJsonTest(
1676 "Int64MapEscapedKey", REQUIRED,
1677 R"({"mapInt64Int64": {"\u0031": 2}})",
1678 "map_int64_int64: {key: 1 value: 2}");
1679 RunValidJsonTest(
1680 "BoolMapEscapedKey", REQUIRED,
1681 R"({"mapBoolBool": {"tr\u0075e": true}})",
1682 "map_bool_bool: {key: true value: true}");
1683
1684 // "null" is accepted for all fields types.
1685 RunValidJsonTest(
1686 "AllFieldAcceptNull", REQUIRED,
1687 R"({
1688 "optionalInt32": null,
1689 "optionalInt64": null,
1690 "optionalUint32": null,
1691 "optionalUint64": null,
1692 "optionalSint32": null,
1693 "optionalSint64": null,
1694 "optionalFixed32": null,
1695 "optionalFixed64": null,
1696 "optionalSfixed32": null,
1697 "optionalSfixed64": null,
1698 "optionalFloat": null,
1699 "optionalDouble": null,
1700 "optionalBool": null,
1701 "optionalString": null,
1702 "optionalBytes": null,
1703 "optionalNestedEnum": null,
1704 "optionalNestedMessage": null,
1705 "repeatedInt32": null,
1706 "repeatedInt64": null,
1707 "repeatedUint32": null,
1708 "repeatedUint64": null,
1709 "repeatedSint32": null,
1710 "repeatedSint64": null,
1711 "repeatedFixed32": null,
1712 "repeatedFixed64": null,
1713 "repeatedSfixed32": null,
1714 "repeatedSfixed64": null,
1715 "repeatedFloat": null,
1716 "repeatedDouble": null,
1717 "repeatedBool": null,
1718 "repeatedString": null,
1719 "repeatedBytes": null,
1720 "repeatedNestedEnum": null,
1721 "repeatedNestedMessage": null,
1722 "mapInt32Int32": null,
1723 "mapBoolBool": null,
1724 "mapStringNestedMessage": null
1725 })",
1726 "");
1727
1728 // Repeated field elements cannot be null.
1729 ExpectParseFailureForJson(
1730 "RepeatedFieldPrimitiveElementIsNull", RECOMMENDED,
1731 R"({"repeatedInt32": [1, null, 2]})");
1732 ExpectParseFailureForJson(
1733 "RepeatedFieldMessageElementIsNull", RECOMMENDED,
1734 R"({"repeatedNestedMessage": [{"a":1}, null, {"a":2}]})");
1735 // Map field keys cannot be null.
1736 ExpectParseFailureForJson(
1737 "MapFieldKeyIsNull", RECOMMENDED,
1738 R"({"mapInt32Int32": {null: 1}})");
1739 // Map field values cannot be null.
1740 ExpectParseFailureForJson(
1741 "MapFieldValueIsNull", RECOMMENDED,
1742 R"({"mapInt32Int32": {"0": null}})");
1743
1744 // http://www.rfc-editor.org/rfc/rfc7159.txt says strings have to use double
1745 // quotes.
1746 ExpectParseFailureForJson(
1747 "StringFieldSingleQuoteKey", RECOMMENDED,
1748 R"({'optionalString': "Hello world!"})");
1749 ExpectParseFailureForJson(
1750 "StringFieldSingleQuoteValue", RECOMMENDED,
1751 R"({"optionalString": 'Hello world!'})");
1752 ExpectParseFailureForJson(
1753 "StringFieldSingleQuoteBoth", RECOMMENDED,
1754 R"({'optionalString': 'Hello world!'})");
1755
1756 // Unknown fields.
1757 {
1758 TestAllTypesProto3 messageProto3;
1759 TestAllTypesProto2 messageProto2;
1760 //TODO(yilunchong): update this behavior when unknown field's behavior
1761 // changed in open source. Also delete
1762 // Required.Proto3.ProtobufInput.UnknownVarint.ProtobufOutput
1763 // from failure list of python_cpp python java
1764 TestUnknownMessage(messageProto3, true);
1765 TestUnknownMessage(messageProto2, false);
1766 }
1767
1768 // Wrapper types.
1769 RunValidJsonTest(
1770 "OptionalBoolWrapper", REQUIRED,
1771 R"({"optionalBoolWrapper": false})",
1772 "optional_bool_wrapper: {value: false}");
1773 RunValidJsonTest(
1774 "OptionalInt32Wrapper", REQUIRED,
1775 R"({"optionalInt32Wrapper": 0})",
1776 "optional_int32_wrapper: {value: 0}");
1777 RunValidJsonTest(
1778 "OptionalUint32Wrapper", REQUIRED,
1779 R"({"optionalUint32Wrapper": 0})",
1780 "optional_uint32_wrapper: {value: 0}");
1781 RunValidJsonTest(
1782 "OptionalInt64Wrapper", REQUIRED,
1783 R"({"optionalInt64Wrapper": 0})",
1784 "optional_int64_wrapper: {value: 0}");
1785 RunValidJsonTest(
1786 "OptionalUint64Wrapper", REQUIRED,
1787 R"({"optionalUint64Wrapper": 0})",
1788 "optional_uint64_wrapper: {value: 0}");
1789 RunValidJsonTest(
1790 "OptionalFloatWrapper", REQUIRED,
1791 R"({"optionalFloatWrapper": 0})",
1792 "optional_float_wrapper: {value: 0}");
1793 RunValidJsonTest(
1794 "OptionalDoubleWrapper", REQUIRED,
1795 R"({"optionalDoubleWrapper": 0})",
1796 "optional_double_wrapper: {value: 0}");
1797 RunValidJsonTest(
1798 "OptionalStringWrapper", REQUIRED,
1799 R"({"optionalStringWrapper": ""})",
1800 R"(optional_string_wrapper: {value: ""})");
1801 RunValidJsonTest(
1802 "OptionalBytesWrapper", REQUIRED,
1803 R"({"optionalBytesWrapper": ""})",
1804 R"(optional_bytes_wrapper: {value: ""})");
1805 RunValidJsonTest(
1806 "OptionalWrapperTypesWithNonDefaultValue", REQUIRED,
1807 R"({
1808 "optionalBoolWrapper": true,
1809 "optionalInt32Wrapper": 1,
1810 "optionalUint32Wrapper": 1,
1811 "optionalInt64Wrapper": "1",
1812 "optionalUint64Wrapper": "1",
1813 "optionalFloatWrapper": 1,
1814 "optionalDoubleWrapper": 1,
1815 "optionalStringWrapper": "1",
1816 "optionalBytesWrapper": "AQI="
1817 })",
1818 R"(
1819 optional_bool_wrapper: {value: true}
1820 optional_int32_wrapper: {value: 1}
1821 optional_uint32_wrapper: {value: 1}
1822 optional_int64_wrapper: {value: 1}
1823 optional_uint64_wrapper: {value: 1}
1824 optional_float_wrapper: {value: 1}
1825 optional_double_wrapper: {value: 1}
1826 optional_string_wrapper: {value: "1"}
1827 optional_bytes_wrapper: {value: "\x01\x02"}
1828 )");
1829 RunValidJsonTest(
1830 "RepeatedBoolWrapper", REQUIRED,
1831 R"({"repeatedBoolWrapper": [true, false]})",
1832 "repeated_bool_wrapper: {value: true}"
1833 "repeated_bool_wrapper: {value: false}");
1834 RunValidJsonTest(
1835 "RepeatedInt32Wrapper", REQUIRED,
1836 R"({"repeatedInt32Wrapper": [0, 1]})",
1837 "repeated_int32_wrapper: {value: 0}"
1838 "repeated_int32_wrapper: {value: 1}");
1839 RunValidJsonTest(
1840 "RepeatedUint32Wrapper", REQUIRED,
1841 R"({"repeatedUint32Wrapper": [0, 1]})",
1842 "repeated_uint32_wrapper: {value: 0}"
1843 "repeated_uint32_wrapper: {value: 1}");
1844 RunValidJsonTest(
1845 "RepeatedInt64Wrapper", REQUIRED,
1846 R"({"repeatedInt64Wrapper": [0, 1]})",
1847 "repeated_int64_wrapper: {value: 0}"
1848 "repeated_int64_wrapper: {value: 1}");
1849 RunValidJsonTest(
1850 "RepeatedUint64Wrapper", REQUIRED,
1851 R"({"repeatedUint64Wrapper": [0, 1]})",
1852 "repeated_uint64_wrapper: {value: 0}"
1853 "repeated_uint64_wrapper: {value: 1}");
1854 RunValidJsonTest(
1855 "RepeatedFloatWrapper", REQUIRED,
1856 R"({"repeatedFloatWrapper": [0, 1]})",
1857 "repeated_float_wrapper: {value: 0}"
1858 "repeated_float_wrapper: {value: 1}");
1859 RunValidJsonTest(
1860 "RepeatedDoubleWrapper", REQUIRED,
1861 R"({"repeatedDoubleWrapper": [0, 1]})",
1862 "repeated_double_wrapper: {value: 0}"
1863 "repeated_double_wrapper: {value: 1}");
1864 RunValidJsonTest(
1865 "RepeatedStringWrapper", REQUIRED,
1866 R"({"repeatedStringWrapper": ["", "AQI="]})",
1867 R"(
1868 repeated_string_wrapper: {value: ""}
1869 repeated_string_wrapper: {value: "AQI="}
1870 )");
1871 RunValidJsonTest(
1872 "RepeatedBytesWrapper", REQUIRED,
1873 R"({"repeatedBytesWrapper": ["", "AQI="]})",
1874 R"(
1875 repeated_bytes_wrapper: {value: ""}
1876 repeated_bytes_wrapper: {value: "\x01\x02"}
1877 )");
1878 RunValidJsonTest(
1879 "WrapperTypesWithNullValue", REQUIRED,
1880 R"({
1881 "optionalBoolWrapper": null,
1882 "optionalInt32Wrapper": null,
1883 "optionalUint32Wrapper": null,
1884 "optionalInt64Wrapper": null,
1885 "optionalUint64Wrapper": null,
1886 "optionalFloatWrapper": null,
1887 "optionalDoubleWrapper": null,
1888 "optionalStringWrapper": null,
1889 "optionalBytesWrapper": null,
1890 "repeatedBoolWrapper": null,
1891 "repeatedInt32Wrapper": null,
1892 "repeatedUint32Wrapper": null,
1893 "repeatedInt64Wrapper": null,
1894 "repeatedUint64Wrapper": null,
1895 "repeatedFloatWrapper": null,
1896 "repeatedDoubleWrapper": null,
1897 "repeatedStringWrapper": null,
1898 "repeatedBytesWrapper": null
1899 })",
1900 "");
1901
1902 // Duration
1903 RunValidJsonTest(
1904 "DurationMinValue", REQUIRED,
1905 R"({"optionalDuration": "-315576000000.999999999s"})",
1906 "optional_duration: {seconds: -315576000000 nanos: -999999999}");
1907 RunValidJsonTest(
1908 "DurationMaxValue", REQUIRED,
1909 R"({"optionalDuration": "315576000000.999999999s"})",
1910 "optional_duration: {seconds: 315576000000 nanos: 999999999}");
1911 RunValidJsonTest(
1912 "DurationRepeatedValue", REQUIRED,
1913 R"({"repeatedDuration": ["1.5s", "-1.5s"]})",
1914 "repeated_duration: {seconds: 1 nanos: 500000000}"
1915 "repeated_duration: {seconds: -1 nanos: -500000000}");
1916 RunValidJsonTest(
1917 "DurationNull", REQUIRED,
1918 R"({"optionalDuration": null})",
1919 "");
1920
1921 ExpectParseFailureForJson(
1922 "DurationMissingS", REQUIRED,
1923 R"({"optionalDuration": "1"})");
1924 ExpectParseFailureForJson(
1925 "DurationJsonInputTooSmall", REQUIRED,
1926 R"({"optionalDuration": "-315576000001.000000000s"})");
1927 ExpectParseFailureForJson(
1928 "DurationJsonInputTooLarge", REQUIRED,
1929 R"({"optionalDuration": "315576000001.000000000s"})");
1930 ExpectSerializeFailureForJson(
1931 "DurationProtoInputTooSmall", REQUIRED,
1932 "optional_duration: {seconds: -315576000001 nanos: 0}");
1933 ExpectSerializeFailureForJson(
1934 "DurationProtoInputTooLarge", REQUIRED,
1935 "optional_duration: {seconds: 315576000001 nanos: 0}");
1936
1937 RunValidJsonTestWithValidator(
1938 "DurationHasZeroFractionalDigit", RECOMMENDED,
1939 R"({"optionalDuration": "1.000000000s"})",
__anonce1777370902(const Json::Value& value) 1940 [](const Json::Value& value) {
1941 return value["optionalDuration"].asString() == "1s";
1942 });
1943 RunValidJsonTestWithValidator(
1944 "DurationHas3FractionalDigits", RECOMMENDED,
1945 R"({"optionalDuration": "1.010000000s"})",
__anonce1777370a02(const Json::Value& value) 1946 [](const Json::Value& value) {
1947 return value["optionalDuration"].asString() == "1.010s";
1948 });
1949 RunValidJsonTestWithValidator(
1950 "DurationHas6FractionalDigits", RECOMMENDED,
1951 R"({"optionalDuration": "1.000010000s"})",
__anonce1777370b02(const Json::Value& value) 1952 [](const Json::Value& value) {
1953 return value["optionalDuration"].asString() == "1.000010s";
1954 });
1955 RunValidJsonTestWithValidator(
1956 "DurationHas9FractionalDigits", RECOMMENDED,
1957 R"({"optionalDuration": "1.000000010s"})",
__anonce1777370c02(const Json::Value& value) 1958 [](const Json::Value& value) {
1959 return value["optionalDuration"].asString() == "1.000000010s";
1960 });
1961
1962 // Timestamp
1963 RunValidJsonTest(
1964 "TimestampMinValue", REQUIRED,
1965 R"({"optionalTimestamp": "0001-01-01T00:00:00Z"})",
1966 "optional_timestamp: {seconds: -62135596800}");
1967 RunValidJsonTest(
1968 "TimestampMaxValue", REQUIRED,
1969 R"({"optionalTimestamp": "9999-12-31T23:59:59.999999999Z"})",
1970 "optional_timestamp: {seconds: 253402300799 nanos: 999999999}");
1971 RunValidJsonTest(
1972 "TimestampRepeatedValue", REQUIRED,
1973 R"({
1974 "repeatedTimestamp": [
1975 "0001-01-01T00:00:00Z",
1976 "9999-12-31T23:59:59.999999999Z"
1977 ]
1978 })",
1979 "repeated_timestamp: {seconds: -62135596800}"
1980 "repeated_timestamp: {seconds: 253402300799 nanos: 999999999}");
1981 RunValidJsonTest(
1982 "TimestampWithPositiveOffset", REQUIRED,
1983 R"({"optionalTimestamp": "1970-01-01T08:00:01+08:00"})",
1984 "optional_timestamp: {seconds: 1}");
1985 RunValidJsonTest(
1986 "TimestampWithNegativeOffset", REQUIRED,
1987 R"({"optionalTimestamp": "1969-12-31T16:00:01-08:00"})",
1988 "optional_timestamp: {seconds: 1}");
1989 RunValidJsonTest(
1990 "TimestampNull", REQUIRED,
1991 R"({"optionalTimestamp": null})",
1992 "");
1993
1994 ExpectParseFailureForJson(
1995 "TimestampJsonInputTooSmall", REQUIRED,
1996 R"({"optionalTimestamp": "0000-01-01T00:00:00Z"})");
1997 ExpectParseFailureForJson(
1998 "TimestampJsonInputTooLarge", REQUIRED,
1999 R"({"optionalTimestamp": "10000-01-01T00:00:00Z"})");
2000 ExpectParseFailureForJson(
2001 "TimestampJsonInputMissingZ", REQUIRED,
2002 R"({"optionalTimestamp": "0001-01-01T00:00:00"})");
2003 ExpectParseFailureForJson(
2004 "TimestampJsonInputMissingT", REQUIRED,
2005 R"({"optionalTimestamp": "0001-01-01 00:00:00Z"})");
2006 ExpectParseFailureForJson(
2007 "TimestampJsonInputLowercaseZ", REQUIRED,
2008 R"({"optionalTimestamp": "0001-01-01T00:00:00z"})");
2009 ExpectParseFailureForJson(
2010 "TimestampJsonInputLowercaseT", REQUIRED,
2011 R"({"optionalTimestamp": "0001-01-01t00:00:00Z"})");
2012 ExpectSerializeFailureForJson(
2013 "TimestampProtoInputTooSmall", REQUIRED,
2014 "optional_timestamp: {seconds: -62135596801}");
2015 ExpectSerializeFailureForJson(
2016 "TimestampProtoInputTooLarge", REQUIRED,
2017 "optional_timestamp: {seconds: 253402300800}");
2018 RunValidJsonTestWithValidator(
2019 "TimestampZeroNormalized", RECOMMENDED,
2020 R"({"optionalTimestamp": "1969-12-31T16:00:00-08:00"})",
__anonce1777370d02(const Json::Value& value) 2021 [](const Json::Value& value) {
2022 return value["optionalTimestamp"].asString() ==
2023 "1970-01-01T00:00:00Z";
2024 });
2025 RunValidJsonTestWithValidator(
2026 "TimestampHasZeroFractionalDigit", RECOMMENDED,
2027 R"({"optionalTimestamp": "1970-01-01T00:00:00.000000000Z"})",
__anonce1777370e02(const Json::Value& value) 2028 [](const Json::Value& value) {
2029 return value["optionalTimestamp"].asString() ==
2030 "1970-01-01T00:00:00Z";
2031 });
2032 RunValidJsonTestWithValidator(
2033 "TimestampHas3FractionalDigits", RECOMMENDED,
2034 R"({"optionalTimestamp": "1970-01-01T00:00:00.010000000Z"})",
__anonce1777370f02(const Json::Value& value) 2035 [](const Json::Value& value) {
2036 return value["optionalTimestamp"].asString() ==
2037 "1970-01-01T00:00:00.010Z";
2038 });
2039 RunValidJsonTestWithValidator(
2040 "TimestampHas6FractionalDigits", RECOMMENDED,
2041 R"({"optionalTimestamp": "1970-01-01T00:00:00.000010000Z"})",
__anonce1777371002(const Json::Value& value) 2042 [](const Json::Value& value) {
2043 return value["optionalTimestamp"].asString() ==
2044 "1970-01-01T00:00:00.000010Z";
2045 });
2046 RunValidJsonTestWithValidator(
2047 "TimestampHas9FractionalDigits", RECOMMENDED,
2048 R"({"optionalTimestamp": "1970-01-01T00:00:00.000000010Z"})",
__anonce1777371102(const Json::Value& value) 2049 [](const Json::Value& value) {
2050 return value["optionalTimestamp"].asString() ==
2051 "1970-01-01T00:00:00.000000010Z";
2052 });
2053
2054 // FieldMask
2055 RunValidJsonTest(
2056 "FieldMask", REQUIRED,
2057 R"({"optionalFieldMask": "foo,barBaz"})",
2058 R"(optional_field_mask: {paths: "foo" paths: "bar_baz"})");
2059 RunValidJsonTest(
2060 "EmptyFieldMask", REQUIRED,
2061 R"({"optionalFieldMask": ""})",
2062 R"(optional_field_mask: {})");
2063 ExpectParseFailureForJson(
2064 "FieldMaskInvalidCharacter", RECOMMENDED,
2065 R"({"optionalFieldMask": "foo,bar_bar"})");
2066 ExpectSerializeFailureForJson(
2067 "FieldMaskPathsDontRoundTrip", RECOMMENDED,
2068 R"(optional_field_mask: {paths: "fooBar"})");
2069 ExpectSerializeFailureForJson(
2070 "FieldMaskNumbersDontRoundTrip", RECOMMENDED,
2071 R"(optional_field_mask: {paths: "foo_3_bar"})");
2072 ExpectSerializeFailureForJson(
2073 "FieldMaskTooManyUnderscore", RECOMMENDED,
2074 R"(optional_field_mask: {paths: "foo__bar"})");
2075
2076 // Struct
2077 RunValidJsonTest(
2078 "Struct", REQUIRED,
2079 R"({
2080 "optionalStruct": {
2081 "nullValue": null,
2082 "intValue": 1234,
2083 "boolValue": true,
2084 "doubleValue": 1234.5678,
2085 "stringValue": "Hello world!",
2086 "listValue": [1234, "5678"],
2087 "objectValue": {
2088 "value": 0
2089 }
2090 }
2091 })",
2092 R"(
2093 optional_struct: {
2094 fields: {
2095 key: "nullValue"
2096 value: {null_value: NULL_VALUE}
2097 }
2098 fields: {
2099 key: "intValue"
2100 value: {number_value: 1234}
2101 }
2102 fields: {
2103 key: "boolValue"
2104 value: {bool_value: true}
2105 }
2106 fields: {
2107 key: "doubleValue"
2108 value: {number_value: 1234.5678}
2109 }
2110 fields: {
2111 key: "stringValue"
2112 value: {string_value: "Hello world!"}
2113 }
2114 fields: {
2115 key: "listValue"
2116 value: {
2117 list_value: {
2118 values: {
2119 number_value: 1234
2120 }
2121 values: {
2122 string_value: "5678"
2123 }
2124 }
2125 }
2126 }
2127 fields: {
2128 key: "objectValue"
2129 value: {
2130 struct_value: {
2131 fields: {
2132 key: "value"
2133 value: {
2134 number_value: 0
2135 }
2136 }
2137 }
2138 }
2139 }
2140 }
2141 )");
2142 RunValidJsonTest(
2143 "StructWithEmptyListValue", REQUIRED,
2144 R"({
2145 "optionalStruct": {
2146 "listValue": []
2147 }
2148 })",
2149 R"(
2150 optional_struct: {
2151 fields: {
2152 key: "listValue"
2153 value: {
2154 list_value: {
2155 }
2156 }
2157 }
2158 }
2159 )");
2160 // Value
2161 RunValidJsonTest(
2162 "ValueAcceptInteger", REQUIRED,
2163 R"({"optionalValue": 1})",
2164 "optional_value: { number_value: 1}");
2165 RunValidJsonTest(
2166 "ValueAcceptFloat", REQUIRED,
2167 R"({"optionalValue": 1.5})",
2168 "optional_value: { number_value: 1.5}");
2169 RunValidJsonTest(
2170 "ValueAcceptBool", REQUIRED,
2171 R"({"optionalValue": false})",
2172 "optional_value: { bool_value: false}");
2173 RunValidJsonTest(
2174 "ValueAcceptNull", REQUIRED,
2175 R"({"optionalValue": null})",
2176 "optional_value: { null_value: NULL_VALUE}");
2177 RunValidJsonTest(
2178 "ValueAcceptString", REQUIRED,
2179 R"({"optionalValue": "hello"})",
2180 R"(optional_value: { string_value: "hello"})");
2181 RunValidJsonTest(
2182 "ValueAcceptList", REQUIRED,
2183 R"({"optionalValue": [0, "hello"]})",
2184 R"(
2185 optional_value: {
2186 list_value: {
2187 values: {
2188 number_value: 0
2189 }
2190 values: {
2191 string_value: "hello"
2192 }
2193 }
2194 }
2195 )");
2196 RunValidJsonTest(
2197 "ValueAcceptObject", REQUIRED,
2198 R"({"optionalValue": {"value": 1}})",
2199 R"(
2200 optional_value: {
2201 struct_value: {
2202 fields: {
2203 key: "value"
2204 value: {
2205 number_value: 1
2206 }
2207 }
2208 }
2209 }
2210 )");
2211 RunValidJsonTest(
2212 "RepeatedValue", REQUIRED,
2213 R"({
2214 "repeatedValue": [["a"]]
2215 })",
2216 R"(
2217 repeated_value: [
2218 {
2219 list_value: {
2220 values: [
2221 { string_value: "a"}
2222 ]
2223 }
2224 }
2225 ]
2226 )");
2227 RunValidJsonTest(
2228 "RepeatedListValue", REQUIRED,
2229 R"({
2230 "repeatedListValue": [["a"]]
2231 })",
2232 R"(
2233 repeated_list_value: [
2234 {
2235 values: [
2236 { string_value: "a"}
2237 ]
2238 }
2239 ]
2240 )");
2241
2242 // Any
2243 RunValidJsonTest(
2244 "Any", REQUIRED,
2245 R"({
2246 "optionalAny": {
2247 "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3",
2248 "optionalInt32": 12345
2249 }
2250 })",
2251 R"(
2252 optional_any: {
2253 [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] {
2254 optional_int32: 12345
2255 }
2256 }
2257 )");
2258 RunValidJsonTest(
2259 "AnyNested", REQUIRED,
2260 R"({
2261 "optionalAny": {
2262 "@type": "type.googleapis.com/google.protobuf.Any",
2263 "value": {
2264 "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3",
2265 "optionalInt32": 12345
2266 }
2267 }
2268 })",
2269 R"(
2270 optional_any: {
2271 [type.googleapis.com/google.protobuf.Any] {
2272 [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] {
2273 optional_int32: 12345
2274 }
2275 }
2276 }
2277 )");
2278 // The special "@type" tag is not required to appear first.
2279 RunValidJsonTest(
2280 "AnyUnorderedTypeTag", REQUIRED,
2281 R"({
2282 "optionalAny": {
2283 "optionalInt32": 12345,
2284 "@type": "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3"
2285 }
2286 })",
2287 R"(
2288 optional_any: {
2289 [type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] {
2290 optional_int32: 12345
2291 }
2292 }
2293 )");
2294 // Well-known types in Any.
2295 RunValidJsonTest(
2296 "AnyWithInt32ValueWrapper", REQUIRED,
2297 R"({
2298 "optionalAny": {
2299 "@type": "type.googleapis.com/google.protobuf.Int32Value",
2300 "value": 12345
2301 }
2302 })",
2303 R"(
2304 optional_any: {
2305 [type.googleapis.com/google.protobuf.Int32Value] {
2306 value: 12345
2307 }
2308 }
2309 )");
2310 RunValidJsonTest(
2311 "AnyWithDuration", REQUIRED,
2312 R"({
2313 "optionalAny": {
2314 "@type": "type.googleapis.com/google.protobuf.Duration",
2315 "value": "1.5s"
2316 }
2317 })",
2318 R"(
2319 optional_any: {
2320 [type.googleapis.com/google.protobuf.Duration] {
2321 seconds: 1
2322 nanos: 500000000
2323 }
2324 }
2325 )");
2326 RunValidJsonTest(
2327 "AnyWithTimestamp", REQUIRED,
2328 R"({
2329 "optionalAny": {
2330 "@type": "type.googleapis.com/google.protobuf.Timestamp",
2331 "value": "1970-01-01T00:00:00Z"
2332 }
2333 })",
2334 R"(
2335 optional_any: {
2336 [type.googleapis.com/google.protobuf.Timestamp] {
2337 seconds: 0
2338 nanos: 0
2339 }
2340 }
2341 )");
2342 RunValidJsonTest(
2343 "AnyWithFieldMask", REQUIRED,
2344 R"({
2345 "optionalAny": {
2346 "@type": "type.googleapis.com/google.protobuf.FieldMask",
2347 "value": "foo,barBaz"
2348 }
2349 })",
2350 R"(
2351 optional_any: {
2352 [type.googleapis.com/google.protobuf.FieldMask] {
2353 paths: ["foo", "bar_baz"]
2354 }
2355 }
2356 )");
2357 RunValidJsonTest(
2358 "AnyWithStruct", REQUIRED,
2359 R"({
2360 "optionalAny": {
2361 "@type": "type.googleapis.com/google.protobuf.Struct",
2362 "value": {
2363 "foo": 1
2364 }
2365 }
2366 })",
2367 R"(
2368 optional_any: {
2369 [type.googleapis.com/google.protobuf.Struct] {
2370 fields: {
2371 key: "foo"
2372 value: {
2373 number_value: 1
2374 }
2375 }
2376 }
2377 }
2378 )");
2379 RunValidJsonTest(
2380 "AnyWithValueForJsonObject", REQUIRED,
2381 R"({
2382 "optionalAny": {
2383 "@type": "type.googleapis.com/google.protobuf.Value",
2384 "value": {
2385 "foo": 1
2386 }
2387 }
2388 })",
2389 R"(
2390 optional_any: {
2391 [type.googleapis.com/google.protobuf.Value] {
2392 struct_value: {
2393 fields: {
2394 key: "foo"
2395 value: {
2396 number_value: 1
2397 }
2398 }
2399 }
2400 }
2401 }
2402 )");
2403 RunValidJsonTest(
2404 "AnyWithValueForInteger", REQUIRED,
2405 R"({
2406 "optionalAny": {
2407 "@type": "type.googleapis.com/google.protobuf.Value",
2408 "value": 1
2409 }
2410 })",
2411 R"(
2412 optional_any: {
2413 [type.googleapis.com/google.protobuf.Value] {
2414 number_value: 1
2415 }
2416 }
2417 )");
2418
2419 RunValidJsonIgnoreUnknownTest(
2420 "IgnoreUnknownJsonNumber", REQUIRED,
2421 R"({
2422 "unknown": 1
2423 })",
2424 "");
2425 RunValidJsonIgnoreUnknownTest(
2426 "IgnoreUnknownJsonString", REQUIRED,
2427 R"({
2428 "unknown": "a"
2429 })",
2430 "");
2431 RunValidJsonIgnoreUnknownTest(
2432 "IgnoreUnknownJsonTrue", REQUIRED,
2433 R"({
2434 "unknown": true
2435 })",
2436 "");
2437 RunValidJsonIgnoreUnknownTest(
2438 "IgnoreUnknownJsonFalse", REQUIRED,
2439 R"({
2440 "unknown": false
2441 })",
2442 "");
2443 RunValidJsonIgnoreUnknownTest(
2444 "IgnoreUnknownJsonNull", REQUIRED,
2445 R"({
2446 "unknown": null
2447 })",
2448 "");
2449 RunValidJsonIgnoreUnknownTest(
2450 "IgnoreUnknownJsonObject", REQUIRED,
2451 R"({
2452 "unknown": {"a": 1}
2453 })",
2454 "");
2455
2456 ExpectParseFailureForJson("RejectTopLevelNull", REQUIRED, "null");
2457 }
2458
2459 } // namespace protobuf
2460 } // namespace google
2461