1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
// 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 <stddef.h>
#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/process/process.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "content/public/test/test_browser_thread.h"
#include "extensions/browser/extension_function.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;
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() {}
~Mapper() override { STLDeleteValues(&buckets_); }
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:
void GetBucketsForArgs(const base::ListValue* args,
BucketList* buckets) override {}
};
class MockFunction : public ExtensionFunction {
public:
explicit MockFunction(const char* name) { set_name(name); }
void SetArgs(const base::ListValue* args) override {}
std::string GetError() const override { return std::string(); }
void SetError(const std::string& error) override {}
void Destruct() const override { delete this; }
ResponseAction Run() override { return RespondLater(); }
void SendResponse(bool) override {}
protected:
~MockFunction() override {}
};
class TimedLimitMockFunction : public MockFunction {
public:
explicit TimedLimitMockFunction(const char* name) : MockFunction(name) {}
void GetQuotaLimitHeuristics(
QuotaLimitHeuristics* heuristics) const override {
heuristics->push_back(
new TimedLimit(k2PerMinute, new Mapper(), kGenericName));
}
private:
~TimedLimitMockFunction() override {}
};
class FrozenMockFunction : public MockFunction {
public:
explicit FrozenMockFunction(const char* name) : MockFunction(name) {}
void GetQuotaLimitHeuristics(
QuotaLimitHeuristics* heuristics) const override {
heuristics->push_back(
new TimedLimit(kFrozenConfig, new Mapper(), kGenericName));
}
private:
~FrozenMockFunction() override {}
};
} // namespace
class QuotaServiceTest : public testing::Test {
public:
QuotaServiceTest()
: extension_a_("a"),
extension_b_("b"),
extension_c_("c"),
loop_(),
ui_thread_(BrowserThread::UI, &loop_) {}
void SetUp() override { service_.reset(new QuotaService()); }
void TearDown() override {
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(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, 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, ViolatorsWillBeForgiven) {
scoped_refptr<MockFunction> f(new TimedLimitMockFunction("foo"));
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)));
// Waiting a while will give the extension access to the function again.
EXPECT_EQ("", service_->Assess(extension_a_, f.get(), &arg,
kStartTime + TimeDelta::FromDays(1)));
// And lose it again soon after.
EXPECT_EQ("", service_->Assess(extension_a_, f.get(), &arg,
kStartTime + TimeDelta::FromDays(1) +
TimeDelta::FromSeconds(10)));
EXPECT_NE("", service_->Assess(extension_a_, f.get(), &arg,
kStartTime + TimeDelta::FromDays(1) +
TimeDelta::FromSeconds(15)));
// Going further over quota should continue to fail within this time period,
// but still all restored later.
EXPECT_NE("", service_->Assess(extension_a_, f.get(), &arg,
kStartTime + TimeDelta::FromDays(1) +
TimeDelta::FromSeconds(20)));
EXPECT_NE("", service_->Assess(extension_a_, f.get(), &arg,
kStartTime + TimeDelta::FromDays(1) +
TimeDelta::FromSeconds(25)));
// Like now.
EXPECT_EQ("", service_->Assess(extension_a_, f.get(), &arg,
kStartTime + TimeDelta::FromDays(2)));
}
} // namespace extensions
|