| OLD | NEW |
| 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 // Note that although this is not a "browser" test, it runs as part of | 5 // Note that although this is not a "browser" test, it runs as part of |
| 6 // browser_tests. This is because WebKit does not work properly if it is | 6 // browser_tests. This is because WebKit does not work properly if it is |
| 7 // shutdown and re-initialized. Since browser_tests runs each test in a | 7 // shutdown and re-initialized. Since browser_tests runs each test in a |
| 8 // new process, this avoids the problem. | 8 // new process, this avoids the problem. |
| 9 | 9 |
| 10 #include "chrome/renderer/safe_browsing/phishing_classifier.h" | 10 #include "chrome/renderer/safe_browsing/phishing_classifier.h" |
| 11 | 11 |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/sha2.h" | |
| 16 #include "base/string16.h" | 15 #include "base/string16.h" |
| 17 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "crypto/sha2.h" |
| 18 #include "chrome/renderer/safe_browsing/client_model.pb.h" | 18 #include "chrome/renderer/safe_browsing/client_model.pb.h" |
| 19 #include "chrome/renderer/safe_browsing/features.h" | 19 #include "chrome/renderer/safe_browsing/features.h" |
| 20 #include "chrome/renderer/safe_browsing/mock_feature_extractor_clock.h" | 20 #include "chrome/renderer/safe_browsing/mock_feature_extractor_clock.h" |
| 21 #include "chrome/renderer/safe_browsing/render_view_fake_resources_test.h" | 21 #include "chrome/renderer/safe_browsing/render_view_fake_resources_test.h" |
| 22 #include "chrome/renderer/safe_browsing/scorer.h" | 22 #include "chrome/renderer/safe_browsing/scorer.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 24 | 24 |
| 25 namespace safe_browsing { | 25 namespace safe_browsing { |
| 26 | 26 |
| 27 class PhishingClassifierTest : public RenderViewFakeResourcesTest { | 27 class PhishingClassifierTest : public RenderViewFakeResourcesTest { |
| 28 protected: | 28 protected: |
| 29 virtual void SetUp() { | 29 virtual void SetUp() { |
| 30 // Set up WebKit and the RenderView. | 30 // Set up WebKit and the RenderView. |
| 31 RenderViewFakeResourcesTest::SetUp(); | 31 RenderViewFakeResourcesTest::SetUp(); |
| 32 | 32 |
| 33 // Construct a model to test with. We include one feature from each of | 33 // Construct a model to test with. We include one feature from each of |
| 34 // the feature extractors, which allows us to verify that they all ran. | 34 // the feature extractors, which allows us to verify that they all ran. |
| 35 ClientSideModel model; | 35 ClientSideModel model; |
| 36 model.add_hashes(base::SHA256HashString(features::kUrlTldToken + | 36 model.add_hashes(crypto::SHA256HashString(features::kUrlTldToken + |
| 37 std::string("net"))); | 37 std::string("net"))); |
| 38 model.add_hashes(base::SHA256HashString(features::kPageLinkDomain + | 38 model.add_hashes(crypto::SHA256HashString(features::kPageLinkDomain + |
| 39 std::string("phishing.com"))); | 39 std::string("phishing.com"))); |
| 40 model.add_hashes(base::SHA256HashString(features::kPageTerm + | 40 model.add_hashes(crypto::SHA256HashString(features::kPageTerm + |
| 41 std::string("login"))); | 41 std::string("login"))); |
| 42 model.add_hashes(base::SHA256HashString("login")); | 42 model.add_hashes(crypto::SHA256HashString("login")); |
| 43 | 43 |
| 44 // Add a default rule with a non-phishy weight. | 44 // Add a default rule with a non-phishy weight. |
| 45 ClientSideModel::Rule* rule = model.add_rule(); | 45 ClientSideModel::Rule* rule = model.add_rule(); |
| 46 rule->set_weight(-1.0); | 46 rule->set_weight(-1.0); |
| 47 | 47 |
| 48 // To give a phishy score, the total weight needs to be >= 0 | 48 // To give a phishy score, the total weight needs to be >= 0 |
| 49 // (0.5 when converted to a probability). This will only happen | 49 // (0.5 when converted to a probability). This will only happen |
| 50 // if all of the listed features are present. | 50 // if all of the listed features are present. |
| 51 rule = model.add_rule(); | 51 rule = model.add_rule(); |
| 52 rule->add_feature(0); | 52 rule->add_feature(0); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 EXPECT_FALSE(RunPhishingClassifier(&page_text, &phishy_score)); | 143 EXPECT_FALSE(RunPhishingClassifier(&page_text, &phishy_score)); |
| 144 EXPECT_EQ(PhishingClassifier::kInvalidScore, phishy_score); | 144 EXPECT_EQ(PhishingClassifier::kInvalidScore, phishy_score); |
| 145 | 145 |
| 146 // Extraction should fail for this case because the URL is a POST request. | 146 // Extraction should fail for this case because the URL is a POST request. |
| 147 LoadURLWithPost("http://host.net/"); | 147 LoadURLWithPost("http://host.net/"); |
| 148 EXPECT_FALSE(RunPhishingClassifier(&page_text, &phishy_score)); | 148 EXPECT_FALSE(RunPhishingClassifier(&page_text, &phishy_score)); |
| 149 EXPECT_EQ(PhishingClassifier::kInvalidScore, phishy_score); | 149 EXPECT_EQ(PhishingClassifier::kInvalidScore, phishy_score); |
| 150 } | 150 } |
| 151 | 151 |
| 152 } // namespace safe_browsing | 152 } // namespace safe_browsing |
| OLD | NEW |