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

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

Issue 394313002: Add support for loading pak files from arbitrary file regions. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address first willchan@ comments Created 6 years, 5 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
(Empty)
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.
4
5 #include "base/files/memory_mapped_file.h"
6
7 #include "base/file_util.h"
8 #include "base/files/file_path.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "testing/platform_test.h"
11
12 using base::File;
willchan no longer on Chromium 2014/07/22 20:45:47 You don't need these using statements if everythin
Primiano Tucci (use gerrit) 2014/07/23 23:17:38 Done.
13 using base::FilePath;
14
15 namespace base {
16
17 namespace {
18
19 // Create a temporary buffer and fill it with a watermark sequence.
20 scoped_ptr<uint8[]> CreateTestBuffer(size_t size, size_t offset) {
21 scoped_ptr<uint8[]> buf(new uint8[size]);
22 for (size_t i = 0; i < size; ++i)
23 buf.get()[i] = static_cast<uint8>((offset + i) % 253);
24 return buf.Pass();
25 }
26
27 // Check that the watermark sequence is consistent with the |offset| provided.
28 bool CheckBufferContents(const uint8* data, size_t size, size_t offset) {
29 scoped_ptr<uint8[]> test_data(CreateTestBuffer(size, offset));
30 return memcmp(test_data.get(), data, size) == 0;
31 }
32
33 class MemoryMappedFileTest : public PlatformTest {
34 protected:
35 virtual void SetUp() OVERRIDE {
36 PlatformTest::SetUp();
37 base::CreateTemporaryFile(&temp_file_path_);
38 }
39
40 virtual void TearDown() {
41 EXPECT_TRUE(base::DeleteFile(temp_file_path_, false));
42 }
43
44 void CreateTemporaryTestFile(size_t size) {
45 File file(temp_file_path_,
46 File::FLAG_CREATE_ALWAYS | File::FLAG_READ | File::FLAG_WRITE);
47 EXPECT_TRUE(file.IsValid());
48
49 scoped_ptr<uint8[]> test_data(CreateTestBuffer(size, 0));
50 size_t bytes_written =
51 file.Write(0, reinterpret_cast<char*>(test_data.get()), size);
52 EXPECT_EQ(size, bytes_written);
53 file.Close();
54 }
55
56 const base::FilePath temp_file_path() const { return temp_file_path_; }
57
58 private:
59 base::FilePath temp_file_path_;
60 };
61
62 TEST_F(MemoryMappedFileTest, MapWholeFileByPath) {
63 const size_t kFileSize = 68 * 1024;
64 CreateTemporaryTestFile(kFileSize);
65 MemoryMappedFile map;
66 map.Initialize(temp_file_path());
67 ASSERT_EQ(kFileSize, map.length());
68 ASSERT_TRUE(map.data() != NULL);
69 EXPECT_TRUE(map.IsValid());
70 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
71 }
72
73 TEST_F(MemoryMappedFileTest, MapWholeFileByFD) {
74 const size_t kFileSize = 68 * 1024;
75 CreateTemporaryTestFile(kFileSize);
76 MemoryMappedFile map;
77 map.Initialize(File(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ));
78 ASSERT_EQ(kFileSize, map.length());
79 ASSERT_TRUE(map.data() != NULL);
80 EXPECT_TRUE(map.IsValid());
81 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
82 }
83
84 TEST_F(MemoryMappedFileTest, MapSmallFile) {
85 const size_t kFileSize = 127;
86 CreateTemporaryTestFile(kFileSize);
87 MemoryMappedFile map;
88 map.Initialize(temp_file_path());
89 ASSERT_EQ(kFileSize, map.length());
90 ASSERT_TRUE(map.data() != NULL);
91 EXPECT_TRUE(map.IsValid());
92 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
93 }
94
95 TEST_F(MemoryMappedFileTest, MapWholeFileUsingRegion) {
96 const size_t kFileSize = 157 * 1024;
97 CreateTemporaryTestFile(kFileSize);
98 MemoryMappedFile map;
99
100 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
101 map.Initialize(file.Pass(), File::Region::WholeFile());
102 ASSERT_EQ(kFileSize, map.length());
103 ASSERT_TRUE(map.data() != NULL);
104 EXPECT_TRUE(map.IsValid());
105 ASSERT_TRUE(CheckBufferContents(map.data(), kFileSize, 0));
106 }
107
108 TEST_F(MemoryMappedFileTest, MapPartialRegionAtBeginning) {
109 const size_t kFileSize = 157 * 1024;
110 const size_t kPartialSize = 6 * 1024;
111 CreateTemporaryTestFile(kFileSize);
112 MemoryMappedFile map;
113
114 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
115 map.Initialize(file.Pass(), File::Region(0, kPartialSize));
116 ASSERT_EQ(kPartialSize, map.length());
117 ASSERT_TRUE(map.data() != NULL);
118 EXPECT_TRUE(map.IsValid());
119 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, 0));
120 }
121
122 TEST_F(MemoryMappedFileTest, MapPartialRegionAtEnd) {
123 const size_t kFileSize = 157 * 1024;
124 const size_t kPartialSize = 6 * 1024;
125 const size_t kOffset = kFileSize - kPartialSize;
126 CreateTemporaryTestFile(kFileSize);
127 MemoryMappedFile map;
128
129 File file(temp_file_path(), File::FLAG_OPEN | File::FLAG_READ);
130 map.Initialize(file.Pass(), File::Region(kOffset, kPartialSize));
131 ASSERT_EQ(kPartialSize, map.length());
132 ASSERT_TRUE(map.data() != NULL);
133 EXPECT_TRUE(map.IsValid());
134 ASSERT_TRUE(CheckBufferContents(map.data(), kPartialSize, kOffset));
135 }
136
137 } // namespace
138
139 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698