summaryrefslogtreecommitdiffstats
path: root/extensions/browser/quota_service_unittest.cc
diff options
context:
space:
mode:
authorbenwells@chromium.org <benwells@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-09 17:34:20 +0000
committerbenwells@chromium.org <benwells@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-11-09 17:34:20 +0000
commit38427a15feef46643bd5d85f56a6885fa2b7a976 (patch)
tree390eea7de7cfe3759c5fcc2170599ff2142c6a8d /extensions/browser/quota_service_unittest.cc
parent56c953af6bb2be5fcc2eac19a0f227ca7ed594e0 (diff)
downloadchromium_src-38427a15feef46643bd5d85f56a6885fa2b7a976.zip
chromium_src-38427a15feef46643bd5d85f56a6885fa2b7a976.tar.gz
chromium_src-38427a15feef46643bd5d85f56a6885fa2b7a976.tar.bz2
Moved ExtensionInfoMap and ExtensionsQuotaService to extensions/
In the move these classes became extensions::InfoMap and extensions::QuotaService. TBR=sky BUG=162530 Review URL: https://codereview.chromium.org/63933003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@234131 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'extensions/browser/quota_service_unittest.cc')
-rw-r--r--extensions/browser/quota_service_unittest.cc417
1 files changed, 417 insertions, 0 deletions
diff --git a/extensions/browser/quota_service_unittest.cc b/extensions/browser/quota_service_unittest.cc
new file mode 100644
index 0000000..ee3316a
--- /dev/null
+++ b/extensions/browser/quota_service_unittest.cc
@@ -0,0 +1,417 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/message_loop/message_loop.h"
+#include "base/process/process.h"
+#include "base/stl_util.h"
+#include "base/strings/string_util.h"
+#include "chrome/browser/extensions/extension_function.h"
+#include "content/public/test/test_browser_thread.h"
+#include "extensions/browser/quota_service.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using base::TimeDelta;
+using base::TimeTicks;
+using content::BrowserThread;
+
+namespace extensions {
+
+typedef QuotaLimitHeuristic::Bucket Bucket;
+typedef QuotaLimitHeuristic::Config Config;
+typedef QuotaLimitHeuristic::BucketList BucketList;
+typedef QuotaService::TimedLimit TimedLimit;
+typedef QuotaService::SustainedLimit SustainedLimit;
+
+namespace {
+
+const char kGenericName[] = "name";
+const Config kFrozenConfig = {0, TimeDelta::FromDays(0)};
+const Config k2PerMinute = {2, TimeDelta::FromMinutes(1)};
+const Config k20PerHour = {20, TimeDelta::FromHours(1)};
+const TimeTicks kStartTime = TimeTicks();
+const TimeTicks k1MinuteAfterStart = kStartTime + TimeDelta::FromMinutes(1);
+
+class Mapper : public QuotaLimitHeuristic::BucketMapper {
+ public:
+ Mapper() {}
+ virtual ~Mapper() { STLDeleteValues(&buckets_); }
+ virtual void GetBucketsForArgs(const base::ListValue* args,
+ BucketList* buckets) OVERRIDE {
+ for (size_t i = 0; i < args->GetSize(); i++) {
+ int id;
+ ASSERT_TRUE(args->GetInteger(i, &id));
+ if (buckets_.find(id) == buckets_.end())
+ buckets_[id] = new Bucket();
+ buckets->push_back(buckets_[id]);
+ }
+ }
+
+ private:
+ typedef std::map<int, Bucket*> BucketMap;
+ BucketMap buckets_;
+ DISALLOW_COPY_AND_ASSIGN(Mapper);
+};
+
+class MockMapper : public QuotaLimitHeuristic::BucketMapper {
+ public:
+ virtual void GetBucketsForArgs(const base::ListValue* args,
+ BucketList* buckets) OVERRIDE {}
+};
+
+class MockFunction : public ExtensionFunction {
+ public:
+ explicit MockFunction(const std::string& name) { set_name(name); }
+
+ virtual void SetArgs(const base::ListValue* args) OVERRIDE {}
+ virtual const std::string GetError() OVERRIDE { return std::string(); }
+ virtual void SetError(const std::string& error) OVERRIDE {}
+ virtual void Run() OVERRIDE {}
+ virtual void Destruct() const OVERRIDE { delete this; }
+ virtual bool RunImpl() OVERRIDE { return true; }
+ virtual void SendResponse(bool) OVERRIDE {}
+
+ protected:
+ virtual ~MockFunction() {}
+};
+
+class TimedLimitMockFunction : public MockFunction {
+ public:
+ explicit TimedLimitMockFunction(const std::string& name)
+ : MockFunction(name) {}
+ virtual void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const
+ OVERRIDE {
+ heuristics->push_back(
+ new TimedLimit(k2PerMinute, new Mapper(), kGenericName));
+ }
+
+ private:
+ virtual ~TimedLimitMockFunction() {}
+};
+
+class ChainedLimitsMockFunction : public MockFunction {
+ public:
+ explicit ChainedLimitsMockFunction(const std::string& name)
+ : MockFunction(name) {}
+ virtual void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const
+ OVERRIDE {
+ // No more than 2 per minute sustained over 5 minutes.
+ heuristics->push_back(new SustainedLimit(
+ TimeDelta::FromMinutes(5), k2PerMinute, new Mapper(), kGenericName));
+ // No more than 20 per hour.
+ heuristics->push_back(
+ new TimedLimit(k20PerHour, new Mapper(), kGenericName));
+ }
+
+ private:
+ virtual ~ChainedLimitsMockFunction() {}
+};
+
+class FrozenMockFunction : public MockFunction {
+ public:
+ explicit FrozenMockFunction(const std::string& name) : MockFunction(name) {}
+ virtual void GetQuotaLimitHeuristics(QuotaLimitHeuristics* heuristics) const
+ OVERRIDE {
+ heuristics->push_back(
+ new TimedLimit(kFrozenConfig, new Mapper(), kGenericName));
+ }
+
+ private:
+ virtual ~FrozenMockFunction() {}
+};
+} // namespace
+
+class QuotaServiceTest : public testing::Test {
+ public:
+ QuotaServiceTest()
+ : extension_a_("a"),
+ extension_b_("b"),
+ extension_c_("c"),
+ loop_(),
+ ui_thread_(BrowserThread::UI, &loop_) {}
+ virtual void SetUp() { service_.reset(new QuotaService()); }
+ virtual void TearDown() {
+ loop_.RunUntilIdle();
+ service_.reset();
+ }
+
+ protected:
+ std::string extension_a_;
+ std::string extension_b_;
+ std::string extension_c_;
+ scoped_ptr<QuotaService> service_;
+ base::MessageLoop loop_;
+ content::TestBrowserThread ui_thread_;
+};
+
+class QuotaLimitHeuristicTest : public testing::Test {
+ public:
+ static void DoMoreThan2PerMinuteFor5Minutes(const TimeTicks& start_time,
+ QuotaLimitHeuristic* lim,
+ Bucket* b,
+ int an_unexhausted_minute) {
+ for (int i = 0; i < 5; i++) {
+ // Perform one operation in each minute.
+ int m = i * 60;
+ EXPECT_TRUE(lim->Apply(b, start_time + TimeDelta::FromSeconds(10 + m)));
+ EXPECT_TRUE(b->has_tokens());
+
+ if (i == an_unexhausted_minute)
+ continue; // Don't exhaust all tokens this minute.
+
+ EXPECT_TRUE(lim->Apply(b, start_time + TimeDelta::FromSeconds(15 + m)));
+ EXPECT_FALSE(b->has_tokens());
+
+ // These are OK because we haven't exhausted all buckets.
+ EXPECT_TRUE(lim->Apply(b, start_time + TimeDelta::FromSeconds(20 + m)));
+ EXPECT_FALSE(b->has_tokens());
+ EXPECT_TRUE(lim->Apply(b, start_time + TimeDelta::FromSeconds(50 + m)));
+ EXPECT_FALSE(b->has_tokens());
+ }
+ }
+};
+
+TEST_F(QuotaLimitHeuristicTest, Timed) {
+ TimedLimit lim(k2PerMinute, new MockMapper(), kGenericName);
+ Bucket b;
+
+ b.Reset(k2PerMinute, kStartTime);
+ EXPECT_TRUE(lim.Apply(&b, kStartTime));
+ EXPECT_TRUE(b.has_tokens());
+ EXPECT_TRUE(lim.Apply(&b, kStartTime + TimeDelta::FromSeconds(30)));
+ EXPECT_FALSE(b.has_tokens());
+ EXPECT_FALSE(lim.Apply(&b, k1MinuteAfterStart));
+
+ b.Reset(k2PerMinute, kStartTime);
+ EXPECT_TRUE(lim.Apply(&b, k1MinuteAfterStart - TimeDelta::FromSeconds(1)));
+ EXPECT_TRUE(lim.Apply(&b, k1MinuteAfterStart));
+ EXPECT_TRUE(lim.Apply(&b, k1MinuteAfterStart + TimeDelta::FromSeconds(1)));
+ EXPECT_TRUE(lim.Apply(&b, k1MinuteAfterStart + TimeDelta::FromSeconds(2)));
+ EXPECT_FALSE(lim.Apply(&b, k1MinuteAfterStart + TimeDelta::FromSeconds(3)));
+}
+
+TEST_F(QuotaLimitHeuristicTest, Sustained) {
+ SustainedLimit lim(
+ TimeDelta::FromMinutes(5), k2PerMinute, new MockMapper(), kGenericName);
+ Bucket bucket;
+
+ bucket.Reset(k2PerMinute, kStartTime);
+ DoMoreThan2PerMinuteFor5Minutes(kStartTime, &lim, &bucket, -1);
+ // This straw breaks the camel's back.
+ EXPECT_FALSE(lim.Apply(&bucket, kStartTime + TimeDelta::FromMinutes(6)));
+
+ // The heuristic resets itself on a safe request.
+ EXPECT_TRUE(lim.Apply(&bucket, kStartTime + TimeDelta::FromDays(1)));
+
+ // Do the same as above except don't exhaust final bucket.
+ bucket.Reset(k2PerMinute, kStartTime);
+ DoMoreThan2PerMinuteFor5Minutes(kStartTime, &lim, &bucket, -1);
+ EXPECT_TRUE(lim.Apply(&bucket, kStartTime + TimeDelta::FromMinutes(7)));
+
+ // Do the same as above except don't exhaust the 3rd (w.l.o.g) bucket.
+ bucket.Reset(k2PerMinute, kStartTime);
+ DoMoreThan2PerMinuteFor5Minutes(kStartTime, &lim, &bucket, 3);
+ // If the 3rd bucket were exhausted, this would fail (see first test).
+ EXPECT_TRUE(lim.Apply(&bucket, kStartTime + TimeDelta::FromMinutes(6)));
+}
+
+TEST_F(QuotaServiceTest, NoHeuristic) {
+ scoped_refptr<MockFunction> f(new MockFunction("foo"));
+ base::ListValue args;
+ EXPECT_EQ("", service_->Assess(extension_a_, f.get(), &args, kStartTime));
+}
+
+TEST_F(QuotaServiceTest, FrozenHeuristic) {
+ scoped_refptr<MockFunction> f(new FrozenMockFunction("foo"));
+ base::ListValue args;
+ args.Append(new base::FundamentalValue(1));
+ EXPECT_NE("", service_->Assess(extension_a_, f.get(), &args, kStartTime));
+}
+
+TEST_F(QuotaServiceTest, SingleHeuristic) {
+ scoped_refptr<MockFunction> f(new TimedLimitMockFunction("foo"));
+ base::ListValue args;
+ args.Append(new base::FundamentalValue(1));
+ EXPECT_EQ("", service_->Assess(extension_a_, f.get(), &args, kStartTime));
+ EXPECT_EQ("",
+ service_->Assess(extension_a_,
+ f.get(),
+ &args,
+ kStartTime + TimeDelta::FromSeconds(10)));
+ EXPECT_NE("",
+ service_->Assess(extension_a_,
+ f.get(),
+ &args,
+ kStartTime + TimeDelta::FromSeconds(15)));
+
+ base::ListValue args2;
+ args2.Append(new base::FundamentalValue(1));
+ args2.Append(new base::FundamentalValue(2));
+ EXPECT_EQ("", service_->Assess(extension_b_, f.get(), &args2, kStartTime));
+ EXPECT_EQ("",
+ service_->Assess(extension_b_,
+ f.get(),
+ &args2,
+ kStartTime + TimeDelta::FromSeconds(10)));
+
+ TimeDelta peace = TimeDelta::FromMinutes(30);
+ EXPECT_EQ("",
+ service_->Assess(extension_b_, f.get(), &args, kStartTime + peace));
+ EXPECT_EQ("",
+ service_->Assess(extension_b_,
+ f.get(),
+ &args,
+ kStartTime + peace + TimeDelta::FromSeconds(10)));
+ EXPECT_NE("",
+ service_->Assess(extension_b_,
+ f.get(),
+ &args2,
+ kStartTime + peace + TimeDelta::FromSeconds(15)));
+
+ // Test that items are independent.
+ base::ListValue args3;
+ args3.Append(new base::FundamentalValue(3));
+ EXPECT_EQ("", service_->Assess(extension_c_, f.get(), &args, kStartTime));
+ EXPECT_EQ("",
+ service_->Assess(extension_c_,
+ f.get(),
+ &args3,
+ kStartTime + TimeDelta::FromSeconds(10)));
+ EXPECT_EQ("",
+ service_->Assess(extension_c_,
+ f.get(),
+ &args,
+ kStartTime + TimeDelta::FromSeconds(15)));
+ EXPECT_EQ("",
+ service_->Assess(extension_c_,
+ f.get(),
+ &args3,
+ kStartTime + TimeDelta::FromSeconds(20)));
+ EXPECT_NE("",
+ service_->Assess(extension_c_,
+ f.get(),
+ &args,
+ kStartTime + TimeDelta::FromSeconds(25)));
+ EXPECT_NE("",
+ service_->Assess(extension_c_,
+ f.get(),
+ &args3,
+ kStartTime + TimeDelta::FromSeconds(30)));
+}
+
+TEST_F(QuotaServiceTest, ChainedHeuristics) {
+ scoped_refptr<MockFunction> f(new ChainedLimitsMockFunction("foo"));
+ base::ListValue args;
+ args.Append(new base::FundamentalValue(1));
+
+ // First, test that the low limit can be avoided but the higher one is hit.
+ // One event per minute for 20 minutes comes in under the sustained limit,
+ // but is equal to the timed limit.
+ for (int i = 0; i < 20; i++) {
+ EXPECT_EQ(
+ "",
+ service_->Assess(extension_a_,
+ f.get(),
+ &args,
+ kStartTime + TimeDelta::FromSeconds(10 + i * 60)));
+ }
+
+ // This will bring us to 21 events in an hour, which is a violation.
+ EXPECT_NE("",
+ service_->Assess(extension_a_,
+ f.get(),
+ &args,
+ kStartTime + TimeDelta::FromMinutes(30)));
+
+ // Now, check that we can still hit the lower limit.
+ for (int i = 0; i < 5; i++) {
+ EXPECT_EQ(
+ "",
+ service_->Assess(extension_b_,
+ f.get(),
+ &args,
+ kStartTime + TimeDelta::FromSeconds(10 + i * 60)));
+ EXPECT_EQ(
+ "",
+ service_->Assess(extension_b_,
+ f.get(),
+ &args,
+ kStartTime + TimeDelta::FromSeconds(15 + i * 60)));
+ EXPECT_EQ(
+ "",
+ service_->Assess(extension_b_,
+ f.get(),
+ &args,
+ kStartTime + TimeDelta::FromSeconds(20 + i * 60)));
+ }
+
+ EXPECT_NE("",
+ service_->Assess(extension_b_,
+ f.get(),
+ &args,
+ kStartTime + TimeDelta::FromMinutes(6)));
+}
+
+TEST_F(QuotaServiceTest, MultipleFunctionsDontInterfere) {
+ scoped_refptr<MockFunction> f(new TimedLimitMockFunction("foo"));
+ scoped_refptr<MockFunction> g(new TimedLimitMockFunction("bar"));
+
+ base::ListValue args_f;
+ base::ListValue args_g;
+ args_f.Append(new base::FundamentalValue(1));
+ args_g.Append(new base::FundamentalValue(2));
+
+ EXPECT_EQ("", service_->Assess(extension_a_, f.get(), &args_f, kStartTime));
+ EXPECT_EQ("", service_->Assess(extension_a_, g.get(), &args_g, kStartTime));
+ EXPECT_EQ("",
+ service_->Assess(extension_a_,
+ f.get(),
+ &args_f,
+ kStartTime + TimeDelta::FromSeconds(10)));
+ EXPECT_EQ("",
+ service_->Assess(extension_a_,
+ g.get(),
+ &args_g,
+ kStartTime + TimeDelta::FromSeconds(10)));
+ EXPECT_NE("",
+ service_->Assess(extension_a_,
+ f.get(),
+ &args_f,
+ kStartTime + TimeDelta::FromSeconds(15)));
+ EXPECT_NE("",
+ service_->Assess(extension_a_,
+ g.get(),
+ &args_g,
+ kStartTime + TimeDelta::FromSeconds(15)));
+}
+
+TEST_F(QuotaServiceTest, ViolatorsWillBeViolators) {
+ scoped_refptr<MockFunction> f(new TimedLimitMockFunction("foo"));
+ scoped_refptr<MockFunction> g(new TimedLimitMockFunction("bar"));
+ base::ListValue arg;
+ arg.Append(new base::FundamentalValue(1));
+ EXPECT_EQ("", service_->Assess(extension_a_, f.get(), &arg, kStartTime));
+ EXPECT_EQ("",
+ service_->Assess(extension_a_,
+ f.get(),
+ &arg,
+ kStartTime + TimeDelta::FromSeconds(10)));
+ EXPECT_NE("",
+ service_->Assess(extension_a_,
+ f.get(),
+ &arg,
+ kStartTime + TimeDelta::FromSeconds(15)));
+
+ // We don't allow this extension to use quota limited functions even if they
+ // wait a while.
+ EXPECT_NE(
+ "",
+ service_->Assess(
+ extension_a_, f.get(), &arg, kStartTime + TimeDelta::FromDays(1)));
+ EXPECT_NE(
+ "",
+ service_->Assess(
+ extension_a_, g.get(), &arg, kStartTime + TimeDelta::FromDays(1)));
+}
+
+} // namespace extensions