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

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

Issue 1543153002: Add a GetFullHashWithApis method to Protocol Manager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make v4 generic Created 4 years, 12 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/memory/scoped_ptr.h" 9 #include "base/memory/scoped_ptr.h"
9 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
10 #include "base/test/test_simple_task_runner.h" 11 #include "base/test/test_simple_task_runner.h"
11 #include "base/thread_task_runner_handle.h" 12 #include "base/thread_task_runner_handle.h"
12 #include "base/time/time.h" 13 #include "base/time/time.h"
13 #include "chrome/browser/safe_browsing/chunk.pb.h" 14 #include "chrome/browser/safe_browsing/chunk.pb.h"
14 #include "chrome/browser/safe_browsing/protocol_manager.h" 15 #include "chrome/browser/safe_browsing/protocol_manager.h"
15 #include "chrome/browser/safe_browsing/safe_browsing_util.h" 16 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
17 #include "components/safe_browsing_db/safebrowsing.pb.h"
16 #include "google_apis/google_api_keys.h" 18 #include "google_apis/google_api_keys.h"
17 #include "net/base/escape.h" 19 #include "net/base/escape.h"
18 #include "net/base/load_flags.h" 20 #include "net/base/load_flags.h"
19 #include "net/base/net_errors.h" 21 #include "net/base/net_errors.h"
20 #include "net/url_request/test_url_fetcher_factory.h" 22 #include "net/url_request/test_url_fetcher_factory.h"
21 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
22 #include "testing/gmock_mutant.h" 24 #include "testing/gmock_mutant.h"
23 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
24 26
25 using base::Time; 27 using base::Time;
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 pm->GetHashUrl(false).spec()); 264 pm->GetHashUrl(false).spec());
263 265
264 pm->set_additional_query(kAdditionalQuery); 266 pm->set_additional_query(kAdditionalQuery);
265 EXPECT_EQ( 267 EXPECT_EQ(
266 "https://prefix.com/foo/gethash?client=unittest&appver=1.0&" 268 "https://prefix.com/foo/gethash?client=unittest&appver=1.0&"
267 "pver=3.0" + 269 "pver=3.0" +
268 key_param_ + "&additional_query&ext=1", 270 key_param_ + "&additional_query&ext=1",
269 pm->GetHashUrl(true).spec()); 271 pm->GetHashUrl(true).spec());
270 } 272 }
271 273
274 TEST_F(SafeBrowsingProtocolManagerTest, TestGetV4HashUrl) {
275 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
276
277 EXPECT_EQ(
278 "https://safebrowsing.googleapis.com/v4/encodedFullHashes/request_base64?"
279 "alt=proto&client_id=unittest&client_version=1.0" + key_param_,
280 pm->GetV4HashUrl("request_base64").spec());
281
282 // Additional query has no effect.
283 pm->set_additional_query(kAdditionalQuery);
284 EXPECT_EQ(
285 "https://safebrowsing.googleapis.com/v4/encodedFullHashes/request_base64?"
286 "alt=proto&client_id=unittest&client_version=1.0" + key_param_,
287 pm->GetV4HashUrl("request_base64").spec());
288 }
289
290 TEST_F(SafeBrowsingProtocolManagerTest, TestGetV4HashRequest) {
291 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
292
293 FindFullHashesRequest req;
294 ThreatInfo* info = req.mutable_threat_info();
295 info->add_threat_types(API_ABUSE);
296 info->add_platform_types(CHROME_PLATFORM);
297 info->add_threat_entry_types(URL_EXPRESSION);
298
299 SBPrefix one = 1u;
300 SBPrefix two = 2u;
301 SBPrefix three = 3u;
302 std::string hash;
303 hash.append(reinterpret_cast<const char*>(&one), sizeof(SBPrefix));
304 info->add_threat_entries()->set_hash(hash);
305 hash.clear();
306 hash.append(reinterpret_cast<const char*>(&two), sizeof(SBPrefix));
307 info->add_threat_entries()->set_hash(hash);
308 hash.clear();
309 hash.append(reinterpret_cast<const char*>(&three), sizeof(SBPrefix));
310 info->add_threat_entries()->set_hash(hash);
311
312 // Serialize and Base64 encode.
313 std::string req_data, req_base64;
314 req.SerializeToString(&req_data);
315 base::Base64Encode(req_data, &req_base64);
316
317 std::vector<SBPrefix> prefixes;
318 prefixes.push_back(one);
319 prefixes.push_back(two);
320 prefixes.push_back(three);
321 EXPECT_EQ(
322 req_base64,
323 pm->GetV4HashRequest(prefixes, API_ABUSE));
324 }
325
272 TEST_F(SafeBrowsingProtocolManagerTest, TestUpdateUrl) { 326 TEST_F(SafeBrowsingProtocolManagerTest, TestUpdateUrl) {
273 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL)); 327 scoped_ptr<SafeBrowsingProtocolManager> pm(CreateProtocolManager(NULL));
274 328
275 EXPECT_EQ( 329 EXPECT_EQ(
276 "https://prefix.com/foo/downloads?client=unittest&appver=1.0&" 330 "https://prefix.com/foo/downloads?client=unittest&appver=1.0&"
277 "pver=3.0" + 331 "pver=3.0" +
278 key_param_ + "&ext=1", 332 key_param_ + "&ext=1",
279 pm->UpdateUrl(true).spec()); 333 pm->UpdateUrl(true).spec());
280 334
281 pm->set_additional_query(kAdditionalQuery); 335 pm->set_additional_query(kAdditionalQuery);
(...skipping 845 matching lines...) Expand 10 before | Expand all | Expand 10 after
1127 1181
1128 EXPECT_FALSE(pm->IsUpdateScheduled()); 1182 EXPECT_FALSE(pm->IsUpdateScheduled());
1129 1183
1130 // Invoke the AddChunksCallback to finish the update. 1184 // Invoke the AddChunksCallback to finish the update.
1131 runner->RunPendingTasks(); 1185 runner->RunPendingTasks();
1132 1186
1133 EXPECT_TRUE(pm->IsUpdateScheduled()); 1187 EXPECT_TRUE(pm->IsUpdateScheduled());
1134 } 1188 }
1135 1189
1136 } // namespace safe_browsing 1190 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698