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