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

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

Issue 10116044: gdata: Support mounting archive files in GData cache. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: gdata: Support mounting archive files in GData cache. Created 8 years, 8 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
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 <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"
11 #include "base/file_path.h" 11 #include "base/file_path.h"
12 #include "base/file_util.h" 12 #include "base/file_util.h"
13 #include "base/json/json_file_value_serializer.h" 13 #include "base/json/json_file_value_serializer.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop.h" 16 #include "base/message_loop.h"
17 #include "base/path_service.h" 17 #include "base/path_service.h"
18 #include "base/string16.h" 18 #include "base/string16.h"
19 #include "base/string_util.h" 19 #include "base/string_util.h"
20 #include "base/threading/sequenced_worker_pool.h" 20 #include "base/threading/sequenced_worker_pool.h"
21 #include "base/time.h" 21 #include "base/time.h"
22 #include "base/utf_string_conversions.h" 22 #include "base/utf_string_conversions.h"
23 #include "base/values.h" 23 #include "base/values.h"
24 #include "chrome/browser/chromeos/gdata/gdata.pb.h" 24 #include "chrome/browser/chromeos/gdata/gdata.pb.h"
25 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" 25 #include "chrome/browser/chromeos/gdata/gdata_file_system.h"
26 #include "chrome/browser/chromeos/gdata/gdata_parser.h" 26 #include "chrome/browser/chromeos/gdata/gdata_parser.h"
27 #include "chrome/browser/chromeos/gdata/gdata_util.h"
27 #include "chrome/browser/chromeos/gdata/mock_directory_change_observer.h" 28 #include "chrome/browser/chromeos/gdata/mock_directory_change_observer.h"
28 #include "chrome/browser/chromeos/gdata/mock_gdata_documents_service.h" 29 #include "chrome/browser/chromeos/gdata/mock_gdata_documents_service.h"
29 #include "chrome/browser/chromeos/gdata/mock_gdata_sync_client.h" 30 #include "chrome/browser/chromeos/gdata/mock_gdata_sync_client.h"
30 #include "chrome/common/chrome_paths.h" 31 #include "chrome/common/chrome_paths.h"
31 #include "chrome/test/base/testing_profile.h" 32 #include "chrome/test/base/testing_profile.h"
32 #include "content/test/test_browser_thread.h" 33 #include "content/test/test_browser_thread.h"
33 #include "content/public/browser/browser_thread.h" 34 #include "content/public/browser/browser_thread.h"
34 #include "testing/gmock/include/gmock/gmock.h" 35 #include "testing/gmock/include/gmock/gmock.h"
35 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
36 37
37 using ::testing::AnyNumber; 38 using ::testing::AnyNumber;
38 using ::testing::AtLeast; 39 using ::testing::AtLeast;
39 using ::testing::Eq; 40 using ::testing::Eq;
40 using ::testing::IsNull; 41 using ::testing::IsNull;
41 using ::testing::Ne; 42 using ::testing::Ne;
42 using ::testing::NotNull; 43 using ::testing::NotNull;
43 using ::testing::Return; 44 using ::testing::Return;
44 using ::testing::ReturnNull; 45 using ::testing::ReturnNull;
45 using ::testing::_; 46 using ::testing::_;
46 47
47 using base::Value; 48 using base::Value;
48 using base::DictionaryValue; 49 using base::DictionaryValue;
49 using base::ListValue; 50 using base::ListValue;
50 using content::BrowserThread; 51 using content::BrowserThread;
51 52
52 namespace { 53 namespace {
53 54
54 const char kSlash[] = "/";
55 const char kEscapedSlash[] = "\xE2\x88\x95";
56 const char kSymLinkToDevNull[] = "/dev/null"; 55 const char kSymLinkToDevNull[] = "/dev/null";
57 56
58 struct InitialCacheResource { 57 struct InitialCacheResource {
59 const char* source_file; // Source file to be used for cache. 58 const char* source_file; // Source file to be used for cache.
60 const char* resource_id; // Resource id of cache file. 59 const char* resource_id; // Resource id of cache file.
61 const char* md5; // MD5 of cache file. 60 const char* md5; // MD5 of cache file.
62 int cache_state; // Cache state of cache file. 61 int cache_state; // Cache state of cache file.
63 const char* expected_file_extension; // Expected extension of cached file. 62 const char* expected_file_extension; // Expected extension of cached file.
64 // Expected CacheSubDirectoryType of cached file. 63 // Expected CacheSubDirectoryType of cached file.
65 gdata::GDataRootDirectory::CacheSubDirectoryType expected_sub_dir_type; 64 gdata::GDataRootDirectory::CacheSubDirectoryType expected_sub_dir_type;
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 GDataRootDirectory::CACHE_TYPE_TMP, 314 GDataRootDirectory::CACHE_TYPE_TMP,
316 GDataFileSystem::CACHED_FILE_FROM_SERVER); 315 GDataFileSystem::CACHED_FILE_FROM_SERVER);
317 FilePath expected_path = 316 FilePath expected_path =
318 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_TMP]; 317 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_TMP];
319 expected_path = expected_path.Append(expected_filename); 318 expected_path = expected_path.Append(expected_filename);
320 EXPECT_EQ(expected_path, actual_path); 319 EXPECT_EQ(expected_path, actual_path);
321 320
322 FilePath base_name = actual_path.BaseName(); 321 FilePath base_name = actual_path.BaseName();
323 322
324 // FilePath::Extension returns ".", so strip it. 323 // FilePath::Extension returns ".", so strip it.
325 std::string unescaped_md5 = GDataEntry::UnescapeUtf8FileName( 324 std::string unescaped_md5 = util::UnescapeCacheFileName(
326 base_name.Extension().substr(1)); 325 base_name.Extension().substr(1));
327 EXPECT_EQ(md5, unescaped_md5); 326 EXPECT_EQ(md5, unescaped_md5);
328 std::string unescaped_resource_id = GDataEntry::UnescapeUtf8FileName( 327 std::string unescaped_resource_id = util::UnescapeCacheFileName(
329 base_name.RemoveExtension().value()); 328 base_name.RemoveExtension().value());
330 EXPECT_EQ(resource_id, unescaped_resource_id); 329 EXPECT_EQ(resource_id, unescaped_resource_id);
331 } 330 }
332 331
333 void TestStoreToCache( 332 void TestStoreToCache(
334 const std::string& resource_id, 333 const std::string& resource_id,
335 const std::string& md5, 334 const std::string& md5,
336 const FilePath& source_path, 335 const FilePath& source_path,
337 base::PlatformFileError expected_error, 336 base::PlatformFileError expected_error,
338 int expected_cache_state, 337 int expected_cache_state,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 const std::string& md5, 379 const std::string& md5,
381 const FilePath& gdata_file_path, 380 const FilePath& gdata_file_path,
382 const FilePath& cache_file_path) { 381 const FilePath& cache_file_path) {
383 ++num_callback_invocations_; 382 ++num_callback_invocations_;
384 383
385 EXPECT_EQ(expected_error_, error); 384 EXPECT_EQ(expected_error_, error);
386 385
387 if (error == base::PLATFORM_FILE_OK) { 386 if (error == base::PLATFORM_FILE_OK) {
388 // Verify filename of |cache_file_path|. 387 // Verify filename of |cache_file_path|.
389 FilePath base_name = cache_file_path.BaseName(); 388 FilePath base_name = cache_file_path.BaseName();
390 EXPECT_EQ(GDataEntry::EscapeUtf8FileName(resource_id) + 389 EXPECT_EQ(util::EscapeCacheFileName(resource_id) +
391 FilePath::kExtensionSeparator + 390 FilePath::kExtensionSeparator +
392 GDataEntry::EscapeUtf8FileName( 391 util::EscapeCacheFileName(
393 expected_file_extension_.empty() ? 392 expected_file_extension_.empty() ?
394 md5 : expected_file_extension_), 393 md5 : expected_file_extension_),
395 base_name.value()); 394 base_name.value());
396 } else { 395 } else {
397 EXPECT_TRUE(cache_file_path.empty()); 396 EXPECT_TRUE(cache_file_path.empty());
398 } 397 }
399 } 398 }
400 399
401 void TestRemoveFromCache(const std::string& resource_id, 400 void TestRemoveFromCache(const std::string& resource_id,
402 base::PlatformFileError expected_error) { 401 base::PlatformFileError expected_error) {
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 void VerifyMarkDirty(base::PlatformFileError error, 585 void VerifyMarkDirty(base::PlatformFileError error,
587 const std::string& resource_id, 586 const std::string& resource_id,
588 const std::string& md5, 587 const std::string& md5,
589 const FilePath& gdata_file_path, 588 const FilePath& gdata_file_path,
590 const FilePath& cache_file_path) { 589 const FilePath& cache_file_path) {
591 VerifyCacheFileState(error, resource_id, md5); 590 VerifyCacheFileState(error, resource_id, md5);
592 591
593 // Verify filename of |cache_file_path|. 592 // Verify filename of |cache_file_path|.
594 if (error == base::PLATFORM_FILE_OK) { 593 if (error == base::PLATFORM_FILE_OK) {
595 FilePath base_name = cache_file_path.BaseName(); 594 FilePath base_name = cache_file_path.BaseName();
596 EXPECT_EQ(GDataEntry::EscapeUtf8FileName(resource_id) + 595 EXPECT_EQ(util::EscapeCacheFileName(resource_id) +
597 FilePath::kExtensionSeparator + 596 FilePath::kExtensionSeparator +
598 "local", 597 "local",
599 base_name.value()); 598 base_name.value());
600 } else { 599 } else {
601 EXPECT_TRUE(cache_file_path.empty()); 600 EXPECT_TRUE(cache_file_path.empty());
602 } 601 }
603 } 602 }
604 603
605 void TestCommitDirty( 604 void TestCommitDirty(
606 const std::string& resource_id, 605 const std::string& resource_id,
(...skipping 24 matching lines...) Expand all
631 expected_sub_dir_type_ = expected_sub_dir_type; 630 expected_sub_dir_type_ = expected_sub_dir_type;
632 expect_outgoing_symlink_ = false; 631 expect_outgoing_symlink_ = false;
633 632
634 file_system_->ClearDirtyInCache(resource_id, md5, 633 file_system_->ClearDirtyInCache(resource_id, md5,
635 base::Bind(&GDataFileSystemTest::VerifyCacheFileState, 634 base::Bind(&GDataFileSystemTest::VerifyCacheFileState,
636 base::Unretained(this))); 635 base::Unretained(this)));
637 636
638 RunAllPendingForIO(); 637 RunAllPendingForIO();
639 } 638 }
640 639
640 void VerifySetMountedState(const std::string& resource_id,
641 const std::string& md5,
642 bool to_mount,
643 base::PlatformFileError error,
644 const FilePath& file_path) {
645 ++num_callback_invocations_;
646 EXPECT_TRUE(file_util::PathExists(file_path));
647 EXPECT_TRUE(file_path == file_system_->GetCacheFilePath(
648 resource_id,
649 md5,
650 expected_sub_dir_type_,
651 to_mount ?
652 GDataFileSystemInterface::CACHED_FILE_MOUNTED :
653 GDataFileSystemInterface::CACHED_FILE_FROM_SERVER));
654 }
655
656 void TestSetMountedState(
657 const std::string& resource_id,
658 const std::string& md5,
659 const FilePath& file_path,
660 bool to_mount,
661 base::PlatformFileError expected_error,
662 int expected_cache_state,
663 GDataRootDirectory::CacheSubDirectoryType expected_sub_dir_type) {
664 expected_error_ = expected_error;
665 expected_cache_state_ = expected_cache_state;
666 expected_sub_dir_type_ = expected_sub_dir_type;
667 expect_outgoing_symlink_ = false;
668
669 file_system_->SetMountedState(file_path, to_mount,
670 base::Bind(&GDataFileSystemTest::VerifySetMountedState,
671 base::Unretained(this), resource_id, md5, to_mount));
672
673 RunAllPendingForIO();
674 }
675
641 void PrepareForInitCacheTest() { 676 void PrepareForInitCacheTest() {
642 // Create gdata cache sub directories. 677 // Create gdata cache sub directories.
643 ASSERT_TRUE(file_util::CreateDirectory( 678 ASSERT_TRUE(file_util::CreateDirectory(
644 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_PERSISTENT])); 679 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_PERSISTENT]));
645 ASSERT_TRUE(file_util::CreateDirectory( 680 ASSERT_TRUE(file_util::CreateDirectory(
646 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_TMP])); 681 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_TMP]));
647 ASSERT_TRUE(file_util::CreateDirectory( 682 ASSERT_TRUE(file_util::CreateDirectory(
648 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_PINNED])); 683 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_PINNED]));
649 ASSERT_TRUE(file_util::CreateDirectory( 684 ASSERT_TRUE(file_util::CreateDirectory(
650 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_OUTGOING])); 685 file_system_->cache_paths_[GDataRootDirectory::CACHE_TYPE_OUTGOING]));
(...skipping 1305 matching lines...) Expand 10 before | Expand all | Expand 10 after
1956 std::string resource_id("pdf:1a2b"); 1991 std::string resource_id("pdf:1a2b");
1957 std::string md5("abcdef0123456789"); 1992 std::string md5("abcdef0123456789");
1958 TestGetCacheFilePath(resource_id, md5, 1993 TestGetCacheFilePath(resource_id, md5,
1959 resource_id + FilePath::kExtensionSeparator + md5); 1994 resource_id + FilePath::kExtensionSeparator + md5);
1960 EXPECT_EQ(0, num_callback_invocations_); 1995 EXPECT_EQ(0, num_callback_invocations_);
1961 1996
1962 // Use non-alphanumeric characters for resource id, including '.' which is an 1997 // Use non-alphanumeric characters for resource id, including '.' which is an
1963 // extension separator, to test that the characters are escaped and unescaped 1998 // extension separator, to test that the characters are escaped and unescaped
1964 // correctly, and '.' doesn't mess up the filename format and operations. 1999 // correctly, and '.' doesn't mess up the filename format and operations.
1965 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; 2000 resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?";
1966 std::string escaped_resource_id; 2001 std::string escaped_resource_id = util::EscapeCacheFileName(resource_id);
1967 ReplaceChars(resource_id, kSlash, std::string(kEscapedSlash), 2002 std::string escaped_md5 = util::EscapeCacheFileName(md5);
1968 &escaped_resource_id);
1969 std::string escaped_md5;
1970 ReplaceChars(md5, kSlash, std::string(kEscapedSlash), &escaped_md5);
1971 num_callback_invocations_ = 0; 2003 num_callback_invocations_ = 0;
1972 TestGetCacheFilePath(resource_id, md5, 2004 TestGetCacheFilePath(resource_id, md5,
1973 escaped_resource_id + FilePath::kExtensionSeparator + 2005 escaped_resource_id + FilePath::kExtensionSeparator +
1974 escaped_md5); 2006 escaped_md5);
1975 EXPECT_EQ(0, num_callback_invocations_); 2007 EXPECT_EQ(0, num_callback_invocations_);
1976 } 2008 }
1977 2009
1978 TEST_F(GDataFileSystemTest, StoreToCacheSimple) { 2010 TEST_F(GDataFileSystemTest, StoreToCacheSimple) {
1979 EXPECT_CALL(*mock_sync_client_, OnCacheInitialized()).Times(1); 2011 EXPECT_CALL(*mock_sync_client_, OnCacheInitialized()).Times(1);
1980 2012
(...skipping 30 matching lines...) Expand all
2011 GDataRootDirectory::CACHE_TYPE_PERSISTENT : 2043 GDataRootDirectory::CACHE_TYPE_PERSISTENT :
2012 GDataRootDirectory::CACHE_TYPE_TMP, 2044 GDataRootDirectory::CACHE_TYPE_TMP,
2013 GDataFileSystem::CACHED_FILE_FROM_SERVER); 2045 GDataFileSystem::CACHED_FILE_FROM_SERVER);
2014 file_util::FileEnumerator enumerator(path.DirName(), false, 2046 file_util::FileEnumerator enumerator(path.DirName(), false,
2015 file_util::FileEnumerator::FILES, 2047 file_util::FileEnumerator::FILES,
2016 path.BaseName().value()); 2048 path.BaseName().value());
2017 size_t num_files_found = 0; 2049 size_t num_files_found = 0;
2018 for (FilePath current = enumerator.Next(); !current.empty(); 2050 for (FilePath current = enumerator.Next(); !current.empty();
2019 current = enumerator.Next()) { 2051 current = enumerator.Next()) {
2020 ++num_files_found; 2052 ++num_files_found;
2021 EXPECT_EQ(GDataEntry::EscapeUtf8FileName(resource_id) + 2053 EXPECT_EQ(util::EscapeCacheFileName(resource_id) +
2022 FilePath::kExtensionSeparator + 2054 FilePath::kExtensionSeparator +
2023 GDataEntry::EscapeUtf8FileName(md5), 2055 util::EscapeCacheFileName(md5),
2024 current.BaseName().value()); 2056 current.BaseName().value());
2025 } 2057 }
2026 EXPECT_EQ(1U, num_files_found); 2058 EXPECT_EQ(1U, num_files_found);
2027 } 2059 }
2028 2060
2029 TEST_F(GDataFileSystemTest, GetFromCacheSimple) { 2061 TEST_F(GDataFileSystemTest, GetFromCacheSimple) {
2030 EXPECT_CALL(*mock_sync_client_, OnCacheInitialized()).Times(1); 2062 EXPECT_CALL(*mock_sync_client_, OnCacheInitialized()).Times(1);
2031 2063
2032 std::string resource_id("pdf:1a2b"); 2064 std::string resource_id("pdf:1a2b");
2033 std::string md5("abcdef0123456789"); 2065 std::string md5("abcdef0123456789");
(...skipping 981 matching lines...) Expand 10 before | Expand all | Expand 10 after
3015 EXPECT_EQ(12345, callback_helper_->quota_bytes_total_); 3047 EXPECT_EQ(12345, callback_helper_->quota_bytes_total_);
3016 3048
3017 // Verify account meta feed is saved to cache. 3049 // Verify account meta feed is saved to cache.
3018 RunAllPendingForIO(); 3050 RunAllPendingForIO();
3019 FilePath path = file_system_->cache_paths_[ 3051 FilePath path = file_system_->cache_paths_[
3020 GDataRootDirectory::CACHE_TYPE_META].Append( 3052 GDataRootDirectory::CACHE_TYPE_META].Append(
3021 FILE_PATH_LITERAL("account_metadata.json")); 3053 FILE_PATH_LITERAL("account_metadata.json"));
3022 EXPECT_TRUE(file_util::PathExists(path)); 3054 EXPECT_TRUE(file_util::PathExists(path));
3023 } 3055 }
3024 3056
3057 TEST_F(GDataFileSystemTest, MountUnmount) {
3058 EXPECT_CALL(*mock_sync_client_, OnCacheInitialized()).Times(1);
3059
3060 FilePath file_path;
3061 std::string resource_id("pdf:1a2b");
3062 std::string md5("abcdef0123456789");
3063
3064 // First store a file to cache in the tmp subdir.
3065 TestStoreToCache(resource_id, md5, GetTestFilePath("root_feed.json"),
3066 base::PLATFORM_FILE_OK, GDataFile::CACHE_STATE_PRESENT,
3067 GDataRootDirectory::CACHE_TYPE_TMP);
3068
3069 // Mark the file mounted.
3070 num_callback_invocations_ = 0;
3071 file_path = file_system_->GetCacheFilePath(
3072 resource_id, md5,
3073 GDataRootDirectory::CACHE_TYPE_TMP,
3074 GDataFileSystemInterface::CACHED_FILE_FROM_SERVER);
3075 TestSetMountedState(resource_id, md5, file_path, true,
3076 base::PLATFORM_FILE_OK,
3077 GDataFile::CACHE_STATE_PRESENT |
3078 GDataFile::CACHE_STATE_MOUNTED,
3079 GDataRootDirectory::CACHE_TYPE_PERSISTENT);
3080 EXPECT_EQ(1, num_callback_invocations_);
3081 EXPECT_TRUE(CacheEntryExists(resource_id, md5));
3082
3083 // Clear mounted state of the file.
3084 num_callback_invocations_ = 0;
3085 file_path = file_system_->GetCacheFilePath(
3086 resource_id,
3087 md5,
3088 GDataRootDirectory::CACHE_TYPE_PERSISTENT,
3089 GDataFileSystemInterface::CACHED_FILE_MOUNTED);
3090 TestSetMountedState(resource_id, md5, file_path, false,
3091 base::PLATFORM_FILE_OK,
3092 GDataFile::CACHE_STATE_PRESENT,
3093 GDataRootDirectory::CACHE_TYPE_TMP);
3094 EXPECT_EQ(1, num_callback_invocations_);
3095 EXPECT_TRUE(CacheEntryExists(resource_id, md5));
3096
3097 // Try to remove the file.
3098 num_callback_invocations_ = 0;
3099 TestRemoveFromCache(resource_id, base::PLATFORM_FILE_OK);
3100 EXPECT_EQ(1, num_callback_invocations_);
3101 }
3102
3025 } // namespace gdata 3103 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698