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

Side by Side Diff: components/safe_browsing_db/v4_get_hash_protocol_manager_unittest.cc

Issue 1726403006: Switch Safe Browsing's metadata from string to struct. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix safe_browsing_service_browsertest and update components_tests.gyp Created 4 years, 9 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 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 <vector> 5 #include <vector>
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "components/safe_browsing_db/safebrowsing.pb.h" 11 #include "components/safe_browsing_db/safebrowsing.pb.h"
12 #include "components/safe_browsing_db/testing_util.h"
12 #include "components/safe_browsing_db/util.h" 13 #include "components/safe_browsing_db/util.h"
13 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" 14 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
14 #include "net/base/escape.h" 15 #include "net/base/escape.h"
15 #include "net/base/load_flags.h" 16 #include "net/base/load_flags.h"
16 #include "net/base/net_errors.h" 17 #include "net/base/net_errors.h"
17 #include "net/url_request/test_url_fetcher_factory.h" 18 #include "net/url_request/test_url_fetcher_factory.h"
18 #include "testing/gtest/include/gtest/gtest.h" 19 #include "testing/gtest/include/gtest/gtest.h"
19 20
20 using base::Time; 21 using base::Time;
21 using base::TimeDelta; 22 using base::TimeDelta;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 } 136 }
136 137
137 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashErrorHandlingOK) { 138 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashErrorHandlingOK) {
138 net::TestURLFetcherFactory factory; 139 net::TestURLFetcherFactory factory;
139 scoped_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 140 scoped_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
140 141
141 std::vector<SBPrefix> prefixes; 142 std::vector<SBPrefix> prefixes;
142 std::vector<SBFullHashResult> expected_full_hashes; 143 std::vector<SBFullHashResult> expected_full_hashes;
143 SBFullHashResult hash_result; 144 SBFullHashResult hash_result;
144 hash_result.hash = SBFullHashForString("Everything's shiny, Cap'n."); 145 hash_result.hash = SBFullHashForString("Everything's shiny, Cap'n.");
145 hash_result.metadata = "NOTIFICATIONS,"; 146 hash_result.metadata.api_permissions.push_back("NOTIFICATIONS");
146 hash_result.cache_duration = base::TimeDelta::FromSeconds(300); 147 hash_result.cache_duration = base::TimeDelta::FromSeconds(300);
147 expected_full_hashes.push_back(hash_result); 148 expected_full_hashes.push_back(hash_result);
148 base::TimeDelta expected_cache_duration = base::TimeDelta::FromSeconds(600); 149 base::TimeDelta expected_cache_duration = base::TimeDelta::FromSeconds(600);
149 150
150 pm->GetFullHashesWithApis( 151 pm->GetFullHashesWithApis(
151 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes, 152 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes,
152 expected_cache_duration)); 153 expected_cache_duration));
153 154
154 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 155 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
155 DCHECK(fetcher); 156 DCHECK(fetcher);
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 223
223 Time now = Time::Now(); 224 Time now = Time::Now();
224 std::vector<SBFullHashResult> full_hashes; 225 std::vector<SBFullHashResult> full_hashes;
225 base::TimeDelta cache_lifetime; 226 base::TimeDelta cache_lifetime;
226 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime)); 227 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime));
227 228
228 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime); 229 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime);
229 EXPECT_EQ(1ul, full_hashes.size()); 230 EXPECT_EQ(1ul, full_hashes.size());
230 EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("Everything's shiny, Cap'n."), 231 EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("Everything's shiny, Cap'n."),
231 full_hashes[0].hash)); 232 full_hashes[0].hash));
232 EXPECT_EQ("NOTIFICATIONS,", full_hashes[0].metadata); 233 EXPECT_EQ(1ul, full_hashes[0].metadata.api_permissions.size());
234 EXPECT_EQ("NOTIFICATIONS", full_hashes[0].metadata.api_permissions[0]);
233 EXPECT_EQ(base::TimeDelta::FromSeconds(300), full_hashes[0].cache_duration); 235 EXPECT_EQ(base::TimeDelta::FromSeconds(300), full_hashes[0].cache_duration);
234 EXPECT_LE(now + base::TimeDelta::FromSeconds(400), pm->next_gethash_time_); 236 EXPECT_LE(now + base::TimeDelta::FromSeconds(400), pm->next_gethash_time_);
235 } 237 }
236 238
237 // Adds an entry with an ignored ThreatEntryType. 239 // Adds an entry with an ignored ThreatEntryType.
238 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, 240 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest,
239 TestParseHashResponseWrongThreatEntryType) { 241 TestParseHashResponseWrongThreatEntryType) {
240 scoped_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 242 scoped_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
241 243
242 FindFullHashesResponse res; 244 FindFullHashesResponse res;
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
311 std::vector<SBFullHashResult> full_hashes; 313 std::vector<SBFullHashResult> full_hashes;
312 base::TimeDelta cache_lifetime; 314 base::TimeDelta cache_lifetime;
313 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime)); 315 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime));
314 316
315 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime); 317 EXPECT_EQ(base::TimeDelta::FromSeconds(600), cache_lifetime);
316 EXPECT_EQ(1ul, full_hashes.size()); 318 EXPECT_EQ(1ul, full_hashes.size());
317 319
318 EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("Not to fret."), 320 EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("Not to fret."),
319 full_hashes[0].hash)); 321 full_hashes[0].hash));
320 // Metadata should be empty. 322 // Metadata should be empty.
321 EXPECT_EQ("", full_hashes[0].metadata); 323 EXPECT_EQ(0ul, full_hashes[0].metadata.api_permissions.size());
322 EXPECT_EQ(base::TimeDelta::FromSeconds(0), full_hashes[0].cache_duration); 324 EXPECT_EQ(base::TimeDelta::FromSeconds(0), full_hashes[0].cache_duration);
323 } 325 }
324 326
325 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, 327 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest,
326 TestParseHashResponseInconsistentThreatTypes) { 328 TestParseHashResponseInconsistentThreatTypes) {
327 scoped_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 329 scoped_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
328 330
329 FindFullHashesResponse res; 331 FindFullHashesResponse res;
330 ThreatMatch* m1 = res.add_matches(); 332 ThreatMatch* m1 = res.add_matches();
331 m1->set_threat_type(API_ABUSE); 333 m1->set_threat_type(API_ABUSE);
(...skipping 11 matching lines...) Expand all
343 // Serialize. 345 // Serialize.
344 std::string res_data; 346 std::string res_data;
345 res.SerializeToString(&res_data); 347 res.SerializeToString(&res_data);
346 348
347 std::vector<SBFullHashResult> full_hashes; 349 std::vector<SBFullHashResult> full_hashes;
348 base::TimeDelta cache_lifetime; 350 base::TimeDelta cache_lifetime;
349 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime)); 351 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_lifetime));
350 } 352 }
351 353
352 } // namespace safe_browsing 354 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698