| 1 |
// Protocol Buffers - Google's data interchange format |
| 2 |
// Copyright 2008 Google Inc. All rights reserved. |
| 3 |
// https://developers.google.com/protocol-buffers/ |
| 4 |
// |
| 5 |
// Redistribution and use in source and binary forms, with or without |
| 6 |
// modification, are permitted provided that the following conditions are |
| 7 |
// met: |
| 8 |
// |
| 9 |
// * Redistributions of source code must retain the above copyright |
| 10 |
// notice, this list of conditions and the following disclaimer. |
| 11 |
// * Redistributions in binary form must reproduce the above |
| 12 |
// copyright notice, this list of conditions and the following disclaimer |
| 13 |
// in the documentation and/or other materials provided with the |
| 14 |
// distribution. |
| 15 |
// * Neither the name of Google Inc. nor the names of its |
| 16 |
// contributors may be used to endorse or promote products derived from |
| 17 |
// this software without specific prior written permission. |
| 18 |
// |
| 19 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
|
| 31 |
#include "text_format_conformance_suite.h" |
| 32 |
|
| 33 |
#include "google/protobuf/any.pb.h" |
| 34 |
#include "google/protobuf/text_format.h" |
| 35 |
#include "absl/log/absl_log.h" |
| 36 |
#include "absl/strings/str_cat.h" |
| 37 |
#include "conformance_test.h" |
| 38 |
#include "google/protobuf/test_messages_proto2.pb.h" |
| 39 |
#include "google/protobuf/test_messages_proto3.pb.h" |
| 40 |
|
| 41 |
namespace proto2_messages = protobuf_test_messages::proto2; |
| 42 |
|
| 43 |
using conformance::ConformanceRequest; |
| 44 |
using conformance::ConformanceResponse; |
| 45 |
using conformance::WireFormat; |
| 46 |
using google::protobuf::Message; |
| 47 |
using google::protobuf::TextFormat; |
| 48 |
using proto2_messages::TestAllTypesProto2; |
| 49 |
using proto2_messages::UnknownToTestAllTypes; |
| 50 |
using protobuf_test_messages::proto3::TestAllTypesProto3; |
| 51 |
using std::string; |
| 52 |
|
| 53 |
namespace google { |
| 54 |
namespace protobuf { |
| 55 |
|
| 56 |
// The number of repetitions to use for performance tests. |
| 57 |
// Corresponds approx to 500KB wireformat bytes. |
| 58 |
static const size_t kPerformanceRepeatCount = 50000; |
| 59 |
|
| 60 |
TextFormatConformanceTestSuite::TextFormatConformanceTestSuite() { |
| 61 |
SetFailureListFlagName("--text_format_failure_list"); |
| 62 |
} |
| 63 |
|
| 64 |
bool TextFormatConformanceTestSuite::ParseTextFormatResponse( |
| 65 |
const ConformanceResponse& response, |
| 66 |
const ConformanceRequestSetting& setting, Message* test_message) { |
| 67 |
TextFormat::Parser parser; |
| 68 |
const ConformanceRequest& request = setting.GetRequest(); |
| 69 |
if (request.print_unknown_fields()) { |
| 70 |
parser.AllowFieldNumber(true); |
| 71 |
} |
| 72 |
if (!parser.ParseFromString(response.text_payload(), test_message)) { |
| 73 |
ABSL_LOG(ERROR) << "INTERNAL ERROR: internal text->protobuf transcode " |
| 74 |
<< "yielded unparseable proto. Text payload: " |
| 75 |
<< response.text_payload(); |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
bool TextFormatConformanceTestSuite::ParseResponse( |
| 83 |
const ConformanceResponse& response, |
| 84 |
const ConformanceRequestSetting& setting, Message* test_message) { |
| 85 |
const ConformanceRequest& request = setting.GetRequest(); |
| 86 |
WireFormat requested_output = request.requested_output_format(); |
| 87 |
const string& test_name = setting.GetTestName(); |
| 88 |
ConformanceLevel level = setting.GetLevel(); |
| 89 |
|
| 90 |
switch (response.result_case()) { |
| 91 |
case ConformanceResponse::kProtobufPayload: { |
| 92 |
if (requested_output != conformance::PROTOBUF) { |
| 93 |
ReportFailure(test_name, level, request, response, |
| 94 |
absl::StrCat("Test was asked for ", |
| 95 |
WireFormatToString(requested_output), |
| 96 |
" output but provided PROTOBUF instead.")); |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
if (!test_message->ParseFromString(response.protobuf_payload())) { |
| 101 |
ReportFailure(test_name, level, request, response, |
| 102 |
"Protobuf output we received from test was unparseable."); |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
break; |
| 107 |
} |
| 108 |
|
| 109 |
case ConformanceResponse::kTextPayload: { |
| 110 |
if (requested_output != conformance::TEXT_FORMAT) { |
| 111 |
ReportFailure( |
| 112 |
test_name, level, request, response, |
| 113 |
absl::StrCat("Test was asked for ", |
| 114 |
WireFormatToString(requested_output), |
| 115 |
" output but provided TEXT_FORMAT instead.")); |
| 116 |
return false; |
| 117 |
} |
| 118 |
|
| 119 |
if (!ParseTextFormatResponse(response, setting, test_message)) { |
| 120 |
ReportFailure( |
| 121 |
test_name, level, request, response, |
| 122 |
"TEXT_FORMAT output we received from test was unparseable."); |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
break; |
| 127 |
} |
| 128 |
|
| 129 |
default: |
| 130 |
ABSL_LOG(FATAL) << test_name |
| 131 |
<< ": unknown payload type: " << response.result_case(); |
| 132 |
} |
| 133 |
|
| 134 |
return true; |
| 135 |
} |
| 136 |
|
| 137 |
void TextFormatConformanceTestSuite::ExpectParseFailure(const string& test_name, |
| 138 |
ConformanceLevel level, |
| 139 |
const string& input) { |
| 140 |
TestAllTypesProto3 prototype; |
| 141 |
// We don't expect output, but if the program erroneously accepts the protobuf |
| 142 |
// we let it send its response as this. We must not leave it unspecified. |
| 143 |
ConformanceRequestSetting setting( |
| 144 |
level, conformance::TEXT_FORMAT, conformance::TEXT_FORMAT, |
| 145 |
conformance::TEXT_FORMAT_TEST, prototype, test_name, input); |
| 146 |
const ConformanceRequest& request = setting.GetRequest(); |
| 147 |
ConformanceResponse response; |
| 148 |
string effective_test_name = |
| 149 |
absl::StrCat(setting.ConformanceLevelToString(level), |
| 150 |
".Proto3.TextFormatInput.", test_name); |
| 151 |
|
| 152 |
RunTest(effective_test_name, request, &response); |
| 153 |
if (response.result_case() == ConformanceResponse::kParseError) { |
| 154 |
ReportSuccess(effective_test_name); |
| 155 |
} else if (response.result_case() == ConformanceResponse::kSkipped) { |
| 156 |
ReportSkip(effective_test_name, request, response); |
| 157 |
} else { |
| 158 |
ReportFailure(effective_test_name, level, request, response, |
| 159 |
"Should have failed to parse, but didn't."); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
void TextFormatConformanceTestSuite::RunValidTextFormatTest( |
| 164 |
const string& test_name, ConformanceLevel level, const string& input_text) { |
| 165 |
TestAllTypesProto3 prototype; |
| 166 |
RunValidTextFormatTestWithMessage(test_name, level, input_text, prototype); |
| 167 |
} |
| 168 |
|
| 169 |
void TextFormatConformanceTestSuite::RunValidTextFormatTestProto2( |
| 170 |
const string& test_name, ConformanceLevel level, const string& input_text) { |
| 171 |
TestAllTypesProto2 prototype; |
| 172 |
RunValidTextFormatTestWithMessage(test_name, level, input_text, prototype); |
| 173 |
} |
| 174 |
|
| 175 |
void TextFormatConformanceTestSuite::RunValidTextFormatTestWithExpected( |
| 176 |
const string& test_name, ConformanceLevel level, const string& input_text, |
| 177 |
const string& expected_text) { |
| 178 |
TestAllTypesProto3 prototype; |
| 179 |
RunValidTextFormatTestWithMessage(test_name, level, input_text, expected_text, |
| 180 |
prototype); |
| 181 |
} |
| 182 |
|
| 183 |
void TextFormatConformanceTestSuite::RunValidTextFormatTestProto2WithExpected( |
| 184 |
const string& test_name, ConformanceLevel level, const string& input_text, |
| 185 |
const string& expected_text) { |
| 186 |
TestAllTypesProto2 prototype; |
| 187 |
RunValidTextFormatTestWithMessage(test_name, level, input_text, expected_text, |
| 188 |
prototype); |
| 189 |
} |
| 190 |
|
| 191 |
void TextFormatConformanceTestSuite::RunValidTextFormatTestWithMessage( |
| 192 |
const string& test_name, ConformanceLevel level, const string& input_text, |
| 193 |
const Message& prototype) { |
| 194 |
ConformanceRequestSetting setting1( |
| 195 |
level, conformance::TEXT_FORMAT, conformance::PROTOBUF, |
| 196 |
conformance::TEXT_FORMAT_TEST, prototype, test_name, input_text); |
| 197 |
RunValidInputTest(setting1, input_text); |
| 198 |
ConformanceRequestSetting setting2( |
| 199 |
level, conformance::TEXT_FORMAT, conformance::TEXT_FORMAT, |
| 200 |
conformance::TEXT_FORMAT_TEST, prototype, test_name, input_text); |
| 201 |
RunValidInputTest(setting2, input_text); |
| 202 |
} |
| 203 |
|
| 204 |
void TextFormatConformanceTestSuite::RunValidTextFormatTestWithMessage( |
| 205 |
const string& test_name, ConformanceLevel level, const string& input_text, |
| 206 |
const string& expected_text, const Message& prototype) { |
| 207 |
ConformanceRequestSetting setting1( |
| 208 |
level, conformance::TEXT_FORMAT, conformance::PROTOBUF, |
| 209 |
conformance::TEXT_FORMAT_TEST, prototype, test_name, input_text); |
| 210 |
RunValidInputTest(setting1, expected_text); |
| 211 |
ConformanceRequestSetting setting2( |
| 212 |
level, conformance::TEXT_FORMAT, conformance::TEXT_FORMAT, |
| 213 |
conformance::TEXT_FORMAT_TEST, prototype, test_name, input_text); |
| 214 |
RunValidInputTest(setting2, expected_text); |
| 215 |
} |
| 216 |
|
| 217 |
void TextFormatConformanceTestSuite::RunValidUnknownTextFormatTest( |
| 218 |
const string& test_name, const Message& message) { |
| 219 |
string serialized_input; |
| 220 |
message.SerializeToString(&serialized_input); |
| 221 |
TestAllTypesProto3 prototype; |
| 222 |
ConformanceRequestSetting setting1( |
| 223 |
RECOMMENDED, conformance::PROTOBUF, conformance::TEXT_FORMAT, |
| 224 |
conformance::TEXT_FORMAT_TEST, prototype, |
| 225 |
absl::StrCat(test_name, "_Drop"), serialized_input); |
| 226 |
setting1.SetPrototypeMessageForCompare(message); |
| 227 |
RunValidBinaryInputTest(setting1, ""); |
| 228 |
|
| 229 |
ConformanceRequestSetting setting2( |
| 230 |
RECOMMENDED, conformance::PROTOBUF, conformance::TEXT_FORMAT, |
| 231 |
conformance::TEXT_FORMAT_TEST, prototype, |
| 232 |
absl::StrCat(test_name, "_Print"), serialized_input); |
| 233 |
setting2.SetPrototypeMessageForCompare(message); |
| 234 |
setting2.SetPrintUnknownFields(true); |
| 235 |
RunValidBinaryInputTest(setting2, serialized_input); |
| 236 |
} |
| 237 |
|
| 238 |
void TextFormatConformanceTestSuite::RunSuiteImpl() { |
| 239 |
if (!performance_) { |
| 240 |
RunValidTextFormatTest("HelloWorld", REQUIRED, |
| 241 |
"optional_string: 'Hello, World!'"); |
| 242 |
// Integer fields. |
| 243 |
RunValidTextFormatTest("Int32FieldMaxValue", REQUIRED, |
| 244 |
"optional_int32: 2147483647"); |
| 245 |
RunValidTextFormatTest("Int32FieldMinValue", REQUIRED, |
| 246 |
"optional_int32: -2147483648"); |
| 247 |
RunValidTextFormatTest("Uint32FieldMaxValue", REQUIRED, |
| 248 |
"optional_uint32: 4294967295"); |
| 249 |
RunValidTextFormatTest("Int64FieldMaxValue", REQUIRED, |
| 250 |
"optional_int64: 9223372036854775807"); |
| 251 |
RunValidTextFormatTest("Int64FieldMinValue", REQUIRED, |
| 252 |
"optional_int64: -9223372036854775808"); |
| 253 |
RunValidTextFormatTest("Uint64FieldMaxValue", REQUIRED, |
| 254 |
"optional_uint64: 18446744073709551615"); |
| 255 |
|
| 256 |
// Parsers reject out-of-bound integer values. |
| 257 |
ExpectParseFailure("Int32FieldTooLarge", REQUIRED, |
| 258 |
"optional_int32: 2147483648"); |
| 259 |
ExpectParseFailure("Int32FieldTooSmall", REQUIRED, |
| 260 |
"optional_int32: -2147483649"); |
| 261 |
ExpectParseFailure("Uint32FieldTooLarge", REQUIRED, |
| 262 |
"optional_uint32: 4294967296"); |
| 263 |
ExpectParseFailure("Int64FieldTooLarge", REQUIRED, |
| 264 |
"optional_int64: 9223372036854775808"); |
| 265 |
ExpectParseFailure("Int64FieldTooSmall", REQUIRED, |
| 266 |
"optional_int64: -9223372036854775809"); |
| 267 |
ExpectParseFailure("Uint64FieldTooLarge", REQUIRED, |
| 268 |
"optional_uint64: 18446744073709551616"); |
| 269 |
|
| 270 |
// Floating point fields |
| 271 |
RunValidTextFormatTest("FloatField", REQUIRED, "optional_float: 3.192837"); |
| 272 |
RunValidTextFormatTest("FloatFieldWithVeryPreciseNumber", REQUIRED, |
| 273 |
"optional_float: 3.123456789123456789"); |
| 274 |
RunValidTextFormatTest("FloatFieldMaxValue", REQUIRED, |
| 275 |
"optional_float: 3.4028235e+38"); |
| 276 |
RunValidTextFormatTest("FloatFieldMinValue", REQUIRED, |
| 277 |
"optional_float: 1.17549e-38"); |
| 278 |
RunValidTextFormatTest("FloatFieldNaNValue", REQUIRED, |
| 279 |
"optional_float: NaN"); |
| 280 |
RunValidTextFormatTest("FloatFieldPosInfValue", REQUIRED, |
| 281 |
"optional_float: inf"); |
| 282 |
RunValidTextFormatTest("FloatFieldNegInfValue", REQUIRED, |
| 283 |
"optional_float: -inf"); |
| 284 |
RunValidTextFormatTest("FloatFieldWithInt32Max", REQUIRED, |
| 285 |
"optional_float: 4294967296"); |
| 286 |
RunValidTextFormatTest("FloatFieldLargerThanInt64", REQUIRED, |
| 287 |
"optional_float: 9223372036854775808"); |
| 288 |
RunValidTextFormatTest("FloatFieldTooLarge", REQUIRED, |
| 289 |
"optional_float: 3.4028235e+39"); |
| 290 |
RunValidTextFormatTest("FloatFieldTooSmall", REQUIRED, |
| 291 |
"optional_float: 1.17549e-39"); |
| 292 |
RunValidTextFormatTest("FloatFieldLargerThanUint64", REQUIRED, |
| 293 |
"optional_float: 18446744073709551616"); |
| 294 |
|
| 295 |
// String literals x {Strings, Bytes} |
| 296 |
for (const auto& field_type : std::vector<std::string>{"String", "Bytes"}) { |
| 297 |
const std::string field_name = |
| 298 |
field_type == "String" ? "optional_string" : "optional_bytes"; |
| 299 |
RunValidTextFormatTest( |
| 300 |
absl::StrCat("StringLiteralConcat", field_type), REQUIRED, |
| 301 |
absl::StrCat(field_name, ": 'first' \"second\"\n'third'")); |
| 302 |
RunValidTextFormatTest( |
| 303 |
absl::StrCat("StringLiteralBasicEscapes", field_type), REQUIRED, |
| 304 |
absl::StrCat(field_name, ": '\\a\\b\\f\\n\\r\\t\\v\\?\\\\\\'\\\"'")); |
| 305 |
RunValidTextFormatTest( |
| 306 |
absl::StrCat("StringLiteralOctalEscapes", field_type), REQUIRED, |
| 307 |
absl::StrCat(field_name, ": '\\341\\210\\264'")); |
| 308 |
RunValidTextFormatTest( |
| 309 |
absl::StrCat("StringLiteralHexEscapes", field_type), REQUIRED, |
| 310 |
absl::StrCat(field_name, ": '\\xe1\\x88\\xb4'")); |
| 311 |
RunValidTextFormatTest( |
| 312 |
absl::StrCat("StringLiteralShortUnicodeEscape", field_type), |
| 313 |
RECOMMENDED, absl::StrCat(field_name, ": '\\u1234'")); |
| 314 |
RunValidTextFormatTest( |
| 315 |
absl::StrCat("StringLiteralLongUnicodeEscapes", field_type), |
| 316 |
RECOMMENDED, absl::StrCat(field_name, ": '\\U00001234\\U00010437'")); |
| 317 |
// String literals don't include line feeds. |
| 318 |
ExpectParseFailure( |
| 319 |
absl::StrCat("StringLiteralIncludesLF", field_type), REQUIRED, |
| 320 |
absl::StrCat(field_name, ": 'first line\nsecond line'")); |
| 321 |
// Unicode escapes don't include code points that lie beyond the planes |
| 322 |
// (> 0x10ffff). |
| 323 |
ExpectParseFailure( |
| 324 |
absl::StrCat("StringLiteralLongUnicodeEscapeTooLarge", field_type), |
| 325 |
REQUIRED, absl::StrCat(field_name, ": '\\U00110000'")); |
| 326 |
// Unicode escapes don't include surrogates. |
| 327 |
ExpectParseFailure( |
| 328 |
absl::StrCat("StringLiteralShortUnicodeEscapeSurrogatePair", |
| 329 |
field_type), |
| 330 |
RECOMMENDED, absl::StrCat(field_name, ": '\\ud801\\udc37'")); |
| 331 |
ExpectParseFailure( |
| 332 |
absl::StrCat("StringLiteralShortUnicodeEscapeSurrogateFirstOnly", |
| 333 |
field_type), |
| 334 |
RECOMMENDED, absl::StrCat(field_name, ": '\\ud800'")); |
| 335 |
ExpectParseFailure( |
| 336 |
absl::StrCat("StringLiteralShortUnicodeEscapeSurrogateSecondOnly", |
| 337 |
field_type), |
| 338 |
RECOMMENDED, absl::StrCat(field_name, ": '\\udc00'")); |
| 339 |
ExpectParseFailure( |
| 340 |
absl::StrCat("StringLiteralLongUnicodeEscapeSurrogateFirstOnly", |
| 341 |
field_type), |
| 342 |
RECOMMENDED, absl::StrCat(field_name, ": '\\U0000d800'")); |
| 343 |
ExpectParseFailure( |
| 344 |
absl::StrCat("StringLiteralLongUnicodeEscapeSurrogateSecondOnly", |
| 345 |
field_type), |
| 346 |
RECOMMENDED, absl::StrCat(field_name, ": '\\U0000dc00'")); |
| 347 |
ExpectParseFailure( |
| 348 |
absl::StrCat("StringLiteralLongUnicodeEscapeSurrogatePair", |
| 349 |
field_type), |
| 350 |
RECOMMENDED, absl::StrCat(field_name, ": '\\U0000d801\\U00000dc37'")); |
| 351 |
ExpectParseFailure( |
| 352 |
absl::StrCat("StringLiteralUnicodeEscapeSurrogatePairLongShort", |
| 353 |
field_type), |
| 354 |
RECOMMENDED, absl::StrCat(field_name, ": '\\U0000d801\\udc37'")); |
| 355 |
ExpectParseFailure( |
| 356 |
absl::StrCat("StringLiteralUnicodeEscapeSurrogatePairShortLong", |
| 357 |
field_type), |
| 358 |
RECOMMENDED, absl::StrCat(field_name, ": '\\ud801\\U0000dc37'")); |
| 359 |
|
| 360 |
// The following method depend on the type of field, as strings have extra |
| 361 |
// validation. |
| 362 |
const auto test_method = |
| 363 |
field_type == "String" |
| 364 |
? &TextFormatConformanceTestSuite::ExpectParseFailure |
| 365 |
: &TextFormatConformanceTestSuite::RunValidTextFormatTest; |
| 366 |
|
| 367 |
// String fields reject invalid UTF-8 byte sequences; bytes fields don't. |
| 368 |
(this->*test_method)(absl::StrCat(field_type, "FieldBadUTF8Octal"), |
| 369 |
REQUIRED, absl::StrCat(field_name, ": '\\300'")); |
| 370 |
(this->*test_method)(absl::StrCat(field_type, "FieldBadUTF8Hex"), |
| 371 |
REQUIRED, absl::StrCat(field_name, ": '\\xc0'")); |
| 372 |
} |
| 373 |
|
| 374 |
// Group fields |
| 375 |
RunValidTextFormatTestProto2("GroupFieldNoColon", REQUIRED, |
| 376 |
"Data { group_int32: 1 }"); |
| 377 |
RunValidTextFormatTestProto2("GroupFieldWithColon", REQUIRED, |
| 378 |
"Data: { group_int32: 1 }"); |
| 379 |
RunValidTextFormatTestProto2("GroupFieldEmpty", REQUIRED, "Data {}"); |
| 380 |
|
| 381 |
// Unknown Fields |
| 382 |
UnknownToTestAllTypes message; |
| 383 |
// Unable to print unknown Fixed32/Fixed64 fields as if they are known. |
| 384 |
// Fixed32/Fixed64 fields are not added in the tests. |
| 385 |
message.set_optional_int32(123); |
| 386 |
message.set_optional_string("hello"); |
| 387 |
message.set_optional_bool(true); |
| 388 |
RunValidUnknownTextFormatTest("ScalarUnknownFields", message); |
| 389 |
|
| 390 |
message.Clear(); |
| 391 |
message.mutable_nested_message()->set_c(111); |
| 392 |
RunValidUnknownTextFormatTest("MessageUnknownFields", message); |
| 393 |
|
| 394 |
message.Clear(); |
| 395 |
message.mutable_optionalgroup()->set_a(321); |
| 396 |
RunValidUnknownTextFormatTest("GroupUnknownFields", message); |
| 397 |
|
| 398 |
message.add_repeated_int32(1); |
| 399 |
message.add_repeated_int32(2); |
| 400 |
message.add_repeated_int32(3); |
| 401 |
RunValidUnknownTextFormatTest("RepeatedUnknownFields", message); |
| 402 |
|
| 403 |
// Any fields |
| 404 |
RunValidTextFormatTest("AnyField", REQUIRED, |
| 405 |
R"( |
| 406 |
optional_any: { |
| 407 |
[type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3] { |
| 408 |
optional_int32: 12345 |
| 409 |
} |
| 410 |
} |
| 411 |
)"); |
| 412 |
RunValidTextFormatTest("AnyFieldWithRawBytes", REQUIRED, |
| 413 |
R"( |
| 414 |
optional_any: { |
| 415 |
type_url: "type.googleapis.com/protobuf_test_messages.proto3.TestAllTypesProto3" |
| 416 |
value: "\b\271`" |
| 417 |
} |
| 418 |
)"); |
| 419 |
ExpectParseFailure("AnyFieldWithInvalidType", REQUIRED, |
| 420 |
R"( |
| 421 |
optional_any: { |
| 422 |
[type.googleapis.com/unknown] { |
| 423 |
optional_int32: 12345 |
| 424 |
} |
| 425 |
} |
| 426 |
)"); |
| 427 |
|
| 428 |
// Map fields |
| 429 |
TestAllTypesProto3 prototype; |
| 430 |
(*prototype.mutable_map_string_string())["c"] = "value"; |
| 431 |
(*prototype.mutable_map_string_string())["b"] = "value"; |
| 432 |
(*prototype.mutable_map_string_string())["a"] = "value"; |
| 433 |
RunValidTextFormatTestWithMessage("AlphabeticallySortedMapStringKeys", |
| 434 |
REQUIRED, |
| 435 |
R"( |
| 436 |
map_string_string { |
| 437 |
key: "a" |
| 438 |
value: "value" |
| 439 |
} |
| 440 |
map_string_string { |
| 441 |
key: "b" |
| 442 |
value: "value" |
| 443 |
} |
| 444 |
map_string_string { |
| 445 |
key: "c" |
| 446 |
value: "value" |
| 447 |
} |
| 448 |
)", |
| 449 |
prototype); |
| 450 |
|
| 451 |
prototype.Clear(); |
| 452 |
(*prototype.mutable_map_int32_int32())[3] = 0; |
| 453 |
(*prototype.mutable_map_int32_int32())[2] = 0; |
| 454 |
(*prototype.mutable_map_int32_int32())[1] = 0; |
| 455 |
RunValidTextFormatTestWithMessage("AlphabeticallySortedMapIntKeys", |
| 456 |
REQUIRED, |
| 457 |
R"( |
| 458 |
map_int32_int32 { |
| 459 |
key: 1 |
| 460 |
value: 0 |
| 461 |
} |
| 462 |
map_int32_int32 { |
| 463 |
key: 2 |
| 464 |
value: 0 |
| 465 |
} |
| 466 |
map_int32_int32 { |
| 467 |
key: 3 |
| 468 |
value: 0 |
| 469 |
} |
| 470 |
)", |
| 471 |
prototype); |
| 472 |
|
| 473 |
prototype.Clear(); |
| 474 |
(*prototype.mutable_map_bool_bool())[true] = false; |
| 475 |
(*prototype.mutable_map_bool_bool())[false] = false; |
| 476 |
RunValidTextFormatTestWithMessage("AlphabeticallySortedMapBoolKeys", |
| 477 |
REQUIRED, |
| 478 |
R"( |
| 479 |
map_bool_bool { |
| 480 |
key: false |
| 481 |
value: false |
| 482 |
} |
| 483 |
map_bool_bool { |
| 484 |
key: true |
| 485 |
value: false |
| 486 |
} |
| 487 |
)", |
| 488 |
prototype); |
| 489 |
|
| 490 |
prototype.Clear(); |
| 491 |
ConformanceRequestSetting setting_map( |
| 492 |
REQUIRED, conformance::TEXT_FORMAT, conformance::PROTOBUF, |
| 493 |
conformance::TEXT_FORMAT_TEST, prototype, "DuplicateMapKey", R"( |
| 494 |
map_string_nested_message { |
| 495 |
key: "duplicate" |
| 496 |
value: { a: 123 } |
| 497 |
} |
| 498 |
map_string_nested_message { |
| 499 |
key: "duplicate" |
| 500 |
value: { corecursive: {} } |
| 501 |
} |
| 502 |
)"); |
| 503 |
// The last-specified value will be retained in a parsed map |
| 504 |
RunValidInputTest(setting_map, R"( |
| 505 |
map_string_nested_message { |
| 506 |
key: "duplicate" |
| 507 |
value: { corecursive: {} } |
| 508 |
} |
| 509 |
)"); |
| 510 |
} |
| 511 |
// Flag control performance tests to keep them internal and opt-in only |
| 512 |
if (performance_) { |
| 513 |
RunTextFormatPerformanceTests(); |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
void TextFormatConformanceTestSuite::RunTextFormatPerformanceTests() { |
| 518 |
TestTextFormatPerformanceMergeMessageWithRepeatedField("Bool", |
| 519 |
"repeated_bool: true"); |
| 520 |
TestTextFormatPerformanceMergeMessageWithRepeatedField( |
| 521 |
"Double", "repeated_double: 123"); |
| 522 |
TestTextFormatPerformanceMergeMessageWithRepeatedField( |
| 523 |
"Int32", "repeated_uint32: 123"); |
| 524 |
TestTextFormatPerformanceMergeMessageWithRepeatedField( |
| 525 |
"Int64", "repeated_uint64: 123"); |
| 526 |
TestTextFormatPerformanceMergeMessageWithRepeatedField( |
| 527 |
"String", R"(repeated_string: "foo")"); |
| 528 |
TestTextFormatPerformanceMergeMessageWithRepeatedField( |
| 529 |
"Bytes", R"(repeated_bytes: "foo")"); |
| 530 |
} |
| 531 |
|
| 532 |
// This is currently considered valid input by some languages but not others |
| 533 |
void TextFormatConformanceTestSuite:: |
| 534 |
TestTextFormatPerformanceMergeMessageWithRepeatedField( |
| 535 |
const string& test_type_name, const string& message_field) { |
| 536 |
string recursive_message = |
| 537 |
absl::StrCat("recursive_message { ", message_field, " }"); |
| 538 |
|
| 539 |
string input; |
| 540 |
for (size_t i = 0; i < kPerformanceRepeatCount; i++) { |
| 541 |
absl::StrAppend(&input, recursive_message); |
| 542 |
} |
| 543 |
|
| 544 |
string expected = "recursive_message { "; |
| 545 |
for (size_t i = 0; i < kPerformanceRepeatCount; i++) { |
| 546 |
absl::StrAppend(&expected, message_field, " "); |
| 547 |
} |
| 548 |
absl::StrAppend(&expected, "}"); |
| 549 |
|
| 550 |
RunValidTextFormatTestProto2WithExpected( |
| 551 |
absl::StrCat("TestTextFormatPerformanceMergeMessageWithRepeatedField", |
| 552 |
test_type_name, "Proto2"), |
| 553 |
RECOMMENDED, input, expected); |
| 554 |
RunValidTextFormatTestWithExpected( |
| 555 |
absl::StrCat("TestTextFormatPerformanceMergeMessageWithRepeatedField", |
| 556 |
test_type_name, "Proto3"), |
| 557 |
RECOMMENDED, input, expected); |
| 558 |
} |
| 559 |
|
| 560 |
} // namespace protobuf |
| 561 |
} // namespace google |