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