OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/resource_metadata_storage.h" | 5 #include "chrome/browser/chromeos/drive/resource_metadata_storage.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
149 keys.push_back("entry2"); | 149 keys.push_back("entry2"); |
150 keys.push_back("entry3"); | 150 keys.push_back("entry3"); |
151 keys.push_back("entry4"); | 151 keys.push_back("entry4"); |
152 | 152 |
153 for (size_t i = 0; i < keys.size(); ++i) { | 153 for (size_t i = 0; i < keys.size(); ++i) { |
154 ResourceEntry entry; | 154 ResourceEntry entry; |
155 entry.set_local_id(keys[i]); | 155 entry.set_local_id(keys[i]); |
156 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); | 156 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); |
157 } | 157 } |
158 | 158 |
159 // Insert some cache entries. | |
160 std::map<std::string, FileCacheEntry> cache_entries; | |
161 cache_entries[keys[0]].set_md5("aaaaaa"); | |
162 cache_entries[keys[1]].set_md5("bbbbbb"); | |
163 for (std::map<std::string, FileCacheEntry>::iterator it = | |
164 cache_entries.begin(); it != cache_entries.end(); ++it) | |
165 EXPECT_EQ(FILE_ERROR_OK, storage_->PutCacheEntry(it->first, it->second)); | |
166 | |
167 // Iterate and check the result. | 159 // Iterate and check the result. |
168 std::map<std::string, ResourceEntry> found_entries; | 160 std::map<std::string, ResourceEntry> found_entries; |
169 scoped_ptr<ResourceMetadataStorage::Iterator> it = storage_->GetIterator(); | 161 scoped_ptr<ResourceMetadataStorage::Iterator> it = storage_->GetIterator(); |
170 ASSERT_TRUE(it); | 162 ASSERT_TRUE(it); |
171 for (; !it->IsAtEnd(); it->Advance()) { | 163 for (; !it->IsAtEnd(); it->Advance()) { |
172 const ResourceEntry& entry = it->GetValue(); | 164 const ResourceEntry& entry = it->GetValue(); |
173 found_entries[it->GetID()] = entry; | 165 found_entries[it->GetID()] = entry; |
174 } | 166 } |
175 EXPECT_FALSE(it->HasError()); | 167 EXPECT_FALSE(it->HasError()); |
176 | 168 |
177 EXPECT_EQ(keys.size(), found_entries.size()); | 169 EXPECT_EQ(keys.size(), found_entries.size()); |
178 for (size_t i = 0; i < keys.size(); ++i) | 170 for (size_t i = 0; i < keys.size(); ++i) |
179 EXPECT_EQ(1U, found_entries.count(keys[i])); | 171 EXPECT_EQ(1U, found_entries.count(keys[i])); |
180 } | 172 } |
181 | 173 |
182 TEST_F(ResourceMetadataStorageTest, PutCacheEntry) { | |
183 FileCacheEntry entry; | |
184 const std::string key1 = "abcdefg"; | |
185 const std::string key2 = "abcd"; | |
186 const std::string md5_1 = "foo"; | |
187 const std::string md5_2 = "bar"; | |
188 | |
189 // Put cache entries. | |
190 entry.set_md5(md5_1); | |
191 EXPECT_EQ(FILE_ERROR_OK, storage_->PutCacheEntry(key1, entry)); | |
192 entry.set_md5(md5_2); | |
193 EXPECT_EQ(FILE_ERROR_OK, storage_->PutCacheEntry(key2, entry)); | |
194 | |
195 // Get cache entires. | |
196 EXPECT_EQ(FILE_ERROR_OK, storage_->GetCacheEntry(key1, &entry)); | |
197 EXPECT_EQ(md5_1, entry.md5()); | |
198 EXPECT_EQ(FILE_ERROR_OK, storage_->GetCacheEntry(key2, &entry)); | |
199 EXPECT_EQ(md5_2, entry.md5()); | |
200 | |
201 // Remove cache entries. | |
202 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveCacheEntry(key1)); | |
203 EXPECT_EQ(FILE_ERROR_NOT_FOUND, storage_->GetCacheEntry(key1, &entry)); | |
204 | |
205 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveCacheEntry(key2)); | |
206 EXPECT_EQ(FILE_ERROR_NOT_FOUND, storage_->GetCacheEntry(key2, &entry)); | |
207 } | |
208 | |
209 TEST_F(ResourceMetadataStorageTest, CacheEntryIterator) { | |
210 // Prepare data. | |
211 std::map<std::string, FileCacheEntry> entries; | |
212 FileCacheEntry cache_entry; | |
213 | |
214 cache_entry.set_md5("aA"); | |
215 entries["entry1"] = cache_entry; | |
216 cache_entry.set_md5("bB"); | |
217 entries["entry2"] = cache_entry; | |
218 cache_entry.set_md5("cC"); | |
219 entries["entry3"] = cache_entry; | |
220 cache_entry.set_md5("dD"); | |
221 entries["entry4"] = cache_entry; | |
222 | |
223 for (std::map<std::string, FileCacheEntry>::iterator it = entries.begin(); | |
224 it != entries.end(); ++it) | |
225 EXPECT_EQ(FILE_ERROR_OK, storage_->PutCacheEntry(it->first, it->second)); | |
226 | |
227 // Insert some dummy entries. | |
228 ResourceEntry entry; | |
229 entry.set_local_id("entry1"); | |
230 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); | |
231 entry.set_local_id("entry2"); | |
232 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); | |
233 | |
234 // Iterate and check the result. | |
235 scoped_ptr<ResourceMetadataStorage::CacheEntryIterator> it = | |
236 storage_->GetCacheEntryIterator(); | |
237 ASSERT_TRUE(it); | |
238 size_t num_entries = 0; | |
239 for (; !it->IsAtEnd(); it->Advance()) { | |
240 EXPECT_EQ(1U, entries.count(it->GetID())); | |
241 EXPECT_EQ(entries[it->GetID()].md5(), it->GetValue().md5()); | |
242 ++num_entries; | |
243 } | |
244 EXPECT_FALSE(it->HasError()); | |
245 EXPECT_EQ(entries.size(), num_entries); | |
246 } | |
247 | |
248 TEST_F(ResourceMetadataStorageTest, GetIdByResourceId) { | 174 TEST_F(ResourceMetadataStorageTest, GetIdByResourceId) { |
249 const std::string local_id = "local_id"; | 175 const std::string local_id = "local_id"; |
250 const std::string resource_id = "resource_id"; | 176 const std::string resource_id = "resource_id"; |
251 | 177 |
252 // Resource ID to local ID mapping is not stored yet. | 178 // Resource ID to local ID mapping is not stored yet. |
253 std::string id; | 179 std::string id; |
254 EXPECT_EQ(FILE_ERROR_NOT_FOUND, | 180 EXPECT_EQ(FILE_ERROR_NOT_FOUND, |
255 storage_->GetIdByResourceId(resource_id, &id)); | 181 storage_->GetIdByResourceId(resource_id, &id)); |
256 | 182 |
257 // Put an entry with the resource ID. | 183 // Put an entry with the resource ID. |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 for (size_t i = 0; i < children_name_id.size(); ++i) { | 227 for (size_t i = 0; i < children_name_id.size(); ++i) { |
302 for (size_t j = 0; j < children_name_id[i].size(); ++j) { | 228 for (size_t j = 0; j < children_name_id[i].size(); ++j) { |
303 ResourceEntry entry; | 229 ResourceEntry entry; |
304 entry.set_local_id(children_name_id[i][j].second); | 230 entry.set_local_id(children_name_id[i][j].second); |
305 entry.set_parent_local_id(parents_id[i]); | 231 entry.set_parent_local_id(parents_id[i]); |
306 entry.set_base_name(children_name_id[i][j].first); | 232 entry.set_base_name(children_name_id[i][j].first); |
307 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); | 233 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); |
308 } | 234 } |
309 } | 235 } |
310 | 236 |
311 // Put some dummy cache entries. | |
312 for (size_t i = 0; i < arraysize(parents_id); ++i) { | |
313 FileCacheEntry cache_entry; | |
314 EXPECT_EQ(FILE_ERROR_OK, | |
315 storage_->PutCacheEntry(parents_id[i], cache_entry)); | |
316 } | |
317 | |
318 // Try to get children. | 237 // Try to get children. |
319 for (size_t i = 0; i < children_name_id.size(); ++i) { | 238 for (size_t i = 0; i < children_name_id.size(); ++i) { |
320 std::vector<std::string> children; | 239 std::vector<std::string> children; |
321 storage_->GetChildren(parents_id[i], &children); | 240 storage_->GetChildren(parents_id[i], &children); |
322 EXPECT_EQ(children_name_id[i].size(), children.size()); | 241 EXPECT_EQ(children_name_id[i].size(), children.size()); |
323 for (size_t j = 0; j < children_name_id[i].size(); ++j) { | 242 for (size_t j = 0; j < children_name_id[i].size(); ++j) { |
324 EXPECT_EQ(1, std::count(children.begin(), | 243 EXPECT_EQ(1, std::count(children.begin(), |
325 children.end(), | 244 children.end(), |
326 children_name_id[i][j].second)); | 245 children_name_id[i][j].second)); |
327 } | 246 } |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
458 TEST_F(ResourceMetadataStorageTest, IncompatibleDB_Unknown) { | 377 TEST_F(ResourceMetadataStorageTest, IncompatibleDB_Unknown) { |
459 const int64 kLargestChangestamp = 1234567890; | 378 const int64 kLargestChangestamp = 1234567890; |
460 const std::string key1 = "abcd"; | 379 const std::string key1 = "abcd"; |
461 | 380 |
462 // Put some data. | 381 // Put some data. |
463 EXPECT_EQ(FILE_ERROR_OK, | 382 EXPECT_EQ(FILE_ERROR_OK, |
464 storage_->SetLargestChangestamp(kLargestChangestamp)); | 383 storage_->SetLargestChangestamp(kLargestChangestamp)); |
465 ResourceEntry entry; | 384 ResourceEntry entry; |
466 entry.set_local_id(key1); | 385 entry.set_local_id(key1); |
467 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); | 386 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); |
468 FileCacheEntry cache_entry; | |
469 EXPECT_EQ(FILE_ERROR_OK, storage_->PutCacheEntry(key1, cache_entry)); | |
470 | 387 |
471 // Set newer version, upgrade and reopen DB. | 388 // Set newer version, upgrade and reopen DB. |
472 SetDBVersion(ResourceMetadataStorage::kDBVersion + 1); | 389 SetDBVersion(ResourceMetadataStorage::kDBVersion + 1); |
473 storage_.reset(); | 390 storage_.reset(); |
474 EXPECT_FALSE(ResourceMetadataStorage::UpgradeOldDB( | 391 EXPECT_FALSE(ResourceMetadataStorage::UpgradeOldDB( |
475 temp_dir_.path(), base::Bind(&util::CanonicalizeResourceId))); | 392 temp_dir_.path(), base::Bind(&util::CanonicalizeResourceId))); |
476 storage_.reset(new ResourceMetadataStorage( | 393 storage_.reset(new ResourceMetadataStorage( |
477 temp_dir_.path(), base::MessageLoopProxy::current().get())); | 394 temp_dir_.path(), base::MessageLoopProxy::current().get())); |
478 ASSERT_TRUE(storage_->Initialize()); | 395 ASSERT_TRUE(storage_->Initialize()); |
479 | 396 |
480 // Data is erased because of the incompatible version. | 397 // Data is erased because of the incompatible version. |
481 int64 largest_changestamp = 0; | 398 int64 largest_changestamp = 0; |
482 EXPECT_EQ(FILE_ERROR_OK, | 399 EXPECT_EQ(FILE_ERROR_OK, |
483 storage_->GetLargestChangestamp(&largest_changestamp)); | 400 storage_->GetLargestChangestamp(&largest_changestamp)); |
484 EXPECT_EQ(0, largest_changestamp); | 401 EXPECT_EQ(0, largest_changestamp); |
485 EXPECT_EQ(FILE_ERROR_NOT_FOUND, storage_->GetEntry(key1, &entry)); | 402 EXPECT_EQ(FILE_ERROR_NOT_FOUND, storage_->GetEntry(key1, &entry)); |
486 EXPECT_EQ(FILE_ERROR_NOT_FOUND, storage_->GetCacheEntry(key1, &cache_entry)); | |
487 } | 403 } |
488 | 404 |
489 TEST_F(ResourceMetadataStorageTest, WrongPath) { | 405 TEST_F(ResourceMetadataStorageTest, WrongPath) { |
490 // Create a file. | 406 // Create a file. |
491 base::FilePath path; | 407 base::FilePath path; |
492 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path)); | 408 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &path)); |
493 | 409 |
494 storage_.reset(new ResourceMetadataStorage( | 410 storage_.reset(new ResourceMetadataStorage( |
495 path, base::MessageLoopProxy::current().get())); | 411 path, base::MessageLoopProxy::current().get())); |
496 // Cannot initialize DB beacause the path does not point a directory. | 412 // Cannot initialize DB beacause the path does not point a directory. |
497 ASSERT_FALSE(storage_->Initialize()); | 413 ASSERT_FALSE(storage_->Initialize()); |
498 } | 414 } |
499 | 415 |
500 TEST_F(ResourceMetadataStorageTest, RecoverCacheEntriesFromTrashedResourceMap) { | 416 TEST_F(ResourceMetadataStorageTest, RecoverCacheEntriesFromTrashedResourceMap) { |
501 // Put some cache entries. | |
502 FileCacheEntry cache_entry; | |
503 cache_entry.set_md5("md5_foo"); | |
504 EXPECT_EQ(FILE_ERROR_OK, storage_->PutCacheEntry("id_foo", cache_entry)); | |
505 cache_entry.set_md5("md5_bar"); | |
506 cache_entry.set_is_dirty(true); | |
507 EXPECT_EQ(FILE_ERROR_OK, storage_->PutCacheEntry("id_bar", cache_entry)); | |
508 | |
509 // Put entry with id_foo. | 417 // Put entry with id_foo. |
510 ResourceEntry entry; | 418 ResourceEntry entry; |
511 entry.set_local_id("id_foo"); | 419 entry.set_local_id("id_foo"); |
512 entry.set_base_name("foo"); | 420 entry.set_base_name("foo"); |
513 entry.set_title("foo"); | 421 entry.set_title("foo"); |
| 422 entry.mutable_file_specific_info()->mutable_cache_state()->set_md5("md5_foo"); |
514 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); | 423 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); |
515 | 424 |
516 // Put entry with id_bar as a id_foo's child. | 425 // Put entry with id_bar as a id_foo's child. |
517 entry.set_local_id("id_bar"); | 426 entry.set_local_id("id_bar"); |
518 entry.set_parent_local_id("id_foo"); | 427 entry.set_parent_local_id("id_foo"); |
519 entry.set_base_name("bar"); | 428 entry.set_base_name("bar"); |
520 entry.set_title("bar"); | 429 entry.set_title("bar"); |
| 430 entry.mutable_file_specific_info()->mutable_cache_state()->set_md5("md5_bar"); |
| 431 entry.mutable_file_specific_info()->mutable_cache_state()->set_is_dirty(true); |
521 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); | 432 EXPECT_EQ(FILE_ERROR_OK, storage_->PutEntry(entry)); |
522 | 433 |
523 // Remove parent-child relationship to make the DB invalid. | 434 // Remove parent-child relationship to make the DB invalid. |
524 RemoveChild("id_foo", "bar"); | 435 RemoveChild("id_foo", "bar"); |
525 EXPECT_FALSE(CheckValidity()); | 436 EXPECT_FALSE(CheckValidity()); |
526 | 437 |
527 // Reopen. This should result in trashing the DB. | 438 // Reopen. This should result in trashing the DB. |
528 storage_.reset(new ResourceMetadataStorage( | 439 storage_.reset(new ResourceMetadataStorage( |
529 temp_dir_.path(), base::MessageLoopProxy::current().get())); | 440 temp_dir_.path(), base::MessageLoopProxy::current().get())); |
530 ASSERT_TRUE(storage_->Initialize()); | 441 ASSERT_TRUE(storage_->Initialize()); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
589 EXPECT_FALSE(CheckValidity()); | 500 EXPECT_FALSE(CheckValidity()); |
590 PutChild(key2, name2, key3); | 501 PutChild(key2, name2, key3); |
591 EXPECT_FALSE(CheckValidity()); | 502 EXPECT_FALSE(CheckValidity()); |
592 | 503 |
593 // Fix up the relationship between key2 and key3. | 504 // Fix up the relationship between key2 and key3. |
594 RemoveChild(key2, name2); | 505 RemoveChild(key2, name2); |
595 EXPECT_FALSE(CheckValidity()); | 506 EXPECT_FALSE(CheckValidity()); |
596 PutChild(key2, name3, key3); | 507 PutChild(key2, name3, key3); |
597 EXPECT_TRUE(CheckValidity()); | 508 EXPECT_TRUE(CheckValidity()); |
598 | 509 |
599 // Add some cache entries. | |
600 FileCacheEntry cache_entry; | |
601 EXPECT_EQ(FILE_ERROR_OK, storage_->PutCacheEntry(key1, cache_entry)); | |
602 EXPECT_EQ(FILE_ERROR_OK, storage_->PutCacheEntry(key2, cache_entry)); | |
603 | |
604 // Remove key2. | 510 // Remove key2. |
605 RemoveChild(key1, name2); | 511 RemoveChild(key1, name2); |
606 EXPECT_FALSE(CheckValidity()); | 512 EXPECT_FALSE(CheckValidity()); |
607 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveEntry(key2)); | 513 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveEntry(key2)); |
608 EXPECT_FALSE(CheckValidity()); | 514 EXPECT_FALSE(CheckValidity()); |
609 | 515 |
610 // Remove key3. | 516 // Remove key3. |
611 RemoveChild(key2, name3); | 517 RemoveChild(key2, name3); |
612 EXPECT_FALSE(CheckValidity()); | 518 EXPECT_FALSE(CheckValidity()); |
613 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveEntry(key3)); | 519 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveEntry(key3)); |
614 EXPECT_TRUE(CheckValidity()); | 520 EXPECT_TRUE(CheckValidity()); |
615 | 521 |
616 // Remove key1. | 522 // Remove key1. |
617 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveEntry(key1)); | 523 EXPECT_EQ(FILE_ERROR_OK, storage_->RemoveEntry(key1)); |
618 EXPECT_TRUE(CheckValidity()); | 524 EXPECT_TRUE(CheckValidity()); |
619 } | 525 } |
620 | 526 |
621 } // namespace internal | 527 } // namespace internal |
622 } // namespace drive | 528 } // namespace drive |
OLD | NEW |