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

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

Issue 1843383002: Safe Browsing: CheckApiBlacklist request implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Test 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
(Empty)
1 // Copyright 2013 The Chromium Authors. All rights reserved.
Nathan Parker 2016/04/07 21:33:37 2016
kcarattini 2016/04/11 03:34:31 My how time flies! Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stddef.h>
6
7 #include <string>
8 #include <vector>
9
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/run_loop.h"
14 #include "components/safe_browsing_db/database_manager.h"
15 #include "components/safe_browsing_db/test_database_manager.h"
16 #include "components/safe_browsing_db/v4_get_hash_protocol_manager.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "url/gurl.h"
21
22 using content::BrowserThread;
23
24 namespace safe_browsing {
25
26 namespace {
27
28 // A TestV4GetHashProtocolManager that returns fixed responses from the
29 // Safe Browsing server for testing purpose.
30 class TestV4GetHashProtocolManager : public V4GetHashProtocolManager {
31 public:
32 TestV4GetHashProtocolManager(
33 net::URLRequestContextGetter* request_context_getter,
34 const V4ProtocolConfig& config)
35 : V4GetHashProtocolManager(request_context_getter, config) {}
36
37 ~TestV4GetHashProtocolManager() override {}
38
39 void GetFullHashesWithApis(const std::vector<SBPrefix>& prefixes,
40 FullHashCallback callback) override {
41 prefixes_ = prefixes;
42 callback.Run(full_hashes_, base::TimeDelta::FromMinutes(0));
Nathan Parker 2016/04/07 21:33:38 You probably dont' need to call the callback, sinc
kcarattini 2016/04/11 03:34:31 I'd like to call it for cleanliness -- it deletes
43 }
44
45 // Prepare the GetFullHash results for the next request.
46 void AddGetFullHashResponse(const SBFullHashResult& full_hash_result) {
47 full_hashes_.push_back(full_hash_result);
48 }
49
50 // Returns the prefixes that were sent in the last request.
51 const std::vector<SBPrefix>& GetRequestPrefixes() { return prefixes_; }
52
53 private:
54 std::vector<SBPrefix> prefixes_;
55 std::vector<SBFullHashResult> full_hashes_;
56 };
57
58 // Factory that creates test protocol manager instances.
59 class TestV4GetHashProtocolManagerFactory :
60 public V4GetHashProtocolManagerFactory {
61 public:
62 TestV4GetHashProtocolManagerFactory() : pm_(NULL) {}
63 ~TestV4GetHashProtocolManagerFactory() override {}
64
65 V4GetHashProtocolManager* CreateProtocolManager(
66 net::URLRequestContextGetter* request_context_getter,
67 const V4ProtocolConfig& config) override {
68 pm_ = new TestV4GetHashProtocolManager(request_context_getter, config);
69 return pm_;
70 }
71
72 TestV4GetHashProtocolManager* GetProtocolManager() { return pm_; }
Nathan Parker 2016/04/07 21:33:37 A thought: Could callers instead get it from the S
kcarattini 2016/04/11 03:34:31 Removed. Turns out I wasn't using it anyway.
73
74 private:
75 // Owned by the SafeBrowsingDatabaseManager.
76 TestV4GetHashProtocolManager* pm_;
77 };
78
79 class TestClient : public SafeBrowsingDatabaseManager::Client {
80 public:
81 TestClient() {}
82 ~TestClient() override {}
83
84 void OnCheckApiBlacklistUrlResult(const GURL& url,
85 const ThreatMetadata& metadata) override {}
86
87 private:
88 DISALLOW_COPY_AND_ASSIGN(TestClient);
89 };
90
91 } // namespace
92
93 class SafeBrowsingDatabaseManagerTest : public testing::Test {
94 private:
95 content::TestBrowserThreadBundle test_browser_thread_bundle_;
96 };
97
98 TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlWrongScheme) {
99 TestV4GetHashProtocolManagerFactory get_hash_pm_factory;
Nathan Parker 2016/04/07 21:33:37 Could move the first few lines into the test class
kcarattini 2016/04/11 03:34:31 Done.
100 V4GetHashProtocolManager::RegisterFactory(&get_hash_pm_factory);
101
102 scoped_refptr<SafeBrowsingDatabaseManager> db_manager_(
103 new TestSafeBrowsingDatabaseManager());
104 db_manager_->StartOnIOThread(NULL, V4ProtocolConfig());
105
106 TestClient client;
107 const GURL url("file://example.txt");
108 EXPECT_TRUE(db_manager_->CheckApiBlacklistUrl(url, &client));
109 db_manager_->StopOnIOThread(false);
110 }
111
112 TEST_F(SafeBrowsingDatabaseManagerTest, CheckApiBlacklistUrlPrefixes) {
113 TestV4GetHashProtocolManagerFactory get_hash_pm_factory;
114 V4GetHashProtocolManager::RegisterFactory(&get_hash_pm_factory);
115
116 scoped_refptr<SafeBrowsingDatabaseManager> db_manager_(
117 new TestSafeBrowsingDatabaseManager());
118 db_manager_->StartOnIOThread(NULL, V4ProtocolConfig());
119
120 // Calculate hash prefixes
121 TestClient client;
122 const GURL url("https://www.example.com/more");
123 std::vector<SBPrefix> expected_prefixes =
Nathan Parker 2016/04/07 21:33:38 Can you add a comment on how you generated these,
kcarattini 2016/04/11 03:34:31 Done.
124 {1237562338, 2871045197, 3553205461, 3766933875};
125
126 EXPECT_FALSE(db_manager_->CheckApiBlacklistUrl(url, &client));
127 std::vector<SBPrefix> prefixes = static_cast<TestV4GetHashProtocolManager*>(
128 db_manager_->v4_get_hash_protocol_manager_)->GetRequestPrefixes();
129 EXPECT_EQ(expected_prefixes.size(), prefixes.size());
130 for (unsigned int i = 0; i < prefixes.size(); ++i) {
131 EXPECT_EQ(expected_prefixes[i], prefixes[i]);
132 }
133 base::RunLoop().RunUntilIdle();
134 db_manager_->StopOnIOThread(false);
135 }
136
137 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698