|
|
|
@ -7,24 +7,28 @@ |
|
|
|
|
|
|
|
|
|
#include "binary_json_conformance_suite.h" |
|
|
|
|
|
|
|
|
|
#include <cassert> |
|
|
|
|
#include <cctype> |
|
|
|
|
#include <cstddef> |
|
|
|
|
#include <cstdint> |
|
|
|
|
#include <cstring> |
|
|
|
|
#include <memory> |
|
|
|
|
#include <string> |
|
|
|
|
#include <type_traits> |
|
|
|
|
#include <utility> |
|
|
|
|
#include <vector> |
|
|
|
|
|
|
|
|
|
#include "google/protobuf/util/json_util.h" |
|
|
|
|
#include "google/protobuf/util/type_resolver_util.h" |
|
|
|
|
#include "absl/log/absl_check.h" |
|
|
|
|
#include "absl/log/absl_log.h" |
|
|
|
|
#include "absl/log/die_if_null.h" |
|
|
|
|
#include "absl/status/status.h" |
|
|
|
|
#include "absl/strings/str_cat.h" |
|
|
|
|
#include "absl/strings/string_view.h" |
|
|
|
|
#include "json/json.h" |
|
|
|
|
#include "conformance/conformance.pb.h" |
|
|
|
|
#include "conformance_test.h" |
|
|
|
|
#include "google/protobuf/endian.h" |
|
|
|
|
#include "google/protobuf/json/json.h" |
|
|
|
|
#include "google/protobuf/test_messages_proto2.pb.h" |
|
|
|
|
#include "google/protobuf/test_messages_proto2.pb.h" |
|
|
|
|
#include "google/protobuf/test_messages_proto3.pb.h" |
|
|
|
@ -43,7 +47,6 @@ using google::protobuf::internal::little_endian::FromHost; |
|
|
|
|
using google::protobuf::util::NewTypeResolverForDescriptorPool; |
|
|
|
|
using protobuf_test_messages::proto2::TestAllTypesProto2; |
|
|
|
|
using protobuf_test_messages::proto3::TestAllTypesProto3; |
|
|
|
|
using std::string; |
|
|
|
|
|
|
|
|
|
namespace { |
|
|
|
|
|
|
|
|
@ -53,7 +56,7 @@ constexpr absl::string_view kTypeUrlPrefix = "type.googleapis.com"; |
|
|
|
|
// Corresponds approx to 500KB wireformat bytes.
|
|
|
|
|
const size_t kPerformanceRepeatCount = 50000; |
|
|
|
|
|
|
|
|
|
string GetTypeUrl(const Descriptor* message) { |
|
|
|
|
std::string GetTypeUrl(const Descriptor* message) { |
|
|
|
|
return absl::StrCat(kTypeUrlPrefix, "/", message->full_name()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -85,52 +88,56 @@ size_t vencode64(uint64_t val, int over_encoded_bytes, char* buf) { |
|
|
|
|
return i; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string varint(uint64_t x) { |
|
|
|
|
std::string varint(uint64_t x) { |
|
|
|
|
char buf[VARINT_MAX_LEN]; |
|
|
|
|
size_t len = vencode64(x, 0, buf); |
|
|
|
|
return string(buf, len); |
|
|
|
|
return std::string(buf, len); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Encodes a varint that is |extra| bytes longer than it needs to be, but still
|
|
|
|
|
// valid.
|
|
|
|
|
string longvarint(uint64_t x, int extra) { |
|
|
|
|
std::string longvarint(uint64_t x, int extra) { |
|
|
|
|
char buf[VARINT_MAX_LEN]; |
|
|
|
|
size_t len = vencode64(x, extra, buf); |
|
|
|
|
return string(buf, len); |
|
|
|
|
return std::string(buf, len); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string fixed32(void* data) { |
|
|
|
|
std::string fixed32(void* data) { |
|
|
|
|
uint32_t data_le; |
|
|
|
|
std::memcpy(&data_le, data, 4); |
|
|
|
|
data_le = FromHost(data_le); |
|
|
|
|
return string(reinterpret_cast<char*>(&data_le), 4); |
|
|
|
|
return std::string(reinterpret_cast<char*>(&data_le), 4); |
|
|
|
|
} |
|
|
|
|
string fixed64(void* data) { |
|
|
|
|
std::string fixed64(void* data) { |
|
|
|
|
uint64_t data_le; |
|
|
|
|
std::memcpy(&data_le, data, 8); |
|
|
|
|
data_le = FromHost(data_le); |
|
|
|
|
return string(reinterpret_cast<char*>(&data_le), 8); |
|
|
|
|
return std::string(reinterpret_cast<char*>(&data_le), 8); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string delim(const string& buf) { |
|
|
|
|
std::string delim(const std::string& buf) { |
|
|
|
|
return absl::StrCat(varint(buf.size()), buf); |
|
|
|
|
} |
|
|
|
|
string u32(uint32_t u32) { return fixed32(&u32); } |
|
|
|
|
string u64(uint64_t u64) { return fixed64(&u64); } |
|
|
|
|
string flt(float f) { return fixed32(&f); } |
|
|
|
|
string dbl(double d) { return fixed64(&d); } |
|
|
|
|
string zz32(int32_t x) { return varint(WireFormatLite::ZigZagEncode32(x)); } |
|
|
|
|
string zz64(int64_t x) { return varint(WireFormatLite::ZigZagEncode64(x)); } |
|
|
|
|
std::string u32(uint32_t u32) { return fixed32(&u32); } |
|
|
|
|
std::string u64(uint64_t u64) { return fixed64(&u64); } |
|
|
|
|
std::string flt(float f) { return fixed32(&f); } |
|
|
|
|
std::string dbl(double d) { return fixed64(&d); } |
|
|
|
|
std::string zz32(int32_t x) { |
|
|
|
|
return varint(WireFormatLite::ZigZagEncode32(x)); |
|
|
|
|
} |
|
|
|
|
std::string zz64(int64_t x) { |
|
|
|
|
return varint(WireFormatLite::ZigZagEncode64(x)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string tag(uint32_t fieldnum, char wire_type) { |
|
|
|
|
std::string tag(uint32_t fieldnum, char wire_type) { |
|
|
|
|
return varint((fieldnum << 3) | wire_type); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string tag(int fieldnum, char wire_type) { |
|
|
|
|
std::string tag(int fieldnum, char wire_type) { |
|
|
|
|
return tag(static_cast<uint32_t>(fieldnum), wire_type); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string GetDefaultValue(FieldDescriptor::Type type) { |
|
|
|
|
std::string GetDefaultValue(FieldDescriptor::Type type) { |
|
|
|
|
switch (type) { |
|
|
|
|
case FieldDescriptor::TYPE_INT32: |
|
|
|
|
case FieldDescriptor::TYPE_INT64: |
|
|
|
@ -163,7 +170,7 @@ string GetDefaultValue(FieldDescriptor::Type type) { |
|
|
|
|
return ""; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string GetNonDefaultValue(FieldDescriptor::Type type) { |
|
|
|
|
std::string GetNonDefaultValue(FieldDescriptor::Type type) { |
|
|
|
|
switch (type) { |
|
|
|
|
case FieldDescriptor::TYPE_INT32: |
|
|
|
|
case FieldDescriptor::TYPE_INT64: |
|
|
|
@ -200,14 +207,15 @@ string GetNonDefaultValue(FieldDescriptor::Type type) { |
|
|
|
|
|
|
|
|
|
#define UNKNOWN_FIELD 666 |
|
|
|
|
|
|
|
|
|
string UpperCase(string str) { |
|
|
|
|
std::string UpperCase(std::string str) { |
|
|
|
|
for (size_t i = 0; i < str.size(); i++) { |
|
|
|
|
str[i] = toupper(str[i]); |
|
|
|
|
} |
|
|
|
|
return str; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool IsProto3Default(FieldDescriptor::Type type, const string& binary_data) { |
|
|
|
|
bool IsProto3Default(FieldDescriptor::Type type, |
|
|
|
|
const std::string& binary_data) { |
|
|
|
|
switch (type) { |
|
|
|
|
case FieldDescriptor::TYPE_DOUBLE: |
|
|
|
|
return binary_data == dbl(0); |
|
|
|
@ -243,9 +251,9 @@ namespace protobuf { |
|
|
|
|
|
|
|
|
|
bool BinaryAndJsonConformanceSuite::ParseJsonResponse( |
|
|
|
|
const ConformanceResponse& response, Message* test_message) { |
|
|
|
|
string binary_protobuf; |
|
|
|
|
std::string binary_protobuf; |
|
|
|
|
absl::Status status = |
|
|
|
|
JsonToBinaryString(type_resolver_.get(), type_url_, |
|
|
|
|
json::JsonToBinaryString(type_resolver_.get(), type_url_, |
|
|
|
|
response.json_payload(), &binary_protobuf); |
|
|
|
|
|
|
|
|
|
if (!status.ok()) { |
|
|
|
@ -266,7 +274,7 @@ bool BinaryAndJsonConformanceSuite::ParseResponse( |
|
|
|
|
const ConformanceRequestSetting& setting, Message* test_message) { |
|
|
|
|
const ConformanceRequest& request = setting.GetRequest(); |
|
|
|
|
WireFormat requested_output = request.requested_output_format(); |
|
|
|
|
const string& test_name = setting.GetTestName(); |
|
|
|
|
const std::string& test_name = setting.GetTestName(); |
|
|
|
|
ConformanceLevel level = setting.GetLevel(); |
|
|
|
|
|
|
|
|
|
switch (response.result_case()) { |
|
|
|
@ -327,8 +335,8 @@ void BinaryAndJsonConformanceSuite::RunSuiteImpl() { |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
ExpectParseFailureForProtoWithProtoVersion(const string& proto, |
|
|
|
|
const string& test_name, |
|
|
|
|
ExpectParseFailureForProtoWithProtoVersion(const std::string& proto, |
|
|
|
|
const std::string& test_name, |
|
|
|
|
ConformanceLevel level) { |
|
|
|
|
MessageType prototype; |
|
|
|
|
// We don't expect output, but if the program erroneously accepts the protobuf
|
|
|
|
@ -339,7 +347,7 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
|
|
|
|
|
const ConformanceRequest& request = setting.GetRequest(); |
|
|
|
|
ConformanceResponse response; |
|
|
|
|
string effective_test_name = |
|
|
|
|
std::string effective_test_name = |
|
|
|
|
absl::StrCat(setting.ConformanceLevelToString(level), ".", |
|
|
|
|
SyntaxIdentifier(), ".ProtobufInput.", test_name); |
|
|
|
|
|
|
|
|
@ -357,7 +365,8 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
// Expect that this precise protobuf will cause a parse error.
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForProto( |
|
|
|
|
const string& proto, const string& test_name, ConformanceLevel level) { |
|
|
|
|
const std::string& proto, const std::string& test_name, |
|
|
|
|
ConformanceLevel level) { |
|
|
|
|
ExpectParseFailureForProtoWithProtoVersion(proto, test_name, level); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -368,16 +377,16 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForProto( |
|
|
|
|
// TODO: implement the second of these.
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
MessageType>::ExpectHardParseFailureForProto(const string& proto, |
|
|
|
|
const string& test_name, |
|
|
|
|
MessageType>::ExpectHardParseFailureForProto(const std::string& proto, |
|
|
|
|
const std::string& test_name, |
|
|
|
|
ConformanceLevel level) { |
|
|
|
|
return ExpectParseFailureForProto(proto, test_name, level); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidJsonTest( |
|
|
|
|
const string& test_name, ConformanceLevel level, const string& input_json, |
|
|
|
|
const string& equivalent_text_format) { |
|
|
|
|
const std::string& test_name, ConformanceLevel level, |
|
|
|
|
const std::string& input_json, const std::string& equivalent_text_format) { |
|
|
|
|
MessageType prototype; |
|
|
|
|
RunValidJsonTestWithMessage(test_name, level, input_json, |
|
|
|
|
equivalent_text_format, prototype); |
|
|
|
@ -385,9 +394,10 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidJsonTest( |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
RunValidJsonTestWithMessage(const string& test_name, ConformanceLevel level, |
|
|
|
|
const string& input_json, |
|
|
|
|
const string& equivalent_text_format, |
|
|
|
|
RunValidJsonTestWithMessage(const std::string& test_name, |
|
|
|
|
ConformanceLevel level, |
|
|
|
|
const std::string& input_json, |
|
|
|
|
const std::string& equivalent_text_format, |
|
|
|
|
const Message& prototype) { |
|
|
|
|
ConformanceRequestSetting setting1( |
|
|
|
|
level, conformance::JSON, conformance::PROTOBUF, conformance::JSON_TEST, |
|
|
|
@ -401,10 +411,10 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
RunValidJsonTestWithProtobufInput(const string& test_name, |
|
|
|
|
ConformanceLevel level, |
|
|
|
|
RunValidJsonTestWithProtobufInput( |
|
|
|
|
const std::string& test_name, ConformanceLevel level, |
|
|
|
|
const TestAllTypesProto3& input, |
|
|
|
|
const string& equivalent_text_format) { |
|
|
|
|
const std::string& equivalent_text_format) { |
|
|
|
|
ConformanceRequestSetting setting( |
|
|
|
|
level, conformance::PROTOBUF, conformance::JSON, conformance::JSON_TEST, |
|
|
|
|
input, test_name, input.SerializeAsString()); |
|
|
|
@ -413,10 +423,10 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
RunValidJsonIgnoreUnknownTest(const string& test_name, |
|
|
|
|
RunValidJsonIgnoreUnknownTest(const std::string& test_name, |
|
|
|
|
ConformanceLevel level, |
|
|
|
|
const string& input_json, |
|
|
|
|
const string& equivalent_text_format) { |
|
|
|
|
const std::string& input_json, |
|
|
|
|
const std::string& equivalent_text_format) { |
|
|
|
|
TestAllTypesProto3 prototype; |
|
|
|
|
ConformanceRequestSetting setting( |
|
|
|
|
level, conformance::JSON, conformance::PROTOBUF, |
|
|
|
@ -427,8 +437,9 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidProtobufTest( |
|
|
|
|
const string& test_name, ConformanceLevel level, |
|
|
|
|
const string& input_protobuf, const string& equivalent_text_format) { |
|
|
|
|
const std::string& test_name, ConformanceLevel level, |
|
|
|
|
const std::string& input_protobuf, |
|
|
|
|
const std::string& equivalent_text_format) { |
|
|
|
|
MessageType prototype; |
|
|
|
|
|
|
|
|
|
ConformanceRequestSetting setting1( |
|
|
|
@ -446,15 +457,15 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidProtobufTest( |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidBinaryProtobufTest( |
|
|
|
|
const string& test_name, ConformanceLevel level, |
|
|
|
|
const string& input_protobuf) { |
|
|
|
|
const std::string& test_name, ConformanceLevel level, |
|
|
|
|
const std::string& input_protobuf) { |
|
|
|
|
RunValidBinaryProtobufTest(test_name, level, input_protobuf, input_protobuf); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidBinaryProtobufTest( |
|
|
|
|
const string& test_name, ConformanceLevel level, |
|
|
|
|
const string& input_protobuf, const string& expected_protobuf) { |
|
|
|
|
const std::string& test_name, ConformanceLevel level, |
|
|
|
|
const std::string& input_protobuf, const std::string& expected_protobuf) { |
|
|
|
|
MessageType prototype; |
|
|
|
|
ConformanceRequestSetting setting( |
|
|
|
|
level, conformance::PROTOBUF, conformance::PROTOBUF, |
|
|
|
@ -464,21 +475,21 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunValidBinaryProtobufTest( |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
RunBinaryPerformanceMergeMessageWithField(const string& test_name, |
|
|
|
|
const string& field_proto) { |
|
|
|
|
string message_tag = tag(27, WireFormatLite::WIRETYPE_LENGTH_DELIMITED); |
|
|
|
|
string message_proto = absl::StrCat(message_tag, delim(field_proto)); |
|
|
|
|
RunBinaryPerformanceMergeMessageWithField(const std::string& test_name, |
|
|
|
|
const std::string& field_proto) { |
|
|
|
|
std::string message_tag = tag(27, WireFormatLite::WIRETYPE_LENGTH_DELIMITED); |
|
|
|
|
std::string message_proto = absl::StrCat(message_tag, delim(field_proto)); |
|
|
|
|
|
|
|
|
|
string proto; |
|
|
|
|
std::string proto; |
|
|
|
|
for (size_t i = 0; i < kPerformanceRepeatCount; i++) { |
|
|
|
|
proto.append(message_proto); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
string multiple_repeated_field_proto; |
|
|
|
|
std::string multiple_repeated_field_proto; |
|
|
|
|
for (size_t i = 0; i < kPerformanceRepeatCount; i++) { |
|
|
|
|
multiple_repeated_field_proto.append(field_proto); |
|
|
|
|
} |
|
|
|
|
string expected_proto = |
|
|
|
|
std::string expected_proto = |
|
|
|
|
absl::StrCat(message_tag, delim(multiple_repeated_field_proto)); |
|
|
|
|
|
|
|
|
|
RunValidBinaryProtobufTest(test_name, RECOMMENDED, proto, expected_proto); |
|
|
|
@ -486,10 +497,10 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
RunValidProtobufTestWithMessage(const string& test_name, |
|
|
|
|
RunValidProtobufTestWithMessage(const std::string& test_name, |
|
|
|
|
ConformanceLevel level, |
|
|
|
|
const Message* input, |
|
|
|
|
const string& equivalent_text_format) { |
|
|
|
|
const std::string& equivalent_text_format) { |
|
|
|
|
RunValidProtobufTest(test_name, level, input->SerializeAsString(), |
|
|
|
|
equivalent_text_format); |
|
|
|
|
} |
|
|
|
@ -501,9 +512,9 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
|
|
|
|
|
template <typename MessageType> // the JSON output directly.
|
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
MessageType>::RunValidJsonTestWithValidator(const string& test_name, |
|
|
|
|
MessageType>::RunValidJsonTestWithValidator(const std::string& test_name, |
|
|
|
|
ConformanceLevel level, |
|
|
|
|
const string& input_json, |
|
|
|
|
const std::string& input_json, |
|
|
|
|
const Validator& validator) { |
|
|
|
|
MessageType prototype; |
|
|
|
|
ConformanceRequestSetting setting(level, conformance::JSON, conformance::JSON, |
|
|
|
@ -511,7 +522,7 @@ void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
test_name, input_json); |
|
|
|
|
const ConformanceRequest& request = setting.GetRequest(); |
|
|
|
|
ConformanceResponse response; |
|
|
|
|
string effective_test_name = |
|
|
|
|
std::string effective_test_name = |
|
|
|
|
absl::StrCat(setting.ConformanceLevelToString(level), ".", |
|
|
|
|
SyntaxIdentifier(), ".JsonInput.", test_name, ".Validator"); |
|
|
|
|
|
|
|
|
@ -547,7 +558,8 @@ void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForJson( |
|
|
|
|
const string& test_name, ConformanceLevel level, const string& input_json) { |
|
|
|
|
const std::string& test_name, ConformanceLevel level, |
|
|
|
|
const std::string& input_json) { |
|
|
|
|
TestAllTypesProto3 prototype; |
|
|
|
|
// We don't expect output, but if the program erroneously accepts the protobuf
|
|
|
|
|
// we let it send its response as this. We must not leave it unspecified.
|
|
|
|
@ -556,7 +568,7 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForJson( |
|
|
|
|
test_name, input_json); |
|
|
|
|
const ConformanceRequest& request = setting.GetRequest(); |
|
|
|
|
ConformanceResponse response; |
|
|
|
|
string effective_test_name = absl::StrCat( |
|
|
|
|
std::string effective_test_name = absl::StrCat( |
|
|
|
|
setting.ConformanceLevelToString(level), ".Proto3.JsonInput.", test_name); |
|
|
|
|
|
|
|
|
|
suite_.RunTest(effective_test_name, request, &response); |
|
|
|
@ -571,10 +583,10 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::ExpectParseFailureForJson( |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
MessageType>::ExpectSerializeFailureForJson(const string& test_name, |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
ExpectSerializeFailureForJson(const std::string& test_name, |
|
|
|
|
ConformanceLevel level, |
|
|
|
|
const string& text_format) { |
|
|
|
|
const std::string& text_format) { |
|
|
|
|
TestAllTypesProto3 payload_message; |
|
|
|
|
ABSL_CHECK(TextFormat::ParseFromString(text_format, &payload_message)) |
|
|
|
|
<< "Failed to parse: " << text_format; |
|
|
|
@ -585,7 +597,7 @@ void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
prototype, test_name, payload_message.SerializeAsString()); |
|
|
|
|
const ConformanceRequest& request = setting.GetRequest(); |
|
|
|
|
ConformanceResponse response; |
|
|
|
|
string effective_test_name = absl::StrCat( |
|
|
|
|
std::string effective_test_name = absl::StrCat( |
|
|
|
|
setting.ConformanceLevelToString(level), ".", test_name, ".JsonOutput"); |
|
|
|
|
|
|
|
|
|
suite_.RunTest(effective_test_name, request, &response); |
|
|
|
@ -617,7 +629,7 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestPrematureEOFForType( |
|
|
|
|
WireFormatLite::WireType wire_type = WireFormatLite::WireTypeForFieldType( |
|
|
|
|
static_cast<WireFormatLite::FieldType>(type)); |
|
|
|
|
absl::string_view incomplete = incompletes[wire_type]; |
|
|
|
|
const string type_name = |
|
|
|
|
const std::string type_name = |
|
|
|
|
UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type))); |
|
|
|
|
|
|
|
|
|
ExpectParseFailureForProto( |
|
|
|
@ -669,7 +681,7 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestPrematureEOFForType( |
|
|
|
|
|
|
|
|
|
if (type == FieldDescriptor::TYPE_MESSAGE) { |
|
|
|
|
// Submessage ends in the middle of a value.
|
|
|
|
|
string incomplete_submsg = absl::StrCat( |
|
|
|
|
std::string incomplete_submsg = absl::StrCat( |
|
|
|
|
tag(WireFormatLite::TYPE_INT32, WireFormatLite::WIRETYPE_VARINT), |
|
|
|
|
incompletes[WireFormatLite::WIRETYPE_VARINT]); |
|
|
|
|
ExpectHardParseFailureForProto( |
|
|
|
@ -701,7 +713,7 @@ template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForType( |
|
|
|
|
FieldDescriptor::Type type, |
|
|
|
|
std::vector<std::pair<std::string, std::string>> values) { |
|
|
|
|
const string type_name = |
|
|
|
|
const std::string type_name = |
|
|
|
|
UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type))); |
|
|
|
|
WireFormatLite::WireType wire_type = WireFormatLite::WireTypeForFieldType( |
|
|
|
|
static_cast<WireFormatLite::FieldType>(type)); |
|
|
|
@ -710,16 +722,16 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForType( |
|
|
|
|
|
|
|
|
|
// Test singular data for singular fields.
|
|
|
|
|
for (size_t i = 0; i < values.size(); i++) { |
|
|
|
|
string proto = |
|
|
|
|
std::string proto = |
|
|
|
|
absl::StrCat(tag(field->number(), wire_type), values[i].first); |
|
|
|
|
// In proto3, default primitive fields should not be encoded.
|
|
|
|
|
string expected_proto = |
|
|
|
|
std::string expected_proto = |
|
|
|
|
run_proto3_tests_ && IsProto3Default(field->type(), values[i].second) |
|
|
|
|
? "" |
|
|
|
|
: absl::StrCat(tag(field->number(), wire_type), values[i].second); |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(expected_proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
|
|
|
|
|
RunValidProtobufTest( |
|
|
|
@ -734,15 +746,15 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForType( |
|
|
|
|
// For scalar message fields, repeated values are merged, which is tested
|
|
|
|
|
// separately.
|
|
|
|
|
if (type != FieldDescriptor::TYPE_MESSAGE) { |
|
|
|
|
string proto; |
|
|
|
|
std::string proto; |
|
|
|
|
for (size_t i = 0; i < values.size(); i++) { |
|
|
|
|
proto += absl::StrCat(tag(field->number(), wire_type), values[i].first); |
|
|
|
|
} |
|
|
|
|
string expected_proto = |
|
|
|
|
std::string expected_proto = |
|
|
|
|
absl::StrCat(tag(field->number(), wire_type), values.back().second); |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(expected_proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
|
|
|
|
|
RunValidProtobufTest(absl::StrCat("RepeatedScalarSelectsLast", type_name), |
|
|
|
@ -756,16 +768,16 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForType( |
|
|
|
|
const FieldDescriptor* unpacked_field = |
|
|
|
|
GetFieldForType(type, true, Packed::kFalse); |
|
|
|
|
|
|
|
|
|
string default_proto_packed; |
|
|
|
|
string default_proto_unpacked; |
|
|
|
|
string default_proto_packed_expected; |
|
|
|
|
string default_proto_unpacked_expected; |
|
|
|
|
string packed_proto_packed; |
|
|
|
|
string packed_proto_unpacked; |
|
|
|
|
string packed_proto_expected; |
|
|
|
|
string unpacked_proto_packed; |
|
|
|
|
string unpacked_proto_unpacked; |
|
|
|
|
string unpacked_proto_expected; |
|
|
|
|
std::string default_proto_packed; |
|
|
|
|
std::string default_proto_unpacked; |
|
|
|
|
std::string default_proto_packed_expected; |
|
|
|
|
std::string default_proto_unpacked_expected; |
|
|
|
|
std::string packed_proto_packed; |
|
|
|
|
std::string packed_proto_unpacked; |
|
|
|
|
std::string packed_proto_expected; |
|
|
|
|
std::string unpacked_proto_packed; |
|
|
|
|
std::string unpacked_proto_unpacked; |
|
|
|
|
std::string unpacked_proto_expected; |
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < values.size(); i++) { |
|
|
|
|
default_proto_unpacked += |
|
|
|
@ -803,7 +815,7 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForType( |
|
|
|
|
|
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(default_proto_packed_expected); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
|
|
|
|
|
// Ensures both packed and unpacked data can be parsed.
|
|
|
|
@ -816,7 +828,7 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForType( |
|
|
|
|
|
|
|
|
|
// proto2 should encode as unpacked by default and proto3 should encode as
|
|
|
|
|
// packed by default.
|
|
|
|
|
string expected_proto = rep_field->is_packed() |
|
|
|
|
std::string expected_proto = rep_field->is_packed() |
|
|
|
|
? default_proto_packed_expected |
|
|
|
|
: default_proto_unpacked_expected; |
|
|
|
|
RunValidBinaryProtobufTest(absl::StrCat("ValidDataRepeated", type_name, |
|
|
|
@ -844,8 +856,8 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForType( |
|
|
|
|
RECOMMENDED, unpacked_proto_packed, |
|
|
|
|
unpacked_proto_expected); |
|
|
|
|
} else { |
|
|
|
|
string proto; |
|
|
|
|
string expected_proto; |
|
|
|
|
std::string proto; |
|
|
|
|
std::string expected_proto; |
|
|
|
|
for (size_t i = 0; i < values.size(); i++) { |
|
|
|
|
proto += |
|
|
|
|
absl::StrCat(tag(rep_field->number(), wire_type), values[i].first); |
|
|
|
@ -854,7 +866,7 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForType( |
|
|
|
|
} |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(expected_proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
|
|
|
|
|
RunValidProtobufTest(absl::StrCat("ValidDataRepeated", type_name), REQUIRED, |
|
|
|
@ -890,7 +902,7 @@ void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
} |
|
|
|
|
})"; |
|
|
|
|
|
|
|
|
|
string proto; |
|
|
|
|
std::string proto; |
|
|
|
|
const FieldDescriptor* field = |
|
|
|
|
GetFieldForType(FieldDescriptor::TYPE_MESSAGE, false); |
|
|
|
|
for (size_t i = 0; i < values.size(); i++) { |
|
|
|
@ -906,36 +918,35 @@ void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForMapType( |
|
|
|
|
FieldDescriptor::Type key_type, FieldDescriptor::Type value_type) { |
|
|
|
|
const string key_type_name = |
|
|
|
|
const std::string key_type_name = |
|
|
|
|
UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(key_type))); |
|
|
|
|
const string value_type_name = |
|
|
|
|
const std::string value_type_name = |
|
|
|
|
UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(value_type))); |
|
|
|
|
WireFormatLite::WireType key_wire_type = |
|
|
|
|
WireFormatLite::WireTypeForFieldType( |
|
|
|
|
WireFormatLite::WireType key_wire_type = WireFormatLite::WireTypeForFieldType( |
|
|
|
|
static_cast<WireFormatLite::FieldType>(key_type)); |
|
|
|
|
WireFormatLite::WireType value_wire_type = |
|
|
|
|
WireFormatLite::WireTypeForFieldType( |
|
|
|
|
static_cast<WireFormatLite::FieldType>(value_type)); |
|
|
|
|
|
|
|
|
|
string key1_data = |
|
|
|
|
std::string key1_data = |
|
|
|
|
absl::StrCat(tag(1, key_wire_type), GetDefaultValue(key_type)); |
|
|
|
|
string value1_data = |
|
|
|
|
std::string value1_data = |
|
|
|
|
absl::StrCat(tag(2, value_wire_type), GetDefaultValue(value_type)); |
|
|
|
|
string key2_data = |
|
|
|
|
std::string key2_data = |
|
|
|
|
absl::StrCat(tag(1, key_wire_type), GetNonDefaultValue(key_type)); |
|
|
|
|
string value2_data = |
|
|
|
|
std::string value2_data = |
|
|
|
|
absl::StrCat(tag(2, value_wire_type), GetNonDefaultValue(value_type)); |
|
|
|
|
|
|
|
|
|
const FieldDescriptor* field = GetFieldForMapType(key_type, value_type); |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
// Tests map with default key and value.
|
|
|
|
|
string proto = absl::StrCat( |
|
|
|
|
std::string proto = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(key1_data, value1_data))); |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name, |
|
|
|
|
value_type_name, ".Default"), |
|
|
|
@ -944,12 +955,12 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForMapType( |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
// Tests map with missing default key and value.
|
|
|
|
|
string proto = absl::StrCat( |
|
|
|
|
std::string proto = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim("")); |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name, |
|
|
|
|
value_type_name, ".MissingDefault"), |
|
|
|
@ -958,12 +969,12 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForMapType( |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
// Tests map with non-default key and value.
|
|
|
|
|
string proto = absl::StrCat( |
|
|
|
|
std::string proto = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(key2_data, value2_data))); |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name, |
|
|
|
|
value_type_name, ".NonDefault"), |
|
|
|
@ -972,12 +983,12 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForMapType( |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
// Tests map with unordered key and value.
|
|
|
|
|
string proto = absl::StrCat( |
|
|
|
|
std::string proto = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(value2_data, key2_data))); |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name, |
|
|
|
|
value_type_name, ".Unordered"), |
|
|
|
@ -986,16 +997,16 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForMapType( |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
// Tests map with duplicate key.
|
|
|
|
|
string proto1 = absl::StrCat( |
|
|
|
|
std::string proto1 = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(key2_data, value1_data))); |
|
|
|
|
string proto2 = absl::StrCat( |
|
|
|
|
std::string proto2 = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(key2_data, value2_data))); |
|
|
|
|
string proto = absl::StrCat(proto1, proto2); |
|
|
|
|
std::string proto = absl::StrCat(proto1, proto2); |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(proto2); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
RunValidProtobufTest(absl::StrCat("ValidDataMap", key_type_name, |
|
|
|
|
value_type_name, ".DuplicateKey"), |
|
|
|
@ -1004,12 +1015,12 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForMapType( |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
// Tests map with duplicate key in map entry.
|
|
|
|
|
string proto = absl::StrCat( |
|
|
|
|
std::string proto = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(key1_data, key2_data, value2_data))); |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
RunValidProtobufTest( |
|
|
|
|
absl::StrCat("ValidDataMap", key_type_name, value_type_name, |
|
|
|
@ -1019,12 +1030,12 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForMapType( |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
// Tests map with duplicate value in map entry.
|
|
|
|
|
string proto = absl::StrCat( |
|
|
|
|
std::string proto = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(key2_data, value1_data, value2_data))); |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
RunValidProtobufTest( |
|
|
|
|
absl::StrCat("ValidDataMap", key_type_name, value_type_name, |
|
|
|
@ -1036,21 +1047,21 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForMapType( |
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
MessageType>::TestOverwriteMessageValueMap() { |
|
|
|
|
string key_data = absl::StrCat( |
|
|
|
|
std::string key_data = absl::StrCat( |
|
|
|
|
tag(1, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), delim("")); |
|
|
|
|
string field1_data = |
|
|
|
|
std::string field1_data = |
|
|
|
|
absl::StrCat(tag(1, WireFormatLite::WIRETYPE_VARINT), varint(1)); |
|
|
|
|
string field2_data = |
|
|
|
|
std::string field2_data = |
|
|
|
|
absl::StrCat(tag(2, WireFormatLite::WIRETYPE_VARINT), varint(1)); |
|
|
|
|
string field31_data = |
|
|
|
|
std::string field31_data = |
|
|
|
|
absl::StrCat(tag(31, WireFormatLite::WIRETYPE_VARINT), varint(1)); |
|
|
|
|
string submsg1_data = delim(absl::StrCat(field1_data, field31_data)); |
|
|
|
|
string submsg2_data = delim(absl::StrCat(field2_data, field31_data)); |
|
|
|
|
string value1_data = absl::StrCat( |
|
|
|
|
std::string submsg1_data = delim(absl::StrCat(field1_data, field31_data)); |
|
|
|
|
std::string submsg2_data = delim(absl::StrCat(field2_data, field31_data)); |
|
|
|
|
std::string value1_data = absl::StrCat( |
|
|
|
|
tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
submsg1_data))); |
|
|
|
|
string value2_data = absl::StrCat( |
|
|
|
|
std::string value2_data = absl::StrCat( |
|
|
|
|
tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
submsg2_data))); |
|
|
|
@ -1058,16 +1069,16 @@ void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
const FieldDescriptor* field = GetFieldForMapType( |
|
|
|
|
FieldDescriptor::TYPE_STRING, FieldDescriptor::TYPE_MESSAGE); |
|
|
|
|
|
|
|
|
|
string proto1 = absl::StrCat( |
|
|
|
|
std::string proto1 = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(key_data, value1_data))); |
|
|
|
|
string proto2 = absl::StrCat( |
|
|
|
|
std::string proto2 = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(key_data, value2_data))); |
|
|
|
|
string proto = absl::StrCat(proto1, proto2); |
|
|
|
|
std::string proto = absl::StrCat(proto1, proto2); |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(proto2); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
RunValidProtobufTest("ValidDataMap.STRING.MESSAGE.MergeValue", REQUIRED, |
|
|
|
|
proto, text); |
|
|
|
@ -1076,23 +1087,23 @@ void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForOneofType( |
|
|
|
|
FieldDescriptor::Type type) { |
|
|
|
|
const string type_name = |
|
|
|
|
const std::string type_name = |
|
|
|
|
UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type))); |
|
|
|
|
WireFormatLite::WireType wire_type = WireFormatLite::WireTypeForFieldType( |
|
|
|
|
static_cast<WireFormatLite::FieldType>(type)); |
|
|
|
|
|
|
|
|
|
const FieldDescriptor* field = GetFieldForOneofType(type); |
|
|
|
|
const string default_value = |
|
|
|
|
const std::string default_value = |
|
|
|
|
absl::StrCat(tag(field->number(), wire_type), GetDefaultValue(type)); |
|
|
|
|
const string non_default_value = |
|
|
|
|
const std::string non_default_value = |
|
|
|
|
absl::StrCat(tag(field->number(), wire_type), GetNonDefaultValue(type)); |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
// Tests oneof with default value.
|
|
|
|
|
const string proto = default_value; |
|
|
|
|
const std::string proto = default_value; |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
|
|
|
|
|
RunValidProtobufTest( |
|
|
|
@ -1105,15 +1116,15 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForOneofType( |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
// Tests oneof with non-default value.
|
|
|
|
|
const string proto = non_default_value; |
|
|
|
|
const std::string proto = non_default_value; |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
|
|
|
|
|
RunValidProtobufTest( |
|
|
|
|
absl::StrCat("ValidDataOneof", type_name, ".NonDefaultValue"), |
|
|
|
|
REQUIRED, proto, text); |
|
|
|
|
absl::StrCat("ValidDataOneof", type_name, ".NonDefaultValue"), REQUIRED, |
|
|
|
|
proto, text); |
|
|
|
|
RunValidBinaryProtobufTest( |
|
|
|
|
absl::StrCat("ValidDataOneofBinary", type_name, ".NonDefaultValue"), |
|
|
|
|
RECOMMENDED, proto, proto); |
|
|
|
@ -1121,11 +1132,11 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForOneofType( |
|
|
|
|
|
|
|
|
|
{ |
|
|
|
|
// Tests oneof with multiple values of the same field.
|
|
|
|
|
const string proto = absl::StrCat(default_value, non_default_value); |
|
|
|
|
const string expected_proto = non_default_value; |
|
|
|
|
const std::string proto = absl::StrCat(default_value, non_default_value); |
|
|
|
|
const std::string expected_proto = non_default_value; |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(expected_proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
|
|
|
|
|
RunValidProtobufTest(absl::StrCat("ValidDataOneof", type_name, |
|
|
|
@ -1143,22 +1154,21 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForOneofType( |
|
|
|
|
WireFormatLite::WireType other_wire_type = |
|
|
|
|
WireFormatLite::WireTypeForFieldType( |
|
|
|
|
static_cast<WireFormatLite::FieldType>(other_type)); |
|
|
|
|
const string other_value = |
|
|
|
|
const std::string other_value = |
|
|
|
|
absl::StrCat(tag(other_field->number(), other_wire_type), |
|
|
|
|
GetDefaultValue(other_type)); |
|
|
|
|
|
|
|
|
|
const string proto = absl::StrCat(other_value, non_default_value); |
|
|
|
|
const string expected_proto = non_default_value; |
|
|
|
|
const std::string proto = absl::StrCat(other_value, non_default_value); |
|
|
|
|
const std::string expected_proto = non_default_value; |
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(expected_proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
|
|
|
|
|
RunValidProtobufTest(absl::StrCat("ValidDataOneof", type_name, |
|
|
|
|
".MultipleValuesForDifferentField"), |
|
|
|
|
REQUIRED, proto, text); |
|
|
|
|
RunValidBinaryProtobufTest( |
|
|
|
|
absl::StrCat("ValidDataOneofBinary", type_name, |
|
|
|
|
RunValidBinaryProtobufTest(absl::StrCat("ValidDataOneofBinary", type_name, |
|
|
|
|
".MultipleValuesForDifferentField"), |
|
|
|
|
RECOMMENDED, proto, expected_proto); |
|
|
|
|
} |
|
|
|
@ -1166,21 +1176,21 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestValidDataForOneofType( |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestMergeOneofMessage() { |
|
|
|
|
string field1_data = |
|
|
|
|
std::string field1_data = |
|
|
|
|
absl::StrCat(tag(1, WireFormatLite::WIRETYPE_VARINT), varint(1)); |
|
|
|
|
string field2a_data = |
|
|
|
|
std::string field2a_data = |
|
|
|
|
absl::StrCat(tag(2, WireFormatLite::WIRETYPE_VARINT), varint(1)); |
|
|
|
|
string field2b_data = |
|
|
|
|
std::string field2b_data = |
|
|
|
|
absl::StrCat(tag(2, WireFormatLite::WIRETYPE_VARINT), varint(1)); |
|
|
|
|
string field89_data = |
|
|
|
|
std::string field89_data = |
|
|
|
|
absl::StrCat(tag(89, WireFormatLite::WIRETYPE_VARINT), varint(1)); |
|
|
|
|
string submsg1_data = absl::StrCat( |
|
|
|
|
std::string submsg1_data = absl::StrCat( |
|
|
|
|
tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(field1_data, field2a_data, field89_data))); |
|
|
|
|
string submsg2_data = |
|
|
|
|
std::string submsg2_data = |
|
|
|
|
absl::StrCat(tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(field2b_data, field89_data))); |
|
|
|
|
string merged_data = |
|
|
|
|
std::string merged_data = |
|
|
|
|
absl::StrCat(tag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(absl::StrCat(field1_data, field2b_data, field89_data, |
|
|
|
|
field89_data))); |
|
|
|
@ -1188,32 +1198,32 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestMergeOneofMessage() { |
|
|
|
|
const FieldDescriptor* field = |
|
|
|
|
GetFieldForOneofType(FieldDescriptor::TYPE_MESSAGE); |
|
|
|
|
|
|
|
|
|
string proto1 = absl::StrCat( |
|
|
|
|
std::string proto1 = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(submsg1_data)); |
|
|
|
|
string proto2 = absl::StrCat( |
|
|
|
|
std::string proto2 = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(submsg2_data)); |
|
|
|
|
string proto = absl::StrCat(proto1, proto2); |
|
|
|
|
string expected_proto = absl::StrCat( |
|
|
|
|
std::string proto = absl::StrCat(proto1, proto2); |
|
|
|
|
std::string expected_proto = absl::StrCat( |
|
|
|
|
tag(field->number(), WireFormatLite::WIRETYPE_LENGTH_DELIMITED), |
|
|
|
|
delim(merged_data)); |
|
|
|
|
|
|
|
|
|
MessageType test_message; |
|
|
|
|
test_message.MergeFromString(expected_proto); |
|
|
|
|
string text; |
|
|
|
|
std::string text; |
|
|
|
|
TextFormat::PrintToString(test_message, &text); |
|
|
|
|
RunValidProtobufTest("ValidDataOneof.MESSAGE.Merge", REQUIRED, proto, text); |
|
|
|
|
RunValidBinaryProtobufTest("ValidDataOneofBinary.MESSAGE.Merge", |
|
|
|
|
RECOMMENDED, proto, expected_proto); |
|
|
|
|
RunValidBinaryProtobufTest("ValidDataOneofBinary.MESSAGE.Merge", RECOMMENDED, |
|
|
|
|
proto, expected_proto); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestIllegalTags() { |
|
|
|
|
// field num 0 is illegal
|
|
|
|
|
string nullfield[] = {"\1DEADBEEF", "\2\1\1", "\3\4", "\5DEAD"}; |
|
|
|
|
std::string nullfield[] = {"\1DEADBEEF", "\2\1\1", "\3\4", "\5DEAD"}; |
|
|
|
|
for (int i = 0; i < 4; i++) { |
|
|
|
|
string name = "IllegalZeroFieldNum_Case_0"; |
|
|
|
|
std::string name = "IllegalZeroFieldNum_Case_0"; |
|
|
|
|
name.back() += i; |
|
|
|
|
ExpectParseFailureForProto(nullfield[i], name, REQUIRED); |
|
|
|
|
} |
|
|
|
@ -1267,35 +1277,34 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::TestUnknownMessage() { |
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
MessageType>::TestBinaryPerformanceForAlternatingUnknownFields() { |
|
|
|
|
string unknown_field_1 = absl::StrCat( |
|
|
|
|
std::string unknown_field_1 = absl::StrCat( |
|
|
|
|
tag(UNKNOWN_FIELD, WireFormatLite::WIRETYPE_VARINT), varint(1234)); |
|
|
|
|
string unknown_field_2 = absl::StrCat( |
|
|
|
|
std::string unknown_field_2 = absl::StrCat( |
|
|
|
|
tag(UNKNOWN_FIELD + 1, WireFormatLite::WIRETYPE_VARINT), varint(5678)); |
|
|
|
|
string proto; |
|
|
|
|
std::string proto; |
|
|
|
|
for (size_t i = 0; i < kPerformanceRepeatCount; i++) { |
|
|
|
|
proto.append(unknown_field_1); |
|
|
|
|
proto.append(unknown_field_2); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
RunValidBinaryProtobufTest( |
|
|
|
|
"TestBinaryPerformanceForAlternatingUnknownFields", RECOMMENDED, proto); |
|
|
|
|
RunValidBinaryProtobufTest("TestBinaryPerformanceForAlternatingUnknownFields", |
|
|
|
|
RECOMMENDED, proto); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
TestBinaryPerformanceMergeMessageWithRepeatedFieldForType( |
|
|
|
|
FieldDescriptor::Type type) { |
|
|
|
|
const string type_name = |
|
|
|
|
const std::string type_name = |
|
|
|
|
UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type))); |
|
|
|
|
int field_number = GetFieldForType(type, true, Packed::kFalse)->number(); |
|
|
|
|
string rep_field_proto = absl::StrCat( |
|
|
|
|
std::string rep_field_proto = absl::StrCat( |
|
|
|
|
tag(field_number, WireFormatLite::WireTypeForFieldType( |
|
|
|
|
static_cast<WireFormatLite::FieldType>(type))), |
|
|
|
|
GetNonDefaultValue(type)); |
|
|
|
|
|
|
|
|
|
RunBinaryPerformanceMergeMessageWithField( |
|
|
|
|
absl::StrCat( |
|
|
|
|
"TestBinaryPerformanceMergeMessageWithRepeatedFieldForType", |
|
|
|
|
absl::StrCat("TestBinaryPerformanceMergeMessageWithRepeatedFieldForType", |
|
|
|
|
type_name), |
|
|
|
|
rep_field_proto); |
|
|
|
|
} |
|
|
|
@ -1304,9 +1313,9 @@ template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
TestBinaryPerformanceMergeMessageWithUnknownFieldForType( |
|
|
|
|
FieldDescriptor::Type type) { |
|
|
|
|
const string type_name = |
|
|
|
|
const std::string type_name = |
|
|
|
|
UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type))); |
|
|
|
|
string unknown_field_proto = absl::StrCat( |
|
|
|
|
std::string unknown_field_proto = absl::StrCat( |
|
|
|
|
tag(UNKNOWN_FIELD, WireFormatLite::WireTypeForFieldType( |
|
|
|
|
static_cast<WireFormatLite::FieldType>(type))), |
|
|
|
|
GetNonDefaultValue(type)); |
|
|
|
@ -1619,25 +1628,25 @@ void BinaryAndJsonConformanceSuiteImpl<MessageType>::RunJsonPerformanceTests() { |
|
|
|
|
template <typename MessageType> |
|
|
|
|
void BinaryAndJsonConformanceSuiteImpl<MessageType>:: |
|
|
|
|
TestJsonPerformanceMergeMessageWithRepeatedFieldForType( |
|
|
|
|
FieldDescriptor::Type type, string field_value) { |
|
|
|
|
const string type_name = |
|
|
|
|
FieldDescriptor::Type type, std::string field_value) { |
|
|
|
|
const std::string type_name = |
|
|
|
|
UpperCase(absl::StrCat(".", FieldDescriptor::TypeName(type))); |
|
|
|
|
const FieldDescriptor* field = GetFieldForType(type, true, Packed::kFalse); |
|
|
|
|
string field_name = field->name(); |
|
|
|
|
std::string field_name = field->name(); |
|
|
|
|
|
|
|
|
|
string message_field = |
|
|
|
|
std::string message_field = |
|
|
|
|
absl::StrCat("\"", field_name, "\": [", field_value, "]"); |
|
|
|
|
string recursive_message = |
|
|
|
|
std::string recursive_message = |
|
|
|
|
absl::StrCat("\"recursive_message\": { ", message_field, "}"); |
|
|
|
|
string input = absl::StrCat("{", recursive_message); |
|
|
|
|
std::string input = absl::StrCat("{", recursive_message); |
|
|
|
|
for (size_t i = 1; i < kPerformanceRepeatCount; i++) { |
|
|
|
|
absl::StrAppend(&input, ",", recursive_message); |
|
|
|
|
} |
|
|
|
|
absl::StrAppend(&input, "}"); |
|
|
|
|
|
|
|
|
|
string textproto_message_field = |
|
|
|
|
std::string textproto_message_field = |
|
|
|
|
absl::StrCat(field_name, ": ", field_value); |
|
|
|
|
string expected_textproto = "recursive_message { "; |
|
|
|
|
std::string expected_textproto = "recursive_message { "; |
|
|
|
|
for (size_t i = 0; i < kPerformanceRepeatCount; i++) { |
|
|
|
|
absl::StrAppend(&expected_textproto, textproto_message_field, " "); |
|
|
|
|
} |
|
|
|
@ -1729,9 +1738,9 @@ void BinaryAndJsonConformanceSuiteImpl< |
|
|
|
|
// * when not ignored, the parser should fail.
|
|
|
|
|
struct TestCase { |
|
|
|
|
// Used in the test name.
|
|
|
|
|
string enum_location; |
|
|
|
|
std::string enum_location; |
|
|
|
|
// JSON input which will contain the unknown field.
|
|
|
|
|
string input_json; |
|
|
|
|
std::string input_json; |
|
|
|
|
}; |
|
|
|
|
const std::vector<TestCase> test_cases = { |
|
|
|
|
{"InOptionalField", R"json({ |
|
|
|
|