aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/google/protobuf/wire_format_unittest.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/google/protobuf/wire_format_unittest.cc')
-rw-r--r--src/google/protobuf/wire_format_unittest.cc171
1 files changed, 163 insertions, 8 deletions
diff --git a/src/google/protobuf/wire_format_unittest.cc b/src/google/protobuf/wire_format_unittest.cc
index 15c37556..736a1282 100644
--- a/src/google/protobuf/wire_format_unittest.cc
+++ b/src/google/protobuf/wire_format_unittest.cc
@@ -45,6 +45,7 @@
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/common.h>
+#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/testing/googletest.h>
#include <gtest/gtest.h>
#include <google/protobuf/stubs/stl_util.h>
@@ -1028,6 +1029,29 @@ TEST_F(WireFormatInvalidInputTest, InvalidSubMessage) {
EXPECT_FALSE(message.ParseFromString(MakeInvalidEmbeddedMessage("\017", 1)));
}
+TEST_F(WireFormatInvalidInputTest, InvalidMessageWithExtraZero) {
+ string data;
+ {
+ // Serialize a valid proto
+ unittest::TestAllTypes message;
+ message.set_optional_int32(1);
+ message.SerializeToString(&data);
+ data.push_back(0); // Append invalid zero tag
+ }
+
+ // Control case.
+ {
+ io::ArrayInputStream ais(data.data(), data.size());
+ io::CodedInputStream is(&ais);
+ unittest::TestAllTypes message;
+ // It should fail but currently passes.
+ EXPECT_TRUE(message.MergePartialFromCodedStream(&is));
+ // Parsing from the string should fail.
+ EXPECT_FALSE(message.ParseFromString(data));
+ }
+}
+
+
TEST_F(WireFormatInvalidInputTest, InvalidGroup) {
unittest::TestAllTypes message;
@@ -1137,7 +1161,7 @@ class Utf8ValidationTest : public ::testing::Test {
TEST_F(Utf8ValidationTest, WriteInvalidUTF8String) {
string wire_buffer;
protobuf_unittest::OneString input;
- vector<string> errors;
+ std::vector<string> errors;
{
ScopedMemoryLog log;
WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
@@ -1161,7 +1185,7 @@ TEST_F(Utf8ValidationTest, ReadInvalidUTF8String) {
protobuf_unittest::OneString input;
WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
protobuf_unittest::OneString output;
- vector<string> errors;
+ std::vector<string> errors;
{
ScopedMemoryLog log;
ReadMessage(wire_buffer, &output);
@@ -1184,7 +1208,7 @@ TEST_F(Utf8ValidationTest, ReadInvalidUTF8String) {
TEST_F(Utf8ValidationTest, WriteValidUTF8String) {
string wire_buffer;
protobuf_unittest::OneString input;
- vector<string> errors;
+ std::vector<string> errors;
{
ScopedMemoryLog log;
WriteMessage(kValidUTF8String, &input, &wire_buffer);
@@ -1198,7 +1222,7 @@ TEST_F(Utf8ValidationTest, ReadValidUTF8String) {
protobuf_unittest::OneString input;
WriteMessage(kValidUTF8String, &input, &wire_buffer);
protobuf_unittest::OneString output;
- vector<string> errors;
+ std::vector<string> errors;
{
ScopedMemoryLog log;
ReadMessage(wire_buffer, &output);
@@ -1212,7 +1236,7 @@ TEST_F(Utf8ValidationTest, ReadValidUTF8String) {
TEST_F(Utf8ValidationTest, WriteArbitraryBytes) {
string wire_buffer;
protobuf_unittest::OneBytes input;
- vector<string> errors;
+ std::vector<string> errors;
{
ScopedMemoryLog log;
WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
@@ -1226,7 +1250,7 @@ TEST_F(Utf8ValidationTest, ReadArbitraryBytes) {
protobuf_unittest::OneBytes input;
WriteMessage(kInvalidUTF8String, &input, &wire_buffer);
protobuf_unittest::OneBytes output;
- vector<string> errors;
+ std::vector<string> errors;
{
ScopedMemoryLog log;
ReadMessage(wire_buffer, &output);
@@ -1244,7 +1268,7 @@ TEST_F(Utf8ValidationTest, ParseRepeatedString) {
string wire_buffer = input.SerializeAsString();
protobuf_unittest::MoreString output;
- vector<string> errors;
+ std::vector<string> errors;
{
ScopedMemoryLog log;
ReadMessage(wire_buffer, &output);
@@ -1263,7 +1287,7 @@ TEST_F(Utf8ValidationTest, ParseRepeatedString) {
TEST_F(Utf8ValidationTest, OldVerifyUTF8String) {
string data(kInvalidUTF8String);
- vector<string> errors;
+ std::vector<string> errors;
{
ScopedMemoryLog log;
WireFormat::VerifyUTF8String(data.data(), data.size(),
@@ -1282,6 +1306,137 @@ TEST_F(Utf8ValidationTest, OldVerifyUTF8String) {
}
+TEST(RepeatedVarint, Int32) {
+ RepeatedField<int32> v;
+
+ // Insert -2^n, 2^n and 2^n-1.
+ for (int n = 0; n < 10; n++) {
+ v.Add(-(1 << n));
+ v.Add(1 << n);
+ v.Add((1 << n) - 1);
+ }
+
+ // Check consistency with the scalar Int32Size.
+ size_t expected = 0;
+ for (int i = 0; i < v.size(); i++) {
+ expected += WireFormatLite::Int32Size(v[i]);
+ }
+
+ EXPECT_EQ(expected, WireFormatLite::Int32Size(v));
+}
+
+TEST(RepeatedVarint, Int64) {
+ RepeatedField<int64> v;
+
+ // Insert -2^n, 2^n and 2^n-1.
+ for (int n = 0; n < 10; n++) {
+ v.Add(-(1 << n));
+ v.Add(1 << n);
+ v.Add((1 << n) - 1);
+ }
+
+ // Check consistency with the scalar Int64Size.
+ size_t expected = 0;
+ for (int i = 0; i < v.size(); i++) {
+ expected += WireFormatLite::Int64Size(v[i]);
+ }
+
+ EXPECT_EQ(expected, WireFormatLite::Int64Size(v));
+}
+
+TEST(RepeatedVarint, SInt32) {
+ RepeatedField<int32> v;
+
+ // Insert -2^n, 2^n and 2^n-1.
+ for (int n = 0; n < 10; n++) {
+ v.Add(-(1 << n));
+ v.Add(1 << n);
+ v.Add((1 << n) - 1);
+ }
+
+ // Check consistency with the scalar SInt32Size.
+ size_t expected = 0;
+ for (int i = 0; i < v.size(); i++) {
+ expected += WireFormatLite::SInt32Size(v[i]);
+ }
+
+ EXPECT_EQ(expected, WireFormatLite::SInt32Size(v));
+}
+
+TEST(RepeatedVarint, SInt64) {
+ RepeatedField<int64> v;
+
+ // Insert -2^n, 2^n and 2^n-1.
+ for (int n = 0; n < 10; n++) {
+ v.Add(-(1 << n));
+ v.Add(1 << n);
+ v.Add((1 << n) - 1);
+ }
+
+ // Check consistency with the scalar SInt64Size.
+ size_t expected = 0;
+ for (int i = 0; i < v.size(); i++) {
+ expected += WireFormatLite::SInt64Size(v[i]);
+ }
+
+ EXPECT_EQ(expected, WireFormatLite::SInt64Size(v));
+}
+
+TEST(RepeatedVarint, UInt32) {
+ RepeatedField<uint32> v;
+
+ // Insert 2^n and 2^n-1.
+ for (int n = 0; n < 10; n++) {
+ v.Add(1 << n);
+ v.Add((1 << n) - 1);
+ }
+
+ // Check consistency with the scalar UInt32Size.
+ size_t expected = 0;
+ for (int i = 0; i < v.size(); i++) {
+ expected += WireFormatLite::UInt32Size(v[i]);
+ }
+
+ EXPECT_EQ(expected, WireFormatLite::UInt32Size(v));
+}
+
+TEST(RepeatedVarint, UInt64) {
+ RepeatedField<uint64> v;
+
+ // Insert 2^n and 2^n-1.
+ for (int n = 0; n < 10; n++) {
+ v.Add(1 << n);
+ v.Add((1 << n) - 1);
+ }
+
+ // Check consistency with the scalar UInt64Size.
+ size_t expected = 0;
+ for (int i = 0; i < v.size(); i++) {
+ expected += WireFormatLite::UInt64Size(v[i]);
+ }
+
+ EXPECT_EQ(expected, WireFormatLite::UInt64Size(v));
+}
+
+TEST(RepeatedVarint, Enum) {
+ RepeatedField<int> v;
+
+ // Insert 2^n and 2^n-1.
+ for (int n = 0; n < 10; n++) {
+ v.Add(1 << n);
+ v.Add((1 << n) - 1);
+ }
+
+ // Check consistency with the scalar EnumSize.
+ size_t expected = 0;
+ for (int i = 0; i < v.size(); i++) {
+ expected += WireFormatLite::EnumSize(v[i]);
+ }
+
+ EXPECT_EQ(expected, WireFormatLite::EnumSize(v));
+}
+
+
} // namespace
} // namespace internal
} // namespace protobuf