| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "webkit/browser/fileapi/quota/quota_backend_impl.h" | |
| 6 | |
| 7 #include "base/files/scoped_temp_dir.h" | |
| 8 #include "base/message_loop/message_loop.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "webkit/browser/fileapi/file_system_usage_cache.h" | |
| 11 #include "webkit/browser/fileapi/obfuscated_file_util.h" | |
| 12 #include "webkit/browser/quota/quota_manager.h" | |
| 13 | |
| 14 namespace fileapi { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 const char kOrigin[] = "http://example.com"; | |
| 19 | |
| 20 bool DidReserveQuota(bool accepted, | |
| 21 base::PlatformFileError* error_out, | |
| 22 base::PlatformFileError error) { | |
| 23 DCHECK(error_out); | |
| 24 *error_out = error; | |
| 25 return accepted; | |
| 26 } | |
| 27 | |
| 28 class MockQuotaManagerProxy : public quota::QuotaManagerProxy { | |
| 29 public: | |
| 30 MockQuotaManagerProxy() | |
| 31 : QuotaManagerProxy(NULL, NULL), | |
| 32 storage_modified_count_(0), | |
| 33 usage_(0), quota_(0) {} | |
| 34 | |
| 35 // We don't mock them. | |
| 36 virtual void NotifyOriginInUse(const GURL& origin) OVERRIDE {} | |
| 37 virtual void NotifyOriginNoLongerInUse(const GURL& origin) OVERRIDE {} | |
| 38 virtual void SetUsageCacheEnabled(quota::QuotaClient::ID client_id, | |
| 39 const GURL& origin, | |
| 40 quota::StorageType type, | |
| 41 bool enabled) OVERRIDE {} | |
| 42 | |
| 43 virtual void NotifyStorageModified( | |
| 44 quota::QuotaClient::ID client_id, | |
| 45 const GURL& origin, | |
| 46 quota::StorageType type, | |
| 47 int64 delta) OVERRIDE { | |
| 48 ++storage_modified_count_; | |
| 49 usage_ += delta; | |
| 50 ASSERT_LT(usage_, quota_); | |
| 51 } | |
| 52 | |
| 53 virtual void GetUsageAndQuota( | |
| 54 base::SequencedTaskRunner* original_task_runner, | |
| 55 const GURL& origin, | |
| 56 quota::StorageType type, | |
| 57 const GetUsageAndQuotaCallback& callback) OVERRIDE { | |
| 58 callback.Run(quota::kQuotaStatusOk, usage_, quota_); | |
| 59 } | |
| 60 | |
| 61 int storage_modified_count() { return storage_modified_count_; } | |
| 62 int64 usage() { return usage_; } | |
| 63 void set_usage(int64 usage) { usage_ = usage; } | |
| 64 void set_quota(int64 quota) { quota_ = quota; } | |
| 65 | |
| 66 protected: | |
| 67 virtual ~MockQuotaManagerProxy() {} | |
| 68 | |
| 69 private: | |
| 70 int storage_modified_count_; | |
| 71 int64 usage_; | |
| 72 int64 quota_; | |
| 73 | |
| 74 DISALLOW_COPY_AND_ASSIGN(MockQuotaManagerProxy); | |
| 75 }; | |
| 76 | |
| 77 } // namespace | |
| 78 | |
| 79 class QuotaBackendImplTest : public testing::Test { | |
| 80 public: | |
| 81 QuotaBackendImplTest() | |
| 82 : file_system_usage_cache_(file_task_runner()), | |
| 83 quota_manager_proxy_(new MockQuotaManagerProxy) {} | |
| 84 | |
| 85 virtual void SetUp() OVERRIDE { | |
| 86 ASSERT_TRUE(data_dir_.CreateUniqueTempDir()); | |
| 87 file_util_.reset(ObfuscatedFileUtil::CreateForTesting( | |
| 88 NULL, data_dir_.path(), file_task_runner())); | |
| 89 backend_.reset(new QuotaBackendImpl(file_task_runner(), | |
| 90 file_util_.get(), | |
| 91 &file_system_usage_cache_, | |
| 92 quota_manager_proxy_.get())); | |
| 93 } | |
| 94 | |
| 95 virtual void TearDown() OVERRIDE { | |
| 96 backend_.reset(); | |
| 97 quota_manager_proxy_ = NULL; | |
| 98 file_util_.reset(); | |
| 99 message_loop_.RunUntilIdle(); | |
| 100 } | |
| 101 | |
| 102 protected: | |
| 103 void InitializeForOriginAndType(const GURL& origin, FileSystemType type) { | |
| 104 file_util_->InitOriginDatabase(origin, true /* create */); | |
| 105 ASSERT_TRUE(file_util_->origin_database_ != NULL); | |
| 106 | |
| 107 std::string type_string = | |
| 108 SandboxFileSystemBackendDelegate::GetTypeString(type); | |
| 109 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 110 base::FilePath path = file_util_->GetDirectoryForOriginAndType( | |
| 111 origin, type_string, true /* create */, &error); | |
| 112 ASSERT_EQ(base::PLATFORM_FILE_OK, error); | |
| 113 file_system_usage_cache_.UpdateUsage(GetUsageCachePath(origin, type), 0); | |
| 114 } | |
| 115 | |
| 116 base::SequencedTaskRunner* file_task_runner() { | |
| 117 return base::MessageLoopProxy::current().get(); | |
| 118 } | |
| 119 | |
| 120 base::FilePath GetUsageCachePath(const GURL& origin, FileSystemType type) { | |
| 121 base::FilePath path; | |
| 122 DCHECK_EQ(base::PLATFORM_FILE_OK, | |
| 123 backend_->GetUsageCachePath(origin, type, &path)); | |
| 124 DCHECK(!path.empty()); | |
| 125 return path; | |
| 126 } | |
| 127 | |
| 128 base::MessageLoop message_loop_; | |
| 129 base::ScopedTempDir data_dir_; | |
| 130 scoped_ptr<ObfuscatedFileUtil> file_util_; | |
| 131 FileSystemUsageCache file_system_usage_cache_; | |
| 132 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; | |
| 133 scoped_ptr<QuotaBackendImpl> backend_; | |
| 134 | |
| 135 private: | |
| 136 DISALLOW_COPY_AND_ASSIGN(QuotaBackendImplTest); | |
| 137 }; | |
| 138 | |
| 139 TEST_F(QuotaBackendImplTest, ReserveQuota_Basic) { | |
| 140 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 141 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 142 quota_manager_proxy_->set_quota(10000); | |
| 143 | |
| 144 const int64 kDelta1 = 1000; | |
| 145 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 146 backend_->ReserveQuota(GURL(kOrigin), type, kDelta1, | |
| 147 base::Bind(&DidReserveQuota, true, &error)); | |
| 148 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
| 149 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); | |
| 150 | |
| 151 const int64 kDelta2 = -300; | |
| 152 error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 153 backend_->ReserveQuota(GURL(kOrigin), type, kDelta2, | |
| 154 base::Bind(&DidReserveQuota, true, &error)); | |
| 155 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
| 156 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); | |
| 157 | |
| 158 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | |
| 159 } | |
| 160 | |
| 161 TEST_F(QuotaBackendImplTest, ReserveQuota_NoSpace) { | |
| 162 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 163 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 164 quota_manager_proxy_->set_quota(100); | |
| 165 | |
| 166 const int64 kDelta = 1000; | |
| 167 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 168 backend_->ReserveQuota(GURL(kOrigin), type, kDelta, | |
| 169 base::Bind(&DidReserveQuota, true, &error)); | |
| 170 EXPECT_EQ(base::PLATFORM_FILE_ERROR_NO_SPACE, error); | |
| 171 EXPECT_EQ(0, quota_manager_proxy_->usage()); | |
| 172 | |
| 173 EXPECT_EQ(0, quota_manager_proxy_->storage_modified_count()); | |
| 174 } | |
| 175 | |
| 176 TEST_F(QuotaBackendImplTest, ReserveQuota_Revert) { | |
| 177 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 178 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 179 quota_manager_proxy_->set_quota(10000); | |
| 180 | |
| 181 const int64 kDelta = 1000; | |
| 182 base::PlatformFileError error = base::PLATFORM_FILE_ERROR_FAILED; | |
| 183 backend_->ReserveQuota(GURL(kOrigin), type, kDelta, | |
| 184 base::Bind(&DidReserveQuota, false, &error)); | |
| 185 EXPECT_EQ(base::PLATFORM_FILE_OK, error); | |
| 186 EXPECT_EQ(0, quota_manager_proxy_->usage()); | |
| 187 | |
| 188 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | |
| 189 } | |
| 190 | |
| 191 TEST_F(QuotaBackendImplTest, ReleaseReservedQuota) { | |
| 192 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 193 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 194 const int64 kInitialUsage = 2000; | |
| 195 quota_manager_proxy_->set_usage(kInitialUsage); | |
| 196 quota_manager_proxy_->set_quota(10000); | |
| 197 | |
| 198 const int64 kSize = 1000; | |
| 199 backend_->ReleaseReservedQuota(GURL(kOrigin), type, kSize); | |
| 200 EXPECT_EQ(kInitialUsage - kSize, quota_manager_proxy_->usage()); | |
| 201 | |
| 202 EXPECT_EQ(1, quota_manager_proxy_->storage_modified_count()); | |
| 203 } | |
| 204 | |
| 205 TEST_F(QuotaBackendImplTest, CommitQuotaUsage) { | |
| 206 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 207 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 208 quota_manager_proxy_->set_quota(10000); | |
| 209 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); | |
| 210 | |
| 211 const int64 kDelta1 = 1000; | |
| 212 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta1); | |
| 213 EXPECT_EQ(kDelta1, quota_manager_proxy_->usage()); | |
| 214 int64 usage = 0; | |
| 215 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); | |
| 216 EXPECT_EQ(kDelta1, usage); | |
| 217 | |
| 218 const int64 kDelta2 = -300; | |
| 219 backend_->CommitQuotaUsage(GURL(kOrigin), type, kDelta2); | |
| 220 EXPECT_EQ(kDelta1 + kDelta2, quota_manager_proxy_->usage()); | |
| 221 usage = 0; | |
| 222 EXPECT_TRUE(file_system_usage_cache_.GetUsage(path, &usage)); | |
| 223 EXPECT_EQ(kDelta1 + kDelta2, usage); | |
| 224 | |
| 225 EXPECT_EQ(2, quota_manager_proxy_->storage_modified_count()); | |
| 226 } | |
| 227 | |
| 228 TEST_F(QuotaBackendImplTest, DirtyCount) { | |
| 229 FileSystemType type = fileapi::kFileSystemTypeTemporary; | |
| 230 InitializeForOriginAndType(GURL(kOrigin), type); | |
| 231 base::FilePath path = GetUsageCachePath(GURL(kOrigin), type); | |
| 232 | |
| 233 backend_->IncrementDirtyCount(GURL(kOrigin), type); | |
| 234 uint32 dirty = 0; | |
| 235 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); | |
| 236 EXPECT_EQ(1u, dirty); | |
| 237 | |
| 238 backend_->DecrementDirtyCount(GURL(kOrigin), type); | |
| 239 ASSERT_TRUE(file_system_usage_cache_.GetDirty(path, &dirty)); | |
| 240 EXPECT_EQ(0u, dirty); | |
| 241 } | |
| 242 | |
| 243 } // namespace fileapi | |
| OLD | NEW |