[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 5 | #include "chrome/browser/chromeos/drive/drive_cache.h" |
[email protected] | 21ed9907 | 2012-09-13 01:49:33 | [diff] [blame] | 6 | |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame^] | 7 | #include <string> |
[email protected] | 79c3752d | 2012-07-17 12:10:08 | [diff] [blame] | 8 | #include <vector> |
| 9 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 10 | #include "base/file_util.h" |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame^] | 11 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 12 | #include "base/message_loop.h" |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 13 | #include "base/threading/sequenced_worker_pool.h" |
[email protected] | 15de814 | 2012-10-11 06:00:54 | [diff] [blame] | 14 | #include "chrome/browser/chromeos/drive/drive.pb.h" |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 15 | #include "chrome/browser/chromeos/drive/fake_free_disk_space_getter.h" |
[email protected] | 4fa2fd5d | 2013-04-26 03:42:52 | [diff] [blame] | 16 | #include "chrome/browser/chromeos/drive/file_system_util.h" |
[email protected] | bd547ad8 | 2013-04-26 04:52:29 | [diff] [blame] | 17 | #include "chrome/browser/chromeos/drive/mock_cache_observer.h" |
[email protected] | 92931d7 | 2013-04-24 05:16:15 | [diff] [blame] | 18 | #include "chrome/browser/chromeos/drive/test_util.h" |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 19 | #include "chrome/browser/google_apis/test_util.h" |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 20 | #include "content/public/test/test_browser_thread.h" |
| 21 | #include "testing/gmock/include/gmock/gmock.h" |
| 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 24 | using ::testing::StrictMock; |
| 25 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 26 | namespace drive { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 27 | namespace { |
| 28 | |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 29 | struct TestCacheResource { |
| 30 | const char* source_file; |
| 31 | const char* resource_id; |
| 32 | const char* md5; |
| 33 | bool is_pinned; |
| 34 | bool is_dirty; |
| 35 | } const test_cache_resources[] = { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 36 | // Cache resource in tmp dir, i.e. not pinned or dirty. |
[email protected] | 21ed9907 | 2012-09-13 01:49:33 | [diff] [blame] | 37 | { "gdata/root_feed.json", "tmp:resource_id", "md5_tmp_alphanumeric", |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 38 | false, false }, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 39 | // Cache resource in tmp dir, i.e. not pinned or dirty, with resource_id |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 40 | // containing non-alphanumeric characters. |
[email protected] | 1669647 | 2013-04-22 05:42:21 | [diff] [blame] | 41 | { "gdata/empty_feed.json", "tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 42 | "md5_tmp_non_alphanumeric", false, false }, |
| 43 | // Cache resource that is pinned and persistent. |
[email protected] | 1669647 | 2013-04-22 05:42:21 | [diff] [blame] | 44 | { "gdata/directory_entry.json", "pinned:existing", "md5_pinned_existing", |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 45 | true, false }, |
| 46 | // Cache resource with a non-existent source file that is pinned. |
| 47 | { "", "pinned:non-existent", "md5_pinned_non_existent", true, false }, |
| 48 | // Cache resource that is dirty. |
[email protected] | 21ed9907 | 2012-09-13 01:49:33 | [diff] [blame] | 49 | { "gdata/account_metadata.json", "dirty:existing", "md5_dirty_existing", |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 50 | false, true }, |
| 51 | // Cache resource that is pinned and dirty. |
[email protected] | 21ed9907 | 2012-09-13 01:49:33 | [diff] [blame] | 52 | { "gdata/basic_feed.json", "dirty_and_pinned:existing", |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 53 | "md5_dirty_and_pinned_existing", true, true }, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | const int64 kLotsOfSpace = kMinFreeSpace * 10; |
| 57 | |
| 58 | struct PathToVerify { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 59 | PathToVerify(const base::FilePath& in_path_to_scan, |
| 60 | const base::FilePath& in_expected_existing_path) : |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 61 | path_to_scan(in_path_to_scan), |
| 62 | expected_existing_path(in_expected_existing_path) { |
| 63 | } |
| 64 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 65 | base::FilePath path_to_scan; |
| 66 | base::FilePath expected_existing_path; |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 67 | }; |
[email protected] | a288c38 | 2012-07-11 20:05:48 | [diff] [blame] | 68 | |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 69 | // Copies results from Iterate(). |
| 70 | void OnIterate(std::vector<std::string>* out_resource_ids, |
| 71 | std::vector<DriveCacheEntry>* out_cache_entries, |
| 72 | const std::string& resource_id, |
| 73 | const DriveCacheEntry& cache_entry) { |
| 74 | out_resource_ids->push_back(resource_id); |
| 75 | out_cache_entries->push_back(cache_entry); |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 76 | } |
| 77 | |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 78 | // Called upon completion of Iterate(). |
| 79 | void OnIterateCompleted(bool* out_is_called) { |
| 80 | *out_is_called = true; |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 81 | } |
| 82 | |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 83 | } // namespace |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 84 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 85 | class DriveCacheTest : public testing::Test { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 86 | protected: |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 87 | DriveCacheTest() |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 88 | : ui_thread_(content::BrowserThread::UI, &message_loop_), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 89 | expected_error_(FILE_ERROR_OK), |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 90 | expected_cache_state_(0), |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 91 | expected_sub_dir_type_(DriveCache::CACHE_TYPE_META), |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 92 | expected_success_(true), |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame^] | 93 | expect_outgoing_symlink_(false) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | virtual void SetUp() OVERRIDE { |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame^] | 97 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 98 | fake_free_disk_space_getter_.reset(new FakeFreeDiskSpaceGetter); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 99 | |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 100 | scoped_refptr<base::SequencedWorkerPool> pool = |
| 101 | content::BrowserThread::GetBlockingPool(); |
| 102 | blocking_task_runner_ = |
| 103 | pool->GetSequencedTaskRunner(pool->GetSequenceToken()); |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame^] | 104 | cache_.reset(new DriveCache(temp_dir_.path(), |
[email protected] | c0a3b8e8 | 2013-03-18 21:07:45 | [diff] [blame] | 105 | blocking_task_runner_, |
| 106 | fake_free_disk_space_getter_.get())); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 107 | |
[email protected] | bd547ad8 | 2013-04-26 04:52:29 | [diff] [blame] | 108 | mock_cache_observer_.reset(new StrictMock<MockCacheObserver>); |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 109 | cache_->AddObserver(mock_cache_observer_.get()); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 110 | |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 111 | bool success = false; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 112 | cache_->RequestInitialize( |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 113 | google_apis::test_util::CreateCopyResultCallback(&success)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 114 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 115 | ASSERT_TRUE(success); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | virtual void TearDown() OVERRIDE { |
[email protected] | c0a3b8e8 | 2013-03-18 21:07:45 | [diff] [blame] | 119 | cache_.reset(); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 120 | } |
| 121 | |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 122 | void PrepareTestCacheResources() { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 123 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 124 | |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 125 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cache_resources); ++i) { |
| 126 | const struct TestCacheResource& resource = test_cache_resources[i]; |
| 127 | // Copy file from data dir to cache. |
| 128 | if (!std::string(resource.source_file).empty()) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 129 | base::FilePath source_path = |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 130 | google_apis::test_util::GetTestFilePath( |
| 131 | std::string("chromeos/") + resource.source_file); |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 132 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 133 | FileError error = FILE_ERROR_OK; |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 134 | cache_->Store( |
| 135 | resource.resource_id, |
| 136 | resource.md5, |
| 137 | source_path, |
| 138 | DriveCache::FILE_OPERATION_COPY, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 139 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 140 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 141 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 142 | } |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 143 | // Pin. |
| 144 | if (resource.is_pinned) { |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 145 | FileError error = FILE_ERROR_OK; |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 146 | EXPECT_CALL(*mock_cache_observer_, |
| 147 | OnCachePinned(resource.resource_id, resource.md5)).Times(1); |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 148 | cache_->Pin( |
| 149 | resource.resource_id, |
| 150 | resource.md5, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 151 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 152 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 153 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 154 | } |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 155 | // Mark dirty. |
| 156 | if (resource.is_dirty) { |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 157 | FileError error = FILE_ERROR_OK; |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 158 | cache_->MarkDirty( |
| 159 | resource.resource_id, |
| 160 | resource.md5, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 161 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 162 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 163 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 164 | |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 165 | EXPECT_CALL(*mock_cache_observer_, |
| 166 | OnCacheCommitted(resource.resource_id)).Times(1); |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 167 | cache_->CommitDirty( |
| 168 | resource.resource_id, |
| 169 | resource.md5, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 170 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 171 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 172 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 173 | } |
| 174 | } |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void TestGetFileFromCacheByResourceIdAndMd5( |
| 178 | const std::string& resource_id, |
| 179 | const std::string& md5, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 180 | FileError expected_error, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 181 | const std::string& expected_file_extension) { |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 182 | FileError error = FILE_ERROR_OK; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 183 | base::FilePath cache_file_path; |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 184 | cache_->GetFile(resource_id, md5, |
| 185 | google_apis::test_util::CreateCopyResultCallback( |
| 186 | &error, &cache_file_path)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 187 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 188 | |
| 189 | EXPECT_EQ(expected_error, error); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 190 | if (error == FILE_ERROR_OK) { |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 191 | // Verify filename of |cache_file_path|. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 192 | base::FilePath base_name = cache_file_path.BaseName(); |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 193 | EXPECT_EQ(util::EscapeCacheFileName(resource_id) + |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 194 | base::FilePath::kExtensionSeparator + |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 195 | util::EscapeCacheFileName( |
| 196 | expected_file_extension.empty() ? |
| 197 | md5 : expected_file_extension), |
| 198 | base_name.value()); |
| 199 | } else { |
| 200 | EXPECT_TRUE(cache_file_path.empty()); |
| 201 | } |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void TestStoreToCache( |
| 205 | const std::string& resource_id, |
| 206 | const std::string& md5, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 207 | const base::FilePath& source_path, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 208 | FileError expected_error, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 209 | int expected_cache_state, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 210 | DriveCache::CacheSubDirectoryType expected_sub_dir_type) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 211 | expected_error_ = expected_error; |
| 212 | expected_cache_state_ = expected_cache_state; |
| 213 | expected_sub_dir_type_ = expected_sub_dir_type; |
| 214 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 215 | FileError error = FILE_ERROR_OK; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 216 | cache_->Store(resource_id, md5, source_path, |
| 217 | DriveCache::FILE_OPERATION_COPY, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 218 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 219 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 220 | VerifyCacheFileState(error, resource_id, md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void TestRemoveFromCache(const std::string& resource_id, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 224 | FileError expected_error) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 225 | expected_error_ = expected_error; |
| 226 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 227 | FileError error = FILE_ERROR_OK; |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 228 | cache_->Remove( |
| 229 | resource_id, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 230 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 231 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 232 | VerifyRemoveFromCache(error, resource_id, ""); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 233 | } |
| 234 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 235 | void VerifyRemoveFromCache(FileError error, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 236 | const std::string& resource_id, |
| 237 | const std::string& md5) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 238 | EXPECT_EQ(expected_error_, error); |
| 239 | |
| 240 | // Verify cache map. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 241 | DriveCacheEntry cache_entry; |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 242 | const bool cache_entry_found = |
| 243 | GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); |
| 244 | if (cache_entry_found) |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 245 | EXPECT_TRUE(cache_entry.is_dirty()); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 246 | |
| 247 | // If entry doesn't exist, verify that: |
| 248 | // - no files with "<resource_id>.* exists in persistent and tmp dirs |
| 249 | // - no "<resource_id>" symlink exists in pinned and outgoing dirs. |
| 250 | std::vector<PathToVerify> paths_to_verify; |
| 251 | paths_to_verify.push_back( // Index 0: CACHE_TYPE_TMP. |
| 252 | PathToVerify(cache_->GetCacheFilePath(resource_id, "*", |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 253 | DriveCache::CACHE_TYPE_TMP, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 254 | DriveCache::CACHED_FILE_FROM_SERVER), base::FilePath())); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 255 | paths_to_verify.push_back( // Index 1: CACHE_TYPE_PERSISTENT. |
| 256 | PathToVerify(cache_->GetCacheFilePath(resource_id, "*", |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 257 | DriveCache::CACHE_TYPE_PERSISTENT, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 258 | DriveCache::CACHED_FILE_FROM_SERVER), base::FilePath())); |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 259 | paths_to_verify.push_back( // Index 2: CACHE_TYPE_OUTGOING. |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 260 | PathToVerify(cache_->GetCacheFilePath(resource_id, "", |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 261 | DriveCache::CACHE_TYPE_OUTGOING, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 262 | DriveCache::CACHED_FILE_FROM_SERVER), base::FilePath())); |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 263 | if (!cache_entry_found) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 264 | for (size_t i = 0; i < paths_to_verify.size(); ++i) { |
| 265 | file_util::FileEnumerator enumerator( |
| 266 | paths_to_verify[i].path_to_scan.DirName(), false /* not recursive*/, |
[email protected] | 84c3f16 | 2012-08-12 01:57:23 | [diff] [blame] | 267 | file_util::FileEnumerator::FILES | |
| 268 | file_util::FileEnumerator::SHOW_SYM_LINKS, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 269 | paths_to_verify[i].path_to_scan.BaseName().value()); |
| 270 | EXPECT_TRUE(enumerator.Next().empty()); |
| 271 | } |
| 272 | } else { |
| 273 | // Entry is dirty, verify that: |
| 274 | // - no files with "<resource_id>.*" exist in tmp dir |
| 275 | // - only 1 "<resource_id>.local" exists in persistent dir |
| 276 | // - only 1 <resource_id> exists in outgoing dir |
| 277 | // - if entry is pinned, only 1 <resource_id> exists in pinned dir. |
| 278 | |
| 279 | // Change expected_existing_path of CACHE_TYPE_PERSISTENT (index 1). |
| 280 | paths_to_verify[1].expected_existing_path = |
| 281 | GetCacheFilePath(resource_id, |
| 282 | std::string(), |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 283 | DriveCache::CACHE_TYPE_PERSISTENT, |
| 284 | DriveCache::CACHED_FILE_LOCALLY_MODIFIED); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 285 | |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 286 | // Change expected_existing_path of CACHE_TYPE_OUTGOING (index 2). |
| 287 | paths_to_verify[2].expected_existing_path = |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 288 | GetCacheFilePath(resource_id, |
| 289 | std::string(), |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 290 | DriveCache::CACHE_TYPE_OUTGOING, |
| 291 | DriveCache::CACHED_FILE_FROM_SERVER); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 292 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 293 | for (size_t i = 0; i < paths_to_verify.size(); ++i) { |
| 294 | const struct PathToVerify& verify = paths_to_verify[i]; |
| 295 | file_util::FileEnumerator enumerator( |
[email protected] | 84c3f16 | 2012-08-12 01:57:23 | [diff] [blame] | 296 | verify.path_to_scan.DirName(), false /* not recursive */, |
| 297 | file_util::FileEnumerator::FILES | |
| 298 | file_util::FileEnumerator::SHOW_SYM_LINKS, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 299 | verify.path_to_scan.BaseName().value()); |
| 300 | size_t num_files_found = 0; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 301 | for (base::FilePath current = enumerator.Next(); !current.empty(); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 302 | current = enumerator.Next()) { |
| 303 | ++num_files_found; |
| 304 | EXPECT_EQ(verify.expected_existing_path, current); |
| 305 | } |
| 306 | if (verify.expected_existing_path.empty()) |
| 307 | EXPECT_EQ(0U, num_files_found); |
| 308 | else |
| 309 | EXPECT_EQ(1U, num_files_found); |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | void TestPin( |
| 315 | const std::string& resource_id, |
| 316 | const std::string& md5, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 317 | FileError expected_error, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 318 | int expected_cache_state, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 319 | DriveCache::CacheSubDirectoryType expected_sub_dir_type) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 320 | expected_error_ = expected_error; |
| 321 | expected_cache_state_ = expected_cache_state; |
| 322 | expected_sub_dir_type_ = expected_sub_dir_type; |
| 323 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 324 | FileError error = FILE_ERROR_OK; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 325 | cache_->Pin(resource_id, md5, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 326 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 327 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 328 | VerifyCacheFileState(error, resource_id, md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void TestUnpin( |
| 332 | const std::string& resource_id, |
| 333 | const std::string& md5, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 334 | FileError expected_error, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 335 | int expected_cache_state, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 336 | DriveCache::CacheSubDirectoryType expected_sub_dir_type) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 337 | expected_error_ = expected_error; |
| 338 | expected_cache_state_ = expected_cache_state; |
| 339 | expected_sub_dir_type_ = expected_sub_dir_type; |
| 340 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 341 | FileError error = FILE_ERROR_OK; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 342 | cache_->Unpin(resource_id, md5, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 343 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 344 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 345 | VerifyCacheFileState(error, resource_id, md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 346 | } |
| 347 | |
[email protected] | bc809e4 | 2012-11-28 04:46:29 | [diff] [blame] | 348 | void TestMarkDirty(const std::string& resource_id, |
| 349 | const std::string& md5, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 350 | FileError expected_error, |
[email protected] | bc809e4 | 2012-11-28 04:46:29 | [diff] [blame] | 351 | int expected_cache_state, |
| 352 | DriveCache::CacheSubDirectoryType expected_sub_dir_type) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 353 | expected_error_ = expected_error; |
| 354 | expected_cache_state_ = expected_cache_state; |
| 355 | expected_sub_dir_type_ = expected_sub_dir_type; |
| 356 | expect_outgoing_symlink_ = false; |
| 357 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 358 | FileError error = FILE_ERROR_OK; |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 359 | cache_->MarkDirty( |
| 360 | resource_id, md5, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 361 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 362 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 363 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 364 | VerifyCacheFileState(error, resource_id, md5); |
| 365 | |
[email protected] | bc809e4 | 2012-11-28 04:46:29 | [diff] [blame] | 366 | // Verify filename. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 367 | if (error == FILE_ERROR_OK) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 368 | base::FilePath cache_file_path; |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 369 | cache_->GetFile(resource_id, md5, |
| 370 | google_apis::test_util::CreateCopyResultCallback( |
| 371 | &error, &cache_file_path)); |
[email protected] | bc809e4 | 2012-11-28 04:46:29 | [diff] [blame] | 372 | google_apis::test_util::RunBlockingPoolTask(); |
| 373 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 374 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 375 | base::FilePath base_name = cache_file_path.BaseName(); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 376 | EXPECT_EQ(util::EscapeCacheFileName(resource_id) + |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 377 | base::FilePath::kExtensionSeparator + |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 378 | "local", |
| 379 | base_name.value()); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | void TestCommitDirty( |
| 384 | const std::string& resource_id, |
| 385 | const std::string& md5, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 386 | FileError expected_error, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 387 | int expected_cache_state, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 388 | DriveCache::CacheSubDirectoryType expected_sub_dir_type) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 389 | expected_error_ = expected_error; |
| 390 | expected_cache_state_ = expected_cache_state; |
| 391 | expected_sub_dir_type_ = expected_sub_dir_type; |
| 392 | expect_outgoing_symlink_ = true; |
| 393 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 394 | FileError error = FILE_ERROR_OK; |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 395 | cache_->CommitDirty( |
| 396 | resource_id, md5, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 397 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 398 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 399 | VerifyCacheFileState(error, resource_id, md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | void TestClearDirty( |
| 403 | const std::string& resource_id, |
| 404 | const std::string& md5, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 405 | FileError expected_error, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 406 | int expected_cache_state, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 407 | DriveCache::CacheSubDirectoryType expected_sub_dir_type) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 408 | expected_error_ = expected_error; |
| 409 | expected_cache_state_ = expected_cache_state; |
| 410 | expected_sub_dir_type_ = expected_sub_dir_type; |
| 411 | expect_outgoing_symlink_ = false; |
| 412 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 413 | FileError error = FILE_ERROR_OK; |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 414 | cache_->ClearDirty( |
| 415 | resource_id, md5, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 416 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 417 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 2a2c415 | 2012-11-26 11:34:50 | [diff] [blame] | 418 | VerifyCacheFileState(error, resource_id, md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 419 | } |
| 420 | |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 421 | void TestMarkAsMounted( |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 422 | const std::string& resource_id, |
| 423 | const std::string& md5, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 424 | FileError expected_error, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 425 | int expected_cache_state, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 426 | DriveCache::CacheSubDirectoryType expected_sub_dir_type) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 427 | expected_error_ = expected_error; |
| 428 | expected_cache_state_ = expected_cache_state; |
| 429 | expected_sub_dir_type_ = expected_sub_dir_type; |
| 430 | expect_outgoing_symlink_ = false; |
| 431 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 432 | FileError error = FILE_ERROR_OK; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 433 | base::FilePath cache_file_path; |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 434 | cache_->MarkAsMounted(resource_id, md5, |
| 435 | google_apis::test_util::CreateCopyResultCallback( |
| 436 | &error, &cache_file_path)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 437 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 438 | |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 439 | EXPECT_TRUE(file_util::PathExists(cache_file_path)); |
| 440 | EXPECT_EQ(cache_file_path, |
| 441 | cache_->GetCacheFilePath(resource_id, |
| 442 | md5, |
| 443 | expected_sub_dir_type_, |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 444 | DriveCache::CACHED_FILE_MOUNTED)); |
| 445 | } |
| 446 | |
| 447 | void TestMarkAsUnmounted( |
| 448 | const std::string& resource_id, |
| 449 | const std::string& md5, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 450 | const base::FilePath& file_path, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 451 | FileError expected_error, |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 452 | int expected_cache_state, |
| 453 | DriveCache::CacheSubDirectoryType expected_sub_dir_type) { |
| 454 | expected_error_ = expected_error; |
| 455 | expected_cache_state_ = expected_cache_state; |
| 456 | expected_sub_dir_type_ = expected_sub_dir_type; |
| 457 | expect_outgoing_symlink_ = false; |
| 458 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 459 | FileError error = FILE_ERROR_OK; |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 460 | cache_->MarkAsUnmounted( |
| 461 | file_path, |
[email protected] | 7601e18 | 2013-03-22 09:38:02 | [diff] [blame] | 462 | google_apis::test_util::CreateCopyResultCallback(&error)); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 463 | google_apis::test_util::RunBlockingPoolTask(); |
| 464 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 465 | base::FilePath cache_file_path; |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 466 | cache_->GetFile(resource_id, md5, |
| 467 | google_apis::test_util::CreateCopyResultCallback( |
| 468 | &error, &cache_file_path)); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 469 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 470 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 471 | |
| 472 | EXPECT_TRUE(file_util::PathExists(cache_file_path)); |
| 473 | EXPECT_EQ(cache_file_path, |
| 474 | cache_->GetCacheFilePath(resource_id, |
| 475 | md5, |
| 476 | expected_sub_dir_type_, |
[email protected] | 717e43c | 2012-11-22 07:47:39 | [diff] [blame] | 477 | DriveCache::CACHED_FILE_FROM_SERVER)); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 480 | void VerifyCacheFileState(FileError error, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 481 | const std::string& resource_id, |
| 482 | const std::string& md5) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 483 | EXPECT_EQ(expected_error_, error); |
| 484 | |
| 485 | // Verify cache map. |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 486 | DriveCacheEntry cache_entry; |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 487 | const bool cache_entry_found = |
| 488 | GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 489 | if (test_util::ToCacheEntry(expected_cache_state_).is_present() || |
| 490 | test_util::ToCacheEntry(expected_cache_state_).is_pinned()) { |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 491 | ASSERT_TRUE(cache_entry_found); |
[email protected] | 48477fe | 2012-07-12 17:45:08 | [diff] [blame] | 492 | EXPECT_TRUE(test_util::CacheStatesEqual( |
| 493 | test_util::ToCacheEntry(expected_cache_state_), |
| 494 | cache_entry)); |
[email protected] | fae353a | 2012-07-11 23:30:27 | [diff] [blame] | 495 | EXPECT_EQ(expected_sub_dir_type_, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 496 | DriveCache::GetSubDirectoryType(cache_entry)); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 497 | } else { |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 498 | EXPECT_FALSE(cache_entry_found); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | // Verify actual cache file. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 502 | base::FilePath dest_path = cache_->GetCacheFilePath( |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 503 | resource_id, |
| 504 | md5, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 505 | test_util::ToCacheEntry(expected_cache_state_).is_pinned() || |
| 506 | test_util::ToCacheEntry(expected_cache_state_).is_dirty() ? |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 507 | DriveCache::CACHE_TYPE_PERSISTENT : |
| 508 | DriveCache::CACHE_TYPE_TMP, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 509 | test_util::ToCacheEntry(expected_cache_state_).is_dirty() ? |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 510 | DriveCache::CACHED_FILE_LOCALLY_MODIFIED : |
| 511 | DriveCache::CACHED_FILE_FROM_SERVER); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 512 | bool exists = file_util::PathExists(dest_path); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 513 | if (test_util::ToCacheEntry(expected_cache_state_).is_present()) |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 514 | EXPECT_TRUE(exists); |
| 515 | else |
| 516 | EXPECT_FALSE(exists); |
| 517 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 518 | // Verify symlink in outgoing dir. |
[email protected] | ae248f0 | 2013-03-06 12:55:41 | [diff] [blame] | 519 | base::FilePath symlink_path = cache_->GetCacheFilePath( |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 520 | resource_id, |
| 521 | std::string(), |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 522 | DriveCache::CACHE_TYPE_OUTGOING, |
| 523 | DriveCache::CACHED_FILE_FROM_SERVER); |
[email protected] | fba5954 | 2012-12-18 06:05:38 | [diff] [blame] | 524 | // Check that outgoing symlink exists, without dereferencing to target path. |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 525 | exists = file_util::IsLink(symlink_path); |
| 526 | if (expect_outgoing_symlink_ && |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 527 | test_util::ToCacheEntry(expected_cache_state_).is_dirty()) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 528 | EXPECT_TRUE(exists); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 529 | base::FilePath target_path; |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 530 | EXPECT_TRUE(file_util::ReadSymbolicLink(symlink_path, &target_path)); |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame^] | 531 | EXPECT_NE(util::kSymLinkToDevNull, target_path.value()); |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 532 | if (test_util::ToCacheEntry(expected_cache_state_).is_present()) |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 533 | EXPECT_EQ(dest_path, target_path); |
| 534 | } else { |
| 535 | EXPECT_FALSE(exists); |
| 536 | } |
| 537 | } |
| 538 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 539 | base::FilePath GetCacheFilePath(const std::string& resource_id, |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 540 | const std::string& md5, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 541 | DriveCache::CacheSubDirectoryType sub_dir_type, |
| 542 | DriveCache::CachedFileOrigin file_origin) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 543 | return cache_->GetCacheFilePath(resource_id, md5, sub_dir_type, |
| 544 | file_origin); |
| 545 | } |
| 546 | |
| 547 | // Helper function to call GetCacheEntry from origin thread. |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 548 | bool GetCacheEntryFromOriginThread(const std::string& resource_id, |
| 549 | const std::string& md5, |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 550 | DriveCacheEntry* cache_entry) { |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 551 | bool result = false; |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 552 | cache_->GetCacheEntry(resource_id, md5, |
| 553 | google_apis::test_util::CreateCopyResultCallback( |
| 554 | &result, cache_entry)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 555 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 556 | return result; |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 557 | } |
| 558 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 559 | // Returns true if the cache entry exists for the given resource ID and MD5. |
| 560 | bool CacheEntryExists(const std::string& resource_id, |
| 561 | const std::string& md5) { |
[email protected] | 28a6409 | 2012-08-21 10:01:12 | [diff] [blame] | 562 | DriveCacheEntry cache_entry; |
[email protected] | b22f87f | 2012-07-12 10:53:17 | [diff] [blame] | 563 | return GetCacheEntryFromOriginThread(resource_id, md5, &cache_entry); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | void TestGetCacheFilePath(const std::string& resource_id, |
| 567 | const std::string& md5, |
| 568 | const std::string& expected_filename) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 569 | base::FilePath actual_path = cache_->GetCacheFilePath( |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 570 | resource_id, |
| 571 | md5, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 572 | DriveCache::CACHE_TYPE_TMP, |
| 573 | DriveCache::CACHED_FILE_FROM_SERVER); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 574 | base::FilePath expected_path = |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 575 | cache_->GetCacheDirectoryPath(DriveCache::CACHE_TYPE_TMP); |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame^] | 576 | expected_path = expected_path.Append( |
| 577 | base::FilePath::FromUTF8Unsafe(expected_filename)); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 578 | EXPECT_EQ(expected_path, actual_path); |
| 579 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 580 | base::FilePath base_name = actual_path.BaseName(); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 581 | |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 582 | // base::FilePath::Extension returns ".", so strip it. |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 583 | std::string unescaped_md5 = util::UnescapeCacheFileName( |
| 584 | base_name.Extension().substr(1)); |
| 585 | EXPECT_EQ(md5, unescaped_md5); |
| 586 | std::string unescaped_resource_id = util::UnescapeCacheFileName( |
| 587 | base_name.RemoveExtension().value()); |
| 588 | EXPECT_EQ(resource_id, unescaped_resource_id); |
| 589 | } |
| 590 | |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 591 | // Returns the number of the cache files with name <resource_id>, and Confirm |
| 592 | // that they have the <md5>. This should return 1 or 0. |
| 593 | size_t CountCacheFiles(const std::string& resource_id, |
| 594 | const std::string& md5) { |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 595 | base::FilePath path = GetCacheFilePath( |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 596 | resource_id, "*", |
| 597 | (test_util::ToCacheEntry(expected_cache_state_).is_pinned() ? |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 598 | DriveCache::CACHE_TYPE_PERSISTENT : |
| 599 | DriveCache::CACHE_TYPE_TMP), |
| 600 | DriveCache::CACHED_FILE_FROM_SERVER); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 601 | file_util::FileEnumerator enumerator(path.DirName(), false, |
| 602 | file_util::FileEnumerator::FILES, |
| 603 | path.BaseName().value()); |
| 604 | size_t num_files_found = 0; |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 605 | for (base::FilePath current = enumerator.Next(); !current.empty(); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 606 | current = enumerator.Next()) { |
| 607 | ++num_files_found; |
| 608 | EXPECT_EQ(util::EscapeCacheFileName(resource_id) + |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 609 | base::FilePath::kExtensionSeparator + |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 610 | util::EscapeCacheFileName(md5), |
| 611 | current.BaseName().value()); |
| 612 | } |
| 613 | return num_files_found; |
| 614 | } |
| 615 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 616 | MessageLoopForUI message_loop_; |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 617 | content::TestBrowserThread ui_thread_; |
[email protected] | ddbf205 | 2012-07-13 15:07:02 | [diff] [blame] | 618 | scoped_refptr<base::SequencedTaskRunner> blocking_task_runner_; |
[email protected] | 2333b2a1 | 2013-04-26 07:22:22 | [diff] [blame^] | 619 | base::ScopedTempDir temp_dir_; |
| 620 | |
[email protected] | c0a3b8e8 | 2013-03-18 21:07:45 | [diff] [blame] | 621 | scoped_ptr<DriveCache, test_util::DestroyHelperForTests> cache_; |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 622 | scoped_ptr<FakeFreeDiskSpaceGetter> fake_free_disk_space_getter_; |
[email protected] | bd547ad8 | 2013-04-26 04:52:29 | [diff] [blame] | 623 | scoped_ptr<StrictMock<MockCacheObserver> > mock_cache_observer_; |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 624 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 625 | FileError expected_error_; |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 626 | int expected_cache_state_; |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 627 | DriveCache::CacheSubDirectoryType expected_sub_dir_type_; |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 628 | bool expected_success_; |
| 629 | bool expect_outgoing_symlink_; |
| 630 | std::string expected_file_extension_; |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 631 | }; |
| 632 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 633 | TEST_F(DriveCacheTest, GetCacheFilePath) { |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 634 | // Use alphanumeric characters for resource id. |
| 635 | std::string resource_id("pdf:1a2b"); |
| 636 | std::string md5("abcdef0123456789"); |
| 637 | TestGetCacheFilePath(resource_id, md5, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 638 | resource_id + base::FilePath::kExtensionSeparator + md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 639 | |
| 640 | // Use non-alphanumeric characters for resource id, including '.' which is an |
| 641 | // extension separator, to test that the characters are escaped and unescaped |
| 642 | // correctly, and '.' doesn't mess up the filename format and operations. |
| 643 | resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; |
| 644 | std::string escaped_resource_id = util::EscapeCacheFileName(resource_id); |
| 645 | std::string escaped_md5 = util::EscapeCacheFileName(md5); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 646 | TestGetCacheFilePath( |
| 647 | resource_id, md5, escaped_resource_id + |
| 648 | base::FilePath::kExtensionSeparator + escaped_md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 649 | } |
| 650 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 651 | TEST_F(DriveCacheTest, StoreToCacheSimple) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 652 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 653 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 654 | std::string resource_id("pdf:1a2b"); |
| 655 | std::string md5("abcdef0123456789"); |
| 656 | |
| 657 | // Store an existing file. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 658 | TestStoreToCache( |
| 659 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 660 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 661 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 662 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 663 | |
| 664 | // Store a non-existent file to the same |resource_id| and |md5|. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 665 | TestStoreToCache(resource_id, md5, base::FilePath("./non_existent.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 666 | FILE_ERROR_FAILED, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 667 | test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 668 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 669 | |
| 670 | // Store a different existing file to the same |resource_id| but different |
| 671 | // |md5|. |
| 672 | md5 = "new_md5"; |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 673 | TestStoreToCache( |
| 674 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 675 | google_apis::test_util::GetTestFilePath( |
[email protected] | 1669647 | 2013-04-22 05:42:21 | [diff] [blame] | 676 | "chromeos/gdata/empty_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 677 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 678 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 679 | |
| 680 | // Verify that there's only one file with name <resource_id>, i.e. previously |
| 681 | // cached file with the different md5 should be deleted. |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 682 | EXPECT_EQ(1U, CountCacheFiles(resource_id, md5)); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 683 | } |
| 684 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 685 | TEST_F(DriveCacheTest, GetFromCacheSimple) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 686 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 687 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 688 | std::string resource_id("pdf:1a2b"); |
| 689 | std::string md5("abcdef0123456789"); |
| 690 | // First store a file to cache. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 691 | TestStoreToCache( |
| 692 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 693 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 694 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 695 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 696 | |
| 697 | // Then try to get the existing file from cache. |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 698 | TestGetFileFromCacheByResourceIdAndMd5( |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 699 | resource_id, md5, FILE_ERROR_OK, md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 700 | |
| 701 | // Get file from cache with same resource id as existing file but different |
| 702 | // md5. |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 703 | TestGetFileFromCacheByResourceIdAndMd5( |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 704 | resource_id, "9999", FILE_ERROR_NOT_FOUND, md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 705 | |
| 706 | // Get file from cache with different resource id from existing file but same |
| 707 | // md5. |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 708 | resource_id = "document:1a2b"; |
| 709 | TestGetFileFromCacheByResourceIdAndMd5( |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 710 | resource_id, md5, FILE_ERROR_NOT_FOUND, md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 711 | } |
| 712 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 713 | TEST_F(DriveCacheTest, RemoveFromCacheSimple) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 714 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 715 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 716 | // Use alphanumeric characters for resource id. |
| 717 | std::string resource_id("pdf:1a2b"); |
| 718 | std::string md5("abcdef0123456789"); |
| 719 | // First store a file to cache. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 720 | TestStoreToCache( |
| 721 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 722 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 723 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 724 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 725 | |
| 726 | // Then try to remove existing file from cache. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 727 | TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 728 | |
| 729 | // Repeat using non-alphanumeric characters for resource id, including '.' |
| 730 | // which is an extension separator. |
| 731 | resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 732 | TestStoreToCache( |
| 733 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 734 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 735 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 736 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 737 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 738 | TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 739 | } |
| 740 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 741 | TEST_F(DriveCacheTest, PinAndUnpin) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 742 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 743 | |
| 744 | std::string resource_id("pdf:1a2b"); |
| 745 | std::string md5("abcdef0123456789"); |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 746 | EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(2); |
| 747 | EXPECT_CALL(*mock_cache_observer_, OnCacheUnpinned(resource_id, md5)) |
| 748 | .Times(1); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 749 | |
| 750 | // First store a file to cache. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 751 | TestStoreToCache( |
| 752 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 753 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 754 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 755 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 756 | |
| 757 | // Pin the existing file in cache. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 758 | TestPin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 759 | test_util::TEST_CACHE_STATE_PRESENT | |
| 760 | test_util::TEST_CACHE_STATE_PINNED | |
| 761 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 762 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 763 | |
| 764 | // Unpin the existing file in cache. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 765 | TestUnpin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 766 | test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 767 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 768 | |
| 769 | // Pin back the same existing file in cache. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 770 | TestPin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 771 | test_util::TEST_CACHE_STATE_PRESENT | |
| 772 | test_util::TEST_CACHE_STATE_PINNED | |
| 773 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 774 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 775 | |
| 776 | // Pin a non-existent file in cache. |
| 777 | resource_id = "document:1a2b"; |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 778 | EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
| 779 | EXPECT_CALL(*mock_cache_observer_, OnCacheUnpinned(resource_id, md5)) |
| 780 | .Times(1); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 781 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 782 | TestPin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 783 | test_util::TEST_CACHE_STATE_PINNED, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 784 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 785 | |
| 786 | // Unpin the previously pinned non-existent file in cache. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 787 | TestUnpin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 788 | test_util::TEST_CACHE_STATE_NONE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 789 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 790 | |
| 791 | // Unpin a file that doesn't exist in cache and is not pinned, i.e. cache |
| 792 | // has zero knowledge of the file. |
| 793 | resource_id = "not-in-cache:1a2b"; |
| 794 | // Because unpinning will fail, OnCacheUnpinned() won't be run. |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 795 | EXPECT_CALL(*mock_cache_observer_, OnCacheUnpinned(resource_id, md5)) |
| 796 | .Times(0); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 797 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 798 | TestUnpin(resource_id, md5, FILE_ERROR_NOT_FOUND, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 799 | test_util::TEST_CACHE_STATE_NONE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 800 | DriveCache::CACHE_TYPE_TMP /* non-applicable */); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 801 | } |
| 802 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 803 | TEST_F(DriveCacheTest, StoreToCachePinned) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 804 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 805 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 806 | std::string resource_id("pdf:1a2b"); |
| 807 | std::string md5("abcdef0123456789"); |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 808 | EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 809 | |
| 810 | // Pin a non-existent file. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 811 | TestPin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 812 | test_util::TEST_CACHE_STATE_PINNED, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 813 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 814 | |
| 815 | // Store an existing file to a previously pinned file. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 816 | TestStoreToCache( |
| 817 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 818 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 819 | FILE_ERROR_OK, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 820 | test_util::TEST_CACHE_STATE_PRESENT | |
| 821 | test_util::TEST_CACHE_STATE_PINNED | |
| 822 | test_util::TEST_CACHE_STATE_PERSISTENT, |
| 823 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 824 | |
| 825 | // Store a non-existent file to a previously pinned and stored file. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 826 | TestStoreToCache(resource_id, md5, base::FilePath("./non_existent.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 827 | FILE_ERROR_FAILED, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 828 | test_util::TEST_CACHE_STATE_PRESENT | |
| 829 | test_util::TEST_CACHE_STATE_PINNED | |
| 830 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 831 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 832 | } |
| 833 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 834 | TEST_F(DriveCacheTest, GetFromCachePinned) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 835 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 836 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 837 | std::string resource_id("pdf:1a2b"); |
| 838 | std::string md5("abcdef0123456789"); |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 839 | EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 840 | |
| 841 | // Pin a non-existent file. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 842 | TestPin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 843 | test_util::TEST_CACHE_STATE_PINNED, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 844 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 845 | |
| 846 | // Get the non-existent pinned file from cache. |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 847 | TestGetFileFromCacheByResourceIdAndMd5( |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 848 | resource_id, md5, FILE_ERROR_NOT_FOUND, md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 849 | |
| 850 | // Store an existing file to the previously pinned non-existent file. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 851 | TestStoreToCache( |
| 852 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 853 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 854 | FILE_ERROR_OK, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 855 | test_util::TEST_CACHE_STATE_PRESENT | |
| 856 | test_util::TEST_CACHE_STATE_PINNED | |
| 857 | test_util::TEST_CACHE_STATE_PERSISTENT, |
| 858 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 859 | |
| 860 | // Get the previously pinned and stored file from cache. |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 861 | TestGetFileFromCacheByResourceIdAndMd5( |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 862 | resource_id, md5, FILE_ERROR_OK, md5); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 863 | } |
| 864 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 865 | TEST_F(DriveCacheTest, RemoveFromCachePinned) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 866 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 867 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 868 | // Use alphanumeric characters for resource_id. |
| 869 | std::string resource_id("pdf:1a2b"); |
| 870 | std::string md5("abcdef0123456789"); |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 871 | EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 872 | |
| 873 | // Store a file to cache, and pin it. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 874 | TestStoreToCache( |
| 875 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 876 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 877 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 878 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 879 | TestPin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 880 | test_util::TEST_CACHE_STATE_PRESENT | |
| 881 | test_util::TEST_CACHE_STATE_PINNED | |
| 882 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 883 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 884 | |
| 885 | // Remove |resource_id| from cache. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 886 | TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 887 | |
| 888 | // Repeat using non-alphanumeric characters for resource id, including '.' |
| 889 | // which is an extension separator. |
| 890 | resource_id = "pdf:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?"; |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 891 | EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 892 | |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 893 | TestStoreToCache( |
| 894 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 895 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 896 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 897 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 898 | TestPin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 899 | test_util::TEST_CACHE_STATE_PRESENT | |
| 900 | test_util::TEST_CACHE_STATE_PINNED | |
| 901 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 902 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 903 | |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 904 | TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 905 | } |
| 906 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 907 | TEST_F(DriveCacheTest, DirtyCacheSimple) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 908 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 909 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 910 | std::string resource_id("pdf:1a2b"); |
| 911 | std::string md5("abcdef0123456789"); |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 912 | EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 913 | |
| 914 | // First store a file to cache. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 915 | TestStoreToCache( |
| 916 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 917 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 918 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 919 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 920 | |
| 921 | // Mark the file dirty. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 922 | TestMarkDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 923 | test_util::TEST_CACHE_STATE_PRESENT | |
| 924 | test_util::TEST_CACHE_STATE_DIRTY | |
| 925 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 926 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 927 | |
| 928 | // Commit the file dirty. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 929 | TestCommitDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 930 | test_util::TEST_CACHE_STATE_PRESENT | |
| 931 | test_util::TEST_CACHE_STATE_DIRTY | |
| 932 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 933 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 934 | |
| 935 | // Clear dirty state of the file. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 936 | TestClearDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 937 | test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 938 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 939 | } |
| 940 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 941 | TEST_F(DriveCacheTest, DirtyCachePinned) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 942 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 943 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 944 | std::string resource_id("pdf:1a2b"); |
| 945 | std::string md5("abcdef0123456789"); |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 946 | EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
| 947 | EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 948 | |
| 949 | // First store a file to cache and pin it. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 950 | TestStoreToCache( |
| 951 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 952 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 953 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 954 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 955 | TestPin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 956 | test_util::TEST_CACHE_STATE_PRESENT | |
| 957 | test_util::TEST_CACHE_STATE_PINNED | |
| 958 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 959 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 960 | |
| 961 | // Mark the file dirty. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 962 | TestMarkDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 963 | test_util::TEST_CACHE_STATE_PRESENT | |
| 964 | test_util::TEST_CACHE_STATE_DIRTY | |
| 965 | test_util::TEST_CACHE_STATE_PINNED | |
| 966 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 967 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 968 | |
| 969 | // Commit the file dirty. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 970 | TestCommitDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 971 | test_util::TEST_CACHE_STATE_PRESENT | |
| 972 | test_util::TEST_CACHE_STATE_DIRTY | |
| 973 | test_util::TEST_CACHE_STATE_PINNED | |
| 974 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 975 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 976 | |
| 977 | // Clear dirty state of the file. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 978 | TestClearDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 979 | test_util::TEST_CACHE_STATE_PRESENT | |
| 980 | test_util::TEST_CACHE_STATE_PINNED | |
| 981 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 982 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | // Test is disabled because it is flaky (http://crbug.com/134146) |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 986 | TEST_F(DriveCacheTest, PinAndUnpinDirtyCache) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 987 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 988 | |
| 989 | std::string resource_id("pdf:1a2b"); |
| 990 | std::string md5("abcdef0123456789"); |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 991 | EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
| 992 | EXPECT_CALL(*mock_cache_observer_, OnCacheUnpinned(resource_id, md5)) |
| 993 | .Times(1); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 994 | |
| 995 | // First store a file to cache and mark it as dirty. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 996 | TestStoreToCache( |
| 997 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 998 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 999 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1000 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1001 | TestMarkDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1002 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1003 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1004 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1005 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1006 | |
| 1007 | // Verifies dirty file exists. |
[email protected] | e966339 | 2013-04-12 09:55:15 | [diff] [blame] | 1008 | base::FilePath dirty_path; |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1009 | FileError error = FILE_ERROR_FAILED; |
[email protected] | e966339 | 2013-04-12 09:55:15 | [diff] [blame] | 1010 | cache_->GetFile( |
| 1011 | resource_id, md5, |
| 1012 | google_apis::test_util::CreateCopyResultCallback(&error, &dirty_path)); |
| 1013 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1014 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1015 | EXPECT_TRUE(file_util::PathExists(dirty_path)); |
| 1016 | |
| 1017 | // Pin the dirty file. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1018 | TestPin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1019 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1020 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1021 | test_util::TEST_CACHE_STATE_PINNED | |
| 1022 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1023 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1024 | |
| 1025 | // Verify dirty file still exist at the same pathname. |
| 1026 | EXPECT_TRUE(file_util::PathExists(dirty_path)); |
| 1027 | |
| 1028 | // Unpin the dirty file. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1029 | TestUnpin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1030 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1031 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1032 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1033 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1034 | |
| 1035 | // Verify dirty file still exist at the same pathname. |
| 1036 | EXPECT_TRUE(file_util::PathExists(dirty_path)); |
| 1037 | } |
| 1038 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1039 | TEST_F(DriveCacheTest, DirtyCacheRepetitive) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 1040 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 1041 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1042 | std::string resource_id("pdf:1a2b"); |
| 1043 | std::string md5("abcdef0123456789"); |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 1044 | EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(3); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1045 | |
| 1046 | // First store a file to cache. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1047 | TestStoreToCache( |
| 1048 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 1049 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1050 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1051 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1052 | |
| 1053 | // Mark the file dirty. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1054 | TestMarkDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1055 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1056 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1057 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1058 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1059 | |
| 1060 | // Again, mark the file dirty. Nothing should change. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1061 | TestMarkDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1062 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1063 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1064 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1065 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1066 | |
| 1067 | // Commit the file dirty. Outgoing symlink should be created. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1068 | TestCommitDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1069 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1070 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1071 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1072 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1073 | |
| 1074 | // Again, commit the file dirty. Nothing should change. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1075 | TestCommitDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1076 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1077 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1078 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1079 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1080 | |
[email protected] | cdbfb438 | 2013-03-24 10:11:09 | [diff] [blame] | 1081 | // Mark the file dirty again after it's being committed. Outgoing symlink |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1082 | // should be deleted. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1083 | TestMarkDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1084 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1085 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1086 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1087 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1088 | |
| 1089 | // Commit the file dirty. Outgoing symlink should be created again. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1090 | TestCommitDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1091 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1092 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1093 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1094 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1095 | |
| 1096 | // Clear dirty state of the file. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1097 | TestClearDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1098 | test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1099 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1100 | |
| 1101 | // Again, clear dirty state of the file, which is no longer dirty. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1102 | TestClearDirty(resource_id, md5, FILE_ERROR_INVALID_OPERATION, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1103 | test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1104 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1105 | } |
| 1106 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1107 | TEST_F(DriveCacheTest, DirtyCacheInvalid) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 1108 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 1109 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1110 | std::string resource_id("pdf:1a2b"); |
| 1111 | std::string md5("abcdef0123456789"); |
| 1112 | |
| 1113 | // Mark a non-existent file dirty. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1114 | TestMarkDirty(resource_id, md5, FILE_ERROR_NOT_FOUND, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1115 | test_util::TEST_CACHE_STATE_NONE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1116 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1117 | |
| 1118 | // Commit a non-existent file dirty. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1119 | TestCommitDirty(resource_id, md5, FILE_ERROR_NOT_FOUND, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1120 | test_util::TEST_CACHE_STATE_NONE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1121 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1122 | |
| 1123 | // Clear dirty state of a non-existent file. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1124 | TestClearDirty(resource_id, md5, FILE_ERROR_NOT_FOUND, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1125 | test_util::TEST_CACHE_STATE_NONE, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1126 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1127 | |
| 1128 | // Store a file to cache. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1129 | TestStoreToCache( |
| 1130 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 1131 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1132 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1133 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1134 | |
| 1135 | // Commit a non-dirty existing file dirty. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1136 | TestCommitDirty(resource_id, md5, FILE_ERROR_INVALID_OPERATION, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1137 | test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1138 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1139 | |
| 1140 | // Clear dirty state of a non-dirty existing file. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1141 | TestClearDirty(resource_id, md5, FILE_ERROR_INVALID_OPERATION, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1142 | test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1143 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1144 | |
| 1145 | // Mark an existing file dirty, then store a new file to the same resource id |
| 1146 | // but different md5, which should fail. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1147 | TestMarkDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1148 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1149 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1150 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1151 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1152 | md5 = "new_md5"; |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1153 | TestStoreToCache( |
| 1154 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 1155 | google_apis::test_util::GetTestFilePath( |
[email protected] | 1669647 | 2013-04-22 05:42:21 | [diff] [blame] | 1156 | "chromeos/gdata/empty_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1157 | FILE_ERROR_IN_USE, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1158 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1159 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1160 | test_util::TEST_CACHE_STATE_PERSISTENT, |
| 1161 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1162 | } |
| 1163 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1164 | TEST_F(DriveCacheTest, RemoveFromDirtyCache) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 1165 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 1166 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1167 | std::string resource_id("pdf:1a2b"); |
| 1168 | std::string md5("abcdef0123456789"); |
[email protected] | 4743aa1c | 2012-07-13 14:54:23 | [diff] [blame] | 1169 | EXPECT_CALL(*mock_cache_observer_, OnCachePinned(resource_id, md5)).Times(1); |
| 1170 | EXPECT_CALL(*mock_cache_observer_, OnCacheCommitted(resource_id)).Times(1); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1171 | |
| 1172 | // Store a file to cache, pin it, mark it dirty and commit it. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1173 | TestStoreToCache( |
| 1174 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 1175 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1176 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1177 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1178 | TestPin(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1179 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1180 | test_util::TEST_CACHE_STATE_PINNED | |
| 1181 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1182 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1183 | TestMarkDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1184 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1185 | test_util::TEST_CACHE_STATE_PINNED | |
| 1186 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1187 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1188 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1189 | TestCommitDirty(resource_id, md5, FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1190 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1191 | test_util::TEST_CACHE_STATE_PINNED | |
| 1192 | test_util::TEST_CACHE_STATE_DIRTY | |
| 1193 | test_util::TEST_CACHE_STATE_PERSISTENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1194 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1195 | |
| 1196 | // Try to remove the file. Since file is dirty, it and the corresponding |
| 1197 | // pinned and outgoing symlinks should not be removed. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1198 | TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1199 | } |
| 1200 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1201 | TEST_F(DriveCacheTest, MountUnmount) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 1202 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 1203 | |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1204 | std::string resource_id("pdf:1a2b"); |
| 1205 | std::string md5("abcdef0123456789"); |
| 1206 | |
| 1207 | // First store a file to cache in the tmp subdir. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1208 | TestStoreToCache( |
| 1209 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 1210 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1211 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1212 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1213 | |
| 1214 | // Mark the file mounted. |
[email protected] | 35c1f9b | 2013-02-07 07:39:42 | [diff] [blame] | 1215 | TestMarkAsMounted(resource_id, |
| 1216 | md5, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1217 | FILE_ERROR_OK, |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 1218 | test_util::TEST_CACHE_STATE_PRESENT | |
| 1219 | test_util::TEST_CACHE_STATE_MOUNTED | |
| 1220 | test_util::TEST_CACHE_STATE_PERSISTENT, |
| 1221 | DriveCache::CACHE_TYPE_PERSISTENT); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1222 | EXPECT_TRUE(CacheEntryExists(resource_id, md5)); |
| 1223 | |
| 1224 | // Clear mounted state of the file. |
[email protected] | e966339 | 2013-04-12 09:55:15 | [diff] [blame] | 1225 | base::FilePath file_path; |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1226 | FileError error = FILE_ERROR_FAILED; |
[email protected] | e966339 | 2013-04-12 09:55:15 | [diff] [blame] | 1227 | cache_->GetFile( |
| 1228 | resource_id, md5, |
| 1229 | google_apis::test_util::CreateCopyResultCallback(&error, &file_path)); |
| 1230 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1231 | EXPECT_EQ(FILE_ERROR_OK, error); |
[email protected] | e966339 | 2013-04-12 09:55:15 | [diff] [blame] | 1232 | |
[email protected] | 9564c150 | 2012-11-28 12:12:16 | [diff] [blame] | 1233 | TestMarkAsUnmounted(resource_id, md5, file_path, |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1234 | FILE_ERROR_OK, |
[email protected] | 0282110 | 2012-07-12 20:19:17 | [diff] [blame] | 1235 | test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1236 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1237 | EXPECT_TRUE(CacheEntryExists(resource_id, md5)); |
| 1238 | |
| 1239 | // Try to remove the file. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1240 | TestRemoveFromCache(resource_id, FILE_ERROR_OK); |
[email protected] | 330278c | 2012-07-05 09:43:55 | [diff] [blame] | 1241 | } |
| 1242 | |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 1243 | TEST_F(DriveCacheTest, Iterate) { |
[email protected] | 5d8abd2 | 2012-11-14 08:31:03 | [diff] [blame] | 1244 | PrepareTestCacheResources(); |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 1245 | |
| 1246 | std::vector<std::string> resource_ids; |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 1247 | std::vector<DriveCacheEntry> cache_entries; |
| 1248 | bool completed = false; |
| 1249 | cache_->Iterate( |
| 1250 | base::Bind(&OnIterate, &resource_ids, &cache_entries), |
| 1251 | base::Bind(&OnIterateCompleted, &completed)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1252 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 1253 | |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 1254 | ASSERT_TRUE(completed); |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 1255 | |
| 1256 | sort(resource_ids.begin(), resource_ids.end()); |
| 1257 | ASSERT_EQ(6U, resource_ids.size()); |
| 1258 | EXPECT_EQ("dirty:existing", resource_ids[0]); |
| 1259 | EXPECT_EQ("dirty_and_pinned:existing", resource_ids[1]); |
| 1260 | EXPECT_EQ("pinned:existing", resource_ids[2]); |
| 1261 | EXPECT_EQ("pinned:non-existent", resource_ids[3]); |
| 1262 | EXPECT_EQ("tmp:`~!@#$%^&*()-_=+[{|]}\\;',<.>/?", resource_ids[4]); |
| 1263 | EXPECT_EQ("tmp:resource_id", resource_ids[5]); |
[email protected] | d68ede0 | 2012-11-07 14:30:53 | [diff] [blame] | 1264 | |
| 1265 | ASSERT_EQ(6U, cache_entries.size()); |
[email protected] | cd23643 | 2012-07-27 18:03:30 | [diff] [blame] | 1266 | } |
| 1267 | |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1268 | |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 1269 | TEST_F(DriveCacheTest, ClearAll) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 1270 | fake_free_disk_space_getter_->set_fake_free_disk_space(kLotsOfSpace); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 1271 | |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1272 | std::string resource_id("pdf:1a2b"); |
| 1273 | std::string md5("abcdef0123456789"); |
| 1274 | |
| 1275 | // Store an existing file. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1276 | TestStoreToCache( |
| 1277 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 1278 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1279 | FILE_ERROR_OK, test_util::TEST_CACHE_STATE_PRESENT, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1280 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1281 | |
| 1282 | // Verify that there's only one cached file. |
| 1283 | EXPECT_EQ(1U, CountCacheFiles(resource_id, md5)); |
| 1284 | |
| 1285 | // Clear cache. |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1286 | bool success = false; |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 1287 | cache_->ClearAll(google_apis::test_util::CreateCopyResultCallback(&success)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1288 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | bdd947c | 2012-11-06 04:35:34 | [diff] [blame] | 1289 | EXPECT_TRUE(success); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1290 | |
| 1291 | // Verify that all the cache is removed. |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1292 | expected_error_ = FILE_ERROR_OK; |
| 1293 | VerifyRemoveFromCache(FILE_ERROR_OK, resource_id, md5); |
[email protected] | f861b39 | 2012-08-03 20:41:12 | [diff] [blame] | 1294 | EXPECT_EQ(0U, CountCacheFiles(resource_id, md5)); |
| 1295 | } |
| 1296 | |
[email protected] | fb37181 | 2012-08-22 16:05:23 | [diff] [blame] | 1297 | TEST_F(DriveCacheTest, StoreToCacheNoSpace) { |
[email protected] | cf64404b | 2012-12-14 07:12:50 | [diff] [blame] | 1298 | fake_free_disk_space_getter_->set_fake_free_disk_space(0); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 1299 | |
| 1300 | std::string resource_id("pdf:1a2b"); |
| 1301 | std::string md5("abcdef0123456789"); |
| 1302 | |
| 1303 | // Try to store an existing file. |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1304 | TestStoreToCache( |
| 1305 | resource_id, md5, |
[email protected] | 3ac6b8f | 2013-03-08 14:12:38 | [diff] [blame] | 1306 | google_apis::test_util::GetTestFilePath("chromeos/gdata/root_feed.json"), |
[email protected] | 78a158b | 2013-04-23 06:57:49 | [diff] [blame] | 1307 | FILE_ERROR_NO_SPACE, |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1308 | test_util::TEST_CACHE_STATE_NONE, |
| 1309 | DriveCache::CACHE_TYPE_TMP); |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 1310 | |
| 1311 | // Verify that there's no files added. |
| 1312 | EXPECT_EQ(0U, CountCacheFiles(resource_id, md5)); |
| 1313 | } |
| 1314 | |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 1315 | // Don't use TEST_F, as we don't want SetUp() and TearDown() for this test. |
| 1316 | TEST(DriveCacheExtraTest, InitializationFailure) { |
| 1317 | MessageLoopForUI message_loop; |
| 1318 | content::TestBrowserThread ui_thread(content::BrowserThread::UI, |
| 1319 | &message_loop); |
| 1320 | |
| 1321 | scoped_refptr<base::SequencedWorkerPool> pool = |
| 1322 | content::BrowserThread::GetBlockingPool(); |
| 1323 | |
| 1324 | // Set the cache root to a non existent path, so the initialization fails. |
[email protected] | c0a3b8e8 | 2013-03-18 21:07:45 | [diff] [blame] | 1325 | scoped_ptr<DriveCache, test_util::DestroyHelperForTests> cache(new DriveCache( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 1326 | base::FilePath::FromUTF8Unsafe("/somewhere/nonexistent/blah/blah"), |
[email protected] | f6fd98a | 2012-12-14 00:04:02 | [diff] [blame] | 1327 | pool->GetSequencedTaskRunner(pool->GetSequenceToken()), |
[email protected] | c0a3b8e8 | 2013-03-18 21:07:45 | [diff] [blame] | 1328 | NULL /* free_disk_space_getter */)); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 1329 | |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 1330 | bool success = true; |
[email protected] | 77fb1a6 | 2012-11-01 13:42:32 | [diff] [blame] | 1331 | cache->RequestInitialize( |
[email protected] | e76fd27 | 2013-03-25 13:37:14 | [diff] [blame] | 1332 | google_apis::test_util::CreateCopyResultCallback(&success)); |
[email protected] | fb0fc20 | 2012-10-22 09:30:28 | [diff] [blame] | 1333 | google_apis::test_util::RunBlockingPoolTask(); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 1334 | EXPECT_FALSE(success); |
[email protected] | 322e003 | 2012-10-07 01:55:53 | [diff] [blame] | 1335 | } |
[email protected] | f0c6700 | 2012-08-15 04:10:38 | [diff] [blame] | 1336 | |
[email protected] | d9d04df | 2012-10-12 07:06:35 | [diff] [blame] | 1337 | } // namespace drive |