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

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

Issue 2490753002: Ignore ThreatMatch responses for lists that we don't care about. (Closed)
Patch Set: Use a set to order platform_types etc to ensure order across platforms Created 4 years, 1 month 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/v4_get_hash_protocol_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 "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
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
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();
288 info->add_platform_types(GetCurrentPlatformType()); 292 for (const PlatformType& p :
289 info->add_platform_types(CHROME_PLATFORM); 293 std::set<PlatformType>{GetCurrentPlatformType(), CHROME_PLATFORM}) {
294 info->add_platform_types(p);
295 }
290 296
291 info->add_threat_entry_types(URL); 297 info->add_threat_entry_types(URL);
292 298
293 info->add_threat_types(MALWARE_THREAT); 299 for (const ThreatType& tt :
294 info->add_threat_types(API_ABUSE); 300 std::set<ThreatType>{MALWARE_THREAT, SOCIAL_ENGINEERING_PUBLIC,
301 POTENTIALLY_HARMFUL_APPLICATION, API_ABUSE}) {
302 info->add_threat_types(tt);
303 }
295 304
296 HashPrefix one = "hashone"; 305 HashPrefix one = "hashone";
297 HashPrefix two = "hashtwo"; 306 HashPrefix two = "hashtwo";
298 info->add_threat_entries()->set_hash(one); 307 info->add_threat_entries()->set_hash(one);
299 info->add_threat_entries()->set_hash(two); 308 info->add_threat_entries()->set_hash(two);
300 309
301 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 310 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
302 req.mutable_client()->set_client_id(pm->config_.client_name); 311 req.mutable_client()->set_client_id(pm->config_.client_name);
303 req.mutable_client()->set_client_version(pm->config_.version); 312 req.mutable_client()->set_client_version(pm->config_.version);
304 313
(...skipping 19 matching lines...) Expand all
324 ThreatMatch* m = res.add_matches(); 333 ThreatMatch* m = res.add_matches();
325 m->set_threat_type(API_ABUSE); 334 m->set_threat_type(API_ABUSE);
326 m->set_platform_type(CHROME_PLATFORM); 335 m->set_platform_type(CHROME_PLATFORM);
327 m->set_threat_entry_type(URL); 336 m->set_threat_entry_type(URL);
328 m->mutable_cache_duration()->set_seconds(300); 337 m->mutable_cache_duration()->set_seconds(300);
329 m->mutable_threat()->set_hash(full_hash); 338 m->mutable_threat()->set_hash(full_hash);
330 ThreatEntryMetadata::MetadataEntry* e = 339 ThreatEntryMetadata::MetadataEntry* e =
331 m->mutable_threat_entry_metadata()->add_entries(); 340 m->mutable_threat_entry_metadata()->add_entries();
332 e->set_key("permission"); 341 e->set_key("permission");
333 e->set_value("NOTIFICATIONS"); 342 e->set_value("NOTIFICATIONS");
343 // Add another ThreatMatch for a list we don't track. This response should
344 // get dropped.
345 m = res.add_matches();
346 m->set_threat_type(THREAT_TYPE_UNSPECIFIED);
347 m->set_platform_type(CHROME_PLATFORM);
348 m->set_threat_entry_type(URL);
349 m->mutable_cache_duration()->set_seconds(300);
350 m->mutable_threat()->set_hash(full_hash);
334 351
335 // Serialize. 352 // Serialize.
336 std::string res_data; 353 std::string res_data;
337 res.SerializeToString(&res_data); 354 res.SerializeToString(&res_data);
338 355
339 std::vector<FullHashInfo> full_hash_infos; 356 std::vector<FullHashInfo> full_hash_infos;
340 base::Time cache_expire; 357 base::Time cache_expire;
341 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hash_infos, &cache_expire)); 358 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hash_infos, &cache_expire));
342 359
343 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); 360 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire);
361 // Even though the server responded with two ThreatMatch responses, one
362 // should have been dropped.
344 ASSERT_EQ(1ul, full_hash_infos.size()); 363 ASSERT_EQ(1ul, full_hash_infos.size());
345 const FullHashInfo& fhi = full_hash_infos[0]; 364 const FullHashInfo& fhi = full_hash_infos[0];
346 EXPECT_EQ(full_hash, fhi.full_hash); 365 EXPECT_EQ(full_hash, fhi.full_hash);
347 EXPECT_EQ(GetChromeUrlApiId(), fhi.list_id); 366 EXPECT_EQ(GetChromeUrlApiId(), fhi.list_id);
348 EXPECT_EQ(1ul, fhi.metadata.api_permissions.size()); 367 EXPECT_EQ(1ul, fhi.metadata.api_permissions.size());
349 EXPECT_EQ(1ul, fhi.metadata.api_permissions.count("NOTIFICATIONS")); 368 EXPECT_EQ(1ul, fhi.metadata.api_permissions.count("NOTIFICATIONS"));
350 EXPECT_EQ(now + base::TimeDelta::FromSeconds(300), fhi.positive_expiry); 369 EXPECT_EQ(now + base::TimeDelta::FromSeconds(300), fhi.positive_expiry);
351 EXPECT_EQ(now + base::TimeDelta::FromSeconds(400), pm->next_gethash_time_); 370 EXPECT_EQ(now + base::TimeDelta::FromSeconds(400), pm->next_gethash_time_);
352 } 371 }
353 372
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
741 full_hash = FullHash("Everything's shiny, Cap'n."); 760 full_hash = FullHash("Everything's shiny, Cap'n.");
742 info = ResponseInfo(full_hash, GetChromeUrlApiId()); 761 info = ResponseInfo(full_hash, GetChromeUrlApiId());
743 info.key_values.emplace_back("permission", "GEOLOCATION"); 762 info.key_values.emplace_back("permission", "GEOLOCATION");
744 infos.push_back(info); 763 infos.push_back(info);
745 SetupFetcherToReturnOKResponse(factory, infos); 764 SetupFetcherToReturnOKResponse(factory, infos);
746 765
747 EXPECT_TRUE(callback_called()); 766 EXPECT_TRUE(callback_called());
748 } 767 }
749 768
750 } // namespace safe_browsing 769 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_get_hash_protocol_manager.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698