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

Side by Side Diff: components/safe_browsing_db/v4_local_database_manager_unittest.cc

Issue 2495783003: Implement support for checking bad IPs aka MatchMalwareIP (Closed)
Patch Set: shess@ review and trying to fix Windows build error Created 4 years, 1 month 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/scoped_temp_dir.h" 5 #include "base/files/scoped_temp_dir.h"
6 #include "base/memory/ptr_util.h" 6 #include "base/memory/ptr_util.h"
7 #include "base/memory/ref_counted.h" 7 #include "base/memory/ref_counted.h"
8 #include "base/run_loop.h" 8 #include "base/run_loop.h"
9 #include "base/test/test_simple_task_runner.h" 9 #include "base/test/test_simple_task_runner.h"
10 #include "components/safe_browsing_db/v4_database.h" 10 #include "components/safe_browsing_db/v4_database.h"
(...skipping 19 matching lines...) Expand all
30 FROM_HERE, 30 FROM_HERE,
31 base::Bind(&FakeV4Database::CreateOnTaskRunner, db_task_runner, 31 base::Bind(&FakeV4Database::CreateOnTaskRunner, db_task_runner,
32 base::Passed(&store_map), store_and_hash_prefixes, 32 base::Passed(&store_map), store_and_hash_prefixes,
33 callback_task_runner, new_db_callback)); 33 callback_task_runner, new_db_callback));
34 } 34 }
35 35
36 void GetStoresMatchingFullHash( 36 void GetStoresMatchingFullHash(
37 const FullHash& full_hash, 37 const FullHash& full_hash,
38 const StoresToCheck& stores_to_check, 38 const StoresToCheck& stores_to_check,
39 StoreAndHashPrefixes* store_and_hash_prefixes) override { 39 StoreAndHashPrefixes* store_and_hash_prefixes) override {
40 *store_and_hash_prefixes = store_and_hash_prefixes_; 40 store_and_hash_prefixes->clear();
41 for (const StoreAndHashPrefix& stored_sahp : store_and_hash_prefixes_) {
42 const PrefixSize& prefix_size = stored_sahp.hash_prefix.size();
43 if (!full_hash.compare(0, prefix_size, stored_sahp.hash_prefix)) {
44 store_and_hash_prefixes->push_back(stored_sahp);
45 }
46 }
41 } 47 }
42 48
43 private: 49 private:
44 static void CreateOnTaskRunner( 50 static void CreateOnTaskRunner(
45 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, 51 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner,
46 std::unique_ptr<StoreMap> store_map, 52 std::unique_ptr<StoreMap> store_map,
47 const StoreAndHashPrefixes& store_and_hash_prefixes, 53 const StoreAndHashPrefixes& store_and_hash_prefixes,
48 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, 54 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner,
49 NewDatabaseReadyCallback new_db_callback) { 55 NewDatabaseReadyCallback new_db_callback) {
50 // Mimics the semantics of V4Database::CreateOnTaskRunner 56 // Mimics the semantics of V4Database::CreateOnTaskRunner
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 WaitForTasksOnTaskRunner(); 188 WaitForTasksOnTaskRunner();
183 } 189 }
184 190
185 void WaitForTasksOnTaskRunner() { 191 void WaitForTasksOnTaskRunner() {
186 // Wait for tasks on the task runner so we're sure that the 192 // Wait for tasks on the task runner so we're sure that the
187 // V4LocalDatabaseManager has read the data from disk. 193 // V4LocalDatabaseManager has read the data from disk.
188 task_runner_->RunPendingTasks(); 194 task_runner_->RunPendingTasks();
189 base::RunLoop().RunUntilIdle(); 195 base::RunLoop().RunUntilIdle();
190 } 196 }
191 197
198 bool IPAddressToEncodedIPV6(const std::string& ip_address,
199 std::string* encoded_ip) {
200 return V4LocalDatabaseManager::IPAddressToEncodedIPV6(ip_address,
201 encoded_ip);
202 }
203
192 base::ScopedTempDir base_dir_; 204 base::ScopedTempDir base_dir_;
193 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 205 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
194 content::TestBrowserThreadBundle thread_bundle_; 206 content::TestBrowserThreadBundle thread_bundle_;
195 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_; 207 scoped_refptr<V4LocalDatabaseManager> v4_local_database_manager_;
196 }; 208 };
197 209
198 TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) { 210 TEST_F(V4LocalDatabaseManagerTest, TestGetThreatSource) {
199 WaitForTasksOnTaskRunner(); 211 WaitForTasksOnTaskRunner();
200 EXPECT_EQ(ThreatSource::LOCAL_PVER4, 212 EXPECT_EQ(ThreatSource::LOCAL_PVER4,
201 v4_local_database_manager_->GetThreatSource()); 213 v4_local_database_manager_->GetThreatSource());
(...skipping 22 matching lines...) Expand all
224 // Both the stores are empty right now so CheckBrowseUrl should return true. 236 // Both the stores are empty right now so CheckBrowseUrl should return true.
225 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( 237 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl(
226 GURL("http://example.com/a/"), nullptr)); 238 GURL("http://example.com/a/"), nullptr));
227 } 239 }
228 240
229 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) { 241 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) {
230 WaitForTasksOnTaskRunner(); 242 WaitForTasksOnTaskRunner();
231 net::TestURLFetcherFactory factory; 243 net::TestURLFetcherFactory factory;
232 244
233 StoreAndHashPrefixes store_and_hash_prefixes; 245 StoreAndHashPrefixes store_and_hash_prefixes;
234 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); 246 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
247 HashPrefix("eW\x1A\xF\xA9"));
235 ReplaceV4Database(store_and_hash_prefixes); 248 ReplaceV4Database(store_and_hash_prefixes);
236 249
237 // The fake database returns a matched hash prefix. 250 // The fake database returns a matched hash prefix.
238 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( 251 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(
239 GURL("http://example.com/a/"), nullptr)); 252 GURL("http://example.com/a/"), nullptr));
240 253
241 // Wait for PerformFullHashCheck to complete. 254 // Wait for PerformFullHashCheck to complete.
242 WaitForTasksOnTaskRunner(); 255 WaitForTasksOnTaskRunner();
243 } 256 }
244 257
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 // ~V4LocalDatabaseManager expects. 324 // ~V4LocalDatabaseManager expects.
312 StopLocalDatabaseManager(); 325 StopLocalDatabaseManager();
313 v4_local_database_manager_ = 326 v4_local_database_manager_ =
314 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath())); 327 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath()));
315 SetTaskRunnerForTest(); 328 SetTaskRunnerForTest();
316 StartLocalDatabaseManager(); 329 StartLocalDatabaseManager();
317 WaitForTasksOnTaskRunner(); 330 WaitForTasksOnTaskRunner();
318 net::TestURLFetcherFactory factory; 331 net::TestURLFetcherFactory factory;
319 332
320 StoreAndHashPrefixes store_and_hash_prefixes; 333 StoreAndHashPrefixes store_and_hash_prefixes;
321 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); 334 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
335 HashPrefix("eW\x1A\xF\xA9"));
322 ReplaceV4Database(store_and_hash_prefixes); 336 ReplaceV4Database(store_and_hash_prefixes);
323 337
324 // The fake database returns a matched hash prefix. 338 // The fake database returns a matched hash prefix.
325 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( 339 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(
326 GURL("http://example.com/a/"), nullptr)); 340 GURL("http://example.com/a/"), nullptr));
327 341
328 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( 342 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
329 v4_local_database_manager_)); 343 v4_local_database_manager_));
330 344
331 // Wait for PerformFullHashCheck to complete. 345 // Wait for PerformFullHashCheck to complete.
332 WaitForTasksOnTaskRunner(); 346 WaitForTasksOnTaskRunner();
333 347
334 EXPECT_TRUE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( 348 EXPECT_TRUE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
335 v4_local_database_manager_)); 349 v4_local_database_manager_));
336 } 350 }
337 351
338 TEST_F(V4LocalDatabaseManagerTest, UsingWeakPtrDropsCallback) { 352 TEST_F(V4LocalDatabaseManagerTest, UsingWeakPtrDropsCallback) {
339 // StopLocalDatabaseManager before resetting it because that's what 353 // StopLocalDatabaseManager before resetting it because that's what
340 // ~V4LocalDatabaseManager expects. 354 // ~V4LocalDatabaseManager expects.
341 StopLocalDatabaseManager(); 355 StopLocalDatabaseManager();
342 v4_local_database_manager_ = 356 v4_local_database_manager_ =
343 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath())); 357 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath()));
344 SetTaskRunnerForTest(); 358 SetTaskRunnerForTest();
345 StartLocalDatabaseManager(); 359 StartLocalDatabaseManager();
346 WaitForTasksOnTaskRunner(); 360 WaitForTasksOnTaskRunner();
347 net::TestURLFetcherFactory factory; 361 net::TestURLFetcherFactory factory;
348 362
349 StoreAndHashPrefixes store_and_hash_prefixes; 363 StoreAndHashPrefixes store_and_hash_prefixes;
350 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); 364 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
365 HashPrefix("eW\x1A\xF\xA9"));
351 ReplaceV4Database(store_and_hash_prefixes); 366 ReplaceV4Database(store_and_hash_prefixes);
352 367
353 // The fake database returns a matched hash prefix. 368 // The fake database returns a matched hash prefix.
354 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( 369 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(
355 GURL("http://example.com/a/"), nullptr)); 370 GURL("http://example.com/a/"), nullptr));
356 v4_local_database_manager_->StopOnIOThread(true); 371 v4_local_database_manager_->StopOnIOThread(true);
357 372
358 // Release the V4LocalDatabaseManager object right away before the callback 373 // Release the V4LocalDatabaseManager object right away before the callback
359 // gets called. When the callback gets called, without using a weak-ptr 374 // gets called. When the callback gets called, without using a weak-ptr
360 // factory, this leads to a use after free. However, using the weak-ptr means 375 // factory, this leads to a use after free. However, using the weak-ptr means
361 // that the callback is simply dropped. 376 // that the callback is simply dropped.
362 v4_local_database_manager_ = nullptr; 377 v4_local_database_manager_ = nullptr;
363 378
364 // Wait for the tasks scheduled by StopOnIOThread to complete. 379 // Wait for the tasks scheduled by StopOnIOThread to complete.
365 WaitForTasksOnTaskRunner(); 380 WaitForTasksOnTaskRunner();
366 } 381 }
367 382
383 TEST_F(V4LocalDatabaseManagerTest, TestIPAddressToEncodedIPV6) {
384 // To verify the test values, here's the python code:
385 // >> import socket, hashlib, binascii
386 // >> hashlib.sha1(socket.inet_pton(socket.AF_INET6, input)).digest() +
387 // chr(128)
388 // For example:
389 // >>> hashlib.sha1(socket.inet_pton(socket.AF_INET6,
390 // '::ffff:192.168.1.1')).digest() + chr(128)
391 // 'X\xf8\xa1\x17I\xe6Pl\xfd\xdb\xbb\xa0\x0c\x02\x9d#\n|\xe7\xcd\x80'
392 std::vector<std::tuple<bool, std::string, std::string>> test_cases = {
393 std::make_tuple(false, "", ""),
394 std::make_tuple(
395 true, "192.168.1.1",
396 "X\xF8\xA1\x17I\xE6Pl\xFD\xDB\xBB\xA0\f\x2\x9D#\n|\xE7\xCD\x80"),
397 std::make_tuple(
398 true, "::",
399 "\xE1)\xF2|Q\x3\xBC\\\xC4K\xCD\xF0\xA1^\x16\rDPf\xFF\x80")};
400 for (size_t i = 0; i < test_cases.size(); i++) {
401 DVLOG(1) << "Running case: " << i;
402 bool success = std::get<0>(test_cases[i]);
403 const auto& input = std::get<1>(test_cases[i]);
404 const auto& expected_output = std::get<2>(test_cases[i]);
405 std::string encoded_ip;
406 ASSERT_EQ(success, IPAddressToEncodedIPV6(input, &encoded_ip));
407 if (success) {
408 ASSERT_EQ(expected_output, encoded_ip);
409 }
410 }
411 }
412
413 TEST_F(V4LocalDatabaseManagerTest, TestMatchMalwareIP) {
414 StopLocalDatabaseManager();
415 v4_local_database_manager_ =
416 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath()));
417 SetTaskRunnerForTest();
418 StartLocalDatabaseManager();
419 WaitForTasksOnTaskRunner();
420
421 // >>> hashlib.sha1(socket.inet_pton(socket.AF_INET6,
422 // '::ffff:192.168.1.2')).digest() + chr(128)
423 // '\xb3\xe0z\xafAv#h\x9a\xcf<\xf3ee\x94\xda\xf6y\xb1\xad\x80'
424 StoreAndHashPrefixes store_and_hash_prefixes;
425 store_and_hash_prefixes.emplace_back(GetAnyIpMalwareId(),
426 FullHash("\xB3\xE0z\xAF"
427 "Av#h\x9A\xCF<\xF3"
428 "ee\x94\xDA\xF6y\xB1\xAD\x80"));
429 ReplaceV4Database(store_and_hash_prefixes);
430
431 EXPECT_FALSE(v4_local_database_manager_->MatchMalwareIP(""));
432 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
433 v4_local_database_manager_));
434
435 // The fake database returns no match.
436 EXPECT_FALSE(v4_local_database_manager_->MatchMalwareIP("192.168.1.1"));
437 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
438 v4_local_database_manager_));
439
440 // The fake database returns a matched hash prefix.
441 EXPECT_TRUE(v4_local_database_manager_->MatchMalwareIP("192.168.1.2"));
442 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
443 v4_local_database_manager_));
444 }
445
368 } // namespace safe_browsing 446 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698