Chromium Code Reviews| Index: components/safe_browsing_db/database_manager_unittest.cc |
| diff --git a/components/safe_browsing_db/database_manager_unittest.cc b/components/safe_browsing_db/database_manager_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..af1cdc44909c25e6e12deb962d73e7e6ce88dc28 |
| --- /dev/null |
| +++ b/components/safe_browsing_db/database_manager_unittest.cc |
| @@ -0,0 +1,137 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
|
Nathan Parker
2016/04/07 21:33:37
2016
kcarattini
2016/04/11 03:34:31
My how time flies! Done.
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <stddef.h> |
| + |
| +#include <string> |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/run_loop.h" |
| +#include "components/safe_browsing_db/database_manager.h" |
| +#include "components/safe_browsing_db/test_database_manager.h" |
| +#include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| +#include "url/gurl.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace safe_browsing { |
| + |
| +namespace { |
| + |
| +// A TestV4GetHashProtocolManager that returns fixed responses from the |
| +// Safe Browsing server for testing purpose. |
| +class TestV4GetHashProtocolManager : public V4GetHashProtocolManager { |
| + public: |
| + TestV4GetHashProtocolManager( |
| + net::URLRequestContextGetter* request_context_getter, |
| + const V4ProtocolConfig& config) |
| + : V4GetHashProtocolManager(request_context_getter, config) {} |
| + |
| + ~TestV4GetHashProtocolManager() override {} |
| + |
| + void GetFullHashesWithApis(const std::vector<SBPrefix>& prefixes, |
| + FullHashCallback callback) override { |
| + prefixes_ = prefixes; |
| + callback.Run(full_hashes_, base::TimeDelta::FromMinutes(0)); |
|
Nathan Parker
2016/04/07 21:33:38
You probably dont' need to call the callback, sinc
kcarattini
2016/04/11 03:34:31
I'd like to call it for cleanliness -- it deletes
|
| + } |
| + |
| + // Prepare the GetFullHash results for the next request. |
| + void AddGetFullHashResponse(const SBFullHashResult& full_hash_result) { |
| + full_hashes_.push_back(full_hash_result); |
| + } |
| + |
| + // Returns the prefixes that were sent in the last request. |
| + const std::vector<SBPrefix>& GetRequestPrefixes() { return prefixes_; } |
| + |
| + private: |
| + std::vector<SBPrefix> prefixes_; |
| + std::vector<SBFullHashResult> full_hashes_; |
| +}; |
| + |
| +// Factory that creates test protocol manager instances. |
| +class TestV4GetHashProtocolManagerFactory : |
| + public V4GetHashProtocolManagerFactory { |
| + public: |
| + TestV4GetHashProtocolManagerFactory() : pm_(NULL) {} |
| + ~TestV4GetHashProtocolManagerFactory() override {} |
| + |
| + V4GetHashProtocolManager* CreateProtocolManager( |
| + net::URLRequestContextGetter* request_context_getter, |
| + const V4ProtocolConfig& config) override { |
| + pm_ = new TestV4GetHashProtocolManager(request_context_getter, config); |
| + return pm_; |
| + } |
| + |
| + TestV4GetHashProtocolManager* GetProtocolManager() { return pm_; } |
|
Nathan Parker
2016/04/07 21:33:37
A thought: Could callers instead get it from the S
kcarattini
2016/04/11 03:34:31
Removed. Turns out I wasn't using it anyway.
|
| + |
| + private: |
| + // Owned by the SafeBrowsingDatabaseManager. |
| + TestV4GetHashProtocolManager* pm_; |
| +}; |
| + |
| +class TestClient : public SafeBrowsingDatabaseManager::Client { |
| + public: |
| + TestClient() {} |
| + ~TestClient() override {} |
| + |
| + void OnCheckApiBlacklistUrlResult(const GURL& url, |
| + const ThreatMetadata& metadata) override {} |
| + |
| + private: |
| + DISALLOW_COPY_AND_ASSIGN(TestClient); |
| +}; |
| + |
| +} // namespace |
| + |
| +class SafeBrowsingDatabaseManagerTest : public testing::Test { |
| + private: |
| + content::TestBrowserThreadBundle test_browser_thread_bundle_; |
| +}; |
| + |
| +TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlWrongScheme) { |
| + TestV4GetHashProtocolManagerFactory get_hash_pm_factory; |
|
Nathan Parker
2016/04/07 21:33:37
Could move the first few lines into the test class
kcarattini
2016/04/11 03:34:31
Done.
|
| + V4GetHashProtocolManager::RegisterFactory(&get_hash_pm_factory); |
| + |
| + scoped_refptr<SafeBrowsingDatabaseManager> db_manager_( |
| + new TestSafeBrowsingDatabaseManager()); |
| + db_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); |
| + |
| + TestClient client; |
| + const GURL url("file://example.txt"); |
| + EXPECT_TRUE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| + db_manager_->StopOnIOThread(false); |
| +} |
| + |
| +TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlPrefixes) { |
| + TestV4GetHashProtocolManagerFactory get_hash_pm_factory; |
| + V4GetHashProtocolManager::RegisterFactory(&get_hash_pm_factory); |
| + |
| + scoped_refptr<SafeBrowsingDatabaseManager> db_manager_( |
| + new TestSafeBrowsingDatabaseManager()); |
| + db_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); |
| + |
| + // Calculate hash prefixes |
| + TestClient client; |
| + const GURL url("https://www.example.com/more"); |
| + std::vector<SBPrefix> expected_prefixes = |
|
Nathan Parker
2016/04/07 21:33:38
Can you add a comment on how you generated these,
kcarattini
2016/04/11 03:34:31
Done.
|
| + {1237562338, 2871045197, 3553205461, 3766933875}; |
| + |
| + EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| + std::vector<SBPrefix> prefixes = static_cast<TestV4GetHashProtocolManager*>( |
| + db_manager_->v4_get_hash_protocol_manager_)->GetRequestPrefixes(); |
| + EXPECT_EQ(expected_prefixes.size(), prefixes.size()); |
| + for (unsigned int i = 0; i < prefixes.size(); ++i) { |
| + EXPECT_EQ(expected_prefixes[i], prefixes[i]); |
| + } |
| + base::RunLoop().RunUntilIdle(); |
| + db_manager_->StopOnIOThread(false); |
| +} |
| + |
| +} // namespace safe_browsing |