Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(178)

Side by Side Diff: third_party/protobuf/conformance/conformance_test.cc

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

Powered by Google App Engine
This is Rietveld 408576698