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