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

Side by Side Diff: chrome/browser/safe_browsing/protocol_manager_unittest.cc

Issue 1556613002: Adds SB V4 response handler to Protocol Manager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@osb-pm-1
Patch Set: Created 4 years, 11 months 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
OLDNEW
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 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 316
317 std::vector<SBPrefix> prefixes; 317 std::vector<SBPrefix> prefixes;
318 prefixes.push_back(one); 318 prefixes.push_back(one);
319 prefixes.push_back(two); 319 prefixes.push_back(two);
320 prefixes.push_back(three); 320 prefixes.push_back(three);
321 EXPECT_EQ( 321 EXPECT_EQ(
322 req_base64, 322 req_base64,
323 pm->GetV4HashRequest(prefixes, API_ABUSE)); 323 pm->GetV4HashRequest(prefixes, API_ABUSE));
324 } 324 }
325 325
326 TEST_F(SafeBrowsingProtocolManagerTest, TestParseV4HashResponse) {
327 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
328
329 FindFullHashesResponse res;
330 res.mutable_negative_cache_duration()->set_seconds(600);
331 ThreatMatch* m = res.add_matches();
332 m->set_threat_type(API_ABUSE);
333 m->set_platform_type(CHROME_PLATFORM);
334 m->set_threat_entry_type(URL_EXPRESSION);
335 m->mutable_cache_duration()->set_seconds(300);
336 m->mutable_threat()->set_hash(SBFullHashToString(
337 SBFullHashForString("Everything's shiny, Cap'n.")));
338 ThreatEntryMetadata::MetadataEntry* e =
339 m->mutable_threat_entry_metadata()->add_entries();
340 e->set_key("permission");
341 e->set_value("NOTIFICATIONS");
342
343 // Serialize and Base64 encode.
344 std::string res_data, res_base64;
345 res.SerializeToString(&res_data);
346 base::Base64Encode(res_data, &res_base64);
347
348 std::vector<SBFullHashResult> full_hashes;
349 base::TimeDelta cache_lifetime;
350 pm->ParseV4HashResponse(res_base64, &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 and Base64 encode.
370 std::string res_data, res_base64;
371 res.SerializeToString(&res_data);
372 base::Base64Encode(res_data, &res_base64);
373
374 std::vector<SBFullHashResult> full_hashes;
375 base::TimeDelta cache_lifetime;
376 pm->ParseV4HashResponse(res_base64, &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 and Base64 encode.
402 std::string res_data, res_base64;
403 res.SerializeToString(&res_data);
404 base::Base64Encode(res_data, &res_base64);
405
406 std::vector<SBFullHashResult> full_hashes;
407 base::TimeDelta cache_lifetime;
408 pm->ParseV4HashResponse(res_base64, &full_hashes, &cache_lifetime);
409
410 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime);
411 EXPECT_EQ(1ul, full_hashes.size());
412
413 EXPECT_TRUE(SBFullHashEqual(
414 SBFullHashForString("Not to fret."), full_hashes[0].hash));
415 // Metadata should be empty.
416 EXPECT_EQ("", full_hashes[0].metadata);
417 EXPECT_EQ(base::TimeDelta::FromSeconds(0), full_hashes[0].cache_duration);
418 }
419
420 // Adds metadata with a key value that is not "permission".
421 TEST_F(SafeBrowsingProtocolManagerTest,
422 TestParseV4HashResponseNonPermissionMetadata) {
423 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
424
425 FindFullHashesResponse res;
426 res.mutable_negative_cache_duration()->set_seconds(600);
427 ThreatMatch* m = res.add_matches();
428 m->set_threat_type(API_ABUSE);
429 m->set_platform_type(CHROME_PLATFORM);
430 m->set_threat_entry_type(URL_EXPRESSION);
431 m->mutable_threat()->set_hash(
432 SBFullHashToString(SBFullHashForString("Not to fret.")));
433 ThreatEntryMetadata::MetadataEntry* e =
434 m->mutable_threat_entry_metadata()->add_entries();
435 e->set_key("notpermission");
436 e->set_value("NOTGEOLOCATION");
437
438 // Serialize and Base64 encode.
439 std::string res_data, res_base64;
440 res.SerializeToString(&res_data);
441 base::Base64Encode(res_data, &res_base64);
442
443 std::vector<SBFullHashResult> full_hashes;
444 base::TimeDelta cache_lifetime;
445 pm->ParseV4HashResponse(res_base64, &full_hashes, &cache_lifetime);
446
447 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime);
448 EXPECT_EQ(1ul, full_hashes.size());
449
450 EXPECT_TRUE(SBFullHashEqual(
451 SBFullHashForString("Not to fret."), full_hashes[0].hash));
452 // Metadata should be empty.
453 EXPECT_EQ("", full_hashes[0].metadata);
454 EXPECT_EQ(base::TimeDelta::FromSeconds(0), full_hashes[0].cache_duration);
455 }
456
326 TEST_F(SafeBrowsingProtocolManagerTest, TestUpdateUrl) { 457 TEST_F(SafeBrowsingProtocolManagerTest, TestUpdateUrl) {
327 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); 458 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
328 459
329 EXPECT_EQ( 460 EXPECT_EQ(
330 "https://prefix.com/foo/downloads?client=unittest&appver=1.0&" 461 "https://prefix.com/foo/downloads?client=unittest&appver=1.0&"
331 "pver=3.0" + 462 "pver=3.0" +
332 key_param_ + "&ext=1", 463 key_param_ + "&ext=1",
333 pm->UpdateUrl(true).spec()); 464 pm->UpdateUrl(true).spec());
334 465
335 pm->set_additional_query(kAdditionalQuery); 466 pm->set_additional_query(kAdditionalQuery);
(...skipping 845 matching lines...) Expand 10 before | Expand all | Expand 10 after
1181 1312
1182 EXPECT_FALSE(pm->IsUpdateScheduled()); 1313 EXPECT_FALSE(pm->IsUpdateScheduled());
1183 1314
1184 // Invoke the AddChunksCallback to finish the update. 1315 // Invoke the AddChunksCallback to finish the update.
1185 runner->RunPendingTasks(); 1316 runner->RunPendingTasks();
1186 1317
1187 EXPECT_TRUE(pm->IsUpdateScheduled()); 1318 EXPECT_TRUE(pm->IsUpdateScheduled());
1188 } 1319 }
1189 1320
1190 } // namespace safe_browsing 1321 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698