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_metadata.h" | 5 #include "chrome/browser/chromeos/drive/file_cache_metadata.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "chrome/browser/chromeos/drive/drive.pb.h" | 9 #include "chrome/browser/chromeos/drive/drive.pb.h" |
10 #include "chrome/browser/chromeos/drive/file_cache.h" | 10 #include "chrome/browser/chromeos/drive/file_cache.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 persistent_directory_ = cache_paths_[FileCache::CACHE_TYPE_PERSISTENT]; | 29 persistent_directory_ = cache_paths_[FileCache::CACHE_TYPE_PERSISTENT]; |
30 tmp_directory_ = cache_paths_[FileCache::CACHE_TYPE_TMP]; | 30 tmp_directory_ = cache_paths_[FileCache::CACHE_TYPE_TMP]; |
31 } | 31 } |
32 | 32 |
33 virtual void TearDown() OVERRIDE { | 33 virtual void TearDown() OVERRIDE { |
34 metadata_.reset(); | 34 metadata_.reset(); |
35 } | 35 } |
36 | 36 |
37 // Sets up the FileCacheMetadata object. | 37 // Sets up the FileCacheMetadata object. |
38 void SetUpCacheMetadata() { | 38 void SetUpCacheMetadata() { |
39 metadata_ = FileCacheMetadata::CreateCacheMetadata(NULL); | 39 metadata_.reset(new FileCacheMetadata(NULL)); |
40 ASSERT_TRUE(metadata_->Initialize(cache_paths_)); | 40 ASSERT_TRUE(metadata_->Initialize(cache_paths_)); |
41 } | 41 } |
42 | 42 |
43 // Sets up the cache directories with various files. | 43 // Sets up the cache directories with various files. |
44 // Should be called before SetUpCacheMetadata(). | 44 // Should be called before SetUpCacheMetadata(). |
45 void SetUpCacheWithVariousFiles() { | 45 void SetUpCacheWithVariousFiles() { |
46 // Create some files in persistent directory. | 46 // Create some files in persistent directory. |
47 // | 47 // |
48 CreateFile(persistent_directory_.AppendASCII("id_foo.md5foo")); | 48 CreateFile(persistent_directory_.AppendASCII("id_foo.md5foo")); |
49 CreateFile(persistent_directory_.AppendASCII("id_bar.local")); | 49 CreateFile(persistent_directory_.AppendASCII("id_bar.local")); |
50 | 50 |
51 // Create some files in tmp directory. | 51 // Create some files in tmp directory. |
52 // | 52 // |
53 CreateFile(tmp_directory_.AppendASCII("id_qux.md5qux")); | 53 CreateFile(tmp_directory_.AppendASCII("id_qux.md5qux")); |
54 // "id_quux" is invalid as we shouldn't have a dirty file in "tmp". | 54 // "id_quux" is invalid as we shouldn't have a dirty file in "tmp". |
55 CreateFile(tmp_directory_.AppendASCII("id_quux.local")); | 55 CreateFile(tmp_directory_.AppendASCII("id_quux.local")); |
56 } | 56 } |
57 | 57 |
58 // Create a file at |file_path|. | 58 // Create a file at |file_path|. |
59 void CreateFile(const base::FilePath& file_path) { | 59 void CreateFile(const base::FilePath& file_path) { |
60 const std::string kFoo = "foo"; | 60 const std::string kFoo = "foo"; |
61 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(file_path, kFoo)) | 61 ASSERT_TRUE(google_apis::test_util::WriteStringToFile(file_path, kFoo)) |
62 << ": " << file_path.value(); | 62 << ": " << file_path.value(); |
63 } | 63 } |
64 | 64 |
65 protected: | 65 protected: |
66 // Helper function to insert an item with key |resource_id| into |cache_map|. | |
67 // |md5| and |cache_state| are used to create the value FileCacheEntry. | |
68 void InsertIntoMap(FileCacheMetadata::CacheMap* cache_map, | |
69 const std::string& resource_id, | |
70 const FileCacheEntry& cache_entry) { | |
71 cache_map->insert(std::make_pair( | |
72 resource_id, cache_entry)); | |
73 } | |
74 | |
75 // Adds all entries in |cache_map| to the metadata storage. | |
76 void AddAllMapEntries(const FileCacheMetadata::CacheMap& cache_map) { | |
77 for (FileCacheMetadata::CacheMap::const_iterator iter = cache_map.begin(); | |
78 iter != cache_map.end(); ++iter) { | |
79 metadata_->AddOrUpdateCacheEntry(iter->first, iter->second); | |
80 } | |
81 } | |
82 | |
83 base::ScopedTempDir temp_dir_; | 66 base::ScopedTempDir temp_dir_; |
84 scoped_ptr<FileCacheMetadata> metadata_; | 67 scoped_ptr<FileCacheMetadata> metadata_; |
85 std::vector<base::FilePath> cache_paths_; | 68 std::vector<base::FilePath> cache_paths_; |
86 base::FilePath persistent_directory_; | 69 base::FilePath persistent_directory_; |
87 base::FilePath tmp_directory_; | 70 base::FilePath tmp_directory_; |
88 }; | 71 }; |
89 | 72 |
90 // Test all the methods of FileCacheMetadata except for | 73 // Test all the methods of FileCacheMetadata except for |
91 // RemoveTemporaryFiles. | 74 // RemoveTemporaryFiles. |
92 TEST_F(FileCacheMetadataTest, CacheTest) { | 75 TEST_F(FileCacheMetadataTest, CacheTest) { |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 EXPECT_TRUE(PathExists(tmp_directory_.AppendASCII("id_qux.md5qux"))); | 229 EXPECT_TRUE(PathExists(tmp_directory_.AppendASCII("id_qux.md5qux"))); |
247 | 230 |
248 // "id_quux" should be removed during cache initialization. | 231 // "id_quux" should be removed during cache initialization. |
249 EXPECT_FALSE(metadata_->GetCacheEntry("id_quux", "md5qux", &cache_entry)); | 232 EXPECT_FALSE(metadata_->GetCacheEntry("id_quux", "md5qux", &cache_entry)); |
250 } | 233 } |
251 | 234 |
252 // Test FileCacheMetadata::RemoveTemporaryFiles. | 235 // Test FileCacheMetadata::RemoveTemporaryFiles. |
253 TEST_F(FileCacheMetadataTest, RemoveTemporaryFiles) { | 236 TEST_F(FileCacheMetadataTest, RemoveTemporaryFiles) { |
254 SetUpCacheMetadata(); | 237 SetUpCacheMetadata(); |
255 | 238 |
256 FileCacheMetadata::CacheMap cache_map; | |
257 { | 239 { |
258 FileCacheEntry cache_entry; | 240 FileCacheEntry cache_entry; |
259 cache_entry.set_md5("<md5>"); | 241 cache_entry.set_md5("<md5>"); |
260 cache_entry.set_is_present(true); | 242 cache_entry.set_is_present(true); |
261 InsertIntoMap(&cache_map, "<resource_id_1>", cache_entry); | 243 metadata_->AddOrUpdateCacheEntry("<resource_id_1>", cache_entry); |
262 } | 244 } |
263 { | 245 { |
264 FileCacheEntry cache_entry; | 246 FileCacheEntry cache_entry; |
265 cache_entry.set_md5("<md5>"); | 247 cache_entry.set_md5("<md5>"); |
266 cache_entry.set_is_present(true); | 248 cache_entry.set_is_present(true); |
267 cache_entry.set_is_persistent(true); | 249 cache_entry.set_is_persistent(true); |
268 InsertIntoMap(&cache_map, "<resource_id_2>", cache_entry); | 250 metadata_->AddOrUpdateCacheEntry("<resource_id_2>", cache_entry); |
269 } | 251 } |
270 { | 252 { |
271 FileCacheEntry cache_entry; | 253 FileCacheEntry cache_entry; |
272 cache_entry.set_md5("<md5>"); | 254 cache_entry.set_md5("<md5>"); |
273 cache_entry.set_is_present(true); | 255 cache_entry.set_is_present(true); |
274 cache_entry.set_is_persistent(true); | 256 cache_entry.set_is_persistent(true); |
275 InsertIntoMap(&cache_map, "<resource_id_3>", cache_entry); | 257 metadata_->AddOrUpdateCacheEntry("<resource_id_3>", cache_entry); |
276 } | 258 } |
277 { | 259 { |
278 FileCacheEntry cache_entry; | 260 FileCacheEntry cache_entry; |
279 cache_entry.set_md5("<md5>"); | 261 cache_entry.set_md5("<md5>"); |
280 cache_entry.set_is_present(true); | 262 cache_entry.set_is_present(true); |
281 InsertIntoMap(&cache_map, "<resource_id_4>", cache_entry); | 263 metadata_->AddOrUpdateCacheEntry("<resource_id_4>", cache_entry); |
282 } | 264 } |
283 | 265 |
284 AddAllMapEntries(cache_map); | |
285 metadata_->RemoveTemporaryFiles(); | 266 metadata_->RemoveTemporaryFiles(); |
286 // resource 1 and 4 should be gone, as these are temporary. | 267 // resource 1 and 4 should be gone, as these are temporary. |
287 FileCacheEntry cache_entry; | 268 FileCacheEntry cache_entry; |
288 EXPECT_FALSE(metadata_->GetCacheEntry("<resource_id_1>", "", &cache_entry)); | 269 EXPECT_FALSE(metadata_->GetCacheEntry("<resource_id_1>", "", &cache_entry)); |
289 EXPECT_TRUE(metadata_->GetCacheEntry("<resource_id_2>", "", &cache_entry)); | 270 EXPECT_TRUE(metadata_->GetCacheEntry("<resource_id_2>", "", &cache_entry)); |
290 EXPECT_TRUE(metadata_->GetCacheEntry("<resource_id_3>", "", &cache_entry)); | 271 EXPECT_TRUE(metadata_->GetCacheEntry("<resource_id_3>", "", &cache_entry)); |
291 EXPECT_FALSE(metadata_->GetCacheEntry("<resource_id_4>", "", &cache_entry)); | 272 EXPECT_FALSE(metadata_->GetCacheEntry("<resource_id_4>", "", &cache_entry)); |
292 } | 273 } |
293 | 274 |
294 // Don't use TEST_F, as we don't want SetUp() and TearDown() for this test. | 275 // Don't use TEST_F, as we don't want SetUp() and TearDown() for this test. |
295 TEST(FileCacheMetadataExtraTest, CannotOpenDB) { | 276 TEST(FileCacheMetadataExtraTest, CannotOpenDB) { |
296 // Create nonexistent cache paths, so the initialization fails due to the | 277 // Create nonexistent cache paths, so the initialization fails due to the |
297 // failure of opening the DB. | 278 // failure of opening the DB. |
298 std::vector<base::FilePath> cache_paths = | 279 std::vector<base::FilePath> cache_paths = |
299 FileCache::GetCachePaths( | 280 FileCache::GetCachePaths( |
300 base::FilePath::FromUTF8Unsafe("/somewhere/nonexistent")); | 281 base::FilePath::FromUTF8Unsafe("/somewhere/nonexistent")); |
301 | 282 |
302 scoped_ptr<FileCacheMetadata> metadata = | 283 scoped_ptr<FileCacheMetadata> metadata(new FileCacheMetadata(NULL)); |
303 FileCacheMetadata::CreateCacheMetadata(NULL); | |
304 EXPECT_FALSE(metadata->Initialize(cache_paths)); | 284 EXPECT_FALSE(metadata->Initialize(cache_paths)); |
305 } | 285 } |
306 | 286 |
307 } // namespace internal | 287 } // namespace internal |
308 } // namespace drive | 288 } // namespace drive |
OLD | NEW |