| 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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 return std::unique_ptr<V4GetHashProtocolManager>( | 44 return std::unique_ptr<V4GetHashProtocolManager>( |
| 45 V4GetHashProtocolManager::Create(NULL, config)); | 45 V4GetHashProtocolManager::Create(NULL, config)); |
| 46 } | 46 } |
| 47 | 47 |
| 48 std::string GetStockV4HashResponse() { | 48 std::string GetStockV4HashResponse() { |
| 49 FindFullHashesResponse res; | 49 FindFullHashesResponse res; |
| 50 res.mutable_negative_cache_duration()->set_seconds(600); | 50 res.mutable_negative_cache_duration()->set_seconds(600); |
| 51 ThreatMatch* m = res.add_matches(); | 51 ThreatMatch* m = res.add_matches(); |
| 52 m->set_threat_type(API_ABUSE); | 52 m->set_threat_type(API_ABUSE); |
| 53 m->set_platform_type(CHROME_PLATFORM); | 53 m->set_platform_type(CHROME_PLATFORM); |
| 54 m->set_threat_entry_type(URL_EXPRESSION); | 54 m->set_threat_entry_type(URL); |
| 55 m->mutable_cache_duration()->set_seconds(300); | 55 m->mutable_cache_duration()->set_seconds(300); |
| 56 m->mutable_threat()->set_hash( | 56 m->mutable_threat()->set_hash( |
| 57 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n."))); | 57 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n."))); |
| 58 ThreatEntryMetadata::MetadataEntry* e = | 58 ThreatEntryMetadata::MetadataEntry* e = |
| 59 m->mutable_threat_entry_metadata()->add_entries(); | 59 m->mutable_threat_entry_metadata()->add_entries(); |
| 60 e->set_key("permission"); | 60 e->set_key("permission"); |
| 61 e->set_value("NOTIFICATIONS"); | 61 e->set_value("NOTIFICATIONS"); |
| 62 | 62 |
| 63 // Serialize. | 63 // Serialize. |
| 64 std::string res_data; | 64 std::string res_data; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); | 172 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); |
| 173 } | 173 } |
| 174 | 174 |
| 175 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashRequest) { | 175 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashRequest) { |
| 176 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 176 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
| 177 | 177 |
| 178 FindFullHashesRequest req; | 178 FindFullHashesRequest req; |
| 179 ThreatInfo* info = req.mutable_threat_info(); | 179 ThreatInfo* info = req.mutable_threat_info(); |
| 180 info->add_threat_types(API_ABUSE); | 180 info->add_threat_types(API_ABUSE); |
| 181 info->add_platform_types(CHROME_PLATFORM); | 181 info->add_platform_types(CHROME_PLATFORM); |
| 182 info->add_threat_entry_types(URL_EXPRESSION); | 182 info->add_threat_entry_types(URL); |
| 183 | 183 |
| 184 SBPrefix one = 1u; | 184 SBPrefix one = 1u; |
| 185 SBPrefix two = 2u; | 185 SBPrefix two = 2u; |
| 186 SBPrefix three = 3u; | 186 SBPrefix three = 3u; |
| 187 std::string hash(reinterpret_cast<const char*>(&one), sizeof(SBPrefix)); | 187 std::string hash(reinterpret_cast<const char*>(&one), sizeof(SBPrefix)); |
| 188 info->add_threat_entries()->set_hash(hash); | 188 info->add_threat_entries()->set_hash(hash); |
| 189 hash.clear(); | 189 hash.clear(); |
| 190 hash.append(reinterpret_cast<const char*>(&two), sizeof(SBPrefix)); | 190 hash.append(reinterpret_cast<const char*>(&two), sizeof(SBPrefix)); |
| 191 info->add_threat_entries()->set_hash(hash); | 191 info->add_threat_entries()->set_hash(hash); |
| 192 hash.clear(); | 192 hash.clear(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 214 base::SimpleTestClock* clock = new base::SimpleTestClock(); | 214 base::SimpleTestClock* clock = new base::SimpleTestClock(); |
| 215 clock->SetNow(now); | 215 clock->SetNow(now); |
| 216 pm->SetClockForTests(base::WrapUnique(clock)); | 216 pm->SetClockForTests(base::WrapUnique(clock)); |
| 217 | 217 |
| 218 FindFullHashesResponse res; | 218 FindFullHashesResponse res; |
| 219 res.mutable_negative_cache_duration()->set_seconds(600); | 219 res.mutable_negative_cache_duration()->set_seconds(600); |
| 220 res.mutable_minimum_wait_duration()->set_seconds(400); | 220 res.mutable_minimum_wait_duration()->set_seconds(400); |
| 221 ThreatMatch* m = res.add_matches(); | 221 ThreatMatch* m = res.add_matches(); |
| 222 m->set_threat_type(API_ABUSE); | 222 m->set_threat_type(API_ABUSE); |
| 223 m->set_platform_type(CHROME_PLATFORM); | 223 m->set_platform_type(CHROME_PLATFORM); |
| 224 m->set_threat_entry_type(URL_EXPRESSION); | 224 m->set_threat_entry_type(URL); |
| 225 m->mutable_cache_duration()->set_seconds(300); | 225 m->mutable_cache_duration()->set_seconds(300); |
| 226 m->mutable_threat()->set_hash( | 226 m->mutable_threat()->set_hash( |
| 227 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n."))); | 227 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n."))); |
| 228 ThreatEntryMetadata::MetadataEntry* e = | 228 ThreatEntryMetadata::MetadataEntry* e = |
| 229 m->mutable_threat_entry_metadata()->add_entries(); | 229 m->mutable_threat_entry_metadata()->add_entries(); |
| 230 e->set_key("permission"); | 230 e->set_key("permission"); |
| 231 e->set_value("NOTIFICATIONS"); | 231 e->set_value("NOTIFICATIONS"); |
| 232 | 232 |
| 233 // Serialize. | 233 // Serialize. |
| 234 std::string res_data; | 234 std::string res_data; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, | 275 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, |
| 276 TestParseHashThreatPatternType) { | 276 TestParseHashThreatPatternType) { |
| 277 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 277 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
| 278 | 278 |
| 279 // Test social engineering pattern type. | 279 // Test social engineering pattern type. |
| 280 FindFullHashesResponse se_res; | 280 FindFullHashesResponse se_res; |
| 281 se_res.mutable_negative_cache_duration()->set_seconds(600); | 281 se_res.mutable_negative_cache_duration()->set_seconds(600); |
| 282 ThreatMatch* se = se_res.add_matches(); | 282 ThreatMatch* se = se_res.add_matches(); |
| 283 se->set_threat_type(SOCIAL_ENGINEERING_PUBLIC); | 283 se->set_threat_type(SOCIAL_ENGINEERING_PUBLIC); |
| 284 se->set_platform_type(CHROME_PLATFORM); | 284 se->set_platform_type(CHROME_PLATFORM); |
| 285 se->set_threat_entry_type(URL_EXPRESSION); | 285 se->set_threat_entry_type(URL); |
| 286 SBFullHash hash_string = SBFullHashForString("Everything's shiny, Cap'n."); | 286 SBFullHash hash_string = SBFullHashForString("Everything's shiny, Cap'n."); |
| 287 se->mutable_threat()->set_hash(SBFullHashToString(hash_string)); | 287 se->mutable_threat()->set_hash(SBFullHashToString(hash_string)); |
| 288 ThreatEntryMetadata::MetadataEntry* se_meta = | 288 ThreatEntryMetadata::MetadataEntry* se_meta = |
| 289 se->mutable_threat_entry_metadata()->add_entries(); | 289 se->mutable_threat_entry_metadata()->add_entries(); |
| 290 se_meta->set_key("se_pattern_type"); | 290 se_meta->set_key("se_pattern_type"); |
| 291 se_meta->set_value("SOCIAL_ENGINEERING_LANDING"); | 291 se_meta->set_value("SOCIAL_ENGINEERING_LANDING"); |
| 292 | 292 |
| 293 std::string se_data; | 293 std::string se_data; |
| 294 se_res.SerializeToString(&se_data); | 294 se_res.SerializeToString(&se_data); |
| 295 | 295 |
| 296 std::vector<SBFullHashResult> full_hashes; | 296 std::vector<SBFullHashResult> full_hashes; |
| 297 base::TimeDelta cache_lifetime; | 297 base::TimeDelta cache_lifetime; |
| 298 EXPECT_TRUE(pm->ParseHashResponse(se_data, &full_hashes, &cache_lifetime)); | 298 EXPECT_TRUE(pm->ParseHashResponse(se_data, &full_hashes, &cache_lifetime)); |
| 299 | 299 |
| 300 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime); | 300 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime); |
| 301 EXPECT_EQ(1ul, full_hashes.size()); | 301 EXPECT_EQ(1ul, full_hashes.size()); |
| 302 EXPECT_TRUE(SBFullHashEqual(hash_string, full_hashes[0].hash)); | 302 EXPECT_TRUE(SBFullHashEqual(hash_string, full_hashes[0].hash)); |
| 303 EXPECT_EQ(ThreatPatternType::SOCIAL_ENGINEERING_LANDING, | 303 EXPECT_EQ(ThreatPatternType::SOCIAL_ENGINEERING_LANDING, |
| 304 full_hashes[0].metadata.threat_pattern_type); | 304 full_hashes[0].metadata.threat_pattern_type); |
| 305 | 305 |
| 306 // Test potentially harmful application pattern type. | 306 // Test potentially harmful application pattern type. |
| 307 FindFullHashesResponse pha_res; | 307 FindFullHashesResponse pha_res; |
| 308 pha_res.mutable_negative_cache_duration()->set_seconds(600); | 308 pha_res.mutable_negative_cache_duration()->set_seconds(600); |
| 309 ThreatMatch* pha = pha_res.add_matches(); | 309 ThreatMatch* pha = pha_res.add_matches(); |
| 310 pha->set_threat_type(POTENTIALLY_HARMFUL_APPLICATION); | 310 pha->set_threat_type(POTENTIALLY_HARMFUL_APPLICATION); |
| 311 pha->set_threat_entry_type(URL_EXPRESSION); | 311 pha->set_threat_entry_type(URL); |
| 312 pha->set_platform_type(CHROME_PLATFORM); | 312 pha->set_platform_type(CHROME_PLATFORM); |
| 313 hash_string = SBFullHashForString("Not to fret."); | 313 hash_string = SBFullHashForString("Not to fret."); |
| 314 pha->mutable_threat()->set_hash(SBFullHashToString(hash_string)); | 314 pha->mutable_threat()->set_hash(SBFullHashToString(hash_string)); |
| 315 ThreatEntryMetadata::MetadataEntry* pha_meta = | 315 ThreatEntryMetadata::MetadataEntry* pha_meta = |
| 316 pha->mutable_threat_entry_metadata()->add_entries(); | 316 pha->mutable_threat_entry_metadata()->add_entries(); |
| 317 pha_meta->set_key("pha_pattern_type"); | 317 pha_meta->set_key("pha_pattern_type"); |
| 318 pha_meta->set_value("LANDING"); | 318 pha_meta->set_value("LANDING"); |
| 319 | 319 |
| 320 std::string pha_data; | 320 std::string pha_data; |
| 321 pha_res.SerializeToString(&pha_data); | 321 pha_res.SerializeToString(&pha_data); |
| 322 full_hashes.clear(); | 322 full_hashes.clear(); |
| 323 EXPECT_TRUE(pm->ParseHashResponse(pha_data, &full_hashes, &cache_lifetime)); | 323 EXPECT_TRUE(pm->ParseHashResponse(pha_data, &full_hashes, &cache_lifetime)); |
| 324 EXPECT_EQ(1ul, full_hashes.size()); | 324 EXPECT_EQ(1ul, full_hashes.size()); |
| 325 EXPECT_TRUE(SBFullHashEqual(hash_string, full_hashes[0].hash)); | 325 EXPECT_TRUE(SBFullHashEqual(hash_string, full_hashes[0].hash)); |
| 326 EXPECT_EQ(ThreatPatternType::MALWARE_LANDING, | 326 EXPECT_EQ(ThreatPatternType::MALWARE_LANDING, |
| 327 full_hashes[0].metadata.threat_pattern_type); | 327 full_hashes[0].metadata.threat_pattern_type); |
| 328 | 328 |
| 329 // Test invalid pattern type. | 329 // Test invalid pattern type. |
| 330 FindFullHashesResponse invalid_res; | 330 FindFullHashesResponse invalid_res; |
| 331 invalid_res.mutable_negative_cache_duration()->set_seconds(600); | 331 invalid_res.mutable_negative_cache_duration()->set_seconds(600); |
| 332 ThreatMatch* invalid = invalid_res.add_matches(); | 332 ThreatMatch* invalid = invalid_res.add_matches(); |
| 333 invalid->set_threat_type(POTENTIALLY_HARMFUL_APPLICATION); | 333 invalid->set_threat_type(POTENTIALLY_HARMFUL_APPLICATION); |
| 334 invalid->set_threat_entry_type(URL_EXPRESSION); | 334 invalid->set_threat_entry_type(URL); |
| 335 invalid->set_platform_type(CHROME_PLATFORM); | 335 invalid->set_platform_type(CHROME_PLATFORM); |
| 336 invalid->mutable_threat()->set_hash(SBFullHashToString(hash_string)); | 336 invalid->mutable_threat()->set_hash(SBFullHashToString(hash_string)); |
| 337 ThreatEntryMetadata::MetadataEntry* invalid_meta = | 337 ThreatEntryMetadata::MetadataEntry* invalid_meta = |
| 338 invalid->mutable_threat_entry_metadata()->add_entries(); | 338 invalid->mutable_threat_entry_metadata()->add_entries(); |
| 339 invalid_meta->set_key("pha_pattern_type"); | 339 invalid_meta->set_key("pha_pattern_type"); |
| 340 invalid_meta->set_value("INVALIDE_VALUE"); | 340 invalid_meta->set_value("INVALIDE_VALUE"); |
| 341 | 341 |
| 342 std::string invalid_data; | 342 std::string invalid_data; |
| 343 invalid_res.SerializeToString(&invalid_data); | 343 invalid_res.SerializeToString(&invalid_data); |
| 344 full_hashes.clear(); | 344 full_hashes.clear(); |
| 345 EXPECT_FALSE( | 345 EXPECT_FALSE( |
| 346 pm->ParseHashResponse(invalid_data, &full_hashes, &cache_lifetime)); | 346 pm->ParseHashResponse(invalid_data, &full_hashes, &cache_lifetime)); |
| 347 EXPECT_EQ(0ul, full_hashes.size()); | 347 EXPECT_EQ(0ul, full_hashes.size()); |
| 348 } | 348 } |
| 349 | 349 |
| 350 // Adds metadata with a key value that is not "permission". | 350 // Adds metadata with a key value that is not "permission". |
| 351 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, | 351 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, |
| 352 TestParseHashResponseNonPermissionMetadata) { | 352 TestParseHashResponseNonPermissionMetadata) { |
| 353 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 353 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
| 354 | 354 |
| 355 FindFullHashesResponse res; | 355 FindFullHashesResponse res; |
| 356 res.mutable_negative_cache_duration()->set_seconds(600); | 356 res.mutable_negative_cache_duration()->set_seconds(600); |
| 357 ThreatMatch* m = res.add_matches(); | 357 ThreatMatch* m = res.add_matches(); |
| 358 m->set_threat_type(API_ABUSE); | 358 m->set_threat_type(API_ABUSE); |
| 359 m->set_platform_type(CHROME_PLATFORM); | 359 m->set_platform_type(CHROME_PLATFORM); |
| 360 m->set_threat_entry_type(URL_EXPRESSION); | 360 m->set_threat_entry_type(URL); |
| 361 m->mutable_threat()->set_hash( | 361 m->mutable_threat()->set_hash( |
| 362 SBFullHashToString(SBFullHashForString("Not to fret."))); | 362 SBFullHashToString(SBFullHashForString("Not to fret."))); |
| 363 ThreatEntryMetadata::MetadataEntry* e = | 363 ThreatEntryMetadata::MetadataEntry* e = |
| 364 m->mutable_threat_entry_metadata()->add_entries(); | 364 m->mutable_threat_entry_metadata()->add_entries(); |
| 365 e->set_key("notpermission"); | 365 e->set_key("notpermission"); |
| 366 e->set_value("NOTGEOLOCATION"); | 366 e->set_value("NOTGEOLOCATION"); |
| 367 | 367 |
| 368 // Serialize. | 368 // Serialize. |
| 369 std::string res_data; | 369 std::string res_data; |
| 370 res.SerializeToString(&res_data); | 370 res.SerializeToString(&res_data); |
| 371 | 371 |
| 372 std::vector<SBFullHashResult> full_hashes; | 372 std::vector<SBFullHashResult> full_hashes; |
| 373 base::TimeDelta cache_lifetime; | 373 base::TimeDelta cache_lifetime; |
| 374 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime)); | 374 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime)); |
| 375 | 375 |
| 376 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime); | 376 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime); |
| 377 EXPECT_EQ(0ul, full_hashes.size()); | 377 EXPECT_EQ(0ul, full_hashes.size()); |
| 378 } | 378 } |
| 379 | 379 |
| 380 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, | 380 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, |
| 381 TestParseHashResponseInconsistentThreatTypes) { | 381 TestParseHashResponseInconsistentThreatTypes) { |
| 382 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); | 382 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); |
| 383 | 383 |
| 384 FindFullHashesResponse res; | 384 FindFullHashesResponse res; |
| 385 ThreatMatch* m1 = res.add_matches(); | 385 ThreatMatch* m1 = res.add_matches(); |
| 386 m1->set_threat_type(API_ABUSE); | 386 m1->set_threat_type(API_ABUSE); |
| 387 m1->set_platform_type(CHROME_PLATFORM); | 387 m1->set_platform_type(CHROME_PLATFORM); |
| 388 m1->set_threat_entry_type(URL_EXPRESSION); | 388 m1->set_threat_entry_type(URL); |
| 389 m1->mutable_threat()->set_hash( | 389 m1->mutable_threat()->set_hash( |
| 390 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n."))); | 390 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n."))); |
| 391 m1->mutable_threat_entry_metadata()->add_entries(); | 391 m1->mutable_threat_entry_metadata()->add_entries(); |
| 392 ThreatMatch* m2 = res.add_matches(); | 392 ThreatMatch* m2 = res.add_matches(); |
| 393 m2->set_threat_type(MALWARE_THREAT); | 393 m2->set_threat_type(MALWARE_THREAT); |
| 394 m2->set_threat_entry_type(URL_EXPRESSION); | 394 m2->set_threat_entry_type(URL); |
| 395 m2->mutable_threat()->set_hash( | 395 m2->mutable_threat()->set_hash( |
| 396 SBFullHashToString(SBFullHashForString("Not to fret."))); | 396 SBFullHashToString(SBFullHashForString("Not to fret."))); |
| 397 | 397 |
| 398 // Serialize. | 398 // Serialize. |
| 399 std::string res_data; | 399 std::string res_data; |
| 400 res.SerializeToString(&res_data); | 400 res.SerializeToString(&res_data); |
| 401 | 401 |
| 402 std::vector<SBFullHashResult> full_hashes; | 402 std::vector<SBFullHashResult> full_hashes; |
| 403 base::TimeDelta cache_lifetime; | 403 base::TimeDelta cache_lifetime; |
| 404 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime)); | 404 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime)); |
| 405 } | 405 } |
| 406 | 406 |
| 407 } // namespace safe_browsing | 407 } // namespace safe_browsing |
| OLD | NEW |