| 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 // Test an initial load. We expect classification to happen normally. | 105 // Test an initial load. We expect classification to happen normally. |
| 106 responses_["http://host.com/"] = | 106 responses_["http://host.com/"] = |
| 107 "<html><body><iframe src=\"http://sub1.com/\"></iframe></body></html>"; | 107 "<html><body><iframe src=\"http://sub1.com/\"></iframe></body></html>"; |
| 108 LoadURL("http://host.com/"); | 108 LoadURL("http://host.com/"); |
| 109 WebKit::WebFrame* child_frame = GetMainFrame()->firstChild(); | 109 WebKit::WebFrame* child_frame = GetMainFrame()->firstChild(); |
| 110 string16 page_text = ASCIIToUTF16("dummy"); | 110 string16 page_text = ASCIIToUTF16("dummy"); |
| 111 EXPECT_CALL(*classifier, CancelPendingClassification()).Times(2); | 111 EXPECT_CALL(*classifier, CancelPendingClassification()).Times(2); |
| 112 delegate.CommittedLoadInFrame(GetMainFrame()); | 112 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 113 delegate.CommittedLoadInFrame(child_frame); | 113 delegate.CommittedLoadInFrame(child_frame); |
| 114 Mock::VerifyAndClearExpectations(classifier); | 114 Mock::VerifyAndClearExpectations(classifier); |
| 115 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 115 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 116 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 116 WillOnce(DeleteArg<1>()); | 117 WillOnce(DeleteArg<1>()); |
| 117 delegate.FinishedLoad(&page_text); | 118 delegate.FinishedLoad(&page_text); |
| 118 Mock::VerifyAndClearExpectations(classifier); | 119 Mock::VerifyAndClearExpectations(classifier); |
| 119 | 120 |
| 120 // Reloading the same page should not trigger a reclassification. | 121 // Reloading the same page should not trigger a reclassification. |
| 121 // However, it will cancel any pending classification since the | 122 // However, it will cancel any pending classification since the |
| 122 // content is being replaced. | 123 // content is being replaced. |
| 123 EXPECT_CALL(*classifier, CancelPendingClassification()); | 124 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 124 delegate.CommittedLoadInFrame(GetMainFrame()); | 125 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 125 Mock::VerifyAndClearExpectations(classifier); | 126 Mock::VerifyAndClearExpectations(classifier); |
| 127 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 128 page_text = ASCIIToUTF16("dummy"); |
| 126 delegate.FinishedLoad(&page_text); | 129 delegate.FinishedLoad(&page_text); |
| 127 | 130 |
| 128 // Navigating in a subframe will increment the page id, but not change | 131 // Navigating in a subframe will increment the page id, but not change |
| 129 // the toplevel URL. This should cancel pending classification since the | 132 // the toplevel URL. This should cancel pending classification since the |
| 130 // page content is changing, and not begin a new classification. | 133 // page content is changing, and not begin a new classification. |
| 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.CommittedLoadInFrame(child_frame); |
| 135 Mock::VerifyAndClearExpectations(classifier); | 138 Mock::VerifyAndClearExpectations(classifier); |
| 139 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 140 page_text = ASCIIToUTF16("dummy"); |
| 136 delegate.FinishedLoad(&page_text); | 141 delegate.FinishedLoad(&page_text); |
| 137 | 142 |
| 138 // Scrolling to an anchor will increment the page id, but should not | 143 // Scrolling to an anchor will increment the page id, but should not |
| 139 // not trigger a reclassification. A pending classification should not | 144 // not trigger a reclassification. A pending classification should not |
| 140 // be cancelled, since the content is not changing. | 145 // be cancelled, since the content is not changing. |
| 141 LoadURL("http://host.com/#foo"); | 146 LoadURL("http://host.com/#foo"); |
| 142 delegate.CommittedLoadInFrame(GetMainFrame()); | 147 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 148 delegate.OnStartPhishingDetection(GURL("http://host.com/#foo")); |
| 149 page_text = ASCIIToUTF16("dummy"); |
| 143 delegate.FinishedLoad(&page_text); | 150 delegate.FinishedLoad(&page_text); |
| 144 | 151 |
| 145 // Now load a new toplevel page, which should trigger another classification. | 152 // Now load a new toplevel page, which should trigger another classification. |
| 146 LoadURL("http://host2.com/"); | 153 LoadURL("http://host2.com/"); |
| 147 page_text = ASCIIToUTF16("dummy2"); | |
| 148 EXPECT_CALL(*classifier, CancelPendingClassification()); | 154 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 149 delegate.CommittedLoadInFrame(GetMainFrame()); | 155 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 150 Mock::VerifyAndClearExpectations(classifier); | 156 Mock::VerifyAndClearExpectations(classifier); |
| 157 page_text = ASCIIToUTF16("dummy2"); |
| 151 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 158 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 152 WillOnce(DeleteArg<1>()); | 159 WillOnce(DeleteArg<1>()); |
| 160 delegate.OnStartPhishingDetection(GURL("http://host2.com/")); |
| 153 delegate.FinishedLoad(&page_text); | 161 delegate.FinishedLoad(&page_text); |
| 154 Mock::VerifyAndClearExpectations(classifier); | 162 Mock::VerifyAndClearExpectations(classifier); |
| 155 | 163 |
| 164 // No classification should happen on back/forward navigation. |
| 165 // Note: in practice, the browser will not send a StartPhishingDetection IPC |
| 166 // in this case. However, we want to make sure that the delegate behaves |
| 167 // correctly regardless. |
| 168 GoBack(); |
| 169 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 170 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 171 Mock::VerifyAndClearExpectations(classifier); |
| 172 page_text = ASCIIToUTF16("dummy"); |
| 173 delegate.OnStartPhishingDetection(GURL("http://host.com/#foo")); |
| 174 delegate.FinishedLoad(&page_text); |
| 175 |
| 156 // The delegate will cancel pending classification on destruction. | 176 // The delegate will cancel pending classification on destruction. |
| 157 EXPECT_CALL(*classifier, CancelPendingClassification()); | 177 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 158 } | 178 } |
| 159 | 179 |
| 160 TEST_F(PhishingClassifierDelegateTest, PendingClassification) { | 180 TEST_F(PhishingClassifierDelegateTest, NoScorer) { |
| 161 // For this test, we'll create the delegate with no scorer available yet. | 181 // For this test, we'll create the delegate with no scorer available yet. |
| 162 MockPhishingClassifier* classifier = | 182 MockPhishingClassifier* classifier = |
| 163 new StrictMock<MockPhishingClassifier>(view_); | 183 new StrictMock<MockPhishingClassifier>(view_); |
| 164 PhishingClassifierDelegate delegate(view_, classifier); | 184 PhishingClassifierDelegate delegate(view_, classifier); |
| 165 ASSERT_FALSE(classifier->is_ready()); | 185 ASSERT_FALSE(classifier->is_ready()); |
| 166 | 186 |
| 167 // Queue up a pending classification, cancel it, then queue up another one. | 187 // Queue up a pending classification, cancel it, then queue up another one. |
| 168 LoadURL("http://host.com/"); | 188 LoadURL("http://host.com/"); |
| 169 string16 page_text = ASCIIToUTF16("dummy"); | 189 string16 page_text = ASCIIToUTF16("dummy"); |
| 170 delegate.CommittedLoadInFrame(GetMainFrame()); | 190 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 191 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 171 delegate.FinishedLoad(&page_text); | 192 delegate.FinishedLoad(&page_text); |
| 172 | 193 |
| 173 LoadURL("http://host2.com/"); | 194 LoadURL("http://host2.com/"); |
| 174 delegate.CommittedLoadInFrame(GetMainFrame()); | 195 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 175 page_text = ASCIIToUTF16("dummy2"); | 196 page_text = ASCIIToUTF16("dummy2"); |
| 197 delegate.OnStartPhishingDetection(GURL("http://host2.com/")); |
| 176 delegate.FinishedLoad(&page_text); | 198 delegate.FinishedLoad(&page_text); |
| 177 | 199 |
| 178 // Now set a scorer, which should cause a classifier to be created and | 200 // 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| | 201 // the classification to proceed. Note that we need to reset |page_text| |
| 180 // since it is modified by the call to FinishedLoad(). | 202 // since it is modified by the call to FinishedLoad(). |
| 181 page_text = ASCIIToUTF16("dummy2"); | 203 page_text = ASCIIToUTF16("dummy2"); |
| 182 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 204 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 183 WillOnce(DeleteArg<1>()); | 205 WillOnce(DeleteArg<1>()); |
| 184 MockScorer scorer; | 206 MockScorer scorer; |
| 185 delegate.SetPhishingScorer(&scorer); | 207 delegate.SetPhishingScorer(&scorer); |
| 186 Mock::VerifyAndClearExpectations(classifier); | 208 Mock::VerifyAndClearExpectations(classifier); |
| 187 | 209 |
| 188 // The delegate will cancel pending classification on destruction. | 210 // The delegate will cancel pending classification on destruction. |
| 189 EXPECT_CALL(*classifier, CancelPendingClassification()); | 211 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 190 } | 212 } |
| 191 | 213 |
| 192 TEST_F(PhishingClassifierDelegateTest, PendingClassification_Ref) { | 214 TEST_F(PhishingClassifierDelegateTest, NoScorer_Ref) { |
| 193 // Similar to the last test, but navigates within the page before | 215 // Similar to the last test, but navigates within the page before |
| 194 // setting the scorer. | 216 // setting the scorer. |
| 195 MockPhishingClassifier* classifier = | 217 MockPhishingClassifier* classifier = |
| 196 new StrictMock<MockPhishingClassifier>(view_); | 218 new StrictMock<MockPhishingClassifier>(view_); |
| 197 PhishingClassifierDelegate delegate(view_, classifier); | 219 PhishingClassifierDelegate delegate(view_, classifier); |
| 198 ASSERT_FALSE(classifier->is_ready()); | 220 ASSERT_FALSE(classifier->is_ready()); |
| 199 | 221 |
| 200 // Queue up a pending classification, cancel it, then queue up another one. | 222 // Queue up a pending classification, cancel it, then queue up another one. |
| 201 LoadURL("http://host.com/"); | 223 LoadURL("http://host.com/"); |
| 202 delegate.CommittedLoadInFrame(GetMainFrame()); | 224 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 203 string16 orig_page_text = ASCIIToUTF16("dummy"); | 225 string16 orig_page_text = ASCIIToUTF16("dummy"); |
| 204 string16 page_text = orig_page_text; | 226 string16 page_text = orig_page_text; |
| 227 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 205 delegate.FinishedLoad(&page_text); | 228 delegate.FinishedLoad(&page_text); |
| 206 | 229 |
| 207 LoadURL("http://host.com/#foo"); | 230 LoadURL("http://host.com/#foo"); |
| 208 page_text = orig_page_text; | 231 page_text = orig_page_text; |
| 209 delegate.CommittedLoadInFrame(GetMainFrame()); | 232 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 233 delegate.OnStartPhishingDetection(GURL("http://host.com/#foo")); |
| 210 delegate.FinishedLoad(&page_text); | 234 delegate.FinishedLoad(&page_text); |
| 211 | 235 |
| 212 // Now set a scorer, which should cause a classifier to be created and | 236 // Now set a scorer, which should cause a classifier to be created and |
| 213 // the classification to proceed. | 237 // the classification to proceed. |
| 214 EXPECT_CALL(*classifier, BeginClassification(Pointee(orig_page_text), _)). | 238 EXPECT_CALL(*classifier, BeginClassification(Pointee(orig_page_text), _)). |
| 215 WillOnce(DeleteArg<1>()); | 239 WillOnce(DeleteArg<1>()); |
| 216 MockScorer scorer; | 240 MockScorer scorer; |
| 217 delegate.SetPhishingScorer(&scorer); | 241 delegate.SetPhishingScorer(&scorer); |
| 218 Mock::VerifyAndClearExpectations(classifier); | 242 Mock::VerifyAndClearExpectations(classifier); |
| 219 | 243 |
| 220 // The delegate will cancel pending classification on destruction. | 244 // The delegate will cancel pending classification on destruction. |
| 221 EXPECT_CALL(*classifier, CancelPendingClassification()); | 245 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 222 } | 246 } |
| 223 | 247 |
| 248 TEST_F(PhishingClassifierDelegateTest, NoStartPhishingDetection) { |
| 249 // Tests the behavior when OnStartPhishingDetection has not yet been called |
| 250 // when the page load finishes. |
| 251 MockPhishingClassifier* classifier = |
| 252 new StrictMock<MockPhishingClassifier>(view_); |
| 253 PhishingClassifierDelegate delegate(view_, classifier); |
| 254 MockScorer scorer; |
| 255 delegate.SetPhishingScorer(&scorer); |
| 256 ASSERT_TRUE(classifier->is_ready()); |
| 257 |
| 258 responses_["http://host.com/"] = "<html><body>phish</body></html>"; |
| 259 LoadURL("http://host.com/"); |
| 260 string16 orig_page_text = ASCIIToUTF16("phish"); |
| 261 string16 page_text = orig_page_text; |
| 262 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 263 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 264 Mock::VerifyAndClearExpectations(classifier); |
| 265 delegate.FinishedLoad(&page_text); |
| 266 |
| 267 // Now simulate the StartPhishingDetection IPC. We expect classification |
| 268 // to begin. |
| 269 EXPECT_CALL(*classifier, BeginClassification(Pointee(orig_page_text), _)). |
| 270 WillOnce(DeleteArg<1>()); |
| 271 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 272 Mock::VerifyAndClearExpectations(classifier); |
| 273 |
| 274 // Now try again, but this time we will navigate the page away before |
| 275 // the IPC is sent. |
| 276 responses_["http://host2.com/"] = "<html><body>phish</body></html>"; |
| 277 LoadURL("http://host2.com/"); |
| 278 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 279 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 280 Mock::VerifyAndClearExpectations(classifier); |
| 281 delegate.FinishedLoad(&page_text); |
| 282 |
| 283 responses_["http://host3.com/"] = "<html><body>phish</body></html>"; |
| 284 LoadURL("http://host3.com/"); |
| 285 delegate.OnStartPhishingDetection(GURL("http://host2.com/")); |
| 286 |
| 287 // The delegate will cancel pending classification on destruction. |
| 288 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 289 } |
| 290 |
| 224 TEST_F(PhishingClassifierDelegateTest, DetectedPhishingSite) { | 291 TEST_F(PhishingClassifierDelegateTest, DetectedPhishingSite) { |
| 225 // Tests that a DetectedPhishingSite IPC is sent to the browser | 292 // Tests that a DetectedPhishingSite IPC is sent to the browser |
| 226 // if a site comes back as phishy. | 293 // if a site comes back as phishy. |
| 227 MockPhishingClassifier* classifier = | 294 MockPhishingClassifier* classifier = |
| 228 new StrictMock<MockPhishingClassifier>(view_); | 295 new StrictMock<MockPhishingClassifier>(view_); |
| 229 PhishingClassifierDelegate delegate(view_, classifier); | 296 PhishingClassifierDelegate delegate(view_, classifier); |
| 230 MockScorer scorer; | 297 MockScorer scorer; |
| 231 delegate.SetPhishingScorer(&scorer); | 298 delegate.SetPhishingScorer(&scorer); |
| 232 ASSERT_TRUE(classifier->is_ready()); | 299 ASSERT_TRUE(classifier->is_ready()); |
| 233 | 300 |
| 234 // Start by loading a page to populate the delegate's state. | 301 // Start by loading a page to populate the delegate's state. |
| 235 responses_["http://host.com/"] = "<html><body>phish</body></html>"; | 302 responses_["http://host.com/"] = "<html><body>phish</body></html>"; |
| 236 LoadURL("http://host.com/"); | 303 LoadURL("http://host.com/"); |
| 237 string16 page_text = ASCIIToUTF16("phish"); | 304 string16 page_text = ASCIIToUTF16("phish"); |
| 238 EXPECT_CALL(*classifier, CancelPendingClassification()); | 305 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 239 delegate.CommittedLoadInFrame(GetMainFrame()); | 306 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 240 Mock::VerifyAndClearExpectations(classifier); | 307 Mock::VerifyAndClearExpectations(classifier); |
| 241 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 308 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 242 WillOnce(DeleteArg<1>()); | 309 WillOnce(DeleteArg<1>()); |
| 310 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 243 delegate.FinishedLoad(&page_text); | 311 delegate.FinishedLoad(&page_text); |
| 244 Mock::VerifyAndClearExpectations(classifier); | 312 Mock::VerifyAndClearExpectations(classifier); |
| 245 | 313 |
| 246 // Now run the callback to simulate the classifier finishing. | 314 // Now run the callback to simulate the classifier finishing. |
| 247 RunClassificationDone(&delegate, true, 0.8); | 315 RunClassificationDone(&delegate, true, 0.8); |
| 248 EXPECT_TRUE(detected_phishing_site_); | 316 EXPECT_TRUE(detected_phishing_site_); |
| 249 EXPECT_EQ(GURL("http://host.com/"), detected_url_); | 317 EXPECT_EQ(GURL("http://host.com/"), detected_url_); |
| 250 EXPECT_EQ(0.8, detected_score_); | 318 EXPECT_EQ(0.8, detected_score_); |
| 251 | 319 |
| 252 // The delegate will cancel pending classification on destruction. | 320 // The delegate will cancel pending classification on destruction. |
| 253 EXPECT_CALL(*classifier, CancelPendingClassification()); | 321 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 254 } | 322 } |
| 255 | 323 |
| 256 } // namespace safe_browsing | 324 } // namespace safe_browsing |
| OLD | NEW |