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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: components/safe_browsing_db/v4_local_database_manager_unittest.cc
diff --git a/components/safe_browsing_db/v4_local_database_manager_unittest.cc b/components/safe_browsing_db/v4_local_database_manager_unittest.cc
index 5d0f850c6714182d2ce65272b9116f37e984d8ee..c0eff431a508e12bbe31d310ea9700ca12c390df 100644
--- a/components/safe_browsing_db/v4_local_database_manager_unittest.cc
+++ b/components/safe_browsing_db/v4_local_database_manager_unittest.cc
@@ -37,7 +37,13 @@ class FakeV4Database : public V4Database {
const FullHash& full_hash,
const StoresToCheck& stores_to_check,
StoreAndHashPrefixes* store_and_hash_prefixes) override {
- *store_and_hash_prefixes = store_and_hash_prefixes_;
+ store_and_hash_prefixes->clear();
+ for (const StoreAndHashPrefix& stored_sahp : store_and_hash_prefixes_) {
+ const PrefixSize& prefix_size = stored_sahp.hash_prefix.size();
+ if (!full_hash.compare(0, prefix_size, stored_sahp.hash_prefix)) {
+ store_and_hash_prefixes->push_back(stored_sahp);
+ }
+ }
}
private:
@@ -189,6 +195,12 @@ class V4LocalDatabaseManagerTest : public PlatformTest {
base::RunLoop().RunUntilIdle();
}
+ bool IPAddressToEncodedIPV6(const std::string& ip_address,
+ std::string* encoded_ip) {
+ return V4LocalDatabaseManager::IPAddressToEncodedIPV6(ip_address,
+ encoded_ip);
+ }
+
base::ScopedTempDir base_dir_;
scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
content::TestBrowserThreadBundle thread_bundle_;
@@ -231,7 +243,8 @@ TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithFakeDbReturnsMatch) {
net::TestURLFetcherFactory factory;
StoreAndHashPrefixes store_and_hash_prefixes;
- store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa"));
+ store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
+ HashPrefix("eW\x1A\xF\xA9"));
ReplaceV4Database(store_and_hash_prefixes);
// The fake database returns a matched hash prefix.
@@ -318,7 +331,8 @@ TEST_F(V4LocalDatabaseManagerTest, PerformFullHashCheckCalledAsync) {
net::TestURLFetcherFactory factory;
StoreAndHashPrefixes store_and_hash_prefixes;
- store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa"));
+ store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
+ HashPrefix("eW\x1A\xF\xA9"));
ReplaceV4Database(store_and_hash_prefixes);
// The fake database returns a matched hash prefix.
@@ -347,7 +361,8 @@ TEST_F(V4LocalDatabaseManagerTest, UsingWeakPtrDropsCallback) {
net::TestURLFetcherFactory factory;
StoreAndHashPrefixes store_and_hash_prefixes;
- store_and_hash_prefixes.emplace_back(GetUrlMalwareId(), HashPrefix("aaaa"));
+ store_and_hash_prefixes.emplace_back(GetUrlMalwareId(),
+ HashPrefix("eW\x1A\xF\xA9"));
ReplaceV4Database(store_and_hash_prefixes);
// The fake database returns a matched hash prefix.
@@ -365,4 +380,67 @@ TEST_F(V4LocalDatabaseManagerTest, UsingWeakPtrDropsCallback) {
WaitForTasksOnTaskRunner();
}
+TEST_F(V4LocalDatabaseManagerTest, TestIPAddressToEncodedIPV6) {
+ // To verify the test values, here's the python code:
+ // >> import socket, hashlib, binascii
+ // >> hashlib.sha1(socket.inet_pton(socket.AF_INET6, input)).digest() +
+ // chr(128)
+ // For example:
+ // >>> hashlib.sha1(socket.inet_pton(socket.AF_INET6,
+ // '::ffff:192.168.1.1')).digest() + chr(128)
+ // 'X\xf8\xa1\x17I\xe6Pl\xfd\xdb\xbb\xa0\x0c\x02\x9d#\n|\xe7\xcd\x80'
+ std::vector<std::tuple<bool, std::string, std::string>> test_cases = {
+ std::make_tuple(false, "", ""),
+ std::make_tuple(
+ true, "192.168.1.1",
+ "X\xF8\xA1\x17I\xE6Pl\xFD\xDB\xBB\xA0\f\x2\x9D#\n|\xE7\xCD\x80"),
+ std::make_tuple(
+ true, "::",
+ "\xE1)\xF2|Q\x3\xBC\\\xC4K\xCD\xF0\xA1^\x16\rDPf\xFF\x80")};
+ for (size_t i = 0; i < test_cases.size(); i++) {
+ DVLOG(1) << "Running case: " << i;
+ bool success = std::get<0>(test_cases[i]);
+ const auto& input = std::get<1>(test_cases[i]);
+ const auto& expected_output = std::get<2>(test_cases[i]);
+ std::string encoded_ip;
+ ASSERT_EQ(success, IPAddressToEncodedIPV6(input, &encoded_ip));
+ if (success) {
+ ASSERT_EQ(expected_output, encoded_ip);
+ }
+ }
+}
+
+TEST_F(V4LocalDatabaseManagerTest, TestMatchMalwareIP) {
+ StopLocalDatabaseManager();
+ v4_local_database_manager_ =
+ make_scoped_refptr(new FakeV4LocalDatabaseManager(base_dir_.GetPath()));
+ SetTaskRunnerForTest();
+ StartLocalDatabaseManager();
+ WaitForTasksOnTaskRunner();
+
+ // >>> hashlib.sha1(socket.inet_pton(socket.AF_INET6,
+ // '::ffff:192.168.1.2')).digest() + chr(128)
+ // '\xb3\xe0z\xafAv#h\x9a\xcf<\xf3ee\x94\xda\xf6y\xb1\xad\x80'
+ StoreAndHashPrefixes store_and_hash_prefixes;
+ store_and_hash_prefixes.emplace_back(GetAnyIpMalwareId(),
+ FullHash("\xB3\xE0z\xAF"
+ "Av#h\x9A\xCF<\xF3"
+ "ee\x94\xDA\xF6y\xB1\xAD\x80"));
+ ReplaceV4Database(store_and_hash_prefixes);
+
+ EXPECT_FALSE(v4_local_database_manager_->MatchMalwareIP(""));
+ EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
+ v4_local_database_manager_));
+
+ // The fake database returns no match.
+ EXPECT_FALSE(v4_local_database_manager_->MatchMalwareIP("192.168.1.1"));
+ EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
+ v4_local_database_manager_));
+
+ // The fake database returns a matched hash prefix.
+ EXPECT_TRUE(v4_local_database_manager_->MatchMalwareIP("192.168.1.2"));
+ EXPECT_FALSE(FakeV4LocalDatabaseManager::PerformFullHashCheckCalled(
+ v4_local_database_manager_));
+}
+
} // namespace safe_browsing

Powered by Google App Engine
This is Rietveld 408576698