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

Unified Diff: components/safe_browsing_db/v4_local_database_manager_unittest.cc

Issue 2687043003: GetThreatSeverity should handle the case of CheckResourceUrl. (Closed)
Patch Set: Created 3 years, 10 months 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
« no previous file with comments | « components/safe_browsing_db/v4_local_database_manager.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 db653626ff47ac5021040349b14f8562b1ba75f3..e005c1839f7003c90e596aedb337e331e9d5cdd6 100644
--- a/components/safe_browsing_db/v4_local_database_manager_unittest.cc
+++ b/components/safe_browsing_db/v4_local_database_manager_unittest.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "components/safe_browsing_db/v4_local_database_manager.h"
+#include "base/base64.h"
#include "base/files/scoped_temp_dir.h"
#include "base/memory/ptr_util.h"
#include "base/memory/ref_counted.h"
@@ -9,7 +11,6 @@
#include "base/test/test_simple_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/safe_browsing_db/v4_database.h"
-#include "components/safe_browsing_db/v4_local_database_manager.h"
#include "components/safe_browsing_db/v4_test_util.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "crypto/sha2.h"
@@ -20,6 +21,8 @@ namespace safe_browsing {
namespace {
+typedef std::vector<FullHashInfo> FullHashInfos;
+
// Utility function for populating hashes.
FullHash HashForUrl(const GURL& url) {
std::vector<FullHash> full_hashes;
@@ -34,39 +37,49 @@ class FakeGetHashProtocolManager : public V4GetHashProtocolManager {
FakeGetHashProtocolManager(
net::URLRequestContextGetter* request_context_getter,
const StoresToCheck& stores_to_check,
- const V4ProtocolConfig& config)
+ const V4ProtocolConfig& config,
+ const FullHashInfos& full_hash_infos)
: V4GetHashProtocolManager(request_context_getter,
stores_to_check,
- config) {}
+ config),
+ full_hash_infos_(full_hash_infos) {}
void GetFullHashes(const FullHashToStoreAndHashPrefixesMap&,
FullHashCallback callback) override {
- std::vector<FullHashInfo> full_hash_infos;
-
// Async, since the real manager might use a fetcher.
base::ThreadTaskRunnerHandle::Get()->PostTask(
- FROM_HERE, base::Bind(callback, full_hash_infos));
+ FROM_HERE, base::Bind(callback, full_hash_infos_));
}
+
+ private:
+ FullHashInfos full_hash_infos_;
};
class FakeGetHashProtocolManagerFactory
: public V4GetHashProtocolManagerFactory {
public:
+ FakeGetHashProtocolManagerFactory(const FullHashInfos& full_hash_infos)
+ : full_hash_infos_(full_hash_infos) {}
+
std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager(
net::URLRequestContextGetter* request_context_getter,
const StoresToCheck& stores_to_check,
const V4ProtocolConfig& config) override {
return base::MakeUnique<FakeGetHashProtocolManager>(
- request_context_getter, stores_to_check, config);
+ request_context_getter, stores_to_check, config, full_hash_infos_);
}
+
+ private:
+ FullHashInfos full_hash_infos_;
};
// Use FakeGetHashProtocolManagerFactory in scope, then reset.
class ScopedFakeGetHashProtocolManagerFactory {
public:
- ScopedFakeGetHashProtocolManagerFactory() {
+ ScopedFakeGetHashProtocolManagerFactory(
+ const FullHashInfos& full_hash_infos) {
V4GetHashProtocolManager::RegisterFactory(
- base::MakeUnique<FakeGetHashProtocolManagerFactory>());
+ base::MakeUnique<FakeGetHashProtocolManagerFactory>(full_hash_infos));
}
~ScopedFakeGetHashProtocolManagerFactory() {
V4GetHashProtocolManager::RegisterFactory(nullptr);
@@ -167,9 +180,7 @@ class TestClient : public SafeBrowsingDatabaseManager::Client {
const std::string& threat_hash) override {
DCHECK_EQ(expected_url, url);
DCHECK_EQ(expected_sb_threat_type, threat_type);
- // |threat_hash| is empty because GetFullHashes calls back with empty
- // |full_hash_infos|.
- DCHECK(threat_hash.empty());
+ DCHECK_EQ(threat_type == SB_THREAT_TYPE_SAFE, threat_hash.empty());
on_check_resource_url_result_called_ = true;
}
@@ -445,7 +456,7 @@ TEST_F(V4LocalDatabaseManagerTest, TestChecksAreQueued) {
// Verify that a window where checks cannot be cancelled is closed.
TEST_F(V4LocalDatabaseManagerTest, CancelPending) {
// Setup to receive full-hash misses.
- ScopedFakeGetHashProtocolManagerFactory pin;
+ ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({}));
// Reset the database manager so it picks up the replacement protocol manager.
ResetLocalDatabaseManager();
@@ -672,7 +683,7 @@ TEST_F(V4LocalDatabaseManagerTest, TestMatchModuleWhitelist) {
// This verifies the fix for race in http://crbug.com/660293
TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithSameClientAndCancel) {
- ScopedFakeGetHashProtocolManagerFactory pin;
+ ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({}));
// Reset the database manager so it picks up the replacement protocol manager.
ResetLocalDatabaseManager();
WaitForTasksOnTaskRunner();
@@ -707,7 +718,7 @@ TEST_F(V4LocalDatabaseManagerTest, TestCheckBrowseUrlWithSameClientAndCancel) {
TEST_F(V4LocalDatabaseManagerTest, TestCheckResourceUrl) {
// Setup to receive full-hash misses.
- ScopedFakeGetHashProtocolManagerFactory pin;
+ ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({}));
// Reset the database manager so it picks up the replacement protocol manager.
ResetLocalDatabaseManager();
@@ -730,6 +741,66 @@ TEST_F(V4LocalDatabaseManagerTest, TestCheckResourceUrl) {
EXPECT_TRUE(client.on_check_resource_url_result_called_);
}
+TEST_F(V4LocalDatabaseManagerTest, TestSubresourceFilterCallback) {
+ // Setup to receive full-hash misses.
+ ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({}));
+
+ // Reset the database manager so it picks up the replacement protocol manager.
+ ResetLocalDatabaseManager();
+ WaitForTasksOnTaskRunner();
+
+ // An URL and matching prefix.
+ const GURL url("http://example.com/a/");
+ const HashPrefix hash_prefix("eW\x1A\xF\xA9");
+
+ // Put a match in the db that will cause a protocol-manager request.
+ StoreAndHashPrefixes store_and_hash_prefixes;
+ store_and_hash_prefixes.emplace_back(GetUrlSubresourceFilterId(),
+ hash_prefix);
+ ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */);
+
+ // Test that a request flows through to the callback.
+ {
+ TestClient client(SB_THREAT_TYPE_SAFE, url);
+ EXPECT_FALSE(
+ v4_local_database_manager_->CheckUrlForSubresourceFilter(url, &client));
+ EXPECT_FALSE(client.on_check_browse_url_result_called_);
+ WaitForTasksOnTaskRunner();
+ EXPECT_TRUE(client.on_check_browse_url_result_called_);
+ }
+}
+
+TEST_F(V4LocalDatabaseManagerTest, TestCheckResourceUrlReturnsBad) {
+ std::string base64_encoded = "ZVcaD6lke9GaaZEf07X3CpuEgMAqbpAyPw3sX/7eK9M=";
+ std::string base64_decoded;
+ base::Base64Decode(base64_encoded, &base64_decoded);
+ FullHashInfo fhi(base64_decoded, GetChromeUrlClientIncidentId(),
+ base::Time());
+
+ // Setup to receive full-hash hit.
+ ScopedFakeGetHashProtocolManagerFactory pin(FullHashInfos({fhi}));
+
+ // Reset the database manager so it picks up the replacement protocol manager.
+ ResetLocalDatabaseManager();
+ WaitForTasksOnTaskRunner();
+
+ // An URL and matching prefix.
+ const GURL url("http://example.com/a/");
+ const HashPrefix hash_prefix("eW\x1A\xF\xA9");
+
+ // Put a match in the db that will cause a protocol-manager request.
+ StoreAndHashPrefixes store_and_hash_prefixes;
+ store_and_hash_prefixes.emplace_back(GetChromeUrlClientIncidentId(),
+ hash_prefix);
+ ReplaceV4Database(store_and_hash_prefixes, true /* stores_available */);
+
+ TestClient client(SB_THREAT_TYPE_BLACKLISTED_RESOURCE, url);
+ EXPECT_FALSE(v4_local_database_manager_->CheckResourceUrl(url, &client));
+ EXPECT_FALSE(client.on_check_resource_url_result_called_);
+ WaitForTasksOnTaskRunner();
+ EXPECT_TRUE(client.on_check_resource_url_result_called_);
+}
+
// TODO(nparker): Add tests for
// CheckDownloadUrl()
// CheckExtensionIDs()
« no previous file with comments | « components/safe_browsing_db/v4_local_database_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698