Debian Codegraph @

extra/protobuf/protobuf-24.4/conformance/binary_json_conformance_suite.cc

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