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.
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_temp_dir.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "chrome/browser/chromeos/drive/remove_stale_cache_files.h"
14 #include "components/drive/drive.pb.h"
15 #include "components/drive/drive_test_util.h"
16 #include "components/drive/fake_free_disk_space_getter.h"
17 #include "components/drive/file_system_core_util.h"
18 #include "components/drive/resource_metadata.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "google_apis/drive/test_util.h"
21 #include "testing/gtest/include/gtest/gtest.h"
26 class RemoveStaleCacheFilesTest
: public testing::Test
{
28 void SetUp() override
{
29 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
31 fake_free_disk_space_getter_
.reset(new FakeFreeDiskSpaceGetter
);
33 metadata_storage_
.reset(new ResourceMetadataStorage(
34 temp_dir_
.path(), base::ThreadTaskRunnerHandle::Get().get()));
36 cache_
.reset(new FileCache(metadata_storage_
.get(),
38 base::ThreadTaskRunnerHandle::Get().get(),
39 fake_free_disk_space_getter_
.get()));
41 resource_metadata_
.reset(new ResourceMetadata(
42 metadata_storage_
.get(), cache_
.get(),
43 base::ThreadTaskRunnerHandle::Get()));
45 ASSERT_TRUE(metadata_storage_
->Initialize());
46 ASSERT_TRUE(cache_
->Initialize());
47 ASSERT_EQ(FILE_ERROR_OK
, resource_metadata_
->Initialize());
50 content::TestBrowserThreadBundle thread_bundle_
;
51 base::ScopedTempDir temp_dir_
;
53 scoped_ptr
<ResourceMetadataStorage
,
54 test_util::DestroyHelperForTests
> metadata_storage_
;
55 scoped_ptr
<FileCache
, test_util::DestroyHelperForTests
> cache_
;
56 scoped_ptr
<ResourceMetadata
, test_util::DestroyHelperForTests
>
58 scoped_ptr
<FakeFreeDiskSpaceGetter
> fake_free_disk_space_getter_
;
61 TEST_F(RemoveStaleCacheFilesTest
, RemoveStaleCacheFiles
) {
62 base::FilePath dummy_file
;
63 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &dummy_file
));
64 std::string
md5_metadata("abcdef0123456789"), md5_cache("ABCDEF9876543210");
66 // Create a stale cache file.
69 entry
.mutable_file_specific_info()->set_md5(md5_metadata
);
70 entry
.set_parent_local_id(util::kDriveGrandRootLocalId
);
71 entry
.set_title("File.txt");
72 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(entry
, &local_id
));
74 EXPECT_EQ(FILE_ERROR_OK
,
75 cache_
->Store(local_id
, md5_cache
, dummy_file
,
76 FileCache::FILE_OPERATION_COPY
));
78 // Remove stale cache files.
79 RemoveStaleCacheFiles(cache_
.get(), resource_metadata_
.get());
81 // Verify that the cache is deleted.
82 EXPECT_EQ(FILE_ERROR_OK
,
83 resource_metadata_
->GetResourceEntryById(local_id
, &entry
));
84 EXPECT_FALSE(entry
.file_specific_info().cache_state().is_present());
87 TEST_F(RemoveStaleCacheFilesTest
, DirtyCacheFiles
) {
88 base::FilePath dummy_file
;
89 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &dummy_file
));
92 std::string
md5_metadata("abcdef0123456789");
96 entry
.mutable_file_specific_info()->set_md5(md5_metadata
);
97 entry
.set_parent_local_id(util::kDriveGrandRootLocalId
);
98 entry
.set_title("file.txt");
99 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(entry
, &local_id
));
101 EXPECT_EQ(FILE_ERROR_OK
,
102 cache_
->Store(local_id
, std::string(), dummy_file
,
103 FileCache::FILE_OPERATION_COPY
));
105 // Remove stale cache files.
106 RemoveStaleCacheFiles(cache_
.get(), resource_metadata_
.get());
108 // Dirty cache should not be removed even though its MD5 doesn't match.
109 EXPECT_EQ(FILE_ERROR_OK
,
110 resource_metadata_
->GetResourceEntryById(local_id
, &entry
));
111 EXPECT_TRUE(entry
.file_specific_info().cache_state().is_present());
114 } // namespace internal