Chromium Code Reviews| 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" |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 73 void SetUp() override { | 73 void SetUp() override { |
| 74 PlatformTest::SetUp(); | 74 PlatformTest::SetUp(); |
| 75 callback_called_ = false; | 75 callback_called_ = false; |
| 76 } | 76 } |
| 77 | 77 |
| 78 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager() { | 78 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager() { |
| 79 V4ProtocolConfig config; | 79 V4ProtocolConfig config; |
| 80 config.client_name = kClient; | 80 config.client_name = kClient; |
| 81 config.version = kAppVer; | 81 config.version = kAppVer; |
| 82 config.key_param = kKeyParam; | 82 config.key_param = kKeyParam; |
| 83 StoresToCheck stores_to_check({GetUrlMalwareId(), GetChromeUrlApiId()}); | 83 StoresToCheck stores_to_check( |
| 84 {GetUrlMalwareId(), GetChromeUrlApiId(), | |
| 85 ListIdentifier(CHROME_PLATFORM, URL, SOCIAL_ENGINEERING_PUBLIC), | |
| 86 ListIdentifier(CHROME_PLATFORM, URL, | |
| 87 POTENTIALLY_HARMFUL_APPLICATION)}); | |
| 84 return V4GetHashProtocolManager::Create(NULL, stores_to_check, config); | 88 return V4GetHashProtocolManager::Create(NULL, stores_to_check, config); |
| 85 } | 89 } |
| 86 | 90 |
| 87 static void SetupFetcherToReturnOKResponse( | 91 static void SetupFetcherToReturnOKResponse( |
| 88 const net::TestURLFetcherFactory& factory, | 92 const net::TestURLFetcherFactory& factory, |
| 89 const std::vector<ResponseInfo>& infos) { | 93 const std::vector<ResponseInfo>& infos) { |
| 90 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); | 94 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); |
| 91 DCHECK(fetcher); | 95 DCHECK(fetcher); |
| 92 fetcher->set_status(net::URLRequestStatus()); | 96 fetcher->set_status(net::URLRequestStatus()); |
| 93 fetcher->set_response_code(200); | 97 fetcher->set_response_code(200); |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 278 | 282 |
| 279 // Verify the state of the cache. | 283 // Verify the state of the cache. |
| 280 const FullHashCache* cache = pm->full_hash_cache_for_tests(); | 284 const FullHashCache* cache = pm->full_hash_cache_for_tests(); |
| 281 // Check the cache. | 285 // Check the cache. |
| 282 EXPECT_EQ(0u, cache->size()); | 286 EXPECT_EQ(0u, cache->size()); |
| 283 } | 287 } |
| 284 | 288 |
| 285 TEST_F(V4GetHashProtocolManagerTest, TestGetHashRequest) { | 289 TEST_F(V4GetHashProtocolManagerTest, TestGetHashRequest) { |
| 286 FindFullHashesRequest req; | 290 FindFullHashesRequest req; |
| 287 ThreatInfo* info = req.mutable_threat_info(); | 291 ThreatInfo* info = req.mutable_threat_info(); |
| 292 // The order of these is important. | |
| 288 info->add_platform_types(GetCurrentPlatformType()); | 293 info->add_platform_types(GetCurrentPlatformType()); |
| 289 info->add_platform_types(CHROME_PLATFORM); | 294 info->add_platform_types(CHROME_PLATFORM); |
| 290 | 295 |
| 291 info->add_threat_entry_types(URL); | 296 info->add_threat_entry_types(URL); |
| 292 | 297 |
| 298 // The order of these is important. | |
|
Nathan Parker
2016/11/08 23:50:11
How does the order matter?
vakh (use Gerrit instead)
2016/11/10 01:05:56
The order mattered because if they are not in the
| |
| 293 info->add_threat_types(MALWARE_THREAT); | 299 info->add_threat_types(MALWARE_THREAT); |
| 300 info->add_threat_types(SOCIAL_ENGINEERING_PUBLIC); | |
| 301 info->add_threat_types(POTENTIALLY_HARMFUL_APPLICATION); | |
| 294 info->add_threat_types(API_ABUSE); | 302 info->add_threat_types(API_ABUSE); |
| 295 | 303 |
| 296 HashPrefix one = "hashone"; | 304 HashPrefix one = "hashone"; |
| 297 HashPrefix two = "hashtwo"; | 305 HashPrefix two = "hashtwo"; |
| 298 info->add_threat_entries()->set_hash(one); | 306 info->add_threat_entries()->set_hash(one); |
| 299 info->add_threat_entries()->set_hash(two); | 307 info->add_threat_entries()->set_hash(two); |
| 300 | 308 |
| 301 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 309 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
| 302 req.mutable_client()->set_client_id(pm->config_.client_name); | 310 req.mutable_client()->set_client_id(pm->config_.client_name); |
| 303 req.mutable_client()->set_client_version(pm->config_.version); | 311 req.mutable_client()->set_client_version(pm->config_.version); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 324 ThreatMatch* m = res.add_matches(); | 332 ThreatMatch* m = res.add_matches(); |
| 325 m->set_threat_type(API_ABUSE); | 333 m->set_threat_type(API_ABUSE); |
| 326 m->set_platform_type(CHROME_PLATFORM); | 334 m->set_platform_type(CHROME_PLATFORM); |
| 327 m->set_threat_entry_type(URL); | 335 m->set_threat_entry_type(URL); |
| 328 m->mutable_cache_duration()->set_seconds(300); | 336 m->mutable_cache_duration()->set_seconds(300); |
| 329 m->mutable_threat()->set_hash(full_hash); | 337 m->mutable_threat()->set_hash(full_hash); |
| 330 ThreatEntryMetadata::MetadataEntry* e = | 338 ThreatEntryMetadata::MetadataEntry* e = |
| 331 m->mutable_threat_entry_metadata()->add_entries(); | 339 m->mutable_threat_entry_metadata()->add_entries(); |
| 332 e->set_key("permission"); | 340 e->set_key("permission"); |
| 333 e->set_value("NOTIFICATIONS"); | 341 e->set_value("NOTIFICATIONS"); |
| 342 // Add another ThreatMatch for a list we don't track. This response should | |
| 343 // get dropped. | |
| 344 m = res.add_matches(); | |
| 345 m->set_threat_type(THREAT_TYPE_UNSPECIFIED); | |
| 346 m->set_platform_type(CHROME_PLATFORM); | |
| 347 m->set_threat_entry_type(URL); | |
| 348 m->mutable_cache_duration()->set_seconds(300); | |
| 349 m->mutable_threat()->set_hash(full_hash); | |
| 334 | 350 |
| 335 // Serialize. | 351 // Serialize. |
| 336 std::string res_data; | 352 std::string res_data; |
| 337 res.SerializeToString(&res_data); | 353 res.SerializeToString(&res_data); |
| 338 | 354 |
| 339 std::vector<FullHashInfo> full_hash_infos; | 355 std::vector<FullHashInfo> full_hash_infos; |
| 340 base::Time cache_expire; | 356 base::Time cache_expire; |
| 341 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hash_infos, &cache_expire)); | 357 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hash_infos, &cache_expire)); |
| 342 | 358 |
| 343 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); | 359 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); |
| 360 // Even though the server responded with two ThreatMatch responses, one | |
| 361 // should have been dropped. | |
| 344 ASSERT_EQ(1ul, full_hash_infos.size()); | 362 ASSERT_EQ(1ul, full_hash_infos.size()); |
| 345 const FullHashInfo& fhi = full_hash_infos[0]; | 363 const FullHashInfo& fhi = full_hash_infos[0]; |
| 346 EXPECT_EQ(full_hash, fhi.full_hash); | 364 EXPECT_EQ(full_hash, fhi.full_hash); |
| 347 EXPECT_EQ(GetChromeUrlApiId(), fhi.list_id); | 365 EXPECT_EQ(GetChromeUrlApiId(), fhi.list_id); |
| 348 EXPECT_EQ(1ul, fhi.metadata.api_permissions.size()); | 366 EXPECT_EQ(1ul, fhi.metadata.api_permissions.size()); |
| 349 EXPECT_EQ(1ul, fhi.metadata.api_permissions.count("NOTIFICATIONS")); | 367 EXPECT_EQ(1ul, fhi.metadata.api_permissions.count("NOTIFICATIONS")); |
| 350 EXPECT_EQ(now + base::TimeDelta::FromSeconds(300), fhi.positive_expiry); | 368 EXPECT_EQ(now + base::TimeDelta::FromSeconds(300), fhi.positive_expiry); |
| 351 EXPECT_EQ(now + base::TimeDelta::FromSeconds(400), pm->next_gethash_time_); | 369 EXPECT_EQ(now + base::TimeDelta::FromSeconds(400), pm->next_gethash_time_); |
| 352 } | 370 } |
| 353 | 371 |
| (...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 741 full_hash = FullHash("Everything's shiny, Cap'n."); | 759 full_hash = FullHash("Everything's shiny, Cap'n."); |
| 742 info = ResponseInfo(full_hash, GetChromeUrlApiId()); | 760 info = ResponseInfo(full_hash, GetChromeUrlApiId()); |
| 743 info.key_values.emplace_back("permission", "GEOLOCATION"); | 761 info.key_values.emplace_back("permission", "GEOLOCATION"); |
| 744 infos.push_back(info); | 762 infos.push_back(info); |
| 745 SetupFetcherToReturnOKResponse(factory, infos); | 763 SetupFetcherToReturnOKResponse(factory, infos); |
| 746 | 764 |
| 747 EXPECT_TRUE(callback_called()); | 765 EXPECT_TRUE(callback_called()); |
| 748 } | 766 } |
| 749 | 767 |
| 750 } // namespace safe_browsing | 768 } // namespace safe_browsing |
| OLD | NEW |