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

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

Issue 2233103002: Move full hash caching logic to v4_get_hash_protocol_manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move another test from database_manager to get_hash_manager Created 4 years, 3 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 "components/safe_browsing_db/v4_get_hash_protocol_manager.h" 5 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/base64.h" 10 #include "base/base64.h"
11 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/run_loop.h"
12 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
13 #include "base/test/simple_test_clock.h" 14 #include "base/test/simple_test_clock.h"
14 #include "base/time/time.h" 15 #include "base/time/time.h"
15 #include "components/safe_browsing_db/safebrowsing.pb.h" 16 #include "components/safe_browsing_db/safebrowsing.pb.h"
16 #include "components/safe_browsing_db/testing_util.h" 17 #include "components/safe_browsing_db/testing_util.h"
17 #include "components/safe_browsing_db/util.h" 18 #include "components/safe_browsing_db/util.h"
18 #include "net/base/escape.h" 19 #include "net/base/escape.h"
19 #include "net/base/load_flags.h" 20 #include "net/base/load_flags.h"
20 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
21 #include "net/url_request/test_url_fetcher_factory.h" 22 #include "net/url_request/test_url_fetcher_factory.h"
22 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/platform_test.h"
23 24
24 using base::Time; 25 using base::Time;
25 using base::TimeDelta; 26 using base::TimeDelta;
26 27
27 namespace { 28 namespace {
28 29
29 const char kClient[] = "unittest"; 30 const char kClient[] = "unittest";
30 const char kAppVer[] = "1.0"; 31 const char kAppVer[] = "1.0";
31 const char kKeyParam[] = "test_key_param"; 32 const char kKeyParam[] = "test_key_param";
32 33
33 } // namespace 34 } // namespace
34 35
35 namespace safe_browsing { 36 namespace safe_browsing {
36 37
37 class SafeBrowsingV4GetHashProtocolManagerTest : public testing::Test { 38 namespace {
38 protected: 39
40 struct KeyValue {
41 std::string key;
42 std::string value;
43
44 explicit KeyValue(const std::string key, const std::string value)
45 : key(key), value(value){};
46 explicit KeyValue(const KeyValue& other) = default;
47
48 private:
49 KeyValue();
50 };
51
52 struct ResponseInfo {
53 FullHash full_hash;
54 UpdateListIdentifier list_id;
55 std::vector<KeyValue> key_values;
56
57 explicit ResponseInfo(FullHash full_hash, UpdateListIdentifier list_id)
58 : full_hash(full_hash), list_id(list_id){};
59 explicit ResponseInfo(const ResponseInfo& other)
60 : full_hash(other.full_hash),
61 list_id(other.list_id),
62 key_values(other.key_values){};
63
64 private:
65 ResponseInfo();
66 };
67
68 } // namespace
69
70 class V4GetHashProtocolManagerTest : public PlatformTest {
71 public:
72 void SetUp() override {
73 PlatformTest::SetUp();
74 callback_called_ = false;
75 }
76
39 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager() { 77 std::unique_ptr<V4GetHashProtocolManager> CreateProtocolManager() {
40 V4ProtocolConfig config; 78 V4ProtocolConfig config;
41 config.client_name = kClient; 79 config.client_name = kClient;
42 config.version = kAppVer; 80 config.version = kAppVer;
43 config.key_param = kKeyParam; 81 config.key_param = kKeyParam;
44 return std::unique_ptr<V4GetHashProtocolManager>( 82 base::hash_set<UpdateListIdentifier> stores_to_look(
45 V4GetHashProtocolManager::Create(NULL, config)); 83 {GetUrlMalwareId(), GetChromeUrlApiId()});
84 return V4GetHashProtocolManager::Create(NULL, stores_to_look, config);
46 } 85 }
47 86
48 std::string GetStockV4HashResponse() { 87 static std::string GetV4HashResponse(
88 std::vector<ResponseInfo> response_infos) {
49 FindFullHashesResponse res; 89 FindFullHashesResponse res;
50 res.mutable_negative_cache_duration()->set_seconds(600); 90 res.mutable_negative_cache_duration()->set_seconds(600);
51 ThreatMatch* m = res.add_matches(); 91 for (const ResponseInfo& info : response_infos) {
52 m->set_threat_type(API_ABUSE); 92 ThreatMatch* m = res.add_matches();
53 m->set_platform_type(CHROME_PLATFORM); 93 m->set_platform_type(info.list_id.platform_type);
54 m->set_threat_entry_type(URL); 94 m->set_threat_entry_type(info.list_id.threat_entry_type);
55 m->mutable_cache_duration()->set_seconds(300); 95 m->set_threat_type(info.list_id.threat_type);
56 m->mutable_threat()->set_hash( 96 m->mutable_cache_duration()->set_seconds(300);
57 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n."))); 97 m->mutable_threat()->set_hash(info.full_hash);
58 ThreatEntryMetadata::MetadataEntry* e = 98
59 m->mutable_threat_entry_metadata()->add_entries(); 99 for (const KeyValue& key_value : info.key_values) {
60 e->set_key("permission"); 100 ThreatEntryMetadata::MetadataEntry* e =
61 e->set_value("NOTIFICATIONS"); 101 m->mutable_threat_entry_metadata()->add_entries();
102 e->set_key(key_value.key);
103 e->set_value(key_value.value);
104 }
105 }
62 106
63 // Serialize. 107 // Serialize.
64 std::string res_data; 108 std::string res_data;
65 res.SerializeToString(&res_data); 109 res.SerializeToString(&res_data);
66 110
67 return res_data; 111 return res_data;
68 } 112 }
69 113
114 static void SetupFetcherToReturnOKResponse(
115 const net::TestURLFetcherFactory& factory,
116 const std::vector<ResponseInfo>& infos) {
117 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
118 DCHECK(fetcher);
119 fetcher->set_status(net::URLRequestStatus());
120 fetcher->set_response_code(200);
121 fetcher->SetResponseString(GetV4HashResponse(infos));
122 fetcher->delegate()->OnURLFetchComplete(fetcher);
123 }
124
125 static std::vector<ResponseInfo> GetStockV4HashResponseInfos() {
126 ResponseInfo info(FullHash("Everything's shiny, Cap'n."),
127 GetChromeUrlApiId());
128 info.key_values.emplace_back("permission", "NOTIFICATIONS");
129 std::vector<ResponseInfo> infos;
130 infos.push_back(info);
131 return infos;
132 }
133
134 static std::string GetStockV4HashResponse() {
135 return GetV4HashResponse(GetStockV4HashResponseInfos());
136 }
137
70 void SetTestClock(base::Time now, V4GetHashProtocolManager* pm) { 138 void SetTestClock(base::Time now, V4GetHashProtocolManager* pm) {
71 base::SimpleTestClock* clock = new base::SimpleTestClock(); 139 base::SimpleTestClock* clock = new base::SimpleTestClock();
72 clock->SetNow(now); 140 clock->SetNow(now);
73 pm->SetClockForTests(base::WrapUnique(clock)); 141 pm->SetClockForTests(base::WrapUnique(clock));
74 } 142 }
143
144 void ValidateGetV4ApiResults(const ThreatMetadata& expected_md,
145 const ThreatMetadata& actual_md) {
146 EXPECT_EQ(expected_md, actual_md);
147 callback_called_ = true;
148 }
149
150 void ValidateGetV4HashResults(
151 const std::vector<FullHashInfo>& expected_results,
152 const std::vector<FullHashInfo>& actual_results) {
153 EXPECT_EQ(expected_results.size(), actual_results.size());
154 for (size_t i = 0; i < actual_results.size(); i++) {
155 EXPECT_TRUE(expected_results[i] == actual_results[i]);
156 }
157 callback_called_ = true;
158 }
159
160 bool callback_called_;
75 }; 161 };
76 162
77 void ValidateGetV4HashResults( 163 TEST_F(V4GetHashProtocolManagerTest, TestGetHashErrorHandlingNetwork) {
78 const std::vector<SBFullHashResult>& expected_full_hashes,
79 const base::Time& expected_cache_expire,
80 const std::vector<SBFullHashResult>& full_hashes,
81 const base::Time& cache_expire) {
82 EXPECT_EQ(expected_cache_expire, cache_expire);
83 ASSERT_EQ(expected_full_hashes.size(), full_hashes.size());
84
85 for (unsigned int i = 0; i < expected_full_hashes.size(); ++i) {
86 const SBFullHashResult& expected = expected_full_hashes[i];
87 const SBFullHashResult& actual = full_hashes[i];
88 EXPECT_TRUE(SBFullHashEqual(expected.hash, actual.hash));
89 EXPECT_EQ(expected.metadata, actual.metadata);
90 EXPECT_EQ(expected.cache_expire_after, actual.cache_expire_after);
91 }
92 }
93
94 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest,
95 TestGetHashErrorHandlingNetwork) {
96 net::TestURLFetcherFactory factory; 164 net::TestURLFetcherFactory factory;
97 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 165 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
98 166
99 std::vector<SBPrefix> prefixes; 167 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
100 std::vector<SBFullHashResult> expected_full_hashes; 168 full_hash_to_store_and_hash_prefixes[FullHash("AFullHash")].push_back(
101 base::Time expected_cache_expire; 169 StoreAndHashPrefix(GetUrlSocEngId(), HashPrefix("AHashPrefix")));
102 170 std::vector<FullHashInfo> expected_results;
103 pm->GetFullHashesWithApis( 171 pm->GetFullHashes(
104 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes, 172 full_hash_to_store_and_hash_prefixes,
105 expected_cache_expire)); 173 base::Bind(&V4GetHashProtocolManagerTest::ValidateGetV4HashResults,
174 base::Unretained(this), expected_results));
106 175
107 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 176 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
108 DCHECK(fetcher); 177 DCHECK(fetcher);
109 // Failed request status should result in error. 178 // Failed request status should result in error.
110 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED, 179 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::FAILED,
111 net::ERR_CONNECTION_RESET)); 180 net::ERR_CONNECTION_RESET));
112 fetcher->set_response_code(200); 181 fetcher->set_response_code(200);
113 fetcher->SetResponseString(GetStockV4HashResponse()); 182 fetcher->SetResponseString(GetStockV4HashResponse());
114 fetcher->delegate()->OnURLFetchComplete(fetcher); 183 fetcher->delegate()->OnURLFetchComplete(fetcher);
115 184
116 // Should have recorded one error, but back off multiplier is unchanged. 185 // Should have recorded one error, but back off multiplier is unchanged.
117 EXPECT_EQ(1ul, pm->gethash_error_count_); 186 EXPECT_EQ(1ul, pm->gethash_error_count_);
118 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); 187 EXPECT_EQ(1ul, pm->gethash_back_off_mult_);
188 EXPECT_TRUE(callback_called_);
119 } 189 }
120 190
121 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, 191 TEST_F(V4GetHashProtocolManagerTest, TestGetHashErrorHandlingResponseCode) {
122 TestGetHashErrorHandlingResponseCode) {
123 net::TestURLFetcherFactory factory; 192 net::TestURLFetcherFactory factory;
124 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 193 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
125 194
126 std::vector<SBPrefix> prefixes; 195 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
127 std::vector<SBFullHashResult> expected_full_hashes; 196 full_hash_to_store_and_hash_prefixes[FullHash("AFullHash")].push_back(
128 base::Time expected_cache_expire; 197 StoreAndHashPrefix(GetUrlSocEngId(), HashPrefix("AHashPrefix")));
129 198 std::vector<FullHashInfo> expected_results;
130 pm->GetFullHashesWithApis( 199 pm->GetFullHashes(
131 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes, 200 full_hash_to_store_and_hash_prefixes,
132 expected_cache_expire)); 201 base::Bind(&V4GetHashProtocolManagerTest::ValidateGetV4HashResults,
202 base::Unretained(this), expected_results));
133 203
134 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 204 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0);
135 DCHECK(fetcher); 205 DCHECK(fetcher);
136 fetcher->set_status(net::URLRequestStatus()); 206 fetcher->set_status(net::URLRequestStatus());
137 // Response code of anything other than 200 should result in error. 207 // Response code of anything other than 200 should result in error.
138 fetcher->set_response_code(204); 208 fetcher->set_response_code(204);
139 fetcher->SetResponseString(GetStockV4HashResponse()); 209 fetcher->SetResponseString(GetStockV4HashResponse());
140 fetcher->delegate()->OnURLFetchComplete(fetcher); 210 fetcher->delegate()->OnURLFetchComplete(fetcher);
141 211
142 // Should have recorded one error, but back off multiplier is unchanged. 212 // Should have recorded one error, but back off multiplier is unchanged.
143 EXPECT_EQ(1ul, pm->gethash_error_count_); 213 EXPECT_EQ(1ul, pm->gethash_error_count_);
144 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); 214 EXPECT_EQ(1ul, pm->gethash_back_off_mult_);
215 EXPECT_TRUE(callback_called_);
145 } 216 }
146 217
147 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashErrorHandlingOK) { 218 TEST_F(V4GetHashProtocolManagerTest, TestGetHashErrorHandlingOK) {
148 net::TestURLFetcherFactory factory; 219 net::TestURLFetcherFactory factory;
149 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 220 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
150 221
151 base::Time now = base::Time::UnixEpoch(); 222 base::Time now = base::Time::UnixEpoch();
152 SetTestClock(now, pm.get()); 223 SetTestClock(now, pm.get());
153 224
154 std::vector<SBPrefix> prefixes; 225 HashPrefix prefix("Everything");
155 std::vector<SBFullHashResult> expected_full_hashes; 226 FullHash full_hash("Everything's shiny, Cap'n.");
156 SBFullHashResult hash_result; 227 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
157 hash_result.hash = SBFullHashForString("Everything's shiny, Cap'n."); 228 full_hash_to_store_and_hash_prefixes[full_hash].push_back(
158 hash_result.metadata.api_permissions.insert("NOTIFICATIONS"); 229 StoreAndHashPrefix(GetChromeUrlApiId(), prefix));
159 hash_result.cache_expire_after = now + base::TimeDelta::FromSeconds(300); 230 std::vector<FullHashInfo> expected_results;
160 expected_full_hashes.push_back(hash_result); 231 FullHashInfo fhi(full_hash, GetChromeUrlApiId(),
161 base::Time expected_cache_expire = now + base::TimeDelta::FromSeconds(600); 232 now + base::TimeDelta::FromSeconds(300));
233 fhi.metadata.api_permissions.insert("NOTIFICATIONS");
234 expected_results.push_back(fhi);
162 235
163 pm->GetFullHashesWithApis( 236 pm->GetFullHashes(
164 prefixes, base::Bind(&ValidateGetV4HashResults, expected_full_hashes, 237 full_hash_to_store_and_hash_prefixes,
165 expected_cache_expire)); 238 base::Bind(&V4GetHashProtocolManagerTest::ValidateGetV4HashResults,
239 base::Unretained(this), expected_results));
166 240
167 net::TestURLFetcher* fetcher = factory.GetFetcherByID(0); 241 SetupFetcherToReturnOKResponse(factory, GetStockV4HashResponseInfos());
168 DCHECK(fetcher);
169 fetcher->set_status(net::URLRequestStatus());
170 fetcher->set_response_code(200);
171 fetcher->SetResponseString(GetStockV4HashResponse());
172 fetcher->delegate()->OnURLFetchComplete(fetcher);
173 242
174 // No error, back off multiplier is unchanged. 243 // No error, back off multiplier is unchanged.
175 EXPECT_EQ(0ul, pm->gethash_error_count_); 244 EXPECT_EQ(0ul, pm->gethash_error_count_);
176 EXPECT_EQ(1ul, pm->gethash_back_off_mult_); 245 EXPECT_EQ(1ul, pm->gethash_back_off_mult_);
246
247 // Verify the state of the cache.
248 const FullHashCache* cache = pm->full_hash_cache_for_tests();
249 // Check the cache.
250 ASSERT_EQ(1u, cache->size());
251 EXPECT_EQ(1u, cache->count(prefix));
252 const CachedHashPrefixInfo& cached_result = cache->at(prefix);
253 EXPECT_EQ(cached_result.negative_ttl,
254 now + base::TimeDelta::FromSeconds(600));
255 ASSERT_EQ(1u, cached_result.full_hash_infos.size());
256 EXPECT_EQ(FullHash("Everything's shiny, Cap'n."),
257 cached_result.full_hash_infos[0].full_hash);
258 EXPECT_TRUE(callback_called_);
177 } 259 }
178 260
179 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestGetHashRequest) { 261 TEST_F(V4GetHashProtocolManagerTest,
262 TestResultsNotCachedForNegativeCacheDuration) {
263 net::TestURLFetcherFactory factory;
180 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 264 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
181 265
266 HashPrefix prefix("Everything");
267 std::vector<HashPrefix> prefixes_requested({prefix});
268 base::Time negative_cache_expire;
269 FullHash full_hash("Everything's shiny, Cap'n.");
270 std::vector<FullHashInfo> fhis;
271 fhis.emplace_back(full_hash, GetChromeUrlApiId(), base::Time::UnixEpoch());
272
273 pm->UpdateCache(prefixes_requested, fhis, negative_cache_expire);
274
275 // Verify the state of the cache.
276 const FullHashCache* cache = pm->full_hash_cache_for_tests();
277 // Check the cache.
278 EXPECT_EQ(0u, cache->size());
279 }
280
281 TEST_F(V4GetHashProtocolManagerTest, TestGetHashRequest) {
282 HashPrefix one = "hashone";
283 HashPrefix two = "hashtwo";
284 std::vector<HashPrefix> prefixes_to_request = {one, two};
285
182 FindFullHashesRequest req; 286 FindFullHashesRequest req;
183 ThreatInfo* info = req.mutable_threat_info(); 287 ThreatInfo* info = req.mutable_threat_info();
288 info->add_threat_types(MALWARE_THREAT);
184 info->add_threat_types(API_ABUSE); 289 info->add_threat_types(API_ABUSE);
290
291 info->add_platform_types(GetCurrentPlatformType());
185 info->add_platform_types(CHROME_PLATFORM); 292 info->add_platform_types(CHROME_PLATFORM);
293
186 info->add_threat_entry_types(URL); 294 info->add_threat_entry_types(URL);
187 295
188 SBPrefix one = 1u; 296 info->add_threat_entries()->set_hash(one);
189 SBPrefix two = 2u; 297 info->add_threat_entries()->set_hash(two);
190 SBPrefix three = 3u;
191 std::string hash(reinterpret_cast<const char*>(&one), sizeof(SBPrefix));
192 info->add_threat_entries()->set_hash(hash);
193 hash.clear();
194 hash.append(reinterpret_cast<const char*>(&two), sizeof(SBPrefix));
195 info->add_threat_entries()->set_hash(hash);
196 hash.clear();
197 hash.append(reinterpret_cast<const char*>(&three), sizeof(SBPrefix));
198 info->add_threat_entries()->set_hash(hash);
199 298
200 // Serialize and Base64 encode. 299 // Serialize and Base64 encode.
201 std::string req_data, req_base64; 300 std::string req_data, req_base64;
202 req.SerializeToString(&req_data); 301 req.SerializeToString(&req_data);
203 base::Base64Encode(req_data, &req_base64); 302 base::Base64Encode(req_data, &req_base64);
204 303
205 std::vector<PlatformType> platform; 304 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
206 platform.push_back(CHROME_PLATFORM); 305 EXPECT_EQ(req_base64, pm->GetHashRequest(prefixes_to_request));
207 std::vector<SBPrefix> prefixes;
208 prefixes.push_back(one);
209 prefixes.push_back(two);
210 prefixes.push_back(three);
211 EXPECT_EQ(req_base64, pm->GetHashRequest(prefixes, platform, API_ABUSE));
212 } 306 }
213 307
214 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, TestParseHashResponse) { 308 TEST_F(V4GetHashProtocolManagerTest, TestParseHashResponse) {
215 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 309 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
216 310
217 base::Time now = base::Time::UnixEpoch(); 311 base::Time now = base::Time::UnixEpoch();
218 SetTestClock(now, pm.get()); 312 SetTestClock(now, pm.get());
219 313
314 FullHash full_hash("Everything's shiny, Cap'n.");
220 FindFullHashesResponse res; 315 FindFullHashesResponse res;
221 res.mutable_negative_cache_duration()->set_seconds(600); 316 res.mutable_negative_cache_duration()->set_seconds(600);
222 res.mutable_minimum_wait_duration()->set_seconds(400); 317 res.mutable_minimum_wait_duration()->set_seconds(400);
223 ThreatMatch* m = res.add_matches(); 318 ThreatMatch* m = res.add_matches();
224 m->set_threat_type(API_ABUSE); 319 m->set_threat_type(API_ABUSE);
225 m->set_platform_type(CHROME_PLATFORM); 320 m->set_platform_type(CHROME_PLATFORM);
226 m->set_threat_entry_type(URL); 321 m->set_threat_entry_type(URL);
227 m->mutable_cache_duration()->set_seconds(300); 322 m->mutable_cache_duration()->set_seconds(300);
228 m->mutable_threat()->set_hash( 323 m->mutable_threat()->set_hash(full_hash);
229 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n.")));
230 ThreatEntryMetadata::MetadataEntry* e = 324 ThreatEntryMetadata::MetadataEntry* e =
231 m->mutable_threat_entry_metadata()->add_entries(); 325 m->mutable_threat_entry_metadata()->add_entries();
232 e->set_key("permission"); 326 e->set_key("permission");
233 e->set_value("NOTIFICATIONS"); 327 e->set_value("NOTIFICATIONS");
234 328
235 // Serialize. 329 // Serialize.
236 std::string res_data; 330 std::string res_data;
237 res.SerializeToString(&res_data); 331 res.SerializeToString(&res_data);
238 332
239 std::vector<SBFullHashResult> full_hashes; 333 std::vector<FullHashInfo> full_hash_infos;
240 base::Time cache_expire; 334 base::Time cache_expire;
241 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hashes, &cache_expire)); 335 EXPECT_TRUE(pm->ParseHashResponse(res_data, &full_hash_infos, &cache_expire));
242 336
243 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); 337 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire);
244 EXPECT_EQ(1ul, full_hashes.size()); 338 ASSERT_EQ(1ul, full_hash_infos.size());
245 EXPECT_TRUE(SBFullHashEqual(SBFullHashForString("Everything's shiny, Cap'n."), 339 const FullHashInfo& fhi = full_hash_infos[0];
246 full_hashes[0].hash)); 340 EXPECT_EQ(full_hash, fhi.full_hash);
247 EXPECT_EQ(1ul, full_hashes[0].metadata.api_permissions.size()); 341 EXPECT_EQ(GetChromeUrlApiId(), fhi.list_id);
248 EXPECT_EQ(1ul, 342 EXPECT_EQ(1ul, fhi.metadata.api_permissions.size());
249 full_hashes[0].metadata.api_permissions.count("NOTIFICATIONS")); 343 EXPECT_EQ(1ul, fhi.metadata.api_permissions.count("NOTIFICATIONS"));
250 EXPECT_EQ(now + 344 EXPECT_EQ(now + base::TimeDelta::FromSeconds(300), fhi.positive_ttl);
251 base::TimeDelta::FromSeconds(300), full_hashes[0].cache_expire_after);
252 EXPECT_EQ(now + base::TimeDelta::FromSeconds(400), pm->next_gethash_time_); 345 EXPECT_EQ(now + base::TimeDelta::FromSeconds(400), pm->next_gethash_time_);
253 } 346 }
254 347
255 // Adds an entry with an ignored ThreatEntryType. 348 // Adds an entry with an ignored ThreatEntryType.
256 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, 349 TEST_F(V4GetHashProtocolManagerTest,
257 TestParseHashResponseWrongThreatEntryType) { 350 TestParseHashResponseWrongThreatEntryType) {
258 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 351 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
259 352
260 base::Time now = base::Time::UnixEpoch(); 353 base::Time now = base::Time::UnixEpoch();
261 SetTestClock(now, pm.get()); 354 SetTestClock(now, pm.get());
262 355
263 FindFullHashesResponse res; 356 FindFullHashesResponse res;
264 res.mutable_negative_cache_duration()->set_seconds(600); 357 res.mutable_negative_cache_duration()->set_seconds(600);
265 res.add_matches()->set_threat_entry_type(EXECUTABLE); 358 res.add_matches()->set_threat_entry_type(EXECUTABLE);
266 359
267 // Serialize. 360 // Serialize.
268 std::string res_data; 361 std::string res_data;
269 res.SerializeToString(&res_data); 362 res.SerializeToString(&res_data);
270 363
271 std::vector<SBFullHashResult> full_hashes; 364 std::vector<FullHashInfo> full_hash_infos;
272 base::Time cache_expire; 365 base::Time cache_expire;
273 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_expire)); 366 EXPECT_FALSE(
367 pm->ParseHashResponse(res_data, &full_hash_infos, &cache_expire));
274 368
275 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); 369 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire);
276 // There should be no hash results. 370 // There should be no hash results.
277 EXPECT_EQ(0ul, full_hashes.size()); 371 EXPECT_EQ(0ul, full_hash_infos.size());
278 } 372 }
279 373
280 // Adds entries with a ThreatPatternType metadata. 374 // Adds entries with a ThreatPatternType metadata.
281 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, 375 TEST_F(V4GetHashProtocolManagerTest, TestParseHashThreatPatternType) {
282 TestParseHashThreatPatternType) {
283 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 376 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
284 377
285 base::Time now = base::Time::UnixEpoch(); 378 base::Time now = base::Time::UnixEpoch();
286 SetTestClock(now, pm.get()); 379 SetTestClock(now, pm.get());
287 380
288 // Test social engineering pattern type. 381 {
289 FindFullHashesResponse se_res; 382 // Test social engineering pattern type.
290 se_res.mutable_negative_cache_duration()->set_seconds(600); 383 FindFullHashesResponse se_res;
291 ThreatMatch* se = se_res.add_matches(); 384 se_res.mutable_negative_cache_duration()->set_seconds(600);
292 se->set_threat_type(SOCIAL_ENGINEERING_PUBLIC); 385 ThreatMatch* se = se_res.add_matches();
293 se->set_platform_type(CHROME_PLATFORM); 386 se->set_threat_type(SOCIAL_ENGINEERING_PUBLIC);
294 se->set_threat_entry_type(URL); 387 se->set_platform_type(CHROME_PLATFORM);
295 SBFullHash hash_string = SBFullHashForString("Everything's shiny, Cap'n."); 388 se->set_threat_entry_type(URL);
296 se->mutable_threat()->set_hash(SBFullHashToString(hash_string)); 389 FullHash full_hash("Everything's shiny, Cap'n.");
297 ThreatEntryMetadata::MetadataEntry* se_meta = 390 se->mutable_threat()->set_hash(full_hash);
298 se->mutable_threat_entry_metadata()->add_entries(); 391 ThreatEntryMetadata::MetadataEntry* se_meta =
299 se_meta->set_key("se_pattern_type"); 392 se->mutable_threat_entry_metadata()->add_entries();
300 se_meta->set_value("SOCIAL_ENGINEERING_LANDING"); 393 se_meta->set_key("se_pattern_type");
394 se_meta->set_value("SOCIAL_ENGINEERING_LANDING");
301 395
302 std::string se_data; 396 std::string se_data;
303 se_res.SerializeToString(&se_data); 397 se_res.SerializeToString(&se_data);
304 398
305 std::vector<SBFullHashResult> full_hashes; 399 std::vector<FullHashInfo> full_hash_infos;
306 base::Time cache_expire; 400 base::Time cache_expire;
307 EXPECT_TRUE(pm->ParseHashResponse(se_data, &full_hashes, &cache_expire)); 401 EXPECT_TRUE(
402 pm->ParseHashResponse(se_data, &full_hash_infos, &cache_expire));
403 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire);
308 404
309 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); 405 ASSERT_EQ(1ul, full_hash_infos.size());
310 EXPECT_EQ(1ul, full_hashes.size()); 406 const FullHashInfo& fhi = full_hash_infos[0];
311 EXPECT_TRUE(SBFullHashEqual(hash_string, full_hashes[0].hash)); 407 EXPECT_EQ(full_hash, fhi.full_hash);
312 EXPECT_EQ(ThreatPatternType::SOCIAL_ENGINEERING_LANDING, 408 const UpdateListIdentifier list_id(CHROME_PLATFORM, URL,
313 full_hashes[0].metadata.threat_pattern_type); 409 SOCIAL_ENGINEERING_PUBLIC);
410 EXPECT_EQ(list_id, fhi.list_id);
411 EXPECT_EQ(ThreatPatternType::SOCIAL_ENGINEERING_LANDING,
412 fhi.metadata.threat_pattern_type);
413 }
314 414
315 // Test potentially harmful application pattern type. 415 {
316 FindFullHashesResponse pha_res; 416 // Test potentially harmful application pattern type.
317 pha_res.mutable_negative_cache_duration()->set_seconds(600); 417 FindFullHashesResponse pha_res;
318 ThreatMatch* pha = pha_res.add_matches(); 418 pha_res.mutable_negative_cache_duration()->set_seconds(600);
319 pha->set_threat_type(POTENTIALLY_HARMFUL_APPLICATION); 419 ThreatMatch* pha = pha_res.add_matches();
320 pha->set_threat_entry_type(URL); 420 pha->set_threat_type(POTENTIALLY_HARMFUL_APPLICATION);
321 pha->set_platform_type(CHROME_PLATFORM); 421 pha->set_threat_entry_type(URL);
322 hash_string = SBFullHashForString("Not to fret."); 422 pha->set_platform_type(CHROME_PLATFORM);
323 pha->mutable_threat()->set_hash(SBFullHashToString(hash_string)); 423 FullHash full_hash("Not to fret.");
324 ThreatEntryMetadata::MetadataEntry* pha_meta = 424 pha->mutable_threat()->set_hash(full_hash);
325 pha->mutable_threat_entry_metadata()->add_entries(); 425 ThreatEntryMetadata::MetadataEntry* pha_meta =
326 pha_meta->set_key("pha_pattern_type"); 426 pha->mutable_threat_entry_metadata()->add_entries();
327 pha_meta->set_value("LANDING"); 427 pha_meta->set_key("pha_pattern_type");
428 pha_meta->set_value("LANDING");
328 429
329 std::string pha_data; 430 std::string pha_data;
330 pha_res.SerializeToString(&pha_data); 431 pha_res.SerializeToString(&pha_data);
331 full_hashes.clear(); 432 std::vector<FullHashInfo> full_hash_infos;
332 EXPECT_TRUE(pm->ParseHashResponse(pha_data, &full_hashes, &cache_expire)); 433 base::Time cache_expire;
333 EXPECT_EQ(1ul, full_hashes.size()); 434 EXPECT_TRUE(
334 EXPECT_TRUE(SBFullHashEqual(hash_string, full_hashes[0].hash)); 435 pm->ParseHashResponse(pha_data, &full_hash_infos, &cache_expire));
335 EXPECT_EQ(ThreatPatternType::MALWARE_LANDING, 436 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire);
336 full_hashes[0].metadata.threat_pattern_type);
337 437
338 // Test invalid pattern type. 438 ASSERT_EQ(1ul, full_hash_infos.size());
339 FindFullHashesResponse invalid_res; 439 const FullHashInfo& fhi = full_hash_infos[0];
340 invalid_res.mutable_negative_cache_duration()->set_seconds(600); 440 EXPECT_EQ(full_hash, fhi.full_hash);
341 ThreatMatch* invalid = invalid_res.add_matches(); 441 const UpdateListIdentifier list_id(CHROME_PLATFORM, URL,
342 invalid->set_threat_type(POTENTIALLY_HARMFUL_APPLICATION); 442 POTENTIALLY_HARMFUL_APPLICATION);
343 invalid->set_threat_entry_type(URL); 443 EXPECT_EQ(list_id, fhi.list_id);
344 invalid->set_platform_type(CHROME_PLATFORM); 444 EXPECT_EQ(ThreatPatternType::MALWARE_LANDING,
345 invalid->mutable_threat()->set_hash(SBFullHashToString(hash_string)); 445 fhi.metadata.threat_pattern_type);
346 ThreatEntryMetadata::MetadataEntry* invalid_meta = 446 }
347 invalid->mutable_threat_entry_metadata()->add_entries();
348 invalid_meta->set_key("pha_pattern_type");
349 invalid_meta->set_value("INVALIDE_VALUE");
350 447
351 std::string invalid_data; 448 {
352 invalid_res.SerializeToString(&invalid_data); 449 // Test invalid pattern type.
353 full_hashes.clear(); 450 FullHash full_hash("Not to fret.");
354 EXPECT_FALSE( 451 FindFullHashesResponse invalid_res;
355 pm->ParseHashResponse(invalid_data, &full_hashes, &cache_expire)); 452 invalid_res.mutable_negative_cache_duration()->set_seconds(600);
356 EXPECT_EQ(0ul, full_hashes.size()); 453 ThreatMatch* invalid = invalid_res.add_matches();
454 invalid->set_threat_type(POTENTIALLY_HARMFUL_APPLICATION);
455 invalid->set_threat_entry_type(URL);
456 invalid->set_platform_type(CHROME_PLATFORM);
457 invalid->mutable_threat()->set_hash(full_hash);
458 ThreatEntryMetadata::MetadataEntry* invalid_meta =
459 invalid->mutable_threat_entry_metadata()->add_entries();
460 invalid_meta->set_key("pha_pattern_type");
461 invalid_meta->set_value("INVALIDE_VALUE");
462
463 std::string invalid_data;
464 invalid_res.SerializeToString(&invalid_data);
465 std::vector<FullHashInfo> full_hash_infos;
466 base::Time cache_expire;
467 EXPECT_FALSE(
468 pm->ParseHashResponse(invalid_data, &full_hash_infos, &cache_expire));
469 EXPECT_EQ(0ul, full_hash_infos.size());
470 }
357 } 471 }
358 472
359 // Adds metadata with a key value that is not "permission". 473 // Adds metadata with a key value that is not "permission".
360 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, 474 TEST_F(V4GetHashProtocolManagerTest,
361 TestParseHashResponseNonPermissionMetadata) { 475 TestParseHashResponseNonPermissionMetadata) {
362 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 476 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
363 477
364 base::Time now = base::Time::UnixEpoch(); 478 base::Time now = base::Time::UnixEpoch();
365 SetTestClock(now, pm.get()); 479 SetTestClock(now, pm.get());
366 480
367 FindFullHashesResponse res; 481 FindFullHashesResponse res;
368 res.mutable_negative_cache_duration()->set_seconds(600); 482 res.mutable_negative_cache_duration()->set_seconds(600);
369 ThreatMatch* m = res.add_matches(); 483 ThreatMatch* m = res.add_matches();
370 m->set_threat_type(API_ABUSE); 484 m->set_threat_type(API_ABUSE);
371 m->set_platform_type(CHROME_PLATFORM); 485 m->set_platform_type(CHROME_PLATFORM);
372 m->set_threat_entry_type(URL); 486 m->set_threat_entry_type(URL);
373 m->mutable_threat()->set_hash( 487 m->mutable_threat()->set_hash(FullHash("Not to fret."));
374 SBFullHashToString(SBFullHashForString("Not to fret.")));
375 ThreatEntryMetadata::MetadataEntry* e = 488 ThreatEntryMetadata::MetadataEntry* e =
376 m->mutable_threat_entry_metadata()->add_entries(); 489 m->mutable_threat_entry_metadata()->add_entries();
377 e->set_key("notpermission"); 490 e->set_key("notpermission");
378 e->set_value("NOTGEOLOCATION"); 491 e->set_value("NOTGEOLOCATION");
379 492
380 // Serialize. 493 // Serialize.
381 std::string res_data; 494 std::string res_data;
382 res.SerializeToString(&res_data); 495 res.SerializeToString(&res_data);
383 496
384 std::vector<SBFullHashResult> full_hashes; 497 std::vector<FullHashInfo> full_hash_infos;
385 base::Time cache_expire; 498 base::Time cache_expire;
386 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_expire)); 499 EXPECT_FALSE(
500 pm->ParseHashResponse(res_data, &full_hash_infos, &cache_expire));
387 501
388 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire); 502 EXPECT_EQ(now + base::TimeDelta::FromSeconds(600), cache_expire);
389 EXPECT_EQ(0ul, full_hashes.size()); 503 EXPECT_EQ(0ul, full_hash_infos.size());
390 } 504 }
391 505
392 TEST_F(SafeBrowsingV4GetHashProtocolManagerTest, 506 TEST_F(V4GetHashProtocolManagerTest,
393 TestParseHashResponseInconsistentThreatTypes) { 507 TestParseHashResponseInconsistentThreatTypes) {
394 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager()); 508 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
395 509
396 FindFullHashesResponse res; 510 FindFullHashesResponse res;
397 res.mutable_negative_cache_duration()->set_seconds(600); 511 res.mutable_negative_cache_duration()->set_seconds(600);
398 ThreatMatch* m1 = res.add_matches(); 512 ThreatMatch* m1 = res.add_matches();
399 m1->set_threat_type(API_ABUSE); 513 m1->set_threat_type(API_ABUSE);
400 m1->set_platform_type(CHROME_PLATFORM); 514 m1->set_platform_type(CHROME_PLATFORM);
401 m1->set_threat_entry_type(URL); 515 m1->set_threat_entry_type(URL);
402 m1->mutable_threat()->set_hash( 516 m1->mutable_threat()->set_hash(FullHash("Everything's shiny, Cap'n."));
403 SBFullHashToString(SBFullHashForString("Everything's shiny, Cap'n.")));
404 m1->mutable_threat_entry_metadata()->add_entries(); 517 m1->mutable_threat_entry_metadata()->add_entries();
405 ThreatMatch* m2 = res.add_matches(); 518 ThreatMatch* m2 = res.add_matches();
406 m2->set_threat_type(MALWARE_THREAT); 519 m2->set_threat_type(MALWARE_THREAT);
407 m2->set_threat_entry_type(URL); 520 m2->set_threat_entry_type(URL);
408 m2->mutable_threat()->set_hash( 521 m2->mutable_threat()->set_hash(FullHash("Not to fret."));
409 SBFullHashToString(SBFullHashForString("Not to fret.")));
410 522
411 // Serialize. 523 // Serialize.
412 std::string res_data; 524 std::string res_data;
413 res.SerializeToString(&res_data); 525 res.SerializeToString(&res_data);
414 526
415 std::vector<SBFullHashResult> full_hashes; 527 std::vector<FullHashInfo> full_hash_infos;
416 base::Time cache_expire; 528 base::Time cache_expire;
417 EXPECT_FALSE(pm->ParseHashResponse(res_data, &full_hashes, &cache_expire)); 529 EXPECT_FALSE(
530 pm->ParseHashResponse(res_data, &full_hash_infos, &cache_expire));
531 }
532
533 // Checks that results are looked up correctly in the cache.
534 TEST_F(V4GetHashProtocolManagerTest, GetCachedResults) {
535 base::Time now = base::Time::UnixEpoch();
536 FullHash full_hash("example.com/");
537 HashPrefix prefix("exam");
538 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
539 std::vector<HashPrefix> prefixes_to_request;
540 std::vector<FullHashInfo> cached_full_hash_infos;
541 full_hash_to_store_and_hash_prefixes[full_hash].push_back(
542 StoreAndHashPrefix(GetUrlMalwareId(), prefix));
543 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
544 FullHashCache* cache = pm->full_hash_cache_for_tests();
545
546 // Test with an empty cache. (Case: 2)
547 pm->GetFullHashCachedResults(full_hash_to_store_and_hash_prefixes, now,
548 &prefixes_to_request, &cached_full_hash_infos);
549 EXPECT_TRUE(cache->empty());
550 ASSERT_EQ(1ul, prefixes_to_request.size());
551 EXPECT_EQ(prefix, prefixes_to_request[0]);
552 EXPECT_TRUE(cached_full_hash_infos.empty());
553
554 // Prefix has a cache entry but full hash is not there. (Case: 1-b-i)
555 CachedHashPrefixInfo* entry = &(*cache)[prefix];
556 entry->negative_ttl = now + base::TimeDelta::FromMinutes(5);
557 prefixes_to_request.clear();
558 cached_full_hash_infos.clear();
559 pm->GetFullHashCachedResults(full_hash_to_store_and_hash_prefixes, now,
560 &prefixes_to_request, &cached_full_hash_infos);
561 EXPECT_TRUE(prefixes_to_request.empty());
562 EXPECT_TRUE(cached_full_hash_infos.empty());
563
564 // Expired negative cache entry. (Case: 1-b-ii)
565 entry->negative_ttl = now - base::TimeDelta::FromMinutes(5);
566 prefixes_to_request.clear();
567 cached_full_hash_infos.clear();
568 pm->GetFullHashCachedResults(full_hash_to_store_and_hash_prefixes, now,
569 &prefixes_to_request, &cached_full_hash_infos);
570 ASSERT_EQ(1ul, prefixes_to_request.size());
571 EXPECT_EQ(prefix, prefixes_to_request[0]);
572 EXPECT_TRUE(cached_full_hash_infos.empty());
573
574 // Now put unexpired full hash in the cache. (Case: 1-a-i)
575 FullHashInfo fhi(full_hash, GetUrlMalwareId(),
576 now + base::TimeDelta::FromMinutes(3));
577 entry->full_hash_infos.push_back(fhi);
578 prefixes_to_request.clear();
579 cached_full_hash_infos.clear();
580 pm->GetFullHashCachedResults(full_hash_to_store_and_hash_prefixes, now,
581 &prefixes_to_request, &cached_full_hash_infos);
582 EXPECT_TRUE(prefixes_to_request.empty());
583 ASSERT_EQ(1ul, cached_full_hash_infos.size());
584 EXPECT_EQ(full_hash, cached_full_hash_infos[0].full_hash);
585
586 // Expire the full hash in the cache. (Case: 1-a-ii)
587 entry->full_hash_infos[0].positive_ttl =
588 now - base::TimeDelta::FromMinutes(3);
589 prefixes_to_request.clear();
590 cached_full_hash_infos.clear();
591 pm->GetFullHashCachedResults(full_hash_to_store_and_hash_prefixes, now,
592 &prefixes_to_request, &cached_full_hash_infos);
593 ASSERT_EQ(1ul, prefixes_to_request.size());
594 EXPECT_EQ(prefix, prefixes_to_request[0]);
595 EXPECT_TRUE(cached_full_hash_infos.empty());
596 }
597
598 TEST_F(V4GetHashProtocolManagerTest, TestUpdatesAreMerged) {
599 // We'll add one of the requested FullHashInfo objects into the cache, and
600 // inject the other one as a response from the server. The result should
601 // include both FullHashInfo objects.
602
603 net::TestURLFetcherFactory factory;
604 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
605 HashPrefix prefix_1("exam");
606 FullHash full_hash_1("example.com/test");
607 HashPrefix prefix_2("Everything");
608 FullHash full_hash_2("Everything's shiny, Cap'n.");
609
610 base::Time now = Time::Now();
611 SetTestClock(now, pm.get());
612
613 FullHashCache* cache = pm->full_hash_cache_for_tests();
614 CachedHashPrefixInfo* entry = &(*cache)[prefix_1];
615 entry->negative_ttl = now + base::TimeDelta::FromMinutes(100);
616 // Put one unexpired full hash in the cache for a store we'll look in.
617 entry->full_hash_infos.emplace_back(full_hash_1, GetUrlMalwareId(),
618 now + base::TimeDelta::FromSeconds(200));
619 // Put one unexpired full hash in the cache for a store we'll not look in.
620 entry->full_hash_infos.emplace_back(full_hash_1, GetUrlSocEngId(),
621 now + base::TimeDelta::FromSeconds(200));
622
623 // Request full hash information from two stores.
624 FullHashToStoreAndHashPrefixesMap full_hash_to_store_and_hash_prefixes;
625 full_hash_to_store_and_hash_prefixes[full_hash_1].push_back(
626 StoreAndHashPrefix(GetUrlMalwareId(), prefix_1));
627 full_hash_to_store_and_hash_prefixes[full_hash_2].push_back(
628 StoreAndHashPrefix(GetChromeUrlApiId(), prefix_2));
629
630 // Expect full hash information from both stores.
631 std::vector<FullHashInfo> expected_results;
632 expected_results.emplace_back(full_hash_1, GetUrlMalwareId(),
633 now + base::TimeDelta::FromSeconds(200));
634 expected_results.emplace_back(full_hash_2, GetChromeUrlApiId(),
635 now + base::TimeDelta::FromSeconds(300));
636 expected_results[1].metadata.api_permissions.insert("NOTIFICATIONS");
637
638 pm->GetFullHashes(
639 full_hash_to_store_and_hash_prefixes,
640 base::Bind(&V4GetHashProtocolManagerTest::ValidateGetV4HashResults,
641 base::Unretained(this), expected_results));
642
643 SetupFetcherToReturnOKResponse(factory, GetStockV4HashResponseInfos());
644
645 // No error, back off multiplier is unchanged.
646 EXPECT_EQ(0ul, pm->gethash_error_count_);
647 EXPECT_EQ(1ul, pm->gethash_back_off_mult_);
648
649 // Verify the state of the cache.
650 ASSERT_EQ(2u, cache->size());
651 const CachedHashPrefixInfo& cached_result_1 = cache->at(prefix_1);
652 EXPECT_EQ(cached_result_1.negative_ttl,
653 now + base::TimeDelta::FromMinutes(100));
654 ASSERT_EQ(2u, cached_result_1.full_hash_infos.size());
655 EXPECT_EQ(full_hash_1, cached_result_1.full_hash_infos[0].full_hash);
656 EXPECT_EQ(GetUrlMalwareId(), cached_result_1.full_hash_infos[0].list_id);
657
658 const CachedHashPrefixInfo& cached_result_2 = cache->at(prefix_2);
659 EXPECT_EQ(cached_result_2.negative_ttl,
660 now + base::TimeDelta::FromSeconds(600));
661 ASSERT_EQ(1u, cached_result_2.full_hash_infos.size());
662 EXPECT_EQ(full_hash_2, cached_result_2.full_hash_infos[0].full_hash);
663 EXPECT_EQ(GetChromeUrlApiId(), cached_result_2.full_hash_infos[0].list_id);
664 EXPECT_TRUE(callback_called_);
665 }
666
667 // The server responds back with full hash information containing metadata
668 // information for one of the full hashes for the URL in test.
669 TEST_F(V4GetHashProtocolManagerTest, TestGetFullHashesWithApisMergesMetadata) {
670 net::TestURLFetcherFactory factory;
671 const GURL url("https://www.example.com/more");
672 ThreatMetadata expected_md;
673 expected_md.api_permissions.insert("NOTIFICATIONS");
674 expected_md.api_permissions.insert("AUDIO_CAPTURE");
675 std::unique_ptr<V4GetHashProtocolManager> pm(CreateProtocolManager());
676 pm->GetFullHashesWithApis(
677 url, base::Bind(&V4GetHashProtocolManagerTest::ValidateGetV4ApiResults,
678 base::Unretained(this), expected_md));
679
680 // The following two random looking strings value are two of the full hashes
681 // produced by UrlToFullHashes in v4_protocol_manager_util.h for the URL:
682 // "https://www.example.com/more"
683 std::vector<ResponseInfo> infos;
684 FullHash full_hash;
685 base::Base64Decode("1ZzJ0/7NjPkg6t0DAS8L5Jf7jA48Pn7opQcP4UXYeXc=",
686 &full_hash);
687 ResponseInfo info(full_hash, GetChromeUrlApiId());
688 info.key_values.emplace_back("permission", "NOTIFICATIONS");
689 infos.push_back(info);
690
691 base::Base64Decode("4rPDSdcei1BiCOPnj9kgsy2O6Ua6X3iFBakqphB3ZfA=",
692 &full_hash);
693 info = ResponseInfo(full_hash, GetChromeUrlApiId());
694 info.key_values.emplace_back("permission", "AUDIO_CAPTURE");
695 infos.push_back(info);
696
697 full_hash = FullHash("Everything's shiny, Cap'n.");
698 info = ResponseInfo(full_hash, GetChromeUrlApiId());
699 info.key_values.emplace_back("permission", "GEOLOCATION");
700 infos.push_back(info);
701 SetupFetcherToReturnOKResponse(factory, infos);
702
703 EXPECT_TRUE(callback_called_);
418 } 704 }
419 705
420 } // namespace safe_browsing 706 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698