summaryrefslogtreecommitdiffstats
path: root/chrome/browser/safe_browsing/safe_browsing_test.cc
blob: 43d3264236f3f9895fa05218ca37e31eda0e063c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/lock.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/renderer_host/resource_dispatcher_host.h"
#include "chrome/browser/safe_browsing/protocol_manager.h"
#include "chrome/browser/safe_browsing/safe_browsing_service.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/test/in_process_browser_test.h"
#include "chrome/test/ui_test_utils.h"
#include "testing/gtest/include/gtest/gtest.h"

// This starts the browser and keeps status of states related to SafeBrowsing.
class SafeBrowsingServiceTest : public InProcessBrowserTest {
 public:
  SafeBrowsingServiceTest()
    : safe_browsing_service_(NULL),
      is_database_ready_(true),
      is_initial_request_(false),
      is_update_scheduled_(false),
      is_url_match_in_db_(false) {
  }

  void UpdateSafeBrowsingStatus() {
    CHECK(safe_browsing_service_);
    AutoLock lock(update_status_mutex_);
    is_initial_request_ =
        safe_browsing_service_->protocol_manager_->is_initial_request();
    last_update_ = safe_browsing_service_->protocol_manager_->last_update();
    is_update_scheduled_ =
        safe_browsing_service_->protocol_manager_->update_timer_.IsRunning();
  }

  void CheckIsDatabaseReady() {
    AutoLock lock(update_status_mutex_);
    is_database_ready_ =
        !safe_browsing_service_->database_update_in_progress_;
  }

  void CheckUrl(SafeBrowsingService::Client* helper, const GURL& url) {
    CHECK(safe_browsing_service_);
    AutoLock lock(update_status_mutex_);
    if (!safe_browsing_service_->CheckUrl(url, helper)) {
      safe_browsing_service_->CancelCheck(helper);
      is_url_match_in_db_ = false;
    }
    is_url_match_in_db_ = true;
  }

  bool is_url_match_in_db() {
    AutoLock l(update_status_mutex_);
    return is_url_match_in_db_;
  }

  bool is_database_ready() {
    AutoLock l(update_status_mutex_);
    return is_database_ready_;
  }

  bool is_initial_request() {
    AutoLock l(update_status_mutex_);
    return is_initial_request_;
  }

  base::Time last_update() {
    AutoLock l(update_status_mutex_);
    return last_update_;
  }

  bool is_update_scheduled() {
    AutoLock l(update_status_mutex_);
    return is_update_scheduled_;
  }

  MessageLoop* SafeBrowsingMessageLoop() {
    return safe_browsing_service_->safe_browsing_thread_->message_loop();
  }

 protected:
  void InitSafeBrowsingService() {
    safe_browsing_service_ =
        g_browser_process->resource_dispatcher_host()->safe_browsing_service();
  }

  virtual void SetUpCommandLine(CommandLine* command_line) {
    // Makes sure the auto update is not triggered. This test will force the
    // update when needed.
    command_line->AppendSwitch(switches::kSbDisableAutoUpdate);
  }

 private:
  SafeBrowsingService* safe_browsing_service_;

  // Protects all variables below since they are read on UI thread
  // but updated on IO thread or safebrowsing thread.
  Lock update_status_mutex_;

  // States associated with safebrowsing service updates.
  bool is_database_ready_;
  bool is_initial_request_;
  base::Time last_update_;
  bool is_update_scheduled_;
  // Indicates if there is a match between a URL's prefix and safebrowsing
  // database (thus potentially it is a phishing url).
  bool is_url_match_in_db_;
  DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest);
};

// A ref counted helper class that handles callbacks between IO thread and UI
// thread.
class SafeBrowsingServiceTestHelper
    : public base::RefCountedThreadSafe<SafeBrowsingServiceTestHelper>,
      public SafeBrowsingService::Client {
 public:
  explicit SafeBrowsingServiceTestHelper(
      SafeBrowsingServiceTest* safe_browsing_test)
      : safe_browsing_test_(safe_browsing_test) {
  }

  // Callbacks for SafeBrowsingService::Client. Not implemented yet.
  virtual void OnUrlCheckResult(const GURL& url,
                                SafeBrowsingService::UrlCheckResult result) {
    NOTREACHED() << "Not implemented.";
  }
  virtual void OnBlockingPageComplete(bool proceed) {
    NOTREACHED() << "Not implemented.";
  }

  // Functions and callbacks related to CheckUrl. These are used to verify if
  // a URL is a phishing URL.
  void CheckUrl(const GURL& url) {
    BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, NewRunnableMethod(
        this, &SafeBrowsingServiceTestHelper::CheckUrlOnIOThread, url));
  }
  void CheckUrlOnIOThread(const GURL& url) {
    CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    safe_browsing_test_->CheckUrl(this, url);
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod(
        this, &SafeBrowsingServiceTestHelper::OnCheckUrlOnIOThreadDone));
  }
  void OnCheckUrlOnIOThreadDone() {
    StopUILoop();
  }

  // Updates status from IO Thread.
  void CheckStatusOnIOThread() {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
    safe_browsing_test_->UpdateSafeBrowsingStatus();
    safe_browsing_test_->SafeBrowsingMessageLoop()->PostTask(
        FROM_HERE, NewRunnableMethod(this,
        &SafeBrowsingServiceTestHelper::CheckIsDatabaseReady));
  }

  // Checks status in SafeBrowsing Thread.
  void CheckIsDatabaseReady() {
    DCHECK_EQ(MessageLoop::current(),
              safe_browsing_test_->SafeBrowsingMessageLoop());
    safe_browsing_test_->CheckIsDatabaseReady();
    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod(
        this, &SafeBrowsingServiceTestHelper::OnCheckStatusAfterDelayDone));
  }

  void OnCheckStatusAfterDelayDone() {
    StopUILoop();
  }

  // Checks safebrowsing status after a given latency.
  void CheckStatusAfterDelay(int64 wait_time_sec) {
    BrowserThread::PostDelayedTask(
        BrowserThread::IO,
        FROM_HERE,
        NewRunnableMethod(this,
            &SafeBrowsingServiceTestHelper::CheckStatusOnIOThread),
        wait_time_sec * 1000);
  }

 private:
  // Stops UI loop after desired status is updated.
  void StopUILoop() {
    CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    MessageLoopForUI::current()->Quit();
  }

  base::OneShotTimer<SafeBrowsingServiceTestHelper> check_update_timer_;
  SafeBrowsingServiceTest* safe_browsing_test_;
  DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTestHelper);
};

IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SafeBrowsingSystemTest) {
  InitSafeBrowsingService();
  scoped_refptr<SafeBrowsingServiceTestHelper> safe_browsing_helper =
      new SafeBrowsingServiceTestHelper(this);

  // Waits for 1 sec and makes sure safebrowsing update is not happening.
  safe_browsing_helper->CheckStatusAfterDelay(1);
  // Loop will stop once OnCheckStatusOnIOThreadDone in safe_browsing_helper
  // is called and status from safe_browsing_service_ is checked.
  ui_test_utils::RunMessageLoop();
  EXPECT_TRUE(is_database_ready());
  EXPECT_TRUE(is_initial_request());
  EXPECT_FALSE(is_update_scheduled());
  EXPECT_TRUE(last_update().is_null());

  // Verify URL.
  const char test_url[] = "http://ianfette.org";
  safe_browsing_helper->CheckUrl(GURL(test_url));
  // Loop will stop once OnCheckUrlOnIOThreadDone in safe_browsing_helper
  // is called and url check is done.
  ui_test_utils::RunMessageLoop();
  EXPECT_TRUE(is_url_match_in_db());

  // TODO(lzheng): Add tests to launch a testing safebrowsing server
  // and issue requests repeatedly:
  // http://code.google.com/p/google-safe-browsing/wiki/ProtocolTesting
}