1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/safe_browsing/download_feedback.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/message_loop.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "chrome/browser/safe_browsing/two_phase_uploader.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/safe_browsing/csd.pb.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace safe_browsing {
namespace {
class FakeUploader : public TwoPhaseUploader {
public:
FakeUploader(net::URLRequestContextGetter* url_request_context_getter,
base::TaskRunner* file_task_runner,
const GURL& base_url,
const std::string& metadata,
const base::FilePath& file_path,
const ProgressCallback& progress_callback,
const FinishCallback& finish_callback);
virtual ~FakeUploader() {}
virtual void Start() OVERRIDE {
start_called_ = true;
}
scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_;
scoped_refptr<base::TaskRunner> file_task_runner_;
GURL base_url_;
std::string metadata_;
base::FilePath file_path_;
ProgressCallback progress_callback_;
FinishCallback finish_callback_;
bool start_called_;
};
FakeUploader::FakeUploader(
net::URLRequestContextGetter* url_request_context_getter,
base::TaskRunner* file_task_runner,
const GURL& base_url,
const std::string& metadata,
const base::FilePath& file_path,
const ProgressCallback& progress_callback,
const FinishCallback& finish_callback)
: url_request_context_getter_(url_request_context_getter),
file_task_runner_(file_task_runner),
base_url_(base_url),
metadata_(metadata),
file_path_(file_path),
progress_callback_(progress_callback),
finish_callback_(finish_callback),
start_called_(false) {
}
class FakeUploaderFactory : public TwoPhaseUploaderFactory {
public:
FakeUploaderFactory() : uploader_(NULL) {}
virtual ~FakeUploaderFactory() {}
virtual TwoPhaseUploader* CreateTwoPhaseUploader(
net::URLRequestContextGetter* url_request_context_getter,
base::TaskRunner* file_task_runner,
const GURL& base_url,
const std::string& metadata,
const base::FilePath& file_path,
const TwoPhaseUploader::ProgressCallback& progress_callback,
const TwoPhaseUploader::FinishCallback& finish_callback) OVERRIDE;
FakeUploader* uploader_;
};
TwoPhaseUploader* FakeUploaderFactory::CreateTwoPhaseUploader(
net::URLRequestContextGetter* url_request_context_getter,
base::TaskRunner* file_task_runner,
const GURL& base_url,
const std::string& metadata,
const base::FilePath& file_path,
const TwoPhaseUploader::ProgressCallback& progress_callback,
const TwoPhaseUploader::FinishCallback& finish_callback) {
EXPECT_FALSE(uploader_);
uploader_ = new FakeUploader(url_request_context_getter, file_task_runner,
base_url, metadata, file_path, progress_callback,
finish_callback);
return uploader_;
}
} // namespace
class DownloadFeedbackTest : public testing::Test {
public:
DownloadFeedbackTest()
: file_task_runner_(content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::FILE)),
io_task_runner_(content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::IO)),
url_request_context_getter_(
new net::TestURLRequestContextGetter(io_task_runner_)),
feedback_finish_called_(false) {
EXPECT_NE(io_task_runner_, file_task_runner_);
}
virtual void SetUp() OVERRIDE {
ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
upload_file_path_ = temp_dir_.path().AppendASCII("test file");
upload_file_data_ = "data";
ASSERT_EQ(static_cast<int>(upload_file_data_.size()),
file_util::WriteFile(upload_file_path_, upload_file_data_.data(),
upload_file_data_.size()));
TwoPhaseUploader::RegisterFactory(&two_phase_uploader_factory_);
}
virtual void TearDown() OVERRIDE {
TwoPhaseUploader::RegisterFactory(NULL);
}
FakeUploader* uploader() const {
return two_phase_uploader_factory_.uploader_;
}
void FinishCallback(DownloadFeedback* feedback) {
EXPECT_FALSE(feedback_finish_called_);
feedback_finish_called_ = true;
delete feedback;
}
protected:
base::ScopedTempDir temp_dir_;
base::FilePath upload_file_path_;
std::string upload_file_data_;
content::TestBrowserThreadBundle thread_bundle_;
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_;
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
FakeUploaderFactory two_phase_uploader_factory_;
scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
bool feedback_finish_called_;
};
TEST_F(DownloadFeedbackTest, CompleteUpload) {
ClientDownloadReport expected_report_metadata;
expected_report_metadata.mutable_download_request()->set_url("http://test");
expected_report_metadata.mutable_download_request()->set_length(
upload_file_data_.size());
expected_report_metadata.mutable_download_request()->mutable_digests(
)->set_sha1("hi");
expected_report_metadata.mutable_download_response()->set_verdict(
ClientDownloadResponse::DANGEROUS_HOST);
std::string ping_request(
expected_report_metadata.download_request().SerializeAsString());
std::string ping_response(
expected_report_metadata.download_response().SerializeAsString());
const char kTestFeedbackURL[] = "https://example.com/test/upload";
CommandLine::ForCurrentProcess()->AppendSwitchASCII(
switches::kSbDownloadFeedbackURL, kTestFeedbackURL);
DownloadFeedback* feedback = DownloadFeedback::Create(
url_request_context_getter_,
file_task_runner_,
upload_file_path_,
ping_request,
ping_response);
EXPECT_FALSE(uploader());
feedback->Start(base::Bind(&DownloadFeedbackTest::FinishCallback,
base::Unretained(this),
feedback));
ASSERT_TRUE(uploader());
EXPECT_FALSE(feedback_finish_called_);
EXPECT_TRUE(uploader()->start_called_);
EXPECT_EQ(url_request_context_getter_,
uploader()->url_request_context_getter_);
EXPECT_EQ(file_task_runner_, uploader()->file_task_runner_);
EXPECT_EQ(upload_file_path_, uploader()->file_path_);
EXPECT_EQ(expected_report_metadata.SerializeAsString(),
uploader()->metadata_);
EXPECT_EQ(kTestFeedbackURL, uploader()->base_url_.spec());
EXPECT_TRUE(file_util::PathExists(upload_file_path_));
EXPECT_FALSE(feedback_finish_called_);
uploader()->finish_callback_.Run(
TwoPhaseUploader::STATE_SUCCESS, net::OK, 0, "");
EXPECT_TRUE(feedback_finish_called_);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(file_util::PathExists(upload_file_path_));
}
TEST_F(DownloadFeedbackTest, CancelUpload) {
ClientDownloadReport expected_report_metadata;
expected_report_metadata.mutable_download_request()->set_url("http://test");
expected_report_metadata.mutable_download_request()->set_length(
upload_file_data_.size());
expected_report_metadata.mutable_download_request()->mutable_digests(
)->set_sha1("hi");
expected_report_metadata.mutable_download_response()->set_verdict(
ClientDownloadResponse::DANGEROUS_HOST);
std::string ping_request(
expected_report_metadata.download_request().SerializeAsString());
std::string ping_response(
expected_report_metadata.download_response().SerializeAsString());
DownloadFeedback* feedback = DownloadFeedback::Create(
url_request_context_getter_,
file_task_runner_,
upload_file_path_,
ping_request,
ping_response);
EXPECT_FALSE(uploader());
feedback->Start(base::Bind(&DownloadFeedbackTest::FinishCallback,
base::Unretained(this),
feedback));
ASSERT_TRUE(uploader());
EXPECT_FALSE(feedback_finish_called_);
EXPECT_TRUE(uploader()->start_called_);
EXPECT_TRUE(file_util::PathExists(upload_file_path_));
delete feedback;
EXPECT_FALSE(feedback_finish_called_);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(file_util::PathExists(upload_file_path_));
}
} // namespace safe_browsing
|