Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Side by Side Diff: base/files/memory_mapped_file_unittest.cc

Issue 1798203002: Support read/write memory-mapped files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebased Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 private: 58 private:
59 FilePath temp_file_path_; 59 FilePath temp_file_path_;
60 }; 60 };
61 61
62 TEST_F(MemoryMappedFileTest, MapWholeFileByPath) { 62 TEST_F(MemoryMappedFileTest, MapWholeFileByPath) {
63 const size_t kFileSize = 68 * 1024; 63 const size_t kFileSize = 68 * 1024;
64 CreateTemporaryTestFile(kFileSize); 64 CreateTemporaryTestFile(kFileSize);
65 MemoryMappedFile map; 65 MemoryMappedFile map;
66 map.Initialize(temp_file_path()); 66 map.Initialize(temp_file_path());
67 ASSERT_EQ(kFileSize, map.length()); 67 ASSERT_EQ(kFileSize, map.length());
68 ASSERT_TRUE(map.data() != NULL); 68 ASSERT_TRUE(map.data() != nullptr);
69 EXPECT_TRUE(map.IsValid()); 69 EXPECT_TRUE(map.IsValid());
70 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0)); 70 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
71 } 71 }
72 72
73 TEST_F(MemoryMappedFileTest, MapWholeFileByFD) { 73 TEST_F(MemoryMappedFileTest, MapWholeFileByFD) {
74 const size_t kFileSize = 68 * 1024; 74 const size_t kFileSize = 68 * 1024;
75 CreateTemporaryTestFile(kFileSize); 75 CreateTemporaryTestFile(kFileSize);
76 MemoryMappedFile map; 76 MemoryMappedFile map;
77 map.Initialize(File(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ)); 77 map.Initialize(File(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ));
78 ASSERT_EQ(kFileSize, map.length()); 78 ASSERT_EQ(kFileSize, map.length());
79 ASSERT_TRUE(map.data() != NULL); 79 ASSERT_TRUE(map.data() != nullptr);
80 EXPECT_TRUE(map.IsValid()); 80 EXPECT_TRUE(map.IsValid());
81 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0)); 81 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
82 } 82 }
83 83
84 TEST_F(MemoryMappedFileTest, MapSmallFile) { 84 TEST_F(MemoryMappedFileTest, MapSmallFile) {
85 const size_t kFileSize = 127; 85 const size_t kFileSize = 127;
86 CreateTemporaryTestFile(kFileSize); 86 CreateTemporaryTestFile(kFileSize);
87 MemoryMappedFile map; 87 MemoryMappedFile map;
88 map.Initialize(temp_file_path()); 88 map.Initialize(temp_file_path());
89 ASSERT_EQ(kFileSize, map.length()); 89 ASSERT_EQ(kFileSize, map.length());
90 ASSERT_TRUE(map.data() != NULL); 90 ASSERT_TRUE(map.data() != nullptr);
91 EXPECT_TRUE(map.IsValid()); 91 EXPECT_TRUE(map.IsValid());
92 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0)); 92 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
93 } 93 }
94 94
95 TEST_F(MemoryMappedFileTest, MapWholeFileUsingRegion) { 95 TEST_F(MemoryMappedFileTest, MapWholeFileUsingRegion) {
96 const size_t kFileSize = 157 * 1024; 96 const size_t kFileSize = 157 * 1024;
97 CreateTemporaryTestFile(kFileSize); 97 CreateTemporaryTestFile(kFileSize);
98 MemoryMappedFile map; 98 MemoryMappedFile map;
99 99
100 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ); 100 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
101 map.Initialize(std::move(file), MemoryMappedFile::Region::kWholeFile); 101 map.Initialize(std::move(file), MemoryMappedFile::Region::kWholeFile);
102 ASSERT_EQ(kFileSize, map.length()); 102 ASSERT_EQ(kFileSize, map.length());
103 ASSERT_TRUE(map.data() != NULL); 103 ASSERT_TRUE(map.data() != nullptr);
104 EXPECT_TRUE(map.IsValid()); 104 EXPECT_TRUE(map.IsValid());
105 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0)); 105 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
106 } 106 }
107 107
108 TEST_F(MemoryMappedFileTest, MapPartialRegionAtBeginning) { 108 TEST_F(MemoryMappedFileTest, MapPartialRegionAtBeginning) {
109 const size_t kFileSize = 157 * 1024; 109 const size_t kFileSize = 157 * 1024;
110 const size_t kPartialSize = 4 * 1024 + 32; 110 const size_t kPartialSize = 4 * 1024 + 32;
111 CreateTemporaryTestFile(kFileSize); 111 CreateTemporaryTestFile(kFileSize);
112 MemoryMappedFile map; 112 MemoryMappedFile map;
113 113
114 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ); 114 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
115 MemoryMappedFile::Region region = {0, kPartialSize}; 115 MemoryMappedFile::Region region = {0, kPartialSize};
116 map.Initialize(std::move(file), region); 116 map.Initialize(std::move(file), region);
117 ASSERT_EQ(kPartialSize, map.length()); 117 ASSERT_EQ(kPartialSize, map.length());
118 ASSERT_TRUE(map.data() != NULL); 118 ASSERT_TRUE(map.data() != nullptr);
119 EXPECT_TRUE(map.IsValid()); 119 EXPECT_TRUE(map.IsValid());
120 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, 0)); 120 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, 0));
121 } 121 }
122 122
123 TEST_F(MemoryMappedFileTest, MapPartialRegionAtEnd) { 123 TEST_F(MemoryMappedFileTest, MapPartialRegionAtEnd) {
124 const size_t kFileSize = 157 * 1024; 124 const size_t kFileSize = 157 * 1024;
125 const size_t kPartialSize = 5 * 1024 - 32; 125 const size_t kPartialSize = 5 * 1024 - 32;
126 const size_t kOffset = kFileSize - kPartialSize; 126 const size_t kOffset = kFileSize - kPartialSize;
127 CreateTemporaryTestFile(kFileSize); 127 CreateTemporaryTestFile(kFileSize);
128 MemoryMappedFile map; 128 MemoryMappedFile map;
129 129
130 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ); 130 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
131 MemoryMappedFile::Region region = {kOffset, kPartialSize}; 131 MemoryMappedFile::Region region = {kOffset, kPartialSize};
132 map.Initialize(std::move(file), region); 132 map.Initialize(std::move(file), region);
133 ASSERT_EQ(kPartialSize, map.length()); 133 ASSERT_EQ(kPartialSize, map.length());
134 ASSERT_TRUE(map.data() != NULL); 134 ASSERT_TRUE(map.data() != nullptr);
135 EXPECT_TRUE(map.IsValid()); 135 EXPECT_TRUE(map.IsValid());
136 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset)); 136 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset));
137 } 137 }
138 138
139 TEST_F(MemoryMappedFileTest, MapSmallPartialRegionInTheMiddle) { 139 TEST_F(MemoryMappedFileTest, MapSmallPartialRegionInTheMiddle) {
140 const size_t kFileSize = 157 * 1024; 140 const size_t kFileSize = 157 * 1024;
141 const size_t kOffset = 1024 * 5 + 32; 141 const size_t kOffset = 1024 * 5 + 32;
142 const size_t kPartialSize = 8; 142 const size_t kPartialSize = 8;
143 143
144 CreateTemporaryTestFile(kFileSize); 144 CreateTemporaryTestFile(kFileSize);
145 MemoryMappedFile map; 145 MemoryMappedFile map;
146 146
147 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ); 147 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
148 MemoryMappedFile::Region region = {kOffset, kPartialSize}; 148 MemoryMappedFile::Region region = {kOffset, kPartialSize};
149 map.Initialize(std::move(file), region); 149 map.Initialize(std::move(file), region);
150 ASSERT_EQ(kPartialSize, map.length()); 150 ASSERT_EQ(kPartialSize, map.length());
151 ASSERT_TRUE(map.data() != NULL); 151 ASSERT_TRUE(map.data() != nullptr);
152 EXPECT_TRUE(map.IsValid()); 152 EXPECT_TRUE(map.IsValid());
153 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset)); 153 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset));
154 } 154 }
155 155
156 TEST_F(MemoryMappedFileTest, MapLargePartialRegionInTheMiddle) { 156 TEST_F(MemoryMappedFileTest, MapLargePartialRegionInTheMiddle) {
157 const size_t kFileSize = 157 * 1024; 157 const size_t kFileSize = 157 * 1024;
158 const size_t kOffset = 1024 * 5 + 32; 158 const size_t kOffset = 1024 * 5 + 32;
159 const size_t kPartialSize = 16 * 1024 - 32; 159 const size_t kPartialSize = 16 * 1024 - 32;
160 160
161 CreateTemporaryTestFile(kFileSize); 161 CreateTemporaryTestFile(kFileSize);
162 MemoryMappedFile map; 162 MemoryMappedFile map;
163 163
164 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ); 164 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
165 MemoryMappedFile::Region region = {kOffset, kPartialSize}; 165 MemoryMappedFile::Region region = {kOffset, kPartialSize};
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() != nullptr);
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 TEST_F(MemoryMappedFileTest, WriteableFile) {
174 const size_t kFileSize = 127;
175 CreateTemporaryTestFile(kFileSize);
176
177 {
178 MemoryMappedFile map;
179 map.Initialize(temp_file_path(), MemoryMappedFile::READ_WRITE);
180 ASSERT_EQ(kFileSize, map.length());
181 ASSERT_TRUE(map.data() != nullptr);
182 EXPECT_TRUE(map.IsValid());
183 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
184
185 uint8_t* bytes = map.data();
186 bytes[0] = 'B';
187 bytes[1] = 'a';
188 bytes[2] = 'r';
189 bytes[kFileSize - 1] = '!';
190 EXPECT_FALSE(CheckBufferContents(map.data(), kFileSize, 0));
191 EXPECT_TRUE(CheckBufferContents(map.data() + 3, kFileSize - 4, 3));
192 }
193
194 int64_t file_size;
195 ASSERT_TRUE(GetFileSize(temp_file_path(), &file_size));
196 EXPECT_EQ(static_cast<int64_t>(kFileSize), file_size);
197
198 std::string contents;
199 ASSERT_TRUE(ReadFileToString(temp_file_path(), &contents));
200 EXPECT_EQ("Bar", contents.substr(0, 3));
201 EXPECT_EQ("!", contents.substr(kFileSize - 1, 1));
202 }
203
204 TEST_F(MemoryMappedFileTest, ExtendableFile) {
205 const size_t kFileSize = 127;
206 const size_t kFileExtend = 100;
207 CreateTemporaryTestFile(kFileSize);
208
209 {
210 File file(temp_file_path(),
211 File::FLAG_OPEN | File::FLAG_READ | File::FLAG_WRITE);
212 MemoryMappedFile::Region region = {0, kFileSize + kFileExtend};
213 MemoryMappedFile map;
214 map.Initialize(std::move(file), region,
215 MemoryMappedFile::READ_WRITE_EXTEND);
216 EXPECT_EQ(kFileSize + kFileExtend, map.length());
217 ASSERT_TRUE(map.data() != nullptr);
218 EXPECT_TRUE(map.IsValid());
219 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
220
221 uint8_t* bytes = map.data();
222 EXPECT_EQ(0, bytes[kFileSize + 0]);
223 EXPECT_EQ(0, bytes[kFileSize + 1]);
224 EXPECT_EQ(0, bytes[kFileSize + 2]);
225 bytes[kFileSize + 0] = 'B';
226 bytes[kFileSize + 1] = 'A';
227 bytes[kFileSize + 2] = 'Z';
228 EXPECT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
229 }
230
231 int64_t file_size;
232 ASSERT_TRUE(GetFileSize(temp_file_path(), &file_size));
233 EXPECT_LE(static_cast<int64_t>(kFileSize + 3), file_size);
234 EXPECT_GE(static_cast<int64_t>(kFileSize + kFileExtend), file_size);
235
236 std::string contents;
237 ASSERT_TRUE(ReadFileToString(temp_file_path(), &contents));
238 EXPECT_EQ("BAZ", contents.substr(kFileSize, 3));
239 }
240
173 } // namespace 241 } // namespace
174 242
175 } // namespace base 243 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698