| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 #include <io.h> | 41 #include <io.h> |
| 42 #else | 42 #else |
| 43 #include <unistd.h> | 43 #include <unistd.h> |
| 44 #endif | 44 #endif |
| 45 #include <sstream> | 45 #include <sstream> |
| 46 #include <fstream> | 46 #include <fstream> |
| 47 | 47 |
| 48 #include <google/protobuf/test_util.h> | 48 #include <google/protobuf/test_util.h> |
| 49 #include <google/protobuf/unittest.pb.h> | 49 #include <google/protobuf/unittest.pb.h> |
| 50 #include <google/protobuf/io/coded_stream.h> | 50 #include <google/protobuf/io/coded_stream.h> |
| 51 #include <google/protobuf/io/zero_copy_stream.h> | |
| 52 #include <google/protobuf/io/zero_copy_stream_impl.h> | 51 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 53 #include <google/protobuf/descriptor.pb.h> | 52 #include <google/protobuf/descriptor.pb.h> |
| 54 #include <google/protobuf/descriptor.h> | 53 #include <google/protobuf/descriptor.h> |
| 55 #include <google/protobuf/generated_message_reflection.h> | 54 #include <google/protobuf/generated_message_reflection.h> |
| 56 | 55 |
| 57 #include <google/protobuf/stubs/logging.h> | 56 #include <google/protobuf/stubs/logging.h> |
| 58 #include <google/protobuf/stubs/common.h> | 57 #include <google/protobuf/stubs/common.h> |
| 59 #include <google/protobuf/stubs/logging.h> | 58 #include <google/protobuf/stubs/logging.h> |
| 60 #include <google/protobuf/testing/googletest.h> | 59 #include <google/protobuf/testing/googletest.h> |
| 61 #include <gtest/gtest.h> | 60 #include <gtest/gtest.h> |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 // EOF is reached before the expected number of bytes. | 175 // EOF is reached before the expected number of bytes. |
| 177 io::ArrayInputStream stream(data.data(), data.size()); | 176 io::ArrayInputStream stream(data.data(), data.size()); |
| 178 protobuf_unittest::TestAllTypes message; | 177 protobuf_unittest::TestAllTypes message; |
| 179 EXPECT_FALSE( | 178 EXPECT_FALSE( |
| 180 message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); | 179 message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); |
| 181 } | 180 } |
| 182 } | 181 } |
| 183 | 182 |
| 184 TEST(MessageTest, ParseFailsIfNotInitialized) { | 183 TEST(MessageTest, ParseFailsIfNotInitialized) { |
| 185 unittest::TestRequired message; | 184 unittest::TestRequired message; |
| 186 std::vector<string> errors; | 185 vector<string> errors; |
| 187 | 186 |
| 188 { | 187 { |
| 189 ScopedMemoryLog log; | 188 ScopedMemoryLog log; |
| 190 EXPECT_FALSE(message.ParseFromString("")); | 189 EXPECT_FALSE(message.ParseFromString("")); |
| 191 errors = log.GetMessages(ERROR); | 190 errors = log.GetMessages(ERROR); |
| 192 } | 191 } |
| 193 | 192 |
| 194 ASSERT_EQ(1, errors.size()); | 193 ASSERT_EQ(1, errors.size()); |
| 195 EXPECT_EQ("Can't parse message of type \"protobuf_unittest.TestRequired\" " | 194 EXPECT_EQ("Can't parse message of type \"protobuf_unittest.TestRequired\" " |
| 196 "because it is missing required fields: a, b, c", | 195 "because it is missing required fields: a, b, c", |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 } | 258 } |
| 260 | 259 |
| 261 for (int i = 0; i < 2 * 1024 + 1; ++i) { | 260 for (int i = 0; i < 2 * 1024 + 1; ++i) { |
| 262 message.add_repeated_cord()->CopyFrom(one_megabyte); | 261 message.add_repeated_cord()->CopyFrom(one_megabyte); |
| 263 } | 262 } |
| 264 | 263 |
| 265 Cord serialized; | 264 Cord serialized; |
| 266 EXPECT_FALSE(message.AppendToCord(&serialized)); | 265 EXPECT_FALSE(message.AppendToCord(&serialized)); |
| 267 } | 266 } |
| 268 | 267 |
| 269 TEST(MessageTest, CheckBigOverflow) { | |
| 270 unittest::TestAllTypes message; | |
| 271 // Create a message with size just over 4GB. We should be able to detect this | |
| 272 // too, even though it will make a plain "int" wrap back to a positive number. | |
| 273 const string data(1024, 'x'); | |
| 274 Cord one_megabyte; | |
| 275 for (int i = 0; i < 1024; i++) { | |
| 276 one_megabyte.Append(data); | |
| 277 } | |
| 278 | |
| 279 for (int i = 0; i < 4 * 1024 + 1; ++i) { | |
| 280 message.add_repeated_cord()->CopyFrom(one_megabyte); | |
| 281 } | |
| 282 | |
| 283 Cord serialized; | |
| 284 EXPECT_FALSE(message.AppendToCord(&serialized)); | |
| 285 } | |
| 286 | |
| 287 #endif // PROTOBUF_HAS_DEATH_TEST | 268 #endif // PROTOBUF_HAS_DEATH_TEST |
| 288 | 269 |
| 289 namespace { | 270 namespace { |
| 290 // An input stream that repeats a string's content for a number of times. It | 271 |
| 291 // helps us create a really large input without consuming too much memory. Used | 272 class NegativeByteSize : public unittest::TestRequired { |
| 292 // to test the parsing behavior when the input size exceeds 2G or close to it. | |
| 293 class RepeatedInputStream : public io::ZeroCopyInputStream { | |
| 294 public: | 273 public: |
| 295 RepeatedInputStream(const string& data, size_t count) | 274 virtual int ByteSize() const { return -1; } |
| 296 : data_(data), count_(count), position_(0), total_byte_count_(0) {} | 275 }; |
| 297 | 276 |
| 298 virtual bool Next(const void** data, int* size) { | |
| 299 if (position_ == data_.size()) { | |
| 300 if (--count_ == 0) { | |
| 301 return false; | |
| 302 } | |
| 303 position_ = 0; | |
| 304 } | |
| 305 *data = &data_[position_]; | |
| 306 *size = static_cast<int>(data_.size() - position_); | |
| 307 position_ = data_.size(); | |
| 308 total_byte_count_ += *size; | |
| 309 return true; | |
| 310 } | |
| 311 | |
| 312 virtual void BackUp(int count) { | |
| 313 position_ -= static_cast<size_t>(count); | |
| 314 total_byte_count_ -= count; | |
| 315 } | |
| 316 | |
| 317 virtual bool Skip(int count) { | |
| 318 while (count > 0) { | |
| 319 const void* data; | |
| 320 int size; | |
| 321 if (!Next(&data, &size)) { | |
| 322 break; | |
| 323 } | |
| 324 if (size >= count) { | |
| 325 BackUp(size - count); | |
| 326 return true; | |
| 327 } else { | |
| 328 count -= size; | |
| 329 } | |
| 330 } | |
| 331 return false; | |
| 332 } | |
| 333 | |
| 334 virtual int64 ByteCount() const { return total_byte_count_; } | |
| 335 | |
| 336 private: | |
| 337 string data_; | |
| 338 size_t count_; // The number of strings that haven't been consuemd. | |
| 339 size_t position_; // Position in the string for the next read. | |
| 340 int64 total_byte_count_; | |
| 341 }; | |
| 342 } // namespace | 277 } // namespace |
| 343 | 278 |
| 344 TEST(MessageTest, TestParseMessagesCloseTo2G) { | 279 TEST(MessageTest, SerializationFailsOnNegativeByteSize) { |
| 345 // Create a message with a large string field. | 280 NegativeByteSize message; |
| 346 string value = string(64 * 1024 * 1024, 'x'); | 281 string string_output; |
| 347 protobuf_unittest::TestAllTypes message; | 282 EXPECT_FALSE(message.AppendPartialToString(&string_output)); |
| 348 message.set_optional_string(value); | |
| 349 | 283 |
| 350 // Repeat this message in the input stream to make the total input size | 284 io::ArrayOutputStream coded_raw_output(NULL, 100); |
| 351 // close to 2G. | 285 io::CodedOutputStream coded_output(&coded_raw_output); |
| 352 string data = message.SerializeAsString(); | 286 EXPECT_FALSE(message.SerializePartialToCodedStream(&coded_output)); |
| 353 size_t count = static_cast<size_t>(kint32max) / data.size(); | |
| 354 RepeatedInputStream input(data, count); | |
| 355 | |
| 356 // The parsing should succeed. | |
| 357 protobuf_unittest::TestAllTypes result; | |
| 358 EXPECT_TRUE(result.ParseFromZeroCopyStream(&input)); | |
| 359 | |
| 360 // When there are multiple occurences of a singulr field, the last one | |
| 361 // should win. | |
| 362 EXPECT_EQ(value, result.optional_string()); | |
| 363 } | |
| 364 | |
| 365 TEST(MessageTest, TestParseMessagesOver2G) { | |
| 366 // Create a message with a large string field. | |
| 367 string value = string(64 * 1024 * 1024, 'x'); | |
| 368 protobuf_unittest::TestAllTypes message; | |
| 369 message.set_optional_string(value); | |
| 370 | |
| 371 // Repeat this message in the input stream to make the total input size | |
| 372 // larger than 2G. | |
| 373 string data = message.SerializeAsString(); | |
| 374 size_t count = static_cast<size_t>(kint32max) / data.size() + 1; | |
| 375 RepeatedInputStream input(data, count); | |
| 376 | |
| 377 // The parsing should fail. | |
| 378 protobuf_unittest::TestAllTypes result; | |
| 379 EXPECT_FALSE(result.ParseFromZeroCopyStream(&input)); | |
| 380 } | 287 } |
| 381 | 288 |
| 382 TEST(MessageTest, BypassInitializationCheckOnSerialize) { | 289 TEST(MessageTest, BypassInitializationCheckOnSerialize) { |
| 383 unittest::TestRequired message; | 290 unittest::TestRequired message; |
| 384 io::ArrayOutputStream raw_output(NULL, 0); | 291 io::ArrayOutputStream raw_output(NULL, 0); |
| 385 io::CodedOutputStream output(&raw_output); | 292 io::CodedOutputStream output(&raw_output); |
| 386 EXPECT_TRUE(message.SerializePartialToCodedStream(&output)); | 293 EXPECT_TRUE(message.SerializePartialToCodedStream(&output)); |
| 387 } | 294 } |
| 388 | 295 |
| 389 TEST(MessageTest, FindInitializationErrors) { | 296 TEST(MessageTest, FindInitializationErrors) { |
| 390 unittest::TestRequired message; | 297 unittest::TestRequired message; |
| 391 std::vector<string> errors; | 298 vector<string> errors; |
| 392 message.FindInitializationErrors(&errors); | 299 message.FindInitializationErrors(&errors); |
| 393 ASSERT_EQ(3, errors.size()); | 300 ASSERT_EQ(3, errors.size()); |
| 394 EXPECT_EQ("a", errors[0]); | 301 EXPECT_EQ("a", errors[0]); |
| 395 EXPECT_EQ("b", errors[1]); | 302 EXPECT_EQ("b", errors[1]); |
| 396 EXPECT_EQ("c", errors[2]); | 303 EXPECT_EQ("c", errors[2]); |
| 397 } | 304 } |
| 398 | 305 |
| 399 TEST(MessageTest, ParseFailsOnInvalidMessageEnd) { | 306 TEST(MessageTest, ParseFailsOnInvalidMessageEnd) { |
| 400 unittest::TestAllTypes message; | 307 unittest::TestAllTypes message; |
| 401 | 308 |
| 402 // Control case. | 309 // Control case. |
| 403 EXPECT_TRUE(message.ParseFromArray("", 0)); | 310 EXPECT_TRUE(message.ParseFromArray("", 0)); |
| 404 | 311 |
| 405 // The byte is a valid varint, but not a valid tag (zero). | 312 // The byte is a valid varint, but not a valid tag (zero). |
| 406 EXPECT_FALSE(message.ParseFromArray("\0", 1)); | 313 EXPECT_FALSE(message.ParseFromArray("\0", 1)); |
| 407 | 314 |
| 408 // The byte is a malformed varint. | 315 // The byte is a malformed varint. |
| 409 EXPECT_FALSE(message.ParseFromArray("\200", 1)); | 316 EXPECT_FALSE(message.ParseFromArray("\200", 1)); |
| 410 | 317 |
| 411 // The byte is an endgroup tag, but we aren't parsing a group. | 318 // The byte is an endgroup tag, but we aren't parsing a group. |
| 412 EXPECT_FALSE(message.ParseFromArray("\014", 1)); | 319 EXPECT_FALSE(message.ParseFromArray("\014", 1)); |
| 413 } | 320 } |
| 414 | 321 |
| 415 // Regression test for b/23630858 | |
| 416 TEST(MessageTest, MessageIsStillValidAfterParseFails) { | |
| 417 unittest::TestAllTypes message; | |
| 418 | |
| 419 // 9 0xFFs for the "optional_uint64" field. | |
| 420 string invalid_data = "\x20\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"; | |
| 421 | |
| 422 EXPECT_FALSE(message.ParseFromString(invalid_data)); | |
| 423 message.Clear(); | |
| 424 EXPECT_EQ(0, message.optional_uint64()); | |
| 425 } | |
| 426 | |
| 427 namespace { | 322 namespace { |
| 428 | 323 |
| 429 void ExpectMessageMerged(const unittest::TestAllTypes& message) { | 324 void ExpectMessageMerged(const unittest::TestAllTypes& message) { |
| 430 EXPECT_EQ(3, message.optional_int32()); | 325 EXPECT_EQ(3, message.optional_int32()); |
| 431 EXPECT_EQ(2, message.optional_int64()); | 326 EXPECT_EQ(2, message.optional_int64()); |
| 432 EXPECT_EQ("hello", message.optional_string()); | 327 EXPECT_EQ("hello", message.optional_string()); |
| 433 } | 328 } |
| 434 | 329 |
| 435 void AssignParsingMergeMessages( | 330 void AssignParsingMergeMessages( |
| 436 unittest::TestAllTypes* msg1, | 331 unittest::TestAllTypes* msg1, |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 const Descriptor* descriptor = pool.BuildFile(file)->message_type(0); | 462 const Descriptor* descriptor = pool.BuildFile(file)->message_type(0); |
| 568 | 463 |
| 569 // Trying to construct it should return NULL. | 464 // Trying to construct it should return NULL. |
| 570 EXPECT_TRUE( | 465 EXPECT_TRUE( |
| 571 MessageFactory::generated_factory()->GetPrototype(descriptor) == NULL); | 466 MessageFactory::generated_factory()->GetPrototype(descriptor) == NULL); |
| 572 } | 467 } |
| 573 | 468 |
| 574 | 469 |
| 575 } // namespace protobuf | 470 } // namespace protobuf |
| 576 } // namespace google | 471 } // namespace google |
| OLD | NEW |