OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/command_line.h" | |
6 #include "base/ref_counted.h" | |
7 #include "base/scoped_ptr.h" | |
8 #include "chrome/browser/browser_thread.h" | |
9 #include "chrome/browser/renderer_host/test/test_render_view_host.h" | |
10 #include "chrome/browser/safe_browsing/client_side_detection_host.h" | |
11 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | |
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
13 #include "chrome/browser/tab_contents/test_tab_contents.h" | |
14 #include "chrome/common/chrome_switches.h" | |
15 #include "chrome/test/ui_test_utils.h" | |
16 #include "googleurl/src/gurl.h" | |
17 #include "testing/gmock/include/gmock/gmock.h" | |
18 #include "testing/gmock/include/gmock/gmock-spec-builders.h" | |
19 | |
20 using ::testing::_; | |
21 using ::testing::DoAll; | |
22 using ::testing::Mock; | |
23 using ::testing::SaveArg; | |
24 | |
25 namespace safe_browsing { | |
26 | |
27 class MockClientSideDetectionService : public ClientSideDetectionService { | |
28 public: | |
29 MockClientSideDetectionService() {} | |
30 virtual ~MockClientSideDetectionService() {}; | |
31 | |
32 MOCK_METHOD3(SendClientReportPhishingRequest, | |
33 void(const GURL& phishing_url, | |
Brian Ryner
2011/02/11 01:30:39
Hm, I usually see these written without the parame
noelutz
2011/02/15 23:00:55
Done.
| |
34 double score, | |
35 ClientReportPhishingRequestCallback* callback)); | |
36 | |
37 private: | |
38 DISALLOW_COPY_AND_ASSIGN(MockClientSideDetectionService); | |
39 }; | |
40 | |
41 class MockSafeBrowsingService : public SafeBrowsingService { | |
42 public: | |
43 MockSafeBrowsingService() {} | |
44 virtual ~MockSafeBrowsingService() {} | |
45 | |
46 MOCK_METHOD7(DisplayBlockingPage, | |
47 void(const GURL& url, | |
48 const GURL& original_url, | |
49 ResourceType::Type resource_type, | |
50 UrlCheckResult result, | |
51 Client* client, | |
52 int render_process_host_id, | |
53 int render_view_id)); | |
54 | |
55 private: | |
56 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingService); | |
57 }; | |
58 | |
59 ACTION_P(QuitMessageLoop, msg_loop) { | |
60 msg_loop->Quit(); | |
61 } | |
62 | |
63 class ClientSideDetectionHostTest : public RenderViewHostTestHarness { | |
64 public: | |
65 virtual void SetUp() { | |
66 RenderViewHostTestHarness::SetUp(); | |
67 ui_thread_.reset(new BrowserThread(BrowserThread::UI, &message_loop_)); | |
68 io_thread_.reset(new BrowserThread(BrowserThread::IO, &message_loop_)); | |
Brian Ryner
2011/02/11 01:30:39
Unless there's a reason not to, you may want to ac
noelutz
2011/02/15 23:00:55
Done. This was much harder than I thought but it
| |
69 | |
70 // Inject service classes. | |
71 csd_service_.reset(new MockClientSideDetectionService()); | |
72 sb_service_ = new MockSafeBrowsingService(); | |
73 csd_host_ = contents()->safebrowsing_detection_host(); | |
74 csd_host_->set_client_side_detection_service(csd_service_.get()); | |
75 csd_host_->set_safe_browsing_service(sb_service_.get()); | |
lzheng
2011/02/11 19:39:42
You can get rid of these two set_xx by using SafeB
noelutz
2011/02/15 23:00:55
I don't think that would work. Another reason I i
lzheng
2011/02/16 19:09:32
Hm, let's stay with what you have for now. It is n
| |
76 } | |
77 | |
78 virtual void TearDown() { | |
79 io_thread_.reset(); | |
80 ui_thread_.reset(); | |
81 RenderViewHostTestHarness::TearDown(); | |
82 } | |
83 | |
84 virtual void ExpectNotShowInterstitial() { | |
85 // Make sure DisplayBlockingPage is not called. | |
86 EXPECT_CALL(*sb_service_, | |
87 DisplayBlockingPage(_, _, _, _, _, _, _)).Times(0); | |
88 MessageLoop::current()->RunAllPending(); | |
89 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | |
90 } | |
91 | |
92 virtual void ExpectShowInterstital(const GURL& phishing_url) { | |
93 | |
Brian Ryner
2011/02/11 01:30:39
Extra blank line?
noelutz
2011/02/15 23:00:55
Done.
| |
94 SafeBrowsingService::Client* client; | |
95 EXPECT_CALL(*sb_service_, | |
96 DisplayBlockingPage( | |
97 phishing_url, | |
98 phishing_url, | |
99 ResourceType::MAIN_FRAME, | |
100 SafeBrowsingService::URL_PHISHING, | |
101 _ /* a CsdClient object */, | |
102 contents()->GetRenderProcessHost()->id(), | |
103 contents()->render_view_host()->routing_id())) | |
104 .WillOnce(DoAll(SaveArg<4>(&client), | |
105 QuitMessageLoop(MessageLoop::current()))); | |
106 MessageLoop::current()->Run(); | |
Brian Ryner
2011/02/11 01:30:39
This is kind of a nit, but you're using different
noelutz
2011/02/15 23:00:55
Agreed. It makes more sense to use the same logic
| |
107 EXPECT_TRUE(Mock::VerifyAndClear(sb_service_.get())); | |
108 ASSERT_TRUE(client != NULL); | |
109 client->OnBlockingPageComplete(false); | |
110 } | |
111 | |
112 protected: | |
113 ClientSideDetectionHost* csd_host_; | |
114 scoped_ptr<MockClientSideDetectionService> csd_service_; | |
115 scoped_refptr<MockSafeBrowsingService> sb_service_; | |
116 scoped_ptr<BrowserThread> ui_thread_; | |
117 scoped_ptr<BrowserThread> io_thread_; | |
118 }; | |
119 | |
120 TEST_F(ClientSideDetectionHostTest, OnDetectedPhishingSite) { | |
121 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb; | |
122 GURL phishing_url("http://phishingurl.com/"); | |
123 | |
124 // Case 1: client thinks the page is phishing. The server does not agree. | |
125 // No interstitial is shown. | |
126 EXPECT_CALL(*csd_service_, | |
127 SendClientReportPhishingRequest(phishing_url, 1.0, _)) | |
128 .WillOnce(SaveArg<2>(&cb)); | |
129 csd_host_->OnDetectedPhishingSite(phishing_url, 1.0); | |
130 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | |
131 ASSERT_TRUE(cb != NULL); | |
132 cb->Run(phishing_url, false); | |
133 delete cb; | |
134 ExpectNotShowInterstitial(); | |
135 | |
136 // Case 2: client thinks the page is phishing and so does the server but | |
137 // showing the interstitial is disabled => no interstitial is shown. | |
138 EXPECT_CALL(*csd_service_, | |
139 SendClientReportPhishingRequest(phishing_url, 1.0, _)) | |
140 .WillOnce(SaveArg<2>(&cb)); | |
141 csd_host_->OnDetectedPhishingSite(phishing_url, 1.0); | |
142 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | |
143 ASSERT_TRUE(cb != NULL); | |
144 cb->Run(phishing_url, true); | |
145 delete cb; | |
146 ExpectNotShowInterstitial(); | |
147 | |
148 // Case 3: client thinks the page is phishing and so does the server. | |
149 // We show an interstitial. | |
150 EXPECT_CALL(*csd_service_, | |
151 SendClientReportPhishingRequest(phishing_url, 1.0, _)) | |
152 .WillOnce(SaveArg<2>(&cb)); | |
153 csd_host_->OnDetectedPhishingSite(phishing_url, 1.0); | |
154 CommandLine::ForCurrentProcess()->AppendSwitch( | |
155 switches::kEnableClientSidePhishingInterstitial); | |
156 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | |
157 ASSERT_TRUE(cb != NULL); | |
158 cb->Run(phishing_url, true); | |
159 delete cb; | |
160 // Verify that we would show an interstitial in this case. | |
161 ExpectShowInterstital(phishing_url); | |
162 | |
163 // Case 4 & 5: client thinks a page is phishing then navigates to | |
164 // another page which is also considered phishing by the client | |
165 // before the server responds with a verdict. After a while the | |
166 // server responds for both requests with a phishing verdict. Only | |
167 // a single interstitial is shown for the second URL. | |
168 EXPECT_CALL(*csd_service_, | |
169 SendClientReportPhishingRequest(phishing_url, 1.0, _)) | |
170 .WillOnce(SaveArg<2>(&cb)); | |
171 csd_host_->OnDetectedPhishingSite(phishing_url, 1.0); | |
172 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | |
173 ASSERT_TRUE(cb != NULL); | |
174 GURL other_phishing_url("http://other_phishing_url.com/bla"); | |
175 // We navigate away. The callback cb should be revoked. | |
176 NavigateAndCommit(other_phishing_url); | |
177 ClientSideDetectionService::ClientReportPhishingRequestCallback* cb_other; | |
178 EXPECT_CALL(*csd_service_, | |
179 SendClientReportPhishingRequest(other_phishing_url, 0.8, _)) | |
180 .WillOnce(SaveArg<2>(&cb_other)); | |
181 csd_host_->OnDetectedPhishingSite(other_phishing_url, 0.8); | |
182 EXPECT_TRUE(Mock::VerifyAndClear(csd_service_.get())); | |
183 cb->Run(phishing_url, true); // Should have no effect. | |
184 delete cb; | |
185 cb_other->Run(other_phishing_url, true); // Should show interstitial. | |
186 delete cb_other; | |
187 // Check that only the interstitial for the other URL is shown. | |
188 ExpectShowInterstital(other_phishing_url); | |
189 } | |
190 | |
191 } // namespace safe_browsing | |
OLD | NEW |