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