| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/pickle.h" |
| 6 |
| 5 #include <limits.h> | 7 #include <limits.h> |
| 6 #include <stddef.h> | 8 #include <stddef.h> |
| 7 #include <stdint.h> | 9 #include <stdint.h> |
| 8 | 10 |
| 11 #include <memory> |
| 9 #include <string> | 12 #include <string> |
| 10 | 13 |
| 11 #include "base/macros.h" | 14 #include "base/macros.h" |
| 12 #include "base/memory/scoped_ptr.h" | |
| 13 #include "base/pickle.h" | |
| 14 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 const bool testbool1 = false; | 23 const bool testbool1 = false; |
| 23 const bool testbool2 = true; | 24 const bool testbool2 = true; |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 EXPECT_DEATH(ignore_result(iter.ReadLong(&outlong)), ""); | 157 EXPECT_DEATH(ignore_result(iter.ReadLong(&outlong)), ""); |
| 157 #endif | 158 #endif |
| 158 } else { | 159 } else { |
| 159 EXPECT_TRUE(iter.ReadLong(&outlong)); | 160 EXPECT_TRUE(iter.ReadLong(&outlong)); |
| 160 EXPECT_EQ(testint64, outlong); | 161 EXPECT_EQ(testint64, outlong); |
| 161 } | 162 } |
| 162 } | 163 } |
| 163 | 164 |
| 164 // Tests that we can handle really small buffers. | 165 // Tests that we can handle really small buffers. |
| 165 TEST(PickleTest, SmallBuffer) { | 166 TEST(PickleTest, SmallBuffer) { |
| 166 scoped_ptr<char[]> buffer(new char[1]); | 167 std::unique_ptr<char[]> buffer(new char[1]); |
| 167 | 168 |
| 168 // We should not touch the buffer. | 169 // We should not touch the buffer. |
| 169 Pickle pickle(buffer.get(), 1); | 170 Pickle pickle(buffer.get(), 1); |
| 170 | 171 |
| 171 PickleIterator iter(pickle); | 172 PickleIterator iter(pickle); |
| 172 int data; | 173 int data; |
| 173 EXPECT_FALSE(iter.ReadInt(&data)); | 174 EXPECT_FALSE(iter.ReadInt(&data)); |
| 174 } | 175 } |
| 175 | 176 |
| 176 // Tests that we can handle improper headers. | 177 // Tests that we can handle improper headers. |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 const char* start = reinterpret_cast<const char*>(pickle.data()); | 323 const char* start = reinterpret_cast<const char*>(pickle.data()); |
| 323 const char* end = start + pickle.size(); | 324 const char* end = start + pickle.size(); |
| 324 | 325 |
| 325 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end)); | 326 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end)); |
| 326 EXPECT_TRUE(NULL == Pickle::FindNext(pickle.header_size_, start, end - 1)); | 327 EXPECT_TRUE(NULL == Pickle::FindNext(pickle.header_size_, start, end - 1)); |
| 327 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end + 1)); | 328 EXPECT_TRUE(end == Pickle::FindNext(pickle.header_size_, start, end + 1)); |
| 328 } | 329 } |
| 329 | 330 |
| 330 TEST(PickleTest, FindNextWithIncompleteHeader) { | 331 TEST(PickleTest, FindNextWithIncompleteHeader) { |
| 331 size_t header_size = sizeof(Pickle::Header); | 332 size_t header_size = sizeof(Pickle::Header); |
| 332 scoped_ptr<char[]> buffer(new char[header_size - 1]); | 333 std::unique_ptr<char[]> buffer(new char[header_size - 1]); |
| 333 memset(buffer.get(), 0x1, header_size - 1); | 334 memset(buffer.get(), 0x1, header_size - 1); |
| 334 | 335 |
| 335 const char* start = buffer.get(); | 336 const char* start = buffer.get(); |
| 336 const char* end = start + header_size - 1; | 337 const char* end = start + header_size - 1; |
| 337 | 338 |
| 338 EXPECT_TRUE(NULL == Pickle::FindNext(header_size, start, end)); | 339 EXPECT_TRUE(NULL == Pickle::FindNext(header_size, start, end)); |
| 339 } | 340 } |
| 340 | 341 |
| 341 #if defined(COMPILER_MSVC) | 342 #if defined(COMPILER_MSVC) |
| 342 #pragma warning(push) | 343 #pragma warning(push) |
| 343 #pragma warning(disable: 4146) | 344 #pragma warning(disable: 4146) |
| 344 #endif | 345 #endif |
| 345 TEST(PickleTest, FindNextOverflow) { | 346 TEST(PickleTest, FindNextOverflow) { |
| 346 size_t header_size = sizeof(Pickle::Header); | 347 size_t header_size = sizeof(Pickle::Header); |
| 347 size_t header_size2 = 2 * header_size; | 348 size_t header_size2 = 2 * header_size; |
| 348 size_t payload_received = 100; | 349 size_t payload_received = 100; |
| 349 scoped_ptr<char[]> buffer(new char[header_size2 + payload_received]); | 350 std::unique_ptr<char[]> buffer(new char[header_size2 + payload_received]); |
| 350 const char* start = buffer.get(); | 351 const char* start = buffer.get(); |
| 351 Pickle::Header* header = reinterpret_cast<Pickle::Header*>(buffer.get()); | 352 Pickle::Header* header = reinterpret_cast<Pickle::Header*>(buffer.get()); |
| 352 const char* end = start + header_size2 + payload_received; | 353 const char* end = start + header_size2 + payload_received; |
| 353 // It is impossible to construct an overflow test otherwise. | 354 // It is impossible to construct an overflow test otherwise. |
| 354 if (sizeof(size_t) > sizeof(header->payload_size) || | 355 if (sizeof(size_t) > sizeof(header->payload_size) || |
| 355 sizeof(uintptr_t) > sizeof(header->payload_size)) | 356 sizeof(uintptr_t) > sizeof(header->payload_size)) |
| 356 return; | 357 return; |
| 357 | 358 |
| 358 header->payload_size = -(reinterpret_cast<uintptr_t>(start) + header_size2); | 359 header->payload_size = -(reinterpret_cast<uintptr_t>(start) + header_size2); |
| 359 EXPECT_TRUE(NULL == Pickle::FindNext(header_size2, start, end)); | 360 EXPECT_TRUE(NULL == Pickle::FindNext(header_size2, start, end)); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 383 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(1)); | 384 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(1)); |
| 384 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(-1)); | 385 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(-1)); |
| 385 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(bytes)); | 386 EXPECT_TRUE(PickleIterator(pickle).GetReadPointerAndAdvance(bytes)); |
| 386 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(bytes + 1)); | 387 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(bytes + 1)); |
| 387 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(INT_MAX)); | 388 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(INT_MAX)); |
| 388 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(INT_MIN)); | 389 EXPECT_FALSE(PickleIterator(pickle).GetReadPointerAndAdvance(INT_MIN)); |
| 389 } | 390 } |
| 390 | 391 |
| 391 TEST(PickleTest, Resize) { | 392 TEST(PickleTest, Resize) { |
| 392 size_t unit = Pickle::kPayloadUnit; | 393 size_t unit = Pickle::kPayloadUnit; |
| 393 scoped_ptr<char[]> data(new char[unit]); | 394 std::unique_ptr<char[]> data(new char[unit]); |
| 394 char* data_ptr = data.get(); | 395 char* data_ptr = data.get(); |
| 395 for (size_t i = 0; i < unit; i++) | 396 for (size_t i = 0; i < unit; i++) |
| 396 data_ptr[i] = 'G'; | 397 data_ptr[i] = 'G'; |
| 397 | 398 |
| 398 // construct a message that will be exactly the size of one payload unit, | 399 // construct a message that will be exactly the size of one payload unit, |
| 399 // note that any data will have a 4-byte header indicating the size | 400 // note that any data will have a 4-byte header indicating the size |
| 400 const size_t payload_size_after_header = unit - sizeof(uint32_t); | 401 const size_t payload_size_after_header = unit - sizeof(uint32_t); |
| 401 Pickle pickle; | 402 Pickle pickle; |
| 402 pickle.WriteData( | 403 pickle.WriteData( |
| 403 data_ptr, static_cast<int>(payload_size_after_header - sizeof(uint32_t))); | 404 data_ptr, static_cast<int>(payload_size_after_header - sizeof(uint32_t))); |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 658 { | 659 { |
| 659 TestingPickle pickle; | 660 TestingPickle pickle; |
| 660 base::PickleSizer sizer; | 661 base::PickleSizer sizer; |
| 661 pickle.WriteBytes(testdata, testdatalen); | 662 pickle.WriteBytes(testdata, testdatalen); |
| 662 sizer.AddBytes(testdatalen); | 663 sizer.AddBytes(testdatalen); |
| 663 EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); | 664 EXPECT_EQ(sizer.payload_size(), pickle.payload_size()); |
| 664 } | 665 } |
| 665 } | 666 } |
| 666 | 667 |
| 667 } // namespace base | 668 } // namespace base |
| OLD | NEW |