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

Side by Side Diff: webkit/fileapi/quota_file_util_unittest.cc

Issue 7608011: Simplify directory path accounting. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rolled in CR feedback. Created 9 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « webkit/fileapi/quota_file_util.cc ('k') | webkit/fileapi/sandbox_mount_point_provider.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "webkit/fileapi/quota_file_util.h" 5 #include "webkit/fileapi/quota_file_util.h"
6 6
7 #include "base/file_path.h" 7 #include "base/file_path.h"
8 #include "base/memory/scoped_callback_factory.h" 8 #include "base/memory/scoped_callback_factory.h"
9 #include "base/message_loop_proxy.h" 9 #include "base/message_loop_proxy.h"
10 #include "base/platform_file.h" 10 #include "base/platform_file.h"
(...skipping 24 matching lines...) Expand all
35 quota_test_helper_.file_system_context(), NULL); 35 quota_test_helper_.file_system_context(), NULL);
36 base_dir_ = obfuscated_test_helper_.GetOriginRootPath(); 36 base_dir_ = obfuscated_test_helper_.GetOriginRootPath();
37 } 37 }
38 38
39 void TearDown() { 39 void TearDown() {
40 quota_test_helper_.TearDown(); 40 quota_test_helper_.TearDown();
41 obfuscated_test_helper_.TearDown(); 41 obfuscated_test_helper_.TearDown();
42 } 42 }
43 43
44 protected: 44 protected:
45 FileSystemOperationContext* NewContext( 45 FileSystemOperationContext* NewContext() {
46 int64 allowed_bytes_growth, 46 return quota_test_helper_.NewOperationContext();
47 const FilePath& src_virtual_path,
48 const FilePath& dest_virtual_path) {
49 FileSystemOperationContext* context =
50 quota_test_helper_.NewOperationContext();
51 context->set_allowed_bytes_growth(allowed_bytes_growth);
52 context->set_src_virtual_path(src_virtual_path);
53 context->set_dest_virtual_path(dest_virtual_path);
54 return context;
55 } 47 }
56 48
57 FilePath Path(const std::string& file_name) { 49 FilePath Path(const std::string& file_name) {
58 return base_dir_.AppendASCII(file_name); 50 return base_dir_.AppendASCII(file_name);
59 } 51 }
60 52
61 int64 ComputeFilePathCost(const char* file_name) { 53 base::PlatformFileError CreateFile(const char* file_name,
62 return quota_file_util_->ComputeFilePathCost( 54 base::PlatformFile* file_handle, bool* created) {
63 FilePath().AppendASCII(file_name)); 55 int file_flags = base::PLATFORM_FILE_CREATE |
56 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC;
57
58 scoped_ptr<FileSystemOperationContext> context(NewContext());
59 return quota_file_util()->CreateOrOpen(
60 context.get(), Path(file_name), file_flags, file_handle, created);
64 } 61 }
65 62
66 base::PlatformFileError EnsureFileExists( 63 base::PlatformFileError EnsureFileExists(
67 const char* file_name, bool* created) { 64 const char* file_name, bool* created) {
68 int64 file_path_cost = ComputeFilePathCost(file_name); 65 scoped_ptr<FileSystemOperationContext> context(NewContext());
69 scoped_ptr<FileSystemOperationContext> context(NewContext(
70 file_path_cost, Path(file_name), FilePath()));
71 return quota_file_util_->EnsureFileExists( 66 return quota_file_util_->EnsureFileExists(
72 context.get(), Path(file_name), created); 67 context.get(), Path(file_name), created);
73 } 68 }
74 69
75 base::PlatformFileError Truncate(
76 const char* file_name, int64 size, int64 quota) {
77 scoped_ptr<FileSystemOperationContext> context(NewContext(
78 quota, Path(file_name), FilePath()));
79 return quota_file_util_->Truncate(
80 context.get(), Path(file_name), size);
81 }
82
83 void CheckUsage(int64 estimated_content, int64 estimated_path) {
84 ASSERT_EQ(estimated_content + estimated_path,
85 quota_test_helper().GetCachedOriginUsage());
86 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage() +
87 estimated_path,
88 quota_test_helper().GetCachedOriginUsage());
89 }
90
91 QuotaFileUtil* quota_file_util() const { 70 QuotaFileUtil* quota_file_util() const {
92 return quota_file_util_.get(); 71 return quota_file_util_.get();
93 } 72 }
94 73
95 const FileSystemTestOriginHelper& quota_test_helper() const { 74 const FileSystemTestOriginHelper& quota_test_helper() const {
96 return quota_test_helper_; 75 return quota_test_helper_;
97 } 76 }
98 77
99 private: 78 private:
100 ScopedTempDir data_dir_; 79 ScopedTempDir data_dir_;
101 FilePath base_dir_; 80 FilePath base_dir_;
102 FileSystemTestOriginHelper obfuscated_test_helper_; 81 FileSystemTestOriginHelper obfuscated_test_helper_;
103 FileSystemTestOriginHelper quota_test_helper_; 82 FileSystemTestOriginHelper quota_test_helper_;
104 scoped_ptr<QuotaFileUtil> quota_file_util_; 83 scoped_ptr<QuotaFileUtil> quota_file_util_;
105 base::ScopedCallbackFactory<QuotaFileUtilTest> callback_factory_; 84 base::ScopedCallbackFactory<QuotaFileUtilTest> callback_factory_;
106 85
107 DISALLOW_COPY_AND_ASSIGN(QuotaFileUtilTest); 86 DISALLOW_COPY_AND_ASSIGN(QuotaFileUtilTest);
108 }; 87 };
109 88
110 TEST_F(QuotaFileUtilTest, CreateAndClose) { 89 TEST_F(QuotaFileUtilTest, CreateAndClose) {
111 const char *file_name = "test_file"; 90 const char *file_name = "test_file";
112 int64 file_path_cost = ComputeFilePathCost(file_name);
113
114 int file_flags = base::PLATFORM_FILE_CREATE |
115 base::PLATFORM_FILE_WRITE | base::PLATFORM_FILE_ASYNC;
116 base::PlatformFile file_handle; 91 base::PlatformFile file_handle;
117 bool created; 92 bool created;
118 scoped_ptr<FileSystemOperationContext> context; 93 ASSERT_EQ(base::PLATFORM_FILE_OK,
119 94 CreateFile(file_name, &file_handle, &created));
120 created = false;
121 context.reset(NewContext(file_path_cost - 1, Path(file_name), FilePath()));
122 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, quota_file_util()->CreateOrOpen(
123 context.get(), Path(file_name), file_flags, &file_handle, &created));
124 ASSERT_FALSE(created);
125
126 created = false;
127 context.reset(NewContext(file_path_cost, Path(file_name), FilePath()));
128 ASSERT_EQ(base::PLATFORM_FILE_OK, quota_file_util()->CreateOrOpen(
129 context.get(), Path(file_name), file_flags, &file_handle, &created));
130 ASSERT_TRUE(created); 95 ASSERT_TRUE(created);
131 96
132 context.reset(NewContext(0, FilePath(), FilePath())); 97 scoped_ptr<FileSystemOperationContext> context(NewContext());
133 EXPECT_EQ(base::PLATFORM_FILE_OK, quota_file_util()->Close( 98 EXPECT_EQ(base::PLATFORM_FILE_OK,
134 context.get(), file_handle)); 99 quota_file_util()->Close(context.get(), file_handle));
135 }
136
137 TEST_F(QuotaFileUtilTest, EnsureFileExists) {
138 const char *file_name = "foobar";
139
140 bool created;
141 scoped_ptr<FileSystemOperationContext> context;
142
143 created = false;
144 context.reset(NewContext(
145 ComputeFilePathCost(file_name) - 1, Path(file_name), FilePath()));
146 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
147 quota_file_util()->EnsureFileExists(
148 context.get(), Path(file_name), &created));
149 ASSERT_FALSE(created);
150
151 created = false;
152 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created));
153 ASSERT_TRUE(created);
154
155 created = false;
156 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created));
157 ASSERT_FALSE(created);
158 } 100 }
159 101
160 TEST_F(QuotaFileUtilTest, Truncate) { 102 TEST_F(QuotaFileUtilTest, Truncate) {
161 const char *file_name = "truncated"; 103 const char *file_name = "truncated";
162 bool created; 104 bool created;
163 105
164 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created)); 106 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file_name, &created));
165 ASSERT_TRUE(created); 107 ASSERT_TRUE(created);
166 108
167 scoped_ptr<FileSystemOperationContext> context; 109 scoped_ptr<FileSystemOperationContext> truncate_context;
168 110
169 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(file_name, 1020, 1020)); 111 truncate_context.reset(NewContext());
170 CheckUsage(1020, ComputeFilePathCost(file_name)); 112 truncate_context->set_allowed_bytes_growth(1020);
171 113 ASSERT_EQ(base::PLATFORM_FILE_OK,
172 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(file_name, 0, 0)); 114 quota_file_util()->Truncate(truncate_context.get(),
173 CheckUsage(0, ComputeFilePathCost(file_name)); 115 Path(file_name),
174 116 1020));
117 ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage());
118 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
119 quota_test_helper().GetCachedOriginUsage());
120
121 truncate_context.reset(NewContext());
122 truncate_context->set_allowed_bytes_growth(0);
123 ASSERT_EQ(base::PLATFORM_FILE_OK,
124 quota_file_util()->Truncate(truncate_context.get(),
125 Path(file_name),
126 0));
127 ASSERT_EQ(0, quota_test_helper().GetCachedOriginUsage());
128 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
129 quota_test_helper().GetCachedOriginUsage());
130
131 truncate_context.reset(NewContext());
132 truncate_context->set_allowed_bytes_growth(1020);
133 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
134 quota_file_util()->Truncate(truncate_context.get(),
135 Path(file_name),
136 1021));
137 ASSERT_EQ(0, quota_test_helper().GetCachedOriginUsage());
138 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
139 quota_test_helper().GetCachedOriginUsage());
140 }
141
142 TEST_F(QuotaFileUtilTest, CopyFile) {
143 const char *from_file = "fromfile";
144 const char *obstacle_file = "obstaclefile";
145 const char *to_file1 = "tofile1";
146 const char *to_file2 = "tofile2";
147 bool created;
148 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created));
149 ASSERT_TRUE(created);
150 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(obstacle_file, &created));
151 ASSERT_TRUE(created);
152 scoped_ptr<FileSystemOperationContext> context;
153
154 context.reset(NewContext());
155 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
156 ASSERT_EQ(base::PLATFORM_FILE_OK,
157 quota_file_util()->Truncate(context.get(),
158 Path(from_file),
159 1020));
160 ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage());
161 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
162 quota_test_helper().GetCachedOriginUsage());
163
164 context.reset(NewContext());
165 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
166 ASSERT_EQ(base::PLATFORM_FILE_OK,
167 quota_file_util()->Truncate(context.get(),
168 Path(obstacle_file),
169 1));
170 ASSERT_EQ(1021, quota_test_helper().GetCachedOriginUsage());
171 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
172 quota_test_helper().GetCachedOriginUsage());
173
174 context.reset(NewContext());
175 context->set_allowed_bytes_growth(1020);
176 ASSERT_EQ(base::PLATFORM_FILE_OK,
177 quota_file_util()->Copy(context.get(),
178 Path(from_file),
179 Path(to_file1)));
180 ASSERT_EQ(2041, quota_test_helper().GetCachedOriginUsage());
181 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
182 quota_test_helper().GetCachedOriginUsage());
183
184 context.reset(NewContext());
185 context->set_allowed_bytes_growth(1019);
175 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, 186 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
176 Truncate(file_name, 1021, 1020));
177 CheckUsage(0, ComputeFilePathCost(file_name));
178 }
179
180 TEST_F(QuotaFileUtilTest, CopyFile) {
181 int64 file_path_cost = 0;
182 const char *from_file = "fromfile";
183 const char *prior_file = "obstaclefile";
184 const char *to_file1 = "tofile1";
185 const char *to_file2 = "tomuchlongerfile2";
186 bool created;
187 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created));
188 ASSERT_TRUE(created);
189 file_path_cost += ComputeFilePathCost(from_file);
190 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(prior_file, &created));
191 ASSERT_TRUE(created);
192 file_path_cost += ComputeFilePathCost(prior_file);
193 scoped_ptr<FileSystemOperationContext> context;
194
195 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(from_file, 1020, 1020));
196 CheckUsage(1020, file_path_cost);
197
198 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(prior_file, 1, 1));
199 CheckUsage(1021, file_path_cost);
200
201 context.reset(NewContext(1020 + ComputeFilePathCost(to_file1),
202 Path(from_file), Path(to_file1)));
203 ASSERT_EQ(base::PLATFORM_FILE_OK, quota_file_util()->Copy(
204 context.get(), Path(from_file), Path(to_file1)));
205 file_path_cost += ComputeFilePathCost(to_file1);
206 CheckUsage(2041, file_path_cost);
207
208 context.reset(NewContext(1020 + ComputeFilePathCost(to_file2) - 1,
209 Path(from_file), Path(to_file2)));
210 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
211 quota_file_util()->Copy(context.get(), 187 quota_file_util()->Copy(context.get(),
212 Path(from_file), 188 Path(from_file),
213 Path(to_file2))); 189 Path(to_file2)));
214 CheckUsage(2041, file_path_cost); 190 ASSERT_EQ(2041, quota_test_helper().GetCachedOriginUsage());
215 191 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
216 context.reset(NewContext(1019, Path(from_file), Path(prior_file))); 192 quota_test_helper().GetCachedOriginUsage());
217 ASSERT_EQ(base::PLATFORM_FILE_OK, 193
218 quota_file_util()->Copy(context.get(), 194 context.reset(NewContext());
219 Path(from_file), 195 context->set_allowed_bytes_growth(1019);
220 Path(prior_file))); 196 ASSERT_EQ(base::PLATFORM_FILE_OK,
221 CheckUsage(3060, file_path_cost); 197 quota_file_util()->Copy(context.get(),
198 Path(from_file),
199 Path(obstacle_file)));
200 ASSERT_EQ(3060, quota_test_helper().GetCachedOriginUsage());
201 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
202 quota_test_helper().GetCachedOriginUsage());
222 } 203 }
223 204
224 TEST_F(QuotaFileUtilTest, CopyDirectory) { 205 TEST_F(QuotaFileUtilTest, CopyDirectory) {
225 int64 file_path_cost = 0;
226 const char *from_dir = "fromdir"; 206 const char *from_dir = "fromdir";
227 const char *from_file1 = "fromdir/fromfile1"; 207 const char *from_file1 = "fromdir/fromfile1";
228 const char *from_file2 = "fromdir/fromlongerfile2"; 208 const char *from_file2 = "fromdir/fromfile2";
229 const char *to_dir1 = "todir1"; 209 const char *to_dir1 = "todir1";
230 const char *to_dir2 = "tolongerdir2"; 210 const char *to_dir2 = "todir2";
231 bool created; 211 bool created;
232 scoped_ptr<FileSystemOperationContext> context; 212 scoped_ptr<FileSystemOperationContext> context;
233 213
234 context.reset(NewContext(ComputeFilePathCost(from_dir), 214 context.reset(NewContext());
235 Path(from_dir), FilePath()));
236 ASSERT_EQ(base::PLATFORM_FILE_OK, 215 ASSERT_EQ(base::PLATFORM_FILE_OK,
237 quota_file_util()->CreateDirectory(context.get(), 216 quota_file_util()->CreateDirectory(context.get(),
238 Path(from_dir), 217 Path(from_dir),
239 false, false)); 218 false, false));
240 file_path_cost += ComputeFilePathCost(from_dir);
241
242 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file1, &created)); 219 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file1, &created));
243 ASSERT_TRUE(created); 220 ASSERT_TRUE(created);
244 file_path_cost += ComputeFilePathCost(from_file1);
245 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file2, &created)); 221 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file2, &created));
246 ASSERT_TRUE(created); 222 ASSERT_TRUE(created);
247 file_path_cost += ComputeFilePathCost(from_file2); 223
248 224 context.reset(NewContext());
249 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(from_file1, 520, 520)); 225 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
250 CheckUsage(520, file_path_cost); 226 ASSERT_EQ(base::PLATFORM_FILE_OK,
251 227 quota_file_util()->Truncate(context.get(),
252 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(from_file2, 500, 500)); 228 Path(from_file1),
253 CheckUsage(1020, file_path_cost); 229 520));
254 230 ASSERT_EQ(520, quota_test_helper().GetCachedOriginUsage());
255 context.reset(NewContext(1020 + 231 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
256 ComputeFilePathCost(to_dir1) + 232 quota_test_helper().GetCachedOriginUsage());
257 ComputeFilePathCost(from_file1) + 233
258 ComputeFilePathCost(from_file2), 234 context.reset(NewContext());
259 Path(from_dir), Path(to_dir1))); 235 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
236 ASSERT_EQ(base::PLATFORM_FILE_OK,
237 quota_file_util()->Truncate(context.get(),
238 Path(from_file2),
239 500));
240 ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage());
241 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
242 quota_test_helper().GetCachedOriginUsage());
243
244 context.reset(NewContext());
245 context->set_allowed_bytes_growth(1020);
260 ASSERT_EQ(base::PLATFORM_FILE_OK, 246 ASSERT_EQ(base::PLATFORM_FILE_OK,
261 quota_file_util()->Copy(context.get(), 247 quota_file_util()->Copy(context.get(),
262 Path(from_dir), 248 Path(from_dir),
263 Path(to_dir1))); 249 Path(to_dir1)));
264 file_path_cost += ComputeFilePathCost(to_dir1) + 250 ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage());
265 ComputeFilePathCost(from_file1) + 251 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
266 ComputeFilePathCost(from_file2); 252 quota_test_helper().GetCachedOriginUsage());
267 CheckUsage(2040, file_path_cost); 253
268 254 context.reset(NewContext());
269 context.reset(NewContext(1020 + 255 context->set_allowed_bytes_growth(1019);
270 ComputeFilePathCost(to_dir2) +
271 ComputeFilePathCost(from_file1) +
272 ComputeFilePathCost(from_file2) - 1,
273 Path(from_dir), Path(to_dir2)));
274 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, 256 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE,
275 quota_file_util()->Copy(context.get(), 257 quota_file_util()->Copy(context.get(),
276 Path(from_dir), 258 Path(from_dir),
277 Path(to_dir2))); 259 Path(to_dir2)));
278 int64 file_path_cost1 = file_path_cost + 260 ASSERT_TRUE(2540 == quota_test_helper().GetCachedOriginUsage() ||
279 ComputeFilePathCost(to_dir2) + ComputeFilePathCost(from_file1); 261 2560 == quota_test_helper().GetCachedOriginUsage());
280 int64 file_path_cost2 = file_path_cost + 262 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
281 ComputeFilePathCost(to_dir2) + ComputeFilePathCost(from_file2); 263 quota_test_helper().GetCachedOriginUsage());
282 ASSERT_TRUE((2560 + file_path_cost1) ==
283 quota_test_helper().GetCachedOriginUsage() ||
284 (2540 + file_path_cost2) ==
285 quota_test_helper().GetCachedOriginUsage());
286 ASSERT_TRUE((quota_test_helper().ComputeCurrentOriginUsage() + file_path_cost1
287 == quota_test_helper().GetCachedOriginUsage()) ||
288 (quota_test_helper().ComputeCurrentOriginUsage() + file_path_cost2
289 == quota_test_helper().GetCachedOriginUsage()));
290 } 264 }
291 265
292 TEST_F(QuotaFileUtilTest, MoveFile) { 266 TEST_F(QuotaFileUtilTest, MoveFile) {
293 int64 file_path_cost = 0;
294 const char *from_file = "fromfile"; 267 const char *from_file = "fromfile";
295 const char *prior_file = "obstaclelongnamefile"; 268 const char *obstacle_file = "obstaclefile";
296 const char *to_file = "tofile"; 269 const char *to_file = "tofile";
297 bool created; 270 bool created;
298 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); 271 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created));
299 ASSERT_TRUE(created); 272 ASSERT_TRUE(created);
300 file_path_cost += ComputeFilePathCost(from_file); 273 scoped_ptr<FileSystemOperationContext> context;
301 scoped_ptr<FileSystemOperationContext> context; 274
302 275 context.reset(NewContext());
303 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(from_file, 1020, 1020)); 276 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
304 CheckUsage(1020, file_path_cost); 277 ASSERT_EQ(base::PLATFORM_FILE_OK,
305 278 quota_file_util()->Truncate(context.get(),
306 context.reset(NewContext(0, Path(from_file), Path(to_file))); 279 Path(from_file),
280 1020));
281 ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage());
282 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
283 quota_test_helper().GetCachedOriginUsage());
284
285 context.reset(NewContext());
286 context->set_allowed_bytes_growth(0);
307 ASSERT_EQ(base::PLATFORM_FILE_OK, 287 ASSERT_EQ(base::PLATFORM_FILE_OK,
308 quota_file_util()->Move(context.get(), 288 quota_file_util()->Move(context.get(),
309 Path(from_file), 289 Path(from_file),
310 Path(to_file))); 290 Path(to_file)));
311 file_path_cost -= ComputeFilePathCost(from_file); 291 ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage());
312 file_path_cost += ComputeFilePathCost(to_file); 292 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
313 CheckUsage(1020, file_path_cost); 293 quota_test_helper().GetCachedOriginUsage());
314 294
315 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); 295 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created));
316 ASSERT_TRUE(created); 296 ASSERT_TRUE(created);
317 file_path_cost += ComputeFilePathCost(from_file); 297 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(obstacle_file, &created));
318 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(prior_file, &created)); 298 ASSERT_TRUE(created);
319 ASSERT_TRUE(created); 299
320 file_path_cost += ComputeFilePathCost(prior_file); 300 context.reset(NewContext());
321 301 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
322 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(from_file, 1020, 1020)); 302 ASSERT_EQ(base::PLATFORM_FILE_OK,
323 CheckUsage(2040, file_path_cost); 303 quota_file_util()->Truncate(context.get(),
324 304 Path(from_file),
325 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(prior_file, 1, 1)); 305 1020));
326 CheckUsage(2041, file_path_cost); 306 ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage());
327 307 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
328 context.reset(NewContext(ComputeFilePathCost(prior_file) - 308 quota_test_helper().GetCachedOriginUsage());
329 ComputeFilePathCost(from_file) - 1 - 1, 309
330 Path(from_file), Path(prior_file))); 310 context.reset(NewContext());
331 ASSERT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, 311 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
332 quota_file_util()->Move(context.get(), 312 ASSERT_EQ(base::PLATFORM_FILE_OK,
333 Path(from_file), 313 quota_file_util()->Truncate(context.get(),
334 Path(prior_file))); 314 Path(obstacle_file),
335 CheckUsage(2041, file_path_cost); 315 1));
336 316 ASSERT_EQ(2041, quota_test_helper().GetCachedOriginUsage());
337 context.reset(NewContext(ComputeFilePathCost(prior_file) - 317 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
338 ComputeFilePathCost(from_file) - 1, 318 quota_test_helper().GetCachedOriginUsage());
339 Path(from_file), Path(prior_file))); 319
340 ASSERT_EQ(base::PLATFORM_FILE_OK, 320 context.reset(NewContext());
341 quota_file_util()->Move(context.get(), 321 context->set_allowed_bytes_growth(0);
342 Path(from_file), 322 ASSERT_EQ(base::PLATFORM_FILE_OK,
343 Path(prior_file))); 323 quota_file_util()->Move(context.get(),
344 file_path_cost -= ComputeFilePathCost(from_file); 324 Path(from_file),
345 file_path_cost += ComputeFilePathCost(prior_file); 325 Path(obstacle_file)));
346 CheckUsage(2040, file_path_cost); 326 ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage());
327 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
328 quota_test_helper().GetCachedOriginUsage());
347 } 329 }
348 330
349 TEST_F(QuotaFileUtilTest, MoveDirectory) { 331 TEST_F(QuotaFileUtilTest, MoveDirectory) {
350 int64 file_path_cost = 0;
351 const char *from_dir = "fromdir"; 332 const char *from_dir = "fromdir";
352 const char *from_file = "fromdir/fromfile"; 333 const char *from_file = "fromdir/fromfile";
353 const char *to_dir1 = "todir1"; 334 const char *to_dir1 = "todir1";
354 const char *to_dir2 = "tolongnamedir2"; 335 const char *to_dir2 = "todir2";
355 bool created; 336 bool created;
356 scoped_ptr<FileSystemOperationContext> context; 337 scoped_ptr<FileSystemOperationContext> context;
357 338
358 context.reset(NewContext(QuotaFileUtil::kNoLimit, 339 context.reset(NewContext());
359 Path(from_dir), FilePath()));
360 ASSERT_EQ(base::PLATFORM_FILE_OK, 340 ASSERT_EQ(base::PLATFORM_FILE_OK,
361 quota_file_util()->CreateDirectory(context.get(), 341 quota_file_util()->CreateDirectory(context.get(),
362 Path(from_dir), 342 Path(from_dir),
363 false, false)); 343 false, false));
364 file_path_cost += ComputeFilePathCost(from_dir); 344 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created));
365 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); 345 ASSERT_TRUE(created);
366 ASSERT_TRUE(created); 346
367 file_path_cost += ComputeFilePathCost(from_file); 347 context.reset(NewContext());
368 348 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
369 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(from_file, 1020, 1020)); 349 ASSERT_EQ(base::PLATFORM_FILE_OK,
370 CheckUsage(1020, file_path_cost); 350 quota_file_util()->Truncate(context.get(),
371 351 Path(from_file),
372 context.reset(NewContext(1020, Path(from_dir), Path(to_dir1))); 352 1020));
353 ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage());
354 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
355 quota_test_helper().GetCachedOriginUsage());
356
357 context.reset(NewContext());
358 context->set_allowed_bytes_growth(1020);
373 ASSERT_EQ(base::PLATFORM_FILE_OK, 359 ASSERT_EQ(base::PLATFORM_FILE_OK,
374 quota_file_util()->Move(context.get(), 360 quota_file_util()->Move(context.get(),
375 Path(from_dir), 361 Path(from_dir),
376 Path(to_dir1))); 362 Path(to_dir1)));
377 file_path_cost -= ComputeFilePathCost(from_dir); 363 ASSERT_EQ(1020, quota_test_helper().GetCachedOriginUsage());
378 file_path_cost += ComputeFilePathCost(to_dir1); 364 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
379 CheckUsage(1020, file_path_cost); 365 quota_test_helper().GetCachedOriginUsage());
380 366
381 context.reset(NewContext(QuotaFileUtil::kNoLimit, 367 context.reset(NewContext());
382 Path(from_dir), FilePath()));
383 ASSERT_EQ(base::PLATFORM_FILE_OK, 368 ASSERT_EQ(base::PLATFORM_FILE_OK,
384 quota_file_util()->CreateDirectory(context.get(), 369 quota_file_util()->CreateDirectory(context.get(),
385 Path(from_dir), 370 Path(from_dir),
386 false, false)); 371 false, false));
387 file_path_cost += ComputeFilePathCost(from_dir); 372 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created));
388 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(from_file, &created)); 373 ASSERT_TRUE(created);
389 ASSERT_TRUE(created); 374
390 file_path_cost += ComputeFilePathCost(from_file); 375 context.reset(NewContext());
391 376 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
392 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(from_file, 1020, 1020)); 377 ASSERT_EQ(base::PLATFORM_FILE_OK,
393 CheckUsage(2040, file_path_cost); 378 quota_file_util()->Truncate(context.get(),
394 379 Path(from_file),
395 context.reset(NewContext(1019, Path(from_dir), Path(to_dir2))); 380 1020));
381 ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage());
382 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
383 quota_test_helper().GetCachedOriginUsage());
384
385 context.reset(NewContext());
386 context->set_allowed_bytes_growth(1019);
396 EXPECT_EQ(base::PLATFORM_FILE_OK, 387 EXPECT_EQ(base::PLATFORM_FILE_OK,
397 quota_file_util()->Move(context.get(), 388 quota_file_util()->Move(context.get(),
398 Path(from_dir), 389 Path(from_dir),
399 Path(to_dir2))); 390 Path(to_dir2)));
400 file_path_cost -= ComputeFilePathCost(from_dir); 391 ASSERT_EQ(2040, quota_test_helper().GetCachedOriginUsage());
401 file_path_cost += ComputeFilePathCost(to_dir2); 392 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
402 CheckUsage(2040, file_path_cost); 393 quota_test_helper().GetCachedOriginUsage());
403 } 394 }
404 395
405 TEST_F(QuotaFileUtilTest, Remove) { 396 TEST_F(QuotaFileUtilTest, Remove) {
406 int64 file_path_cost = 0;
407 const char *dir = "dir"; 397 const char *dir = "dir";
408 const char *file = "file"; 398 const char *file = "file";
409 const char *dfile1 = "dir/dfile1"; 399 const char *dfile1 = "dir/dfile1";
410 const char *dfile2 = "dir/dfile2"; 400 const char *dfile2 = "dir/dfile2";
411 bool created; 401 bool created;
412 scoped_ptr<FileSystemOperationContext> context; 402 scoped_ptr<FileSystemOperationContext> context;
413 403
414 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file, &created)); 404 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(file, &created));
415 ASSERT_TRUE(created); 405 ASSERT_TRUE(created);
416 file_path_cost += ComputeFilePathCost(file); 406 context.reset(NewContext());
417 context.reset(NewContext(QuotaFileUtil::kNoLimit, Path(dir), FilePath()));
418 ASSERT_EQ(base::PLATFORM_FILE_OK, 407 ASSERT_EQ(base::PLATFORM_FILE_OK,
419 quota_file_util()->CreateDirectory(context.get(), 408 quota_file_util()->CreateDirectory(context.get(),
420 Path(dir), 409 Path(dir),
421 false, false)); 410 false, false));
422 file_path_cost += ComputeFilePathCost(dir);
423 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(dfile1, &created)); 411 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(dfile1, &created));
424 ASSERT_TRUE(created); 412 ASSERT_TRUE(created);
425 file_path_cost += ComputeFilePathCost(dfile1);
426 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(dfile2, &created)); 413 ASSERT_EQ(base::PLATFORM_FILE_OK, EnsureFileExists(dfile2, &created));
427 ASSERT_TRUE(created); 414 ASSERT_TRUE(created);
428 file_path_cost += ComputeFilePathCost(dfile2);
429 415
430 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(file, 340, 340)); 416 context.reset(NewContext());
431 CheckUsage(340, file_path_cost); 417 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
418 ASSERT_EQ(base::PLATFORM_FILE_OK,
419 quota_file_util()->Truncate(context.get(),
420 Path(file),
421 340));
422 ASSERT_EQ(340, quota_test_helper().GetCachedOriginUsage());
423 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
424 quota_test_helper().GetCachedOriginUsage());
432 425
433 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(dfile1, 1020, 1020)); 426 context.reset(NewContext());
434 CheckUsage(1360, file_path_cost); 427 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
428 ASSERT_EQ(base::PLATFORM_FILE_OK,
429 quota_file_util()->Truncate(context.get(),
430 Path(dfile1),
431 1020));
432 ASSERT_EQ(1360, quota_test_helper().GetCachedOriginUsage());
433 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
434 quota_test_helper().GetCachedOriginUsage());
435 435
436 ASSERT_EQ(base::PLATFORM_FILE_OK, Truncate(dfile2, 120, 120)); 436 context.reset(NewContext());
437 CheckUsage(1480, file_path_cost); 437 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
438 ASSERT_EQ(base::PLATFORM_FILE_OK,
439 quota_file_util()->Truncate(context.get(),
440 Path(dfile2),
441 120));
442 ASSERT_EQ(1480, quota_test_helper().GetCachedOriginUsage());
443 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
444 quota_test_helper().GetCachedOriginUsage());
438 445
439 context.reset(NewContext(QuotaFileUtil::kNoLimit, Path(file), FilePath())); 446 context.reset(NewContext());
447 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
440 ASSERT_EQ(base::PLATFORM_FILE_OK, 448 ASSERT_EQ(base::PLATFORM_FILE_OK,
441 quota_file_util()->Delete(context.get(), 449 quota_file_util()->Delete(context.get(),
442 Path(file), 450 Path(file),
443 false)); 451 false));
444 file_path_cost -= ComputeFilePathCost(file); 452 ASSERT_EQ(1140, quota_test_helper().GetCachedOriginUsage());
445 CheckUsage(1140, file_path_cost); 453 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
454 quota_test_helper().GetCachedOriginUsage());
446 455
447 context.reset(NewContext(QuotaFileUtil::kNoLimit, Path(dir), FilePath())); 456 context.reset(NewContext());
457 context->set_allowed_bytes_growth(QuotaFileUtil::kNoLimit);
448 ASSERT_EQ(base::PLATFORM_FILE_OK, 458 ASSERT_EQ(base::PLATFORM_FILE_OK,
449 quota_file_util()->Delete(context.get(), 459 quota_file_util()->Delete(context.get(),
450 Path(dir), 460 Path(dir),
451 true)); 461 true));
452 file_path_cost = 0; 462 ASSERT_EQ(0, quota_test_helper().GetCachedOriginUsage());
453 CheckUsage(0, 0); 463 ASSERT_EQ(quota_test_helper().ComputeCurrentOriginUsage(),
464 quota_test_helper().GetCachedOriginUsage());
454 } 465 }
455 466
456 } // namespace fileapi 467 } // namespace fileapi
OLDNEW
« no previous file with comments | « webkit/fileapi/quota_file_util.cc ('k') | webkit/fileapi/sandbox_mount_point_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698