Clean up conformance tests warnings and formatting.

PiperOrigin-RevId: 574304654
pull/14421/head
Mike Kruskal 1 year ago committed by Copybara-Service
parent cb86ad6d81
commit 39752aaea3
  1. 379
      conformance/binary_json_conformance_suite.cc
  2. 1
      conformance/binary_json_conformance_suite.h
  3. 64
      conformance/conformance_test.cc
  4. 10
      conformance/conformance_test.h
  5. 53
      conformance/text_format_conformance_suite.cc
  6. 3
      conformance/text_format_conformance_suite.h

@ -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({

@ -8,6 +8,7 @@
#ifndef CONFORMANCE_BINARY_JSON_CONFORMANCE_SUITE_H
#define CONFORMANCE_BINARY_JSON_CONFORMANCE_SUITE_H
#include <functional>
#include <memory>
#include <string>
#include <utility>

@ -9,12 +9,15 @@
#include <stdarg.h>
#include <cstddef>
#include <cstdint>
#include <fstream>
#include <memory>
#include <string>
#include "google/protobuf/util/field_comparator.h"
#include "google/protobuf/util/json_util.h"
#include "google/protobuf/util/message_differencer.h"
#include "absl/log/absl_check.h"
#include "absl/log/absl_log.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
@ -28,15 +31,14 @@
using conformance::ConformanceRequest;
using conformance::ConformanceResponse;
using conformance::WireFormat;
using google::protobuf::TextFormat;
using google::protobuf::util::DefaultFieldComparator;
using google::protobuf::util::MessageDifferencer;
using std::string;
namespace {
static string ToOctString(const string& binary_string) {
string oct_string;
static std::string ToOctString(const std::string& binary_string) {
std::string oct_string;
for (size_t i = 0; i < binary_string.size(); i++) {
uint8_t c = binary_string.at(i);
uint8_t high = c / 64;
@ -96,7 +98,7 @@ ConformanceTestSuite::ConformanceRequestSetting::ConformanceRequestSetting(
ConformanceLevel level, conformance::WireFormat input_format,
conformance::WireFormat output_format,
conformance::TestCategory test_category, const Message& prototype_message,
const string& test_name, const string& input)
const std::string& test_name, const std::string& input)
: level_(level),
input_format_(input_format),
output_format_(output_format),
@ -139,8 +141,9 @@ ConformanceTestSuite::ConformanceRequestSetting::NewTestMessage() const {
return std::unique_ptr<Message>(prototype_message_for_compare_->New());
}
string ConformanceTestSuite::ConformanceRequestSetting::GetTestName() const {
string rname;
std::string ConformanceTestSuite::ConformanceRequestSetting::GetTestName()
const {
std::string rname;
switch (FileDescriptorLegacy(prototype_message_.GetDescriptor()->file())
.syntax()) {
case FileDescriptorLegacy::Syntax::SYNTAX_PROTO3:
@ -158,7 +161,7 @@ string ConformanceTestSuite::ConformanceRequestSetting::GetTestName() const {
OutputFormatString(output_format_));
}
string
std::string
ConformanceTestSuite::ConformanceRequestSetting::ConformanceLevelToString(
ConformanceLevel level) const {
switch (level) {
@ -171,7 +174,7 @@ ConformanceTestSuite::ConformanceRequestSetting::ConformanceLevelToString(
return "";
}
string ConformanceTestSuite::ConformanceRequestSetting::InputFormatString(
std::string ConformanceTestSuite::ConformanceRequestSetting::InputFormatString(
conformance::WireFormat format) const {
switch (format) {
case conformance::PROTOBUF:
@ -186,7 +189,7 @@ string ConformanceTestSuite::ConformanceRequestSetting::InputFormatString(
return "";
}
string ConformanceTestSuite::ConformanceRequestSetting::OutputFormatString(
std::string ConformanceTestSuite::ConformanceRequestSetting::OutputFormatString(
conformance::WireFormat format) const {
switch (format) {
case conformance::PROTOBUF:
@ -208,7 +211,7 @@ void ConformanceTestSuite::TruncateDebugPayload(string* payload) {
}
}
const ConformanceRequest ConformanceTestSuite::TruncateRequest(
ConformanceRequest ConformanceTestSuite::TruncateRequest(
const ConformanceRequest& request) {
ConformanceRequest debug_request(request);
switch (debug_request.payload_case()) {
@ -231,7 +234,7 @@ const ConformanceRequest ConformanceTestSuite::TruncateRequest(
return debug_request;
}
const ConformanceResponse ConformanceTestSuite::TruncateResponse(
ConformanceResponse ConformanceTestSuite::TruncateResponse(
const ConformanceResponse& response) {
ConformanceResponse debug_response(response);
switch (debug_response.result_case()) {
@ -254,7 +257,7 @@ const ConformanceResponse ConformanceTestSuite::TruncateResponse(
return debug_response;
}
void ConformanceTestSuite::ReportSuccess(const string& test_name) {
void ConformanceTestSuite::ReportSuccess(const std::string& test_name) {
if (expected_to_fail_.erase(test_name) != 0) {
absl::StrAppendFormat(
&output_,
@ -266,7 +269,7 @@ void ConformanceTestSuite::ReportSuccess(const string& test_name) {
successes_++;
}
void ConformanceTestSuite::ReportFailure(const string& test_name,
void ConformanceTestSuite::ReportFailure(const std::string& test_name,
ConformanceLevel level,
const ConformanceRequest& request,
const ConformanceResponse& response,
@ -286,7 +289,7 @@ void ConformanceTestSuite::ReportFailure(const string& test_name,
TruncateResponse(response).ShortDebugString());
}
void ConformanceTestSuite::ReportSkip(const string& test_name,
void ConformanceTestSuite::ReportSkip(const std::string& test_name,
const ConformanceRequest& request,
const ConformanceResponse& response) {
if (verbose_) {
@ -299,19 +302,20 @@ void ConformanceTestSuite::ReportSkip(const string& test_name,
void ConformanceTestSuite::RunValidInputTest(
const ConformanceRequestSetting& setting,
const string& equivalent_text_format) {
const std::string& equivalent_text_format) {
std::unique_ptr<Message> reference_message(setting.NewTestMessage());
ABSL_CHECK(TextFormat::ParseFromString(equivalent_text_format,
reference_message.get()))
<< "Failed to parse data for test case: " << setting.GetTestName()
<< ", data: " << equivalent_text_format;
const string equivalent_wire_format = reference_message->SerializeAsString();
const std::string equivalent_wire_format =
reference_message->SerializeAsString();
RunValidBinaryInputTest(setting, equivalent_wire_format);
}
void ConformanceTestSuite::RunValidBinaryInputTest(
const ConformanceRequestSetting& setting,
const string& equivalent_wire_format, bool require_same_wire_format) {
const std::string& equivalent_wire_format, bool require_same_wire_format) {
const ConformanceRequest& request = setting.GetRequest();
ConformanceResponse response;
RunTest(setting.GetTestName(), request, &response);
@ -321,11 +325,12 @@ void ConformanceTestSuite::RunValidBinaryInputTest(
void ConformanceTestSuite::VerifyResponse(
const ConformanceRequestSetting& setting,
const string& equivalent_wire_format, const ConformanceResponse& response,
bool need_report_success, bool require_same_wire_format) {
const std::string& equivalent_wire_format,
const ConformanceResponse& response, bool need_report_success,
bool require_same_wire_format) {
std::unique_ptr<Message> test_message(setting.NewTestMessage());
const ConformanceRequest& request = setting.GetRequest();
const string& test_name = setting.GetTestName();
const std::string& test_name = setting.GetTestName();
ConformanceLevel level = setting.GetLevel();
std::unique_ptr<Message> reference_message = setting.NewTestMessage();
@ -358,7 +363,7 @@ void ConformanceTestSuite::VerifyResponse(
DefaultFieldComparator field_comparator;
field_comparator.set_treat_nan_as_equal(true);
differencer.set_field_comparator(&field_comparator);
string differences;
std::string differences;
differencer.ReportDifferencesToString(&differences);
bool check = false;
@ -366,7 +371,7 @@ void ConformanceTestSuite::VerifyResponse(
if (require_same_wire_format) {
ABSL_DCHECK_EQ(response.result_case(),
ConformanceResponse::kProtobufPayload);
const string& protobuf_payload = response.protobuf_payload();
const std::string& protobuf_payload = response.protobuf_payload();
check = equivalent_wire_format == protobuf_payload;
differences = absl::StrCat("Expect: ", ToOctString(equivalent_wire_format),
", but got: ", ToOctString(protobuf_payload));
@ -386,15 +391,15 @@ void ConformanceTestSuite::VerifyResponse(
}
}
void ConformanceTestSuite::RunTest(const string& test_name,
void ConformanceTestSuite::RunTest(const std::string& test_name,
const ConformanceRequest& request,
ConformanceResponse* response) {
if (test_names_.insert(test_name).second == false) {
ABSL_LOG(FATAL) << "Duplicated test name: " << test_name;
}
string serialized_request;
string serialized_response;
std::string serialized_request;
std::string serialized_response;
request.SerializeToString(&serialized_request);
runner_->RunTest(test_name, serialized_request, &serialized_response);
@ -412,7 +417,7 @@ void ConformanceTestSuite::RunTest(const string& test_name,
}
}
string ConformanceTestSuite::WireFormatToString(WireFormat wire_format) {
std::string ConformanceTestSuite::WireFormatToString(WireFormat wire_format) {
switch (wire_format) {
case conformance::PROTOBUF:
return "PROTOBUF";
@ -435,7 +440,8 @@ void ConformanceTestSuite::AddExpectedFailedTest(const std::string& test_name) {
}
bool ConformanceTestSuite::RunSuite(ConformanceTestRunner* runner,
std::string* output, const string& filename,
std::string* output,
const std::string& filename,
conformance::FailureSet* failure_list) {
runner_ = runner;
successes_ = 0;
@ -449,7 +455,7 @@ bool ConformanceTestSuite::RunSuite(ConformanceTestRunner* runner,
failure_list_filename_ = filename;
expected_to_fail_.clear();
for (const string& failure : failure_list->failure()) {
for (const std::string& failure : failure_list->failure()) {
AddExpectedFailedTest(failure);
}
RunSuiteImpl();

@ -14,13 +14,15 @@
#ifndef CONFORMANCE_CONFORMANCE_TEST_H
#define CONFORMANCE_CONFORMANCE_TEST_H
#include <functional>
#include <cstddef>
#include <memory>
#include <string>
#include <vector>
#include "google/protobuf/util/type_resolver.h"
#include "absl/container/btree_set.h"
#include "absl/container/flat_hash_set.h"
#include "absl/strings/string_view.h"
#include "conformance/conformance.pb.h"
#include "google/protobuf/descriptor.h"
#include "google/protobuf/wire_format_lite.h"
@ -204,7 +206,7 @@ class ConformanceTestSuite {
return request_;
}
const ConformanceLevel GetLevel() const { return level_; }
ConformanceLevel GetLevel() const { return level_; }
std::string ConformanceLevelToString(ConformanceLevel level) const;
@ -245,9 +247,9 @@ class ConformanceTestSuite {
bool need_report_success, bool require_same_wire_format);
void TruncateDebugPayload(std::string* payload);
const conformance::ConformanceRequest TruncateRequest(
conformance::ConformanceRequest TruncateRequest(
const conformance::ConformanceRequest& request);
const conformance::ConformanceResponse TruncateResponse(
conformance::ConformanceResponse TruncateResponse(
const conformance::ConformanceResponse& response);
void ReportSuccess(const std::string& test_name);

@ -7,7 +7,10 @@
#include "text_format_conformance_suite.h"
#include "google/protobuf/any.pb.h"
#include <cstddef>
#include <string>
#include <vector>
#include "absl/log/absl_log.h"
#include "absl/strings/str_cat.h"
#include "conformance_test.h"
@ -20,12 +23,9 @@ namespace proto2_messages = protobuf_test_messages::proto2;
using conformance::ConformanceRequest;
using conformance::ConformanceResponse;
using conformance::WireFormat;
using google::protobuf::Message;
using google::protobuf::TextFormat;
using proto2_messages::TestAllTypesProto2;
using proto2_messages::UnknownToTestAllTypes;
using protobuf_test_messages::proto3::TestAllTypesProto3;
using std::string;
namespace google {
namespace protobuf {
@ -61,7 +61,7 @@ bool TextFormatConformanceTestSuite::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()) {
@ -111,9 +111,9 @@ bool TextFormatConformanceTestSuite::ParseResponse(
return true;
}
void TextFormatConformanceTestSuite::ExpectParseFailure(const string& test_name,
ConformanceLevel level,
const string& input) {
void TextFormatConformanceTestSuite::ExpectParseFailure(
const std::string& test_name, ConformanceLevel level,
const std::string& input) {
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.
@ -122,7 +122,7 @@ void TextFormatConformanceTestSuite::ExpectParseFailure(const string& test_name,
conformance::TEXT_FORMAT_TEST, prototype, test_name, input);
const ConformanceRequest& request = setting.GetRequest();
ConformanceResponse response;
string effective_test_name =
std::string effective_test_name =
absl::StrCat(setting.ConformanceLevelToString(level),
".Proto3.TextFormatInput.", test_name);
@ -138,36 +138,38 @@ void TextFormatConformanceTestSuite::ExpectParseFailure(const string& test_name,
}
void TextFormatConformanceTestSuite::RunValidTextFormatTest(
const string& test_name, ConformanceLevel level, const string& input_text) {
const std::string& test_name, ConformanceLevel level,
const std::string& input_text) {
TestAllTypesProto3 prototype;
RunValidTextFormatTestWithMessage(test_name, level, input_text, prototype);
}
void TextFormatConformanceTestSuite::RunValidTextFormatTestProto2(
const string& test_name, ConformanceLevel level, const string& input_text) {
const std::string& test_name, ConformanceLevel level,
const std::string& input_text) {
TestAllTypesProto2 prototype;
RunValidTextFormatTestWithMessage(test_name, level, input_text, prototype);
}
void TextFormatConformanceTestSuite::RunValidTextFormatTestWithExpected(
const string& test_name, ConformanceLevel level, const string& input_text,
const string& expected_text) {
const std::string& test_name, ConformanceLevel level,
const std::string& input_text, const std::string& expected_text) {
TestAllTypesProto3 prototype;
RunValidTextFormatTestWithMessage(test_name, level, input_text, expected_text,
prototype);
}
void TextFormatConformanceTestSuite::RunValidTextFormatTestProto2WithExpected(
const string& test_name, ConformanceLevel level, const string& input_text,
const string& expected_text) {
const std::string& test_name, ConformanceLevel level,
const std::string& input_text, const std::string& expected_text) {
TestAllTypesProto2 prototype;
RunValidTextFormatTestWithMessage(test_name, level, input_text, expected_text,
prototype);
}
void TextFormatConformanceTestSuite::RunValidTextFormatTestWithMessage(
const string& test_name, ConformanceLevel level, const string& input_text,
const Message& prototype) {
const std::string& test_name, ConformanceLevel level,
const std::string& input_text, const Message& prototype) {
ConformanceRequestSetting setting1(
level, conformance::TEXT_FORMAT, conformance::PROTOBUF,
conformance::TEXT_FORMAT_TEST, prototype, test_name, input_text);
@ -179,8 +181,9 @@ void TextFormatConformanceTestSuite::RunValidTextFormatTestWithMessage(
}
void TextFormatConformanceTestSuite::RunValidTextFormatTestWithMessage(
const string& test_name, ConformanceLevel level, const string& input_text,
const string& expected_text, const Message& prototype) {
const std::string& test_name, ConformanceLevel level,
const std::string& input_text, const std::string& expected_text,
const Message& prototype) {
ConformanceRequestSetting setting1(
level, conformance::TEXT_FORMAT, conformance::PROTOBUF,
conformance::TEXT_FORMAT_TEST, prototype, test_name, input_text);
@ -192,8 +195,8 @@ void TextFormatConformanceTestSuite::RunValidTextFormatTestWithMessage(
}
void TextFormatConformanceTestSuite::RunValidUnknownTextFormatTest(
const string& test_name, const Message& message) {
string serialized_input;
const std::string& test_name, const Message& message) {
std::string serialized_input;
message.SerializeToString(&serialized_input);
TestAllTypesProto3 prototype;
ConformanceRequestSetting setting1(
@ -509,16 +512,16 @@ void TextFormatConformanceTestSuite::RunTextFormatPerformanceTests() {
// This is currently considered valid input by some languages but not others
void TextFormatConformanceTestSuite::
TestTextFormatPerformanceMergeMessageWithRepeatedField(
const string& test_type_name, const string& message_field) {
string recursive_message =
const std::string& test_type_name, const std::string& message_field) {
std::string recursive_message =
absl::StrCat("recursive_message { ", message_field, " }");
string input;
std::string input;
for (size_t i = 0; i < kPerformanceRepeatCount; i++) {
absl::StrAppend(&input, recursive_message);
}
string expected = "recursive_message { ";
std::string expected = "recursive_message { ";
for (size_t i = 0; i < kPerformanceRepeatCount; i++) {
absl::StrAppend(&expected, message_field, " ");
}

@ -8,7 +8,10 @@
#ifndef TEXT_FORMAT_CONFORMANCE_SUITE_H_
#define TEXT_FORMAT_CONFORMANCE_SUITE_H_
#include <string>
#include "conformance_test.h"
#include "google/protobuf/message.h"
namespace google {
namespace protobuf {

Loading…
Cancel
Save