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

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

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

Powered by Google App Engine
This is Rietveld 408576698