| 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 GURL unused; | 158 GURL unused; |
| 159 return file_system_->UpdateDirectoryWithDocumentFeed( | 159 return file_system_->UpdateDirectoryWithDocumentFeed( |
| 160 list, FROM_SERVER) == base::PLATFORM_FILE_OK; | 160 list, FROM_SERVER) == base::PLATFORM_FILE_OK; |
| 161 } | 161 } |
| 162 | 162 |
| 163 bool RemoveFile(const FilePath& file_path) { | 163 bool RemoveFile(const FilePath& file_path) { |
| 164 return file_system_->RemoveFileFromFileSystem(file_path) == | 164 return file_system_->RemoveFileFromFileSystem(file_path) == |
| 165 base::PLATFORM_FILE_OK; | 165 base::PLATFORM_FILE_OK; |
| 166 } | 166 } |
| 167 | 167 |
| 168 FilePath GetCachePathForFile(GDataFile* file) { |
| 169 return file_system_->GetCacheFilePath( |
| 170 file->resource_id(), |
| 171 file->file_md5(), |
| 172 GDataFileSystem::CACHE_TYPE_TMP, |
| 173 GDataFileSystem::CACHED_FILE_FROM_SERVER); |
| 174 } |
| 175 |
| 168 GDataFileBase* FindFile(const FilePath& file_path) { | 176 GDataFileBase* FindFile(const FilePath& file_path) { |
| 169 ReadOnlyFindFileDelegate search_delegate; | 177 ReadOnlyFindFileDelegate search_delegate; |
| 170 file_system_->FindFileByPathSync(file_path, &search_delegate); | 178 file_system_->FindFileByPathSync(file_path, &search_delegate); |
| 171 return search_delegate.file(); | 179 return search_delegate.file(); |
| 172 } | 180 } |
| 173 | 181 |
| 174 void FindAndTestFilePath(const FilePath& file_path) { | 182 void FindAndTestFilePath(const FilePath& file_path) { |
| 175 GDataFileBase* file = FindFile(file_path); | 183 GDataFileBase* file = FindFile(file_path); |
| 176 ASSERT_TRUE(file) << "File can't be found " << file_path.value(); | 184 ASSERT_TRUE(file) << "File can't be found " << file_path.value(); |
| 177 EXPECT_EQ(file->GetFilePath(), file_path); | 185 EXPECT_EQ(file->GetFilePath(), file_path); |
| 178 } | 186 } |
| 179 | 187 |
| 180 GDataFileBase* FindFileByResource(const std::string& resource) { | 188 GDataFileBase* FindFileByResource(const std::string& resource) { |
| 181 return file_system_->root_->GetFileByResource(resource); | 189 return file_system_->root_->GetFileByResource(resource); |
| 182 } | 190 } |
| 183 | 191 |
| 184 void TestGetCacheFilePath(const std::string& res_id, const std::string& md5, | 192 void TestGetCacheFilePath(const std::string& res_id, const std::string& md5, |
| 185 const std::string& expected_filename) { | 193 const std::string& expected_filename) { |
| 186 FilePath actual_path = file_system_->GetCacheFilePath( | 194 FilePath actual_path = file_system_->GetCacheFilePath( |
| 187 res_id, | 195 res_id, |
| 188 md5, | 196 md5, |
| 189 GDataFileSystem::CACHE_TYPE_TMP, | 197 GDataFileSystem::CACHE_TYPE_TMP, |
| 190 false /* is_local */); | 198 GDataFileSystem::CACHED_FILE_FROM_SERVER); |
| 191 FilePath expected_path = | 199 FilePath expected_path = |
| 192 file_system_->cache_paths_[GDataFileSystem::CACHE_TYPE_TMP]; | 200 file_system_->cache_paths_[GDataFileSystem::CACHE_TYPE_TMP]; |
| 193 expected_path = expected_path.Append(expected_filename); | 201 expected_path = expected_path.Append(expected_filename); |
| 194 EXPECT_EQ(expected_path, actual_path); | 202 EXPECT_EQ(expected_path, actual_path); |
| 195 | 203 |
| 196 FilePath base_name = actual_path.BaseName(); | 204 FilePath base_name = actual_path.BaseName(); |
| 197 | 205 |
| 198 // FilePath::Extension returns ".", so strip it. | 206 // FilePath::Extension returns ".", so strip it. |
| 199 std::string unescaped_md5 = GDataFileBase::UnescapeUtf8FileName( | 207 std::string unescaped_md5 = GDataFileBase::UnescapeUtf8FileName( |
| 200 base_name.Extension().substr(1)); | 208 base_name.Extension().substr(1)); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 EXPECT_EQ(expected_error_, error); | 306 EXPECT_EQ(expected_error_, error); |
| 299 | 307 |
| 300 // Verify cache map. | 308 // Verify cache map. |
| 301 EXPECT_FALSE(file_system_->root_->CacheFileExists(res_id, md5)); | 309 EXPECT_FALSE(file_system_->root_->CacheFileExists(res_id, md5)); |
| 302 | 310 |
| 303 // Verify that no files with $res_id.* exist in dir. | 311 // Verify that no files with $res_id.* exist in dir. |
| 304 FilePath path = file_system_->GetCacheFilePath( | 312 FilePath path = file_system_->GetCacheFilePath( |
| 305 res_id, | 313 res_id, |
| 306 "*", | 314 "*", |
| 307 GDataFileSystem::CACHE_TYPE_TMP, | 315 GDataFileSystem::CACHE_TYPE_TMP, |
| 308 false /* is_local*/); | 316 GDataFileSystem::CACHED_FILE_FROM_SERVER); |
| 309 std::string all_res_id = res_id + FilePath::kExtensionSeparator + "*"; | 317 std::string all_res_id = res_id + FilePath::kExtensionSeparator + "*"; |
| 310 file_util::FileEnumerator traversal(path.DirName(), false, | 318 file_util::FileEnumerator traversal(path.DirName(), false, |
| 311 file_util::FileEnumerator::FILES, | 319 file_util::FileEnumerator::FILES, |
| 312 all_res_id); | 320 all_res_id); |
| 313 EXPECT_TRUE(traversal.Next().empty()); | 321 EXPECT_TRUE(traversal.Next().empty()); |
| 314 } | 322 } |
| 315 | 323 |
| 316 void TestPin(const std::string& res_id, const std::string& md5, | 324 void TestPin(const std::string& res_id, const std::string& md5, |
| 317 base::PlatformFileError expected_error) { | 325 base::PlatformFileError expected_error) { |
| 318 expected_error_ = expected_error; | 326 expected_error_ = expected_error; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 // Determine gdata cache blobs absolute path. | 406 // Determine gdata cache blobs absolute path. |
| 399 // Copy files from data dir to cache dir to act as cached files. | 407 // Copy files from data dir to cache dir to act as cached files. |
| 400 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(init_cache_table); ++i) { | 408 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(init_cache_table); ++i) { |
| 401 // Copy file from data dir to cache dir, naming it per cache files | 409 // Copy file from data dir to cache dir, naming it per cache files |
| 402 // convention. | 410 // convention. |
| 403 FilePath source_path = GetTestFilePath(init_cache_table[i].source); | 411 FilePath source_path = GetTestFilePath(init_cache_table[i].source); |
| 404 FilePath dest_path = file_system_->GetCacheFilePath( | 412 FilePath dest_path = file_system_->GetCacheFilePath( |
| 405 init_cache_table[i].resource, | 413 init_cache_table[i].resource, |
| 406 init_cache_table[i].md5, | 414 init_cache_table[i].md5, |
| 407 GDataFileSystem::CACHE_TYPE_TMP, | 415 GDataFileSystem::CACHE_TYPE_TMP, |
| 408 false /* is_local */); | 416 GDataFileSystem::CACHED_FILE_FROM_SERVER); |
| 409 ASSERT_TRUE(file_util::CopyFile(source_path, dest_path)); | 417 ASSERT_TRUE(file_util::CopyFile(source_path, dest_path)); |
| 410 | 418 |
| 411 // Change mode of cached file. | 419 // Change mode of cached file. |
| 412 struct stat64 stat_buf; | 420 struct stat64 stat_buf; |
| 413 ASSERT_FALSE(stat64(dest_path.value().c_str(), &stat_buf)); | 421 ASSERT_FALSE(stat64(dest_path.value().c_str(), &stat_buf)); |
| 414 ASSERT_FALSE(chmod(dest_path.value().c_str(), | 422 ASSERT_FALSE(chmod(dest_path.value().c_str(), |
| 415 stat_buf.st_mode | | 423 stat_buf.st_mode | |
| 416 init_cache_table[i].mode_bits_to_enable)); | 424 init_cache_table[i].mode_bits_to_enable)); |
| 417 } | 425 } |
| 418 } | 426 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 439 // Verify cache map. | 447 // Verify cache map. |
| 440 EXPECT_TRUE(file_system_->root_->CacheFileExists(res_id, md5)); | 448 EXPECT_TRUE(file_system_->root_->CacheFileExists(res_id, md5)); |
| 441 EXPECT_EQ(expected_status_flags, | 449 EXPECT_EQ(expected_status_flags, |
| 442 file_system_->root_->GetCacheState(res_id, md5)); | 450 file_system_->root_->GetCacheState(res_id, md5)); |
| 443 | 451 |
| 444 // Verify file attributes of actual cache file. | 452 // Verify file attributes of actual cache file. |
| 445 FilePath path = file_system_->GetCacheFilePath( | 453 FilePath path = file_system_->GetCacheFilePath( |
| 446 res_id, | 454 res_id, |
| 447 md5, | 455 md5, |
| 448 GDataFileSystem::CACHE_TYPE_TMP, | 456 GDataFileSystem::CACHE_TYPE_TMP, |
| 449 false /* is_local */); | 457 GDataFileSystem::CACHED_FILE_FROM_SERVER); |
| 450 struct stat64 stat_buf; | 458 struct stat64 stat_buf; |
| 451 EXPECT_EQ(0, stat64(path.value().c_str(), &stat_buf)); | 459 EXPECT_EQ(0, stat64(path.value().c_str(), &stat_buf)); |
| 452 EXPECT_TRUE(stat_buf.st_mode & expected_mode_bits); | 460 EXPECT_TRUE(stat_buf.st_mode & expected_mode_bits); |
| 453 } | 461 } |
| 454 | 462 |
| 455 void RunAllPendingForCache() { | 463 void RunAllPendingForCache() { |
| 456 // Let cache operations run on IO blocking pool. | 464 // Let cache operations run on IO blocking pool. |
| 457 content::BrowserThread::GetBlockingPool()->FlushForTesting(); | 465 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 458 // Let callbacks for cache operations run on UI thread. | 466 // Let callbacks for cache operations run on UI thread. |
| 459 message_loop_.RunAllPending(); | 467 message_loop_.RunAllPending(); |
| (...skipping 1032 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1492 false, // is_exclusive | 1500 false, // is_exclusive |
| 1493 true, // is_recursive | 1501 true, // is_recursive |
| 1494 base::Bind(&CallbackHelper::FileOperationCallback, | 1502 base::Bind(&CallbackHelper::FileOperationCallback, |
| 1495 callback_helper_.get())); | 1503 callback_helper_.get())); |
| 1496 message_loop_.RunAllPending(); // Wait to get our result. | 1504 message_loop_.RunAllPending(); // Wait to get our result. |
| 1497 // TODO(gspencer): Uncomment this when we get a blob that | 1505 // TODO(gspencer): Uncomment this when we get a blob that |
| 1498 // works that can be returned from the mock. | 1506 // works that can be returned from the mock. |
| 1499 // EXPECT_EQ(base::PLATFORM_FILE_OK, callback_helper_->last_error_); | 1507 // EXPECT_EQ(base::PLATFORM_FILE_OK, callback_helper_->last_error_); |
| 1500 } | 1508 } |
| 1501 | 1509 |
| 1502 TEST_F(GDataFileSystemTest, GetFile) { | 1510 TEST_F(GDataFileSystemTest, GetFileFromDownloads) { |
| 1503 LoadRootFeedDocument("root_feed.json"); | 1511 LoadRootFeedDocument("root_feed.json"); |
| 1504 | 1512 |
| 1505 GetFileCallback callback = | 1513 GetFileCallback callback = |
| 1506 base::Bind(&CallbackHelper::GetFileCallback, | 1514 base::Bind(&CallbackHelper::GetFileCallback, |
| 1507 callback_helper_.get()); | 1515 callback_helper_.get()); |
| 1508 | 1516 |
| 1517 FilePath file_in_root(FILE_PATH_LITERAL("gdata/File 1.txt")); |
| 1518 GDataFileBase* file_base = FindFile(file_in_root); |
| 1519 GDataFile* file = file_base->AsGDataFile(); |
| 1520 FilePath downloaded_file = GetCachePathForFile(file); |
| 1521 |
| 1509 EXPECT_CALL(*mock_doc_service_, | 1522 EXPECT_CALL(*mock_doc_service_, |
| 1510 DownloadFile(_, GURL("https://file_content_url/"), _)); | 1523 DownloadFile(file_in_root, |
| 1524 downloaded_file, |
| 1525 GURL("https://file_content_url/"), |
| 1526 _)) |
| 1527 .Times(1); |
| 1511 | 1528 |
| 1512 FilePath file_in_root(FILE_PATH_LITERAL("gdata/File 1.txt")); | |
| 1513 file_system_->GetFile(file_in_root, callback); | 1529 file_system_->GetFile(file_in_root, callback); |
| 1514 message_loop_.RunAllPending(); // Wait to get our result. | 1530 RunAllPendingForCache(); |
| 1531 |
| 1515 EXPECT_EQ(REGULAR_FILE, callback_helper_->file_type_); | 1532 EXPECT_EQ(REGULAR_FILE, callback_helper_->file_type_); |
| 1516 EXPECT_STREQ("file_content_url/", | 1533 EXPECT_STREQ(downloaded_file.value().c_str(), |
| 1517 callback_helper_->download_path_.value().c_str()); | 1534 callback_helper_->download_path_.value().c_str()); |
| 1518 } | 1535 } |
| 1519 | 1536 |
| 1537 TEST_F(GDataFileSystemTest, GetFileFromCache) { |
| 1538 LoadRootFeedDocument("root_feed.json"); |
| 1539 |
| 1540 GetFileCallback callback = |
| 1541 base::Bind(&CallbackHelper::GetFileCallback, |
| 1542 callback_helper_.get()); |
| 1543 |
| 1544 FilePath file_in_root(FILE_PATH_LITERAL("gdata/File 1.txt")); |
| 1545 GDataFileBase* file_base = FindFile(file_in_root); |
| 1546 GDataFile* file = file_base->AsGDataFile(); |
| 1547 FilePath downloaded_file = GetCachePathForFile(file); |
| 1548 |
| 1549 // Store something as cached version of this file. |
| 1550 TestStoreToCache(file->resource_id(), |
| 1551 file->file_md5(), |
| 1552 GetTestFilePath("root_feed.json"), |
| 1553 base::PLATFORM_FILE_OK); |
| 1554 |
| 1555 // Make sure we don't call downloads at all. |
| 1556 EXPECT_CALL(*mock_doc_service_, |
| 1557 DownloadFile(file_in_root, |
| 1558 downloaded_file, |
| 1559 GURL("https://file_content_url/"), |
| 1560 _)) |
| 1561 .Times(0); |
| 1562 |
| 1563 file_system_->GetFile(file_in_root, callback); |
| 1564 RunAllPendingForCache(); |
| 1565 |
| 1566 EXPECT_EQ(REGULAR_FILE, callback_helper_->file_type_); |
| 1567 EXPECT_STREQ(downloaded_file.value().c_str(), |
| 1568 callback_helper_->download_path_.value().c_str()); |
| 1569 } |
| 1570 |
| 1520 TEST_F(GDataFileSystemTest, GetFileForHostedDocument) { | 1571 TEST_F(GDataFileSystemTest, GetFileForHostedDocument) { |
| 1521 LoadRootFeedDocument("root_feed.json"); | 1572 LoadRootFeedDocument("root_feed.json"); |
| 1522 | 1573 |
| 1523 GetFileCallback callback = | 1574 GetFileCallback callback = |
| 1524 base::Bind(&CallbackHelper::GetFileCallback, | 1575 base::Bind(&CallbackHelper::GetFileCallback, |
| 1525 callback_helper_.get()); | 1576 callback_helper_.get()); |
| 1526 | 1577 |
| 1527 FilePath file_in_root(FILE_PATH_LITERAL("gdata/Document 1.gdoc")); | 1578 FilePath file_in_root(FILE_PATH_LITERAL("gdata/Document 1.gdoc")); |
| 1528 GDataFileBase* file = NULL; | 1579 GDataFileBase* file = NULL; |
| 1529 EXPECT_TRUE((file = FindFile(file_in_root)) != NULL); | 1580 EXPECT_TRUE((file = FindFile(file_in_root)) != NULL); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 1558 | 1609 |
| 1559 EXPECT_CALL(*mock_doc_service_, GetAccountMetadata(_)); | 1610 EXPECT_CALL(*mock_doc_service_, GetAccountMetadata(_)); |
| 1560 | 1611 |
| 1561 file_system_->GetAvailableSpace(callback); | 1612 file_system_->GetAvailableSpace(callback); |
| 1562 message_loop_.RunAllPending(); // Wait to get our result. | 1613 message_loop_.RunAllPending(); // Wait to get our result. |
| 1563 EXPECT_EQ(1234, callback_helper_->quota_bytes_used_); | 1614 EXPECT_EQ(1234, callback_helper_->quota_bytes_used_); |
| 1564 EXPECT_EQ(12345, callback_helper_->quota_bytes_total_); | 1615 EXPECT_EQ(12345, callback_helper_->quota_bytes_total_); |
| 1565 } | 1616 } |
| 1566 | 1617 |
| 1567 } // namespace gdata | 1618 } // namespace gdata |
| OLD | NEW |