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