OLD | NEW |
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 #include <map> | 5 #include <map> |
6 #include <queue> | 6 #include <queue> |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/file_path.h" | 10 #include "base/file_path.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/file_util_proxy.h" | 12 #include "base/file_util_proxy.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "base/platform_file.h" | 15 #include "base/platform_file.h" |
16 #include "base/scoped_ptr.h" | 16 #include "base/scoped_ptr.h" |
17 #include "base/scoped_temp_dir.h" | 17 #include "base/scoped_temp_dir.h" |
18 #include "base/task.h" | 18 #include "base/task.h" |
19 #include "base/time.h" | 19 #include "base/time.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
21 #include "chrome/browser/browser_thread.h" | 21 #include "chrome/browser/browser_thread.h" |
| 22 #include "chrome/browser/renderer_host/test/test_render_view_host.h" |
22 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 23 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
23 #include "chrome/browser/safe_browsing/csd.pb.h" | 24 #include "chrome/browser/safe_browsing/csd.pb.h" |
| 25 #include "chrome/common/render_messages.h" |
24 #include "chrome/common/net/test_url_fetcher_factory.h" | 26 #include "chrome/common/net/test_url_fetcher_factory.h" |
25 #include "chrome/common/net/url_fetcher.h" | 27 #include "chrome/common/net/url_fetcher.h" |
26 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
| 29 #include "ipc/ipc_channel.h" |
| 30 #include "ipc/ipc_test_sink.h" |
27 #include "net/url_request/url_request_status.h" | 31 #include "net/url_request/url_request_status.h" |
28 | 32 |
29 namespace safe_browsing { | 33 namespace safe_browsing { |
30 | 34 |
31 class ClientSideDetectionServiceTest : public testing::Test { | 35 class ClientSideDetectionServiceTest : public testing::Test { |
32 protected: | 36 protected: |
33 virtual void SetUp() { | 37 virtual void SetUp() { |
34 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &msg_loop_)); | 38 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &msg_loop_)); |
35 | 39 |
36 factory_.reset(new FakeURLFetcherFactory()); | 40 factory_.reset(new FakeURLFetcherFactory()); |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 base::Time now = base::Time::Now(); | 221 base::Time now = base::Time::Now(); |
218 base::TimeDelta twenty_five_hours = base::TimeDelta::FromHours(25); | 222 base::TimeDelta twenty_five_hours = base::TimeDelta::FromHours(25); |
219 report_times.push(now - twenty_five_hours); | 223 report_times.push(now - twenty_five_hours); |
220 report_times.push(now - twenty_five_hours); | 224 report_times.push(now - twenty_five_hours); |
221 report_times.push(now); | 225 report_times.push(now); |
222 report_times.push(now); | 226 report_times.push(now); |
223 | 227 |
224 EXPECT_EQ(2, GetNumReportsPerDay()); | 228 EXPECT_EQ(2, GetNumReportsPerDay()); |
225 } | 229 } |
226 | 230 |
| 231 // We use a separate test fixture for testing the ClientSideDetectionService's |
| 232 // handling of load notifications from TabContents. This uses |
| 233 // RenderViewHostTestHarness to set up a fake TabContents and related objects. |
| 234 class ClientSideDetectionServiceHooksTest : public RenderViewHostTestHarness, |
| 235 public IPC::Channel::Listener { |
| 236 public: |
| 237 // IPC::Channel::Listener |
| 238 virtual bool OnMessageReceived(const IPC::Message& msg) { |
| 239 if (msg.type() == ViewMsg_StartPhishingDetection::ID) { |
| 240 received_msg_ = msg; |
| 241 MessageLoop::current()->Quit(); |
| 242 return true; |
| 243 } |
| 244 return false; |
| 245 } |
| 246 |
| 247 protected: |
| 248 virtual void SetUp() { |
| 249 RenderViewHostTestHarness::SetUp(); |
| 250 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &message_loop_)); |
| 251 ui_thread_.reset(new BrowserThread(BrowserThread::UI, &message_loop_)); |
| 252 |
| 253 // We're not exercising model fetching here, so just set up a canned |
| 254 // success response. |
| 255 factory_.reset(new FakeURLFetcherFactory()); |
| 256 factory_->SetFakeResponse(ClientSideDetectionService::kClientModelUrl, |
| 257 "dummy model data", true); |
| 258 URLFetcher::set_factory(factory_.get()); |
| 259 |
| 260 process()->sink().AddFilter(this); |
| 261 } |
| 262 |
| 263 virtual void TearDown() { |
| 264 process()->sink().RemoveFilter(this); |
| 265 URLFetcher::set_factory(NULL); |
| 266 file_thread_.reset(); |
| 267 ui_thread_.reset(); |
| 268 RenderViewHostTestHarness::TearDown(); |
| 269 } |
| 270 |
| 271 scoped_ptr<FakeURLFetcherFactory> factory_; |
| 272 scoped_ptr<BrowserThread> ui_thread_; |
| 273 scoped_ptr<BrowserThread> file_thread_; |
| 274 IPC::Message received_msg_; |
| 275 }; |
| 276 |
| 277 TEST_F(ClientSideDetectionServiceHooksTest, ShouldClassifyUrl) { |
| 278 ScopedTempDir tmp_dir; |
| 279 ASSERT_TRUE(tmp_dir.CreateUniqueTempDir()); |
| 280 FilePath model_path = tmp_dir.path().AppendASCII("model"); |
| 281 |
| 282 scoped_ptr<ClientSideDetectionService> csd_service( |
| 283 ClientSideDetectionService::Create(model_path, NULL)); |
| 284 |
| 285 // Navigate the tab to a page. We should see a StartPhishingDetection IPC. |
| 286 NavigateAndCommit(GURL("http://host.com/")); |
| 287 // Wait for the StartPhishingDetection message to arrive. |
| 288 MessageLoop::current()->Run(); |
| 289 |
| 290 Tuple1<GURL> url; |
| 291 ViewMsg_StartPhishingDetection::Read(&received_msg_, &url); |
| 292 EXPECT_EQ(GURL("http://host.com/"), url.a); |
| 293 EXPECT_EQ(rvh()->routing_id(), received_msg_.routing_id()); |
| 294 } |
| 295 |
227 } // namespace safe_browsing | 296 } // namespace safe_browsing |
OLD | NEW |