| 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/")); |
| 126 delegate.FinishedLoad(&page_text); | 128 delegate.FinishedLoad(&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. |
| 131 child_frame->loadRequest(WebKit::WebURLRequest(GURL("http://sub2.com/"))); | 133 child_frame->loadRequest(WebKit::WebURLRequest(GURL("http://sub2.com/"))); |
| 132 message_loop_.Run(); | 134 message_loop_.Run(); |
| 133 EXPECT_CALL(*classifier, CancelPendingClassification()); | 135 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 134 delegate.CommittedLoadInFrame(child_frame); | 136 delegate.CommittedLoadInFrame(child_frame); |
| 135 Mock::VerifyAndClearExpectations(classifier); | 137 Mock::VerifyAndClearExpectations(classifier); |
| 138 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 136 delegate.FinishedLoad(&page_text); | 139 delegate.FinishedLoad(&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.CommittedLoadInFrame(GetMainFrame()); |
| 146 delegate.OnStartPhishingDetection(GURL("http://host.com/#foo")); |
| 143 delegate.FinishedLoad(&page_text); | 147 delegate.FinishedLoad(&page_text); |
| 144 | 148 |
| 145 // Now load a new toplevel page, which should trigger another classification. | 149 // Now load a new toplevel page, which should trigger another classification. |
| 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.CommittedLoadInFrame(GetMainFrame()); |
| 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>()); |
| 157 delegate.OnStartPhishingDetection(GURL("http://host2.com/")); |
| 153 delegate.FinishedLoad(&page_text); | 158 delegate.FinishedLoad(&page_text); |
| 154 Mock::VerifyAndClearExpectations(classifier); | 159 Mock::VerifyAndClearExpectations(classifier); |
| 155 | 160 |
| 156 // The delegate will cancel pending classification on destruction. | 161 // The delegate will cancel pending classification on destruction. |
| 157 EXPECT_CALL(*classifier, CancelPendingClassification()); | 162 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 158 } | 163 } |
| 159 | 164 |
| 160 TEST_F(PhishingClassifierDelegateTest, PendingClassification) { | 165 TEST_F(PhishingClassifierDelegateTest, NoScorer) { |
| 161 // For this test, we'll create the delegate with no scorer available yet. | 166 // For this test, we'll create the delegate with no scorer available yet. |
| 162 MockPhishingClassifier* classifier = | 167 MockPhishingClassifier* classifier = |
| 163 new StrictMock<MockPhishingClassifier>(view_); | 168 new StrictMock<MockPhishingClassifier>(view_); |
| 164 PhishingClassifierDelegate delegate(view_, classifier); | 169 PhishingClassifierDelegate delegate(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.CommittedLoadInFrame(GetMainFrame()); |
| 176 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 171 delegate.FinishedLoad(&page_text); | 177 delegate.FinishedLoad(&page_text); |
| 172 | 178 |
| 173 LoadURL("http://host2.com/"); | 179 LoadURL("http://host2.com/"); |
| 174 delegate.CommittedLoadInFrame(GetMainFrame()); | 180 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 175 page_text = ASCIIToUTF16("dummy2"); | 181 page_text = ASCIIToUTF16("dummy2"); |
| 182 delegate.OnStartPhishingDetection(GURL("http://host2.com/")); |
| 176 delegate.FinishedLoad(&page_text); | 183 delegate.FinishedLoad(&page_text); |
| 177 | 184 |
| 178 // Now set a scorer, which should cause a classifier to be created and | 185 // 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| | 186 // the classification to proceed. Note that we need to reset |page_text| |
| 180 // since it is modified by the call to FinishedLoad(). | 187 // since it is modified by the call to FinishedLoad(). |
| 181 page_text = ASCIIToUTF16("dummy2"); | 188 page_text = ASCIIToUTF16("dummy2"); |
| 182 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 189 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 183 WillOnce(DeleteArg<1>()); | 190 WillOnce(DeleteArg<1>()); |
| 184 MockScorer scorer; | 191 MockScorer scorer; |
| 185 delegate.SetPhishingScorer(&scorer); | 192 delegate.SetPhishingScorer(&scorer); |
| 186 Mock::VerifyAndClearExpectations(classifier); | 193 Mock::VerifyAndClearExpectations(classifier); |
| 187 | 194 |
| 188 // The delegate will cancel pending classification on destruction. | 195 // The delegate will cancel pending classification on destruction. |
| 189 EXPECT_CALL(*classifier, CancelPendingClassification()); | 196 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 190 } | 197 } |
| 191 | 198 |
| 192 TEST_F(PhishingClassifierDelegateTest, PendingClassification_Ref) { | 199 TEST_F(PhishingClassifierDelegateTest, NoScorer_Ref) { |
| 193 // Similar to the last test, but navigates within the page before | 200 // Similar to the last test, but navigates within the page before |
| 194 // setting the scorer. | 201 // setting the scorer. |
| 195 MockPhishingClassifier* classifier = | 202 MockPhishingClassifier* classifier = |
| 196 new StrictMock<MockPhishingClassifier>(view_); | 203 new StrictMock<MockPhishingClassifier>(view_); |
| 197 PhishingClassifierDelegate delegate(view_, classifier); | 204 PhishingClassifierDelegate delegate(view_, classifier); |
| 198 ASSERT_FALSE(classifier->is_ready()); | 205 ASSERT_FALSE(classifier->is_ready()); |
| 199 | 206 |
| 200 // Queue up a pending classification, cancel it, then queue up another one. | 207 // Queue up a pending classification, cancel it, then queue up another one. |
| 201 LoadURL("http://host.com/"); | 208 LoadURL("http://host.com/"); |
| 202 delegate.CommittedLoadInFrame(GetMainFrame()); | 209 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 203 string16 orig_page_text = ASCIIToUTF16("dummy"); | 210 string16 orig_page_text = ASCIIToUTF16("dummy"); |
| 204 string16 page_text = orig_page_text; | 211 string16 page_text = orig_page_text; |
| 212 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 205 delegate.FinishedLoad(&page_text); | 213 delegate.FinishedLoad(&page_text); |
| 206 | 214 |
| 207 LoadURL("http://host.com/#foo"); | 215 LoadURL("http://host.com/#foo"); |
| 208 page_text = orig_page_text; | 216 page_text = orig_page_text; |
| 209 delegate.CommittedLoadInFrame(GetMainFrame()); | 217 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 218 delegate.OnStartPhishingDetection(GURL("http://host.com/#foo")); |
| 210 delegate.FinishedLoad(&page_text); | 219 delegate.FinishedLoad(&page_text); |
| 211 | 220 |
| 212 // Now set a scorer, which should cause a classifier to be created and | 221 // Now set a scorer, which should cause a classifier to be created and |
| 213 // the classification to proceed. | 222 // the classification to proceed. |
| 214 EXPECT_CALL(*classifier, BeginClassification(Pointee(orig_page_text), _)). | 223 EXPECT_CALL(*classifier, BeginClassification(Pointee(orig_page_text), _)). |
| 215 WillOnce(DeleteArg<1>()); | 224 WillOnce(DeleteArg<1>()); |
| 216 MockScorer scorer; | 225 MockScorer scorer; |
| 217 delegate.SetPhishingScorer(&scorer); | 226 delegate.SetPhishingScorer(&scorer); |
| 218 Mock::VerifyAndClearExpectations(classifier); | 227 Mock::VerifyAndClearExpectations(classifier); |
| 219 | 228 |
| 220 // The delegate will cancel pending classification on destruction. | 229 // The delegate will cancel pending classification on destruction. |
| 221 EXPECT_CALL(*classifier, CancelPendingClassification()); | 230 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 222 } | 231 } |
| 223 | 232 |
| 233 TEST_F(PhishingClassifierDelegateTest, NoStartPhishingDetection) { |
| 234 // Tests the behavior when OnStartPhishingDetection has not yet been called |
| 235 // when the page load finishes. |
| 236 MockPhishingClassifier* classifier = |
| 237 new StrictMock<MockPhishingClassifier>(view_); |
| 238 PhishingClassifierDelegate delegate(view_, classifier); |
| 239 MockScorer scorer; |
| 240 delegate.SetPhishingScorer(&scorer); |
| 241 ASSERT_TRUE(classifier->is_ready()); |
| 242 |
| 243 responses_["http://host.com/"] = "<html><body>phish</body></html>"; |
| 244 LoadURL("http://host.com/"); |
| 245 string16 orig_page_text = ASCIIToUTF16("phish"); |
| 246 string16 page_text = orig_page_text; |
| 247 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 248 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 249 Mock::VerifyAndClearExpectations(classifier); |
| 250 delegate.FinishedLoad(&page_text); |
| 251 |
| 252 // Now send the OnStartPhishingDetection RPC. We expect classification |
| 253 // to begin. |
| 254 EXPECT_CALL(*classifier, BeginClassification(Pointee(orig_page_text), _)). |
| 255 WillOnce(DeleteArg<1>()); |
| 256 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 257 Mock::VerifyAndClearExpectations(classifier); |
| 258 |
| 259 // Now try again, but this time we will navigate the page away before |
| 260 // the OnStartPhishingDetection IPC is sent. |
| 261 responses_["http://host2.com/"] = "<html><body>phish</body></html>"; |
| 262 LoadURL("http://host2.com/"); |
| 263 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 264 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 265 Mock::VerifyAndClearExpectations(classifier); |
| 266 delegate.FinishedLoad(&page_text); |
| 267 |
| 268 responses_["http://host3.com/"] = "<html><body>phish</body></html>"; |
| 269 LoadURL("http://host3.com/"); |
| 270 delegate.OnStartPhishingDetection(GURL("http://host2.com/")); |
| 271 |
| 272 // The delegate will cancel pending classification on destruction. |
| 273 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 274 } |
| 275 |
| 224 TEST_F(PhishingClassifierDelegateTest, DetectedPhishingSite) { | 276 TEST_F(PhishingClassifierDelegateTest, DetectedPhishingSite) { |
| 225 // Tests that a DetectedPhishingSite IPC is sent to the browser | 277 // Tests that a DetectedPhishingSite IPC is sent to the browser |
| 226 // if a site comes back as phishy. | 278 // if a site comes back as phishy. |
| 227 MockPhishingClassifier* classifier = | 279 MockPhishingClassifier* classifier = |
| 228 new StrictMock<MockPhishingClassifier>(view_); | 280 new StrictMock<MockPhishingClassifier>(view_); |
| 229 PhishingClassifierDelegate delegate(view_, classifier); | 281 PhishingClassifierDelegate delegate(view_, classifier); |
| 230 MockScorer scorer; | 282 MockScorer scorer; |
| 231 delegate.SetPhishingScorer(&scorer); | 283 delegate.SetPhishingScorer(&scorer); |
| 232 ASSERT_TRUE(classifier->is_ready()); | 284 ASSERT_TRUE(classifier->is_ready()); |
| 233 | 285 |
| 234 // Start by loading a page to populate the delegate's state. | 286 // Start by loading a page to populate the delegate's state. |
| 235 responses_["http://host.com/"] = "<html><body>phish</body></html>"; | 287 responses_["http://host.com/"] = "<html><body>phish</body></html>"; |
| 236 LoadURL("http://host.com/"); | 288 LoadURL("http://host.com/"); |
| 237 string16 page_text = ASCIIToUTF16("phish"); | 289 string16 page_text = ASCIIToUTF16("phish"); |
| 238 EXPECT_CALL(*classifier, CancelPendingClassification()); | 290 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 239 delegate.CommittedLoadInFrame(GetMainFrame()); | 291 delegate.CommittedLoadInFrame(GetMainFrame()); |
| 240 Mock::VerifyAndClearExpectations(classifier); | 292 Mock::VerifyAndClearExpectations(classifier); |
| 241 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). | 293 EXPECT_CALL(*classifier, BeginClassification(Pointee(page_text), _)). |
| 242 WillOnce(DeleteArg<1>()); | 294 WillOnce(DeleteArg<1>()); |
| 295 delegate.OnStartPhishingDetection(GURL("http://host.com/")); |
| 243 delegate.FinishedLoad(&page_text); | 296 delegate.FinishedLoad(&page_text); |
| 244 Mock::VerifyAndClearExpectations(classifier); | 297 Mock::VerifyAndClearExpectations(classifier); |
| 245 | 298 |
| 246 // Now run the callback to simulate the classifier finishing. | 299 // Now run the callback to simulate the classifier finishing. |
| 247 RunClassificationDone(&delegate, true, 0.8); | 300 RunClassificationDone(&delegate, true, 0.8); |
| 248 EXPECT_TRUE(detected_phishing_site_); | 301 EXPECT_TRUE(detected_phishing_site_); |
| 249 EXPECT_EQ(GURL("http://host.com/"), detected_url_); | 302 EXPECT_EQ(GURL("http://host.com/"), detected_url_); |
| 250 EXPECT_EQ(0.8, detected_score_); | 303 EXPECT_EQ(0.8, detected_score_); |
| 251 | 304 |
| 252 // The delegate will cancel pending classification on destruction. | 305 // The delegate will cancel pending classification on destruction. |
| 253 EXPECT_CALL(*classifier, CancelPendingClassification()); | 306 EXPECT_CALL(*classifier, CancelPendingClassification()); |
| 254 } | 307 } |
| 255 | 308 |
| 256 } // namespace safe_browsing | 309 } // namespace safe_browsing |
| OLD | NEW |