| OLD | NEW |
| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" |
| 13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 14 #include "components/safe_browsing_db/database_manager.h" | 15 #include "components/safe_browsing_db/database_manager.h" |
| 15 #include "components/safe_browsing_db/test_database_manager.h" | 16 #include "components/safe_browsing_db/test_database_manager.h" |
| 16 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" | 17 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 21 | 22 |
| 22 using content::BrowserThread; | 23 using content::BrowserThread; |
| 23 | 24 |
| 24 namespace safe_browsing { | 25 namespace safe_browsing { |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 29 void InvokeFullHashCallback( |
| 30 V4GetHashProtocolManager::FullHashCallback callback, |
| 31 const std::vector<SBFullHashResult>& full_hashes) { |
| 32 callback.Run(full_hashes, base::TimeDelta::FromMinutes(0)); |
| 33 } |
| 34 |
| 28 // A TestV4GetHashProtocolManager that returns fixed responses from the | 35 // A TestV4GetHashProtocolManager that returns fixed responses from the |
| 29 // Safe Browsing server for testing purpose. | 36 // Safe Browsing server for testing purpose. |
| 30 class TestV4GetHashProtocolManager : public V4GetHashProtocolManager { | 37 class TestV4GetHashProtocolManager : public V4GetHashProtocolManager { |
| 31 public: | 38 public: |
| 32 TestV4GetHashProtocolManager( | 39 TestV4GetHashProtocolManager( |
| 33 net::URLRequestContextGetter* request_context_getter, | 40 net::URLRequestContextGetter* request_context_getter, |
| 34 const V4ProtocolConfig& config) | 41 const V4ProtocolConfig& config) |
| 35 : V4GetHashProtocolManager(request_context_getter, config) {} | 42 : V4GetHashProtocolManager(request_context_getter, config), |
| 43 delay_seconds_(0) {} |
| 36 | 44 |
| 37 ~TestV4GetHashProtocolManager() override {} | 45 ~TestV4GetHashProtocolManager() override {} |
| 38 | 46 |
| 39 void GetFullHashesWithApis(const std::vector<SBPrefix>& prefixes, | 47 void GetFullHashesWithApis(const std::vector<SBPrefix>& prefixes, |
| 40 FullHashCallback callback) override { | 48 FullHashCallback callback) override { |
| 41 prefixes_ = prefixes; | 49 prefixes_ = prefixes; |
| 42 callback.Run(full_hashes_, base::TimeDelta::FromMinutes(0)); | 50 base::MessageLoop::current()->PostDelayedTask( |
| 51 FROM_HERE, base::Bind(InvokeFullHashCallback, callback, full_hashes_), |
| 52 base::TimeDelta::FromSeconds(delay_seconds_)); |
| 53 } |
| 54 |
| 55 void SetDelaySeconds(int delay) { |
| 56 delay_seconds_ = delay; |
| 43 } | 57 } |
| 44 | 58 |
| 45 // Prepare the GetFullHash results for the next request. | 59 // Prepare the GetFullHash results for the next request. |
| 46 void AddGetFullHashResponse(const SBFullHashResult& full_hash_result) { | 60 void AddGetFullHashResponse(const SBFullHashResult& full_hash_result) { |
| 47 full_hashes_.push_back(full_hash_result); | 61 full_hashes_.push_back(full_hash_result); |
| 48 } | 62 } |
| 49 | 63 |
| 50 // Returns the prefixes that were sent in the last request. | 64 // Returns the prefixes that were sent in the last request. |
| 51 const std::vector<SBPrefix>& GetRequestPrefixes() { return prefixes_; } | 65 const std::vector<SBPrefix>& GetRequestPrefixes() { return prefixes_; } |
| 52 | 66 |
| 53 private: | 67 private: |
| 54 std::vector<SBPrefix> prefixes_; | 68 std::vector<SBPrefix> prefixes_; |
| 55 std::vector<SBFullHashResult> full_hashes_; | 69 std::vector<SBFullHashResult> full_hashes_; |
| 70 int delay_seconds_; |
| 56 }; | 71 }; |
| 57 | 72 |
| 58 // Factory that creates test protocol manager instances. | 73 // Factory that creates test protocol manager instances. |
| 59 class TestV4GetHashProtocolManagerFactory : | 74 class TestV4GetHashProtocolManagerFactory : |
| 60 public V4GetHashProtocolManagerFactory { | 75 public V4GetHashProtocolManagerFactory { |
| 61 public: | 76 public: |
| 62 TestV4GetHashProtocolManagerFactory() : pm_(NULL) {} | 77 TestV4GetHashProtocolManagerFactory() : pm_(NULL) {} |
| 63 ~TestV4GetHashProtocolManagerFactory() override {} | 78 ~TestV4GetHashProtocolManagerFactory() override {} |
| 64 | 79 |
| 65 V4GetHashProtocolManager* CreateProtocolManager( | 80 V4GetHashProtocolManager* CreateProtocolManager( |
| 66 net::URLRequestContextGetter* request_context_getter, | 81 net::URLRequestContextGetter* request_context_getter, |
| 67 const V4ProtocolConfig& config) override { | 82 const V4ProtocolConfig& config) override { |
| 68 pm_ = new TestV4GetHashProtocolManager(request_context_getter, config); | 83 pm_ = new TestV4GetHashProtocolManager(request_context_getter, config); |
| 69 return pm_; | 84 return pm_; |
| 70 } | 85 } |
| 71 | 86 |
| 72 private: | 87 private: |
| 73 // Owned by the SafeBrowsingDatabaseManager. | 88 // Owned by the SafeBrowsingDatabaseManager. |
| 74 TestV4GetHashProtocolManager* pm_; | 89 TestV4GetHashProtocolManager* pm_; |
| 75 }; | 90 }; |
| 76 | 91 |
| 77 class TestClient : public SafeBrowsingDatabaseManager::Client { | 92 class TestClient : public SafeBrowsingDatabaseManager::Client { |
| 78 public: | 93 public: |
| 79 TestClient() {} | 94 TestClient() : callback_invoked_(false) {} |
| 80 ~TestClient() override {} | 95 ~TestClient() override {} |
| 81 | 96 |
| 82 void OnCheckApiBlacklistUrlResult(const GURL& url, | 97 void OnCheckApiBlacklistUrlResult(const GURL& url, |
| 83 const ThreatMetadata& metadata) override { | 98 const ThreatMetadata& metadata) override { |
| 84 blocked_permissions_ = metadata.api_permissions; | 99 blocked_permissions_ = metadata.api_permissions; |
| 100 callback_invoked_ = true; |
| 85 } | 101 } |
| 86 | 102 |
| 87 const std::vector<std::string>& GetBlockedPermissions() { | 103 const std::vector<std::string>& GetBlockedPermissions() { |
| 88 return blocked_permissions_; | 104 return blocked_permissions_; |
| 89 } | 105 } |
| 90 | 106 |
| 107 bool callback_invoked() {return callback_invoked_;} |
| 108 |
| 91 private: | 109 private: |
| 92 std::vector<std::string> blocked_permissions_; | 110 std::vector<std::string> blocked_permissions_; |
| 111 bool callback_invoked_; |
| 93 DISALLOW_COPY_AND_ASSIGN(TestClient); | 112 DISALLOW_COPY_AND_ASSIGN(TestClient); |
| 94 }; | 113 }; |
| 95 | 114 |
| 96 } // namespace | 115 } // namespace |
| 97 | 116 |
| 98 class SafeBrowsingDatabaseManagerTest : public testing::Test { | 117 class SafeBrowsingDatabaseManagerTest : public testing::Test { |
| 99 protected: | 118 protected: |
| 100 void SetUp() override { | 119 void SetUp() override { |
| 101 TestV4GetHashProtocolManagerFactory get_hash_pm_factory; | 120 TestV4GetHashProtocolManagerFactory get_hash_pm_factory; |
| 102 V4GetHashProtocolManager::RegisterFactory(&get_hash_pm_factory); | 121 V4GetHashProtocolManager::RegisterFactory(&get_hash_pm_factory); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 123 } | 142 } |
| 124 | 143 |
| 125 TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlPrefixes) { | 144 TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlPrefixes) { |
| 126 TestClient client; | 145 TestClient client; |
| 127 const GURL url("https://www.example.com/more"); | 146 const GURL url("https://www.example.com/more"); |
| 128 // Generated from the sorted output of UrlToFullHashes in util.h. | 147 // Generated from the sorted output of UrlToFullHashes in util.h. |
| 129 std::vector<SBPrefix> expected_prefixes = | 148 std::vector<SBPrefix> expected_prefixes = |
| 130 {1237562338, 2871045197, 3553205461, 3766933875}; | 149 {1237562338, 2871045197, 3553205461, 3766933875}; |
| 131 | 150 |
| 132 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); | 151 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 152 base::RunLoop().RunUntilIdle(); |
| 133 std::vector<SBPrefix> prefixes = static_cast<TestV4GetHashProtocolManager*>( | 153 std::vector<SBPrefix> prefixes = static_cast<TestV4GetHashProtocolManager*>( |
| 134 db_manager_->v4_get_hash_protocol_manager_)->GetRequestPrefixes(); | 154 db_manager_->v4_get_hash_protocol_manager_)->GetRequestPrefixes(); |
| 135 EXPECT_EQ(expected_prefixes.size(), prefixes.size()); | 155 EXPECT_EQ(expected_prefixes.size(), prefixes.size()); |
| 136 for (unsigned int i = 0; i < prefixes.size(); ++i) { | 156 for (unsigned int i = 0; i < prefixes.size(); ++i) { |
| 137 EXPECT_EQ(expected_prefixes[i], prefixes[i]); | 157 EXPECT_EQ(expected_prefixes[i], prefixes[i]); |
| 138 } | 158 } |
| 139 } | 159 } |
| 140 | 160 |
| 141 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResults) { | 161 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResults) { |
| 142 TestClient client; | 162 TestClient client; |
| 143 const GURL url("https://www.example.com/more"); | 163 const GURL url("https://www.example.com/more"); |
| 144 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>( | 164 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>( |
| 145 db_manager_->v4_get_hash_protocol_manager_); | 165 db_manager_->v4_get_hash_protocol_manager_); |
| 146 SBFullHashResult full_hash_result; | 166 SBFullHashResult full_hash_result; |
| 147 full_hash_result.hash = SBFullHashForString("example.com/"); | 167 full_hash_result.hash = SBFullHashForString("example.com/"); |
| 148 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION"); | 168 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION"); |
| 149 pm->AddGetFullHashResponse(full_hash_result); | 169 pm->AddGetFullHashResponse(full_hash_result); |
| 150 | 170 |
| 151 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); | 171 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 152 base::RunLoop().RunUntilIdle(); | 172 base::RunLoop().RunUntilIdle(); |
| 153 | 173 |
| 174 EXPECT_TRUE(client.callback_invoked()); |
| 154 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); | 175 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); |
| 155 EXPECT_EQ(1ul, permissions.size()); | 176 EXPECT_EQ(1ul, permissions.size()); |
| 156 EXPECT_EQ("GEOLOCATION", permissions[0]); | 177 EXPECT_EQ("GEOLOCATION", permissions[0]); |
| 157 } | 178 } |
| 158 | 179 |
| 159 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResultsNoMatch) { | 180 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResultsNoMatch) { |
| 160 TestClient client; | 181 TestClient client; |
| 161 const GURL url("https://www.example.com/more"); | 182 const GURL url("https://www.example.com/more"); |
| 162 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>( | 183 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>( |
| 163 db_manager_->v4_get_hash_protocol_manager_); | 184 db_manager_->v4_get_hash_protocol_manager_); |
| 164 SBFullHashResult full_hash_result; | 185 SBFullHashResult full_hash_result; |
| 165 full_hash_result.hash = SBFullHashForString("wrongexample.com/"); | 186 full_hash_result.hash = SBFullHashForString("wrongexample.com/"); |
| 166 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION"); | 187 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION"); |
| 167 pm->AddGetFullHashResponse(full_hash_result); | 188 pm->AddGetFullHashResponse(full_hash_result); |
| 168 | 189 |
| 169 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); | 190 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 170 base::RunLoop().RunUntilIdle(); | 191 base::RunLoop().RunUntilIdle(); |
| 171 | 192 |
| 193 EXPECT_TRUE(client.callback_invoked()); |
| 172 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); | 194 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); |
| 173 EXPECT_EQ(0ul, permissions.size()); | 195 EXPECT_EQ(0ul, permissions.size()); |
| 174 } | 196 } |
| 175 | 197 |
| 176 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResultsMatches) { | 198 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResultsMatches) { |
| 177 TestClient client; | 199 TestClient client; |
| 178 const GURL url("https://www.example.com/more"); | 200 const GURL url("https://www.example.com/more"); |
| 179 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>( | 201 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>( |
| 180 db_manager_->v4_get_hash_protocol_manager_); | 202 db_manager_->v4_get_hash_protocol_manager_); |
| 181 SBFullHashResult full_hash_result; | 203 SBFullHashResult full_hash_result; |
| 182 full_hash_result.hash = SBFullHashForString("example.com/"); | 204 full_hash_result.hash = SBFullHashForString("example.com/"); |
| 183 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION"); | 205 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION"); |
| 184 pm->AddGetFullHashResponse(full_hash_result); | 206 pm->AddGetFullHashResponse(full_hash_result); |
| 185 SBFullHashResult full_hash_result2; | 207 SBFullHashResult full_hash_result2; |
| 186 full_hash_result2.hash = SBFullHashForString("example.com/more"); | 208 full_hash_result2.hash = SBFullHashForString("example.com/more"); |
| 187 full_hash_result2.metadata.api_permissions.push_back("NOTIFICATIONS"); | 209 full_hash_result2.metadata.api_permissions.push_back("NOTIFICATIONS"); |
| 188 pm->AddGetFullHashResponse(full_hash_result2); | 210 pm->AddGetFullHashResponse(full_hash_result2); |
| 189 SBFullHashResult full_hash_result3; | 211 SBFullHashResult full_hash_result3; |
| 190 full_hash_result3.hash = SBFullHashForString("wrongexample.com/"); | 212 full_hash_result3.hash = SBFullHashForString("wrongexample.com/"); |
| 191 full_hash_result3.metadata.api_permissions.push_back("AUDIO_CAPTURE"); | 213 full_hash_result3.metadata.api_permissions.push_back("AUDIO_CAPTURE"); |
| 192 pm->AddGetFullHashResponse(full_hash_result3); | 214 pm->AddGetFullHashResponse(full_hash_result3); |
| 193 | 215 |
| 194 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); | 216 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 195 base::RunLoop().RunUntilIdle(); | 217 base::RunLoop().RunUntilIdle(); |
| 196 | 218 |
| 219 EXPECT_TRUE(client.callback_invoked()); |
| 197 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); | 220 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); |
| 198 EXPECT_EQ(2ul, permissions.size()); | 221 EXPECT_EQ(2ul, permissions.size()); |
| 199 EXPECT_EQ("GEOLOCATION", permissions[0]); | 222 EXPECT_EQ("GEOLOCATION", permissions[0]); |
| 200 EXPECT_EQ("NOTIFICATIONS", permissions[1]); | 223 EXPECT_EQ("NOTIFICATIONS", permissions[1]); |
| 201 } | 224 } |
| 202 | 225 |
| 226 TEST_F(SafeBrowsingDatabaseManagerTest, CancelApiCheck) { |
| 227 TestClient client; |
| 228 const GURL url("https://www.example.com/more"); |
| 229 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>( |
| 230 db_manager_->v4_get_hash_protocol_manager_); |
| 231 SBFullHashResult full_hash_result; |
| 232 full_hash_result.hash = SBFullHashForString("example.com/"); |
| 233 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION"); |
| 234 pm->AddGetFullHashResponse(full_hash_result); |
| 235 pm->SetDelaySeconds(100); |
| 236 |
| 237 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 238 EXPECT_TRUE(db_manager_->CancelApiCheck(&client)); |
| 239 base::RunLoop().RunUntilIdle(); |
| 240 |
| 241 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); |
| 242 EXPECT_EQ(0ul, permissions.size()); |
| 243 EXPECT_FALSE(client.callback_invoked()); |
| 244 } |
| 245 |
| 203 } // namespace safe_browsing | 246 } // namespace safe_browsing |
| OLD | NEW |