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

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

Issue 8345033: Collect some histograms about signed binary downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 9 years, 2 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "chrome/browser/safe_browsing/download_protection_service.h" 5 #include "chrome/browser/safe_browsing/download_protection_service.h"
6 6
7 #include <map> 7 #include <map>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/callback.h" 11 #include "base/callback.h"
12 #include "base/file_path.h"
12 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
14 #include "base/message_loop.h" 15 #include "base/message_loop.h"
15 #include "chrome/common/safe_browsing/csd.pb.h" 16 #include "chrome/common/safe_browsing/csd.pb.h"
16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" 17 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
17 #include "content/browser/browser_thread.h" 18 #include "content/browser/browser_thread.h"
18 #include "content/common/net/url_fetcher.h" 19 #include "content/common/net/url_fetcher.h"
19 #include "content/test/test_url_fetcher_factory.h" 20 #include "content/test/test_url_fetcher_factory.h"
20 #include "googleurl/src/gurl.h" 21 #include "googleurl/src/gurl.h"
21 #include "testing/gmock/include/gmock/gmock.h" 22 #include "testing/gmock/include/gmock/gmock.h"
(...skipping 14 matching lines...) Expand all
36 private: 37 private:
37 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingService); 38 DISALLOW_COPY_AND_ASSIGN(MockSafeBrowsingService);
38 }; 39 };
39 } // namespace 40 } // namespace
40 41
41 class DownloadProtectionServiceTest : public testing::Test { 42 class DownloadProtectionServiceTest : public testing::Test {
42 protected: 43 protected:
43 virtual void SetUp() { 44 virtual void SetUp() {
44 ui_thread_.reset(new BrowserThread(BrowserThread::UI, &msg_loop_)); 45 ui_thread_.reset(new BrowserThread(BrowserThread::UI, &msg_loop_));
45 io_thread_.reset(new BrowserThread(BrowserThread::IO, &msg_loop_)); 46 io_thread_.reset(new BrowserThread(BrowserThread::IO, &msg_loop_));
47 file_thread_.reset(new BrowserThread(BrowserThread::FILE, &msg_loop_));
46 sb_service_ = new MockSafeBrowsingService(); 48 sb_service_ = new MockSafeBrowsingService();
47 download_service_ = new DownloadProtectionService(sb_service_.get(), 49 download_service_ = sb_service_->download_protection_service();
48 NULL);
49 download_service_->SetEnabled(true); 50 download_service_->SetEnabled(true);
50 msg_loop_.RunAllPending(); 51 msg_loop_.RunAllPending();
51 } 52 }
52 53
53 virtual void TearDown() { 54 virtual void TearDown() {
54 msg_loop_.RunAllPending(); 55 msg_loop_.RunAllPending();
55 download_service_ = NULL;
56 sb_service_ = NULL; 56 sb_service_ = NULL;
57 // Allow the DownloadProtectionService to be deleted.
58 msg_loop_.RunAllPending();
57 io_thread_.reset(); 59 io_thread_.reset();
60 file_thread_.reset();
58 ui_thread_.reset(); 61 ui_thread_.reset();
59 } 62 }
60 63
61 bool RequestContainsResource(const ClientDownloadRequest& request, 64 bool RequestContainsResource(const ClientDownloadRequest& request,
62 ClientDownloadRequest::ResourceType type, 65 ClientDownloadRequest::ResourceType type,
63 const std::string& url, 66 const std::string& url,
64 const std::string& referrer) { 67 const std::string& referrer) {
65 for (int i = 0; i < request.resources_size(); ++i) { 68 for (int i = 0; i < request.resources_size(); ++i) {
66 if (request.resources(i).url() == url && 69 if (request.resources(i).url() == url &&
67 request.resources(i).type() == type && 70 request.resources(i).type() == type &&
68 (referrer.empty() || request.resources(i).referrer() == referrer)) { 71 (referrer.empty() || request.resources(i).referrer() == referrer)) {
69 return true; 72 return true;
70 } 73 }
71 } 74 }
72 return false; 75 return false;
73 } 76 }
74 77
75 public: 78 public:
76 void CheckDoneCallback( 79 void CheckDoneCallback(
77 DownloadProtectionService::DownloadCheckResult result) { 80 DownloadProtectionService::DownloadCheckResult result) {
78 result_ = result; 81 result_ = result;
79 msg_loop_.Quit(); 82 msg_loop_.Quit();
80 } 83 }
81 84
82 protected: 85 protected:
83 scoped_refptr<MockSafeBrowsingService> sb_service_; 86 scoped_refptr<MockSafeBrowsingService> sb_service_;
84 scoped_refptr<DownloadProtectionService> download_service_; 87 DownloadProtectionService* download_service_;
85 MessageLoop msg_loop_; 88 MessageLoop msg_loop_;
86 DownloadProtectionService::DownloadCheckResult result_; 89 DownloadProtectionService::DownloadCheckResult result_;
87 scoped_ptr<BrowserThread> io_thread_; 90 scoped_ptr<BrowserThread> io_thread_;
91 scoped_ptr<BrowserThread> file_thread_;
88 scoped_ptr<BrowserThread> ui_thread_; 92 scoped_ptr<BrowserThread> ui_thread_;
89 }; 93 };
90 94
91 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) { 95 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadInvalidUrl) {
92 DownloadProtectionService::DownloadInfo info; 96 DownloadProtectionService::DownloadInfo info;
93 EXPECT_TRUE(download_service_->CheckClientDownload( 97 EXPECT_TRUE(download_service_->CheckClientDownload(
94 info, 98 info,
95 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 99 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
96 base::Unretained(this)))); 100 base::Unretained(this))));
97 // Only http is supported for now. 101 // Only http is supported for now.
102 info.local_file = FilePath(FILE_PATH_LITERAL("a.exe"));
98 info.download_url_chain.push_back(GURL("https://www.google.com/")); 103 info.download_url_chain.push_back(GURL("https://www.google.com/"));
99 EXPECT_TRUE(download_service_->CheckClientDownload( 104 EXPECT_TRUE(download_service_->CheckClientDownload(
100 info, 105 info,
101 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 106 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
102 base::Unretained(this)))); 107 base::Unretained(this))));
103 info.download_url_chain[0] = GURL("ftp://www.google.com/"); 108 info.download_url_chain[0] = GURL("ftp://www.google.com/");
104 EXPECT_TRUE(download_service_->CheckClientDownload( 109 EXPECT_TRUE(download_service_->CheckClientDownload(
105 info, 110 info,
106 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 111 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
107 base::Unretained(this)))); 112 base::Unretained(this))));
108 } 113 }
109 114
110 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) { 115 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadWhitelistedUrl) {
111 DownloadProtectionService::DownloadInfo info; 116 DownloadProtectionService::DownloadInfo info;
117 info.local_file = FilePath(FILE_PATH_LITERAL("a.exe"));
112 info.download_url_chain.push_back(GURL("http://www.evil.com/bla.exe")); 118 info.download_url_chain.push_back(GURL("http://www.evil.com/bla.exe"));
113 info.download_url_chain.push_back(GURL("http://www.google.com/a.exe")); 119 info.download_url_chain.push_back(GURL("http://www.google.com/a.exe"));
114 info.referrer_url = GURL("http://www.google.com/"); 120 info.referrer_url = GURL("http://www.google.com/");
115 121
116 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) 122 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_))
117 .WillRepeatedly(Return(false)); 123 .WillRepeatedly(Return(false));
118 EXPECT_CALL(*sb_service_, 124 EXPECT_CALL(*sb_service_,
119 MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe"))) 125 MatchDownloadWhitelistUrl(GURL("http://www.google.com/a.exe")))
120 .WillRepeatedly(Return(true)); 126 .WillRepeatedly(Return(true));
121 127
(...skipping 18 matching lines...) Expand all
140 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) { 146 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadFetchFailed) {
141 FakeURLFetcherFactory factory; 147 FakeURLFetcherFactory factory;
142 // HTTP request will fail. 148 // HTTP request will fail.
143 factory.SetFakeResponse( 149 factory.SetFakeResponse(
144 DownloadProtectionService::kDownloadRequestUrl, "", false); 150 DownloadProtectionService::kDownloadRequestUrl, "", false);
145 151
146 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) 152 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_))
147 .WillRepeatedly(Return(false)); 153 .WillRepeatedly(Return(false));
148 154
149 DownloadProtectionService::DownloadInfo info; 155 DownloadProtectionService::DownloadInfo info;
156 info.local_file = FilePath(FILE_PATH_LITERAL("a.exe"));
150 info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe")); 157 info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe"));
151 info.referrer_url = GURL("http://www.google.com/"); 158 info.referrer_url = GURL("http://www.google.com/");
152 EXPECT_FALSE(download_service_->CheckClientDownload( 159 EXPECT_FALSE(download_service_->CheckClientDownload(
153 info, 160 info,
154 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 161 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
155 base::Unretained(this)))); 162 base::Unretained(this))));
156 msg_loop_.Run(); 163 msg_loop_.Run();
157 EXPECT_EQ(DownloadProtectionService::SAFE, result_); 164 EXPECT_EQ(DownloadProtectionService::SAFE, result_);
158 } 165 }
159 166
160 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSuccess) { 167 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadSuccess) {
161 FakeURLFetcherFactory factory; 168 FakeURLFetcherFactory factory;
162 // Empty response means SAFE. 169 // Empty response means SAFE.
163 factory.SetFakeResponse( 170 factory.SetFakeResponse(
164 DownloadProtectionService::kDownloadRequestUrl, "", true); 171 DownloadProtectionService::kDownloadRequestUrl, "", true);
165 172
166 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) 173 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_))
167 .WillRepeatedly(Return(false)); 174 .WillRepeatedly(Return(false));
168 175
169 DownloadProtectionService::DownloadInfo info; 176 DownloadProtectionService::DownloadInfo info;
177 info.local_file = FilePath(FILE_PATH_LITERAL("a.exe"));
170 info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe")); 178 info.download_url_chain.push_back(GURL("http://www.evil.com/a.exe"));
171 info.referrer_url = GURL("http://www.google.com/"); 179 info.referrer_url = GURL("http://www.google.com/");
172 EXPECT_FALSE(download_service_->CheckClientDownload( 180 EXPECT_FALSE(download_service_->CheckClientDownload(
173 info, 181 info,
174 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 182 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
175 base::Unretained(this)))); 183 base::Unretained(this))));
176 msg_loop_.Run(); 184 msg_loop_.Run();
177 EXPECT_EQ(DownloadProtectionService::SAFE, result_); 185 EXPECT_EQ(DownloadProtectionService::SAFE, result_);
178 186
179 // Invalid response should be safe too. 187 // Invalid response should be safe too.
180 factory.SetFakeResponse( 188 factory.SetFakeResponse(
181 DownloadProtectionService::kDownloadRequestUrl, "bla", true); 189 DownloadProtectionService::kDownloadRequestUrl, "bla", true);
182 190
183 EXPECT_FALSE(download_service_->CheckClientDownload( 191 EXPECT_FALSE(download_service_->CheckClientDownload(
184 info, 192 info,
185 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback, 193 base::Bind(&DownloadProtectionServiceTest::CheckDoneCallback,
186 base::Unretained(this)))); 194 base::Unretained(this))));
187 msg_loop_.Run(); 195 msg_loop_.Run();
188 EXPECT_EQ(DownloadProtectionService::SAFE, result_); 196 EXPECT_EQ(DownloadProtectionService::SAFE, result_);
189 } 197 }
190 198
191 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadValidateRequest) { 199 TEST_F(DownloadProtectionServiceTest, CheckClientDownloadValidateRequest) {
192 TestURLFetcherFactory factory; 200 TestURLFetcherFactory factory;
193 201
194 DownloadProtectionService::DownloadInfo info; 202 DownloadProtectionService::DownloadInfo info;
203 info.local_file = FilePath(FILE_PATH_LITERAL("bla.exe"));
195 info.download_url_chain.push_back(GURL("http://www.google.com/")); 204 info.download_url_chain.push_back(GURL("http://www.google.com/"));
196 info.download_url_chain.push_back(GURL("http://www.google.com/bla.exe")); 205 info.download_url_chain.push_back(GURL("http://www.google.com/bla.exe"));
197 info.referrer_url = GURL("http://www.google.com/"); 206 info.referrer_url = GURL("http://www.google.com/");
198 info.sha256_hash = "hash"; 207 info.sha256_hash = "hash";
199 info.total_bytes = 100; 208 info.total_bytes = 100;
200 info.user_initiated = false; 209 info.user_initiated = false;
201 210
202 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_)) 211 EXPECT_CALL(*sb_service_, MatchDownloadWhitelistUrl(_))
203 .WillRepeatedly(Return(false)); 212 .WillRepeatedly(Return(false));
204 EXPECT_FALSE(download_service_->CheckClientDownload( 213 EXPECT_FALSE(download_service_->CheckClientDownload(
(...skipping 13 matching lines...) Expand all
218 EXPECT_EQ(2, request.resources_size()); 227 EXPECT_EQ(2, request.resources_size());
219 EXPECT_TRUE(RequestContainsResource(request, 228 EXPECT_TRUE(RequestContainsResource(request,
220 ClientDownloadRequest::DOWNLOAD_REDIRECT, 229 ClientDownloadRequest::DOWNLOAD_REDIRECT,
221 "http://www.google.com/", "")); 230 "http://www.google.com/", ""));
222 EXPECT_TRUE(RequestContainsResource(request, 231 EXPECT_TRUE(RequestContainsResource(request,
223 ClientDownloadRequest::DOWNLOAD_URL, 232 ClientDownloadRequest::DOWNLOAD_URL,
224 "http://www.google.com/bla.exe", 233 "http://www.google.com/bla.exe",
225 info.referrer_url.spec())); 234 info.referrer_url.spec()));
226 } 235 }
227 } // namespace safe_browsing 236 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698