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

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: Rebase 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 295 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(1ul, full_hashes.size());
410
411 EXPECT_TRUE(SBFullHashEqual(
412 SBFullHashForString("Not to fret."), full_hashes[0].hash));
413 // Metadata should be empty.
414 EXPECT_EQ("", full_hashes[0].metadata);
415 EXPECT_EQ(base::TimeDelta::FromSeconds(0), full_hashes[0].cache_duration);
416 }
417
418 // Adds metadata with a key value that is not "permission".
419 TEST_F(SafeBrowsingProtocolManagerTest,
420 TestParseV4HashResponseNonPermissionMetadata) {
421 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
422
423 FindFullHashesResponse res;
424 res.mutable_negative_cache_duration()->set_seconds(600);
425 ThreatMatch* m = res.add_matches();
426 m->set_threat_type(API_ABUSE);
427 m->set_platform_type(CHROME_PLATFORM);
428 m->set_threat_entry_type(URL_EXPRESSION);
429 m->mutable_threat()->set_hash(
430 SBFullHashToString(SBFullHashForString("Not to fret.")));
431 ThreatEntryMetadata::MetadataEntry* e =
432 m->mutable_threat_entry_metadata()->add_entries();
433 e->set_key("notpermission");
434 e->set_value("NOTGEOLOCATION");
435
436 // Serialize.
437 std::string res_data;
438 res.SerializeToString(&res_data);
439
440 std::vector<SBFullHashResult> full_hashes;
441 base::TimeDelta cache_lifetime;
442 pm->ParseV4HashResponse(res_data, &full_hashes, &cache_lifetime);
443
444 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime);
445 EXPECT_EQ(1ul, full_hashes.size());
446
447 EXPECT_TRUE(SBFullHashEqual(
448 SBFullHashForString("Not to fret."), full_hashes[0].hash));
449 // Metadata should be empty.
450 EXPECT_EQ("", full_hashes[0].metadata);
451 EXPECT_EQ(base::TimeDelta::FromSeconds(0), full_hashes[0].cache_duration);
323 } 452 }
Nathan Parker 2016/01/11 23:50:32 We should add some tests that validate the network
kcarattini 2016/01/12 00:43:34 Acknowledged.
324 453
325 TEST_F(SafeBrowsingProtocolManagerTest, TestUpdateUrl) { 454 TEST_F(SafeBrowsingProtocolManagerTest, TestUpdateUrl) {
326 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); 455 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
327 456
328 EXPECT_EQ( 457 EXPECT_EQ(
329 "https://prefix.com/foo/downloads?client=unittest&appver=1.0&" 458 "https://prefix.com/foo/downloads?client=unittest&appver=1.0&"
330 "pver=3.0" + 459 "pver=3.0" +
331 key_param_ + "&ext=1", 460 key_param_ + "&ext=1",
332 pm->UpdateUrl(true).spec()); 461 pm->UpdateUrl(true).spec());
333 462
(...skipping 846 matching lines...) Expand 10 before | Expand all | Expand 10 after
1180 1309
1181 EXPECT_FALSE(pm->IsUpdateScheduled()); 1310 EXPECT_FALSE(pm->IsUpdateScheduled());
1182 1311
1183 // Invoke the AddChunksCallback to finish the update. 1312 // Invoke the AddChunksCallback to finish the update.
1184 runner->RunPendingTasks(); 1313 runner->RunPendingTasks();
1185 1314
1186 EXPECT_TRUE(pm->IsUpdateScheduled()); 1315 EXPECT_TRUE(pm->IsUpdateScheduled());
1187 } 1316 }
1188 1317
1189 } // namespace safe_browsing 1318 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698