QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / base / files / memory_mapped_file_unittest.cc
blob05b941c308af2d8b3ae5e0d28e4e5e1425386acb
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
3 // found in the LICENSE file.
5 #include "base/files/memory_mapped_file.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
12 namespace base {
14 namespace {
16 // Create a temporary buffer and fill it with a watermark sequence.
17 scoped_ptr<uint8[]> CreateTestBuffer(size_t size, size_t offset) {
18 scoped_ptr<uint8[]> buf(new uint8[size]);
19 for (size_t i = 0; i < size; ++i)
20 buf.get()[i] = static_cast<uint8>((offset + i) % 253);
21 return buf.Pass();
24 // Check that the watermark sequence is consistent with the |offset| provided.
25 bool CheckBufferContents(const uint8* data, size_t size, size_t offset) {
26 scoped_ptr<uint8[]> test_data(CreateTestBuffer(size, offset));
27 return memcmp(test_data.get(), data, size) == 0;
30 class MemoryMappedFileTest : public PlatformTest {
31 protected:
32 void SetUp() override {
33 PlatformTest::SetUp();
34 CreateTemporaryFile(&temp_file_path_);
37 void TearDown() override { EXPECT_TRUE(DeleteFile(temp_file_path_, false)); }
39 void CreateTemporaryTestFile(size_t size) {
40 File file(temp_file_path_,
41 File::FLAG_CREATE_ALWAYS | File::FLAG_READ | File::FLAG_WRITE);
42 EXPECT_TRUE(file.IsValid());
44 scoped_ptr<uint8[]> test_data(CreateTestBuffer(size, 0));
45 size_t bytes_written =
46 file.Write(0, reinterpret_cast<char*>(test_data.get()), size);
47 EXPECT_EQ(size, bytes_written);
48 file.Close();
51 const FilePath temp_file_path() const { return temp_file_path_; }
53 private:
54 FilePath temp_file_path_;
57 TEST_F(MemoryMappedFileTest, MapWholeFileByPath) {
58 const size_t kFileSize = 68 * 1024;
59 CreateTemporaryTestFile(kFileSize);
60 MemoryMappedFile map;
61 map.Initialize(temp_file_path());
62 ASSERT_EQ(kFileSize, map.length());
63 ASSERT_TRUE(map.data() != NULL);
64 EXPECT_TRUE(map.IsValid());
65 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
68 TEST_F(MemoryMappedFileTest, MapWholeFileByFD) {
69 const size_t kFileSize = 68 * 1024;
70 CreateTemporaryTestFile(kFileSize);
71 MemoryMappedFile map;
72 map.Initialize(File(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ));
73 ASSERT_EQ(kFileSize, map.length());
74 ASSERT_TRUE(map.data() != NULL);
75 EXPECT_TRUE(map.IsValid());
76 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
79 TEST_F(MemoryMappedFileTest, MapSmallFile) {
80 const size_t kFileSize = 127;
81 CreateTemporaryTestFile(kFileSize);
82 MemoryMappedFile map;
83 map.Initialize(temp_file_path());
84 ASSERT_EQ(kFileSize, map.length());
85 ASSERT_TRUE(map.data() != NULL);
86 EXPECT_TRUE(map.IsValid());
87 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
90 TEST_F(MemoryMappedFileTest, MapWholeFileUsingRegion) {
91 const size_t kFileSize = 157 * 1024;
92 CreateTemporaryTestFile(kFileSize);
93 MemoryMappedFile map;
95 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
96 map.Initialize(file.Pass(), MemoryMappedFile::Region::kWholeFile);
97 ASSERT_EQ(kFileSize, map.length());
98 ASSERT_TRUE(map.data() != NULL);
99 EXPECT_TRUE(map.IsValid());
100 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
103 TEST_F(MemoryMappedFileTest, MapPartialRegionAtBeginning) {
104 const size_t kFileSize = 157 * 1024;
105 const size_t kPartialSize = 4 * 1024 + 32;
106 CreateTemporaryTestFile(kFileSize);
107 MemoryMappedFile map;
109 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
110 MemoryMappedFile::Region region = {0, kPartialSize};
111 map.Initialize(file.Pass(), region);
112 ASSERT_EQ(kPartialSize, map.length());
113 ASSERT_TRUE(map.data() != NULL);
114 EXPECT_TRUE(map.IsValid());
115 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, 0));
118 TEST_F(MemoryMappedFileTest, MapPartialRegionAtEnd) {
119 const size_t kFileSize = 157 * 1024;
120 const size_t kPartialSize = 5 * 1024 - 32;
121 const size_t kOffset = kFileSize - kPartialSize;
122 CreateTemporaryTestFile(kFileSize);
123 MemoryMappedFile map;
125 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
126 MemoryMappedFile::Region region = {kOffset, kPartialSize};
127 map.Initialize(file.Pass(), region);
128 ASSERT_EQ(kPartialSize, map.length());
129 ASSERT_TRUE(map.data() != NULL);
130 EXPECT_TRUE(map.IsValid());
131 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset));
134 TEST_F(MemoryMappedFileTest, MapSmallPartialRegionInTheMiddle) {
135 const size_t kFileSize = 157 * 1024;
136 const size_t kOffset = 1024 * 5 + 32;
137 const size_t kPartialSize = 8;
139 CreateTemporaryTestFile(kFileSize);
140 MemoryMappedFile map;
142 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
143 MemoryMappedFile::Region region = {kOffset, kPartialSize};
144 map.Initialize(file.Pass(), region);
145 ASSERT_EQ(kPartialSize, map.length());
146 ASSERT_TRUE(map.data() != NULL);
147 EXPECT_TRUE(map.IsValid());
148 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset));
151 TEST_F(MemoryMappedFileTest, MapLargePartialRegionInTheMiddle) {
152 const size_t kFileSize = 157 * 1024;
153 const size_t kOffset = 1024 * 5 + 32;
154 const size_t kPartialSize = 16 * 1024 - 32;
156 CreateTemporaryTestFile(kFileSize);
157 MemoryMappedFile map;
159 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
160 MemoryMappedFile::Region region = {kOffset, kPartialSize};
161 map.Initialize(file.Pass(), region);
162 ASSERT_EQ(kPartialSize, map.length());
163 ASSERT_TRUE(map.data() != NULL);
164 EXPECT_TRUE(map.IsValid());
165 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset));
168 } // namespace
170 } // namespace base