| 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_dom_feature_extractor.h" | 5 #include "chrome/renderer/safe_browsing/phishing_dom_feature_extractor.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" |
| 10 #include "base/callback.h" | 11 #include "base/callback.h" |
| 11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
| 12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
| 14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 15 #include "chrome/renderer/chrome_content_renderer_client.h" | 16 #include "chrome/renderer/chrome_content_renderer_client.h" |
| 16 #include "chrome/renderer/safe_browsing/features.h" | 17 #include "chrome/renderer/safe_browsing/features.h" |
| 17 #include "chrome/renderer/safe_browsing/mock_feature_extractor_clock.h" | 18 #include "chrome/renderer/safe_browsing/mock_feature_extractor_clock.h" |
| 18 #include "chrome/renderer/safe_browsing/test_utils.h" | 19 #include "chrome/renderer/safe_browsing/test_utils.h" |
| 19 #include "chrome/test/base/chrome_render_view_test.h" | 20 #include "chrome/test/base/chrome_render_view_test.h" |
| 21 #include "content/common/frame_messages.h" |
| 22 #include "content/common/navigation_params.h" |
| 20 #include "content/public/common/content_switches.h" | 23 #include "content/public/common/content_switches.h" |
| 24 #include "content/public/common/resource_response.h" |
| 21 #include "content/public/renderer/render_frame.h" | 25 #include "content/public/renderer/render_frame.h" |
| 22 #include "content/public/test/test_utils.h" | 26 #include "content/public/test/test_utils.h" |
| 27 #include "ipc/ipc_message_macros.h" |
| 23 #include "net/base/escape.h" | 28 #include "net/base/escape.h" |
| 24 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 29 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 25 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 26 #include "third_party/WebKit/public/platform/URLConversion.h" | 31 #include "third_party/WebKit/public/platform/URLConversion.h" |
| 27 #include "third_party/WebKit/public/platform/WebString.h" | 32 #include "third_party/WebKit/public/platform/WebString.h" |
| 28 #include "third_party/WebKit/public/web/WebFrame.h" | 33 #include "third_party/WebKit/public/web/WebFrame.h" |
| 29 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 34 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 30 #include "third_party/WebKit/public/web/WebScriptSource.h" | 35 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 31 | 36 |
| 32 using ::testing::DoAll; | 37 using ::testing::DoAll; |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 | 182 |
| 178 // Helper for the SubframeRemoval test that posts a message to remove | 183 // Helper for the SubframeRemoval test that posts a message to remove |
| 179 // the iframe "frame1" from the document. | 184 // the iframe "frame1" from the document. |
| 180 void ScheduleRemoveIframe() { | 185 void ScheduleRemoveIframe() { |
| 181 base::ThreadTaskRunnerHandle::Get()->PostTask( | 186 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 182 FROM_HERE, base::Bind(&PhishingDOMFeatureExtractorTest::RemoveIframe, | 187 FROM_HERE, base::Bind(&PhishingDOMFeatureExtractorTest::RemoveIframe, |
| 183 weak_factory_.GetWeakPtr())); | 188 weak_factory_.GetWeakPtr())); |
| 184 } | 189 } |
| 185 | 190 |
| 186 protected: | 191 protected: |
| 192 // Subclasses the ChromeMockRenderThread class for providing functionality |
| 193 // to mock IPCs being sent to the browser from RenderViewTests. |
| 194 class PhishingMockRenderThread : public ChromeMockRenderThread { |
| 195 public: |
| 196 PhishingMockRenderThread() |
| 197 : current_message_routing_id_(-1) { |
| 198 } |
| 199 |
| 200 protected: |
| 201 bool OnMessageReceived(const IPC::Message& msg) override { |
| 202 current_message_routing_id_ = msg.routing_id(); |
| 203 |
| 204 IPC_BEGIN_MESSAGE_MAP(PhishingMockRenderThread, msg) |
| 205 IPC_MESSAGE_HANDLER(FrameHostMsg_BeginNavigation, OnBeginNavigation) |
| 206 IPC_END_MESSAGE_MAP() |
| 207 |
| 208 current_message_routing_id_ = -1; |
| 209 return ChromeMockRenderThread::OnMessageReceived(msg); |
| 210 } |
| 211 |
| 212 void OnBeginNavigation( |
| 213 const content::CommonNavigationParams& common_params, |
| 214 const content::BeginNavigationParams& begin_params) { |
| 215 std::unique_ptr<IPC::Message> message; |
| 216 |
| 217 message.reset(new FrameMsg_CommitNavigation( |
| 218 current_message_routing_id_, content::ResourceResponseHead(), |
| 219 common_params.url, common_params, |
| 220 content::RequestNavigationParams())); |
| 221 content::RenderFrame* frame = content::RenderFrame::FromRoutingID( |
| 222 current_message_routing_id_); |
| 223 |
| 224 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 225 FROM_HERE, |
| 226 base::Bind( |
| 227 base::IgnoreResult(&content::RenderFrame::OnMessageReceived), |
| 228 base::Unretained(frame), *message)); |
| 229 } |
| 230 |
| 231 int current_message_routing_id_; |
| 232 }; |
| 233 |
| 187 void SetUp() override { | 234 void SetUp() override { |
| 188 ChromeRenderViewTest::SetUp(); | 235 ChromeRenderViewTest::SetUp(); |
| 189 extractor_.reset(new TestPhishingDOMFeatureExtractor(&clock_)); | 236 extractor_.reset(new TestPhishingDOMFeatureExtractor(&clock_)); |
| 190 } | 237 } |
| 191 | 238 |
| 192 void TearDown() override { | 239 void TearDown() override { |
| 193 extractor_.reset(nullptr); | 240 extractor_.reset(nullptr); |
| 194 ChromeRenderViewTest::TearDown(); | 241 ChromeRenderViewTest::TearDown(); |
| 195 } | 242 } |
| 196 | 243 |
| 197 content::ContentRendererClient* CreateContentRendererClient() override { | 244 content::ContentRendererClient* CreateContentRendererClient() override { |
| 198 ChromeContentRendererClient* client = new TestChromeContentRendererClient(); | 245 ChromeContentRendererClient* client = new TestChromeContentRendererClient(); |
| 199 InitChromeContentRendererClient(client); | 246 InitChromeContentRendererClient(client); |
| 200 return client; | 247 return client; |
| 201 } | 248 } |
| 202 | 249 |
| 203 void AnotherExtractionDone(bool success) { | 250 void AnotherExtractionDone(bool success) { |
| 204 success_ = success; | 251 success_ = success; |
| 205 message_loop_->QuitClosure().Run(); | 252 message_loop_->QuitClosure().Run(); |
| 206 } | 253 } |
| 207 | 254 |
| 208 // Does the actual work of removing the iframe "frame1" from the document. | 255 // Does the actual work of removing the iframe "frame1" from the document. |
| 209 void RemoveIframe() { | 256 void RemoveIframe() { |
| 210 blink::WebFrame* main_frame = GetMainFrame(); | 257 blink::WebFrame* main_frame = GetMainFrame(); |
| 211 ASSERT_TRUE(main_frame); | 258 ASSERT_TRUE(main_frame); |
| 212 main_frame->executeScript(blink::WebString( | 259 main_frame->executeScript(blink::WebString( |
| 213 "document.body.removeChild(document.getElementById('frame1'));")); | 260 "document.body.removeChild(document.getElementById('frame1'));")); |
| 214 } | 261 } |
| 215 | 262 |
| 263 // ChromeMockRenderThread overrides. |
| 264 ChromeMockRenderThread* CreateMockRenderThread() override { |
| 265 return new PhishingMockRenderThread(); |
| 266 } |
| 267 |
| 216 MockFeatureExtractorClock clock_; | 268 MockFeatureExtractorClock clock_; |
| 217 bool success_; | 269 bool success_; |
| 218 std::unique_ptr<TestPhishingDOMFeatureExtractor> extractor_; | 270 std::unique_ptr<TestPhishingDOMFeatureExtractor> extractor_; |
| 219 scoped_refptr<content::MessageLoopRunner> message_loop_; | 271 scoped_refptr<content::MessageLoopRunner> message_loop_; |
| 220 base::WeakPtrFactory<PhishingDOMFeatureExtractorTest> weak_factory_; | 272 base::WeakPtrFactory<PhishingDOMFeatureExtractorTest> weak_factory_; |
| 221 }; | 273 }; |
| 222 | 274 |
| 223 TEST_F(PhishingDOMFeatureExtractorTest, FormFeatures) { | 275 TEST_F(PhishingDOMFeatureExtractorTest, FormFeatures) { |
| 224 // This test doesn't exercise the extraction timing. | 276 // This test doesn't exercise the extraction timing. |
| 225 EXPECT_CALL(clock_, Now()).WillRepeatedly(Return(base::TimeTicks::Now())); | 277 EXPECT_CALL(clock_, Now()).WillRepeatedly(Return(base::TimeTicks::Now())); |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 564 "<html><head></head><body>" | 616 "<html><head></head><body>" |
| 565 "<iframe src=\"" + | 617 "<iframe src=\"" + |
| 566 net::EscapeForHTML(iframe1_url.spec()) + | 618 net::EscapeForHTML(iframe1_url.spec()) + |
| 567 "\" id=\"frame1\"></iframe>" | 619 "\" id=\"frame1\"></iframe>" |
| 568 "<form></form></body></html>"); | 620 "<form></form></body></html>"); |
| 569 ExtractFeatures("host.com", html, &features); | 621 ExtractFeatures("host.com", html, &features); |
| 570 ExpectFeatureMapsAreEqual(features, expected_features); | 622 ExpectFeatureMapsAreEqual(features, expected_features); |
| 571 } | 623 } |
| 572 | 624 |
| 573 } // namespace safe_browsing | 625 } // namespace safe_browsing |
| OLD | NEW |