| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <stddef.h> |
| 6 |
| 7 #include <string> |
| 8 #include <vector> |
| 9 |
| 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "components/safe_browsing_db/database_manager.h" |
| 15 #include "components/safe_browsing_db/test_database_manager.h" |
| 16 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" |
| 21 |
| 22 using content::BrowserThread; |
| 23 |
| 24 namespace safe_browsing { |
| 25 |
| 26 namespace { |
| 27 |
| 28 // A TestV4GetHashProtocolManager that returns fixed responses from the |
| 29 // Safe Browsing server for testing purpose. |
| 30 class TestV4GetHashProtocolManager : public V4GetHashProtocolManager { |
| 31 public: |
| 32 TestV4GetHashProtocolManager( |
| 33 net::URLRequestContextGetter* request_context_getter, |
| 34 const V4ProtocolConfig& config) |
| 35 : V4GetHashProtocolManager(request_context_getter, config) {} |
| 36 |
| 37 ~TestV4GetHashProtocolManager() override {} |
| 38 |
| 39 void GetFullHashesWithApis(const std::vector<SBPrefix>& prefixes, |
| 40 FullHashCallback callback) override { |
| 41 prefixes_ = prefixes; |
| 42 callback.Run(full_hashes_, base::TimeDelta::FromMinutes(0)); |
| 43 } |
| 44 |
| 45 // Prepare the GetFullHash results for the next request. |
| 46 void AddGetFullHashResponse(const SBFullHashResult& full_hash_result) { |
| 47 full_hashes_.push_back(full_hash_result); |
| 48 } |
| 49 |
| 50 // Returns the prefixes that were sent in the last request. |
| 51 const std::vector<SBPrefix>& GetRequestPrefixes() { return prefixes_; } |
| 52 |
| 53 private: |
| 54 std::vector<SBPrefix> prefixes_; |
| 55 std::vector<SBFullHashResult> full_hashes_; |
| 56 }; |
| 57 |
| 58 // Factory that creates test protocol manager instances. |
| 59 class TestV4GetHashProtocolManagerFactory : |
| 60 public V4GetHashProtocolManagerFactory { |
| 61 public: |
| 62 TestV4GetHashProtocolManagerFactory() : pm_(NULL) {} |
| 63 ~TestV4GetHashProtocolManagerFactory() override {} |
| 64 |
| 65 V4GetHashProtocolManager* CreateProtocolManager( |
| 66 net::URLRequestContextGetter* request_context_getter, |
| 67 const V4ProtocolConfig& config) override { |
| 68 pm_ = new TestV4GetHashProtocolManager(request_context_getter, config); |
| 69 return pm_; |
| 70 } |
| 71 |
| 72 private: |
| 73 // Owned by the SafeBrowsingDatabaseManager. |
| 74 TestV4GetHashProtocolManager* pm_; |
| 75 }; |
| 76 |
| 77 class TestClient : public SafeBrowsingDatabaseManager::Client { |
| 78 public: |
| 79 TestClient() {} |
| 80 ~TestClient() override {} |
| 81 |
| 82 void OnCheckApiBlacklistUrlResult(const GURL& url, |
| 83 const ThreatMetadata& metadata) override {} |
| 84 |
| 85 private: |
| 86 DISALLOW_COPY_AND_ASSIGN(TestClient); |
| 87 }; |
| 88 |
| 89 } // namespace |
| 90 |
| 91 class SafeBrowsingDatabaseManagerTest : public testing::Test { |
| 92 protected: |
| 93 void SetUp() override { |
| 94 TestV4GetHashProtocolManagerFactory get_hash_pm_factory; |
| 95 V4GetHashProtocolManager::RegisterFactory(&get_hash_pm_factory); |
| 96 |
| 97 db_manager_ = new TestSafeBrowsingDatabaseManager(); |
| 98 db_manager_->StartOnIOThread(NULL, V4ProtocolConfig()); |
| 99 } |
| 100 |
| 101 void TearDown() override { |
| 102 base::RunLoop().RunUntilIdle(); |
| 103 db_manager_->StopOnIOThread(false); |
| 104 } |
| 105 |
| 106 scoped_refptr<SafeBrowsingDatabaseManager> db_manager_; |
| 107 |
| 108 private: |
| 109 content::TestBrowserThreadBundle test_browser_thread_bundle_; |
| 110 }; |
| 111 |
| 112 TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlWrongScheme) { |
| 113 TestClient client; |
| 114 const GURL url("file://example.txt"); |
| 115 EXPECT_TRUE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 116 } |
| 117 |
| 118 TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlPrefixes) { |
| 119 TestClient client; |
| 120 const GURL url("https://www.example.com/more"); |
| 121 // Generated from the sorted output of UrlToFullHashes in util.h. |
| 122 std::vector<SBPrefix> expected_prefixes = |
| 123 {1237562338, 2871045197, 3553205461, 3766933875}; |
| 124 |
| 125 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 126 std::vector<SBPrefix> prefixes = static_cast<TestV4GetHashProtocolManager*>( |
| 127 db_manager_->v4_get_hash_protocol_manager_)->GetRequestPrefixes(); |
| 128 EXPECT_EQ(expected_prefixes.size(), prefixes.size()); |
| 129 for (unsigned int i = 0; i < prefixes.size(); ++i) { |
| 130 EXPECT_EQ(expected_prefixes[i], prefixes[i]); |
| 131 } |
| 132 } |
| 133 |
| 134 } // namespace safe_browsing |
| OLD | NEW |