Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: chrome/browser/chromeos/gdata/drive_cache_metadata_unittest.cc

Issue 10877005: Rename GDataCache* to DriveCache* (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/gdata/gdata_cache_metadata.h" 5 #include "chrome/browser/chromeos/gdata/drive_cache_metadata.h"
6 6
7 #include "base/file_util.h" 7 #include "base/file_util.h"
8 #include "base/scoped_temp_dir.h" 8 #include "base/scoped_temp_dir.h"
9 #include "chrome/browser/chromeos/gdata/drive.pb.h" 9 #include "chrome/browser/chromeos/gdata/drive.pb.h"
10 #include "chrome/browser/chromeos/gdata/gdata_test_util.h" 10 #include "chrome/browser/chromeos/gdata/gdata_test_util.h"
11 #include "chrome/browser/chromeos/gdata/gdata_util.h" 11 #include "chrome/browser/chromeos/gdata/gdata_util.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace gdata { 14 namespace gdata {
15 15
16 class GDataCacheMetadataTest : public testing::Test { 16 class DriveCacheMetadataTest : public testing::Test {
17 public: 17 public:
18 GDataCacheMetadataTest() {} 18 DriveCacheMetadataTest() {}
19 19
20 virtual void SetUp() OVERRIDE { 20 virtual void SetUp() OVERRIDE {
21 // Create cache directories. 21 // Create cache directories.
22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); 22 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
23 cache_paths_ = GDataCache::GetCachePaths(temp_dir_.path()); 23 cache_paths_ = DriveCache::GetCachePaths(temp_dir_.path());
24 ASSERT_TRUE(GDataCache::CreateCacheDirectories(cache_paths_)); 24 ASSERT_TRUE(DriveCache::CreateCacheDirectories(cache_paths_));
25 25
26 persistent_directory_ = cache_paths_[GDataCache::CACHE_TYPE_PERSISTENT]; 26 persistent_directory_ = cache_paths_[DriveCache::CACHE_TYPE_PERSISTENT];
27 tmp_directory_ = cache_paths_[GDataCache::CACHE_TYPE_TMP]; 27 tmp_directory_ = cache_paths_[DriveCache::CACHE_TYPE_TMP];
28 pinned_directory_ = cache_paths_[GDataCache::CACHE_TYPE_PINNED]; 28 pinned_directory_ = cache_paths_[DriveCache::CACHE_TYPE_PINNED];
29 outgoing_directory_ = cache_paths_[GDataCache::CACHE_TYPE_OUTGOING]; 29 outgoing_directory_ = cache_paths_[DriveCache::CACHE_TYPE_OUTGOING];
30 } 30 }
31 31
32 virtual void TearDown() OVERRIDE { 32 virtual void TearDown() OVERRIDE {
33 metadata_.reset(); 33 metadata_.reset();
34 } 34 }
35 35
36 // Sets up the GDataCacheMetadata object. 36 // Sets up the DriveCacheMetadata object.
37 void SetUpCacheMetadata() { 37 void SetUpCacheMetadata() {
38 metadata_ = GDataCacheMetadata::CreateGDataCacheMetadata(NULL).Pass(); 38 metadata_ = DriveCacheMetadata::CreateDriveCacheMetadata(NULL).Pass();
39 metadata_->Initialize(cache_paths_); 39 metadata_->Initialize(cache_paths_);
40 } 40 }
41 41
42 // Sets up the cache directories with various files, including stale 42 // Sets up the cache directories with various files, including stale
43 // symbolic links etc. Should be called before SetUpCacheMetadata(). 43 // symbolic links etc. Should be called before SetUpCacheMetadata().
44 void SetUpCacheWithVariousFiles() { 44 void SetUpCacheWithVariousFiles() {
45 // Create some files in persistent directory. 45 // Create some files in persistent directory.
46 // 46 //
47 CreateFile(persistent_directory_.AppendASCII("id_foo.md5foo")); 47 CreateFile(persistent_directory_.AppendASCII("id_foo.md5foo"));
48 CreateFile(persistent_directory_.AppendASCII("id_bar.local")); 48 CreateFile(persistent_directory_.AppendASCII("id_bar.local"));
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 106
107 // Create an symlink to |target| at |symlink|. 107 // Create an symlink to |target| at |symlink|.
108 void CreateSymbolicLink(const FilePath& target, const FilePath& symlink) { 108 void CreateSymbolicLink(const FilePath& target, const FilePath& symlink) {
109 ASSERT_TRUE(file_util::CreateSymbolicLink(target, symlink)) 109 ASSERT_TRUE(file_util::CreateSymbolicLink(target, symlink))
110 << ": " << target.value() << ": " << symlink.value(); 110 << ": " << target.value() << ": " << symlink.value();
111 } 111 }
112 112
113 protected: 113 protected:
114 // Helper function to insert an item with key |resource_id| into |cache_map|. 114 // Helper function to insert an item with key |resource_id| into |cache_map|.
115 // |md5| and |cache_state| are used to create the value CacheEntry. 115 // |md5| and |cache_state| are used to create the value CacheEntry.
116 void InsertIntoMap(GDataCacheMetadata::CacheMap* cache_map, 116 void InsertIntoMap(DriveCacheMetadata::CacheMap* cache_map,
117 const std::string& resource_id, 117 const std::string& resource_id,
118 const DriveCacheEntry& cache_entry) { 118 const DriveCacheEntry& cache_entry) {
119 cache_map->insert(std::make_pair( 119 cache_map->insert(std::make_pair(
120 resource_id, cache_entry)); 120 resource_id, cache_entry));
121 } 121 }
122 122
123 // Adds all entries in |cache_map| to the metadata storage. 123 // Adds all entries in |cache_map| to the metadata storage.
124 void AddAllMapEntries(const GDataCacheMetadata::CacheMap& cache_map) { 124 void AddAllMapEntries(const DriveCacheMetadata::CacheMap& cache_map) {
125 for (GDataCacheMetadata::CacheMap::const_iterator iter = cache_map.begin(); 125 for (DriveCacheMetadata::CacheMap::const_iterator iter = cache_map.begin();
126 iter != cache_map.end(); ++iter) { 126 iter != cache_map.end(); ++iter) {
127 metadata_->AddOrUpdateCacheEntry(iter->first, iter->second); 127 metadata_->AddOrUpdateCacheEntry(iter->first, iter->second);
128 } 128 }
129 } 129 }
130 130
131 ScopedTempDir temp_dir_; 131 ScopedTempDir temp_dir_;
132 scoped_ptr<GDataCacheMetadata> metadata_; 132 scoped_ptr<DriveCacheMetadata> metadata_;
133 std::vector<FilePath> cache_paths_; 133 std::vector<FilePath> cache_paths_;
134 FilePath persistent_directory_; 134 FilePath persistent_directory_;
135 FilePath tmp_directory_; 135 FilePath tmp_directory_;
136 FilePath pinned_directory_; 136 FilePath pinned_directory_;
137 FilePath outgoing_directory_; 137 FilePath outgoing_directory_;
138 }; 138 };
139 139
140 // Test all the methods of GDataCacheMetadata except for 140 // Test all the methods of DriveCacheMetadata except for
141 // RemoveTemporaryFiles. 141 // RemoveTemporaryFiles.
142 TEST_F(GDataCacheMetadataTest, CacheTest) { 142 TEST_F(DriveCacheMetadataTest, CacheTest) {
143 SetUpCacheMetadata(); 143 SetUpCacheMetadata();
144 144
145 // Save an initial entry. 145 // Save an initial entry.
146 std::string test_resource_id("test_resource_id"); 146 std::string test_resource_id("test_resource_id");
147 std::string test_file_md5("test_file_md5"); 147 std::string test_file_md5("test_file_md5");
148 { 148 {
149 DriveCacheEntry new_cache_entry; 149 DriveCacheEntry new_cache_entry;
150 new_cache_entry.set_md5(test_file_md5); 150 new_cache_entry.set_md5(test_file_md5);
151 new_cache_entry.set_is_present(true); 151 new_cache_entry.set_is_present(true);
152 new_cache_entry.set_is_persistent(true); 152 new_cache_entry.set_is_persistent(true);
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 metadata_->AddOrUpdateCacheEntry(test_resource_id, new_cache_entry); 234 metadata_->AddOrUpdateCacheEntry(test_resource_id, new_cache_entry);
235 } 235 }
236 236
237 // Make sure the values took. 237 // Make sure the values took.
238 ASSERT_TRUE(metadata_->GetCacheEntry( 238 ASSERT_TRUE(metadata_->GetCacheEntry(
239 test_resource_id, test_file_md5, &cache_entry)); 239 test_resource_id, test_file_md5, &cache_entry));
240 EXPECT_EQ(test_file_md5, cache_entry.md5()); 240 EXPECT_EQ(test_file_md5, cache_entry.md5());
241 EXPECT_TRUE(cache_entry.is_present()); 241 EXPECT_TRUE(cache_entry.is_present());
242 } 242 }
243 243
244 TEST_F(GDataCacheMetadataTest, Initialization) { 244 TEST_F(DriveCacheMetadataTest, Initialization) {
245 using file_util::PathExists; 245 using file_util::PathExists;
246 using file_util::IsLink; 246 using file_util::IsLink;
247 SetUpCacheWithVariousFiles(); 247 SetUpCacheWithVariousFiles();
248 248
249 // Some files are removed during cache initialization. Make sure these 249 // Some files are removed during cache initialization. Make sure these
250 // exist beforehand. 250 // exist beforehand.
251 EXPECT_TRUE(PathExists(persistent_directory_.AppendASCII("id_baz.local"))); 251 EXPECT_TRUE(PathExists(persistent_directory_.AppendASCII("id_baz.local")));
252 EXPECT_TRUE(PathExists(persistent_directory_.AppendASCII("id_bad.md5bad"))); 252 EXPECT_TRUE(PathExists(persistent_directory_.AppendASCII("id_bad.md5bad")));
253 EXPECT_TRUE(PathExists(tmp_directory_.AppendASCII("id_quux.local"))); 253 EXPECT_TRUE(PathExists(tmp_directory_.AppendASCII("id_quux.local")));
254 EXPECT_TRUE(PathExists(pinned_directory_.AppendASCII("id_not_symlink"))); 254 EXPECT_TRUE(PathExists(pinned_directory_.AppendASCII("id_not_symlink")));
255 EXPECT_TRUE(IsLink(pinned_directory_.AppendASCII("id_dangling"))); 255 EXPECT_TRUE(IsLink(pinned_directory_.AppendASCII("id_dangling")));
256 EXPECT_TRUE(IsLink(pinned_directory_.AppendASCII("id_outside"))); 256 EXPECT_TRUE(IsLink(pinned_directory_.AppendASCII("id_outside")));
257 EXPECT_TRUE(IsLink(outgoing_directory_.AppendASCII("id_foo"))); 257 EXPECT_TRUE(IsLink(outgoing_directory_.AppendASCII("id_foo")));
258 EXPECT_TRUE(IsLink(persistent_directory_.AppendASCII("id_symlink"))); 258 EXPECT_TRUE(IsLink(persistent_directory_.AppendASCII("id_symlink")));
259 EXPECT_TRUE(IsLink(tmp_directory_.AppendASCII("id_symlink_tmp"))); 259 EXPECT_TRUE(IsLink(tmp_directory_.AppendASCII("id_symlink_tmp")));
260 260
261 SetUpCacheMetadata(); 261 SetUpCacheMetadata();
262 262
263 // Check contents in "persistent" directory. 263 // Check contents in "persistent" directory.
264 // 264 //
265 // "id_foo" is present and pinned. 265 // "id_foo" is present and pinned.
266 DriveCacheEntry cache_entry; 266 DriveCacheEntry cache_entry;
267 ASSERT_TRUE(metadata_->GetCacheEntry("id_foo", "md5foo", &cache_entry)); 267 ASSERT_TRUE(metadata_->GetCacheEntry("id_foo", "md5foo", &cache_entry));
268 EXPECT_EQ("md5foo", cache_entry.md5()); 268 EXPECT_EQ("md5foo", cache_entry.md5());
269 EXPECT_EQ(GDataCache::CACHE_TYPE_PERSISTENT, 269 EXPECT_EQ(DriveCache::CACHE_TYPE_PERSISTENT,
270 GDataCache::GetSubDirectoryType(cache_entry)); 270 DriveCache::GetSubDirectoryType(cache_entry));
271 EXPECT_TRUE(test_util::CacheStatesEqual( 271 EXPECT_TRUE(test_util::CacheStatesEqual(
272 test_util::ToCacheEntry(test_util::TEST_CACHE_STATE_PRESENT | 272 test_util::ToCacheEntry(test_util::TEST_CACHE_STATE_PRESENT |
273 test_util::TEST_CACHE_STATE_PINNED | 273 test_util::TEST_CACHE_STATE_PINNED |
274 test_util::TEST_CACHE_STATE_PERSISTENT), 274 test_util::TEST_CACHE_STATE_PERSISTENT),
275 cache_entry)); 275 cache_entry));
276 EXPECT_TRUE(PathExists(persistent_directory_.AppendASCII("id_foo.md5foo"))); 276 EXPECT_TRUE(PathExists(persistent_directory_.AppendASCII("id_foo.md5foo")));
277 EXPECT_TRUE(PathExists(pinned_directory_.AppendASCII("id_foo"))); 277 EXPECT_TRUE(PathExists(pinned_directory_.AppendASCII("id_foo")));
278 // The invalid symlink in "outgoing" should be removed. 278 // The invalid symlink in "outgoing" should be removed.
279 EXPECT_FALSE(PathExists(outgoing_directory_.AppendASCII("id_foo"))); 279 EXPECT_FALSE(PathExists(outgoing_directory_.AppendASCII("id_foo")));
280 280
281 // "id_bar" is present and dirty. 281 // "id_bar" is present and dirty.
282 ASSERT_TRUE(metadata_->GetCacheEntry("id_bar", "", &cache_entry)); 282 ASSERT_TRUE(metadata_->GetCacheEntry("id_bar", "", &cache_entry));
283 EXPECT_EQ("local", cache_entry.md5()); 283 EXPECT_EQ("local", cache_entry.md5());
284 EXPECT_EQ(GDataCache::CACHE_TYPE_PERSISTENT, 284 EXPECT_EQ(DriveCache::CACHE_TYPE_PERSISTENT,
285 GDataCache::GetSubDirectoryType(cache_entry)); 285 DriveCache::GetSubDirectoryType(cache_entry));
286 EXPECT_TRUE(test_util::CacheStatesEqual( 286 EXPECT_TRUE(test_util::CacheStatesEqual(
287 test_util::ToCacheEntry(test_util::TEST_CACHE_STATE_PRESENT | 287 test_util::ToCacheEntry(test_util::TEST_CACHE_STATE_PRESENT |
288 test_util::TEST_CACHE_STATE_DIRTY | 288 test_util::TEST_CACHE_STATE_DIRTY |
289 test_util::TEST_CACHE_STATE_PERSISTENT), 289 test_util::TEST_CACHE_STATE_PERSISTENT),
290 cache_entry)); 290 cache_entry));
291 EXPECT_TRUE(PathExists(persistent_directory_.AppendASCII("id_bar.local"))); 291 EXPECT_TRUE(PathExists(persistent_directory_.AppendASCII("id_bar.local")));
292 EXPECT_TRUE(PathExists(outgoing_directory_.AppendASCII("id_bar"))); 292 EXPECT_TRUE(PathExists(outgoing_directory_.AppendASCII("id_bar")));
293 293
294 // "id_baz" should be removed during cache initialization. 294 // "id_baz" should be removed during cache initialization.
295 EXPECT_FALSE(metadata_->GetCacheEntry("id_baz", "", &cache_entry)); 295 EXPECT_FALSE(metadata_->GetCacheEntry("id_baz", "", &cache_entry));
296 EXPECT_FALSE(PathExists(persistent_directory_.AppendASCII("id_baz.local"))); 296 EXPECT_FALSE(PathExists(persistent_directory_.AppendASCII("id_baz.local")));
297 297
298 // "id_bad" should be removed during cache initialization. 298 // "id_bad" should be removed during cache initialization.
299 EXPECT_FALSE(metadata_->GetCacheEntry("id_bad", "md5bad", &cache_entry)); 299 EXPECT_FALSE(metadata_->GetCacheEntry("id_bad", "md5bad", &cache_entry));
300 EXPECT_FALSE(PathExists(persistent_directory_.AppendASCII("id_bad.md5bad"))); 300 EXPECT_FALSE(PathExists(persistent_directory_.AppendASCII("id_bad.md5bad")));
301 301
302 // "id_symlink" should be removed during cache initialization. 302 // "id_symlink" should be removed during cache initialization.
303 EXPECT_FALSE(metadata_->GetCacheEntry("id_symlink", "", &cache_entry)); 303 EXPECT_FALSE(metadata_->GetCacheEntry("id_symlink", "", &cache_entry));
304 EXPECT_FALSE(PathExists(persistent_directory_.AppendASCII("id_symlink"))); 304 EXPECT_FALSE(PathExists(persistent_directory_.AppendASCII("id_symlink")));
305 305
306 // Check contents in "tmp" directory. 306 // Check contents in "tmp" directory.
307 // 307 //
308 // "id_qux" is just present in tmp directory. 308 // "id_qux" is just present in tmp directory.
309 ASSERT_TRUE(metadata_->GetCacheEntry("id_qux", "md5qux", &cache_entry)); 309 ASSERT_TRUE(metadata_->GetCacheEntry("id_qux", "md5qux", &cache_entry));
310 EXPECT_EQ("md5qux", cache_entry.md5()); 310 EXPECT_EQ("md5qux", cache_entry.md5());
311 EXPECT_EQ(GDataCache::CACHE_TYPE_TMP, 311 EXPECT_EQ(DriveCache::CACHE_TYPE_TMP,
312 GDataCache::GetSubDirectoryType(cache_entry)); 312 DriveCache::GetSubDirectoryType(cache_entry));
313 EXPECT_TRUE(test_util::CacheStatesEqual( 313 EXPECT_TRUE(test_util::CacheStatesEqual(
314 test_util::ToCacheEntry(test_util::TEST_CACHE_STATE_PRESENT), 314 test_util::ToCacheEntry(test_util::TEST_CACHE_STATE_PRESENT),
315 cache_entry)); 315 cache_entry));
316 EXPECT_TRUE(PathExists(tmp_directory_.AppendASCII("id_qux.md5qux"))); 316 EXPECT_TRUE(PathExists(tmp_directory_.AppendASCII("id_qux.md5qux")));
317 317
318 // "id_quux" should be removed during cache initialization. 318 // "id_quux" should be removed during cache initialization.
319 EXPECT_FALSE(metadata_->GetCacheEntry("id_quux", "md5qux", &cache_entry)); 319 EXPECT_FALSE(metadata_->GetCacheEntry("id_quux", "md5qux", &cache_entry));
320 EXPECT_FALSE(PathExists(pinned_directory_.AppendASCII("id_quux.local"))); 320 EXPECT_FALSE(PathExists(pinned_directory_.AppendASCII("id_quux.local")));
321 321
322 // "id_symlink_tmp" should be removed during cache initialization. 322 // "id_symlink_tmp" should be removed during cache initialization.
(...skipping 16 matching lines...) Expand all
339 339
340 // "id_outside" should be removed during cache initialization. 340 // "id_outside" should be removed during cache initialization.
341 EXPECT_FALSE(metadata_->GetCacheEntry("id_outside", "", &cache_entry)); 341 EXPECT_FALSE(metadata_->GetCacheEntry("id_outside", "", &cache_entry));
342 EXPECT_FALSE(IsLink(pinned_directory_.AppendASCII("id_outside"))); 342 EXPECT_FALSE(IsLink(pinned_directory_.AppendASCII("id_outside")));
343 343
344 // "id_not_symlink" should be removed during cache initialization. 344 // "id_not_symlink" should be removed during cache initialization.
345 EXPECT_FALSE(metadata_->GetCacheEntry("id_not_symlink", "", &cache_entry)); 345 EXPECT_FALSE(metadata_->GetCacheEntry("id_not_symlink", "", &cache_entry));
346 EXPECT_FALSE(IsLink(pinned_directory_.AppendASCII("id_not_symlink"))); 346 EXPECT_FALSE(IsLink(pinned_directory_.AppendASCII("id_not_symlink")));
347 } 347 }
348 348
349 // Test GDataCacheMetadata::RemoveTemporaryFiles. 349 // Test DriveCacheMetadata::RemoveTemporaryFiles.
350 TEST_F(GDataCacheMetadataTest, RemoveTemporaryFilesTest) { 350 TEST_F(DriveCacheMetadataTest, RemoveTemporaryFilesTest) {
351 SetUpCacheMetadata(); 351 SetUpCacheMetadata();
352 352
353 GDataCacheMetadata::CacheMap cache_map; 353 DriveCacheMetadata::CacheMap cache_map;
354 { 354 {
355 DriveCacheEntry cache_entry; 355 DriveCacheEntry cache_entry;
356 cache_entry.set_md5("<md5>"); 356 cache_entry.set_md5("<md5>");
357 cache_entry.set_is_present(true); 357 cache_entry.set_is_present(true);
358 InsertIntoMap(&cache_map, "<resource_id_1>", cache_entry); 358 InsertIntoMap(&cache_map, "<resource_id_1>", cache_entry);
359 } 359 }
360 { 360 {
361 DriveCacheEntry cache_entry; 361 DriveCacheEntry cache_entry;
362 cache_entry.set_md5("<md5>"); 362 cache_entry.set_md5("<md5>");
363 cache_entry.set_is_present(true); 363 cache_entry.set_is_present(true);
(...skipping 18 matching lines...) Expand all
382 metadata_->RemoveTemporaryFiles(); 382 metadata_->RemoveTemporaryFiles();
383 // resource 1 and 4 should be gone, as these are temporary. 383 // resource 1 and 4 should be gone, as these are temporary.
384 DriveCacheEntry cache_entry; 384 DriveCacheEntry cache_entry;
385 EXPECT_FALSE(metadata_->GetCacheEntry("<resource_id_1>", "", &cache_entry)); 385 EXPECT_FALSE(metadata_->GetCacheEntry("<resource_id_1>", "", &cache_entry));
386 EXPECT_TRUE(metadata_->GetCacheEntry("<resource_id_2>", "", &cache_entry)); 386 EXPECT_TRUE(metadata_->GetCacheEntry("<resource_id_2>", "", &cache_entry));
387 EXPECT_TRUE(metadata_->GetCacheEntry("<resource_id_3>", "", &cache_entry)); 387 EXPECT_TRUE(metadata_->GetCacheEntry("<resource_id_3>", "", &cache_entry));
388 EXPECT_FALSE(metadata_->GetCacheEntry("<resource_id_4>", "", &cache_entry)); 388 EXPECT_FALSE(metadata_->GetCacheEntry("<resource_id_4>", "", &cache_entry));
389 } 389 }
390 390
391 } // namespace gdata 391 } // namespace gdata
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/gdata/drive_cache_metadata.cc ('k') | chrome/browser/chromeos/gdata/drive_cache_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698