Index: third_party/protobuf/src/google/protobuf/wire_format_unittest.cc |
diff --git a/third_party/protobuf/src/google/protobuf/wire_format_unittest.cc b/third_party/protobuf/src/google/protobuf/wire_format_unittest.cc |
index 9422f9abc73207621698638a12da860e7156df8c..15c37556eb166daf5bf8f41f35a7cb08e34759da 100644 |
--- a/third_party/protobuf/src/google/protobuf/wire_format_unittest.cc |
+++ b/third_party/protobuf/src/google/protobuf/wire_format_unittest.cc |
@@ -1,6 +1,6 @@ |
// Protocol Buffers - Google's data interchange format |
// Copyright 2008 Google Inc. All rights reserved. |
-// http://code.google.com/p/protobuf/ |
+// https://developers.google.com/protocol-buffers/ |
// |
// Redistribution and use in source and binary forms, with or without |
// modification, are permitted provided that the following conditions are |
@@ -38,9 +38,12 @@ |
#include <google/protobuf/io/zero_copy_stream_impl.h> |
#include <google/protobuf/io/coded_stream.h> |
#include <google/protobuf/unittest.pb.h> |
+#include <google/protobuf/unittest_proto3_arena.pb.h> |
#include <google/protobuf/unittest_mset.pb.h> |
+#include <google/protobuf/unittest_mset_wire_format.pb.h> |
#include <google/protobuf/test_util.h> |
+#include <google/protobuf/stubs/logging.h> |
#include <google/protobuf/stubs/common.h> |
#include <google/protobuf/testing/googletest.h> |
#include <gtest/gtest.h> |
@@ -175,6 +178,43 @@ TEST(WireFormatTest, ParsePackedExtensions) { |
TestUtil::ExpectPackedExtensionsSet(dest); |
} |
+TEST(WireFormatTest, ParseOneof) { |
+ unittest::TestOneof2 source, dest; |
+ string data; |
+ |
+ // Serialize using the generated code. |
+ TestUtil::SetOneof1(&source); |
+ source.SerializeToString(&data); |
+ |
+ // Parse using WireFormat. |
+ io::ArrayInputStream raw_input(data.data(), data.size()); |
+ io::CodedInputStream input(&raw_input); |
+ WireFormat::ParseAndMergePartial(&input, &dest); |
+ |
+ // Check. |
+ TestUtil::ExpectOneofSet1(dest); |
+} |
+ |
+TEST(WireFormatTest, OneofOnlySetLast) { |
+ unittest::TestOneofBackwardsCompatible source; |
+ unittest::TestOneof oneof_dest; |
+ string data; |
+ |
+ // Set two fields |
+ source.set_foo_int(100); |
+ source.set_foo_string("101"); |
+ |
+ // Serialize and parse to oneof message. |
+ source.SerializeToString(&data); |
+ io::ArrayInputStream raw_input(data.data(), data.size()); |
+ io::CodedInputStream input(&raw_input); |
+ WireFormat::ParseAndMergePartial(&input, &oneof_dest); |
+ |
+ // Only the last field is set. |
+ EXPECT_FALSE(oneof_dest.has_foo_int()); |
+ EXPECT_TRUE(oneof_dest.has_foo_string()); |
+} |
+ |
TEST(WireFormatTest, ByteSize) { |
unittest::TestAllTypes message; |
TestUtil::SetAllFields(&message); |
@@ -217,6 +257,18 @@ TEST(WireFormatTest, ByteSizePackedExtensions) { |
EXPECT_EQ(0, WireFormat::ByteSize(message)); |
} |
+TEST(WireFormatTest, ByteSizeOneof) { |
+ unittest::TestOneof2 message; |
+ TestUtil::SetOneof1(&message); |
+ |
+ EXPECT_EQ(message.ByteSize(), |
+ WireFormat::ByteSize(message)); |
+ message.Clear(); |
+ |
+ EXPECT_EQ(0, message.ByteSize()); |
+ EXPECT_EQ(0, WireFormat::ByteSize(message)); |
+} |
+ |
TEST(WireFormatTest, Serialize) { |
unittest::TestAllTypes message; |
string generated_data; |
@@ -311,6 +363,36 @@ TEST(WireFormatTest, SerializeFieldsAndExtensions) { |
TestUtil::ExpectAllFieldsAndExtensionsInOrder(generated_data); |
} |
+TEST(WireFormatTest, SerializeOneof) { |
+ unittest::TestOneof2 message; |
+ string generated_data; |
+ string dynamic_data; |
+ |
+ TestUtil::SetOneof1(&message); |
+ int size = message.ByteSize(); |
+ |
+ // Serialize using the generated code. |
+ { |
+ io::StringOutputStream raw_output(&generated_data); |
+ io::CodedOutputStream output(&raw_output); |
+ message.SerializeWithCachedSizes(&output); |
+ ASSERT_FALSE(output.HadError()); |
+ } |
+ |
+ // Serialize using WireFormat. |
+ { |
+ io::StringOutputStream raw_output(&dynamic_data); |
+ io::CodedOutputStream output(&raw_output); |
+ WireFormat::SerializeWithCachedSizes(message, size, &output); |
+ ASSERT_FALSE(output.HadError()); |
+ } |
+ |
+ // Should be the same. |
+ // Don't use EXPECT_EQ here because we're comparing raw binary data and |
+ // we really don't want it dumped to stdout on failure. |
+ EXPECT_TRUE(dynamic_data == generated_data); |
+} |
+ |
TEST(WireFormatTest, ParseMultipleExtensionRanges) { |
// Make sure we can parse a message that contains multiple extensions ranges. |
unittest::TestFieldOrderings source; |
@@ -339,7 +421,7 @@ const int kUnknownTypeId = 1550055; |
TEST(WireFormatTest, SerializeMessageSet) { |
// Set up a TestMessageSet with two known messages and an unknown one. |
- unittest::TestMessageSet message_set; |
+ proto2_wireformat_unittest::TestMessageSet message_set; |
message_set.MutableExtension( |
unittest::TestMessageSetExtension1::message_set_extension)->set_i(123); |
message_set.MutableExtension( |
@@ -382,7 +464,7 @@ TEST(WireFormatTest, SerializeMessageSetVariousWaysAreEqual) { |
// Set up a TestMessageSet with two known messages and an unknown one, as |
// above. |
- unittest::TestMessageSet message_set; |
+ proto2_wireformat_unittest::TestMessageSet message_set; |
message_set.MutableExtension( |
unittest::TestMessageSetExtension1::message_set_extension)->set_i(123); |
message_set.MutableExtension( |
@@ -459,7 +541,7 @@ TEST(WireFormatTest, ParseMessageSet) { |
ASSERT_TRUE(raw.SerializeToString(&data)); |
// Parse as a TestMessageSet and check the contents. |
- unittest::TestMessageSet message_set; |
+ proto2_wireformat_unittest::TestMessageSet message_set; |
ASSERT_TRUE(message_set.ParseFromString(data)); |
EXPECT_EQ(123, message_set.GetExtension( |
@@ -473,7 +555,7 @@ TEST(WireFormatTest, ParseMessageSet) { |
EXPECT_EQ("bar", message_set.unknown_fields().field(0).length_delimited()); |
// Also parse using WireFormat. |
- unittest::TestMessageSet dynamic_message_set; |
+ proto2_wireformat_unittest::TestMessageSet dynamic_message_set; |
io::CodedInputStream input(reinterpret_cast<const uint8*>(data.data()), |
data.size()); |
ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &dynamic_message_set)); |
@@ -503,7 +585,7 @@ TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) { |
coded_output.WriteTag(WireFormatLite::kMessageSetItemEndTag); |
} |
{ |
- unittest::TestMessageSet message_set; |
+ proto2_wireformat_unittest::TestMessageSet message_set; |
ASSERT_TRUE(message_set.ParseFromString(data)); |
EXPECT_EQ(123, message_set.GetExtension( |
@@ -511,7 +593,7 @@ TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) { |
} |
{ |
// Test parse the message via Reflection. |
- unittest::TestMessageSet message_set; |
+ proto2_wireformat_unittest::TestMessageSet message_set; |
io::CodedInputStream input( |
reinterpret_cast<const uint8*>(data.data()), data.size()); |
EXPECT_TRUE(WireFormat::ParseAndMergePartial(&input, &message_set)); |
@@ -523,7 +605,7 @@ TEST(WireFormatTest, ParseMessageSetWithReverseTagOrder) { |
} |
TEST(WireFormatTest, ParseBrokenMessageSet) { |
- unittest::TestMessageSet message_set; |
+ proto2_wireformat_unittest::TestMessageSet message_set; |
string input("goodbye"); // Invalid wire format data. |
EXPECT_FALSE(message_set.ParseFromString(input)); |
} |
@@ -687,6 +769,197 @@ TEST(WireFormatTest, RepeatedScalarsDifferentTagSizes) { |
EXPECT_EQ(msg1.DebugString(), msg2.DebugString()); |
} |
+TEST(WireFormatTest, CompatibleTypes) { |
+ const int64 data = 0x100000000LL; |
+ unittest::Int64Message msg1; |
+ msg1.set_data(data); |
+ string serialized; |
+ msg1.SerializeToString(&serialized); |
+ |
+ // Test int64 is compatible with bool |
+ unittest::BoolMessage msg2; |
+ ASSERT_TRUE(msg2.ParseFromString(serialized)); |
+ ASSERT_EQ(static_cast<bool>(data), msg2.data()); |
+ |
+ // Test int64 is compatible with uint64 |
+ unittest::Uint64Message msg3; |
+ ASSERT_TRUE(msg3.ParseFromString(serialized)); |
+ ASSERT_EQ(static_cast<uint64>(data), msg3.data()); |
+ |
+ // Test int64 is compatible with int32 |
+ unittest::Int32Message msg4; |
+ ASSERT_TRUE(msg4.ParseFromString(serialized)); |
+ ASSERT_EQ(static_cast<int32>(data), msg4.data()); |
+ |
+ // Test int64 is compatible with uint32 |
+ unittest::Uint32Message msg5; |
+ ASSERT_TRUE(msg5.ParseFromString(serialized)); |
+ ASSERT_EQ(static_cast<uint32>(data), msg5.data()); |
+} |
+ |
+class Proto3PrimitiveRepeatedWireFormatTest : public ::testing::Test { |
+ protected: |
+ Proto3PrimitiveRepeatedWireFormatTest() |
+ : packedTestAllTypes_( |
+ "\xFA\x01\x01\x01" |
+ "\x82\x02\x01\x01" |
+ "\x8A\x02\x01\x01" |
+ "\x92\x02\x01\x01" |
+ "\x9A\x02\x01\x02" |
+ "\xA2\x02\x01\x02" |
+ "\xAA\x02\x04\x01\x00\x00\x00" |
+ "\xB2\x02\x08\x01\x00\x00\x00\x00\x00\x00\x00" |
+ "\xBA\x02\x04\x01\x00\x00\x00" |
+ "\xC2\x02\x08\x01\x00\x00\x00\x00\x00\x00\x00" |
+ "\xCA\x02\x04\x00\x00\x80\x3f" |
+ "\xD2\x02\x08\x00\x00\x00\x00\x00\x00\xf0\x3f" |
+ "\xDA\x02\x01\x01" |
+ "\x9A\x03\x01\x01", |
+ 86), |
+ packedTestUnpackedTypes_( |
+ "\x0A\x01\x01" |
+ "\x12\x01\x01" |
+ "\x1A\x01\x01" |
+ "\x22\x01\x01" |
+ "\x2A\x01\x02" |
+ "\x32\x01\x02" |
+ "\x3A\x04\x01\x00\x00\x00" |
+ "\x42\x08\x01\x00\x00\x00\x00\x00\x00\x00" |
+ "\x4A\x04\x01\x00\x00\x00" |
+ "\x52\x08\x01\x00\x00\x00\x00\x00\x00\x00" |
+ "\x5A\x04\x00\x00\x80\x3f" |
+ "\x62\x08\x00\x00\x00\x00\x00\x00\xf0\x3f" |
+ "\x6A\x01\x01" |
+ "\x72\x01\x01", |
+ 72), |
+ unpackedTestAllTypes_( |
+ "\xF8\x01\x01" |
+ "\x80\x02\x01" |
+ "\x88\x02\x01" |
+ "\x90\x02\x01" |
+ "\x98\x02\x02" |
+ "\xA0\x02\x02" |
+ "\xAD\x02\x01\x00\x00\x00" |
+ "\xB1\x02\x01\x00\x00\x00\x00\x00\x00\x00" |
+ "\xBD\x02\x01\x00\x00\x00" |
+ "\xC1\x02\x01\x00\x00\x00\x00\x00\x00\x00" |
+ "\xCD\x02\x00\x00\x80\x3f" |
+ "\xD1\x02\x00\x00\x00\x00\x00\x00\xf0\x3f" |
+ "\xD8\x02\x01" |
+ "\x98\x03\x01", |
+ 72), |
+ unpackedTestUnpackedTypes_( |
+ "\x08\x01" |
+ "\x10\x01" |
+ "\x18\x01" |
+ "\x20\x01" |
+ "\x28\x02" |
+ "\x30\x02" |
+ "\x3D\x01\x00\x00\x00" |
+ "\x41\x01\x00\x00\x00\x00\x00\x00\x00" |
+ "\x4D\x01\x00\x00\x00" |
+ "\x51\x01\x00\x00\x00\x00\x00\x00\x00" |
+ "\x5D\x00\x00\x80\x3f" |
+ "\x61\x00\x00\x00\x00\x00\x00\xf0\x3f" |
+ "\x68\x01" |
+ "\x70\x01", |
+ 58) {} |
+ template <class Proto> |
+ void SetProto3PrimitiveRepeatedFields(Proto* message) { |
+ message->add_repeated_int32(1); |
+ message->add_repeated_int64(1); |
+ message->add_repeated_uint32(1); |
+ message->add_repeated_uint64(1); |
+ message->add_repeated_sint32(1); |
+ message->add_repeated_sint64(1); |
+ message->add_repeated_fixed32(1); |
+ message->add_repeated_fixed64(1); |
+ message->add_repeated_sfixed32(1); |
+ message->add_repeated_sfixed64(1); |
+ message->add_repeated_float(1.0); |
+ message->add_repeated_double(1.0); |
+ message->add_repeated_bool(true); |
+ message->add_repeated_nested_enum( |
+ proto3_arena_unittest::TestAllTypes_NestedEnum_FOO); |
+ } |
+ |
+ template <class Proto> |
+ void ExpectProto3PrimitiveRepeatedFieldsSet(const Proto& message) { |
+ EXPECT_EQ(1, message.repeated_int32(0)); |
+ EXPECT_EQ(1, message.repeated_int64(0)); |
+ EXPECT_EQ(1, message.repeated_uint32(0)); |
+ EXPECT_EQ(1, message.repeated_uint64(0)); |
+ EXPECT_EQ(1, message.repeated_sint32(0)); |
+ EXPECT_EQ(1, message.repeated_sint64(0)); |
+ EXPECT_EQ(1, message.repeated_fixed32(0)); |
+ EXPECT_EQ(1, message.repeated_fixed64(0)); |
+ EXPECT_EQ(1, message.repeated_sfixed32(0)); |
+ EXPECT_EQ(1, message.repeated_sfixed64(0)); |
+ EXPECT_EQ(1.0, message.repeated_float(0)); |
+ EXPECT_EQ(1.0, message.repeated_double(0)); |
+ EXPECT_EQ(true, message.repeated_bool(0)); |
+ EXPECT_EQ(proto3_arena_unittest::TestAllTypes_NestedEnum_FOO, |
+ message.repeated_nested_enum(0)); |
+ } |
+ |
+ template <class Proto> |
+ void TestSerialization(Proto* message, const string& expected) { |
+ SetProto3PrimitiveRepeatedFields(message); |
+ |
+ int size = message->ByteSize(); |
+ |
+ // Serialize using the generated code. |
+ string generated_data; |
+ { |
+ io::StringOutputStream raw_output(&generated_data); |
+ io::CodedOutputStream output(&raw_output); |
+ message->SerializeWithCachedSizes(&output); |
+ ASSERT_FALSE(output.HadError()); |
+ } |
+ EXPECT_TRUE(expected == generated_data); |
+ |
+ // Serialize using the dynamic code. |
+ string dynamic_data; |
+ { |
+ io::StringOutputStream raw_output(&dynamic_data); |
+ io::CodedOutputStream output(&raw_output); |
+ WireFormat::SerializeWithCachedSizes(*message, size, &output); |
+ ASSERT_FALSE(output.HadError()); |
+ } |
+ EXPECT_TRUE(expected == dynamic_data); |
+ } |
+ |
+ template <class Proto> |
+ void TestParsing(Proto* message, const string& compatible_data) { |
+ message->Clear(); |
+ message->ParseFromString(compatible_data); |
+ ExpectProto3PrimitiveRepeatedFieldsSet(*message); |
+ |
+ message->Clear(); |
+ io::CodedInputStream input( |
+ reinterpret_cast<const uint8*>(compatible_data.data()), |
+ compatible_data.size()); |
+ WireFormat::ParseAndMergePartial(&input, message); |
+ ExpectProto3PrimitiveRepeatedFieldsSet(*message); |
+ } |
+ |
+ const string packedTestAllTypes_; |
+ const string packedTestUnpackedTypes_; |
+ const string unpackedTestAllTypes_; |
+ const string unpackedTestUnpackedTypes_; |
+}; |
+ |
+TEST_F(Proto3PrimitiveRepeatedWireFormatTest, Proto3PrimitiveRepeated) { |
+ proto3_arena_unittest::TestAllTypes packed_message; |
+ proto3_arena_unittest::TestUnpackedTypes unpacked_message; |
+ TestSerialization(&packed_message, packedTestAllTypes_); |
+ TestParsing(&packed_message, packedTestAllTypes_); |
+ TestParsing(&packed_message, unpackedTestAllTypes_); |
+ TestSerialization(&unpacked_message, unpackedTestUnpackedTypes_); |
+ TestParsing(&unpacked_message, packedTestUnpackedTypes_); |
+ TestParsing(&unpacked_message, unpackedTestUnpackedTypes_); |
+} |
+ |
class WireFormatInvalidInputTest : public testing::Test { |
protected: |
// Make a serialized TestAllTypes in which the field optional_nested_message |
@@ -848,11 +1121,20 @@ bool ReadMessage(const string &wire_buffer, T *message) { |
return message->ParseFromArray(wire_buffer.data(), wire_buffer.size()); |
} |
-bool base::StartsWith(const string& s, const string& prefix) { |
+bool StartsWith(const string& s, const string& prefix) { |
return s.substr(0, prefix.length()) == prefix; |
} |
-TEST(Utf8ValidationTest, WriteInvalidUTF8String) { |
+class Utf8ValidationTest : public ::testing::Test { |
+ protected: |
+ Utf8ValidationTest() {} |
+ virtual ~Utf8ValidationTest() {} |
+ virtual void SetUp() { |
+ } |
+ |
+}; |
+ |
+TEST_F(Utf8ValidationTest, WriteInvalidUTF8String) { |
string wire_buffer; |
protobuf_unittest::OneString input; |
vector<string> errors; |
@@ -863,17 +1145,18 @@ TEST(Utf8ValidationTest, WriteInvalidUTF8String) { |
} |
#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
ASSERT_EQ(1, errors.size()); |
- EXPECT_TRUE( |
- base::StartsWith(errors[0], |
- "String field contains invalid UTF-8 data when " |
- "serializing a protocol buffer. Use the " |
- "'bytes' type if you intend to send raw bytes.")); |
+ EXPECT_TRUE(StartsWith(errors[0], |
+ "String field 'protobuf_unittest.OneString.data' " |
+ "contains invalid UTF-8 data when " |
+ "serializing a protocol buffer. Use the " |
+ "'bytes' type if you intend to send raw bytes.")); |
#else |
ASSERT_EQ(0, errors.size()); |
#endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
} |
-TEST(Utf8ValidationTest, ReadInvalidUTF8String) { |
+ |
+TEST_F(Utf8ValidationTest, ReadInvalidUTF8String) { |
string wire_buffer; |
protobuf_unittest::OneString input; |
WriteMessage(kInvalidUTF8String, &input, &wire_buffer); |
@@ -886,18 +1169,19 @@ TEST(Utf8ValidationTest, ReadInvalidUTF8String) { |
} |
#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
ASSERT_EQ(1, errors.size()); |
- EXPECT_TRUE( |
- base::StartsWith(errors[0], |
- "String field contains invalid UTF-8 data when " |
- "parsing a protocol buffer. Use the " |
- "'bytes' type if you intend to send raw bytes.")); |
+ EXPECT_TRUE(StartsWith(errors[0], |
+ "String field 'protobuf_unittest.OneString.data' " |
+ "contains invalid UTF-8 data when " |
+ "parsing a protocol buffer. Use the " |
+ "'bytes' type if you intend to send raw bytes.")); |
#else |
ASSERT_EQ(0, errors.size()); |
#endif // GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
} |
-TEST(Utf8ValidationTest, WriteValidUTF8String) { |
+ |
+TEST_F(Utf8ValidationTest, WriteValidUTF8String) { |
string wire_buffer; |
protobuf_unittest::OneString input; |
vector<string> errors; |
@@ -909,7 +1193,7 @@ TEST(Utf8ValidationTest, WriteValidUTF8String) { |
ASSERT_EQ(0, errors.size()); |
} |
-TEST(Utf8ValidationTest, ReadValidUTF8String) { |
+TEST_F(Utf8ValidationTest, ReadValidUTF8String) { |
string wire_buffer; |
protobuf_unittest::OneString input; |
WriteMessage(kValidUTF8String, &input, &wire_buffer); |
@@ -925,7 +1209,7 @@ TEST(Utf8ValidationTest, ReadValidUTF8String) { |
} |
// Bytes: anything can pass as bytes, use invalid UTF-8 string to test |
-TEST(Utf8ValidationTest, WriteArbitraryBytes) { |
+TEST_F(Utf8ValidationTest, WriteArbitraryBytes) { |
string wire_buffer; |
protobuf_unittest::OneBytes input; |
vector<string> errors; |
@@ -937,7 +1221,7 @@ TEST(Utf8ValidationTest, WriteArbitraryBytes) { |
ASSERT_EQ(0, errors.size()); |
} |
-TEST(Utf8ValidationTest, ReadArbitraryBytes) { |
+TEST_F(Utf8ValidationTest, ReadArbitraryBytes) { |
string wire_buffer; |
protobuf_unittest::OneBytes input; |
WriteMessage(kInvalidUTF8String, &input, &wire_buffer); |
@@ -952,7 +1236,7 @@ TEST(Utf8ValidationTest, ReadArbitraryBytes) { |
EXPECT_EQ(input.data(), output.data()); |
} |
-TEST(Utf8ValidationTest, ParseRepeatedString) { |
+TEST_F(Utf8ValidationTest, ParseRepeatedString) { |
protobuf_unittest::MoreBytes input; |
input.add_data(kValidUTF8String); |
input.add_data(kInvalidUTF8String); |
@@ -974,6 +1258,30 @@ TEST(Utf8ValidationTest, ParseRepeatedString) { |
EXPECT_EQ(wire_buffer, output.SerializeAsString()); |
} |
+// Test the old VerifyUTF8String() function, which may still be called by old |
+// generated code. |
+TEST_F(Utf8ValidationTest, OldVerifyUTF8String) { |
+ string data(kInvalidUTF8String); |
+ |
+ vector<string> errors; |
+ { |
+ ScopedMemoryLog log; |
+ WireFormat::VerifyUTF8String(data.data(), data.size(), |
+ WireFormat::SERIALIZE); |
+ errors = log.GetMessages(ERROR); |
+ } |
+#ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED |
+ ASSERT_EQ(1, errors.size()); |
+ EXPECT_TRUE(StartsWith(errors[0], |
+ "String field contains invalid UTF-8 data when " |
+ "serializing a protocol buffer. Use the " |
+ "'bytes' type if you intend to send raw bytes.")); |
+#else |
+ ASSERT_EQ(0, errors.size()); |
+#endif |
+} |
+ |
+ |
} // namespace |
} // namespace internal |
} // namespace protobuf |