| 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" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 // Owned by the SafeBrowsingDatabaseManager. | 73 // Owned by the SafeBrowsingDatabaseManager. |
| 74 TestV4GetHashProtocolManager* pm_; | 74 TestV4GetHashProtocolManager* pm_; |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 class TestClient : public SafeBrowsingDatabaseManager::Client { | 77 class TestClient : public SafeBrowsingDatabaseManager::Client { |
| 78 public: | 78 public: |
| 79 TestClient() {} | 79 TestClient() {} |
| 80 ~TestClient() override {} | 80 ~TestClient() override {} |
| 81 | 81 |
| 82 void OnCheckApiBlacklistUrlResult(const GURL& url, | 82 void OnCheckApiBlacklistUrlResult(const GURL& url, |
| 83 const ThreatMetadata& metadata) override {} | 83 const ThreatMetadata& metadata) override { |
| 84 blocked_permissions_ = metadata.api_permissions; |
| 85 } |
| 86 |
| 87 const std::vector<std::string>& GetBlockedPermissions() { |
| 88 return blocked_permissions_; |
| 89 } |
| 84 | 90 |
| 85 private: | 91 private: |
| 92 std::vector<std::string> blocked_permissions_; |
| 86 DISALLOW_COPY_AND_ASSIGN(TestClient); | 93 DISALLOW_COPY_AND_ASSIGN(TestClient); |
| 87 }; | 94 }; |
| 88 | 95 |
| 89 } // namespace | 96 } // namespace |
| 90 | 97 |
| 91 class SafeBrowsingDatabaseManagerTest : public testing::Test { | 98 class SafeBrowsingDatabaseManagerTest : public testing::Test { |
| 92 protected: | 99 protected: |
| 93 void SetUp() override { | 100 void SetUp() override { |
| 94 TestV4GetHashProtocolManagerFactory get_hash_pm_factory; | 101 TestV4GetHashProtocolManagerFactory get_hash_pm_factory; |
| 95 V4GetHashProtocolManager::RegisterFactory(&get_hash_pm_factory); | 102 V4GetHashProtocolManager::RegisterFactory(&get_hash_pm_factory); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 124 | 131 |
| 125 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); | 132 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 126 std::vector<SBPrefix> prefixes = static_cast<TestV4GetHashProtocolManager*>( | 133 std::vector<SBPrefix> prefixes = static_cast<TestV4GetHashProtocolManager*>( |
| 127 db_manager_->v4_get_hash_protocol_manager_)->GetRequestPrefixes(); | 134 db_manager_->v4_get_hash_protocol_manager_)->GetRequestPrefixes(); |
| 128 EXPECT_EQ(expected_prefixes.size(), prefixes.size()); | 135 EXPECT_EQ(expected_prefixes.size(), prefixes.size()); |
| 129 for (unsigned int i = 0; i < prefixes.size(); ++i) { | 136 for (unsigned int i = 0; i < prefixes.size(); ++i) { |
| 130 EXPECT_EQ(expected_prefixes[i], prefixes[i]); | 137 EXPECT_EQ(expected_prefixes[i], prefixes[i]); |
| 131 } | 138 } |
| 132 } | 139 } |
| 133 | 140 |
| 141 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResults) { |
| 142 TestClient client; |
| 143 const GURL url("https://www.example.com/more"); |
| 144 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>( |
| 145 db_manager_->v4_get_hash_protocol_manager_); |
| 146 SBFullHashResult full_hash_result; |
| 147 full_hash_result.hash = SBFullHashForString("example.com/"); |
| 148 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION"); |
| 149 pm->AddGetFullHashResponse(full_hash_result); |
| 150 |
| 151 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 152 base::RunLoop().RunUntilIdle(); |
| 153 |
| 154 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); |
| 155 EXPECT_EQ(1ul, permissions.size()); |
| 156 EXPECT_EQ("GEOLOCATION", permissions[0]); |
| 157 } |
| 158 |
| 159 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResultsNoMatch) { |
| 160 TestClient client; |
| 161 const GURL url("https://www.example.com/more"); |
| 162 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>( |
| 163 db_manager_->v4_get_hash_protocol_manager_); |
| 164 SBFullHashResult full_hash_result; |
| 165 full_hash_result.hash = SBFullHashForString("wrongexample.com/"); |
| 166 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION"); |
| 167 pm->AddGetFullHashResponse(full_hash_result); |
| 168 |
| 169 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 170 base::RunLoop().RunUntilIdle(); |
| 171 |
| 172 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); |
| 173 EXPECT_EQ(0ul, permissions.size()); |
| 174 } |
| 175 |
| 176 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResultsMatches) { |
| 177 TestClient client; |
| 178 const GURL url("https://www.example.com/more"); |
| 179 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>( |
| 180 db_manager_->v4_get_hash_protocol_manager_); |
| 181 SBFullHashResult full_hash_result; |
| 182 full_hash_result.hash = SBFullHashForString("example.com/"); |
| 183 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION"); |
| 184 pm->AddGetFullHashResponse(full_hash_result); |
| 185 SBFullHashResult full_hash_result2; |
| 186 full_hash_result2.hash = SBFullHashForString("example.com/more"); |
| 187 full_hash_result2.metadata.api_permissions.push_back("NOTIFICATIONS"); |
| 188 pm->AddGetFullHashResponse(full_hash_result2); |
| 189 SBFullHashResult full_hash_result3; |
| 190 full_hash_result3.hash = SBFullHashForString("wrongexample.com/"); |
| 191 full_hash_result3.metadata.api_permissions.push_back("AUDIO_CAPTURE"); |
| 192 pm->AddGetFullHashResponse(full_hash_result3); |
| 193 |
| 194 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); |
| 195 base::RunLoop().RunUntilIdle(); |
| 196 |
| 197 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); |
| 198 EXPECT_EQ(2ul, permissions.size()); |
| 199 EXPECT_EQ("GEOLOCATION", permissions[0]); |
| 200 EXPECT_EQ("NOTIFICATIONS", permissions[1]); |
| 201 } |
| 202 |
| 134 } // namespace safe_browsing | 203 } // namespace safe_browsing |
| OLD | NEW |