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

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

Issue 1890753002: SafeBrowsing: Track and cancel API checks. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@osb-impl-2
Patch Set: Use raw pointer in set Created 4 years, 8 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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/macros.h" 10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h" 14 #include "base/run_loop.h"
14 #include "components/safe_browsing_db/database_manager.h" 15 #include "components/safe_browsing_db/database_manager.h"
15 #include "components/safe_browsing_db/test_database_manager.h" 16 #include "components/safe_browsing_db/test_database_manager.h"
16 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h" 17 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
17 #include "content/public/browser/browser_thread.h" 18 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h" 19 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h" 21 #include "url/gurl.h"
21 22
22 using content::BrowserThread; 23 using content::BrowserThread;
23 24
24 namespace safe_browsing { 25 namespace safe_browsing {
25 26
26 namespace { 27 namespace {
27 28
29 void InvokeFullHashCallback(
30 V4GetHashProtocolManager::FullHashCallback callback,
31 const std::vector<SBFullHashResult>& full_hashes) {
32 callback.Run(full_hashes, base::TimeDelta::FromMinutes(0));
33 }
34
28 // A TestV4GetHashProtocolManager that returns fixed responses from the 35 // A TestV4GetHashProtocolManager that returns fixed responses from the
29 // Safe Browsing server for testing purpose. 36 // Safe Browsing server for testing purpose.
30 class TestV4GetHashProtocolManager : public V4GetHashProtocolManager { 37 class TestV4GetHashProtocolManager : public V4GetHashProtocolManager {
31 public: 38 public:
32 TestV4GetHashProtocolManager( 39 TestV4GetHashProtocolManager(
33 net::URLRequestContextGetter* request_context_getter, 40 net::URLRequestContextGetter* request_context_getter,
34 const V4ProtocolConfig& config) 41 const V4ProtocolConfig& config)
35 : V4GetHashProtocolManager(request_context_getter, config) {} 42 : V4GetHashProtocolManager(request_context_getter, config),
43 delay_seconds_(0) {}
36 44
37 ~TestV4GetHashProtocolManager() override {} 45 ~TestV4GetHashProtocolManager() override {}
38 46
39 void GetFullHashesWithApis(const std::vector<SBPrefix>& prefixes, 47 void GetFullHashesWithApis(const std::vector<SBPrefix>& prefixes,
40 FullHashCallback callback) override { 48 FullHashCallback callback) override {
41 prefixes_ = prefixes; 49 prefixes_ = prefixes;
42 callback.Run(full_hashes_, base::TimeDelta::FromMinutes(0)); 50 base::MessageLoop::current()->PostDelayedTask(
51 FROM_HERE, base::Bind(InvokeFullHashCallback, callback, full_hashes_),
52 base::TimeDelta::FromSeconds(delay_seconds_));
Nathan Parker 2016/04/19 18:05:19 Does this use real time, or just ensure the orderi
kcarattini 2016/04/20 01:55:53 I'm not sure. It doesn't appear to use real time b
53 }
54
55 void SetDelaySeconds(int delay) {
56 delay_seconds_ = delay;
43 } 57 }
44 58
45 // Prepare the GetFullHash results for the next request. 59 // Prepare the GetFullHash results for the next request.
46 void AddGetFullHashResponse(const SBFullHashResult& full_hash_result) { 60 void AddGetFullHashResponse(const SBFullHashResult& full_hash_result) {
47 full_hashes_.push_back(full_hash_result); 61 full_hashes_.push_back(full_hash_result);
48 } 62 }
49 63
50 // Returns the prefixes that were sent in the last request. 64 // Returns the prefixes that were sent in the last request.
51 const std::vector<SBPrefix>& GetRequestPrefixes() { return prefixes_; } 65 const std::vector<SBPrefix>& GetRequestPrefixes() { return prefixes_; }
52 66
53 private: 67 private:
54 std::vector<SBPrefix> prefixes_; 68 std::vector<SBPrefix> prefixes_;
55 std::vector<SBFullHashResult> full_hashes_; 69 std::vector<SBFullHashResult> full_hashes_;
70 int delay_seconds_;
56 }; 71 };
57 72
58 // Factory that creates test protocol manager instances. 73 // Factory that creates test protocol manager instances.
59 class TestV4GetHashProtocolManagerFactory : 74 class TestV4GetHashProtocolManagerFactory :
60 public V4GetHashProtocolManagerFactory { 75 public V4GetHashProtocolManagerFactory {
61 public: 76 public:
62 TestV4GetHashProtocolManagerFactory() : pm_(NULL) {} 77 TestV4GetHashProtocolManagerFactory() : pm_(NULL) {}
63 ~TestV4GetHashProtocolManagerFactory() override {} 78 ~TestV4GetHashProtocolManagerFactory() override {}
64 79
65 V4GetHashProtocolManager* CreateProtocolManager( 80 V4GetHashProtocolManager* CreateProtocolManager(
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 } 138 }
124 139
125 TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlPrefixes) { 140 TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlPrefixes) {
126 TestClient client; 141 TestClient client;
127 const GURL url("https://www.example.com/more"); 142 const GURL url("https://www.example.com/more");
128 // Generated from the sorted output of UrlToFullHashes in util.h. 143 // Generated from the sorted output of UrlToFullHashes in util.h.
129 std::vector<SBPrefix> expected_prefixes = 144 std::vector<SBPrefix> expected_prefixes =
130 {1237562338, 2871045197, 3553205461, 3766933875}; 145 {1237562338, 2871045197, 3553205461, 3766933875};
131 146
132 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); 147 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
148 base::RunLoop().RunUntilIdle();
133 std::vector<SBPrefix> prefixes = static_cast<TestV4GetHashProtocolManager*>( 149 std::vector<SBPrefix> prefixes = static_cast<TestV4GetHashProtocolManager*>(
134 db_manager_->v4_get_hash_protocol_manager_)->GetRequestPrefixes(); 150 db_manager_->v4_get_hash_protocol_manager_)->GetRequestPrefixes();
135 EXPECT_EQ(expected_prefixes.size(), prefixes.size()); 151 EXPECT_EQ(expected_prefixes.size(), prefixes.size());
136 for (unsigned int i = 0; i < prefixes.size(); ++i) { 152 for (unsigned int i = 0; i < prefixes.size(); ++i) {
137 EXPECT_EQ(expected_prefixes[i], prefixes[i]); 153 EXPECT_EQ(expected_prefixes[i], prefixes[i]);
138 } 154 }
139 } 155 }
140 156
141 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResults) { 157 TEST_F(SafeBrowsingDatabaseManagerTest, HandleGetHashesWithApisResults) {
142 TestClient client; 158 TestClient client;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 209
194 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client)); 210 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
195 base::RunLoop().RunUntilIdle(); 211 base::RunLoop().RunUntilIdle();
196 212
197 const std::vector<std::string>& permissions = client.GetBlockedPermissions(); 213 const std::vector<std::string>& permissions = client.GetBlockedPermissions();
198 EXPECT_EQ(2ul, permissions.size()); 214 EXPECT_EQ(2ul, permissions.size());
199 EXPECT_EQ("GEOLOCATION", permissions[0]); 215 EXPECT_EQ("GEOLOCATION", permissions[0]);
200 EXPECT_EQ("NOTIFICATIONS", permissions[1]); 216 EXPECT_EQ("NOTIFICATIONS", permissions[1]);
201 } 217 }
202 218
219 TEST_F(SafeBrowsingDatabaseManagerTest, CancelApiCheck) {
220 TestClient client;
221 const GURL url("https://www.example.com/more");
222 TestV4GetHashProtocolManager* pm = static_cast<TestV4GetHashProtocolManager*>(
223 db_manager_->v4_get_hash_protocol_manager_);
224 SBFullHashResult full_hash_result;
225 full_hash_result.hash = SBFullHashForString("example.com/");
226 full_hash_result.metadata.api_permissions.push_back("GEOLOCATION");
227 pm->AddGetFullHashResponse(full_hash_result);
228 pm->SetDelaySeconds(100);
229
230 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
231 EXPECT_TRUE(db_manager_->CancelApiCheck(&client));
232 base::RunLoop().RunUntilIdle();
233
234 const std::vector<std::string>& permissions = client.GetBlockedPermissions();
235 EXPECT_EQ(0ul, permissions.size());
Nathan Parker 2016/04/19 18:05:19 Does this verify that the callback was invoked?
kcarattini 2016/04/20 01:55:53 No. How would I do that?
Nathan Parker 2016/04/20 03:03:35 You could do something like set a "was_callback_in
kcarattini 2016/04/20 04:44:01 Oh, that callback. Done.
236 }
237
203 } // namespace safe_browsing 238 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698