OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <map> | 5 #include <map> |
6 #include <queue> | 6 #include <queue> |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 csd_service_->SendClientReportMalwareRequest( | 100 csd_service_->SendClientReportMalwareRequest( |
101 request.release(), | 101 request.release(), |
102 base::Bind(&ClientSideDetectionServiceTest::SendMalwareRequestDone, | 102 base::Bind(&ClientSideDetectionServiceTest::SendMalwareRequestDone, |
103 base::Unretained(this))); | 103 base::Unretained(this))); |
104 phishing_url_ = url; | 104 phishing_url_ = url; |
105 msg_loop_.Run(); // Waits until callback is called. | 105 msg_loop_.Run(); // Waits until callback is called. |
106 return is_malware_; | 106 return is_malware_; |
107 } | 107 } |
108 | 108 |
109 void SetModelFetchResponse(std::string response_data, | 109 void SetModelFetchResponse(std::string response_data, |
110 net::HttpStatusCode response_code) { | 110 net::HttpStatusCode response_code, |
| 111 net::URLRequestStatus::Status status) { |
111 factory_->SetFakeResponse(GURL(ClientSideDetectionService::kClientModelUrl), | 112 factory_->SetFakeResponse(GURL(ClientSideDetectionService::kClientModelUrl), |
112 response_data, | 113 response_data, response_code, status); |
113 response_code); | |
114 } | 114 } |
115 | 115 |
116 void SetClientReportPhishingResponse(std::string response_data, | 116 void SetClientReportPhishingResponse(std::string response_data, |
117 net::HttpStatusCode response_code) { | 117 net::HttpStatusCode response_code, |
| 118 net::URLRequestStatus::Status status) { |
118 factory_->SetFakeResponse( | 119 factory_->SetFakeResponse( |
119 ClientSideDetectionService::GetClientReportUrl( | 120 ClientSideDetectionService::GetClientReportUrl( |
120 ClientSideDetectionService::kClientReportPhishingUrl), | 121 ClientSideDetectionService::kClientReportPhishingUrl), |
121 response_data, | 122 response_data, response_code, status); |
122 response_code); | |
123 } | 123 } |
124 | 124 |
125 void SetClientReportMalwareResponse(std::string response_data, | 125 void SetClientReportMalwareResponse(std::string response_data, |
126 net::HttpStatusCode response_code) { | 126 net::HttpStatusCode response_code, |
| 127 net::URLRequestStatus::Status status) { |
127 factory_->SetFakeResponse( | 128 factory_->SetFakeResponse( |
128 ClientSideDetectionService::GetClientReportUrl( | 129 ClientSideDetectionService::GetClientReportUrl( |
129 ClientSideDetectionService::kClientReportMalwareUrl), | 130 ClientSideDetectionService::kClientReportMalwareUrl), |
130 response_data, | 131 response_data, response_code, status); |
131 response_code); | |
132 } | 132 } |
133 | 133 |
134 int GetNumReports(std::queue<base::Time>* report_times) { | 134 int GetNumReports(std::queue<base::Time>* report_times) { |
135 return csd_service_->GetNumReports(report_times); | 135 return csd_service_->GetNumReports(report_times); |
136 } | 136 } |
137 | 137 |
138 std::queue<base::Time>& GetPhishingReportTimes() { | 138 std::queue<base::Time>& GetPhishingReportTimes() { |
139 return csd_service_->phishing_report_times_; | 139 return csd_service_->phishing_report_times_; |
140 } | 140 } |
141 | 141 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 253 |
254 TEST_F(ClientSideDetectionServiceTest, FetchModelTest) { | 254 TEST_F(ClientSideDetectionServiceTest, FetchModelTest) { |
255 // We don't want to use a real service class here because we can't call | 255 // We don't want to use a real service class here because we can't call |
256 // the real EndFetchModel. It would reschedule a reload which might | 256 // the real EndFetchModel. It would reschedule a reload which might |
257 // make the test flaky. | 257 // make the test flaky. |
258 MockClientSideDetectionService service; | 258 MockClientSideDetectionService service; |
259 EXPECT_CALL(service, ScheduleFetchModel(_)).Times(1); | 259 EXPECT_CALL(service, ScheduleFetchModel(_)).Times(1); |
260 service.SetEnabledAndRefreshState(true); | 260 service.SetEnabledAndRefreshState(true); |
261 | 261 |
262 // The model fetch failed. | 262 // The model fetch failed. |
263 SetModelFetchResponse("blamodel", net::HTTP_INTERNAL_SERVER_ERROR); | 263 SetModelFetchResponse("blamodel", net::HTTP_INTERNAL_SERVER_ERROR, |
| 264 net::URLRequestStatus::FAILED); |
264 EXPECT_CALL(service, EndFetchModel( | 265 EXPECT_CALL(service, EndFetchModel( |
265 ClientSideDetectionService::MODEL_FETCH_FAILED)) | 266 ClientSideDetectionService::MODEL_FETCH_FAILED)) |
266 .WillOnce(QuitCurrentMessageLoop()); | 267 .WillOnce(QuitCurrentMessageLoop()); |
267 service.StartFetchModel(); | 268 service.StartFetchModel(); |
268 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 269 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
269 Mock::VerifyAndClearExpectations(&service); | 270 Mock::VerifyAndClearExpectations(&service); |
270 | 271 |
271 // Empty model file. | 272 // Empty model file. |
272 SetModelFetchResponse(std::string(), net::HTTP_OK); | 273 SetModelFetchResponse(std::string(), net::HTTP_OK, |
| 274 net::URLRequestStatus::SUCCESS); |
273 EXPECT_CALL(service, EndFetchModel(ClientSideDetectionService::MODEL_EMPTY)) | 275 EXPECT_CALL(service, EndFetchModel(ClientSideDetectionService::MODEL_EMPTY)) |
274 .WillOnce(QuitCurrentMessageLoop()); | 276 .WillOnce(QuitCurrentMessageLoop()); |
275 service.StartFetchModel(); | 277 service.StartFetchModel(); |
276 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 278 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
277 Mock::VerifyAndClearExpectations(&service); | 279 Mock::VerifyAndClearExpectations(&service); |
278 | 280 |
279 // Model is too large. | 281 // Model is too large. |
280 SetModelFetchResponse( | 282 SetModelFetchResponse( |
281 std::string(ClientSideDetectionService::kMaxModelSizeBytes + 1, 'x'), | 283 std::string(ClientSideDetectionService::kMaxModelSizeBytes + 1, 'x'), |
282 net::HTTP_OK); | 284 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
283 EXPECT_CALL(service, EndFetchModel( | 285 EXPECT_CALL(service, EndFetchModel( |
284 ClientSideDetectionService::MODEL_TOO_LARGE)) | 286 ClientSideDetectionService::MODEL_TOO_LARGE)) |
285 .WillOnce(QuitCurrentMessageLoop()); | 287 .WillOnce(QuitCurrentMessageLoop()); |
286 service.StartFetchModel(); | 288 service.StartFetchModel(); |
287 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 289 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
288 Mock::VerifyAndClearExpectations(&service); | 290 Mock::VerifyAndClearExpectations(&service); |
289 | 291 |
290 // Unable to parse the model file. | 292 // Unable to parse the model file. |
291 SetModelFetchResponse("Invalid model file", net::HTTP_OK); | 293 SetModelFetchResponse("Invalid model file", net::HTTP_OK, |
| 294 net::URLRequestStatus::SUCCESS); |
292 EXPECT_CALL(service, EndFetchModel( | 295 EXPECT_CALL(service, EndFetchModel( |
293 ClientSideDetectionService::MODEL_PARSE_ERROR)) | 296 ClientSideDetectionService::MODEL_PARSE_ERROR)) |
294 .WillOnce(QuitCurrentMessageLoop()); | 297 .WillOnce(QuitCurrentMessageLoop()); |
295 service.StartFetchModel(); | 298 service.StartFetchModel(); |
296 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 299 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
297 Mock::VerifyAndClearExpectations(&service); | 300 Mock::VerifyAndClearExpectations(&service); |
298 | 301 |
299 // Model that is missing some required fields (missing the version field). | 302 // Model that is missing some required fields (missing the version field). |
300 ClientSideModel model; | 303 ClientSideModel model; |
301 model.set_max_words_per_term(4); | 304 model.set_max_words_per_term(4); |
302 SetModelFetchResponse(model.SerializePartialAsString(), net::HTTP_OK); | 305 SetModelFetchResponse(model.SerializePartialAsString(), net::HTTP_OK, |
| 306 net::URLRequestStatus::SUCCESS); |
303 EXPECT_CALL(service, EndFetchModel( | 307 EXPECT_CALL(service, EndFetchModel( |
304 ClientSideDetectionService::MODEL_MISSING_FIELDS)) | 308 ClientSideDetectionService::MODEL_MISSING_FIELDS)) |
305 .WillOnce(QuitCurrentMessageLoop()); | 309 .WillOnce(QuitCurrentMessageLoop()); |
306 service.StartFetchModel(); | 310 service.StartFetchModel(); |
307 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 311 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
308 Mock::VerifyAndClearExpectations(&service); | 312 Mock::VerifyAndClearExpectations(&service); |
309 | 313 |
310 // Model that points to hashes that don't exist. | 314 // Model that points to hashes that don't exist. |
311 model.set_version(10); | 315 model.set_version(10); |
312 model.add_hashes("bla"); | 316 model.add_hashes("bla"); |
313 model.add_page_term(1); // Should be 0 instead of 1. | 317 model.add_page_term(1); // Should be 0 instead of 1. |
314 SetModelFetchResponse(model.SerializePartialAsString(), net::HTTP_OK); | 318 SetModelFetchResponse(model.SerializePartialAsString(), net::HTTP_OK, |
| 319 net::URLRequestStatus::SUCCESS); |
315 EXPECT_CALL(service, EndFetchModel( | 320 EXPECT_CALL(service, EndFetchModel( |
316 ClientSideDetectionService::MODEL_BAD_HASH_IDS)) | 321 ClientSideDetectionService::MODEL_BAD_HASH_IDS)) |
317 .WillOnce(QuitCurrentMessageLoop()); | 322 .WillOnce(QuitCurrentMessageLoop()); |
318 service.StartFetchModel(); | 323 service.StartFetchModel(); |
319 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 324 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
320 Mock::VerifyAndClearExpectations(&service); | 325 Mock::VerifyAndClearExpectations(&service); |
321 model.set_page_term(0, 0); | 326 model.set_page_term(0, 0); |
322 | 327 |
323 // Model version number is wrong. | 328 // Model version number is wrong. |
324 model.set_version(-1); | 329 model.set_version(-1); |
325 SetModelFetchResponse(model.SerializeAsString(), net::HTTP_OK); | 330 SetModelFetchResponse(model.SerializeAsString(), net::HTTP_OK, |
| 331 net::URLRequestStatus::SUCCESS); |
326 EXPECT_CALL(service, EndFetchModel( | 332 EXPECT_CALL(service, EndFetchModel( |
327 ClientSideDetectionService::MODEL_INVALID_VERSION_NUMBER)) | 333 ClientSideDetectionService::MODEL_INVALID_VERSION_NUMBER)) |
328 .WillOnce(QuitCurrentMessageLoop()); | 334 .WillOnce(QuitCurrentMessageLoop()); |
329 service.StartFetchModel(); | 335 service.StartFetchModel(); |
330 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 336 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
331 Mock::VerifyAndClearExpectations(&service); | 337 Mock::VerifyAndClearExpectations(&service); |
332 | 338 |
333 // Normal model. | 339 // Normal model. |
334 model.set_version(10); | 340 model.set_version(10); |
335 SetModelFetchResponse(model.SerializeAsString(), net::HTTP_OK); | 341 SetModelFetchResponse(model.SerializeAsString(), net::HTTP_OK, |
| 342 net::URLRequestStatus::SUCCESS); |
336 EXPECT_CALL(service, EndFetchModel( | 343 EXPECT_CALL(service, EndFetchModel( |
337 ClientSideDetectionService::MODEL_SUCCESS)) | 344 ClientSideDetectionService::MODEL_SUCCESS)) |
338 .WillOnce(QuitCurrentMessageLoop()); | 345 .WillOnce(QuitCurrentMessageLoop()); |
339 service.StartFetchModel(); | 346 service.StartFetchModel(); |
340 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 347 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
341 Mock::VerifyAndClearExpectations(&service); | 348 Mock::VerifyAndClearExpectations(&service); |
342 | 349 |
343 // Model version number is decreasing. Set the model version number of the | 350 // Model version number is decreasing. Set the model version number of the |
344 // model that is currently loaded in the service object to 11. | 351 // model that is currently loaded in the service object to 11. |
345 service.model_.reset(new ClientSideModel(model)); | 352 service.model_.reset(new ClientSideModel(model)); |
346 service.model_->set_version(11); | 353 service.model_->set_version(11); |
347 SetModelFetchResponse(model.SerializeAsString(), net::HTTP_OK); | 354 SetModelFetchResponse(model.SerializeAsString(), net::HTTP_OK, |
| 355 net::URLRequestStatus::SUCCESS); |
348 EXPECT_CALL(service, EndFetchModel( | 356 EXPECT_CALL(service, EndFetchModel( |
349 ClientSideDetectionService::MODEL_INVALID_VERSION_NUMBER)) | 357 ClientSideDetectionService::MODEL_INVALID_VERSION_NUMBER)) |
350 .WillOnce(QuitCurrentMessageLoop()); | 358 .WillOnce(QuitCurrentMessageLoop()); |
351 service.StartFetchModel(); | 359 service.StartFetchModel(); |
352 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 360 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
353 Mock::VerifyAndClearExpectations(&service); | 361 Mock::VerifyAndClearExpectations(&service); |
354 | 362 |
355 // Model version hasn't changed since the last reload. | 363 // Model version hasn't changed since the last reload. |
356 service.model_->set_version(10); | 364 service.model_->set_version(10); |
357 SetModelFetchResponse(model.SerializeAsString(), net::HTTP_OK); | 365 SetModelFetchResponse(model.SerializeAsString(), net::HTTP_OK, |
| 366 net::URLRequestStatus::SUCCESS); |
358 EXPECT_CALL(service, EndFetchModel( | 367 EXPECT_CALL(service, EndFetchModel( |
359 ClientSideDetectionService::MODEL_NOT_CHANGED)) | 368 ClientSideDetectionService::MODEL_NOT_CHANGED)) |
360 .WillOnce(QuitCurrentMessageLoop()); | 369 .WillOnce(QuitCurrentMessageLoop()); |
361 service.StartFetchModel(); | 370 service.StartFetchModel(); |
362 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 371 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
363 Mock::VerifyAndClearExpectations(&service); | 372 Mock::VerifyAndClearExpectations(&service); |
364 } | 373 } |
365 | 374 |
366 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) { | 375 TEST_F(ClientSideDetectionServiceTest, ServiceObjectDeletedBeforeCallbackDone) { |
367 SetModelFetchResponse("bogus model", net::HTTP_OK); | 376 SetModelFetchResponse("bogus model", net::HTTP_OK, |
| 377 net::URLRequestStatus::SUCCESS); |
368 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 378 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
369 csd_service_->SetEnabledAndRefreshState(true); | 379 csd_service_->SetEnabledAndRefreshState(true); |
370 EXPECT_TRUE(csd_service_.get() != NULL); | 380 EXPECT_TRUE(csd_service_.get() != NULL); |
371 // We delete the client-side detection service class even though the callbacks | 381 // We delete the client-side detection service class even though the callbacks |
372 // haven't run yet. | 382 // haven't run yet. |
373 csd_service_.reset(); | 383 csd_service_.reset(); |
374 // Waiting for the callbacks to run should not crash even if the service | 384 // Waiting for the callbacks to run should not crash even if the service |
375 // object is gone. | 385 // object is gone. |
376 msg_loop_.RunUntilIdle(); | 386 msg_loop_.RunUntilIdle(); |
377 } | 387 } |
378 | 388 |
379 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) { | 389 TEST_F(ClientSideDetectionServiceTest, SendClientReportPhishingRequest) { |
380 SetModelFetchResponse("bogus model", net::HTTP_OK); | 390 SetModelFetchResponse("bogus model", net::HTTP_OK, |
| 391 net::URLRequestStatus::SUCCESS); |
381 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 392 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
382 csd_service_->SetEnabledAndRefreshState(true); | 393 csd_service_->SetEnabledAndRefreshState(true); |
383 | 394 |
384 GURL url("http://a.com/"); | 395 GURL url("http://a.com/"); |
385 float score = 0.4f; // Some random client score. | 396 float score = 0.4f; // Some random client score. |
386 | 397 |
387 base::Time before = base::Time::Now(); | 398 base::Time before = base::Time::Now(); |
388 | 399 |
389 // Invalid response body from the server. | 400 // Invalid response body from the server. |
390 SetClientReportPhishingResponse("invalid proto response", net::HTTP_OK); | 401 SetClientReportPhishingResponse("invalid proto response", net::HTTP_OK, |
| 402 net::URLRequestStatus::SUCCESS); |
391 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); | 403 EXPECT_FALSE(SendClientReportPhishingRequest(url, score)); |
392 | 404 |
393 // Normal behavior. | 405 // Normal behavior. |
394 ClientPhishingResponse response; | 406 ClientPhishingResponse response; |
395 response.set_phishy(true); | 407 response.set_phishy(true); |
396 SetClientReportPhishingResponse(response.SerializeAsString(), | 408 SetClientReportPhishingResponse(response.SerializeAsString(), net::HTTP_OK, |
397 net::HTTP_OK); | 409 net::URLRequestStatus::SUCCESS); |
398 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); | 410 EXPECT_TRUE(SendClientReportPhishingRequest(url, score)); |
399 | 411 |
400 // This request will fail | 412 // This request will fail |
401 GURL second_url("http://b.com/"); | 413 GURL second_url("http://b.com/"); |
402 response.set_phishy(false); | 414 response.set_phishy(false); |
403 SetClientReportPhishingResponse(response.SerializeAsString(), | 415 SetClientReportPhishingResponse(response.SerializeAsString(), |
404 net::HTTP_INTERNAL_SERVER_ERROR); | 416 net::HTTP_INTERNAL_SERVER_ERROR, |
| 417 net::URLRequestStatus::FAILED); |
405 EXPECT_FALSE(SendClientReportPhishingRequest(second_url, score)); | 418 EXPECT_FALSE(SendClientReportPhishingRequest(second_url, score)); |
406 | 419 |
407 base::Time after = base::Time::Now(); | 420 base::Time after = base::Time::Now(); |
408 | 421 |
409 // Check that we have recorded all 3 requests within the correct time range. | 422 // Check that we have recorded all 3 requests within the correct time range. |
410 std::queue<base::Time>& report_times = GetPhishingReportTimes(); | 423 std::queue<base::Time>& report_times = GetPhishingReportTimes(); |
411 EXPECT_EQ(3U, report_times.size()); | 424 EXPECT_EQ(3U, report_times.size()); |
412 while (!report_times.empty()) { | 425 while (!report_times.empty()) { |
413 base::Time time = report_times.back(); | 426 base::Time time = report_times.back(); |
414 report_times.pop(); | 427 report_times.pop(); |
415 EXPECT_LE(before, time); | 428 EXPECT_LE(before, time); |
416 EXPECT_GE(after, time); | 429 EXPECT_GE(after, time); |
417 } | 430 } |
418 | 431 |
419 // Only the first url should be in the cache. | 432 // Only the first url should be in the cache. |
420 bool is_phishing; | 433 bool is_phishing; |
421 EXPECT_TRUE(csd_service_->IsInCache(url)); | 434 EXPECT_TRUE(csd_service_->IsInCache(url)); |
422 EXPECT_TRUE(csd_service_->GetValidCachedResult(url, &is_phishing)); | 435 EXPECT_TRUE(csd_service_->GetValidCachedResult(url, &is_phishing)); |
423 EXPECT_TRUE(is_phishing); | 436 EXPECT_TRUE(is_phishing); |
424 EXPECT_FALSE(csd_service_->IsInCache(second_url)); | 437 EXPECT_FALSE(csd_service_->IsInCache(second_url)); |
425 } | 438 } |
426 | 439 |
427 TEST_F(ClientSideDetectionServiceTest, SendClientReportMalwareRequest) { | 440 TEST_F(ClientSideDetectionServiceTest, SendClientReportMalwareRequest) { |
428 SetModelFetchResponse("bogus model", net::HTTP_OK); | 441 SetModelFetchResponse("bogus model", net::HTTP_OK, |
| 442 net::URLRequestStatus::SUCCESS); |
429 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 443 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
430 csd_service_->SetEnabledAndRefreshState(true); | 444 csd_service_->SetEnabledAndRefreshState(true); |
431 GURL url("http://a.com/"); | 445 GURL url("http://a.com/"); |
432 | 446 |
433 base::Time before = base::Time::Now(); | 447 base::Time before = base::Time::Now(); |
434 // Invalid response body from the server. | 448 // Invalid response body from the server. |
435 SetClientReportMalwareResponse("invalid proto response", net::HTTP_OK); | 449 SetClientReportMalwareResponse("invalid proto response", net::HTTP_OK, |
| 450 net::URLRequestStatus::SUCCESS); |
436 EXPECT_FALSE(SendClientReportMalwareRequest(url)); | 451 EXPECT_FALSE(SendClientReportMalwareRequest(url)); |
437 | 452 |
438 // Missing bad_url. | 453 // Missing bad_url. |
439 ClientMalwareResponse response; | 454 ClientMalwareResponse response; |
440 response.set_blacklist(true); | 455 response.set_blacklist(true); |
441 SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK); | 456 SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK, |
| 457 net::URLRequestStatus::SUCCESS); |
442 EXPECT_FALSE(SendClientReportMalwareRequest(url)); | 458 EXPECT_FALSE(SendClientReportMalwareRequest(url)); |
443 | 459 |
444 // Normal behavior. | 460 // Normal behavior. |
445 response.set_blacklist(true); | 461 response.set_blacklist(true); |
446 response.set_bad_url("http://response-bad.com/"); | 462 response.set_bad_url("http://response-bad.com/"); |
447 SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK); | 463 SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK, |
| 464 net::URLRequestStatus::SUCCESS); |
448 EXPECT_TRUE(SendClientReportMalwareRequest(url)); | 465 EXPECT_TRUE(SendClientReportMalwareRequest(url)); |
449 CheckConfirmedMalwareUrl(GURL("http://response-bad.com/")); | 466 CheckConfirmedMalwareUrl(GURL("http://response-bad.com/")); |
450 | 467 |
451 // This request will fail | 468 // This request will fail |
452 response.set_blacklist(false); | 469 response.set_blacklist(false); |
453 SetClientReportMalwareResponse(response.SerializeAsString(), | 470 SetClientReportMalwareResponse(response.SerializeAsString(), |
454 net::HTTP_INTERNAL_SERVER_ERROR); | 471 net::HTTP_INTERNAL_SERVER_ERROR, |
| 472 net::URLRequestStatus::FAILED); |
455 EXPECT_FALSE(SendClientReportMalwareRequest(url)); | 473 EXPECT_FALSE(SendClientReportMalwareRequest(url)); |
456 | 474 |
457 // server blacklist decision is false, and response is succesful | 475 // server blacklist decision is false, and response is succesful |
458 response.set_blacklist(false); | 476 response.set_blacklist(false); |
459 SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK); | 477 SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK, |
| 478 net::URLRequestStatus::SUCCESS); |
460 EXPECT_FALSE(SendClientReportMalwareRequest(url)); | 479 EXPECT_FALSE(SendClientReportMalwareRequest(url)); |
461 | 480 |
462 // Check that we have recorded all 4 requests within the correct time range. | 481 // Check that we have recorded all 4 requests within the correct time range. |
463 base::Time after = base::Time::Now(); | 482 base::Time after = base::Time::Now(); |
464 std::queue<base::Time>& report_times = GetMalwareReportTimes(); | 483 std::queue<base::Time>& report_times = GetMalwareReportTimes(); |
465 EXPECT_EQ(4U, report_times.size()); | 484 EXPECT_EQ(4U, report_times.size()); |
466 | 485 |
467 // Another normal behavior will fail because of the limit is hit | 486 // Another normal behavior will fail because of the limit is hit |
468 response.set_blacklist(true); | 487 response.set_blacklist(true); |
469 SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK); | 488 SetClientReportMalwareResponse(response.SerializeAsString(), net::HTTP_OK, |
| 489 net::URLRequestStatus::SUCCESS); |
470 EXPECT_FALSE(SendClientReportMalwareRequest(url)); | 490 EXPECT_FALSE(SendClientReportMalwareRequest(url)); |
471 | 491 |
472 report_times = GetMalwareReportTimes(); | 492 report_times = GetMalwareReportTimes(); |
473 EXPECT_EQ(4U, report_times.size()); | 493 EXPECT_EQ(4U, report_times.size()); |
474 while (!report_times.empty()) { | 494 while (!report_times.empty()) { |
475 base::Time time = report_times.back(); | 495 base::Time time = report_times.back(); |
476 report_times.pop(); | 496 report_times.pop(); |
477 EXPECT_LE(before, time); | 497 EXPECT_LE(before, time); |
478 EXPECT_GE(after, time); | 498 EXPECT_GE(after, time); |
479 } | 499 } |
480 } | 500 } |
481 | 501 |
482 TEST_F(ClientSideDetectionServiceTest, GetNumReportTest) { | 502 TEST_F(ClientSideDetectionServiceTest, GetNumReportTest) { |
483 SetModelFetchResponse("bogus model", net::HTTP_OK); | 503 SetModelFetchResponse("bogus model", net::HTTP_OK, |
| 504 net::URLRequestStatus::SUCCESS); |
484 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 505 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
485 | 506 |
486 std::queue<base::Time>& report_times = GetPhishingReportTimes(); | 507 std::queue<base::Time>& report_times = GetPhishingReportTimes(); |
487 base::Time now = base::Time::Now(); | 508 base::Time now = base::Time::Now(); |
488 base::TimeDelta twenty_five_hours = base::TimeDelta::FromHours(25); | 509 base::TimeDelta twenty_five_hours = base::TimeDelta::FromHours(25); |
489 report_times.push(now - twenty_five_hours); | 510 report_times.push(now - twenty_five_hours); |
490 report_times.push(now - twenty_five_hours); | 511 report_times.push(now - twenty_five_hours); |
491 report_times.push(now); | 512 report_times.push(now); |
492 report_times.push(now); | 513 report_times.push(now); |
493 | 514 |
494 EXPECT_EQ(2, GetNumReports(&report_times)); | 515 EXPECT_EQ(2, GetNumReports(&report_times)); |
495 } | 516 } |
496 | 517 |
497 TEST_F(ClientSideDetectionServiceTest, CacheTest) { | 518 TEST_F(ClientSideDetectionServiceTest, CacheTest) { |
498 SetModelFetchResponse("bogus model", net::HTTP_OK); | 519 SetModelFetchResponse("bogus model", net::HTTP_OK, |
| 520 net::URLRequestStatus::SUCCESS); |
499 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 521 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
500 | 522 |
501 TestCache(); | 523 TestCache(); |
502 } | 524 } |
503 | 525 |
504 TEST_F(ClientSideDetectionServiceTest, IsPrivateIPAddress) { | 526 TEST_F(ClientSideDetectionServiceTest, IsPrivateIPAddress) { |
505 SetModelFetchResponse("bogus model", net::HTTP_OK); | 527 SetModelFetchResponse("bogus model", net::HTTP_OK, |
| 528 net::URLRequestStatus::SUCCESS); |
506 csd_service_.reset(ClientSideDetectionService::Create(NULL)); | 529 csd_service_.reset(ClientSideDetectionService::Create(NULL)); |
507 | 530 |
508 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("10.1.2.3")); | 531 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("10.1.2.3")); |
509 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("127.0.0.1")); | 532 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("127.0.0.1")); |
510 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("172.24.3.4")); | 533 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("172.24.3.4")); |
511 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("192.168.1.1")); | 534 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("192.168.1.1")); |
512 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("fc00::")); | 535 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("fc00::")); |
513 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("fec0::")); | 536 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("fec0::")); |
514 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("fec0:1:2::3")); | 537 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("fec0:1:2::3")); |
515 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("::1")); | 538 EXPECT_TRUE(csd_service_->IsPrivateIPAddress("::1")); |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
626 new StrictMock<MockClientSideDetectionService>(); | 649 new StrictMock<MockClientSideDetectionService>(); |
627 csd_service_.reset(service); | 650 csd_service_.reset(service); |
628 EXPECT_FALSE(csd_service_->enabled()); | 651 EXPECT_FALSE(csd_service_->enabled()); |
629 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL); | 652 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL); |
630 // No calls expected yet. | 653 // No calls expected yet. |
631 Mock::VerifyAndClearExpectations(service); | 654 Mock::VerifyAndClearExpectations(service); |
632 | 655 |
633 ClientSideModel model; | 656 ClientSideModel model; |
634 model.set_version(10); | 657 model.set_version(10); |
635 model.set_max_words_per_term(4); | 658 model.set_max_words_per_term(4); |
636 SetModelFetchResponse(model.SerializeAsString(), net::HTTP_OK); | 659 SetModelFetchResponse(model.SerializeAsString(), net::HTTP_OK, |
| 660 net::URLRequestStatus::SUCCESS); |
637 EXPECT_CALL(*service, ScheduleFetchModel(_)) | 661 EXPECT_CALL(*service, ScheduleFetchModel(_)) |
638 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); | 662 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); |
639 EXPECT_CALL(*service, EndFetchModel( | 663 EXPECT_CALL(*service, EndFetchModel( |
640 ClientSideDetectionService::MODEL_SUCCESS)) | 664 ClientSideDetectionService::MODEL_SUCCESS)) |
641 .WillOnce(QuitCurrentMessageLoop()); | 665 .WillOnce(QuitCurrentMessageLoop()); |
642 csd_service_->SetEnabledAndRefreshState(true); | 666 csd_service_->SetEnabledAndRefreshState(true); |
643 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL); | 667 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL); |
644 msg_loop_.Run(); // EndFetchModel will quit the message loop. | 668 msg_loop_.Run(); // EndFetchModel will quit the message loop. |
645 Mock::VerifyAndClearExpectations(service); | 669 Mock::VerifyAndClearExpectations(service); |
646 | 670 |
(...skipping 11 matching lines...) Expand all Loading... |
658 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL); | 682 EXPECT_TRUE(csd_service_->model_fetcher_.get() != NULL); |
659 csd_service_->SetEnabledAndRefreshState(false); | 683 csd_service_->SetEnabledAndRefreshState(false); |
660 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL); | 684 EXPECT_TRUE(csd_service_->model_fetcher_.get() == NULL); |
661 msg_loop_.RunUntilIdle(); | 685 msg_loop_.RunUntilIdle(); |
662 // No calls expected. | 686 // No calls expected. |
663 Mock::VerifyAndClearExpectations(service); | 687 Mock::VerifyAndClearExpectations(service); |
664 | 688 |
665 // Requests always return false when the service is disabled. | 689 // Requests always return false when the service is disabled. |
666 ClientPhishingResponse response; | 690 ClientPhishingResponse response; |
667 response.set_phishy(true); | 691 response.set_phishy(true); |
668 SetClientReportPhishingResponse(response.SerializeAsString(), | 692 SetClientReportPhishingResponse(response.SerializeAsString(), net::HTTP_OK, |
669 net::HTTP_OK); | 693 net::URLRequestStatus::SUCCESS); |
670 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); | 694 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); |
671 | 695 |
672 // Pending requests also return false if the service is disabled before they | 696 // Pending requests also return false if the service is disabled before they |
673 // report back. | 697 // report back. |
674 EXPECT_CALL(*service, ScheduleFetchModel(_)) | 698 EXPECT_CALL(*service, ScheduleFetchModel(_)) |
675 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); | 699 .WillOnce(Invoke(service, &MockClientSideDetectionService::Schedule)); |
676 EXPECT_CALL(*service, EndFetchModel( | 700 EXPECT_CALL(*service, EndFetchModel( |
677 ClientSideDetectionService::MODEL_NOT_CHANGED)) | 701 ClientSideDetectionService::MODEL_NOT_CHANGED)) |
678 .WillOnce(Invoke(service, &MockClientSideDetectionService::Disable)); | 702 .WillOnce(Invoke(service, &MockClientSideDetectionService::Disable)); |
679 csd_service_->SetEnabledAndRefreshState(true); | 703 csd_service_->SetEnabledAndRefreshState(true); |
680 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); | 704 EXPECT_FALSE(SendClientReportPhishingRequest(GURL("http://a.com/"), 0.4f)); |
681 Mock::VerifyAndClearExpectations(service); | 705 Mock::VerifyAndClearExpectations(service); |
682 } | 706 } |
683 } // namespace safe_browsing | 707 } // namespace safe_browsing |
OLD | NEW |