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

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: Add a comma after the last enum value 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 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 // Both the stores are empty right now so CheckBrowseUrl should return true. 230 // Both the stores are empty right now so CheckBrowseUrl should return true.
225 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl( 231 EXPECT_TRUE(v4_local_database_manager_->CheckBrowseUrl(
226 GURL("http://example.com/a/"), nullptr)); 232 GURL("http://example.com/a/"), nullptr));
227 } 233 }
228 234
229 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) { 235 TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) {
230 WaitForTasksOnTaskRunner(); 236 WaitForTasksOnTaskRunner();
231 net::TestURLFetcherFactory factory; 237 net::TestURLFetcherFactory factory;
232 238
233 StoreAndHashPrefixes store_and_hash_prefixes; 239 StoreAndHashPrefixes store_and_hash_prefixes;
234 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); 240 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
241 HashPrefix("eW\x1A\xF\xA9"));
235 ReplaceV4Database(store_and_hash_prefixes); 242 ReplaceV4Database(store_and_hash_prefixes);
236 243
237 // The fake database returns a matched hash prefix. 244 // The fake database returns a matched hash prefix.
238 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( 245 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(
239 GURL("http://example.com/a/"), nullptr)); 246 GURL("http://example.com/a/"), nullptr));
240 247
241 // Wait for PerformFullHashCheck to complete. 248 // Wait for PerformFullHashCheck to complete.
242 WaitForTasksOnTaskRunner(); 249 WaitForTasksOnTaskRunner();
243 } 250 }
244 251
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 // ~V4LocalDatabaseManager expects. 318 // ~V4LocalDatabaseManager expects.
312 StopLocalDatabaseManager(); 319 StopLocalDatabaseManager();
313 v4_local_database_manager_ = 320 v4_local_database_manager_ =
314 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath())); 321 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath()));
315 SetTaskRunnerForTest(); 322 SetTaskRunnerForTest();
316 StartLocalDatabaseManager(); 323 StartLocalDatabaseManager();
317 WaitForTasksOnTaskRunner(); 324 WaitForTasksOnTaskRunner();
318 net::TestURLFetcherFactory factory; 325 net::TestURLFetcherFactory factory;
319 326
320 StoreAndHashPrefixes store_and_hash_prefixes; 327 StoreAndHashPrefixes store_and_hash_prefixes;
321 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); 328 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
329 HashPrefix("eW\x1A\xF\xA9"));
322 ReplaceV4Database(store_and_hash_prefixes); 330 ReplaceV4Database(store_and_hash_prefixes);
323 331
324 // The fake database returns a matched hash prefix. 332 // The fake database returns a matched hash prefix.
325 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( 333 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(
326 GURL("http://example.com/a/"), nullptr)); 334 GURL("http://example.com/a/"), nullptr));
327 335
328 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( 336 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
329 v4_local_database_manager_)); 337 v4_local_database_manager_));
330 338
331 // Wait for PerformFullHashCheck to complete. 339 // Wait for PerformFullHashCheck to complete.
332 WaitForTasksOnTaskRunner(); 340 WaitForTasksOnTaskRunner();
333 341
334 EXPECT_TRUE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled( 342 EXPECT_TRUE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
335 v4_local_database_manager_)); 343 v4_local_database_manager_));
336 } 344 }
337 345
338 TEST_F(V4LocalDatabaseManagerTest, UsingWeakPtrDropsCallback) { 346 TEST_F(V4LocalDatabaseManagerTest, UsingWeakPtrDropsCallback) {
339 // StopLocalDatabaseManager before resetting it because that's what 347 // StopLocalDatabaseManager before resetting it because that's what
340 // ~V4LocalDatabaseManager expects. 348 // ~V4LocalDatabaseManager expects.
341 StopLocalDatabaseManager(); 349 StopLocalDatabaseManager();
342 v4_local_database_manager_ = 350 v4_local_database_manager_ =
343 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath())); 351 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath()));
344 SetTaskRunnerForTest(); 352 SetTaskRunnerForTest();
345 StartLocalDatabaseManager(); 353 StartLocalDatabaseManager();
346 WaitForTasksOnTaskRunner(); 354 WaitForTasksOnTaskRunner();
347 net::TestURLFetcherFactory factory; 355 net::TestURLFetcherFactory factory;
348 356
349 StoreAndHashPrefixes store_and_hash_prefixes; 357 StoreAndHashPrefixes store_and_hash_prefixes;
350 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa")); 358 store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
359 HashPrefix("eW\x1A\xF\xA9"));
351 ReplaceV4Database(store_and_hash_prefixes); 360 ReplaceV4Database(store_and_hash_prefixes);
352 361
353 // The fake database returns a matched hash prefix. 362 // The fake database returns a matched hash prefix.
354 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl( 363 EXPECT_FALSE(v4_local_database_manager_->CheckBrowseUrl(
355 GURL("http://example.com/a/"), nullptr)); 364 GURL("http://example.com/a/"), nullptr));
356 v4_local_database_manager_->StopOnIOThread(true); 365 v4_local_database_manager_->StopOnIOThread(true);
357 366
358 // Release the V4LocalDatabaseManager object right away before the callback 367 // Release the V4LocalDatabaseManager object right away before the callback
359 // gets called. When the callback gets called, without using a weak-ptr 368 // 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 369 // factory, this leads to a use after free. However, using the weak-ptr means
361 // that the callback is simply dropped. 370 // that the callback is simply dropped.
362 v4_local_database_manager_ = nullptr; 371 v4_local_database_manager_ = nullptr;
363 372
364 // Wait for the tasks scheduled by StopOnIOThread to complete. 373 // Wait for the tasks scheduled by StopOnIOThread to complete.
365 WaitForTasksOnTaskRunner(); 374 WaitForTasksOnTaskRunner();
366 } 375 }
367 376
377 TEST_F(V4LocalDatabaseManagerTest, TestMatchMalwareIP) {
378 StopLocalDatabaseManager();
379 v4_local_database_manager_ =
380 make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath()));
381 SetTaskRunnerForTest();
382 StartLocalDatabaseManager();
383 WaitForTasksOnTaskRunner();
384
385 // >>> hashlib.sha1(socket.inet_pton(socket.AF_INET6,
386 // '::ffff:192.168.1.2')).digest() + chr(128)
387 // '\xb3\xe0z\xafAv#h\x9a\xcf<\xf3ee\x94\xda\xf6y\xb1\xad\x80'
388 StoreAndHashPrefixes store_and_hash_prefixes;
389 store_and_hash_prefixes.emplace_back(GetAnyIpMalwareId(),
390 FullHash("\xB3\xE0z\xAF"
391 "Av#h\x9A\xCF<\xF3"
392 "ee\x94\xDA\xF6y\xB1\xAD\x80"));
393 ReplaceV4Database(store_and_hash_prefixes);
394
395 EXPECT_FALSE(v4_local_database_manager_->MatchMalwareIP(""));
396 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
397 v4_local_database_manager_));
398
399 // The fake database returns no match.
400 EXPECT_FALSE(v4_local_database_manager_->MatchMalwareIP("192.168.1.1"));
401 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
402 v4_local_database_manager_));
403
404 // The fake database returns a matched hash prefix.
405 EXPECT_TRUE(v4_local_database_manager_->MatchMalwareIP("192.168.1.2"));
406 EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
407 v4_local_database_manager_));
408 }
409
368 } // namespace safe_browsing 410 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_local_database_manager.cc ('k') | components/safe_browsing_db/v4_protocol_manager_util.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698