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

Side by Side Diff: chrome/browser/chromeos/drive/file_cache_unittest.cc

Issue 1296483003: Move chrome/browser/chromeos/drive/resource* (+deps) into components/drive. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebasing... Created 5 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
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 "chrome/browser/chromeos/drive/file_cache.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "base/callback_helpers.h"
11 #include "base/files/file_enumerator.h"
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/md5.h"
15 #include "base/path_service.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h"
19 #include "chrome/browser/chromeos/drive/file_system_core_util.h"
20 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h"
21 #include "components/drive/drive.pb.h"
22 #include "components/drive/drive_test_util.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "google_apis/drive/test_util.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 namespace drive {
28 namespace internal {
29 namespace {
30
31 const char kCacheFileDirectory[] = "files";
32
33 } // namespace
34
35 // Tests FileCache methods working with the blocking task runner.
36 class FileCacheTest : public testing::Test {
37 protected:
38 void SetUp() override {
39 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
40 const base::FilePath metadata_dir = temp_dir_.path().AppendASCII("meta");
41 cache_files_dir_ = temp_dir_.path().AppendASCII(kCacheFileDirectory);
42
43 ASSERT_TRUE(base::CreateDirectory(metadata_dir));
44 ASSERT_TRUE(base::CreateDirectory(cache_files_dir_));
45
46 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter);
47
48 metadata_storage_.reset(new ResourceMetadataStorage(
49 metadata_dir,
50 base::ThreadTaskRunnerHandle::Get().get()));
51 ASSERT_TRUE(metadata_storage_->Initialize());
52
53 cache_.reset(new FileCache(
54 metadata_storage_.get(),
55 cache_files_dir_,
56 base::ThreadTaskRunnerHandle::Get().get(),
57 fake_free_disk_space_getter_.get()));
58 ASSERT_TRUE(cache_->Initialize());
59 }
60
61 static bool RenameCacheFilesToNewFormat(FileCache* cache) {
62 return cache->RenameCacheFilesToNewFormat();
63 }
64
65 content::TestBrowserThreadBundle thread_bundle_;
66 base::ScopedTempDir temp_dir_;
67 base::FilePath cache_files_dir_;
68
69 scoped_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests>
70 metadata_storage_;
71 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_;
72 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_;
73 };
74
75 TEST_F(FileCacheTest, RecoverFilesFromCacheDirectory) {
76 base::FilePath dir_source_root;
77 EXPECT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &dir_source_root));
78 const base::FilePath src_path =
79 dir_source_root.AppendASCII("chrome/test/data/chromeos/drive/image.png");
80
81 // Store files. This file should not be moved.
82 ResourceEntry entry;
83 entry.set_local_id("id_foo");
84 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
85 EXPECT_EQ(FILE_ERROR_OK, cache_->Store("id_foo", "md5", src_path,
86 FileCache::FILE_OPERATION_COPY));
87
88 // Set up files in the cache directory. These files should be moved.
89 const base::FilePath file_directory =
90 temp_dir_.path().AppendASCII(kCacheFileDirectory);
91 ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_bar")));
92 ASSERT_TRUE(base::CopyFile(src_path, file_directory.AppendASCII("id_baz")));
93
94 // Insert a dirty entry with "id_baz" to |recovered_cache_info|.
95 // This should not prevent the file from being recovered.
96 ResourceMetadataStorage::RecoveredCacheInfoMap recovered_cache_info;
97 recovered_cache_info["id_baz"].is_dirty = true;
98 recovered_cache_info["id_baz"].title = "baz.png";
99
100 // Recover files.
101 const base::FilePath dest_directory = temp_dir_.path().AppendASCII("dest");
102 EXPECT_TRUE(cache_->RecoverFilesFromCacheDirectory(dest_directory,
103 recovered_cache_info));
104
105 // Only two files should be recovered.
106 EXPECT_TRUE(base::PathExists(dest_directory));
107 // base::FileEnumerator does not guarantee the order.
108 if (base::PathExists(dest_directory.AppendASCII("baz00000001.png"))) {
109 EXPECT_TRUE(base::ContentsEqual(
110 src_path,
111 dest_directory.AppendASCII("baz00000001.png")));
112 EXPECT_TRUE(base::ContentsEqual(
113 src_path,
114 dest_directory.AppendASCII("image00000002.png")));
115 } else {
116 EXPECT_TRUE(base::ContentsEqual(
117 src_path,
118 dest_directory.AppendASCII("image00000001.png")));
119 EXPECT_TRUE(base::ContentsEqual(
120 src_path,
121 dest_directory.AppendASCII("baz00000002.png")));
122 }
123 EXPECT_FALSE(base::PathExists(
124 dest_directory.AppendASCII("image00000003.png")));
125 }
126
127 TEST_F(FileCacheTest, FreeDiskSpaceIfNeededFor) {
128 base::FilePath src_file;
129 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
130
131 // Store a file as a 'temporary' file and remember the path.
132 const std::string id_tmp = "id_tmp", md5_tmp = "md5_tmp";
133
134 ResourceEntry entry;
135 entry.set_local_id(id_tmp);
136 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
137 ASSERT_EQ(FILE_ERROR_OK,
138 cache_->Store(id_tmp, md5_tmp, src_file,
139 FileCache::FILE_OPERATION_COPY));
140 base::FilePath tmp_path;
141 ASSERT_EQ(FILE_ERROR_OK, cache_->GetFile(id_tmp, &tmp_path));
142
143 // Store a file as a pinned file and remember the path.
144 const std::string id_pinned = "id_pinned", md5_pinned = "md5_pinned";
145 entry.Clear();
146 entry.set_local_id(id_pinned);
147 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
148 ASSERT_EQ(FILE_ERROR_OK,
149 cache_->Store(id_pinned, md5_pinned, src_file,
150 FileCache::FILE_OPERATION_COPY));
151 ASSERT_EQ(FILE_ERROR_OK, cache_->Pin(id_pinned));
152 base::FilePath pinned_path;
153 ASSERT_EQ(FILE_ERROR_OK, cache_->GetFile(id_pinned, &pinned_path));
154
155 // Call FreeDiskSpaceIfNeededFor().
156 fake_free_disk_space_getter_->set_default_value(test_util::kLotsOfSpace);
157 fake_free_disk_space_getter_->PushFakeValue(0);
158 const int64 kNeededBytes = 1;
159 EXPECT_TRUE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes));
160
161 // Only 'temporary' file gets removed.
162 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_tmp, &entry));
163 EXPECT_FALSE(entry.file_specific_info().cache_state().is_present());
164 EXPECT_FALSE(base::PathExists(tmp_path));
165
166 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_pinned, &entry));
167 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
168 EXPECT_TRUE(base::PathExists(pinned_path));
169
170 // Returns false when disk space cannot be freed.
171 fake_free_disk_space_getter_->set_default_value(0);
172 EXPECT_FALSE(cache_->FreeDiskSpaceIfNeededFor(kNeededBytes));
173 }
174
175 TEST_F(FileCacheTest, GetFile) {
176 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
177 const std::string src_contents = "test";
178 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
179 src_contents));
180 std::string id("id1");
181 std::string md5(base::MD5String(src_contents));
182
183 const base::FilePath cache_file_directory =
184 temp_dir_.path().AppendASCII(kCacheFileDirectory);
185
186 // Try to get an existing file from cache.
187 ResourceEntry entry;
188 entry.set_local_id(id);
189 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
190 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, md5, src_file_path,
191 FileCache::FILE_OPERATION_COPY));
192 base::FilePath cache_file_path;
193 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
194 EXPECT_EQ(
195 cache_file_directory.AppendASCII(util::EscapeCacheFileName(id)).value(),
196 cache_file_path.value());
197
198 std::string contents;
199 EXPECT_TRUE(base::ReadFileToString(cache_file_path, &contents));
200 EXPECT_EQ(src_contents, contents);
201
202 // Get file from cache with different id.
203 id = "id2";
204 entry.Clear();
205 entry.set_local_id(id);
206 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
207 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->GetFile(id, &cache_file_path));
208
209 // Pin a non-existent file.
210 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id));
211
212 // Get the non-existent pinned file from cache.
213 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->GetFile(id, &cache_file_path));
214
215 // Get a previously pinned and stored file from cache.
216 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, md5, src_file_path,
217 FileCache::FILE_OPERATION_COPY));
218
219 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
220 EXPECT_EQ(
221 cache_file_directory.AppendASCII(util::EscapeCacheFileName(id)).value(),
222 cache_file_path.value());
223
224 contents.clear();
225 EXPECT_TRUE(base::ReadFileToString(cache_file_path, &contents));
226 EXPECT_EQ(src_contents, contents);
227 }
228
229 TEST_F(FileCacheTest, Store) {
230 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
231 const std::string src_contents = "test";
232 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
233 src_contents));
234 std::string id("id");
235 std::string md5(base::MD5String(src_contents));
236
237 // Store a file.
238 ResourceEntry entry;
239 entry.set_local_id(id);
240 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
241 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
242 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
243
244 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
245 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
246 EXPECT_EQ(md5, entry.file_specific_info().cache_state().md5());
247
248 base::FilePath cache_file_path;
249 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
250 EXPECT_TRUE(base::ContentsEqual(src_file_path, cache_file_path));
251
252 // Store a non-existent file.
253 EXPECT_EQ(FILE_ERROR_FAILED, cache_->Store(
254 id, md5, base::FilePath::FromUTF8Unsafe("non_existent_file"),
255 FileCache::FILE_OPERATION_COPY));
256
257 // Passing empty MD5 marks the entry as dirty.
258 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
259 id, std::string(), src_file_path, FileCache::FILE_OPERATION_COPY));
260
261 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
262 EXPECT_TRUE(entry.file_specific_info().cache_state().is_present());
263 EXPECT_TRUE(entry.file_specific_info().cache_state().md5().empty());
264 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
265
266 // No free space available.
267 fake_free_disk_space_getter_->set_default_value(0);
268
269 EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE, cache_->Store(
270 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
271 }
272
273 TEST_F(FileCacheTest, PinAndUnpin) {
274 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
275 const std::string src_contents = "test";
276 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
277 src_contents));
278 std::string id("id_present");
279 std::string md5(base::MD5String(src_contents));
280
281 // Store a file.
282 ResourceEntry entry;
283 entry.set_local_id(id);
284 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
285 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
286 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
287
288 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
289 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned());
290
291 // Pin the existing file.
292 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id));
293
294 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
295 EXPECT_TRUE(entry.file_specific_info().cache_state().is_pinned());
296
297 // Unpin the file.
298 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id));
299
300 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
301 EXPECT_FALSE(entry.file_specific_info().cache_state().is_pinned());
302
303 // Pin a non-present file.
304 std::string id_non_present = "id_non_present";
305 entry.Clear();
306 entry.set_local_id(id_non_present);
307 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
308 EXPECT_EQ(FILE_ERROR_OK, cache_->Pin(id_non_present));
309
310 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_non_present, &entry));
311 EXPECT_TRUE(entry.file_specific_info().cache_state().is_pinned());
312
313 // Unpin the previously pinned non-existent file.
314 EXPECT_EQ(FILE_ERROR_OK, cache_->Unpin(id_non_present));
315
316 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id_non_present, &entry));
317 EXPECT_FALSE(entry.file_specific_info().has_cache_state());
318
319 // Unpin a file that doesn't exist in cache and is not pinned.
320 EXPECT_EQ(FILE_ERROR_NOT_FOUND, cache_->Unpin("id_non_existent"));
321 }
322
323 TEST_F(FileCacheTest, MountUnmount) {
324 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
325 const std::string src_contents = "test";
326 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
327 src_contents));
328 std::string id("id_present");
329 std::string md5(base::MD5String(src_contents));
330
331 // Store a file.
332 ResourceEntry entry;
333 entry.set_local_id(id);
334 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
335 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
336 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
337
338 // Mark the file mounted.
339 base::FilePath cache_file_path;
340 EXPECT_EQ(FILE_ERROR_OK, cache_->MarkAsMounted(id, &cache_file_path));
341
342 // Try to remove it.
343 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->Remove(id));
344
345 // Clear mounted state of the file.
346 EXPECT_EQ(FILE_ERROR_OK, cache_->MarkAsUnmounted(cache_file_path));
347
348 // Try to remove again.
349 EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id));
350 }
351
352 TEST_F(FileCacheTest, OpenForWrite) {
353 // Prepare a file.
354 base::FilePath src_file;
355 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
356
357 const std::string id = "id";
358 ResourceEntry entry;
359 entry.set_local_id(id);
360 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
361 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file,
362 FileCache::FILE_OPERATION_COPY));
363 EXPECT_EQ(0, entry.file_info().last_modified());
364
365 // Entry is not dirty nor opened.
366 EXPECT_FALSE(cache_->IsOpenedForWrite(id));
367 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
368 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
369
370 // Open (1).
371 scoped_ptr<base::ScopedClosureRunner> file_closer1;
372 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer1));
373 EXPECT_TRUE(cache_->IsOpenedForWrite(id));
374
375 // Entry is dirty.
376 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
377 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
378
379 // Open (2).
380 scoped_ptr<base::ScopedClosureRunner> file_closer2;
381 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer2));
382 EXPECT_TRUE(cache_->IsOpenedForWrite(id));
383
384 // Close (1).
385 file_closer1.reset();
386 base::RunLoop().RunUntilIdle();
387 EXPECT_TRUE(cache_->IsOpenedForWrite(id));
388
389 // last_modified is updated.
390 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
391 EXPECT_NE(0, entry.file_info().last_modified());
392
393 // Close (2).
394 file_closer2.reset();
395 base::RunLoop().RunUntilIdle();
396 EXPECT_FALSE(cache_->IsOpenedForWrite(id));
397
398 // Try to open non-existent file.
399 EXPECT_EQ(FILE_ERROR_NOT_FOUND,
400 cache_->OpenForWrite("nonexistent_id", &file_closer1));
401 }
402
403 TEST_F(FileCacheTest, UpdateMd5) {
404 // Store test data.
405 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
406 const std::string contents_before = "before";
407 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
408 contents_before));
409 std::string id("id1");
410 ResourceEntry entry;
411 entry.set_local_id(id);
412 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
413 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(id, base::MD5String(contents_before),
414 src_file_path,
415 FileCache::FILE_OPERATION_COPY));
416
417 // Modify the cache file.
418 scoped_ptr<base::ScopedClosureRunner> file_closer;
419 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer));
420 base::FilePath cache_file_path;
421 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
422 const std::string contents_after = "after";
423 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(cache_file_path,
424 contents_after));
425
426 // Cannot update MD5 of an opend file.
427 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->UpdateMd5(id));
428
429 // Close file.
430 file_closer.reset();
431 base::RunLoop().RunUntilIdle();
432
433 // MD5 was cleared by OpenForWrite().
434 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
435 EXPECT_TRUE(entry.file_specific_info().cache_state().md5().empty());
436
437 // Update MD5.
438 EXPECT_EQ(FILE_ERROR_OK, cache_->UpdateMd5(id));
439 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
440 EXPECT_EQ(base::MD5String(contents_after),
441 entry.file_specific_info().cache_state().md5());
442 }
443
444 TEST_F(FileCacheTest, ClearDirty) {
445 // Prepare a file.
446 base::FilePath src_file;
447 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
448
449 const std::string id = "id";
450 ResourceEntry entry;
451 entry.set_local_id(id);
452 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
453 ASSERT_EQ(FILE_ERROR_OK, cache_->Store(id, "md5", src_file,
454 FileCache::FILE_OPERATION_COPY));
455
456 // Open the file.
457 scoped_ptr<base::ScopedClosureRunner> file_closer;
458 EXPECT_EQ(FILE_ERROR_OK, cache_->OpenForWrite(id, &file_closer));
459
460 // Entry is dirty.
461 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
462 EXPECT_TRUE(entry.file_specific_info().cache_state().is_dirty());
463
464 // Cannot clear the dirty bit of an opened entry.
465 EXPECT_EQ(FILE_ERROR_IN_USE, cache_->ClearDirty(id));
466
467 // Close the file and clear the dirty bit.
468 file_closer.reset();
469 base::RunLoop().RunUntilIdle();
470 EXPECT_EQ(FILE_ERROR_OK, cache_->ClearDirty(id));
471
472 // Entry is not dirty.
473 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->GetEntry(id, &entry));
474 EXPECT_FALSE(entry.file_specific_info().cache_state().is_dirty());
475 }
476
477 TEST_F(FileCacheTest, Remove) {
478 const base::FilePath src_file_path = temp_dir_.path().Append("test.dat");
479 const std::string src_contents = "test";
480 EXPECT_TRUE(google_apis::test_util::WriteStringToFile(src_file_path,
481 src_contents));
482 std::string id("id");
483 std::string md5(base::MD5String(src_contents));
484
485 // First store a file to cache.
486 ResourceEntry entry;
487 entry.set_local_id(id);
488 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
489 base::FilePath src_file;
490 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
491 EXPECT_EQ(FILE_ERROR_OK, cache_->Store(
492 id, md5, src_file_path, FileCache::FILE_OPERATION_COPY));
493
494 base::FilePath cache_file_path;
495 EXPECT_EQ(FILE_ERROR_OK, cache_->GetFile(id, &cache_file_path));
496
497 // Then try to remove existing file from cache.
498 EXPECT_EQ(FILE_ERROR_OK, cache_->Remove(id));
499 EXPECT_FALSE(base::PathExists(cache_file_path));
500 }
501
502 TEST_F(FileCacheTest, RenameCacheFilesToNewFormat) {
503 const base::FilePath file_directory =
504 temp_dir_.path().AppendASCII(kCacheFileDirectory);
505
506 // File with an old style "<prefix>:<ID>.<MD5>" name.
507 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
508 file_directory.AppendASCII("file:id_koo.md5"), "koo"));
509
510 // File with multiple extensions should be removed.
511 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
512 file_directory.AppendASCII("id_kyu.md5.mounted"), "kyu (mounted)"));
513 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(
514 file_directory.AppendASCII("id_kyu.md5"), "kyu"));
515
516 // Rename and verify the result.
517 EXPECT_TRUE(RenameCacheFilesToNewFormat(cache_.get()));
518 std::string contents;
519 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_koo"),
520 &contents));
521 EXPECT_EQ("koo", contents);
522 contents.clear();
523 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"),
524 &contents));
525 EXPECT_EQ("kyu", contents);
526
527 // Rename again.
528 EXPECT_TRUE(RenameCacheFilesToNewFormat(cache_.get()));
529
530 // Files with new style names are not affected.
531 contents.clear();
532 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_koo"),
533 &contents));
534 EXPECT_EQ("koo", contents);
535 contents.clear();
536 EXPECT_TRUE(base::ReadFileToString(file_directory.AppendASCII("id_kyu"),
537 &contents));
538 EXPECT_EQ("kyu", contents);
539 }
540
541 TEST_F(FileCacheTest, ClearAll) {
542 const std::string id("1a2b");
543 const std::string md5("abcdef0123456789");
544
545 // Store an existing file.
546 ResourceEntry entry;
547 entry.set_local_id(id);
548 EXPECT_EQ(FILE_ERROR_OK, metadata_storage_->PutEntry(entry));
549 base::FilePath src_file;
550 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &src_file));
551 ASSERT_EQ(FILE_ERROR_OK,
552 cache_->Store(id, md5, src_file, FileCache::FILE_OPERATION_COPY));
553
554 // Clear cache.
555 EXPECT_TRUE(cache_->ClearAll());
556
557 // Verify that the cache is removed.
558 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_));
559 }
560
561 } // namespace internal
562 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/drive/file_cache.cc ('k') | chrome/browser/chromeos/drive/file_system.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698