| OLD | NEW | 
 | (Empty) | 
|    1 // Copyright 2014 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 <stdint.h> |  | 
|    6  |  | 
|    7 #include "base/bind.h" |  | 
|    8 #include "base/location.h" |  | 
|    9 #include "base/macros.h" |  | 
|   10 #include "base/run_loop.h" |  | 
|   11 #include "base/single_thread_task_runner.h" |  | 
|   12 #include "base/threading/thread_task_runner_handle.h" |  | 
|   13 #include "content/public/test/mock_special_storage_policy.h" |  | 
|   14 #include "net/base/url_util.h" |  | 
|   15 #include "storage/browser/quota/usage_tracker.h" |  | 
|   16 #include "testing/gtest/include/gtest/gtest.h" |  | 
|   17  |  | 
|   18 using storage::kQuotaStatusOk; |  | 
|   19 using storage::kStorageTypeTemporary; |  | 
|   20 using storage::QuotaClient; |  | 
|   21 using storage::QuotaClientList; |  | 
|   22 using storage::SpecialStoragePolicy; |  | 
|   23 using storage::StorageType; |  | 
|   24 using storage::UsageTracker; |  | 
|   25  |  | 
|   26 namespace content { |  | 
|   27  |  | 
|   28 namespace { |  | 
|   29  |  | 
|   30 void DidGetGlobalUsage(bool* done, |  | 
|   31                        int64_t* usage_out, |  | 
|   32                        int64_t* unlimited_usage_out, |  | 
|   33                        int64_t usage, |  | 
|   34                        int64_t unlimited_usage) { |  | 
|   35   EXPECT_FALSE(*done); |  | 
|   36   *done = true; |  | 
|   37   *usage_out = usage; |  | 
|   38   *unlimited_usage_out = unlimited_usage; |  | 
|   39 } |  | 
|   40  |  | 
|   41 void DidGetUsage(bool* done, int64_t* usage_out, int64_t usage) { |  | 
|   42   EXPECT_FALSE(*done); |  | 
|   43   *done = true; |  | 
|   44   *usage_out = usage; |  | 
|   45 } |  | 
|   46  |  | 
|   47 }  // namespace |  | 
|   48  |  | 
|   49 class MockQuotaClient : public QuotaClient { |  | 
|   50  public: |  | 
|   51   MockQuotaClient() {} |  | 
|   52   ~MockQuotaClient() override {} |  | 
|   53  |  | 
|   54   ID id() const override { return kFileSystem; } |  | 
|   55  |  | 
|   56   void OnQuotaManagerDestroyed() override {} |  | 
|   57  |  | 
|   58   void GetOriginUsage(const GURL& origin, |  | 
|   59                       StorageType type, |  | 
|   60                       const GetUsageCallback& callback) override { |  | 
|   61     EXPECT_EQ(kStorageTypeTemporary, type); |  | 
|   62     int64_t usage = GetUsage(origin); |  | 
|   63     base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |  | 
|   64                                                   base::Bind(callback, usage)); |  | 
|   65   } |  | 
|   66  |  | 
|   67   void GetOriginsForType(StorageType type, |  | 
|   68                          const GetOriginsCallback& callback) override { |  | 
|   69     EXPECT_EQ(kStorageTypeTemporary, type); |  | 
|   70     std::set<GURL> origins; |  | 
|   71     for (UsageMap::const_iterator itr = usage_map_.begin(); |  | 
|   72          itr != usage_map_.end(); ++itr) { |  | 
|   73       origins.insert(itr->first); |  | 
|   74     } |  | 
|   75     base::ThreadTaskRunnerHandle::Get()->PostTask( |  | 
|   76         FROM_HERE, base::Bind(callback, origins)); |  | 
|   77   } |  | 
|   78  |  | 
|   79   void GetOriginsForHost(StorageType type, |  | 
|   80                          const std::string& host, |  | 
|   81                          const GetOriginsCallback& callback) override { |  | 
|   82     EXPECT_EQ(kStorageTypeTemporary, type); |  | 
|   83     std::set<GURL> origins; |  | 
|   84     for (UsageMap::const_iterator itr = usage_map_.begin(); |  | 
|   85          itr != usage_map_.end(); ++itr) { |  | 
|   86       if (net::GetHostOrSpecFromURL(itr->first) == host) |  | 
|   87         origins.insert(itr->first); |  | 
|   88     } |  | 
|   89     base::ThreadTaskRunnerHandle::Get()->PostTask( |  | 
|   90         FROM_HERE, base::Bind(callback, origins)); |  | 
|   91   } |  | 
|   92  |  | 
|   93   void DeleteOriginData(const GURL& origin, |  | 
|   94                         StorageType type, |  | 
|   95                         const DeletionCallback& callback) override { |  | 
|   96     EXPECT_EQ(kStorageTypeTemporary, type); |  | 
|   97     usage_map_.erase(origin); |  | 
|   98     base::ThreadTaskRunnerHandle::Get()->PostTask( |  | 
|   99         FROM_HERE, base::Bind(callback, kQuotaStatusOk)); |  | 
|  100   } |  | 
|  101  |  | 
|  102   bool DoesSupport(storage::StorageType type) const override { |  | 
|  103     return type == storage::kStorageTypeTemporary; |  | 
|  104   } |  | 
|  105  |  | 
|  106   int64_t GetUsage(const GURL& origin) { |  | 
|  107     UsageMap::const_iterator found = usage_map_.find(origin); |  | 
|  108     if (found == usage_map_.end()) |  | 
|  109       return 0; |  | 
|  110     return found->second; |  | 
|  111   } |  | 
|  112  |  | 
|  113   void SetUsage(const GURL& origin, int64_t usage) { |  | 
|  114     usage_map_[origin] = usage; |  | 
|  115   } |  | 
|  116  |  | 
|  117   int64_t UpdateUsage(const GURL& origin, int64_t delta) { |  | 
|  118     return usage_map_[origin] += delta; |  | 
|  119   } |  | 
|  120  |  | 
|  121  private: |  | 
|  122   typedef std::map<GURL, int64_t> UsageMap; |  | 
|  123  |  | 
|  124   UsageMap usage_map_; |  | 
|  125  |  | 
|  126   DISALLOW_COPY_AND_ASSIGN(MockQuotaClient); |  | 
|  127 }; |  | 
|  128  |  | 
|  129 class UsageTrackerTest : public testing::Test { |  | 
|  130  public: |  | 
|  131   UsageTrackerTest() |  | 
|  132       : storage_policy_(new MockSpecialStoragePolicy()), |  | 
|  133         usage_tracker_(GetUsageTrackerList(), kStorageTypeTemporary, |  | 
|  134                        storage_policy_.get(), NULL) { |  | 
|  135   } |  | 
|  136  |  | 
|  137   ~UsageTrackerTest() override {} |  | 
|  138  |  | 
|  139   UsageTracker* usage_tracker() { |  | 
|  140     return &usage_tracker_; |  | 
|  141   } |  | 
|  142  |  | 
|  143   void UpdateUsage(const GURL& origin, int64_t delta) { |  | 
|  144     quota_client_.UpdateUsage(origin, delta); |  | 
|  145     usage_tracker_.UpdateUsageCache(quota_client_.id(), origin, delta); |  | 
|  146     base::RunLoop().RunUntilIdle(); |  | 
|  147   } |  | 
|  148  |  | 
|  149   void UpdateUsageWithoutNotification(const GURL& origin, int64_t delta) { |  | 
|  150     quota_client_.UpdateUsage(origin, delta); |  | 
|  151   } |  | 
|  152  |  | 
|  153   void GetGlobalLimitedUsage(int64_t* limited_usage) { |  | 
|  154     bool done = false; |  | 
|  155     usage_tracker_.GetGlobalLimitedUsage(base::Bind( |  | 
|  156         &DidGetUsage, &done, limited_usage)); |  | 
|  157     base::RunLoop().RunUntilIdle(); |  | 
|  158  |  | 
|  159     EXPECT_TRUE(done); |  | 
|  160   } |  | 
|  161  |  | 
|  162   void GetGlobalUsage(int64_t* usage, int64_t* unlimited_usage) { |  | 
|  163     bool done = false; |  | 
|  164     usage_tracker_.GetGlobalUsage(base::Bind( |  | 
|  165         &DidGetGlobalUsage, |  | 
|  166         &done, usage, unlimited_usage)); |  | 
|  167     base::RunLoop().RunUntilIdle(); |  | 
|  168  |  | 
|  169     EXPECT_TRUE(done); |  | 
|  170   } |  | 
|  171  |  | 
|  172   void GetHostUsage(const std::string& host, int64_t* usage) { |  | 
|  173     bool done = false; |  | 
|  174     usage_tracker_.GetHostUsage(host, base::Bind(&DidGetUsage, &done, usage)); |  | 
|  175     base::RunLoop().RunUntilIdle(); |  | 
|  176  |  | 
|  177     EXPECT_TRUE(done); |  | 
|  178   } |  | 
|  179  |  | 
|  180   void GrantUnlimitedStoragePolicy(const GURL& origin) { |  | 
|  181     if (!storage_policy_->IsStorageUnlimited(origin)) { |  | 
|  182       storage_policy_->AddUnlimited(origin); |  | 
|  183       storage_policy_->NotifyGranted( |  | 
|  184           origin, SpecialStoragePolicy::STORAGE_UNLIMITED); |  | 
|  185     } |  | 
|  186   } |  | 
|  187  |  | 
|  188   void RevokeUnlimitedStoragePolicy(const GURL& origin) { |  | 
|  189     if (storage_policy_->IsStorageUnlimited(origin)) { |  | 
|  190       storage_policy_->RemoveUnlimited(origin); |  | 
|  191       storage_policy_->NotifyRevoked( |  | 
|  192           origin, SpecialStoragePolicy::STORAGE_UNLIMITED); |  | 
|  193     } |  | 
|  194   } |  | 
|  195  |  | 
|  196   void SetUsageCacheEnabled(const GURL& origin, bool enabled) { |  | 
|  197     usage_tracker_.SetUsageCacheEnabled( |  | 
|  198         quota_client_.id(), origin, enabled); |  | 
|  199   } |  | 
|  200  |  | 
|  201  private: |  | 
|  202   QuotaClientList GetUsageTrackerList() { |  | 
|  203     QuotaClientList client_list; |  | 
|  204     client_list.push_back("a_client_); |  | 
|  205     return client_list; |  | 
|  206   } |  | 
|  207  |  | 
|  208   base::MessageLoop message_loop_; |  | 
|  209  |  | 
|  210   scoped_refptr<MockSpecialStoragePolicy> storage_policy_; |  | 
|  211   MockQuotaClient quota_client_; |  | 
|  212   UsageTracker usage_tracker_; |  | 
|  213  |  | 
|  214   DISALLOW_COPY_AND_ASSIGN(UsageTrackerTest); |  | 
|  215 }; |  | 
|  216  |  | 
|  217 TEST_F(UsageTrackerTest, GrantAndRevokeUnlimitedStorage) { |  | 
|  218   int64_t usage = 0; |  | 
|  219   int64_t unlimited_usage = 0; |  | 
|  220   int64_t host_usage = 0; |  | 
|  221   GetGlobalUsage(&usage, &unlimited_usage); |  | 
|  222   EXPECT_EQ(0, usage); |  | 
|  223   EXPECT_EQ(0, unlimited_usage); |  | 
|  224  |  | 
|  225   const GURL origin("http://example.com"); |  | 
|  226   const std::string host(net::GetHostOrSpecFromURL(origin)); |  | 
|  227  |  | 
|  228   UpdateUsage(origin, 100); |  | 
|  229   GetGlobalUsage(&usage, &unlimited_usage); |  | 
|  230   GetHostUsage(host, &host_usage); |  | 
|  231   EXPECT_EQ(100, usage); |  | 
|  232   EXPECT_EQ(0, unlimited_usage); |  | 
|  233   EXPECT_EQ(100, host_usage); |  | 
|  234  |  | 
|  235   GrantUnlimitedStoragePolicy(origin); |  | 
|  236   GetGlobalUsage(&usage, &unlimited_usage); |  | 
|  237   GetHostUsage(host, &host_usage); |  | 
|  238   EXPECT_EQ(100, usage); |  | 
|  239   EXPECT_EQ(100, unlimited_usage); |  | 
|  240   EXPECT_EQ(100, host_usage); |  | 
|  241  |  | 
|  242   RevokeUnlimitedStoragePolicy(origin); |  | 
|  243   GetGlobalUsage(&usage, &unlimited_usage); |  | 
|  244   GetHostUsage(host, &host_usage); |  | 
|  245   EXPECT_EQ(100, usage); |  | 
|  246   EXPECT_EQ(0, unlimited_usage); |  | 
|  247   EXPECT_EQ(100, host_usage); |  | 
|  248 } |  | 
|  249  |  | 
|  250 TEST_F(UsageTrackerTest, CacheDisabledClientTest) { |  | 
|  251   int64_t usage = 0; |  | 
|  252   int64_t unlimited_usage = 0; |  | 
|  253   int64_t host_usage = 0; |  | 
|  254  |  | 
|  255   const GURL origin("http://example.com"); |  | 
|  256   const std::string host(net::GetHostOrSpecFromURL(origin)); |  | 
|  257  |  | 
|  258   UpdateUsage(origin, 100); |  | 
|  259   GetGlobalUsage(&usage, &unlimited_usage); |  | 
|  260   GetHostUsage(host, &host_usage); |  | 
|  261   EXPECT_EQ(100, usage); |  | 
|  262   EXPECT_EQ(0, unlimited_usage); |  | 
|  263   EXPECT_EQ(100, host_usage); |  | 
|  264  |  | 
|  265   UpdateUsageWithoutNotification(origin, 100); |  | 
|  266   GetGlobalUsage(&usage, &unlimited_usage); |  | 
|  267   GetHostUsage(host, &host_usage); |  | 
|  268   EXPECT_EQ(100, usage); |  | 
|  269   EXPECT_EQ(0, unlimited_usage); |  | 
|  270   EXPECT_EQ(100, host_usage); |  | 
|  271  |  | 
|  272   GrantUnlimitedStoragePolicy(origin); |  | 
|  273   UpdateUsageWithoutNotification(origin, 100); |  | 
|  274   SetUsageCacheEnabled(origin, false); |  | 
|  275   UpdateUsageWithoutNotification(origin, 100); |  | 
|  276  |  | 
|  277   GetGlobalUsage(&usage, &unlimited_usage); |  | 
|  278   GetHostUsage(host, &host_usage); |  | 
|  279   EXPECT_EQ(400, usage); |  | 
|  280   EXPECT_EQ(400, unlimited_usage); |  | 
|  281   EXPECT_EQ(400, host_usage); |  | 
|  282  |  | 
|  283   RevokeUnlimitedStoragePolicy(origin); |  | 
|  284   GetGlobalUsage(&usage, &unlimited_usage); |  | 
|  285   GetHostUsage(host, &host_usage); |  | 
|  286   EXPECT_EQ(400, usage); |  | 
|  287   EXPECT_EQ(0, unlimited_usage); |  | 
|  288   EXPECT_EQ(400, host_usage); |  | 
|  289  |  | 
|  290   SetUsageCacheEnabled(origin, true); |  | 
|  291   UpdateUsage(origin, 100); |  | 
|  292  |  | 
|  293   GetGlobalUsage(&usage, &unlimited_usage); |  | 
|  294   GetHostUsage(host, &host_usage); |  | 
|  295   EXPECT_EQ(500, usage); |  | 
|  296   EXPECT_EQ(0, unlimited_usage); |  | 
|  297   EXPECT_EQ(500, host_usage); |  | 
|  298 } |  | 
|  299  |  | 
|  300 TEST_F(UsageTrackerTest, LimitedGlobalUsageTest) { |  | 
|  301   const GURL kNormal("http://normal"); |  | 
|  302   const GURL kUnlimited("http://unlimited"); |  | 
|  303   const GURL kNonCached("http://non_cached"); |  | 
|  304   const GURL kNonCachedUnlimited("http://non_cached-unlimited"); |  | 
|  305  |  | 
|  306   GrantUnlimitedStoragePolicy(kUnlimited); |  | 
|  307   GrantUnlimitedStoragePolicy(kNonCachedUnlimited); |  | 
|  308  |  | 
|  309   SetUsageCacheEnabled(kNonCached, false); |  | 
|  310   SetUsageCacheEnabled(kNonCachedUnlimited, false); |  | 
|  311  |  | 
|  312   UpdateUsageWithoutNotification(kNormal, 1); |  | 
|  313   UpdateUsageWithoutNotification(kUnlimited, 2); |  | 
|  314   UpdateUsageWithoutNotification(kNonCached, 4); |  | 
|  315   UpdateUsageWithoutNotification(kNonCachedUnlimited, 8); |  | 
|  316  |  | 
|  317   int64_t limited_usage = 0; |  | 
|  318   int64_t total_usage = 0; |  | 
|  319   int64_t unlimited_usage = 0; |  | 
|  320  |  | 
|  321   GetGlobalLimitedUsage(&limited_usage); |  | 
|  322   GetGlobalUsage(&total_usage, &unlimited_usage); |  | 
|  323   EXPECT_EQ(1 + 4, limited_usage); |  | 
|  324   EXPECT_EQ(1 + 2 + 4 + 8, total_usage); |  | 
|  325   EXPECT_EQ(2 + 8, unlimited_usage); |  | 
|  326  |  | 
|  327   UpdateUsageWithoutNotification(kNonCached, 16 - 4); |  | 
|  328   UpdateUsageWithoutNotification(kNonCachedUnlimited, 32 - 8); |  | 
|  329  |  | 
|  330   GetGlobalLimitedUsage(&limited_usage); |  | 
|  331   GetGlobalUsage(&total_usage, &unlimited_usage); |  | 
|  332   EXPECT_EQ(1 + 16, limited_usage); |  | 
|  333   EXPECT_EQ(1 + 2 + 16 + 32, total_usage); |  | 
|  334   EXPECT_EQ(2 + 32, unlimited_usage); |  | 
|  335 } |  | 
|  336  |  | 
|  337  |  | 
|  338 }  // namespace content |  | 
| OLD | NEW |