OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" | 5 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 intercepting_filter_ = new InterceptingMessageFilter(); | 159 intercepting_filter_ = new InterceptingMessageFilter(); |
160 render_view_routing_id_ = | 160 render_view_routing_id_ = |
161 GetWebContents()->GetRenderViewHost()->GetRoutingID(); | 161 GetWebContents()->GetRenderViewHost()->GetRoutingID(); |
162 | 162 |
163 GetWebContents()->GetRenderProcessHost()->AddFilter( | 163 GetWebContents()->GetRenderProcessHost()->AddFilter( |
164 intercepting_filter_.get()); | 164 intercepting_filter_.get()); |
165 content::RenderFrame* render_frame = GetRenderView()->GetMainRenderFrame(); | 165 content::RenderFrame* render_frame = GetRenderView()->GetMainRenderFrame(); |
166 classifier_ = new StrictMock<MockPhishingClassifier>(render_frame); | 166 classifier_ = new StrictMock<MockPhishingClassifier>(render_frame); |
167 delegate_ = PhishingClassifierDelegate::Create(render_frame, classifier_); | 167 delegate_ = PhishingClassifierDelegate::Create(render_frame, classifier_); |
168 | 168 |
169 ASSERT_TRUE(StartTestServer()); | 169 embedded_test_server()->RegisterRequestHandler( |
| 170 base::Bind(&PhishingClassifierDelegateTest::HandleRequest, |
| 171 base::Unretained(this))); |
| 172 ASSERT_TRUE(embedded_test_server()->Start()); |
170 host_resolver()->AddRule("*", "127.0.0.1"); | 173 host_resolver()->AddRule("*", "127.0.0.1"); |
171 } | 174 } |
172 | 175 |
173 // Runs the ClassificationDone callback, then waits for the | 176 // Runs the ClassificationDone callback, then waits for the |
174 // PhishingDetectionDone IPC to arrive. | 177 // PhishingDetectionDone IPC to arrive. |
175 void RunClassificationDone(const ClientPhishingRequest& verdict) { | 178 void RunClassificationDone(const ClientPhishingRequest& verdict) { |
176 // Clear out any previous state. | 179 // Clear out any previous state. |
177 intercepting_filter_->Reset(); | 180 intercepting_filter_->Reset(); |
178 PostTaskToInProcessRendererAndWait( | 181 PostTaskToInProcessRendererAndWait( |
179 base::Bind(&PhishingClassifierDelegate::ClassificationDone, | 182 base::Bind(&PhishingClassifierDelegate::ClassificationDone, |
180 base::Unretained(delegate_), | 183 base::Unretained(delegate_), |
181 verdict)); | 184 verdict)); |
182 intercepting_filter_->RunUntilVerdictReceived(); | 185 intercepting_filter_->RunUntilVerdictReceived(); |
183 } | 186 } |
184 | 187 |
185 void OnStartPhishingDetection(const GURL& url) { | 188 void OnStartPhishingDetection(const GURL& url) { |
186 PostTaskToInProcessRendererAndWait( | 189 PostTaskToInProcessRendererAndWait( |
187 base::Bind(&PhishingClassifierDelegate::OnStartPhishingDetection, | 190 base::Bind(&PhishingClassifierDelegate::OnStartPhishingDetection, |
188 base::Unretained(delegate_), url)); | 191 base::Unretained(delegate_), url)); |
189 } | 192 } |
190 | 193 |
191 void PageCaptured(base::string16* page_text, bool preliminary_capture) { | 194 void PageCaptured(base::string16* page_text, bool preliminary_capture) { |
192 PostTaskToInProcessRendererAndWait( | 195 PostTaskToInProcessRendererAndWait( |
193 base::Bind(&PhishingClassifierDelegate::PageCaptured, | 196 base::Bind(&PhishingClassifierDelegate::PageCaptured, |
194 base::Unretained(delegate_), page_text, | 197 base::Unretained(delegate_), page_text, |
195 preliminary_capture)); | 198 preliminary_capture)); |
196 } | 199 } |
197 | 200 |
198 bool StartTestServer() { | |
199 CHECK(!embedded_test_server_); | |
200 embedded_test_server_.reset(new net::test_server::EmbeddedTestServer()); | |
201 embedded_test_server_->RegisterRequestHandler( | |
202 base::Bind(&PhishingClassifierDelegateTest::HandleRequest, | |
203 base::Unretained(this))); | |
204 return embedded_test_server_->InitializeAndWaitUntilReady(); | |
205 } | |
206 | |
207 scoped_ptr<net::test_server::HttpResponse> HandleRequest( | 201 scoped_ptr<net::test_server::HttpResponse> HandleRequest( |
208 const net::test_server::HttpRequest& request) { | 202 const net::test_server::HttpRequest& request) { |
209 std::map<std::string, std::string>::const_iterator host_it = | 203 std::map<std::string, std::string>::const_iterator host_it = |
210 request.headers.find("Host"); | 204 request.headers.find("Host"); |
211 if (host_it == request.headers.end()) | 205 if (host_it == request.headers.end()) |
212 return scoped_ptr<net::test_server::HttpResponse>(); | 206 return scoped_ptr<net::test_server::HttpResponse>(); |
213 | 207 |
214 std::string url = | 208 std::string url = |
215 std::string("http://") + host_it->second + request.relative_url; | 209 std::string("http://") + host_it->second + request.relative_url; |
216 if (response_url_.spec() != url) | 210 if (response_url_.spec() != url) |
(...skipping 14 matching lines...) Expand all Loading... |
231 content::RenderView* GetRenderView() { | 225 content::RenderView* GetRenderView() { |
232 return content::RenderView::FromRoutingID(render_view_routing_id_); | 226 return content::RenderView::FromRoutingID(render_view_routing_id_); |
233 } | 227 } |
234 | 228 |
235 // Returns the URL that was loaded. | 229 // Returns the URL that was loaded. |
236 GURL LoadHtml(const std::string& host, const std::string& content) { | 230 GURL LoadHtml(const std::string& host, const std::string& content) { |
237 GURL::Replacements replace_host; | 231 GURL::Replacements replace_host; |
238 replace_host.SetHostStr(host); | 232 replace_host.SetHostStr(host); |
239 response_content_ = content; | 233 response_content_ = content; |
240 response_url_ = | 234 response_url_ = |
241 embedded_test_server_->base_url().ReplaceComponents(replace_host); | 235 embedded_test_server()->base_url().ReplaceComponents(replace_host); |
242 ui_test_utils::NavigateToURL(browser(), response_url_); | 236 ui_test_utils::NavigateToURL(browser(), response_url_); |
243 return response_url_; | 237 return response_url_; |
244 } | 238 } |
245 | 239 |
246 void GoBack() { | 240 void GoBack() { |
247 GetWebContents()->GetController().GoBack(); | 241 GetWebContents()->GetController().GoBack(); |
248 content::WaitForLoadStop(GetWebContents()); | 242 content::WaitForLoadStop(GetWebContents()); |
249 } | 243 } |
250 | 244 |
251 void GoForward() { | 245 void GoForward() { |
252 GetWebContents()->GetController().GoForward(); | 246 GetWebContents()->GetController().GoForward(); |
253 content::WaitForLoadStop(GetWebContents()); | 247 content::WaitForLoadStop(GetWebContents()); |
254 } | 248 } |
255 | 249 |
256 scoped_refptr<InterceptingMessageFilter> intercepting_filter_; | 250 scoped_refptr<InterceptingMessageFilter> intercepting_filter_; |
257 GURL response_url_; | 251 GURL response_url_; |
258 std::string response_content_; | 252 std::string response_content_; |
259 scoped_ptr<net::test_server::EmbeddedTestServer> embedded_test_server_; | |
260 scoped_ptr<ClientPhishingRequest> verdict_; | 253 scoped_ptr<ClientPhishingRequest> verdict_; |
261 StrictMock<MockPhishingClassifier>* classifier_; // Owned by |delegate_|. | 254 StrictMock<MockPhishingClassifier>* classifier_; // Owned by |delegate_|. |
262 int32_t render_view_routing_id_; | 255 int32_t render_view_routing_id_; |
263 PhishingClassifierDelegate* delegate_; // Owned by the RenderView. | 256 PhishingClassifierDelegate* delegate_; // Owned by the RenderView. |
264 scoped_refptr<content::MessageLoopRunner> runner_; | 257 scoped_refptr<content::MessageLoopRunner> runner_; |
265 }; | 258 }; |
266 | 259 |
267 IN_PROC_BROWSER_TEST_F(PhishingClassifierDelegateTest, Navigation) { | 260 IN_PROC_BROWSER_TEST_F(PhishingClassifierDelegateTest, Navigation) { |
268 MockScorer scorer; | 261 MockScorer scorer; |
269 delegate_->SetPhishingScorer(&scorer); | 262 delegate_->SetPhishingScorer(&scorer); |
270 ASSERT_TRUE(classifier_->is_ready()); | 263 ASSERT_TRUE(classifier_->is_ready()); |
271 | 264 |
272 // Test an initial load. We expect classification to happen normally. | 265 // Test an initial load. We expect classification to happen normally. |
273 EXPECT_CALL(*classifier_, CancelPendingClassification()); | 266 EXPECT_CALL(*classifier_, CancelPendingClassification()); |
274 std::string port = base::UintToString(embedded_test_server_->port()); | 267 GURL iframe_url = embedded_test_server()->GetURL("/"); |
275 std::string html = "<html><body><iframe src=\"http://sub1.com:"; | 268 GURL::Replacements replace_host; |
276 html += port; | 269 replace_host.SetHostStr("sub1.com"); |
277 html += "/\"></iframe></body></html>"; | 270 std::string html = "<html><body><iframe src=\""; |
| 271 html += iframe_url.ReplaceComponents(replace_host).spec(); |
| 272 html += "\"></iframe></body></html>"; |
278 GURL url = LoadHtml("host.com", html); | 273 GURL url = LoadHtml("host.com", html); |
279 Mock::VerifyAndClearExpectations(classifier_); | 274 Mock::VerifyAndClearExpectations(classifier_); |
280 OnStartPhishingDetection(url); | 275 OnStartPhishingDetection(url); |
281 base::string16 page_text = ASCIIToUTF16("dummy"); | 276 base::string16 page_text = ASCIIToUTF16("dummy"); |
282 { | 277 { |
283 InSequence s; | 278 InSequence s; |
284 EXPECT_CALL(*classifier_, CancelPendingClassification()); | 279 EXPECT_CALL(*classifier_, CancelPendingClassification()); |
285 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); | 280 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); |
286 PageCaptured(&page_text, false); | 281 PageCaptured(&page_text, false); |
287 Mock::VerifyAndClearExpectations(classifier_); | 282 Mock::VerifyAndClearExpectations(classifier_); |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 EXPECT_CALL(*classifier_, CancelPendingClassification()); | 509 EXPECT_CALL(*classifier_, CancelPendingClassification()); |
515 PageCaptured(&page_text, false); | 510 PageCaptured(&page_text, false); |
516 Mock::VerifyAndClearExpectations(classifier_); | 511 Mock::VerifyAndClearExpectations(classifier_); |
517 EXPECT_CALL(*classifier_, CancelPendingClassification()); | 512 EXPECT_CALL(*classifier_, CancelPendingClassification()); |
518 | 513 |
519 ui_test_utils::NavigateToURL( | 514 ui_test_utils::NavigateToURL( |
520 browser(), GURL("javascript:location.replace(\'redir\');")); | 515 browser(), GURL("javascript:location.replace(\'redir\');")); |
521 | 516 |
522 Mock::VerifyAndClearExpectations(classifier_); | 517 Mock::VerifyAndClearExpectations(classifier_); |
523 | 518 |
524 std::string url_str = "http://host4.com:"; | 519 GURL redir_url = embedded_test_server()->GetURL("/redir"); |
525 url_str += base::UintToString(embedded_test_server_->port()); | 520 GURL::Replacements replace_host; |
526 url_str += "/redir"; | 521 replace_host.SetHostStr("host4.com"); |
527 OnStartPhishingDetection(GURL(url_str)); | 522 OnStartPhishingDetection(redir_url.ReplaceComponents(replace_host)); |
528 page_text = ASCIIToUTF16("123"); | 523 page_text = ASCIIToUTF16("123"); |
529 { | 524 { |
530 InSequence s; | 525 InSequence s; |
531 EXPECT_CALL(*classifier_, CancelPendingClassification()); | 526 EXPECT_CALL(*classifier_, CancelPendingClassification()); |
532 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); | 527 EXPECT_CALL(*classifier_, BeginClassification(Pointee(page_text), _)); |
533 PageCaptured(&page_text, false); | 528 PageCaptured(&page_text, false); |
534 Mock::VerifyAndClearExpectations(classifier_); | 529 Mock::VerifyAndClearExpectations(classifier_); |
535 } | 530 } |
536 | 531 |
537 // The delegate will cancel pending classification on destruction. | 532 // The delegate will cancel pending classification on destruction. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 RunClassificationDone(verdict); | 638 RunClassificationDone(verdict); |
644 ASSERT_TRUE(intercepting_filter_->verdict()); | 639 ASSERT_TRUE(intercepting_filter_->verdict()); |
645 EXPECT_EQ(verdict.SerializeAsString(), | 640 EXPECT_EQ(verdict.SerializeAsString(), |
646 intercepting_filter_->verdict()->SerializeAsString()); | 641 intercepting_filter_->verdict()->SerializeAsString()); |
647 | 642 |
648 // The delegate will cancel pending classification on destruction. | 643 // The delegate will cancel pending classification on destruction. |
649 EXPECT_CALL(*classifier_, CancelPendingClassification()); | 644 EXPECT_CALL(*classifier_, CancelPendingClassification()); |
650 } | 645 } |
651 | 646 |
652 } // namespace safe_browsing | 647 } // namespace safe_browsing |
OLD | NEW |