Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(456)

Side by Side Diff: chrome/browser/safe_browsing/safe_browsing_browsertest.cc

Issue 2845035: First change to add safebrowsing service test. ... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/command_line.h"
6 #include "base/lock.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/chrome_thread.h"
9 #include "chrome/browser/renderer_host/resource_dispatcher_host.h"
10 #include "chrome/browser/safe_browsing/protocol_manager.h"
11 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/test/in_process_browser_test.h"
14 #include "chrome/test/ui_test_utils.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 // This starts the browser and keeps status of states related to SafeBrowsing.
18 class SafeBrowsingServiceTest : public InProcessBrowserTest {
19 public:
20 SafeBrowsingServiceTest()
21 : safe_browsing_service_(NULL),
22 is_update_in_progress_(false),
23 is_initial_request_(false),
24 is_update_scheduled_(false),
25 is_url_match_in_db_(false) {
26 }
27
28 void UpdateSafeBrowsingStatus() {
29 CHECK(safe_browsing_service_);
30 AutoLock lock(update_status_mutex_);
31 is_update_in_progress_ = safe_browsing_service_->IsUpdateInProgress();
32 is_initial_request_ =
33 safe_browsing_service_->protocol_manager_->is_initial_request();
34 last_update_ = safe_browsing_service_->protocol_manager_->last_update();
35 is_update_scheduled_ =
36 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning();
37 }
38
39 void CheckUrl(SafeBrowsingService::Client* helper, const GURL& url) {
40 CHECK(safe_browsing_service_);
41 AutoLock lock(update_status_mutex_);
42 if (!safe_browsing_service_->CheckUrl(url, helper)) {
43 safe_browsing_service_->CancelCheck(helper);
44 is_url_match_in_db_ = false;
45 }
46 is_url_match_in_db_ = true;
47 }
48
49 bool is_url_match_in_db() {
50 AutoLock l(update_status_mutex_);
51 return is_url_match_in_db_;
52 }
53
54 bool is_update_in_progress() {
55 AutoLock l(update_status_mutex_);
56 return is_update_in_progress_;
57 }
58
59 bool is_initial_request() {
60 AutoLock l(update_status_mutex_);
61 return is_initial_request_;
62 }
63
64 base::Time last_update() {
65 AutoLock l(update_status_mutex_);
66 return last_update_;
67 }
68
69 bool is_update_scheduled() {
70 AutoLock l(update_status_mutex_);
71 return is_update_scheduled_;
72 }
73
74 protected:
75 void InitSafeBrowsingService() {
76 safe_browsing_service_ =
77 g_browser_process->resource_dispatcher_host()->safe_browsing_service();
78 }
79
80 virtual void SetUpCommandLine(CommandLine* command_line) {
81 // Makes sure the auto update is not triggered. This test will force the
82 // update when needed.
83 command_line->AppendSwitch(switches::kSbDisableAutoUpdate);
84 }
85
86 private:
87 SafeBrowsingService* safe_browsing_service_;
88
89 // Protects all variables below since they are updated on IO thread but
90 // read on UI thread.
91 Lock update_status_mutex_;
92 // States associated with safebrowsing service updates.
93 bool is_update_in_progress_;
94 bool is_initial_request_;
95 base::Time last_update_;
96 bool is_update_scheduled_;
97 // Indicates if there is a match between a URL's prefix and safebrowsing
98 // database (thus potentially it is a phishing url).
99 bool is_url_match_in_db_;
100 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest);
101 };
102
103 // A ref counted helper class that handles callbacks between IO thread and UI
104 // thread.
105 class SafeBrowsingServiceTestHelper
106 : public base::RefCountedThreadSafe<SafeBrowsingServiceTestHelper>,
107 public SafeBrowsingService::Client {
108 public:
109 explicit SafeBrowsingServiceTestHelper(
110 SafeBrowsingServiceTest* safe_browsing_test)
111 : safe_browsing_test_(safe_browsing_test) {
112 }
113
114 // Callbacks for SafeBrowsingService::Client. Not implemented yet.
115 virtual void OnUrlCheckResult(const GURL& url,
116 SafeBrowsingService::UrlCheckResult result) {
117 NOTREACHED() << "Not implemented.";
118 }
119 virtual void OnBlockingPageComplete(bool proceed) {
120 NOTREACHED() << "Not implemented.";
121 }
122
123 // Functions and callbacks related to CheckUrl. These are used to verify if
124 // a URL is a phishing URL.
125 void CheckUrl(const GURL& url) {
126 ChromeThread::PostTask(ChromeThread::IO, FROM_HERE, NewRunnableMethod(this,
127 &SafeBrowsingServiceTestHelper::CheckUrlOnIOThread, url));
128 }
129 void CheckUrlOnIOThread(const GURL& url) {
130 CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
131 safe_browsing_test_->CheckUrl(this, url);
132 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod(this,
133 &SafeBrowsingServiceTestHelper::OnCheckUrlOnIOThreadDone));
134 }
135 void OnCheckUrlOnIOThreadDone() {
136 StopUILoop();
137 }
138
139 // Functions and callbacks related to safebrowsing server status.
140 void CheckStatusOnIOThread() {
141 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
142 safe_browsing_test_->UpdateSafeBrowsingStatus();
143 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod(this,
144 &SafeBrowsingServiceTestHelper::OnCheckStatusOnIOThreadDone));
145 }
146 void OnCheckStatusOnIOThreadDone() {
147 StopUILoop();
148 }
149 void CheckStatusAfterDelay(int64 wait_time_sec) {
150 ChromeThread::PostDelayedTask(
151 ChromeThread::IO,
152 FROM_HERE,
153 NewRunnableMethod(this,
154 &SafeBrowsingServiceTestHelper::CheckStatusOnIOThread),
155 wait_time_sec * 1000);
156 }
157
158 private:
159 // Stops UI loop after desired status is updated.
160 void StopUILoop() {
161 CHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
162 MessageLoopForUI::current()->Quit();
163 }
164
165 base::OneShotTimer<SafeBrowsingServiceTestHelper> check_update_timer_;
166 SafeBrowsingServiceTest* safe_browsing_test_;
167 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTestHelper);
168 };
169
170 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SafeBrowsingSystemTest) {
171 InitSafeBrowsingService();
172 scoped_refptr<SafeBrowsingServiceTestHelper> safe_browsing_helper =
173 new SafeBrowsingServiceTestHelper(this);
174
175 // Waits for 1 sec and makes sure safebrowsing update is not happening.
176 safe_browsing_helper->CheckStatusAfterDelay(1);
177 // Loop will stop once OnCheckStatusOnIOThreadDone in safe_browsing_helper
178 // is called and status from safe_browsing_service_ is checked.
179 ui_test_utils::RunMessageLoop();
180 EXPECT_FALSE(is_update_in_progress());
181 EXPECT_TRUE(is_initial_request());
182 EXPECT_FALSE(is_update_scheduled());
183 EXPECT_TRUE(last_update().is_null());
184
185 // Verify URL.
186 const char test_url[] = "http://ianfette.org";
187 safe_browsing_helper->CheckUrl(GURL(test_url));
188 // Loop will stop once OnCheckUrlOnIOThreadDone in safe_browsing_helper
189 // is called and url check is done.
190 ui_test_utils::RunMessageLoop();
191 EXPECT_TRUE(is_url_match_in_db());
192
193 // TODO(lzheng): Add tests to launch a testing safebrowsing server
194 // and issue requests repeatedly:
195 // http://code.google.com/p/google-safe-browsing/wiki/ProtocolTesting
196 }
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/protocol_manager.h ('k') | chrome/browser/safe_browsing/safe_browsing_service.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698