Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(582)

Side by Side Diff: chrome/browser/safe_browsing/client_side_detection_service_unittest.cc

Issue 7465101: Check that all the hashes ids in the model are valid. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Avoid implicit conversion from double to float. Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/callback.h" 9 #include "base/callback.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
214 ClientSideModel model; 214 ClientSideModel model;
215 model.set_max_words_per_term(4); 215 model.set_max_words_per_term(4);
216 SetModelFetchResponse(model.SerializePartialAsString(), true /* success */); 216 SetModelFetchResponse(model.SerializePartialAsString(), true /* success */);
217 EXPECT_CALL(service, EndFetchModel( 217 EXPECT_CALL(service, EndFetchModel(
218 ClientSideDetectionService::MODEL_MISSING_FIELDS)) 218 ClientSideDetectionService::MODEL_MISSING_FIELDS))
219 .WillOnce(QuitCurrentMessageLoop()); 219 .WillOnce(QuitCurrentMessageLoop());
220 service.StartFetchModel(); 220 service.StartFetchModel();
221 msg_loop_.Run(); // EndFetchModel will quit the message loop. 221 msg_loop_.Run(); // EndFetchModel will quit the message loop.
222 Mock::VerifyAndClearExpectations(&service); 222 Mock::VerifyAndClearExpectations(&service);
223 223
224 // Model that points to hashes that don't exist.
225 model.set_version(10);
226 model.add_hashes("bla");
227 model.add_page_term(1); // Should be 0 instead of 1.
228 SetModelFetchResponse(model.SerializePartialAsString(), true /* success */);
229 EXPECT_CALL(service, EndFetchModel(
230 ClientSideDetectionService::MODEL_BAD_HASH_IDS))
231 .WillOnce(QuitCurrentMessageLoop());
232 service.StartFetchModel();
233 msg_loop_.Run(); // EndFetchModel will quit the message loop.
234 Mock::VerifyAndClearExpectations(&service);
235 model.set_page_term(0, 0);
236
224 // Model version number is wrong. 237 // Model version number is wrong.
225 model.set_version(-1); 238 model.set_version(-1);
226 SetModelFetchResponse(model.SerializeAsString(), true /* success */); 239 SetModelFetchResponse(model.SerializeAsString(), true /* success */);
227 EXPECT_CALL(service, EndFetchModel( 240 EXPECT_CALL(service, EndFetchModel(
228 ClientSideDetectionService::MODEL_INVALID_VERSION_NUMBER)) 241 ClientSideDetectionService::MODEL_INVALID_VERSION_NUMBER))
229 .WillOnce(QuitCurrentMessageLoop()); 242 .WillOnce(QuitCurrentMessageLoop());
230 service.StartFetchModel(); 243 service.StartFetchModel();
231 msg_loop_.Run(); // EndFetchModel will quit the message loop. 244 msg_loop_.Run(); // EndFetchModel will quit the message loop.
232 Mock::VerifyAndClearExpectations(&service); 245 Mock::VerifyAndClearExpectations(&service);
233 246
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.1.124.0")); 506 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.1.124.0"));
494 507
495 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.1.127.255")); 508 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.1.127.255"));
496 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.128.0")); 509 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.128.0"));
497 EXPECT_TRUE(csd_service_->IsBadIpAddress("::ffff:192.1.128.1")); 510 EXPECT_TRUE(csd_service_->IsBadIpAddress("::ffff:192.1.128.1"));
498 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.128.255")); 511 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.128.255"));
499 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.255.0")); 512 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.255.0"));
500 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.255.255")); 513 EXPECT_TRUE(csd_service_->IsBadIpAddress("192.1.255.255"));
501 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.2.0.0")); 514 EXPECT_FALSE(csd_service_->IsBadIpAddress("192.2.0.0"));
502 } 515 }
516
517 TEST_F(ClientSideDetectionServiceTest, ModelHasValidHashIds) {
518 ClientSideModel model;
519 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model));
520 model.add_hashes("bla");
521 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model));
522 model.add_page_term(0);
523 model.add_page_word(0);
524 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model));
525
526 model.add_page_term(-1);
527 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model));
528 model.set_page_term(1, 1);
529 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model));
530 model.set_page_term(1, 0);
531 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model));
532
533 model.add_page_word(-2);
534 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model));
535 model.set_page_word(1, 2);
536 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model));
537 model.set_page_word(1, 0);
538 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model));
539
540 // Test bad rules.
541 model.add_hashes("blu");
542 ClientSideModel::Rule* rule = model.add_rule();
543 rule->add_feature(0);
544 rule->add_feature(1);
545 rule->set_weight(0.1f);
546 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model));
547
548 rule = model.add_rule();
549 rule->add_feature(0);
550 rule->add_feature(1);
551 rule->add_feature(-1);
552 rule->set_weight(0.2f);
553 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model));
554
555 rule->set_feature(2, 2);
556 EXPECT_FALSE(ClientSideDetectionService::ModelHasValidHashIds(model));
557
558 rule->set_feature(2, 1);
559 EXPECT_TRUE(ClientSideDetectionService::ModelHasValidHashIds(model));
560 }
503 } // namespace safe_browsing 561 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698