| 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/gdata/gdata_cache_metadata.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_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/gdata.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 GDataCacheMetadataTest : public testing::Test { |
| 17 public: | 17 public: |
| 18 GDataCacheMetadataTest() {} | 18 GDataCacheMetadataTest() {} |
| 19 | 19 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(GDataCacheMetadata::CacheMap* cache_map, |
| 117 const std::string& resource_id, | 117 const std::string& resource_id, |
| 118 const GDataCacheEntry& 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 GDataCacheMetadata::CacheMap& cache_map) { |
| 125 for (GDataCacheMetadata::CacheMap::const_iterator iter = cache_map.begin(); | 125 for (GDataCacheMetadata::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 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 139 | 139 |
| 140 // Test all the methods of GDataCacheMetadata except for | 140 // Test all the methods of GDataCacheMetadata except for |
| 141 // RemoveTemporaryFiles. | 141 // RemoveTemporaryFiles. |
| 142 TEST_F(GDataCacheMetadataTest, CacheTest) { | 142 TEST_F(GDataCacheMetadataTest, 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 GDataCacheEntry 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); |
| 153 metadata_->AddOrUpdateCacheEntry(test_resource_id, new_cache_entry); | 153 metadata_->AddOrUpdateCacheEntry(test_resource_id, new_cache_entry); |
| 154 } | 154 } |
| 155 | 155 |
| 156 // Test that the entry can be retrieved. | 156 // Test that the entry can be retrieved. |
| 157 GDataCacheEntry cache_entry; | 157 DriveCacheEntry cache_entry; |
| 158 ASSERT_TRUE(metadata_->GetCacheEntry( | 158 ASSERT_TRUE(metadata_->GetCacheEntry( |
| 159 test_resource_id, test_file_md5, &cache_entry)); | 159 test_resource_id, test_file_md5, &cache_entry)); |
| 160 EXPECT_EQ(test_file_md5, cache_entry.md5()); | 160 EXPECT_EQ(test_file_md5, cache_entry.md5()); |
| 161 EXPECT_TRUE(cache_entry.is_present()); | 161 EXPECT_TRUE(cache_entry.is_present()); |
| 162 EXPECT_TRUE(cache_entry.is_persistent()); | 162 EXPECT_TRUE(cache_entry.is_persistent()); |
| 163 | 163 |
| 164 // Empty md5 should also work. | 164 // Empty md5 should also work. |
| 165 ASSERT_TRUE(metadata_->GetCacheEntry( | 165 ASSERT_TRUE(metadata_->GetCacheEntry( |
| 166 test_resource_id, std::string(), &cache_entry)); | 166 test_resource_id, std::string(), &cache_entry)); |
| 167 EXPECT_EQ(test_file_md5, cache_entry.md5()); | 167 EXPECT_EQ(test_file_md5, cache_entry.md5()); |
| 168 | 168 |
| 169 // resource_id doesn't exist. | 169 // resource_id doesn't exist. |
| 170 EXPECT_FALSE(metadata_->GetCacheEntry( | 170 EXPECT_FALSE(metadata_->GetCacheEntry( |
| 171 "not_found_resource_id", std::string(), &cache_entry)); | 171 "not_found_resource_id", std::string(), &cache_entry)); |
| 172 | 172 |
| 173 // md5 doesn't match. | 173 // md5 doesn't match. |
| 174 EXPECT_FALSE(metadata_->GetCacheEntry( | 174 EXPECT_FALSE(metadata_->GetCacheEntry( |
| 175 test_resource_id, "mismatch_md5", &cache_entry)); | 175 test_resource_id, "mismatch_md5", &cache_entry)); |
| 176 | 176 |
| 177 // Update all attributes. | 177 // Update all attributes. |
| 178 test_file_md5 = "test_file_md5_2"; | 178 test_file_md5 = "test_file_md5_2"; |
| 179 { | 179 { |
| 180 GDataCacheEntry updated_cache_entry; | 180 DriveCacheEntry updated_cache_entry; |
| 181 updated_cache_entry.set_md5(test_file_md5); | 181 updated_cache_entry.set_md5(test_file_md5); |
| 182 updated_cache_entry.set_is_pinned(true); | 182 updated_cache_entry.set_is_pinned(true); |
| 183 metadata_->AddOrUpdateCacheEntry(test_resource_id, updated_cache_entry); | 183 metadata_->AddOrUpdateCacheEntry(test_resource_id, updated_cache_entry); |
| 184 } | 184 } |
| 185 | 185 |
| 186 // Make sure the values took. | 186 // Make sure the values took. |
| 187 ASSERT_TRUE(metadata_->GetCacheEntry( | 187 ASSERT_TRUE(metadata_->GetCacheEntry( |
| 188 test_resource_id, test_file_md5, &cache_entry)); | 188 test_resource_id, test_file_md5, &cache_entry)); |
| 189 EXPECT_EQ(test_file_md5, cache_entry.md5()); | 189 EXPECT_EQ(test_file_md5, cache_entry.md5()); |
| 190 EXPECT_TRUE(cache_entry.is_pinned()); | 190 EXPECT_TRUE(cache_entry.is_pinned()); |
| 191 | 191 |
| 192 // Empty m5 should work. | 192 // Empty m5 should work. |
| 193 ASSERT_TRUE(metadata_->GetCacheEntry( | 193 ASSERT_TRUE(metadata_->GetCacheEntry( |
| 194 test_resource_id, std::string(), &cache_entry)); | 194 test_resource_id, std::string(), &cache_entry)); |
| 195 EXPECT_EQ(test_file_md5, cache_entry.md5()); | 195 EXPECT_EQ(test_file_md5, cache_entry.md5()); |
| 196 | 196 |
| 197 // Test dirty cache. | 197 // Test dirty cache. |
| 198 test_file_md5 = "test_file_md5_3"; | 198 test_file_md5 = "test_file_md5_3"; |
| 199 { | 199 { |
| 200 GDataCacheEntry new_cache_entry; | 200 DriveCacheEntry new_cache_entry; |
| 201 new_cache_entry.set_md5(test_file_md5); | 201 new_cache_entry.set_md5(test_file_md5); |
| 202 new_cache_entry.set_is_dirty(true); | 202 new_cache_entry.set_is_dirty(true); |
| 203 metadata_->AddOrUpdateCacheEntry(test_resource_id, new_cache_entry); | 203 metadata_->AddOrUpdateCacheEntry(test_resource_id, new_cache_entry); |
| 204 } | 204 } |
| 205 | 205 |
| 206 // Make sure the values took. | 206 // Make sure the values took. |
| 207 ASSERT_TRUE(metadata_->GetCacheEntry( | 207 ASSERT_TRUE(metadata_->GetCacheEntry( |
| 208 test_resource_id, test_file_md5, &cache_entry)); | 208 test_resource_id, test_file_md5, &cache_entry)); |
| 209 EXPECT_EQ(test_file_md5, cache_entry.md5()); | 209 EXPECT_EQ(test_file_md5, cache_entry.md5()); |
| 210 EXPECT_TRUE(cache_entry.is_dirty()); | 210 EXPECT_TRUE(cache_entry.is_dirty()); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 221 | 221 |
| 222 // Remove the entry. | 222 // Remove the entry. |
| 223 metadata_->RemoveCacheEntry(test_resource_id); | 223 metadata_->RemoveCacheEntry(test_resource_id); |
| 224 EXPECT_FALSE(metadata_->GetCacheEntry( | 224 EXPECT_FALSE(metadata_->GetCacheEntry( |
| 225 test_resource_id, std::string(), &cache_entry)); | 225 test_resource_id, std::string(), &cache_entry)); |
| 226 | 226 |
| 227 // Add another one. | 227 // Add another one. |
| 228 test_resource_id = "test_resource_id_2"; | 228 test_resource_id = "test_resource_id_2"; |
| 229 test_file_md5 = "test_file_md5_4"; | 229 test_file_md5 = "test_file_md5_4"; |
| 230 { | 230 { |
| 231 GDataCacheEntry new_cache_entry; | 231 DriveCacheEntry new_cache_entry; |
| 232 new_cache_entry.set_md5(test_file_md5); | 232 new_cache_entry.set_md5(test_file_md5); |
| 233 new_cache_entry.set_is_present(true); | 233 new_cache_entry.set_is_present(true); |
| 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()); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 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 GDataCacheEntry 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(GDataCache::CACHE_TYPE_PERSISTENT, |
| 270 GDataCache::GetSubDirectoryType(cache_entry)); | 270 GDataCache::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"))); |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 GDataCacheMetadata::RemoveTemporaryFiles. |
| 350 TEST_F(GDataCacheMetadataTest, RemoveTemporaryFilesTest) { | 350 TEST_F(GDataCacheMetadataTest, RemoveTemporaryFilesTest) { |
| 351 SetUpCacheMetadata(); | 351 SetUpCacheMetadata(); |
| 352 | 352 |
| 353 GDataCacheMetadata::CacheMap cache_map; | 353 GDataCacheMetadata::CacheMap cache_map; |
| 354 { | 354 { |
| 355 GDataCacheEntry 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 GDataCacheEntry 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); |
| 364 cache_entry.set_is_persistent(true); | 364 cache_entry.set_is_persistent(true); |
| 365 InsertIntoMap(&cache_map, "<resource_id_2>", cache_entry); | 365 InsertIntoMap(&cache_map, "<resource_id_2>", cache_entry); |
| 366 } | 366 } |
| 367 { | 367 { |
| 368 GDataCacheEntry cache_entry; | 368 DriveCacheEntry cache_entry; |
| 369 cache_entry.set_md5("<md5>"); | 369 cache_entry.set_md5("<md5>"); |
| 370 cache_entry.set_is_present(true); | 370 cache_entry.set_is_present(true); |
| 371 cache_entry.set_is_persistent(true); | 371 cache_entry.set_is_persistent(true); |
| 372 InsertIntoMap(&cache_map, "<resource_id_3>", cache_entry); | 372 InsertIntoMap(&cache_map, "<resource_id_3>", cache_entry); |
| 373 } | 373 } |
| 374 { | 374 { |
| 375 GDataCacheEntry cache_entry; | 375 DriveCacheEntry cache_entry; |
| 376 cache_entry.set_md5("<md5>"); | 376 cache_entry.set_md5("<md5>"); |
| 377 cache_entry.set_is_present(true); | 377 cache_entry.set_is_present(true); |
| 378 InsertIntoMap(&cache_map, "<resource_id_4>", cache_entry); | 378 InsertIntoMap(&cache_map, "<resource_id_4>", cache_entry); |
| 379 } | 379 } |
| 380 | 380 |
| 381 AddAllMapEntries(cache_map); | 381 AddAllMapEntries(cache_map); |
| 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 GDataCacheEntry 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 |
| OLD | NEW |