| OLD | NEW |
| (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_database_ready_(true), | |
| 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_initial_request_ = | |
| 32 safe_browsing_service_->protocol_manager_->is_initial_request(); | |
| 33 last_update_ = safe_browsing_service_->protocol_manager_->last_update(); | |
| 34 is_update_scheduled_ = | |
| 35 safe_browsing_service_->protocol_manager_->update_timer_.IsRunning(); | |
| 36 } | |
| 37 | |
| 38 void CheckIsDatabaseReady() { | |
| 39 AutoLock lock(update_status_mutex_); | |
| 40 is_database_ready_ = | |
| 41 !safe_browsing_service_->database_update_in_progress_; | |
| 42 } | |
| 43 | |
| 44 void CheckUrl(SafeBrowsingService::Client* helper, const GURL& url) { | |
| 45 CHECK(safe_browsing_service_); | |
| 46 AutoLock lock(update_status_mutex_); | |
| 47 if (!safe_browsing_service_->CheckUrl(url, helper)) { | |
| 48 safe_browsing_service_->CancelCheck(helper); | |
| 49 is_url_match_in_db_ = false; | |
| 50 } | |
| 51 is_url_match_in_db_ = true; | |
| 52 } | |
| 53 | |
| 54 bool is_url_match_in_db() { | |
| 55 AutoLock l(update_status_mutex_); | |
| 56 return is_url_match_in_db_; | |
| 57 } | |
| 58 | |
| 59 bool is_database_ready() { | |
| 60 AutoLock l(update_status_mutex_); | |
| 61 return is_database_ready_; | |
| 62 } | |
| 63 | |
| 64 bool is_initial_request() { | |
| 65 AutoLock l(update_status_mutex_); | |
| 66 return is_initial_request_; | |
| 67 } | |
| 68 | |
| 69 base::Time last_update() { | |
| 70 AutoLock l(update_status_mutex_); | |
| 71 return last_update_; | |
| 72 } | |
| 73 | |
| 74 bool is_update_scheduled() { | |
| 75 AutoLock l(update_status_mutex_); | |
| 76 return is_update_scheduled_; | |
| 77 } | |
| 78 | |
| 79 MessageLoop* SafeBrowsingMessageLoop() { | |
| 80 return safe_browsing_service_->safe_browsing_thread_->message_loop(); | |
| 81 } | |
| 82 | |
| 83 protected: | |
| 84 void InitSafeBrowsingService() { | |
| 85 safe_browsing_service_ = | |
| 86 g_browser_process->resource_dispatcher_host()->safe_browsing_service(); | |
| 87 } | |
| 88 | |
| 89 virtual void SetUpCommandLine(CommandLine* command_line) { | |
| 90 // Makes sure the auto update is not triggered. This test will force the | |
| 91 // update when needed. | |
| 92 command_line->AppendSwitch(switches::kSbDisableAutoUpdate); | |
| 93 } | |
| 94 | |
| 95 private: | |
| 96 SafeBrowsingService* safe_browsing_service_; | |
| 97 | |
| 98 // Protects all variables below since they are read on UI thread | |
| 99 // but updated on IO thread or safebrowsing thread. | |
| 100 Lock update_status_mutex_; | |
| 101 | |
| 102 // States associated with safebrowsing service updates. | |
| 103 bool is_database_ready_; | |
| 104 bool is_initial_request_; | |
| 105 base::Time last_update_; | |
| 106 bool is_update_scheduled_; | |
| 107 // Indicates if there is a match between a URL's prefix and safebrowsing | |
| 108 // database (thus potentially it is a phishing url). | |
| 109 bool is_url_match_in_db_; | |
| 110 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); | |
| 111 }; | |
| 112 | |
| 113 // A ref counted helper class that handles callbacks between IO thread and UI | |
| 114 // thread. | |
| 115 class SafeBrowsingServiceTestHelper | |
| 116 : public base::RefCountedThreadSafe<SafeBrowsingServiceTestHelper>, | |
| 117 public SafeBrowsingService::Client { | |
| 118 public: | |
| 119 explicit SafeBrowsingServiceTestHelper( | |
| 120 SafeBrowsingServiceTest* safe_browsing_test) | |
| 121 : safe_browsing_test_(safe_browsing_test) { | |
| 122 } | |
| 123 | |
| 124 // Callbacks for SafeBrowsingService::Client. Not implemented yet. | |
| 125 virtual void OnUrlCheckResult(const GURL& url, | |
| 126 SafeBrowsingService::UrlCheckResult result) { | |
| 127 NOTREACHED() << "Not implemented."; | |
| 128 } | |
| 129 virtual void OnBlockingPageComplete(bool proceed) { | |
| 130 NOTREACHED() << "Not implemented."; | |
| 131 } | |
| 132 | |
| 133 // Functions and callbacks related to CheckUrl. These are used to verify if | |
| 134 // a URL is a phishing URL. | |
| 135 void CheckUrl(const GURL& url) { | |
| 136 ChromeThread::PostTask(ChromeThread::IO, FROM_HERE, NewRunnableMethod(this, | |
| 137 &SafeBrowsingServiceTestHelper::CheckUrlOnIOThread, url)); | |
| 138 } | |
| 139 void CheckUrlOnIOThread(const GURL& url) { | |
| 140 CHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
| 141 safe_browsing_test_->CheckUrl(this, url); | |
| 142 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod(this, | |
| 143 &SafeBrowsingServiceTestHelper::OnCheckUrlOnIOThreadDone)); | |
| 144 } | |
| 145 void OnCheckUrlOnIOThreadDone() { | |
| 146 StopUILoop(); | |
| 147 } | |
| 148 | |
| 149 // Updates status from IO Thread. | |
| 150 void CheckStatusOnIOThread() { | |
| 151 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); | |
| 152 safe_browsing_test_->UpdateSafeBrowsingStatus(); | |
| 153 safe_browsing_test_->SafeBrowsingMessageLoop()->PostTask( | |
| 154 FROM_HERE, NewRunnableMethod(this, | |
| 155 &SafeBrowsingServiceTestHelper::CheckIsDatabaseReady)); | |
| 156 } | |
| 157 | |
| 158 // Checks status in SafeBrowsing Thread. | |
| 159 void CheckIsDatabaseReady() { | |
| 160 DCHECK_EQ(MessageLoop::current(), | |
| 161 safe_browsing_test_->SafeBrowsingMessageLoop()); | |
| 162 safe_browsing_test_->CheckIsDatabaseReady(); | |
| 163 ChromeThread::PostTask(ChromeThread::UI, FROM_HERE, NewRunnableMethod(this, | |
| 164 &SafeBrowsingServiceTestHelper::OnCheckStatusAfterDelayDone)); | |
| 165 } | |
| 166 | |
| 167 void OnCheckStatusAfterDelayDone() { | |
| 168 StopUILoop(); | |
| 169 } | |
| 170 | |
| 171 // Checks safebrowsing status after a given latency. | |
| 172 void CheckStatusAfterDelay(int64 wait_time_sec) { | |
| 173 ChromeThread::PostDelayedTask( | |
| 174 ChromeThread::IO, | |
| 175 FROM_HERE, | |
| 176 NewRunnableMethod(this, | |
| 177 &SafeBrowsingServiceTestHelper::CheckStatusOnIOThread), | |
| 178 wait_time_sec * 1000); | |
| 179 } | |
| 180 | |
| 181 private: | |
| 182 // Stops UI loop after desired status is updated. | |
| 183 void StopUILoop() { | |
| 184 CHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); | |
| 185 MessageLoopForUI::current()->Quit(); | |
| 186 } | |
| 187 | |
| 188 base::OneShotTimer<SafeBrowsingServiceTestHelper> check_update_timer_; | |
| 189 SafeBrowsingServiceTest* safe_browsing_test_; | |
| 190 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTestHelper); | |
| 191 }; | |
| 192 | |
| 193 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, SafeBrowsingSystemTest) { | |
| 194 InitSafeBrowsingService(); | |
| 195 scoped_refptr<SafeBrowsingServiceTestHelper> safe_browsing_helper = | |
| 196 new SafeBrowsingServiceTestHelper(this); | |
| 197 | |
| 198 // Waits for 1 sec and makes sure safebrowsing update is not happening. | |
| 199 safe_browsing_helper->CheckStatusAfterDelay(1); | |
| 200 // Loop will stop once OnCheckStatusOnIOThreadDone in safe_browsing_helper | |
| 201 // is called and status from safe_browsing_service_ is checked. | |
| 202 ui_test_utils::RunMessageLoop(); | |
| 203 EXPECT_TRUE(is_database_ready()); | |
| 204 EXPECT_TRUE(is_initial_request()); | |
| 205 EXPECT_FALSE(is_update_scheduled()); | |
| 206 EXPECT_TRUE(last_update().is_null()); | |
| 207 | |
| 208 // Verify URL. | |
| 209 const char test_url[] = "http://ianfette.org"; | |
| 210 safe_browsing_helper->CheckUrl(GURL(test_url)); | |
| 211 // Loop will stop once OnCheckUrlOnIOThreadDone in safe_browsing_helper | |
| 212 // is called and url check is done. | |
| 213 ui_test_utils::RunMessageLoop(); | |
| 214 EXPECT_TRUE(is_url_match_in_db()); | |
| 215 | |
| 216 // TODO(lzheng): Add tests to launch a testing safebrowsing server | |
| 217 // and issue requests repeatedly: | |
| 218 // http://code.google.com/p/google-safe-browsing/wiki/ProtocolTesting | |
| 219 } | |
| OLD | NEW |