OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/chromeos/drive/file_cache.h" | 5 #include "chrome/browser/chromeos/drive/file_cache.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/files/file_enumerator.h" | 11 #include "base/files/file_enumerator.h" |
12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
15 #include "chrome/browser/chromeos/drive/drive.pb.h" | 15 #include "chrome/browser/chromeos/drive/drive.pb.h" |
16 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" | 16 #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" |
17 #include "chrome/browser/chromeos/drive/file_cache_metadata.h" | 17 #include "chrome/browser/chromeos/drive/file_cache_metadata.h" |
18 #include "chrome/browser/chromeos/drive/file_system_util.h" | 18 #include "chrome/browser/chromeos/drive/file_system_util.h" |
| 19 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h" |
19 #include "chrome/browser/chromeos/drive/test_util.h" | 20 #include "chrome/browser/chromeos/drive/test_util.h" |
20 #include "chrome/browser/google_apis/test_util.h" | 21 #include "chrome/browser/google_apis/test_util.h" |
21 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
22 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
24 | 25 |
25 namespace drive { | 26 namespace drive { |
26 namespace internal { | 27 namespace internal { |
27 namespace { | 28 namespace { |
28 | 29 |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 temp_dir_.path().Append(util::kCacheFileDirectory))); | 68 temp_dir_.path().Append(util::kCacheFileDirectory))); |
68 | 69 |
69 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), | 70 ASSERT_TRUE(file_util::CreateTemporaryFileInDir(temp_dir_.path(), |
70 &dummy_file_path_)); | 71 &dummy_file_path_)); |
71 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); | 72 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); |
72 | 73 |
73 scoped_refptr<base::SequencedWorkerPool> pool = | 74 scoped_refptr<base::SequencedWorkerPool> pool = |
74 content::BrowserThread::GetBlockingPool(); | 75 content::BrowserThread::GetBlockingPool(); |
75 blocking_task_runner_ = | 76 blocking_task_runner_ = |
76 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); | 77 pool->GetSequencedTaskRunner(pool->GetSequenceToken()); |
77 cache_.reset(new FileCache( | 78 |
78 temp_dir_.path().Append(util::kMetadataDirectory), | 79 metadata_storage_.reset(new ResourceMetadataStorage( |
79 temp_dir_.path().Append(util::kCacheFileDirectory), | 80 temp_dir_.path(), blocking_task_runner_)); |
80 blocking_task_runner_.get(), | |
81 fake_free_disk_space_getter_.get())); | |
82 | 81 |
83 bool success = false; | 82 bool success = false; |
84 base::PostTaskAndReplyWithResult( | 83 base::PostTaskAndReplyWithResult( |
85 blocking_task_runner_, | 84 blocking_task_runner_, |
86 FROM_HERE, | 85 FROM_HERE, |
| 86 base::Bind(&ResourceMetadataStorage::Initialize, |
| 87 base::Unretained(metadata_storage_.get())), |
| 88 google_apis::test_util::CreateCopyResultCallback(&success)); |
| 89 google_apis::test_util::RunBlockingPoolTask(); |
| 90 ASSERT_TRUE(success); |
| 91 |
| 92 cache_.reset(new FileCache( |
| 93 metadata_storage_.get(), |
| 94 temp_dir_.path().Append(util::kCacheFileDirectory), |
| 95 blocking_task_runner_.get(), |
| 96 fake_free_disk_space_getter_.get())); |
| 97 |
| 98 success = false; |
| 99 base::PostTaskAndReplyWithResult( |
| 100 blocking_task_runner_, |
| 101 FROM_HERE, |
87 base::Bind(&FileCache::Initialize, | 102 base::Bind(&FileCache::Initialize, |
88 base::Unretained(cache_.get())), | 103 base::Unretained(cache_.get())), |
89 google_apis::test_util::CreateCopyResultCallback(&success)); | 104 google_apis::test_util::CreateCopyResultCallback(&success)); |
90 google_apis::test_util::RunBlockingPoolTask(); | 105 google_apis::test_util::RunBlockingPoolTask(); |
91 ASSERT_TRUE(success); | 106 ASSERT_TRUE(success); |
92 } | 107 } |
93 | 108 |
94 virtual void TearDown() OVERRIDE { | |
95 cache_.reset(); | |
96 } | |
97 | |
98 void TestGetFileFromCacheByResourceIdAndMd5( | 109 void TestGetFileFromCacheByResourceIdAndMd5( |
99 const std::string& resource_id, | 110 const std::string& resource_id, |
100 const std::string& md5, | 111 const std::string& md5, |
101 FileError expected_error, | 112 FileError expected_error, |
102 const std::string& expected_file_extension) { | 113 const std::string& expected_file_extension) { |
103 FileError error = FILE_ERROR_OK; | 114 FileError error = FILE_ERROR_OK; |
104 base::FilePath cache_file_path; | 115 base::FilePath cache_file_path; |
105 cache_->GetFileOnUIThread(resource_id, md5, | 116 cache_->GetFileOnUIThread(resource_id, md5, |
106 google_apis::test_util::CreateCopyResultCallback( | 117 google_apis::test_util::CreateCopyResultCallback( |
107 &error, &cache_file_path)); | 118 &error, &cache_file_path)); |
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
412 current.BaseName().value()); | 423 current.BaseName().value()); |
413 } | 424 } |
414 return num_files_found; | 425 return num_files_found; |
415 } | 426 } |
416 | 427 |
417 content::TestBrowserThreadBundle thread_bundle_; | 428 content::TestBrowserThreadBundle thread_bundle_; |
418 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; | 429 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
419 base::ScopedTempDir temp_dir_; | 430 base::ScopedTempDir temp_dir_; |
420 base::FilePath dummy_file_path_; | 431 base::FilePath dummy_file_path_; |
421 | 432 |
| 433 scoped_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests> |
| 434 metadata_storage_; |
422 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; | 435 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; |
423 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; | 436 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; |
424 | 437 |
425 FileError expected_error_; | 438 FileError expected_error_; |
426 int expected_cache_state_; | 439 int expected_cache_state_; |
427 std::string expected_file_extension_; | 440 std::string expected_file_extension_; |
428 }; | 441 }; |
429 | 442 |
430 TEST_F(FileCacheTestOnUIThread, GetCacheFilePath) { | 443 TEST_F(FileCacheTestOnUIThread, GetCacheFilePath) { |
431 // Use alphanumeric characters for resource id. | 444 // Use alphanumeric characters for resource id. |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
846 std::string md5("abcdef0123456789"); | 859 std::string md5("abcdef0123456789"); |
847 | 860 |
848 // Try to store an existing file. | 861 // Try to store an existing file. |
849 TestStoreToCache(resource_id, md5, dummy_file_path_, FILE_ERROR_NO_SPACE, | 862 TestStoreToCache(resource_id, md5, dummy_file_path_, FILE_ERROR_NO_SPACE, |
850 TEST_CACHE_STATE_NONE); | 863 TEST_CACHE_STATE_NONE); |
851 | 864 |
852 // Verify that there's no files added. | 865 // Verify that there's no files added. |
853 EXPECT_EQ(0U, CountCacheFiles(resource_id, md5)); | 866 EXPECT_EQ(0U, CountCacheFiles(resource_id, md5)); |
854 } | 867 } |
855 | 868 |
856 // Don't use TEST_F, as we don't want SetUp() and TearDown() for this test. | |
857 TEST(FileCacheExtraTest, InitializationFailure) { | |
858 content::TestBrowserThreadBundle thread_bundle; | |
859 | |
860 // Set the cache root to a non existent path, so the initialization fails. | |
861 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache(new FileCache( | |
862 base::FilePath::FromUTF8Unsafe("/somewhere/nonexistent/blah/meta"), | |
863 base::FilePath::FromUTF8Unsafe("/somewhere/nonexistent/blah/files"), | |
864 base::MessageLoopProxy::current(), | |
865 NULL /* free_disk_space_getter */)); | |
866 | |
867 EXPECT_FALSE(cache->Initialize()); | |
868 } | |
869 | |
870 TEST_F(FileCacheTestOnUIThread, UpdatePinnedCache) { | 869 TEST_F(FileCacheTestOnUIThread, UpdatePinnedCache) { |
871 std::string resource_id("pdf:1a2b"); | 870 std::string resource_id("pdf:1a2b"); |
872 std::string md5("abcdef0123456789"); | 871 std::string md5("abcdef0123456789"); |
873 std::string md5_modified("aaaaaa0000000000"); | 872 std::string md5_modified("aaaaaa0000000000"); |
874 | 873 |
875 // Store an existing file. | 874 // Store an existing file. |
876 TestStoreToCache(resource_id, md5, dummy_file_path_, FILE_ERROR_OK, | 875 TestStoreToCache(resource_id, md5, dummy_file_path_, FILE_ERROR_OK, |
877 TEST_CACHE_STATE_PRESENT); | 876 TEST_CACHE_STATE_PRESENT); |
878 | 877 |
879 // Pin the file. | 878 // Pin the file. |
(...skipping 10 matching lines...) Expand all Loading... |
890 protected: | 889 protected: |
891 virtual void SetUp() OVERRIDE { | 890 virtual void SetUp() OVERRIDE { |
892 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 891 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
893 ASSERT_TRUE(file_util::CreateDirectory( | 892 ASSERT_TRUE(file_util::CreateDirectory( |
894 temp_dir_.path().Append(util::kMetadataDirectory))); | 893 temp_dir_.path().Append(util::kMetadataDirectory))); |
895 ASSERT_TRUE(file_util::CreateDirectory( | 894 ASSERT_TRUE(file_util::CreateDirectory( |
896 temp_dir_.path().Append(util::kCacheFileDirectory))); | 895 temp_dir_.path().Append(util::kCacheFileDirectory))); |
897 | 896 |
898 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); | 897 fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); |
899 | 898 |
| 899 metadata_storage_.reset(new ResourceMetadataStorage( |
| 900 temp_dir_.path().Append(util::kMetadataDirectory), |
| 901 base::MessageLoopProxy::current())); |
| 902 ASSERT_TRUE(metadata_storage_->Initialize()); |
| 903 |
900 cache_.reset(new FileCache( | 904 cache_.reset(new FileCache( |
901 temp_dir_.path().Append(util::kMetadataDirectory), | 905 metadata_storage_.get(), |
902 temp_dir_.path().Append(util::kCacheFileDirectory), | 906 temp_dir_.path().Append(util::kCacheFileDirectory), |
903 base::MessageLoopProxy::current(), | 907 base::MessageLoopProxy::current(), |
904 fake_free_disk_space_getter_.get())); | 908 fake_free_disk_space_getter_.get())); |
905 | |
906 ASSERT_TRUE(cache_->Initialize()); | 909 ASSERT_TRUE(cache_->Initialize()); |
907 } | 910 } |
908 | 911 |
909 virtual void TearDown() OVERRIDE { | 912 virtual void TearDown() OVERRIDE { |
910 cache_.reset(); | 913 cache_.reset(); |
911 } | 914 } |
912 | 915 |
913 static void ImportOldDB(FileCache* cache, const base::FilePath& old_db_path) { | 916 static bool ImportOldDB(FileCache* cache, const base::FilePath& old_db_path) { |
914 cache->ImportOldDB(old_db_path); | 917 return cache->ImportOldDB(old_db_path); |
915 } | 918 } |
916 | 919 |
917 content::TestBrowserThreadBundle thread_bundle_; | 920 content::TestBrowserThreadBundle thread_bundle_; |
918 base::ScopedTempDir temp_dir_; | 921 base::ScopedTempDir temp_dir_; |
919 | 922 |
| 923 scoped_ptr<ResourceMetadataStorage, test_util::DestroyHelperForTests> |
| 924 metadata_storage_; |
920 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; | 925 scoped_ptr<FileCache, test_util::DestroyHelperForTests> cache_; |
921 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; | 926 scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; |
922 }; | 927 }; |
923 | 928 |
924 TEST_F(FileCacheTest, ScanCacheFile) { | 929 TEST_F(FileCacheTest, ScanCacheFile) { |
925 // Set up files in the cache directory. | 930 // Set up files in the cache directory. |
926 const base::FilePath directory = | 931 const base::FilePath file_directory = |
927 temp_dir_.path().Append(util::kCacheFileDirectory); | 932 temp_dir_.path().Append(util::kCacheFileDirectory); |
928 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( | 933 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( |
929 directory.AppendASCII("id_foo.md5foo"), "foo")); | 934 file_directory.AppendASCII("id_foo.md5foo"), "foo")); |
930 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( | 935 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( |
931 directory.AppendASCII("id_bar.local"), "bar")); | 936 file_directory.AppendASCII("id_bar.local"), "bar")); |
932 | 937 |
933 // Remove the existing DB. | 938 // Remove the existing DB. |
934 ASSERT_TRUE(file_util::Delete( | 939 const base::FilePath metadata_directory = |
935 temp_dir_.path().Append(util::kMetadataDirectory), true /* recursive */)); | 940 temp_dir_.path().Append(util::kMetadataDirectory); |
| 941 ASSERT_TRUE(file_util::Delete(metadata_directory, true /* recursive */)); |
| 942 |
| 943 // Put an empty file with the same name as old DB. |
| 944 // This file cannot be opened by ImportOldDB() and will be dismissed. |
| 945 ASSERT_TRUE(file_util::CreateDirectory(metadata_directory)); |
| 946 ASSERT_TRUE(google_apis::test_util::WriteStringToFile( |
| 947 metadata_directory.Append(FileCache::kOldCacheMetadataDBName), "")); |
936 | 948 |
937 // Create a new cache and initialize it. | 949 // Create a new cache and initialize it. |
938 cache_.reset(new FileCache(temp_dir_.path().Append(util::kMetadataDirectory), | 950 metadata_storage_.reset(new ResourceMetadataStorage( |
| 951 metadata_directory, base::MessageLoopProxy::current())); |
| 952 ASSERT_TRUE(metadata_storage_->Initialize()); |
| 953 |
| 954 cache_.reset(new FileCache(metadata_storage_.get(), |
939 temp_dir_.path().Append(util::kCacheFileDirectory), | 955 temp_dir_.path().Append(util::kCacheFileDirectory), |
940 base::MessageLoopProxy::current(), | 956 base::MessageLoopProxy::current(), |
941 fake_free_disk_space_getter_.get())); | 957 fake_free_disk_space_getter_.get())); |
942 ASSERT_TRUE(cache_->Initialize()); | 958 ASSERT_TRUE(cache_->Initialize()); |
943 | 959 |
944 // Check contents of the cache. | 960 // Check contents of the cache. |
945 FileCacheEntry cache_entry; | 961 FileCacheEntry cache_entry; |
946 EXPECT_TRUE(cache_->GetCacheEntry("id_foo", std::string(), &cache_entry)); | 962 EXPECT_TRUE(cache_->GetCacheEntry("id_foo", std::string(), &cache_entry)); |
947 EXPECT_TRUE(cache_entry.is_present()); | 963 EXPECT_TRUE(cache_entry.is_present()); |
948 EXPECT_EQ("md5foo", cache_entry.md5()); | 964 EXPECT_EQ("md5foo", cache_entry.md5()); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1010 FileCacheEntry entry; | 1026 FileCacheEntry entry; |
1011 entry.set_md5(md5_1); | 1027 entry.set_md5(md5_1); |
1012 old_metadata.AddOrUpdateCacheEntry(key1, entry); | 1028 old_metadata.AddOrUpdateCacheEntry(key1, entry); |
1013 | 1029 |
1014 entry.set_md5(md5_2); | 1030 entry.set_md5(md5_2); |
1015 old_metadata.AddOrUpdateCacheEntry(key2, entry); | 1031 old_metadata.AddOrUpdateCacheEntry(key2, entry); |
1016 } | 1032 } |
1017 EXPECT_TRUE(file_util::PathExists(old_db_path)); | 1033 EXPECT_TRUE(file_util::PathExists(old_db_path)); |
1018 | 1034 |
1019 // Do import. | 1035 // Do import. |
1020 ImportOldDB(cache_.get(), old_db_path); | 1036 EXPECT_TRUE(ImportOldDB(cache_.get(), old_db_path)); |
1021 | 1037 |
1022 // Old DB should be removed. | 1038 // Old DB should be removed. |
1023 EXPECT_FALSE(file_util::PathExists(old_db_path)); | 1039 EXPECT_FALSE(file_util::PathExists(old_db_path)); |
1024 | 1040 |
1025 // Data is imported correctly. | 1041 // Data is imported correctly. |
1026 FileCacheEntry entry; | 1042 FileCacheEntry entry; |
1027 EXPECT_TRUE(cache_->GetCacheEntry(key1, std::string(), &entry)); | 1043 EXPECT_TRUE(cache_->GetCacheEntry(key1, std::string(), &entry)); |
1028 EXPECT_EQ(md5_1, entry.md5()); | 1044 EXPECT_EQ(md5_1, entry.md5()); |
1029 EXPECT_TRUE(cache_->GetCacheEntry(key2, std::string(), &entry)); | 1045 EXPECT_TRUE(cache_->GetCacheEntry(key2, std::string(), &entry)); |
1030 EXPECT_EQ(md5_2, entry.md5()); | 1046 EXPECT_EQ(md5_2, entry.md5()); |
1031 } | 1047 } |
1032 | 1048 |
1033 } // namespace internal | 1049 } // namespace internal |
1034 } // namespace drive | 1050 } // namespace drive |
OLD | NEW |