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

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

Issue 2458743003: Failure to parse full hash metadata shouldn't discard the response (Closed)
Patch Set: Nits: Updated comments from nparker@'s review 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 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 401
402 std::string se_data; 402 std::string se_data;
403 se_res.SerializeToString(&se_data); 403 se_res.SerializeToString(&se_data);
404 404
405 std::vector<FullHashInfo> full_hash_infos; 405 std::vector<FullHashInfo> full_hash_infos;
406 base::Time cache_expire; 406 base::Time cache_expire;
407 EXPECT_TRUE( 407 EXPECT_TRUE(
408 pm->ParseHashResponse(se_data, &full_hash_infos, &cache_expire)); 408 pm->ParseHashResponse(se_data, &full_hash_infos, &cache_expire));
409 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); 409 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire);
410 410
411 // Ensure that the threat remains valid since we found a full hash match,
412 // even though the metadata information could not be parsed correctly.
411 ASSERT_EQ(1ul, full_hash_infos.size()); 413 ASSERT_EQ(1ul, full_hash_infos.size());
412 const FullHashInfo& fhi = full_hash_infos[0]; 414 const FullHashInfo& fhi = full_hash_infos[0];
413 EXPECT_EQ(full_hash, fhi.full_hash); 415 EXPECT_EQ(full_hash, fhi.full_hash);
414 const ListIdentifier list_id(CHROME_PLATFORM, URL, 416 const ListIdentifier list_id(CHROME_PLATFORM, URL,
415 SOCIAL_ENGINEERING_PUBLIC); 417 SOCIAL_ENGINEERING_PUBLIC);
416 EXPECT_EQ(list_id, fhi.list_id); 418 EXPECT_EQ(list_id, fhi.list_id);
417 EXPECT_EQ(ThreatPatternType::SOCIAL_ENGINEERING_LANDING, 419 EXPECT_EQ(ThreatPatternType::SOCIAL_ENGINEERING_LANDING,
418 fhi.metadata.threat_pattern_type); 420 fhi.metadata.threat_pattern_type);
419 } 421 }
420 422
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 invalid->mutable_threat()->set_hash(full_hash); 465 invalid->mutable_threat()->set_hash(full_hash);
464 ThreatEntryMetadata::MetadataEntry* invalid_meta = 466 ThreatEntryMetadata::MetadataEntry* invalid_meta =
465 invalid->mutable_threat_entry_metadata()->add_entries(); 467 invalid->mutable_threat_entry_metadata()->add_entries();
466 invalid_meta->set_key("pha_pattern_type"); 468 invalid_meta->set_key("pha_pattern_type");
467 invalid_meta->set_value("INVALIDE_VALUE"); 469 invalid_meta->set_value("INVALIDE_VALUE");
468 470
469 std::string invalid_data; 471 std::string invalid_data;
470 invalid_res.SerializeToString(&invalid_data); 472 invalid_res.SerializeToString(&invalid_data);
471 std::vector<FullHashInfo> full_hash_infos; 473 std::vector<FullHashInfo> full_hash_infos;
472 base::Time cache_expire; 474 base::Time cache_expire;
473 EXPECT_FALSE( 475 EXPECT_TRUE(
474 pm->ParseHashResponse(invalid_data, &full_hash_infos, &cache_expire)); 476 pm->ParseHashResponse(invalid_data, &full_hash_infos, &cache_expire));
475 EXPECT_EQ(0ul, full_hash_infos.size()); 477
478 // Ensure that the threat remains valid since we found a full hash match,
479 // even though the metadata information could not be parsed correctly.
480 ASSERT_EQ(1ul, full_hash_infos.size());
481 const auto& fhi = full_hash_infos[0];
482 EXPECT_EQ(full_hash, fhi.full_hash);
483 EXPECT_EQ(
484 ListIdentifier(CHROME_PLATFORM, URL, POTENTIALLY_HARMFUL_APPLICATION),
485 fhi.list_id);
486 EXPECT_EQ(ThreatPatternType::NONE, fhi.metadata.threat_pattern_type);
476 } 487 }
477 } 488 }
478 489
479 // Adds metadata with a key value that is not "permission". 490 // Adds metadata with a key value that is not "permission".
480 TEST_F(V4GetHashProtocolManagerTest, 491 TEST_F(V4GetHashProtocolManagerTest,
481 TestParseHashResponseNonPermissionMetadata) { 492 TestParseHashResponseNonPermissionMetadata) {
482 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 493 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
483 494
484 base::Time now = base::Time::UnixEpoch(); 495 base::Time now = base::Time::UnixEpoch();
485 SetTestClock(now, pm.get()); 496 SetTestClock(now, pm.get());
486 497
498 FullHash full_hash("Not to fret.");
487 FindFullHashesResponse res; 499 FindFullHashesResponse res;
488 res.mutable_negative_cache_duration()->set_seconds(600); 500 res.mutable_negative_cache_duration()->set_seconds(600);
489 ThreatMatch* m = res.add_matches(); 501 ThreatMatch* m = res.add_matches();
490 m->set_threat_type(API_ABUSE); 502 m->set_threat_type(API_ABUSE);
491 m->set_platform_type(CHROME_PLATFORM); 503 m->set_platform_type(CHROME_PLATFORM);
492 m->set_threat_entry_type(URL); 504 m->set_threat_entry_type(URL);
493 m->mutable_threat()->set_hash(FullHash("Not to fret.")); 505 m->mutable_threat()->set_hash(full_hash);
494 ThreatEntryMetadata::MetadataEntry* e = 506 ThreatEntryMetadata::MetadataEntry* e =
495 m->mutable_threat_entry_metadata()->add_entries(); 507 m->mutable_threat_entry_metadata()->add_entries();
496 e->set_key("notpermission"); 508 e->set_key("notpermission");
497 e->set_value("NOTGEOLOCATION"); 509 e->set_value("NOTGEOLOCATION");
498 510
499 // Serialize. 511 // Serialize.
500 std::string res_data; 512 std::string res_data;
501 res.SerializeToString(&res_data); 513 res.SerializeToString(&res_data);
502 514
503 std::vector<FullHashInfo> full_hash_infos; 515 std::vector<FullHashInfo> full_hash_infos;
504 base::Time cache_expire; 516 base::Time cache_expire;
505 EXPECT_FALSE( 517 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hash_infos, &cache_expire));
506 pm->ParseHashResponse(res_data, &full_hash_infos, &cache_expire));
507 518
508 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); 519 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire);
509 EXPECT_EQ(0ul, full_hash_infos.size()); 520 ASSERT_EQ(1ul, full_hash_infos.size());
521 const auto& fhi = full_hash_infos[0];
522 EXPECT_EQ(full_hash, fhi.full_hash);
523 EXPECT_EQ(GetChromeUrlApiId(), fhi.list_id);
524 EXPECT_TRUE(fhi.metadata.api_permissions.empty());
510 } 525 }
511 526
512 TEST_F(V4GetHashProtocolManagerTest, 527 TEST_F(V4GetHashProtocolManagerTest,
513 TestParseHashResponseInconsistentThreatTypes) { 528 TestParseHashResponseInconsistentThreatTypes) {
514 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 529 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
515 530
516 FindFullHashesResponse res; 531 FindFullHashesResponse res;
517 res.mutable_negative_cache_duration()->set_seconds(600); 532 res.mutable_negative_cache_duration()->set_seconds(600);
518 ThreatMatch* m1 = res.add_matches(); 533 ThreatMatch* m1 = res.add_matches();
519 m1->set_threat_type(API_ABUSE); 534 m1->set_threat_type(API_ABUSE);
(...skipping 206 matching lines...) Expand 10 before | Expand all | Expand 10 after
726 full_hash = FullHash("Everything's shiny, Cap'n."); 741 full_hash = FullHash("Everything's shiny, Cap'n.");
727 info = ResponseInfo(full_hash, GetChromeUrlApiId()); 742 info = ResponseInfo(full_hash, GetChromeUrlApiId());
728 info.key_values.emplace_back("permission", "GEOLOCATION"); 743 info.key_values.emplace_back("permission", "GEOLOCATION");
729 infos.push_back(info); 744 infos.push_back(info);
730 SetupFetcherToReturnOKResponse(factory, infos); 745 SetupFetcherToReturnOKResponse(factory, infos);
731 746
732 EXPECT_TRUE(callback_called()); 747 EXPECT_TRUE(callback_called());
733 } 748 }
734 749
735 } // namespace safe_browsing 750 } // 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