|
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 // This test creates a safebrowsing service using test safebrowsing database | |
6 // and a test 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 namespace { | |
25 | |
26 // A SafeBrowingDatabase class that allows us to inject the malicious URLs. | |
27 class TestSafeBrowsingDatabase : public SafeBrowsingDatabase { | |
28 public: | |
29 TestSafeBrowsingDatabase() {} | |
30 | |
31 virtual ~TestSafeBrowsingDatabase() {} | |
32 | |
33 // Initializes the database with the given filename. | |
34 virtual void Init(const FilePath& filename) {} | |
35 | |
36 // Deletes the current database and creates a new one. | |
37 virtual bool ResetDatabase() { | |
38 badurls_.clear(); | |
39 return true; | |
40 } | |
41 | |
42 // Called on the IO thread to check if the given URL is safe or not. If we | |
43 // can synchronously determine that the URL is safe, CheckUrl returns true, | |
44 // otherwise it returns false. | |
45 virtual bool ContainsUrl(const GURL& url, | |
46 std::string* matching_list, | |
47 std::vector<SBPrefix>* prefix_hits, | |
48 std::vector<SBFullHashResult>* full_hits, | |
49 base::Time last_update) { | |
50 base::hash_map<std::string, Hits>::const_iterator | |
51 badurls_it = badurls_.find(url.spec()); | |
Scott Hess - ex-Googler
2010/12/10 22:20:59
Sigh. Catching the iterator always seems like suc
lzheng
2010/12/11 01:15:20
LoL.
On 2010/12/10 22:20:59, shess wrote:
| |
52 if (badurls_it == badurls_.end()) | |
53 return false; | |
54 *prefix_hits = badurls_it->second.prefix_hits; | |
55 *full_hits = badurls_it->second.full_hits; | |
56 return true; | |
57 } | |
58 | |
59 virtual bool UpdateStarted(std::vector<SBListChunkRanges>* lists) { | |
60 ADD_FAILURE() << "Not implemented."; | |
61 return false; | |
62 } | |
63 virtual void InsertChunks(const std::string& list_name, | |
64 const SBChunkList& chunks) { | |
65 ADD_FAILURE() << "Not implemented."; | |
66 } | |
67 virtual void DeleteChunks(const std::vector<SBChunkDelete>& chunk_deletes) { | |
68 ADD_FAILURE() << "Not implemented."; | |
69 } | |
70 virtual void UpdateFinished(bool update_succeeded) { | |
71 ADD_FAILURE() << "Not implemented."; | |
72 } | |
73 virtual void CacheHashResults(const std::vector<SBPrefix>& prefixes, | |
74 const std::vector<SBFullHashResult>& full_hits) { | |
75 // Do nothing for the cache. | |
76 return; | |
77 } | |
78 | |
79 // Fill up the database with test URL. | |
80 void AddUrl(const GURL& url, | |
81 const std::vector<SBPrefix>& prefix_hits, | |
82 const std::vector<SBFullHashResult>& full_hits) { | |
83 badurls_[url.spec()].prefix_hits = prefix_hits; | |
84 badurls_[url.spec()].full_hits = full_hits; | |
85 } | |
86 private: | |
87 struct Hits { | |
88 std::vector<SBPrefix> prefix_hits; | |
89 std::vector<SBFullHashResult> full_hits; | |
90 }; | |
91 base::hash_map<std::string, Hits> badurls_; | |
92 }; | |
93 | |
94 // Factory that creates TestSafeBrowsingDatabase instances. | |
95 class TestSafeBrowsingDatabaseFactory : public SafeBrowsingDatabaseFactory { | |
96 public: | |
97 TestSafeBrowsingDatabaseFactory() : db_(NULL) {} | |
98 virtual ~TestSafeBrowsingDatabaseFactory() {} | |
99 | |
100 virtual SafeBrowsingDatabase* CreateSafeBrowsingDatabase() { | |
101 db_ = new TestSafeBrowsingDatabase(); | |
102 return db_; | |
103 } | |
104 TestSafeBrowsingDatabase* GetDb() { | |
105 return db_; | |
106 } | |
107 private: | |
108 // Owned by the Safebrowsing service. | |
109 TestSafeBrowsingDatabase* db_; | |
Scott Hess - ex-Googler
2010/12/10 22:20:59
Should this be scoped_ptr<>?
lzheng
2010/12/11 01:15:20
No, see comments above. It is owned by the safebro
Scott Hess - ex-Googler
2010/12/13 20:33:52
Sorry, I think I was conflating the factory instan
| |
110 }; | |
111 | |
112 | |
113 // A TestProtocolManager that could return fixed responses from | |
114 // safebrowsing server for testing purpose. | |
115 class TestProtocolManager : public SafeBrowsingProtocolManager { | |
116 public: | |
117 TestProtocolManager(SafeBrowsingService* sb_service, | |
118 const std::string& client_name, | |
119 const std::string& client_key, | |
120 const std::string& wrapped_key, | |
121 URLRequestContextGetter* request_context_getter, | |
122 const std::string& info_url_prefix, | |
123 const std::string& mackey_url_prefix, | |
124 bool disable_auto_update) | |
125 : SafeBrowsingProtocolManager(sb_service, client_name, client_key, | |
126 wrapped_key, request_context_getter, | |
127 info_url_prefix, mackey_url_prefix, | |
128 disable_auto_update), | |
129 sb_service_(sb_service) { | |
130 } | |
131 | |
132 // This function is called when there is a prefix hit in local safebrowsing | |
133 // database and safebrowsing service issues a get hash request to backends. | |
134 // We return a result from the prefilled full_hashes_ hash_map to simulate | |
135 // server's response. | |
136 virtual void GetFullHash(SafeBrowsingService::SafeBrowsingCheck* check, | |
137 const std::vector<SBPrefix>& prefixes) { | |
138 // The hash result should be inserted to the full_hashes_. | |
139 ASSERT_TRUE(full_hashes_.find(check->url.spec()) != full_hashes_.end()); | |
140 // When we get a valid response, always cache the result. | |
141 bool cancache = true; | |
142 BrowserThread::PostTask( | |
143 BrowserThread::IO, FROM_HERE, | |
144 NewRunnableMethod( | |
145 sb_service_, &SafeBrowsingService::HandleGetHashResults, | |
146 check, full_hashes_[check->url.spec()], cancache)); | |
147 } | |
148 | |
149 // Prepare the GetFullHash results for |url|. | |
150 void SetGetFullHashResponse(const GURL& url, | |
151 const SBFullHashResult& full_hash_result) { | |
152 full_hashes_[url.spec()].push_back(full_hash_result); | |
153 } | |
154 | |
155 private: | |
156 base::hash_map<std::string, std::vector<SBFullHashResult> > full_hashes_; | |
157 SafeBrowsingService* sb_service_; | |
158 }; | |
159 | |
160 // Factory that creates TestProtocolManager instances. | |
161 class TestSBProtocolManagerFactory : public SBProtocolManagerFactory { | |
162 public: | |
163 TestSBProtocolManagerFactory() : pm_(NULL) {} | |
164 virtual ~TestSBProtocolManagerFactory() {} | |
165 | |
166 virtual SafeBrowsingProtocolManager* CreateProtocolManager( | |
167 SafeBrowsingService* sb_service, | |
168 const std::string& client_name, | |
169 const std::string& client_key, | |
170 const std::string& wrapped_key, | |
171 URLRequestContextGetter* request_context_getter, | |
172 const std::string& info_url_prefix, | |
173 const std::string& mackey_url_prefix, | |
174 bool disable_auto_update) { | |
175 pm_ = new TestProtocolManager( | |
176 sb_service, client_name, client_key, wrapped_key, | |
177 request_context_getter, info_url_prefix, mackey_url_prefix, | |
178 disable_auto_update); | |
179 return pm_; | |
180 } | |
181 TestProtocolManager* GetProtocolManager() { | |
182 return pm_; | |
183 } | |
184 private: | |
185 // Owned by the Safebrowsing service. | |
186 TestProtocolManager* pm_; | |
Scott Hess - ex-Googler
2010/12/10 22:20:59
Should this be scoped_ptr<>?
lzheng
2010/12/11 01:15:20
Same as db_. Owned by safebrowsing service.
| |
187 }; | |
188 | |
189 | |
190 // Tests the safe browsing blocking page in a browser. | |
191 class SafeBrowsingServiceTest : public InProcessBrowserTest { | |
192 public: | |
193 SafeBrowsingServiceTest() { | |
194 } | |
195 | |
196 static void GenerateFullhashResult(const GURL& url, | |
197 const std::string& list_name, | |
198 int add_chunk_id, | |
199 SBFullHashResult* full_hash) { | |
200 std::string host; | |
201 std::string path; | |
202 safe_browsing_util::CanonicalizeUrl(url, &host, &path, NULL); | |
203 base::SHA256HashString(host + path, &full_hash->hash, | |
204 sizeof(SBFullHash)); | |
205 full_hash->list_name = list_name; | |
206 full_hash->add_chunk_id = add_chunk_id; | |
207 } | |
208 | |
209 virtual void SetUp() { | |
210 // InProcessBrowserTest::SetUp() intantiates SafebrowsingService and | |
211 // RegisterFactory has to be called before SafeBrowsingService is created. | |
212 SafeBrowsingDatabase::RegisterFactory(&db_factory_); | |
213 SafeBrowsingProtocolManager::RegisterFactory(&pm_factory_); | |
214 | |
215 InProcessBrowserTest::SetUp(); | |
216 } | |
217 | |
218 virtual void TearDown() { | |
219 InProcessBrowserTest::TearDown(); | |
220 | |
221 // Unregister test factories after InProcessBrowserTest::TearDown | |
222 // (which distructs SafeBrowsingService). | |
Scott Hess - ex-Googler
2010/12/10 22:20:59
s/distructs/destructs/
lzheng
2010/12/11 01:15:20
Done.
| |
223 SafeBrowsingDatabase::RegisterFactory(NULL); | |
224 SafeBrowsingProtocolManager::RegisterFactory(NULL); | |
225 } | |
226 | |
227 virtual void SetUpCommandLine(CommandLine* command_line) { | |
228 // Makes sure the auto update is not triggered during the test. | |
229 // This test will fill up the database using testing prefixes | |
230 // and urls. | |
231 command_line->AppendSwitch(switches::kSbDisableAutoUpdate); | |
232 } | |
233 | |
234 virtual void SetUpInProcessBrowserTestFixture() { | |
235 ASSERT_TRUE(test_server()->Start()); | |
236 } | |
237 | |
238 // This will setup the prefix in database and prepare protocol manager | |
239 // to response with |full_hash| for get full hash request. | |
240 void SetupResponseForUrl(const GURL& url, | |
241 const SBFullHashResult& full_hash) { | |
Scott Hess - ex-Googler
2010/12/10 22:20:59
indent.
lzheng
2010/12/11 01:15:20
Done.
| |
242 std::vector<SBPrefix> prefix_hits; | |
243 prefix_hits.push_back(full_hash.hash.prefix); | |
244 | |
245 // Make sure the full hits is empty unless we need to test the | |
246 // full hash is hit in database's local cache. | |
247 std::vector<SBFullHashResult> empty_full_hits; | |
248 TestSafeBrowsingDatabase* db = db_factory_.GetDb(); | |
249 db->AddUrl(url, prefix_hits, empty_full_hits); | |
250 | |
251 TestProtocolManager* pm = pm_factory_.GetProtocolManager(); | |
252 pm->SetGetFullHashResponse(url, full_hash); | |
253 } | |
254 | |
255 bool ShowingInterstitialPage() { | |
256 TabContents* contents = browser()->GetSelectedTabContents(); | |
257 InterstitialPage* interstitial_page = contents->interstitial_page(); | |
258 return interstitial_page != NULL; | |
259 } | |
260 | |
261 private: | |
262 TestSafeBrowsingDatabaseFactory db_factory_; | |
263 TestSBProtocolManagerFactory pm_factory_; | |
264 | |
265 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceTest); | |
266 }; | |
267 | |
268 const char kEmptyPage[] = "files/empty.html"; | |
269 const char kMalwarePage[] = "files/safe_browsing/malware.html"; | |
270 const char kMalwareIframe[] = "files/safe_browsing/malware_iframe.html"; | |
271 | |
272 IN_PROC_BROWSER_TEST_F(SafeBrowsingServiceTest, Malware) { | |
273 GURL url = test_server()->GetURL(kEmptyPage); | |
274 // After adding the url to safebrowsing database and getfullhash result, | |
275 // we should see the interstitial page. | |
276 SBFullHashResult malware_full_hash; | |
277 int chunk_id = 0; | |
278 GenerateFullhashResult(url, safe_browsing_util::kMalwareList, chunk_id, | |
279 &malware_full_hash); | |
280 SetupResponseForUrl(url, malware_full_hash); | |
281 ui_test_utils::NavigateToURL(browser(), url); | |
282 EXPECT_TRUE(ShowingInterstitialPage()); | |
283 } | |
284 | |
285 } // namespace | |
OLD | NEW |