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

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

Issue 5544008: Add a browser test for safebrowsing service.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: rename functions Created 10 years 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 // This test creates a safebrowsing service using fake safebrowsing database
6 // and a fake protocol manager. It is used to test logics in safebrowsing
7 // service.
8
9 #include "base/command_line.h"
10 #include "base/sha2.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/browser_thread.h"
13 #include "chrome/browser/safe_browsing/safe_browsing_database.h"
14 #include "chrome/browser/safe_browsing/protocol_manager.h"
15 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
16 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
17 #include "chrome/browser/tab_contents/tab_contents.h"
18 #include "chrome/browser/tab_contents/tab_contents_view.h"
19 #include "chrome/browser/ui/browser.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/test/in_process_browser_test.h"
22 #include "chrome/test/ui_test_utils.h"
23
24 // A SafeBrowingDatabase class that allows us to inject the malicious URLs.
25 class FakeSafeBrowsingDatabase : public SafeBrowsingDatabase {
26 public:
27 FakeSafeBrowsingDatabase() {}
28
29 virtual ~FakeSafeBrowsingDatabase() {}
30
31 // Initializes the database with the given filename.
32 virtual void Init(const FilePath& filename) {}
33
34 // Deletes the current database and creates a new one.
35 virtual bool ResetDatabase() {
36 badurls_.clear();
37 return true;
38 }
39
40 // Called on the IO thread to check if the given URL is safe or not. If we
41 // can synchronously determine that the URL is safe, CheckUrl returns true,
42 // otherwise it returns false.
43 virtual bool ContainsUrl(const GURL& url,
44 std::string* matching_list,
45 std::vector<SBPrefix>* prefix_hits,
46 std::vector<SBFullHashResult>* full_hits,
47 base::Time last_update) {
48 if (badurls_.find(url.spec()) == badurls_.end())
Scott Hess - ex-Googler 2010/12/07 21:22:57 I'd be happy enough with badurls_.count(), here, b
lzheng 2010/12/08 01:57:32 Done.
49 return false;
50 *prefix_hits = badurls_[url.spec()].prefix_hits;
51 *full_hits = badurls_[url.spec()].full_hits;
52 return true;
53 }
54
55 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) {
56 NOTREACHED(); // TODO(lzheng): Implement this for future tests.
Paweł Hajdan Jr. 2010/12/07 08:17:22 I suggest removing those NOTREACHED()s unless you
lzheng 2010/12/08 01:57:32 Done.
57 return false;
58 }
59 virtual void InsertChunks(const std::string& list_name,
60 const SBChunkList& chunks) {
61 NOTREACHED(); // TODO(lzheng): Implement this for future tests.
62 }
63 virtual void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) {
64 NOTREACHED(); // TODO(lzheng): Implement this for future tests.
65 }
66 virtual void UpdateFinished(bool update_succeeded) {
67 NOTREACHED(); // TODO(lzheng): Implement this for future tests.
68 }
69 virtual void CacheHashResults(const std::vector<SBPrefix>& prefixes,
70 const std::vector<SBFullHashResult>& full_hits) {
71 // NOTREACHED(); // TODO(lzheng): Implement this for future tests.
72 }
73
74 // Fill up the database with test URL.
75 void AddUrl(const GURL& url,
76 const std::vector<SBPrefix>& prefix_hits,
77 const std::vector<SBFullHashResult>& full_hits) {
78 badurls_[url.spec()].prefix_hits = prefix_hits;
79 badurls_[url.spec()].full_hits = full_hits;
80 }
81 private:
82 struct Hits {
83 std::vector<SBPrefix> prefix_hits;
84 std::vector<SBFullHashResult> full_hits;
85 };
86 base::hash_map<std::string, Hits> badurls_;
Scott Hess - ex-Googler 2010/12/07 21:22:57 Why not a hash_multimap<>? [Answer: Because that
87 };
88
89 // Factory that creates FakeSafeBrowsingDatabase instances.
90 class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory {
Scott Hess - ex-Googler 2010/12/07 21:22:57 Would rather you used consistent naming. Fake* or
lzheng 2010/12/08 01:57:32 "Test " then. On 2010/12/07 21:22:57, shess wrote
lzheng 2010/12/08 01:57:32 "Test " then. On 2010/12/07 21:22:57, shess wrote
91 public:
92 TestSafeBrowsingDatabaseFactory() : db_(NULL) { }
93 virtual ~TestSafeBrowsingDatabaseFactory() { }
94
95 virtual SafeBrowsingDatabase* CreateSafeBrowsingDatabase() {
96 db_ = new FakeSafeBrowsingDatabase();
97 return db_;
98 }
99 SafeBrowsingDatabase* GetDb() {
100 return db_;
101 }
102 private:
103 // Owned by the Safebrowsing service.
104 SafeBrowsingDatabase* db_;
105 };
106
107
108 // A FakeProtocolManager that could return fixed responses from
109 // safebrowsing server for testing purpose.
110 class FakeProtocolManager : public SafeBrowsingProtocolManager {
111 public:
112 FakeProtocolManager(SafeBrowsingService* sb_service,
113 const std::string& client_name,
114 const std::string& client_key,
115 const std::string& wrapped_key,
116 URLRequestContextGetter* request_context_getter,
117 const std::string& info_url_prefix,
118 const std::string& mackey_url_prefix,
119 bool disable_auto_update)
120 : SafeBrowsingProtocolManager(sb_service, client_name, client_key,
121 wrapped_key, request_context_getter,
122 info_url_prefix, mackey_url_prefix,
123 disable_auto_update),
124 sb_service_(sb_service) {
125 }
126
127 // This function is called when there is a prefix hit in local safebrowsing
128 // database and safebrowsing service issues a get hash request to backends.
129 // We return a result from the prefilled full_hashes_ hash_map to simulate
130 // server's response.
131 virtual void GetFullHash(SafeBrowsingService::SafeBrowsingCheck* check,
132 const std::vector<SBPrefix>& prefixes) {
133 // The hash result should be inserted to the full_hashes_.
134 ASSERT_TRUE(full_hashes_.find(check->url.spec()) != full_hashes_.end());
135 // When we get a valid response, always cache the result.
136 bool cancache = true;
137 BrowserThread::PostTask(
138 BrowserThread::IO, FROM_HERE,
139 NewRunnableMethod(
140 sb_service_, &SafeBrowsingService::HandleGetHashResults,
141 check, full_hashes_[check->url.spec()], cancache));
Scott Hess - ex-Googler 2010/12/07 21:22:57 This scares me a little. I can live with it.
142 }
143
144 // Prepare the GetFullHash results for |url|.
145 void SetGetFullHashResponse(const GURL& url,
146 const SBFullHashResult& full_hash_result) {
147 full_hashes_[url.spec()].push_back(full_hash_result);
148 }
149
150 private:
151 base::hash_map<std::string, std::vector<SBFullHashResult> > full_hashes_;
152 SafeBrowsingService* sb_service_;
153 };
154
155 // Factory that creates FakeSafeBrowsingDatabase instances.
Scott Hess - ex-Googler 2010/12/07 21:22:57 Wrong class name in comment. Again consistent nam
lzheng 2010/12/08 01:57:32 Done.
156 class TestProtocolManagerFactory : public ProtocolManagerFactory {
157 public:
158 TestProtocolManagerFactory() : pm_(NULL) { }
159 virtual ~TestProtocolManagerFactory() { }
160
161 virtual SafeBrowsingProtocolManager* CreateProtocolManager(
162 SafeBrowsingService* sb_service,
163 const std::string& client_name,
164 const std::string& client_key,
165 const std::string& wrapped_key,
166 URLRequestContextGetter* request_context_getter,
167 const std::string& info_url_prefix,
168 const std::string& mackey_url_prefix,
169 bool disable_auto_update) {
170 pm_ = new FakeProtocolManager(
171 sb_service, client_name, client_key, wrapped_key,
172 request_context_getter, info_url_prefix, mackey_url_prefix,
173 disable_auto_update);
174 return pm_;
175 }
176 SafeBrowsingProtocolManager* GetProtocolManager() {
177 return pm_;
178 }
179 private:
180 // Owned by the Safebrowsing service.
181 SafeBrowsingProtocolManager* pm_;
182 };
183
184
185 // Tests the safe browsing blocking page in a browser.
186 class SafeBrowsingServiceTest : public InProcessBrowserTest {
187 public:
188 SafeBrowsingServiceTest() {
189 }
190
191 static void GenerateFullhashResult(const GURL& url,
192 const std::string& list_name,
193 int add_chunk_id,
194 SBFullHashResult* full_hash) {
195 std::string host;
196 std::string path;
197 safe_browsing_util::CanonicalizeUrl(url, &host, &path, NULL);
198 base::SHA256HashString(host + path, &full_hash->hash,
199 sizeof(SBFullHash));
200 full_hash->list_name = list_name;
201 full_hash->add_chunk_id = add_chunk_id;
202 }
203
204 virtual void SetUp() {
205 SafeBrowsingDatabase::RegisterFactory(&db_factory_);
206 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_);
207 InProcessBrowserTest::SetUp();
208 }
209
210 virtual void TearDown() {
211 InProcessBrowserTest::TearDown();
212 SafeBrowsingDatabase::RegisterFactory(NULL);
Scott Hess - ex-Googler 2010/12/07 21:22:57 Un-register both factories. I suspect you have th
lzheng 2010/12/08 01:57:32 Done.
213 }
214
215 virtual void SetUpCommandLine(CommandLine* command_line) {
216 // Makes sure the auto update is not triggered during the test.
217 // This test will fill up the database using testing prefixes
218 // and urls.
219 command_line->AppendSwitch(switches::kSbDisableAutoUpdate);
220 }
221
222 virtual void SetUpInProcessBrowserTestFixture() {
223 ASSERT_TRUE(test_server()->Start());
224 }
225
226 // This will setup the prefix in database and prepare protocol manager
227 // to response with |full_hash| for get full hash request.
228 void SetupResponseForUrl(const GURL& url,
229 const SBFullHashResult& full_hash) {
230 std::vector<SBPrefix> prefix_hits;
231 prefix_hits.push_back(full_hash.hash.prefix);
232
233 // Make sure the full hits is empty unless we need to test the
234 // full hash is hit in database's local cache.
235 std::vector<SBFullHashResult> empty_full_hits;
236 FakeSafeBrowsingDatabase* db =
237 static_cast<FakeSafeBrowsingDatabase*>(db_factory_.GetDb());
Scott Hess - ex-Googler 2010/12/07 21:22:57 Just have db_factory_.GetDb() return FakeSafeBrows
lzheng 2010/12/08 01:57:32 Done.
238 db->AddUrl(url, prefix_hits, empty_full_hits);
239
240 FakeProtocolManager* pm =
241 static_cast<FakeProtocolManager*>(pm_factory_.GetProtocolManager());
Scott Hess - ex-Googler 2010/12/07 21:22:57 Here too.
lzheng 2010/12/08 01:57:32 Done.
242 pm->SetGetFullHashResponse(url, full_hash);
243 }
244
245 bool ShowingInterstitialPage() {
246 TabContents* contents = browser()->GetSelectedTabContents();
247 InterstitialPage* interstitial_page = contents->interstitial_page();
248 return interstitial_page != NULL;
249 }
250
251 private:
252 TestSafeBrowsingDatabaseFactory db_factory_;
253 TestProtocolManagerFactory pm_factory_;
254
255 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest);
256 };
257
258 namespace {
Paweł Hajdan Jr. 2010/12/07 08:17:22 nit: Is it possible to put everything here in the
lzheng 2010/12/08 01:57:32 Done.
259
260 const char kEmptyPage[] = "files/empty.html";
261 const char kMalwarePage[] = "files/safe_browsing/malware.html";
262 const char kMalwareIframe[] = "files/safe_browsing/malware_iframe.html";
263
264 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Malware) {
265 GURL url = test_server()->GetURL(kEmptyPage);
266 // After adding the url to safebrowsing database and getfullhash result,
267 // we should see the interstitial page.
268 SBFullHashResult malware_full_hash;
269 int chunk_id = 0;
270 GenerateFullhashResult(url, safe_browsing_util::kMalwareList, chunk_id,
271 &malware_full_hash);
272 SetupResponseForUrl(url, malware_full_hash);
273 ui_test_utils::NavigateToURL(browser(), url);
274 EXPECT_TRUE(ShowingInterstitialPage());
275 }
276
277 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698