OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 5 |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
8 #include "base/base64.h" | 8 #include "base/base64.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
307 info->add_threat_entries()->set_hash(hash); | 307 info->add_threat_entries()->set_hash(hash); |
308 hash.clear(); | 308 hash.clear(); |
309 hash.append(reinterpret_cast<const char*>(&three), sizeof(SBPrefix)); | 309 hash.append(reinterpret_cast<const char*>(&three), sizeof(SBPrefix)); |
310 info->add_threat_entries()->set_hash(hash); | 310 info->add_threat_entries()->set_hash(hash); |
311 | 311 |
312 // Serialize and Base64 encode. | 312 // Serialize and Base64 encode. |
313 std::string req_data, req_base64; | 313 std::string req_data, req_base64; |
314 req.SerializeToString(&req_data); | 314 req.SerializeToString(&req_data); |
315 base::Base64Encode(req_data, &req_base64); | 315 base::Base64Encode(req_data, &req_base64); |
316 | 316 |
| 317 std::vector<PlatformType> platform; |
| 318 platform.push_back(CHROME_PLATFORM); |
317 std::vector<SBPrefix> prefixes; | 319 std::vector<SBPrefix> prefixes; |
318 prefixes.push_back(one); | 320 prefixes.push_back(one); |
319 prefixes.push_back(two); | 321 prefixes.push_back(two); |
320 prefixes.push_back(three); | 322 prefixes.push_back(three); |
321 EXPECT_EQ( | 323 EXPECT_EQ( |
322 req_base64, | 324 req_base64, |
323 pm->GetV4HashRequest(prefixes, API_ABUSE)); | 325 pm->GetV4HashRequest(prefixes, platform, API_ABUSE)); |
| 326 } |
| 327 |
| 328 TEST_F(SafeBrowsingProtocolManagerTest, TestParseV4HashResponse) { |
| 329 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); |
| 330 |
| 331 FindFullHashesResponse res; |
| 332 res.mutable_negative_cache_duration()->set_seconds(600); |
| 333 ThreatMatch* m = res.add_matches(); |
| 334 m->set_threat_type(API_ABUSE); |
| 335 m->set_platform_type(CHROME_PLATFORM); |
| 336 m->set_threat_entry_type(URL_EXPRESSION); |
| 337 m->mutable_cache_duration()->set_seconds(300); |
| 338 m->mutable_threat()->set_hash(SBFullHashToString( |
| 339 SBFullHashForString("Everything's shiny, Cap'n."))); |
| 340 ThreatEntryMetadata::MetadataEntry* e = |
| 341 m->mutable_threat_entry_metadata()->add_entries(); |
| 342 e->set_key("permission"); |
| 343 e->set_value("NOTIFICATIONS"); |
| 344 |
| 345 // Serialize. |
| 346 std::string res_data; |
| 347 res.SerializeToString(&res_data); |
| 348 |
| 349 std::vector<SBFullHashResult> full_hashes; |
| 350 base::TimeDelta cache_lifetime; |
| 351 pm->ParseV4HashResponse(res_data, &full_hashes, &cache_lifetime); |
| 352 |
| 353 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime); |
| 354 EXPECT_EQ(1ul, full_hashes.size()); |
| 355 EXPECT_TRUE(SBFullHashEqual( |
| 356 SBFullHashForString("Everything's shiny, Cap'n."), full_hashes[0].hash)); |
| 357 EXPECT_EQ("NOTIFICATIONS,", full_hashes[0].metadata); |
| 358 EXPECT_EQ(base::TimeDelta::FromSeconds(300), full_hashes[0].cache_duration); |
| 359 } |
| 360 |
| 361 // Adds an entry with an ignored ThreatEntryType. |
| 362 TEST_F(SafeBrowsingProtocolManagerTest, |
| 363 TestParseV4HashResponseWrongThreatEntryType) { |
| 364 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); |
| 365 |
| 366 FindFullHashesResponse res; |
| 367 res.mutable_negative_cache_duration()->set_seconds(600); |
| 368 res.add_matches()->set_threat_entry_type(BINARY_DIGEST); |
| 369 |
| 370 // Serialize. |
| 371 std::string res_data; |
| 372 res.SerializeToString(&res_data); |
| 373 |
| 374 std::vector<SBFullHashResult> full_hashes; |
| 375 base::TimeDelta cache_lifetime; |
| 376 pm->ParseV4HashResponse(res_data, &full_hashes, &cache_lifetime); |
| 377 |
| 378 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime); |
| 379 // THere should be no hash results. |
| 380 EXPECT_EQ(0ul, full_hashes.size()); |
| 381 } |
| 382 |
| 383 // Adds an entry with an SOCIAL_ENGINEERING threat type. |
| 384 TEST_F(SafeBrowsingProtocolManagerTest, |
| 385 TestParseV4HashResponseSocialEngineeringThreatType) { |
| 386 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); |
| 387 |
| 388 FindFullHashesResponse res; |
| 389 res.mutable_negative_cache_duration()->set_seconds(600); |
| 390 ThreatMatch* m = res.add_matches(); |
| 391 m->set_threat_type(SOCIAL_ENGINEERING); |
| 392 m->set_platform_type(CHROME_PLATFORM); |
| 393 m->set_threat_entry_type(URL_EXPRESSION); |
| 394 m->mutable_threat()->set_hash( |
| 395 SBFullHashToString(SBFullHashForString("Not to fret."))); |
| 396 ThreatEntryMetadata::MetadataEntry* e = |
| 397 m->mutable_threat_entry_metadata()->add_entries(); |
| 398 e->set_key("permission"); |
| 399 e->set_value("IGNORED"); |
| 400 |
| 401 // Serialize. |
| 402 std::string res_data; |
| 403 res.SerializeToString(&res_data); |
| 404 |
| 405 std::vector<SBFullHashResult> full_hashes; |
| 406 base::TimeDelta cache_lifetime; |
| 407 pm->ParseV4HashResponse(res_data, &full_hashes, &cache_lifetime); |
| 408 |
| 409 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime); |
| 410 EXPECT_EQ(1ul, full_hashes.size()); |
| 411 |
| 412 EXPECT_TRUE(SBFullHashEqual( |
| 413 SBFullHashForString("Not to fret."), full_hashes[0].hash)); |
| 414 // Metadata should be empty. |
| 415 EXPECT_EQ("", full_hashes[0].metadata); |
| 416 EXPECT_EQ(base::TimeDelta::FromSeconds(0), full_hashes[0].cache_duration); |
| 417 } |
| 418 |
| 419 // Adds metadata with a key value that is not "permission". |
| 420 TEST_F(SafeBrowsingProtocolManagerTest, |
| 421 TestParseV4HashResponseNonPermissionMetadata) { |
| 422 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); |
| 423 |
| 424 FindFullHashesResponse res; |
| 425 res.mutable_negative_cache_duration()->set_seconds(600); |
| 426 ThreatMatch* m = res.add_matches(); |
| 427 m->set_threat_type(API_ABUSE); |
| 428 m->set_platform_type(CHROME_PLATFORM); |
| 429 m->set_threat_entry_type(URL_EXPRESSION); |
| 430 m->mutable_threat()->set_hash( |
| 431 SBFullHashToString(SBFullHashForString("Not to fret."))); |
| 432 ThreatEntryMetadata::MetadataEntry* e = |
| 433 m->mutable_threat_entry_metadata()->add_entries(); |
| 434 e->set_key("notpermission"); |
| 435 e->set_value("NOTGEOLOCATION"); |
| 436 |
| 437 // Serialize. |
| 438 std::string res_data; |
| 439 res.SerializeToString(&res_data); |
| 440 |
| 441 std::vector<SBFullHashResult> full_hashes; |
| 442 base::TimeDelta cache_lifetime; |
| 443 pm->ParseV4HashResponse(res_data, &full_hashes, &cache_lifetime); |
| 444 |
| 445 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime); |
| 446 EXPECT_EQ(1ul, full_hashes.size()); |
| 447 |
| 448 EXPECT_TRUE(SBFullHashEqual( |
| 449 SBFullHashForString("Not to fret."), full_hashes[0].hash)); |
| 450 // Metadata should be empty. |
| 451 EXPECT_EQ("", full_hashes[0].metadata); |
| 452 EXPECT_EQ(base::TimeDelta::FromSeconds(0), full_hashes[0].cache_duration); |
324 } | 453 } |
325 | 454 |
326 TEST_F(SafeBrowsingProtocolManagerTest, TestUpdateUrl) { | 455 TEST_F(SafeBrowsingProtocolManagerTest, TestUpdateUrl) { |
327 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); | 456 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); |
328 | 457 |
329 EXPECT_EQ( | 458 EXPECT_EQ( |
330 "https://prefix.com/foo/downloads?client=unittest&appver=1.0&" | 459 "https://prefix.com/foo/downloads?client=unittest&appver=1.0&" |
331 "pver=3.0" + | 460 "pver=3.0" + |
332 key_param_ + "&ext=1", | 461 key_param_ + "&ext=1", |
333 pm->UpdateUrl(true).spec()); | 462 pm->UpdateUrl(true).spec()); |
(...skipping 847 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1181 | 1310 |
1182 EXPECT_FALSE(pm->IsUpdateScheduled()); | 1311 EXPECT_FALSE(pm->IsUpdateScheduled()); |
1183 | 1312 |
1184 // Invoke the AddChunksCallback to finish the update. | 1313 // Invoke the AddChunksCallback to finish the update. |
1185 runner->RunPendingTasks(); | 1314 runner->RunPendingTasks(); |
1186 | 1315 |
1187 EXPECT_TRUE(pm->IsUpdateScheduled()); | 1316 EXPECT_TRUE(pm->IsUpdateScheduled()); |
1188 } | 1317 } |
1189 | 1318 |
1190 } // namespace safe_browsing | 1319 } // namespace safe_browsing |
OLD | NEW |