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

Side by Side Diff: chrome/renderer/safe_browsing/phishing_classifier_delegate_browsertest.cc

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

Powered by Google App Engine
This is Rietveld 408576698