Chromium Code Reviews| 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 "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "testing/platform_test.h" | 10 #include "testing/platform_test.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 MemoryMappedFile map; | 154 MemoryMappedFile map; |
| 155 | 155 |
| 156 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ); | 156 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ); |
| 157 map.Initialize(file.Pass(), MemoryMappedFile::Region(kOffset, kPartialSize)); | 157 map.Initialize(file.Pass(), MemoryMappedFile::Region(kOffset, kPartialSize)); |
| 158 ASSERT_EQ(kPartialSize, map.length()); | 158 ASSERT_EQ(kPartialSize, map.length()); |
| 159 ASSERT_TRUE(map.data() != NULL); | 159 ASSERT_TRUE(map.data() != NULL); |
| 160 EXPECT_TRUE(map.IsValid()); | 160 EXPECT_TRUE(map.IsValid()); |
| 161 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset)); | 161 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset)); |
| 162 } | 162 } |
| 163 | 163 |
| 164 TEST_F(MemoryMappedFileTest, MapWholeFileWithWrite) { | |
| 165 const size_t kFileSize = 68 * 1024; | |
| 166 CreateTemporaryTestFile(kFileSize); | |
| 167 MemoryMappedFile map; | |
| 168 map.Initialize(File(temp_file_path(), File::FLAG_OPEN | | |
| 169 File::FLAG_READ | | |
| 170 File::FLAG_WRITE), true); | |
| 171 ASSERT_EQ(kFileSize, map.length()); | |
| 172 ASSERT_TRUE(map.data() != NULL); | |
| 173 EXPECT_TRUE(map.IsValid()); | |
| 174 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0)); | |
| 175 | |
| 176 const char* test_string = "TEST_STRING"; | |
|
wuchengli
2014/09/01 06:57:15
Can we use CreateTestBuffer with a non-0 offset?
henryhsu
2014/09/01 07:49:49
Done.
| |
| 177 memcpy(map.data(), test_string, strlen(test_string)); | |
| 178 map.CloseHandles(); | |
| 179 | |
| 180 MemoryMappedFile read_map; | |
| 181 read_map.Initialize(File(temp_file_path(), File::FLAG_OPEN | | |
| 182 File::FLAG_READ)); | |
| 183 ASSERT_EQ(kFileSize, read_map.length()); | |
| 184 ASSERT_TRUE(read_map.data() != NULL); | |
| 185 EXPECT_TRUE(read_map.IsValid()); | |
| 186 ASSERT_TRUE(memcmp(read_map.data(), test_string, strlen(test_string)) == 0); | |
|
wuchengli
2014/09/01 06:57:15
Use CheckBufferContents with a non-0 offset?
henryhsu
2014/09/01 07:49:49
Done.
| |
| 187 } | |
| 188 | |
| 189 TEST_F(MemoryMappedFileTest, MapLargePartialRegionInTheMiddleWithWrite) { | |
| 190 const size_t kFileSize = 157 * 1024; | |
| 191 const size_t kOffset = 1024 * 5 + 32; | |
| 192 const size_t kPartialSize = 16 * 1024 - 32; | |
| 193 | |
| 194 CreateTemporaryTestFile(kFileSize); | |
| 195 MemoryMappedFile map; | |
| 196 | |
| 197 File file(temp_file_path(), File::FLAG_OPEN | | |
| 198 File::FLAG_READ | | |
| 199 File::FLAG_WRITE); | |
| 200 map.Initialize( | |
| 201 file.Pass(), MemoryMappedFile::Region(kOffset, kPartialSize), true); | |
| 202 ASSERT_EQ(kPartialSize, map.length()); | |
| 203 ASSERT_TRUE(map.data() != NULL); | |
| 204 EXPECT_TRUE(map.IsValid()); | |
| 205 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset)); | |
| 206 | |
| 207 const char* test_string = "TEST_STRING"; | |
| 208 memcpy(map.data(), test_string, strlen(test_string)); | |
| 209 map.CloseHandles(); | |
| 210 | |
| 211 MemoryMappedFile read_map; | |
| 212 read_map.Initialize(File(temp_file_path(), File::FLAG_OPEN | | |
| 213 File::FLAG_READ)); | |
| 214 ASSERT_EQ(kFileSize, read_map.length()); | |
| 215 ASSERT_TRUE(read_map.data() != NULL); | |
| 216 EXPECT_TRUE(read_map.IsValid()); | |
| 217 ASSERT_TRUE( | |
| 218 memcmp(read_map.data() + kOffset, test_string, strlen(test_string)) == 0); | |
|
wuchengli
2014/09/01 06:57:15
Use CheckBufferContents with a non-0 offset?
henryhsu
2014/09/01 07:49:49
Done.
| |
| 219 } | |
| 220 | |
| 164 } // namespace | 221 } // namespace |
| 165 | 222 |
| 166 } // namespace base | 223 } // namespace base |
| OLD | NEW |