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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
|
// Copyright (c) 2012 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 <map>
#include <queue>
#include <string>
#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/field_trial.h"
#include "base/strings/string_number_conversions.h"
#include "base/time/time.h"
#include "chrome/browser/safe_browsing/client_side_detection_service.h"
#include "chrome/common/safe_browsing/client_model.pb.h"
#include "chrome/common/safe_browsing/csd.pb.h"
#include "components/variations/variations_associated_data.h"
#include "content/public/test/test_browser_thread.h"
#include "crypto/sha2.h"
#include "net/http/http_status_code.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_status.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
using ::testing::Invoke;
using ::testing::Mock;
using ::testing::StrictMock;
using ::testing::_;
using content::BrowserThread;
namespace safe_browsing {
namespace {
class MockModelLoader : public ModelLoader {
public:
explicit MockModelLoader(const std::string model_name)
: ModelLoader(base::Closure(), model_name) {}
~MockModelLoader() override {}
MOCK_METHOD1(ScheduleFetch, void(int64));
MOCK_METHOD0(CancelFetcher, void());
private:
DISALLOW_COPY_AND_ASSIGN(MockModelLoader);
};
class MockClientSideDetectionService : public ClientSideDetectionService {
public:
MockClientSideDetectionService() : ClientSideDetectionService(NULL) {}
~MockClientSideDetectionService() override {}
private:
DISALLOW_COPY_AND_ASSIGN(MockClientSideDetectionService);
};
} // namespace
class ClientSideDetectionServiceTest : public testing::Test {
protected:
void SetUp() override {
file_thread_.reset(new content::TestBrowserThread(BrowserThread::FILE,
&msg_loop_));
factory_.reset(new net::FakeURLFetcherFactory(NULL));
browser_thread_.reset(new content::TestBrowserThread(BrowserThread::UI,
&msg_loop_));
}
void TearDown() override {
msg_loop_.RunUntilIdle();
csd_service_.reset();
file_thread_.reset();
browser_thread_.reset();
}
bool SendClientReportPhishingRequest(const GURL& phishing_url,
float score) {
ClientPhishingRequest* request = new ClientPhishingRequest();
request->set_url(phishing_url.spec());
request->set_client_score(score);
request->set_is_phishing(true); // client thinks the URL is phishing.
csd_service_->SendClientReportPhishingRequest(
request,
false,
base::Bind(&ClientSideDetectionServiceTest::SendRequestDone,
base::Unretained(this)));
phishing_url_ = phishing_url;
msg_loop_.Run(); // Waits until callback is called.
return is_phishing_;
}
bool SendClientReportMalwareRequest(const GURL& url) {
scoped_ptr<ClientMalwareRequest> request(new ClientMalwareRequest());
request->set_url(url.spec());
csd_service_->SendClientReportMalwareRequest(
request.release(),
base::Bind(&ClientSideDetectionServiceTest::SendMalwareRequestDone,
base::Unretained(this)));
phishing_url_ = url;
msg_loop_.Run(); // Waits until callback is called.
return is_malware_;
}
void SetModelFetchResponses() {
// Set reponses for both models.
factory_->SetFakeResponse(GURL(ModelLoader::kClientModelUrlPrefix +
ModelLoader::FillInModelName(false, 0)),
"bogusmodel", net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
factory_->SetFakeResponse(GURL(ModelLoader::kClientModelUrlPrefix +
ModelLoader::FillInModelName(true, 0)),
"bogusmodel", net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
}
void SetClientReportPhishingResponse(std::string response_data,
net::HttpStatusCode response_code,
net::URLRequestStatus::Status status) {
factory_->SetFakeResponse(
ClientSideDetectionService::GetClientReportUrl(
ClientSideDetectionService::kClientReportPhishingUrl),
response_data, response_code, status);
}
void SetClientReportMalwareResponse(std::string response_data,
net::HttpStatusCode response_code,
net::URLRequestStatus::Status status) {
factory_->SetFakeResponse(
ClientSideDetectionService::GetClientReportUrl(
ClientSideDetectionService::kClientReportMalwareUrl),
response_data, response_code, status);
}
int GetNumReports(std::queue<base::Time>* report_times) {
return csd_service_->GetNumReports(report_times);
}
std::queue<base::Time>& GetPhishingReportTimes() {
return csd_service_->phishing_report_times_;
}
std::queue<base::Time>& GetMalwareReportTimes() {
return csd_service_->malware_report_times_;
}
void SetCache(const GURL& gurl, bool is_phishing, base::Time time) {
csd_service_->cache_[gurl] =
make_linked_ptr(new ClientSideDetectionService::CacheState(is_phishing,
time));
}
void TestCache() {
ClientSideDetectionService::PhishingCache& cache = csd_service_->cache_;
base::Time now = base::Time::Now();
base::Time time =
now - base::TimeDelta::FromDays(
ClientSideDetectionService::kNegativeCacheIntervalDays) +
base::TimeDelta::FromMinutes(5);
cache[GURL("http://first.url.com/")] =
make_linked_ptr(new ClientSideDetectionService::CacheState(false,
time));
time =
now - base::TimeDelta::FromDays(
ClientSideDetectionService::kNegativeCacheIntervalDays) -
base::TimeDelta::FromHours(1);
cache[GURL("http://second.url.com/")] =
make_linked_ptr(new ClientSideDetectionService::CacheState(false,
time));
time =
now - base::TimeDelta::FromMinutes(
ClientSideDetectionService::kPositiveCacheIntervalMinutes) -
base::TimeDelta::FromMinutes(5);
cache[GURL("http://third.url.com/")] =
make_linked_ptr(new ClientSideDetectionService::CacheState(true, time));
time =
now - base::TimeDelta::FromMinutes(
ClientSideDetectionService::kPositiveCacheIntervalMinutes) +
base::TimeDelta::FromMinutes(5);
cache[GURL("http://fourth.url.com/")] =
make_linked_ptr(new ClientSideDetectionService::CacheState(true, time));
csd_service_->UpdateCache();
// 3 elements should be in the cache, the first, third, and fourth.
EXPECT_EQ(3U, cache.size());
EXPECT_TRUE(cache.find(GURL("http://first.url.com/")) != cache.end());
EXPECT_TRUE(cache.find(GURL("http://third.url.com/")) != cache.end());
EXPECT_TRUE(cache.find(GURL("http://fourth.url.com/")) != cache.end());
// While 3 elements remain, only the first and the fourth are actually
// valid.
bool is_phishing;
EXPECT_TRUE(csd_service_->GetValidCachedResult(
GURL("http://first.url.com"), &is_phishing));
EXPECT_FALSE(is_phishing);
EXPECT_FALSE(csd_service_->GetValidCachedResult(
GURL("http://third.url.com"), &is_phishing));
EXPECT_TRUE(csd_service_->GetValidCachedResult(
GURL("http://fourth.url.com"), &is_phishing));
EXPECT_TRUE(is_phishing);
}
void AddFeature(const std::string& name, double value,
ClientPhishingRequest* request) {
ClientPhishingRequest_Feature* feature = request->add_feature_map();
feature->set_name(name);
feature->set_value(value);
}
void AddNonModelFeature(const std::string& name, double value,
ClientPhishingRequest* request) {
ClientPhishingRequest_Feature* feature =
request->add_non_model_feature_map();
feature->set_name(name);
feature->set_value(value);
}
void CheckConfirmedMalwareUrl(GURL url) {
ASSERT_EQ(confirmed_malware_url_, url);
}
protected:
scoped_ptr<ClientSideDetectionService> csd_service_;
scoped_ptr<net::FakeURLFetcherFactory> factory_;
base::MessageLoop msg_loop_;
private:
void SendRequestDone(GURL phishing_url, bool is_phishing) {
ASSERT_EQ(phishing_url, phishing_url_);
is_phishing_ = is_phishing;
msg_loop_.Quit();
}
void SendMalwareRequestDone(GURL original_url, GURL malware_url,
bool is_malware) {
ASSERT_EQ(phishing_url_, original_url);
confirmed_malware_url_ = malware_url;
is_malware_ = is_malware;
msg_loop_.Quit();
}
scoped_ptr<content::TestBrowserThread> browser_thread_;
scoped_ptr<content::TestBrowserThread> file_thread_;
scoped_ptr<base::FieldTrialList> field_trials_;
GURL phishing_url_;
GURL confirmed_malware_url_;
bool is_phishing_;
bool is_malware_;
};
TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) {
SetModelFetchResponses();
csd_service_.reset(ClientSideDetectionService::Create(NULL));
csd_service_->SetEnabledAndRefreshState(true);
EXPECT_TRUE(csd_service_.get() != NULL);
// We delete the client-side detection service class even though the callbacks
// haven't run yet.
csd_service_.reset();
// Waiting for the callbacks to run should not crash even if the service
// object is gone.
msg_loop_.RunUntilIdle();
}
TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) {
SetModelFetchResponses();
csd_service_.reset(ClientSideDetectionService::Create(NULL));
csd_service_->SetEnabledAndRefreshState(true);
GURL url("http://a.com/");
float score = 0.4f; // Some random client score.
base::Time before = base::Time::Now();
// Invalid response body from the server.
SetClientReportPhishingResponse("invalid proto response", net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_FALSE(SendClientReportPhishingRequest(url, score));
// Normal behavior.
ClientPhishingResponse response;
response.set_phishy(true);
SetClientReportPhishingResponse(response.SerializeAsString(), net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_TRUE(SendClientReportPhishingRequest(url, score));
// This request will fail
GURL second_url("http://b.com/");
response.set_phishy(false);
SetClientReportPhishingResponse(response.SerializeAsString(),
net::HTTP_INTERNAL_SERVER_ERROR,
net::URLRequestStatus::FAILED);
EXPECT_FALSE(SendClientReportPhishingRequest(second_url, score));
base::Time after = base::Time::Now();
// Check that we have recorded all 3 requests within the correct time range.
std::queue<base::Time>& report_times = GetPhishingReportTimes();
EXPECT_EQ(3U, report_times.size());
while (!report_times.empty()) {
base::Time time = report_times.back();
report_times.pop();
EXPECT_LE(before, time);
EXPECT_GE(after, time);
}
// Only the first url should be in the cache.
bool is_phishing;
EXPECT_TRUE(csd_service_->IsInCache(url));
EXPECT_TRUE(csd_service_->GetValidCachedResult(url, &is_phishing));
EXPECT_TRUE(is_phishing);
EXPECT_FALSE(csd_service_->IsInCache(second_url));
}
TEST_F(ClientSideDetectionServiceTest, SendClientReportMalwareRequest) {
SetModelFetchResponses();
csd_service_.reset(ClientSideDetectionService::Create(NULL));
csd_service_->SetEnabledAndRefreshState(true);
GURL url("http://a.com/");
base::Time before = base::Time::Now();
// Invalid response body from the server.
SetClientReportMalwareResponse("invalid proto response", net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_FALSE(SendClientReportMalwareRequest(url));
// Missing bad_url.
ClientMalwareResponse response;
response.set_blacklist(true);
SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_FALSE(SendClientReportMalwareRequest(url));
// Normal behavior.
response.set_blacklist(true);
response.set_bad_url("http://response-bad.com/");
SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_TRUE(SendClientReportMalwareRequest(url));
CheckConfirmedMalwareUrl(GURL("http://response-bad.com/"));
// This request will fail
response.set_blacklist(false);
SetClientReportMalwareResponse(response.SerializeAsString(),
net::HTTP_INTERNAL_SERVER_ERROR,
net::URLRequestStatus::FAILED);
EXPECT_FALSE(SendClientReportMalwareRequest(url));
// Server blacklist decision is false, and response is successful
response.set_blacklist(false);
SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK,
net::URLRequestStatus::SUCCESS);
EXPECT_FALSE(SendClientReportMalwareRequest(url));
// Check that we have recorded all 5 requests within the correct time range.
base::Time after = base::Time::Now();
std::queue<base::Time>& report_times = GetMalwareReportTimes();
EXPECT_EQ(5U, report_times.size());
// Check that the malware report limit was reached.
EXPECT_TRUE(csd_service_->OverMalwareReportLimit());
report_times = GetMalwareReportTimes();
EXPECT_EQ(5U, report_times.size());
while (!report_times.empty()) {
base::Time time = report_times.back();
report_times.pop();
EXPECT_LE(before, time);
EXPECT_GE(after, time);
}
}
TEST_F(ClientSideDetectionServiceTest, GetNumReportTest) {
SetModelFetchResponses();
csd_service_.reset(ClientSideDetectionService::Create(NULL));
std::queue<base::Time>& report_times = GetPhishingReportTimes();
base::Time now = base::Time::Now();
base::TimeDelta twenty_five_hours = base::TimeDelta::FromHours(25);
report_times.push(now - twenty_five_hours);
report_times.push(now - twenty_five_hours);
report_times.push(now);
report_times.push(now);
EXPECT_EQ(2, GetNumReports(&report_times));
}
TEST_F(ClientSideDetectionServiceTest, CacheTest) {
SetModelFetchResponses();
csd_service_.reset(ClientSideDetectionService::Create(NULL));
TestCache();
}
TEST_F(ClientSideDetectionServiceTest, IsPrivateIPAddress) {
SetModelFetchResponses();
csd_service_.reset(ClientSideDetectionService::Create(NULL));
EXPECT_TRUE(csd_service_->IsPrivateIPAddress("10.1.2.3"));
EXPECT_TRUE(csd_service_->IsPrivateIPAddress("127.0.0.1"));
EXPECT_TRUE(csd_service_->IsPrivateIPAddress("172.24.3.4"));
EXPECT_TRUE(csd_service_->IsPrivateIPAddress("192.168.1.1"));
EXPECT_TRUE(csd_service_->IsPrivateIPAddress("fc00::"));
EXPECT_TRUE(csd_service_->IsPrivateIPAddress("fec0::"));
EXPECT_TRUE(csd_service_->IsPrivateIPAddress("fec0:1:2::3"));
EXPECT_TRUE(csd_service_->IsPrivateIPAddress("::1"));
EXPECT_FALSE(csd_service_->IsPrivateIPAddress("1.2.3.4"));
EXPECT_FALSE(csd_service_->IsPrivateIPAddress("200.1.1.1"));
EXPECT_FALSE(csd_service_->IsPrivateIPAddress("2001:0db8:ac10:fe01::"));
// If the address can't be parsed, the default is true.
EXPECT_TRUE(csd_service_->IsPrivateIPAddress("blah"));
}
TEST_F(ClientSideDetectionServiceTest, SetEnabledAndRefreshState) {
// Check that the model isn't downloaded until the service is enabled.
csd_service_.reset(ClientSideDetectionService::Create(NULL));
EXPECT_FALSE(csd_service_->enabled());
EXPECT_TRUE(csd_service_->model_loader_standard_->fetcher_.get() == NULL);
// Use a MockClientSideDetectionService for the rest of the test, to avoid
// the scheduling delay.
MockClientSideDetectionService* service =
new StrictMock<MockClientSideDetectionService>();
// Inject mock loaders.
MockModelLoader* loader_1 = new StrictMock<MockModelLoader>("model1");
MockModelLoader* loader_2 = new StrictMock<MockModelLoader>("model2");
service->model_loader_standard_.reset(loader_1);
service->model_loader_extended_.reset(loader_2);
csd_service_.reset(service);
EXPECT_FALSE(csd_service_->enabled());
// No calls expected yet.
Mock::VerifyAndClearExpectations(service);
Mock::VerifyAndClearExpectations(loader_1);
Mock::VerifyAndClearExpectations(loader_2);
// Check that initial ScheduleFetch() calls are made.
EXPECT_CALL(*loader_1,
ScheduleFetch(
ClientSideDetectionService::kInitialClientModelFetchDelayMs));
EXPECT_CALL(*loader_2,
ScheduleFetch(
ClientSideDetectionService::kInitialClientModelFetchDelayMs));
csd_service_->SetEnabledAndRefreshState(true);
msg_loop_.RunUntilIdle();
Mock::VerifyAndClearExpectations(service);
Mock::VerifyAndClearExpectations(loader_1);
Mock::VerifyAndClearExpectations(loader_2);
// Check that enabling again doesn't request the model.
csd_service_->SetEnabledAndRefreshState(true);
// No calls expected.
msg_loop_.RunUntilIdle();
Mock::VerifyAndClearExpectations(service);
Mock::VerifyAndClearExpectations(loader_1);
Mock::VerifyAndClearExpectations(loader_2);
// Check that disabling the service cancels pending requests.
EXPECT_CALL(*loader_1, CancelFetcher());
EXPECT_CALL(*loader_2, CancelFetcher());
csd_service_->SetEnabledAndRefreshState(false);
msg_loop_.RunUntilIdle();
Mock::VerifyAndClearExpectations(service);
Mock::VerifyAndClearExpectations(loader_1);
Mock::VerifyAndClearExpectations(loader_2);
// Check that disabling again doesn't request the model.
csd_service_->SetEnabledAndRefreshState(false);
// No calls expected.
msg_loop_.RunUntilIdle();
Mock::VerifyAndClearExpectations(service);
Mock::VerifyAndClearExpectations(loader_1);
Mock::VerifyAndClearExpectations(loader_2);
}
} // namespace safe_browsing
|