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 "chrome/browser/chromeos/drive/drive.pb.h"
12 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h"
13 #include "chrome/browser/chromeos/drive/file_system_util.h"
14 #include "chrome/browser/chromeos/drive/remove_stale_cache_files.h"
15 #include "chrome/browser/chromeos/drive/resource_metadata.h"
16 #include "chrome/browser/chromeos/drive/test_util.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "google_apis/drive/test_util.h"
19 #include "testing/gtest/include/gtest/gtest.h"
24 class RemoveStaleCacheFilesTest
: public testing::Test
{
26 void SetUp() override
{
27 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
29 fake_free_disk_space_getter_
.reset(new FakeFreeDiskSpaceGetter
);
31 metadata_storage_
.reset(new ResourceMetadataStorage(
32 temp_dir_
.path(), base::MessageLoopProxy::current().get()));
34 cache_
.reset(new FileCache(metadata_storage_
.get(),
36 base::MessageLoopProxy::current().get(),
37 fake_free_disk_space_getter_
.get()));
39 resource_metadata_
.reset(new ResourceMetadata(
40 metadata_storage_
.get(), cache_
.get(),
41 base::MessageLoopProxy::current()));
43 ASSERT_TRUE(metadata_storage_
->Initialize());
44 ASSERT_TRUE(cache_
->Initialize());
45 ASSERT_EQ(FILE_ERROR_OK
, resource_metadata_
->Initialize());
48 content::TestBrowserThreadBundle thread_bundle_
;
49 base::ScopedTempDir temp_dir_
;
51 scoped_ptr
<ResourceMetadataStorage
,
52 test_util::DestroyHelperForTests
> metadata_storage_
;
53 scoped_ptr
<FileCache
, test_util::DestroyHelperForTests
> cache_
;
54 scoped_ptr
<ResourceMetadata
, test_util::DestroyHelperForTests
>
56 scoped_ptr
<FakeFreeDiskSpaceGetter
> fake_free_disk_space_getter_
;
59 TEST_F(RemoveStaleCacheFilesTest
, RemoveStaleCacheFiles
) {
60 base::FilePath dummy_file
;
61 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &dummy_file
));
62 std::string
md5_metadata("abcdef0123456789"), md5_cache("ABCDEF9876543210");
64 // Create a stale cache file.
67 entry
.mutable_file_specific_info()->set_md5(md5_metadata
);
68 entry
.set_parent_local_id(util::kDriveGrandRootLocalId
);
69 entry
.set_title("File.txt");
70 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(entry
, &local_id
));
72 EXPECT_EQ(FILE_ERROR_OK
,
73 cache_
->Store(local_id
, md5_cache
, dummy_file
,
74 FileCache::FILE_OPERATION_COPY
));
76 // Remove stale cache files.
77 RemoveStaleCacheFiles(cache_
.get(), resource_metadata_
.get());
79 // Verify that the cache is deleted.
80 EXPECT_EQ(FILE_ERROR_OK
,
81 resource_metadata_
->GetResourceEntryById(local_id
, &entry
));
82 EXPECT_FALSE(entry
.file_specific_info().cache_state().is_present());
85 TEST_F(RemoveStaleCacheFilesTest
, DirtyCacheFiles
) {
86 base::FilePath dummy_file
;
87 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &dummy_file
));
90 std::string
md5_metadata("abcdef0123456789");
94 entry
.mutable_file_specific_info()->set_md5(md5_metadata
);
95 entry
.set_parent_local_id(util::kDriveGrandRootLocalId
);
96 entry
.set_title("file.txt");
97 EXPECT_EQ(FILE_ERROR_OK
, resource_metadata_
->AddEntry(entry
, &local_id
));
99 EXPECT_EQ(FILE_ERROR_OK
,
100 cache_
->Store(local_id
, std::string(), dummy_file
,
101 FileCache::FILE_OPERATION_COPY
));
103 // Remove stale cache files.
104 RemoveStaleCacheFiles(cache_
.get(), resource_metadata_
.get());
106 // Dirty cache should not be removed even though its MD5 doesn't match.
107 EXPECT_EQ(FILE_ERROR_OK
,
108 resource_metadata_
->GetResourceEntryById(local_id
, &entry
));
109 EXPECT_TRUE(entry
.file_specific_info().cache_state().is_present());
112 } // namespace internal