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 "components/safe_browsing_db/v4_get_hash_protocol_manager.h" | 5 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" |
12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
13 #include "base/test/simple_test_clock.h" | 14 #include "base/test/simple_test_clock.h" |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 #include "components/safe_browsing_db/safebrowsing.pb.h" | 16 #include "components/safe_browsing_db/safebrowsing.pb.h" |
16 #include "components/safe_browsing_db/testing_util.h" | 17 #include "components/safe_browsing_db/testing_util.h" |
17 #include "components/safe_browsing_db/util.h" | 18 #include "components/safe_browsing_db/util.h" |
18 #include "net/base/escape.h" | 19 #include "net/base/escape.h" |
19 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
20 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
21 #include "net/url_request/test_url_fetcher_factory.h" | 22 #include "net/url_request/test_url_fetcher_factory.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 using base::Time; | 25 using base::Time; |
25 using base::TimeDelta; | 26 using base::TimeDelta; |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 const char kClient[] = "unittest"; | 30 const char kClient[] = "unittest"; |
30 const char kAppVer[] = "1.0"; | 31 const char kAppVer[] = "1.0"; |
31 const char kKeyParam[] = "test_key_param"; | 32 const char kKeyParam[] = "test_key_param"; |
32 | 33 |
33 } // namespace | 34 } // namespace |
34 | 35 |
35 namespace safe_browsing { | 36 namespace safe_browsing { |
36 | 37 |
37 class SafeBrowsingV4GetHashProtocolManagerTest : public testing::Test { | 38 class V4GetHashProtocolManagerTest : public testing::Test { |
38 protected: | 39 protected: |
39 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager() { | 40 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager() { |
40 V4ProtocolConfig config; | 41 V4ProtocolConfig config; |
41 config.client_name = kClient; | 42 config.client_name = kClient; |
42 config.version = kAppVer; | 43 config.version = kAppVer; |
43 config.key_param = kKeyParam; | 44 config.key_param = kKeyParam; |
44 return std::unique_ptr<V4GetHashProtocolManager>( | 45 return std::unique_ptr<V4GetHashProtocolManager>( |
45 V4GetHashProtocolManager::Create(NULL, config)); | 46 V4GetHashProtocolManager::Create(NULL, config)); |
46 } | 47 } |
47 | 48 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 EXPECT_EQ(expected_cache_expire, cache_expire); | 83 EXPECT_EQ(expected_cache_expire, cache_expire); |
83 ASSERT_EQ(expected_full_hashes.size(), full_hashes.size()); | 84 ASSERT_EQ(expected_full_hashes.size(), full_hashes.size()); |
84 | 85 |
85 for (unsigned int i = 0; i < expected_full_hashes.size(); ++i) { | 86 for (unsigned int i = 0; i < expected_full_hashes.size(); ++i) { |
86 const SBFullHashResult& expected = expected_full_hashes[i]; | 87 const SBFullHashResult& expected = expected_full_hashes[i]; |
87 const SBFullHashResult& actual = full_hashes[i]; | 88 const SBFullHashResult& actual = full_hashes[i]; |
88 EXPECT_TRUE(SBFullHashEqual(expected.hash, actual.hash)); | 89 EXPECT_TRUE(SBFullHashEqual(expected.hash, actual.hash)); |
89 EXPECT_EQ(expected.metadata, actual.metadata); | 90 EXPECT_EQ(expected.metadata, actual.metadata); |
90 EXPECT_EQ(expected.cache_expire_after, actual.cache_expire_after); | 91 EXPECT_EQ(expected.cache_expire_after, actual.cache_expire_after); |
91 } | 92 } |
| 93 |
92 } | 94 } |
93 | 95 |
94 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, | 96 TEST_F(V4GetHashProtocolManagerTest, |
95 TestGetHashErrorHandlingNetwork) { | 97 TestGetHashErrorHandlingNetwork) { |
96 net::TestURLFetcherFactory factory; | 98 net::TestURLFetcherFactory factory; |
97 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 99 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
98 | 100 |
99 std::vector<SBPrefix> prefixes; | 101 std::vector<SBPrefix> prefixes; |
100 std::vector<SBFullHashResult> expected_full_hashes; | 102 std::vector<SBFullHashResult> expected_full_hashes; |
101 base::Time expected_cache_expire; | 103 base::Time expected_cache_expire; |
102 | 104 |
103 pm->GetFullHashesWithApis( | 105 pm->GetFullHashesWithApis( |
104 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes, | 106 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes, |
105 expected_cache_expire)); | 107 expected_cache_expire)); |
106 | 108 |
107 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 109 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
108 DCHECK(fetcher); | 110 DCHECK(fetcher); |
109 // Failed request status should result in error. | 111 // Failed request status should result in error. |
110 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 112 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
111 net::ERR_CONNECTION_RESET)); | 113 net::ERR_CONNECTION_RESET)); |
112 fetcher->set_response_code(200); | 114 fetcher->set_response_code(200); |
113 fetcher->SetResponseString(GetStockV4HashResponse()); | 115 fetcher->SetResponseString(GetStockV4HashResponse()); |
114 fetcher->delegate()->OnURLFetchComplete(fetcher); | 116 fetcher->delegate()->OnURLFetchComplete(fetcher); |
115 | 117 |
116 // Should have recorded one error, but back off multiplier is unchanged. | 118 // Should have recorded one error, but back off multiplier is unchanged. |
117 EXPECT_EQ(1ul, pm->gethash_error_count_); | 119 EXPECT_EQ(1ul, pm->gethash_error_count_); |
118 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); | 120 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); |
119 } | 121 } |
120 | 122 |
121 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, | 123 TEST_F(V4GetHashProtocolManagerTest, |
122 TestGetHashErrorHandlingResponseCode) { | 124 TestGetHashErrorHandlingResponseCode) { |
123 net::TestURLFetcherFactory factory; | 125 net::TestURLFetcherFactory factory; |
124 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 126 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
125 | 127 |
126 std::vector<SBPrefix> prefixes; | 128 std::vector<SBPrefix> prefixes; |
127 std::vector<SBFullHashResult> expected_full_hashes; | 129 std::vector<SBFullHashResult> expected_full_hashes; |
128 base::Time expected_cache_expire; | 130 base::Time expected_cache_expire; |
129 | 131 |
130 pm->GetFullHashesWithApis( | 132 pm->GetFullHashesWithApis( |
131 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes, | 133 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes, |
132 expected_cache_expire)); | 134 expected_cache_expire)); |
133 | 135 |
134 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 136 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
135 DCHECK(fetcher); | 137 DCHECK(fetcher); |
136 fetcher->set_status(net::URLRequestStatus()); | 138 fetcher->set_status(net::URLRequestStatus()); |
137 // Response code of anything other than 200 should result in error. | 139 // Response code of anything other than 200 should result in error. |
138 fetcher->set_response_code(204); | 140 fetcher->set_response_code(204); |
139 fetcher->SetResponseString(GetStockV4HashResponse()); | 141 fetcher->SetResponseString(GetStockV4HashResponse()); |
140 fetcher->delegate()->OnURLFetchComplete(fetcher); | 142 fetcher->delegate()->OnURLFetchComplete(fetcher); |
141 | 143 |
142 // Should have recorded one error, but back off multiplier is unchanged. | 144 // Should have recorded one error, but back off multiplier is unchanged. |
143 EXPECT_EQ(1ul, pm->gethash_error_count_); | 145 EXPECT_EQ(1ul, pm->gethash_error_count_); |
144 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); | 146 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); |
145 } | 147 } |
146 | 148 |
147 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashErrorHandlingOK) { | 149 TEST_F(V4GetHashProtocolManagerTest, TestGetHashErrorHandlingOK) { |
148 net::TestURLFetcherFactory factory; | 150 net::TestURLFetcherFactory factory; |
149 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 151 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
150 | 152 |
151 base::Time now = base::Time::UnixEpoch(); | 153 base::Time now = base::Time::UnixEpoch(); |
152 SetTestClock(now, pm.get()); | 154 SetTestClock(now, pm.get()); |
153 | 155 |
154 std::vector<SBPrefix> prefixes; | 156 std::vector<SBPrefix> prefixes = {2877448190}; |
155 std::vector<SBFullHashResult> expected_full_hashes; | 157 std::vector<SBFullHashResult> expected_full_hashes; |
156 SBFullHashResult hash_result; | 158 SBFullHashResult hash_result; |
157 hash_result.hash = SBFullHashForString("Everything's shiny, Cap'n."); | 159 hash_result.hash = SBFullHashForString("Everything's shiny, Cap'n."); |
158 hash_result.metadata.api_permissions.insert("NOTIFICATIONS"); | 160 hash_result.metadata.api_permissions.insert("NOTIFICATIONS"); |
159 hash_result.cache_expire_after = now + base::TimeDelta::FromSeconds(300); | 161 hash_result.cache_expire_after = now + base::TimeDelta::FromSeconds(300); |
160 expected_full_hashes.push_back(hash_result); | 162 expected_full_hashes.push_back(hash_result); |
161 base::Time expected_cache_expire = now + base::TimeDelta::FromSeconds(600); | 163 base::Time expected_cache_expire = now + base::TimeDelta::FromSeconds(600); |
162 | 164 |
163 pm->GetFullHashesWithApis( | 165 pm->GetFullHashesWithApis( |
164 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes, | 166 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes, |
165 expected_cache_expire)); | 167 expected_cache_expire)); |
166 | 168 |
167 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 169 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
168 DCHECK(fetcher); | 170 DCHECK(fetcher); |
169 fetcher->set_status(net::URLRequestStatus()); | 171 fetcher->set_status(net::URLRequestStatus()); |
170 fetcher->set_response_code(200); | 172 fetcher->set_response_code(200); |
171 fetcher->SetResponseString(GetStockV4HashResponse()); | 173 fetcher->SetResponseString(GetStockV4HashResponse()); |
172 fetcher->delegate()->OnURLFetchComplete(fetcher); | 174 fetcher->delegate()->OnURLFetchComplete(fetcher); |
173 | 175 |
174 // No error, back off multiplier is unchanged. | 176 // No error, back off multiplier is unchanged. |
175 EXPECT_EQ(0ul, pm->gethash_error_count_); | 177 EXPECT_EQ(0ul, pm->gethash_error_count_); |
176 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); | 178 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); |
| 179 |
| 180 // Verify the state of the cache. |
| 181 const V4GetHashProtocolManager::PrefixToFullHashResultsMap& cache = |
| 182 pm->v4_full_hash_cache()->at(SB_THREAT_TYPE_API_ABUSE); |
| 183 // Check the cache. |
| 184 EXPECT_EQ(1u, cache.size()); |
| 185 EXPECT_EQ(1u, cache.count(prefixes[0])); |
| 186 const SBCachedFullHashResult& cached_result = cache.at(prefixes[0]); |
| 187 EXPECT_EQ(1u, cached_result.full_hashes.size()); |
| 188 EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("Everything's shiny, Cap'n."),
cached_result.full_hashes[0].hash)); |
177 } | 189 } |
178 | 190 |
179 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashRequest) { | 191 TEST_F(V4GetHashProtocolManagerTest, TestGetHashRequest) { |
180 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 192 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
181 | 193 |
182 FindFullHashesRequest req; | 194 FindFullHashesRequest req; |
183 ThreatInfo* info = req.mutable_threat_info(); | 195 ThreatInfo* info = req.mutable_threat_info(); |
184 info->add_threat_types(API_ABUSE); | 196 info->add_threat_types(API_ABUSE); |
185 info->add_platform_types(CHROME_PLATFORM); | 197 info->add_platform_types(CHROME_PLATFORM); |
186 info->add_threat_entry_types(URL); | 198 info->add_threat_entry_types(URL); |
187 | 199 |
188 SBPrefix one = 1u; | 200 SBPrefix one = 1u; |
189 SBPrefix two = 2u; | 201 SBPrefix two = 2u; |
(...skipping 14 matching lines...) Expand all Loading... |
204 | 216 |
205 std::vector<PlatformType> platform; | 217 std::vector<PlatformType> platform; |
206 platform.push_back(CHROME_PLATFORM); | 218 platform.push_back(CHROME_PLATFORM); |
207 std::vector<SBPrefix> prefixes; | 219 std::vector<SBPrefix> prefixes; |
208 prefixes.push_back(one); | 220 prefixes.push_back(one); |
209 prefixes.push_back(two); | 221 prefixes.push_back(two); |
210 prefixes.push_back(three); | 222 prefixes.push_back(three); |
211 EXPECT_EQ(req_base64, pm->GetHashRequest(prefixes, platform, API_ABUSE)); | 223 EXPECT_EQ(req_base64, pm->GetHashRequest(prefixes, platform, API_ABUSE)); |
212 } | 224 } |
213 | 225 |
214 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestParseHashResponse) { | 226 TEST_F(V4GetHashProtocolManagerTest, TestParseHashResponse) { |
215 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 227 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
216 | 228 |
217 base::Time now = base::Time::UnixEpoch(); | 229 base::Time now = base::Time::UnixEpoch(); |
218 SetTestClock(now, pm.get()); | 230 SetTestClock(now, pm.get()); |
219 | 231 |
220 FindFullHashesResponse res; | 232 FindFullHashesResponse res; |
221 res.mutable_negative_cache_duration()->set_seconds(600); | 233 res.mutable_negative_cache_duration()->set_seconds(600); |
222 res.mutable_minimum_wait_duration()->set_seconds(400); | 234 res.mutable_minimum_wait_duration()->set_seconds(400); |
223 ThreatMatch* m = res.add_matches(); | 235 ThreatMatch* m = res.add_matches(); |
224 m->set_threat_type(API_ABUSE); | 236 m->set_threat_type(API_ABUSE); |
(...skipping 21 matching lines...) Expand all Loading... |
246 full_hashes[0].hash)); | 258 full_hashes[0].hash)); |
247 EXPECT_EQ(1ul, full_hashes[0].metadata.api_permissions.size()); | 259 EXPECT_EQ(1ul, full_hashes[0].metadata.api_permissions.size()); |
248 EXPECT_EQ(1ul, | 260 EXPECT_EQ(1ul, |
249 full_hashes[0].metadata.api_permissions.count("NOTIFICATIONS")); | 261 full_hashes[0].metadata.api_permissions.count("NOTIFICATIONS")); |
250 EXPECT_EQ(now + | 262 EXPECT_EQ(now + |
251 base::TimeDelta::FromSeconds(300), full_hashes[0].cache_expire_after); | 263 base::TimeDelta::FromSeconds(300), full_hashes[0].cache_expire_after); |
252 EXPECT_EQ(now + base::TimeDelta::FromSeconds(400), pm->next_gethash_time_); | 264 EXPECT_EQ(now + base::TimeDelta::FromSeconds(400), pm->next_gethash_time_); |
253 } | 265 } |
254 | 266 |
255 // Adds an entry with an ignored ThreatEntryType. | 267 // Adds an entry with an ignored ThreatEntryType. |
256 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, | 268 TEST_F(V4GetHashProtocolManagerTest, |
257 TestParseHashResponseWrongThreatEntryType) { | 269 TestParseHashResponseWrongThreatEntryType) { |
258 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 270 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
259 | 271 |
260 base::Time now = base::Time::UnixEpoch(); | 272 base::Time now = base::Time::UnixEpoch(); |
261 SetTestClock(now, pm.get()); | 273 SetTestClock(now, pm.get()); |
262 | 274 |
263 FindFullHashesResponse res; | 275 FindFullHashesResponse res; |
264 res.mutable_negative_cache_duration()->set_seconds(600); | 276 res.mutable_negative_cache_duration()->set_seconds(600); |
265 res.add_matches()->set_threat_entry_type(EXECUTABLE); | 277 res.add_matches()->set_threat_entry_type(EXECUTABLE); |
266 | 278 |
267 // Serialize. | 279 // Serialize. |
268 std::string res_data; | 280 std::string res_data; |
269 res.SerializeToString(&res_data); | 281 res.SerializeToString(&res_data); |
270 | 282 |
271 std::vector<SBFullHashResult> full_hashes; | 283 std::vector<SBFullHashResult> full_hashes; |
272 base::Time cache_expire; | 284 base::Time cache_expire; |
273 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_expire)); | 285 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_expire)); |
274 | 286 |
275 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); | 287 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); |
276 // There should be no hash results. | 288 // There should be no hash results. |
277 EXPECT_EQ(0ul, full_hashes.size()); | 289 EXPECT_EQ(0ul, full_hashes.size()); |
278 } | 290 } |
279 | 291 |
280 // Adds entries with a ThreatPatternType metadata. | 292 // Adds entries with a ThreatPatternType metadata. |
281 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, | 293 TEST_F(V4GetHashProtocolManagerTest, |
282 TestParseHashThreatPatternType) { | 294 TestParseHashThreatPatternType) { |
283 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 295 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
284 | 296 |
285 base::Time now = base::Time::UnixEpoch(); | 297 base::Time now = base::Time::UnixEpoch(); |
286 SetTestClock(now, pm.get()); | 298 SetTestClock(now, pm.get()); |
287 | 299 |
288 // Test social engineering pattern type. | 300 // Test social engineering pattern type. |
289 FindFullHashesResponse se_res; | 301 FindFullHashesResponse se_res; |
290 se_res.mutable_negative_cache_duration()->set_seconds(600); | 302 se_res.mutable_negative_cache_duration()->set_seconds(600); |
291 ThreatMatch* se = se_res.add_matches(); | 303 ThreatMatch* se = se_res.add_matches(); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 | 362 |
351 std::string invalid_data; | 363 std::string invalid_data; |
352 invalid_res.SerializeToString(&invalid_data); | 364 invalid_res.SerializeToString(&invalid_data); |
353 full_hashes.clear(); | 365 full_hashes.clear(); |
354 EXPECT_FALSE( | 366 EXPECT_FALSE( |
355 pm->ParseHashResponse(invalid_data, &full_hashes, &cache_expire)); | 367 pm->ParseHashResponse(invalid_data, &full_hashes, &cache_expire)); |
356 EXPECT_EQ(0ul, full_hashes.size()); | 368 EXPECT_EQ(0ul, full_hashes.size()); |
357 } | 369 } |
358 | 370 |
359 // Adds metadata with a key value that is not "permission". | 371 // Adds metadata with a key value that is not "permission". |
360 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, | 372 TEST_F(V4GetHashProtocolManagerTest, |
361 TestParseHashResponseNonPermissionMetadata) { | 373 TestParseHashResponseNonPermissionMetadata) { |
362 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 374 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
363 | 375 |
364 base::Time now = base::Time::UnixEpoch(); | 376 base::Time now = base::Time::UnixEpoch(); |
365 SetTestClock(now, pm.get()); | 377 SetTestClock(now, pm.get()); |
366 | 378 |
367 FindFullHashesResponse res; | 379 FindFullHashesResponse res; |
368 res.mutable_negative_cache_duration()->set_seconds(600); | 380 res.mutable_negative_cache_duration()->set_seconds(600); |
369 ThreatMatch* m = res.add_matches(); | 381 ThreatMatch* m = res.add_matches(); |
370 m->set_threat_type(API_ABUSE); | 382 m->set_threat_type(API_ABUSE); |
(...skipping 11 matching lines...) Expand all Loading... |
382 res.SerializeToString(&res_data); | 394 res.SerializeToString(&res_data); |
383 | 395 |
384 std::vector<SBFullHashResult> full_hashes; | 396 std::vector<SBFullHashResult> full_hashes; |
385 base::Time cache_expire; | 397 base::Time cache_expire; |
386 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_expire)); | 398 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_expire)); |
387 | 399 |
388 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); | 400 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); |
389 EXPECT_EQ(0ul, full_hashes.size()); | 401 EXPECT_EQ(0ul, full_hashes.size()); |
390 } | 402 } |
391 | 403 |
392 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, | 404 TEST_F(V4GetHashProtocolManagerTest, |
393 TestParseHashResponseInconsistentThreatTypes) { | 405 TestParseHashResponseInconsistentThreatTypes) { |
394 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 406 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
395 | 407 |
396 FindFullHashesResponse res; | 408 FindFullHashesResponse res; |
397 res.mutable_negative_cache_duration()->set_seconds(600); | 409 res.mutable_negative_cache_duration()->set_seconds(600); |
398 ThreatMatch* m1 = res.add_matches(); | 410 ThreatMatch* m1 = res.add_matches(); |
399 m1->set_threat_type(API_ABUSE); | 411 m1->set_threat_type(API_ABUSE); |
400 m1->set_platform_type(CHROME_PLATFORM); | 412 m1->set_platform_type(CHROME_PLATFORM); |
401 m1->set_threat_entry_type(URL); | 413 m1->set_threat_entry_type(URL); |
402 m1->mutable_threat()->set_hash( | 414 m1->mutable_threat()->set_hash( |
403 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n."))); | 415 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n."))); |
404 m1->mutable_threat_entry_metadata()->add_entries(); | 416 m1->mutable_threat_entry_metadata()->add_entries(); |
405 ThreatMatch* m2 = res.add_matches(); | 417 ThreatMatch* m2 = res.add_matches(); |
406 m2->set_threat_type(MALWARE_THREAT); | 418 m2->set_threat_type(MALWARE_THREAT); |
407 m2->set_threat_entry_type(URL); | 419 m2->set_threat_entry_type(URL); |
408 m2->mutable_threat()->set_hash( | 420 m2->mutable_threat()->set_hash( |
409 SBFullHashToString(SBFullHashForString("Not to fret."))); | 421 SBFullHashToString(SBFullHashForString("Not to fret."))); |
410 | 422 |
411 // Serialize. | 423 // Serialize. |
412 std::string res_data; | 424 std::string res_data; |
413 res.SerializeToString(&res_data); | 425 res.SerializeToString(&res_data); |
414 | 426 |
415 std::vector<SBFullHashResult> full_hashes; | 427 std::vector<SBFullHashResult> full_hashes; |
416 base::Time cache_expire; | 428 base::Time cache_expire; |
417 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_expire)); | 429 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_expire)); |
418 } | 430 } |
419 | 431 |
| 432 // Checks that results are looked up correctly in the cache. |
| 433 TEST_F(V4GetHashProtocolManagerTest, GetCachedResults) { |
| 434 base::Time now = base::Time::UnixEpoch(); |
| 435 std::vector<SBFullHash> full_hashes; |
| 436 SBFullHash full_hash = SBFullHashForString("example.com/"); |
| 437 full_hashes.push_back(full_hash); |
| 438 std::vector<SBFullHashResult> cached_results; |
| 439 std::vector<SBPrefix> prefixes; |
| 440 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
| 441 pm->GetFullHashCachedResults(SB_THREAT_TYPE_API_ABUSE, |
| 442 full_hashes, now, &prefixes, &cached_results); |
| 443 |
| 444 // The cache is empty. |
| 445 EXPECT_TRUE(cached_results.empty()); |
| 446 EXPECT_EQ(1ul, prefixes.size()); |
| 447 EXPECT_EQ(full_hash.prefix, prefixes[0]); |
| 448 |
| 449 // Prefix has a cache entry but full hash is not there. |
| 450 SBCachedFullHashResult& entry = pm-> |
| 451 v4_full_hash_cache()->at(SB_THREAT_TYPE_API_ABUSE)[full_hash.prefix] = |
| 452 SBCachedFullHashResult(now + base::TimeDelta::FromMinutes(5)); |
| 453 pm->GetFullHashCachedResults(SB_THREAT_TYPE_API_ABUSE, |
| 454 full_hashes, now, &prefixes, &cached_results); |
| 455 |
| 456 EXPECT_TRUE(prefixes.empty()); |
| 457 EXPECT_TRUE(cached_results.empty()); |
| 458 |
| 459 // Expired negative cache entry. |
| 460 entry.expire_after = now - base::TimeDelta::FromMinutes(5); |
| 461 pm->GetFullHashCachedResults(SB_THREAT_TYPE_API_ABUSE, |
| 462 full_hashes, now, &prefixes, &cached_results); |
| 463 |
| 464 EXPECT_TRUE(cached_results.empty()); |
| 465 EXPECT_EQ(1ul, prefixes.size()); |
| 466 EXPECT_EQ(full_hash.prefix, prefixes[0]); |
| 467 |
| 468 // Now put the full hash in the cache. |
| 469 SBFullHashResult full_hash_result; |
| 470 full_hash_result.hash = full_hash; |
| 471 full_hash_result.cache_expire_after = now + base::TimeDelta::FromMinutes(3); |
| 472 entry.full_hashes.push_back(full_hash_result); |
| 473 pm->GetFullHashCachedResults(SB_THREAT_TYPE_API_ABUSE, |
| 474 full_hashes, now, &prefixes, &cached_results); |
| 475 |
| 476 EXPECT_TRUE(prefixes.empty()); |
| 477 EXPECT_EQ(1ul, cached_results.size()); |
| 478 EXPECT_TRUE(SBFullHashEqual(full_hash, cached_results[0].hash)); |
| 479 |
| 480 // Expired full hash in cache. |
| 481 entry.full_hashes.clear(); |
| 482 full_hash_result.cache_expire_after = now - base::TimeDelta::FromMinutes(3); |
| 483 entry.full_hashes.push_back(full_hash_result); |
| 484 pm->GetFullHashCachedResults(SB_THREAT_TYPE_API_ABUSE, |
| 485 full_hashes, now, &prefixes, &cached_results); |
| 486 |
| 487 EXPECT_TRUE(cached_results.empty()); |
| 488 EXPECT_EQ(1ul, prefixes.size()); |
| 489 EXPECT_EQ(full_hash.prefix, prefixes[0]); |
| 490 } |
| 491 /* |
| 492 // Checks that the cached results and request results are merged. |
| 493 TEST_F(V4GetHashProtocolManagerTest, CachedResultsMerged) { |
| 494 //TestClient client; |
| 495 const GURL url("https://www.example.com/more"); |
| 496 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
| 497 // Set now to max time so the cache expire times are in the future. |
| 498 SBFullHash full_hash = SBFullHashForString("example.com/"); |
| 499 SBFullHashResult full_hash_result; |
| 500 full_hash_result.hash = full_hash; |
| 501 full_hash_result.metadata.api_permissions.insert("GEOLOCATION"); |
| 502 full_hash_result.cache_expire_after = base::Time::Max(); |
| 503 //pm->AddGetFullHashResponse(full_hash_result); |
| 504 //pm->SetNegativeCacheDurationMins(base::Time::Max(), 0); |
| 505 |
| 506 EXPECT_TRUE(pm->v4_full_hash_cache()->empty()); |
| 507 EXPECT_FALSE(pm->CheckApiBlacklistUrl(url, &client)); |
| 508 base::RunLoop().RunUntilIdle(); |
| 509 |
| 510 EXPECT_TRUE(client.callback_invoked()); |
| 511 const std::set<std::string>& permissions = client.GetBlockedPermissions(); |
| 512 EXPECT_EQ(1ul, permissions.size()); |
| 513 EXPECT_EQ(1ul, permissions.count("GEOLOCATION")); |
| 514 |
| 515 // The results should be cached, so remove them from the protocol manager |
| 516 // response. |
| 517 //TestClient client2; |
| 518 //pm->ClearFullHashResponse(); |
| 519 //pm->SetNegativeCacheDurationMins(base::Time(), 0); |
| 520 EXPECT_FALSE(pm->CheckApiBlacklistUrl(url, &client2)); |
| 521 base::RunLoop().RunUntilIdle(); |
| 522 |
| 523 EXPECT_TRUE(client2.callback_invoked()); |
| 524 const std::set<std::string>& permissions2 = |
| 525 client2.GetBlockedPermissions(); |
| 526 EXPECT_EQ(1ul, permissions2.size()); |
| 527 EXPECT_EQ(1ul, permissions2.count("GEOLOCATION")); |
| 528 |
| 529 // Add a different result to the protocol manager response and ensure it is |
| 530 // merged with the cached result in the metadata. |
| 531 //TestClient client3; |
| 532 const GURL url2("https://m.example.com/more"); |
| 533 full_hash_result.hash = SBFullHashForString("m.example.com/"); |
| 534 full_hash_result.metadata.api_permissions.insert("NOTIFICATIONS"); |
| 535 //pm->AddGetFullHashResponse(full_hash_result); |
| 536 //pm->SetNegativeCacheDurationMins(base::Time::Max(), 0); |
| 537 EXPECT_FALSE(pm->CheckApiBlacklistUrl(url2, &client3)); |
| 538 base::RunLoop().RunUntilIdle(); |
| 539 |
| 540 EXPECT_TRUE(client3.callback_invoked()); |
| 541 const std::set<std::string>& permissions3 = |
| 542 client3.GetBlockedPermissions(); |
| 543 EXPECT_EQ(2ul, permissions3.size()); |
| 544 EXPECT_EQ(1ul, permissions3.count("GEOLOCATION")); |
| 545 EXPECT_EQ(1ul, permissions3.count("NOTIFICATIONS")); |
| 546 } |
| 547 |
| 548 TEST_F(V4GetHashProtocolManagerTest, CachedResultsAreEvicted) { |
| 549 base::Time epoch = base::Time::UnixEpoch(); |
| 550 SBFullHashResult full_hash_result; |
| 551 full_hash_result.hash = SBFullHashForString("example.com/"); |
| 552 full_hash_result.cache_expire_after = epoch; |
| 553 |
| 554 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
| 555 V4GetHashProtocolManager::PrefixToFullHashResultsMap& cache = |
| 556 pm->v4_full_hash_cache()->at(SB_THREAT_TYPE_API_ABUSE); |
| 557 |
| 558 // Fill the cache with some expired entries. |
| 559 // Both negative cache and full hash expired. |
| 560 cache[full_hash_result.hash.prefix] = SBCachedFullHashResult(epoch); |
| 561 cache[full_hash_result.hash.prefix].full_hashes.push_back(full_hash_result); |
| 562 |
| 563 TestClient client; |
| 564 const GURL url("https://www.example.com/more"); |
| 565 |
| 566 EXPECT_EQ(1ul, cache.size()); |
| 567 EXPECT_FALSE(pm->CheckApiBlacklistUrl(url, &client)); |
| 568 base::RunLoop().RunUntilIdle(); |
| 569 |
| 570 // Cache should be empty. |
| 571 EXPECT_TRUE(client.callback_invoked()); |
| 572 EXPECT_TRUE(cache.empty()); |
| 573 |
| 574 // Negative cache still valid and full hash expired. |
| 575 cache[full_hash_result.hash.prefix] = |
| 576 SBCachedFullHashResult(base::Time::Max()); |
| 577 cache[full_hash_result.hash.prefix].full_hashes.push_back(full_hash_result); |
| 578 |
| 579 EXPECT_EQ(1ul, cache.size()); |
| 580 EXPECT_FALSE(pm->CheckApiBlacklistUrl(url, &client)); |
| 581 base::RunLoop().RunUntilIdle(); |
| 582 |
| 583 // Cache entry should still be there. |
| 584 EXPECT_EQ(1ul, cache.size()); |
| 585 auto entry = cache.find(full_hash_result.hash.prefix); |
| 586 EXPECT_NE(cache.end(), entry); |
| 587 EXPECT_EQ(base::Time::Max(), entry->second.expire_after); |
| 588 EXPECT_EQ(1ul, entry->second.full_hashes.size()); |
| 589 EXPECT_TRUE(SBFullHashEqual(full_hash_result.hash, |
| 590 entry->second.full_hashes[0].hash)); |
| 591 EXPECT_EQ(full_hash_result.cache_expire_after, |
| 592 entry->second.full_hashes[0].cache_expire_after); |
| 593 |
| 594 // Negative cache still valid and full hash still valid. |
| 595 cache[full_hash_result.hash.prefix].full_hashes[0]. |
| 596 cache_expire_after = base::Time::Max(); |
| 597 |
| 598 EXPECT_EQ(1ul, cache.size()); |
| 599 EXPECT_FALSE(pm->CheckApiBlacklistUrl(url, &client)); |
| 600 base::RunLoop().RunUntilIdle(); |
| 601 |
| 602 // Cache entry should still be there. |
| 603 EXPECT_EQ(1ul, cache.size()); |
| 604 entry = cache.find(full_hash_result.hash.prefix); |
| 605 EXPECT_NE(cache.end(), entry); |
| 606 EXPECT_EQ(base::Time::Max(), entry->second.expire_after); |
| 607 EXPECT_EQ(1ul, entry->second.full_hashes.size()); |
| 608 EXPECT_TRUE(SBFullHashEqual(full_hash_result.hash, |
| 609 entry->second.full_hashes[0].hash)); |
| 610 EXPECT_EQ(base::Time::Max(), |
| 611 entry->second.full_hashes[0].cache_expire_after); |
| 612 |
| 613 // Negative cache expired and full hash still valid. |
| 614 cache[full_hash_result.hash.prefix].expire_after = epoch; |
| 615 |
| 616 EXPECT_EQ(1ul, cache.size()); |
| 617 EXPECT_FALSE(pm->CheckApiBlacklistUrl(url, &client)); |
| 618 base::RunLoop().RunUntilIdle(); |
| 619 |
| 620 // Cache entry should still be there. |
| 621 EXPECT_EQ(1ul, cache.size()); |
| 622 entry = cache.find(full_hash_result.hash.prefix); |
| 623 EXPECT_NE(cache.end(), entry); |
| 624 EXPECT_EQ(epoch, entry->second.expire_after); |
| 625 EXPECT_EQ(1ul, entry->second.full_hashes.size()); |
| 626 EXPECT_TRUE(SBFullHashEqual(full_hash_result.hash, |
| 627 entry->second.full_hashes[0].hash)); |
| 628 EXPECT_EQ(base::Time::Max(), |
| 629 entry->second.full_hashes[0].cache_expire_after); |
| 630 } |
| 631 */ |
420 } // namespace safe_browsing | 632 } // namespace safe_browsing |
OLD | NEW |