| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/files/memory_mapped_file.h" | 5 #include "base/files/memory_mapped_file.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/platform_test.h" | 15 #include "testing/platform_test.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // Create a temporary buffer and fill it with a watermark sequence. | 21 // Create a temporary buffer and fill it with a watermark sequence. |
| 22 scoped_ptr<uint8_t[]> CreateTestBuffer(size_t size, size_t offset) { | 22 std::unique_ptr<uint8_t[]> CreateTestBuffer(size_t size, size_t offset) { |
| 23 scoped_ptr<uint8_t[]> buf(new uint8_t[size]); | 23 std::unique_ptr<uint8_t[]> buf(new uint8_t[size]); |
| 24 for (size_t i = 0; i < size; ++i) | 24 for (size_t i = 0; i < size; ++i) |
| 25 buf.get()[i] = static_cast<uint8_t>((offset + i) % 253); | 25 buf.get()[i] = static_cast<uint8_t>((offset + i) % 253); |
| 26 return buf; | 26 return buf; |
| 27 } | 27 } |
| 28 | 28 |
| 29 // Check that the watermark sequence is consistent with the |offset| provided. | 29 // Check that the watermark sequence is consistent with the |offset| provided. |
| 30 bool CheckBufferContents(const uint8_t* data, size_t size, size_t offset) { | 30 bool CheckBufferContents(const uint8_t* data, size_t size, size_t offset) { |
| 31 scoped_ptr<uint8_t[]> test_data(CreateTestBuffer(size, offset)); | 31 std::unique_ptr<uint8_t[]> test_data(CreateTestBuffer(size, offset)); |
| 32 return memcmp(test_data.get(), data, size) == 0; | 32 return memcmp(test_data.get(), data, size) == 0; |
| 33 } | 33 } |
| 34 | 34 |
| 35 class MemoryMappedFileTest : public PlatformTest { | 35 class MemoryMappedFileTest : public PlatformTest { |
| 36 protected: | 36 protected: |
| 37 void SetUp() override { | 37 void SetUp() override { |
| 38 PlatformTest::SetUp(); | 38 PlatformTest::SetUp(); |
| 39 CreateTemporaryFile(&temp_file_path_); | 39 CreateTemporaryFile(&temp_file_path_); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void TearDown() override { EXPECT_TRUE(DeleteFile(temp_file_path_, false)); } | 42 void TearDown() override { EXPECT_TRUE(DeleteFile(temp_file_path_, false)); } |
| 43 | 43 |
| 44 void CreateTemporaryTestFile(size_t size) { | 44 void CreateTemporaryTestFile(size_t size) { |
| 45 File file(temp_file_path_, | 45 File file(temp_file_path_, |
| 46 File::FLAG_CREATE_ALWAYS | File::FLAG_READ | File::FLAG_WRITE); | 46 File::FLAG_CREATE_ALWAYS | File::FLAG_READ | File::FLAG_WRITE); |
| 47 EXPECT_TRUE(file.IsValid()); | 47 EXPECT_TRUE(file.IsValid()); |
| 48 | 48 |
| 49 scoped_ptr<uint8_t[]> test_data(CreateTestBuffer(size, 0)); | 49 std::unique_ptr<uint8_t[]> test_data(CreateTestBuffer(size, 0)); |
| 50 size_t bytes_written = | 50 size_t bytes_written = |
| 51 file.Write(0, reinterpret_cast<char*>(test_data.get()), size); | 51 file.Write(0, reinterpret_cast<char*>(test_data.get()), size); |
| 52 EXPECT_EQ(size, bytes_written); | 52 EXPECT_EQ(size, bytes_written); |
| 53 file.Close(); | 53 file.Close(); |
| 54 } | 54 } |
| 55 | 55 |
| 56 const FilePath temp_file_path() const { return temp_file_path_; } | 56 const FilePath temp_file_path() const { return temp_file_path_; } |
| 57 | 57 |
| 58 private: | 58 private: |
| 59 FilePath temp_file_path_; | 59 FilePath temp_file_path_; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 map.Initialize(std::move(file), region); | 166 map.Initialize(std::move(file), region); |
| 167 ASSERT_EQ(kPartialSize, map.length()); | 167 ASSERT_EQ(kPartialSize, map.length()); |
| 168 ASSERT_TRUE(map.data() != NULL); | 168 ASSERT_TRUE(map.data() != NULL); |
| 169 EXPECT_TRUE(map.IsValid()); | 169 EXPECT_TRUE(map.IsValid()); |
| 170 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset)); | 170 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset)); |
| 171 } | 171 } |
| 172 | 172 |
| 173 } // namespace | 173 } // namespace |
| 174 | 174 |
| 175 } // namespace base | 175 } // namespace base |
| OLD | NEW |