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.
5 #include "components/drive/file_cache.h"
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"
15 #include "base/path_service.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "components/drive/drive.pb.h"
19 #include "components/drive/drive_test_util.h"
20 #include "components/drive/fake_free_disk_space_getter.h"
21 #include "components/drive/file_system_core_util.h"
22 #include "components/drive/resource_metadata_storage.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"
31 const char kCacheFileDirectory
[] = "files";
35 // Tests FileCache methods working with the blocking task runner.
36 class FileCacheTest
: public testing::Test
{
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
);
43 ASSERT_TRUE(base::CreateDirectory(metadata_dir
));
44 ASSERT_TRUE(base::CreateDirectory(cache_files_dir_
));
46 fake_free_disk_space_getter_
.reset(new FakeFreeDiskSpaceGetter
);
48 metadata_storage_
.reset(new ResourceMetadataStorage(
50 base::ThreadTaskRunnerHandle::Get().get()));
51 ASSERT_TRUE(metadata_storage_
->Initialize());
53 cache_
.reset(new FileCache(
54 metadata_storage_
.get(),
56 base::ThreadTaskRunnerHandle::Get().get(),
57 fake_free_disk_space_getter_
.get()));
58 ASSERT_TRUE(cache_
->Initialize());
61 static bool RenameCacheFilesToNewFormat(FileCache
* cache
) {
62 return cache
->RenameCacheFilesToNewFormat();
65 content::TestBrowserThreadBundle thread_bundle_
;
66 base::ScopedTempDir temp_dir_
;
67 base::FilePath cache_files_dir_
;
69 scoped_ptr
<ResourceMetadataStorage
, test_util::DestroyHelperForTests
>
71 scoped_ptr
<FileCache
, test_util::DestroyHelperForTests
> cache_
;
72 scoped_ptr
<FakeFreeDiskSpaceGetter
> fake_free_disk_space_getter_
;
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");
81 // Store files. This file should not be moved.
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
));
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")));
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";
101 const base::FilePath dest_directory
= temp_dir_
.path().AppendASCII("dest");
102 EXPECT_TRUE(cache_
->RecoverFilesFromCacheDirectory(dest_directory
,
103 recovered_cache_info
));
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(
111 dest_directory
.AppendASCII("baz00000001.png")));
112 EXPECT_TRUE(base::ContentsEqual(
114 dest_directory
.AppendASCII("image00000002.png")));
116 EXPECT_TRUE(base::ContentsEqual(
118 dest_directory
.AppendASCII("image00000001.png")));
119 EXPECT_TRUE(base::ContentsEqual(
121 dest_directory
.AppendASCII("baz00000002.png")));
123 EXPECT_FALSE(base::PathExists(
124 dest_directory
.AppendASCII("image00000003.png")));
127 TEST_F(FileCacheTest
, FreeDiskSpaceIfNeededFor
) {
128 base::FilePath src_file
;
129 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &src_file
));
131 // Store a file as a 'temporary' file and remember the path.
132 const std::string id_tmp
= "id_tmp", md5_tmp
= "md5_tmp";
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
));
143 // Store a file as a pinned file and remember the path.
144 const std::string id_pinned
= "id_pinned", md5_pinned
= "md5_pinned";
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
));
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
));
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
));
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
));
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
));
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
,
180 std::string
id("id1");
181 std::string
md5(base::MD5String(src_contents
));
183 const base::FilePath cache_file_directory
=
184 temp_dir_
.path().AppendASCII(kCacheFileDirectory
);
186 // Try to get an existing file from cache.
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
));
195 cache_file_directory
.AppendASCII(util::EscapeCacheFileName(id
)).value(),
196 cache_file_path
.value());
198 std::string contents
;
199 EXPECT_TRUE(base::ReadFileToString(cache_file_path
, &contents
));
200 EXPECT_EQ(src_contents
, contents
);
202 // Get file from cache with different id.
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
));
209 // Pin a non-existent file.
210 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Pin(id
));
212 // Get the non-existent pinned file from cache.
213 EXPECT_EQ(FILE_ERROR_NOT_FOUND
, cache_
->GetFile(id
, &cache_file_path
));
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
));
219 EXPECT_EQ(FILE_ERROR_OK
, cache_
->GetFile(id
, &cache_file_path
));
221 cache_file_directory
.AppendASCII(util::EscapeCacheFileName(id
)).value(),
222 cache_file_path
.value());
225 EXPECT_TRUE(base::ReadFileToString(cache_file_path
, &contents
));
226 EXPECT_EQ(src_contents
, contents
);
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
,
234 std::string
id("id");
235 std::string
md5(base::MD5String(src_contents
));
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
));
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());
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
));
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
));
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
));
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());
266 // No free space available.
267 fake_free_disk_space_getter_
->set_default_value(0);
269 EXPECT_EQ(FILE_ERROR_NO_LOCAL_SPACE
, cache_
->Store(
270 id
, md5
, src_file_path
, FileCache::FILE_OPERATION_COPY
));
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
,
278 std::string
id("id_present");
279 std::string
md5(base::MD5String(src_contents
));
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
));
288 EXPECT_EQ(FILE_ERROR_OK
, metadata_storage_
->GetEntry(id
, &entry
));
289 EXPECT_FALSE(entry
.file_specific_info().cache_state().is_pinned());
291 // Pin the existing file.
292 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Pin(id
));
294 EXPECT_EQ(FILE_ERROR_OK
, metadata_storage_
->GetEntry(id
, &entry
));
295 EXPECT_TRUE(entry
.file_specific_info().cache_state().is_pinned());
298 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Unpin(id
));
300 EXPECT_EQ(FILE_ERROR_OK
, metadata_storage_
->GetEntry(id
, &entry
));
301 EXPECT_FALSE(entry
.file_specific_info().cache_state().is_pinned());
303 // Pin a non-present file.
304 std::string id_non_present
= "id_non_present";
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
));
310 EXPECT_EQ(FILE_ERROR_OK
, metadata_storage_
->GetEntry(id_non_present
, &entry
));
311 EXPECT_TRUE(entry
.file_specific_info().cache_state().is_pinned());
313 // Unpin the previously pinned non-existent file.
314 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Unpin(id_non_present
));
316 EXPECT_EQ(FILE_ERROR_OK
, metadata_storage_
->GetEntry(id_non_present
, &entry
));
317 EXPECT_FALSE(entry
.file_specific_info().has_cache_state());
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"));
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
,
328 std::string
id("id_present");
329 std::string
md5(base::MD5String(src_contents
));
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
));
338 // Mark the file mounted.
339 base::FilePath cache_file_path
;
340 EXPECT_EQ(FILE_ERROR_OK
, cache_
->MarkAsMounted(id
, &cache_file_path
));
343 EXPECT_EQ(FILE_ERROR_IN_USE
, cache_
->Remove(id
));
345 // Clear mounted state of the file.
346 EXPECT_EQ(FILE_ERROR_OK
, cache_
->MarkAsUnmounted(cache_file_path
));
348 // Try to remove again.
349 EXPECT_EQ(FILE_ERROR_OK
, cache_
->Remove(id
));
352 TEST_F(FileCacheTest
, OpenForWrite
) {
354 base::FilePath src_file
;
355 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &src_file
));
357 const std::string id
= "id";
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());
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());
371 scoped_ptr
<base::ScopedClosureRunner
> file_closer1
;
372 EXPECT_EQ(FILE_ERROR_OK
, cache_
->OpenForWrite(id
, &file_closer1
));
373 EXPECT_TRUE(cache_
->IsOpenedForWrite(id
));
376 EXPECT_EQ(FILE_ERROR_OK
, metadata_storage_
->GetEntry(id
, &entry
));
377 EXPECT_TRUE(entry
.file_specific_info().cache_state().is_dirty());
380 scoped_ptr
<base::ScopedClosureRunner
> file_closer2
;
381 EXPECT_EQ(FILE_ERROR_OK
, cache_
->OpenForWrite(id
, &file_closer2
));
382 EXPECT_TRUE(cache_
->IsOpenedForWrite(id
));
385 file_closer1
.reset();
386 base::RunLoop().RunUntilIdle();
387 EXPECT_TRUE(cache_
->IsOpenedForWrite(id
));
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());
394 file_closer2
.reset();
395 base::RunLoop().RunUntilIdle();
396 EXPECT_FALSE(cache_
->IsOpenedForWrite(id
));
398 // Try to open non-existent file.
399 EXPECT_EQ(FILE_ERROR_NOT_FOUND
,
400 cache_
->OpenForWrite("nonexistent_id", &file_closer1
));
403 TEST_F(FileCacheTest
, UpdateMd5
) {
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
,
409 std::string
id("id1");
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
),
415 FileCache::FILE_OPERATION_COPY
));
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
,
426 // Cannot update MD5 of an opend file.
427 EXPECT_EQ(FILE_ERROR_IN_USE
, cache_
->UpdateMd5(id
));
431 base::RunLoop().RunUntilIdle();
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());
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());
444 TEST_F(FileCacheTest
, ClearDirty
) {
446 base::FilePath src_file
;
447 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &src_file
));
449 const std::string id
= "id";
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
));
457 scoped_ptr
<base::ScopedClosureRunner
> file_closer
;
458 EXPECT_EQ(FILE_ERROR_OK
, cache_
->OpenForWrite(id
, &file_closer
));
461 EXPECT_EQ(FILE_ERROR_OK
, metadata_storage_
->GetEntry(id
, &entry
));
462 EXPECT_TRUE(entry
.file_specific_info().cache_state().is_dirty());
464 // Cannot clear the dirty bit of an opened entry.
465 EXPECT_EQ(FILE_ERROR_IN_USE
, cache_
->ClearDirty(id
));
467 // Close the file and clear the dirty bit.
469 base::RunLoop().RunUntilIdle();
470 EXPECT_EQ(FILE_ERROR_OK
, cache_
->ClearDirty(id
));
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());
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
,
482 std::string
id("id");
483 std::string
md5(base::MD5String(src_contents
));
485 // First store a file to cache.
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
));
494 base::FilePath cache_file_path
;
495 EXPECT_EQ(FILE_ERROR_OK
, cache_
->GetFile(id
, &cache_file_path
));
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
));
502 TEST_F(FileCacheTest
, RenameCacheFilesToNewFormat
) {
503 const base::FilePath file_directory
=
504 temp_dir_
.path().AppendASCII(kCacheFileDirectory
);
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"));
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"));
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"),
521 EXPECT_EQ("koo", contents
);
523 EXPECT_TRUE(base::ReadFileToString(file_directory
.AppendASCII("id_kyu"),
525 EXPECT_EQ("kyu", contents
);
528 EXPECT_TRUE(RenameCacheFilesToNewFormat(cache_
.get()));
530 // Files with new style names are not affected.
532 EXPECT_TRUE(base::ReadFileToString(file_directory
.AppendASCII("id_koo"),
534 EXPECT_EQ("koo", contents
);
536 EXPECT_TRUE(base::ReadFileToString(file_directory
.AppendASCII("id_kyu"),
538 EXPECT_EQ("kyu", contents
);
541 TEST_F(FileCacheTest
, ClearAll
) {
542 const std::string
id("1a2b");
543 const std::string
md5("abcdef0123456789");
545 // Store an existing file.
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
));
555 EXPECT_TRUE(cache_
->ClearAll());
557 // Verify that the cache is removed.
558 EXPECT_TRUE(base::IsDirectoryEmpty(cache_files_dir_
));
561 } // namespace internal