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

Side by Side Diff: components/safe_browsing_db/database_manager_unittest.cc

Issue 1881353002: SafeBrowsing: Adds response handler for API blacklist check in Database Manager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@osb-impl-1
Patch Set: Review Comments Created 4 years, 8 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 unified diff | Download patch
« no previous file with comments | « components/safe_browsing_db/database_manager.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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
OLDNEW
« no previous file with comments | « components/safe_browsing_db/database_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698