| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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: this test uses RenderViewFakeResourcesTest in order to set up a | 5 // Note: this test uses RenderViewFakeResourcesTest in order to set up a |
| 6 // real RenderThread to hold the phishing Scorer object. | 6 // real RenderThread to hold the phishing Scorer object. |
| 7 | 7 |
| 8 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" | 8 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" |
| 9 | 9 |
| 10 #include "base/scoped_ptr.h" | 10 #include "base/scoped_ptr.h" |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 } | 90 } |
| 91 | 91 |
| 92 bool detected_phishing_site_; | 92 bool detected_phishing_site_; |
| 93 GURL detected_url_; | 93 GURL detected_url_; |
| 94 double detected_score_; | 94 double detected_score_; |
| 95 }; | 95 }; |
| 96 | 96 |
| 97 TEST_F(PhishingClassifierDelegateTest, Navigation) { | 97 TEST_F(PhishingClassifierDelegateTest, Navigation) { |
| 98 MockPhishingClassifier* classifier = | 98 MockPhishingClassifier* classifier = |
| 99 new StrictMock<MockPhishingClassifier>(view_); | 99 new StrictMock<MockPhishingClassifier>(view_); |
| 100 PhishingClassifierDelegate delegate(view_, classifier); | 100 PhishingClassifierDelegate* delegate = |
| 101 new PhishingClassifierDelegate(view_, classifier); |
| 101 MockScorer scorer; | 102 MockScorer scorer; |
| 102 delegate.SetPhishingScorer(&scorer); | 103 delegate->SetPhishingScorer(&scorer); |
| 103 ASSERT_TRUE(classifier->is_ready()); | 104 ASSERT_TRUE(classifier->is_ready()); |
| 104 | 105 |
| 105 // Test an initial load. We expect classification to happen normally. | 106 // Test an initial load. We expect classification to happen normally. |
| 107 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 106 responses_["http://host.com/"] = | 108 responses_["http://host.com/"] = |
| 107 "<html><body><iframe src=\"http://sub1.com/\"></iframe></body></html>"; | 109 "<html><body><iframe src=\"http://sub1.com/\"></iframe></body></html>"; |
| 108 LoadURL("http://host.com/"); | 110 LoadURL("http://host.com/"); |
| 109 WebKit::WebFrame* child_frame = GetMainFrame()->firstChild(); | 111 WebKit::WebFrame* child_frame = GetMainFrame()->firstChild(); |
| 110 string16 page_text = ASCIIToUTF16("dummy"); | 112 string16 page_text = ASCIIToUTF16("dummy"); |
| 111 EXPECT_CALL(*classifier, CancelPendingClassification()).Times(2); | 113 EXPECT_CALL(*classifier, CancelPendingClassification()).Times(2); |
| 112 delegate.CommittedLoadInFrame(GetMainFrame()); | 114 delegate->DidCommitProvisionalLoad(GetMainFrame(), true); |
| 113 delegate.CommittedLoadInFrame(child_frame); | 115 delegate->DidCommitProvisionalLoad(child_frame, true); |
| 114 Mock::VerifyAndClearExpectations(classifier); | 116 Mock::VerifyAndClearExpectations(classifier); |
| 115 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 117 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 116 WillOnce(DeleteArg<1>()); | 118 WillOnce(DeleteArg<1>()); |
| 117 delegate.FinishedLoad(&page_text); | 119 delegate->PageCaptured(page_text); |
| 118 Mock::VerifyAndClearExpectations(classifier); | 120 Mock::VerifyAndClearExpectations(classifier); |
| 119 | 121 |
| 120 // Reloading the same page should not trigger a reclassification. | 122 // Reloading the same page should not trigger a reclassification. |
| 121 // However, it will cancel any pending classification since the | 123 // However, it will cancel any pending classification since the |
| 122 // content is being replaced. | 124 // content is being replaced. |
| 123 EXPECT_CALL(*classifier, CancelPendingClassification()); | 125 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 124 delegate.CommittedLoadInFrame(GetMainFrame()); | 126 delegate->DidCommitProvisionalLoad(GetMainFrame(), true); |
| 125 Mock::VerifyAndClearExpectations(classifier); | 127 Mock::VerifyAndClearExpectations(classifier); |
| 126 delegate.FinishedLoad(&page_text); | 128 delegate->PageCaptured(page_text); |
| 127 | 129 |
| 128 // Navigating in a subframe will increment the page id, but not change | 130 // Navigating in a subframe will increment the page id, but not change |
| 129 // the toplevel URL. This should cancel pending classification since the | 131 // the toplevel URL. This should cancel pending classification since the |
| 130 // page content is changing, and not begin a new classification. | 132 // page content is changing, and not begin a new classification. |
| 133 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 131 child_frame->loadRequest(WebKit::WebURLRequest(GURL("http://sub2.com/"))); | 134 child_frame->loadRequest(WebKit::WebURLRequest(GURL("http://sub2.com/"))); |
| 132 message_loop_.Run(); | 135 message_loop_.Run(); |
| 133 EXPECT_CALL(*classifier, CancelPendingClassification()); | 136 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 134 delegate.CommittedLoadInFrame(child_frame); | 137 delegate->DidCommitProvisionalLoad(child_frame, true); |
| 135 Mock::VerifyAndClearExpectations(classifier); | 138 Mock::VerifyAndClearExpectations(classifier); |
| 136 delegate.FinishedLoad(&page_text); | 139 delegate->PageCaptured(page_text); |
| 137 | 140 |
| 138 // Scrolling to an anchor will increment the page id, but should not | 141 // Scrolling to an anchor will increment the page id, but should not |
| 139 // not trigger a reclassification. A pending classification should not | 142 // not trigger a reclassification. A pending classification should not |
| 140 // be cancelled, since the content is not changing. | 143 // be cancelled, since the content is not changing. |
| 141 LoadURL("http://host.com/#foo"); | 144 LoadURL("http://host.com/#foo"); |
| 142 delegate.CommittedLoadInFrame(GetMainFrame()); | 145 delegate->DidCommitProvisionalLoad(GetMainFrame(), true); |
| 143 delegate.FinishedLoad(&page_text); | 146 delegate->PageCaptured(page_text); |
| 144 | 147 |
| 145 // Now load a new toplevel page, which should trigger another classification. | 148 // Now load a new toplevel page, which should trigger another classification. |
| 149 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 146 LoadURL("http://host2.com/"); | 150 LoadURL("http://host2.com/"); |
| 147 page_text = ASCIIToUTF16("dummy2"); | 151 page_text = ASCIIToUTF16("dummy2"); |
| 148 EXPECT_CALL(*classifier, CancelPendingClassification()); | 152 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 149 delegate.CommittedLoadInFrame(GetMainFrame()); | 153 delegate->DidCommitProvisionalLoad(GetMainFrame(), true); |
| 150 Mock::VerifyAndClearExpectations(classifier); | 154 Mock::VerifyAndClearExpectations(classifier); |
| 151 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 155 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 152 WillOnce(DeleteArg<1>()); | 156 WillOnce(DeleteArg<1>()); |
| 153 delegate.FinishedLoad(&page_text); | 157 delegate->PageCaptured(page_text); |
| 154 Mock::VerifyAndClearExpectations(classifier); | 158 Mock::VerifyAndClearExpectations(classifier); |
| 155 | 159 |
| 156 // The delegate will cancel pending classification on destruction. | 160 // The delegate will cancel pending classification on destruction. |
| 157 EXPECT_CALL(*classifier, CancelPendingClassification()); | 161 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 158 } | 162 } |
| 159 | 163 |
| 160 TEST_F(PhishingClassifierDelegateTest, PendingClassification) { | 164 TEST_F(PhishingClassifierDelegateTest, PendingClassification) { |
| 161 // For this test, we'll create the delegate with no scorer available yet. | 165 // For this test, we'll create the delegate with no scorer available yet. |
| 162 MockPhishingClassifier* classifier = | 166 MockPhishingClassifier* classifier = |
| 163 new StrictMock<MockPhishingClassifier>(view_); | 167 new StrictMock<MockPhishingClassifier>(view_); |
| 164 PhishingClassifierDelegate delegate(view_, classifier); | 168 PhishingClassifierDelegate* delegate = |
| 169 new PhishingClassifierDelegate(view_, classifier); |
| 165 ASSERT_FALSE(classifier->is_ready()); | 170 ASSERT_FALSE(classifier->is_ready()); |
| 166 | 171 |
| 167 // Queue up a pending classification, cancel it, then queue up another one. | 172 // Queue up a pending classification, cancel it, then queue up another one. |
| 168 LoadURL("http://host.com/"); | 173 LoadURL("http://host.com/"); |
| 169 string16 page_text = ASCIIToUTF16("dummy"); | 174 string16 page_text = ASCIIToUTF16("dummy"); |
| 170 delegate.CommittedLoadInFrame(GetMainFrame()); | 175 delegate->DidCommitProvisionalLoad(GetMainFrame(), true); |
| 171 delegate.FinishedLoad(&page_text); | 176 delegate->PageCaptured(page_text); |
| 172 | 177 |
| 173 LoadURL("http://host2.com/"); | 178 LoadURL("http://host2.com/"); |
| 174 delegate.CommittedLoadInFrame(GetMainFrame()); | 179 delegate->DidCommitProvisionalLoad(GetMainFrame(), true); |
| 175 page_text = ASCIIToUTF16("dummy2"); | 180 page_text = ASCIIToUTF16("dummy2"); |
| 176 delegate.FinishedLoad(&page_text); | 181 delegate->PageCaptured(page_text); |
| 177 | 182 |
| 178 // Now set a scorer, which should cause a classifier to be created and | 183 // Now set a scorer, which should cause a classifier to be created and |
| 179 // the classification to proceed. Note that we need to reset |page_text| | 184 // the classification to proceed. Note that we need to reset |page_text| |
| 180 // since it is modified by the call to FinishedLoad(). | 185 // since it is modified by the call to PageCaptured(). |
| 181 page_text = ASCIIToUTF16("dummy2"); | 186 page_text = ASCIIToUTF16("dummy2"); |
| 182 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 187 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 183 WillOnce(DeleteArg<1>()); | 188 WillOnce(DeleteArg<1>()); |
| 184 MockScorer scorer; | 189 MockScorer scorer; |
| 185 delegate.SetPhishingScorer(&scorer); | 190 delegate->SetPhishingScorer(&scorer); |
| 186 Mock::VerifyAndClearExpectations(classifier); | 191 Mock::VerifyAndClearExpectations(classifier); |
| 187 | 192 |
| 188 // The delegate will cancel pending classification on destruction. | 193 // The delegate will cancel pending classification on destruction. |
| 189 EXPECT_CALL(*classifier, CancelPendingClassification()); | 194 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 190 } | 195 } |
| 191 | 196 |
| 192 TEST_F(PhishingClassifierDelegateTest, PendingClassification_Ref) { | 197 TEST_F(PhishingClassifierDelegateTest, PendingClassification_Ref) { |
| 193 // Similar to the last test, but navigates within the page before | 198 // Similar to the last test, but navigates within the page before |
| 194 // setting the scorer. | 199 // setting the scorer. |
| 195 MockPhishingClassifier* classifier = | 200 MockPhishingClassifier* classifier = |
| 196 new StrictMock<MockPhishingClassifier>(view_); | 201 new StrictMock<MockPhishingClassifier>(view_); |
| 197 PhishingClassifierDelegate delegate(view_, classifier); | 202 PhishingClassifierDelegate* delegate = |
| 203 new PhishingClassifierDelegate(view_, classifier); |
| 198 ASSERT_FALSE(classifier->is_ready()); | 204 ASSERT_FALSE(classifier->is_ready()); |
| 199 | 205 |
| 200 // Queue up a pending classification, cancel it, then queue up another one. | 206 // Queue up a pending classification, cancel it, then queue up another one. |
| 201 LoadURL("http://host.com/"); | 207 LoadURL("http://host.com/"); |
| 202 delegate.CommittedLoadInFrame(GetMainFrame()); | 208 delegate->DidCommitProvisionalLoad(GetMainFrame(), true); |
| 203 string16 orig_page_text = ASCIIToUTF16("dummy"); | 209 string16 orig_page_text = ASCIIToUTF16("dummy"); |
| 204 string16 page_text = orig_page_text; | 210 string16 page_text = orig_page_text; |
| 205 delegate.FinishedLoad(&page_text); | 211 delegate->PageCaptured(page_text); |
| 206 | 212 |
| 207 LoadURL("http://host.com/#foo"); | 213 LoadURL("http://host.com/#foo"); |
| 208 page_text = orig_page_text; | 214 page_text = orig_page_text; |
| 209 delegate.CommittedLoadInFrame(GetMainFrame()); | 215 delegate->DidCommitProvisionalLoad(GetMainFrame(), true); |
| 210 delegate.FinishedLoad(&page_text); | 216 delegate->PageCaptured(page_text); |
| 211 | 217 |
| 212 // Now set a scorer, which should cause a classifier to be created and | 218 // Now set a scorer, which should cause a classifier to be created and |
| 213 // the classification to proceed. | 219 // the classification to proceed. |
| 214 EXPECT_CALL(*classifier, BeginClassification(Pointee(orig_page_text), _)). | 220 EXPECT_CALL(*classifier, BeginClassification(Pointee(orig_page_text), _)). |
| 215 WillOnce(DeleteArg<1>()); | 221 WillOnce(DeleteArg<1>()); |
| 216 MockScorer scorer; | 222 MockScorer scorer; |
| 217 delegate.SetPhishingScorer(&scorer); | 223 delegate->SetPhishingScorer(&scorer); |
| 218 Mock::VerifyAndClearExpectations(classifier); | 224 Mock::VerifyAndClearExpectations(classifier); |
| 219 | 225 |
| 220 // The delegate will cancel pending classification on destruction. | 226 // The delegate will cancel pending classification on destruction. |
| 221 EXPECT_CALL(*classifier, CancelPendingClassification()); | 227 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 222 } | 228 } |
| 223 | 229 |
| 224 TEST_F(PhishingClassifierDelegateTest, DetectedPhishingSite) { | 230 TEST_F(PhishingClassifierDelegateTest, DetectedPhishingSite) { |
| 225 // Tests that a DetectedPhishingSite IPC is sent to the browser | 231 // Tests that a DetectedPhishingSite IPC is sent to the browser |
| 226 // if a site comes back as phishy. | 232 // if a site comes back as phishy. |
| 227 MockPhishingClassifier* classifier = | 233 MockPhishingClassifier* classifier = |
| 228 new StrictMock<MockPhishingClassifier>(view_); | 234 new StrictMock<MockPhishingClassifier>(view_); |
| 229 PhishingClassifierDelegate delegate(view_, classifier); | 235 PhishingClassifierDelegate* delegate = |
| 236 new PhishingClassifierDelegate(view_, classifier); |
| 230 MockScorer scorer; | 237 MockScorer scorer; |
| 231 delegate.SetPhishingScorer(&scorer); | 238 delegate->SetPhishingScorer(&scorer); |
| 232 ASSERT_TRUE(classifier->is_ready()); | 239 ASSERT_TRUE(classifier->is_ready()); |
| 233 | 240 |
| 234 // Start by loading a page to populate the delegate's state. | 241 // Start by loading a page to populate the delegate's state. |
| 235 responses_["http://host.com/"] = "<html><body>phish</body></html>"; | 242 responses_["http://host.com/"] = "<html><body>phish</body></html>"; |
| 243 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 236 LoadURL("http://host.com/"); | 244 LoadURL("http://host.com/"); |
| 237 string16 page_text = ASCIIToUTF16("phish"); | 245 string16 page_text = ASCIIToUTF16("phish"); |
| 238 EXPECT_CALL(*classifier, CancelPendingClassification()); | 246 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 239 delegate.CommittedLoadInFrame(GetMainFrame()); | 247 delegate->DidCommitProvisionalLoad(GetMainFrame(), true); |
| 240 Mock::VerifyAndClearExpectations(classifier); | 248 Mock::VerifyAndClearExpectations(classifier); |
| 241 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 249 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 242 WillOnce(DeleteArg<1>()); | 250 WillOnce(DeleteArg<1>()); |
| 243 delegate.FinishedLoad(&page_text); | 251 delegate->PageCaptured(page_text); |
| 244 Mock::VerifyAndClearExpectations(classifier); | 252 Mock::VerifyAndClearExpectations(classifier); |
| 245 | 253 |
| 246 // Now run the callback to simulate the classifier finishing. | 254 // Now run the callback to simulate the classifier finishing. |
| 247 RunClassificationDone(&delegate, true, 0.8); | 255 RunClassificationDone(delegate, true, 0.8); |
| 248 EXPECT_TRUE(detected_phishing_site_); | 256 EXPECT_TRUE(detected_phishing_site_); |
| 249 EXPECT_EQ(GURL("http://host.com/"), detected_url_); | 257 EXPECT_EQ(GURL("http://host.com/"), detected_url_); |
| 250 EXPECT_EQ(0.8, detected_score_); | 258 EXPECT_EQ(0.8, detected_score_); |
| 251 | 259 |
| 252 // The delegate will cancel pending classification on destruction. | 260 // The delegate will cancel pending classification on destruction. |
| 253 EXPECT_CALL(*classifier, CancelPendingClassification()); | 261 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 254 } | 262 } |
| 255 | 263 |
| 256 } // namespace safe_browsing | 264 } // namespace safe_browsing |
| OLD | NEW |