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/two_phase_uploader.h"
7 #include "base/files/file_path.h"
8 #include "base/message_loop/message_loop.h"
9 #include "chrome/browser/safe_browsing/local_two_phase_testserver.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "content/public/test/test_utils.h"
12 #include "net/base/net_errors.h"
13 #include "net/url_request/url_fetcher.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using content::BrowserThread
;
18 using content::MessageLoopRunner
;
24 Delegate() : state_(TwoPhaseUploader::STATE_NONE
) {
27 void ProgressCallback(int64 current
, int64 total
) {}
29 void FinishCallback(scoped_refptr
<MessageLoopRunner
> runner
,
30 TwoPhaseUploader::State state
,
33 const std::string
& response
) {
35 net_error_
= net_error
;
36 response_code_
= response_code
;
41 TwoPhaseUploader::State state_
;
44 std::string response_
;
47 base::FilePath
GetTestFilePath() {
48 base::FilePath file_path
;
49 PathService::Get(base::DIR_SOURCE_ROOT
, &file_path
);
50 file_path
= file_path
.Append(FILE_PATH_LITERAL("net"));
51 file_path
= file_path
.Append(FILE_PATH_LITERAL("data"));
52 file_path
= file_path
.Append(FILE_PATH_LITERAL("url_request_unittest"));
53 file_path
= file_path
.Append(FILE_PATH_LITERAL("BullRunSpeech.txt"));
59 class TwoPhaseUploaderTest
: public testing::Test
{
61 TwoPhaseUploaderTest()
62 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
63 url_request_context_getter_(new net::TestURLRequestContextGetter(
64 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO
))) {
68 content::TestBrowserThreadBundle thread_bundle_
;
70 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
73 TEST_F(TwoPhaseUploaderTest
, UploadFile
) {
74 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner
;
75 LocalTwoPhaseTestServer test_server
;
76 ASSERT_TRUE(test_server
.Start());
78 scoped_ptr
<TwoPhaseUploader
> uploader(TwoPhaseUploader::Create(
79 url_request_context_getter_
.get(),
80 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
81 test_server
.GetURL("start"),
84 base::Bind(&Delegate::ProgressCallback
, base::Unretained(&delegate
)),
86 &Delegate::FinishCallback
, base::Unretained(&delegate
), runner
)));
89 EXPECT_EQ(TwoPhaseUploader::STATE_SUCCESS
, delegate
.state_
);
90 EXPECT_EQ(net::OK
, delegate
.net_error_
);
91 EXPECT_EQ(200, delegate
.response_code_
);
93 "/start\n" // path of start request
94 "4c24b2612e94e2ae622e54397663f2b7bf0a2e17\n" // sha1sum of "metadata"
95 "944857cc626f2cafe232521986b4c6d3f9993c97\n", // sha1sum of test file
99 TEST_F(TwoPhaseUploaderTest
, BadPhaseOneResponse
) {
100 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner
;
101 LocalTwoPhaseTestServer test_server
;
102 ASSERT_TRUE(test_server
.Start());
104 scoped_ptr
<TwoPhaseUploader
> uploader(TwoPhaseUploader::Create(
105 url_request_context_getter_
.get(),
106 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
107 test_server
.GetURL("start?p1code=500"),
110 base::Bind(&Delegate::ProgressCallback
, base::Unretained(&delegate
)),
112 &Delegate::FinishCallback
, base::Unretained(&delegate
), runner
)));
115 EXPECT_EQ(TwoPhaseUploader::UPLOAD_METADATA
, delegate
.state_
);
116 EXPECT_EQ(net::OK
, delegate
.net_error_
);
117 EXPECT_EQ(500, delegate
.response_code_
);
118 EXPECT_EQ("", delegate
.response_
);
121 TEST_F(TwoPhaseUploaderTest
, BadPhaseTwoResponse
) {
122 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner
;
123 LocalTwoPhaseTestServer test_server
;
124 ASSERT_TRUE(test_server
.Start());
126 scoped_ptr
<TwoPhaseUploader
> uploader(TwoPhaseUploader::Create(
127 url_request_context_getter_
.get(),
128 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
129 test_server
.GetURL("start?p2code=500"),
132 base::Bind(&Delegate::ProgressCallback
, base::Unretained(&delegate
)),
134 &Delegate::FinishCallback
, base::Unretained(&delegate
), runner
)));
137 EXPECT_EQ(TwoPhaseUploader::UPLOAD_FILE
, delegate
.state_
);
138 EXPECT_EQ(net::OK
, delegate
.net_error_
);
139 EXPECT_EQ(500, delegate
.response_code_
);
141 "/start\n" // path of start request
142 "4c24b2612e94e2ae622e54397663f2b7bf0a2e17\n" // sha1sum of "metadata"
143 "944857cc626f2cafe232521986b4c6d3f9993c97\n", // sha1sum of test file
147 TEST_F(TwoPhaseUploaderTest
, PhaseOneConnectionClosed
) {
148 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner
;
149 LocalTwoPhaseTestServer test_server
;
150 ASSERT_TRUE(test_server
.Start());
152 scoped_ptr
<TwoPhaseUploader
> uploader(TwoPhaseUploader::Create(
153 url_request_context_getter_
.get(),
154 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
155 test_server
.GetURL("start?p1close=1"),
158 base::Bind(&Delegate::ProgressCallback
, base::Unretained(&delegate
)),
160 &Delegate::FinishCallback
, base::Unretained(&delegate
), runner
)));
163 EXPECT_EQ(TwoPhaseUploader::UPLOAD_METADATA
, delegate
.state_
);
164 EXPECT_EQ(net::ERR_EMPTY_RESPONSE
, delegate
.net_error_
);
165 EXPECT_EQ(net::URLFetcher::RESPONSE_CODE_INVALID
, delegate
.response_code_
);
166 EXPECT_EQ("", delegate
.response_
);
169 TEST_F(TwoPhaseUploaderTest
, PhaseTwoConnectionClosed
) {
170 scoped_refptr
<MessageLoopRunner
> runner
= new MessageLoopRunner
;
171 LocalTwoPhaseTestServer test_server
;
172 ASSERT_TRUE(test_server
.Start());
174 scoped_ptr
<TwoPhaseUploader
> uploader(TwoPhaseUploader::Create(
175 url_request_context_getter_
.get(),
176 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB
).get(),
177 test_server
.GetURL("start?p2close=1"),
180 base::Bind(&Delegate::ProgressCallback
, base::Unretained(&delegate
)),
182 &Delegate::FinishCallback
, base::Unretained(&delegate
), runner
)));
185 EXPECT_EQ(TwoPhaseUploader::UPLOAD_FILE
, delegate
.state_
);
186 EXPECT_EQ(net::ERR_EMPTY_RESPONSE
, delegate
.net_error_
);
187 EXPECT_EQ(net::URLFetcher::RESPONSE_CODE_INVALID
, delegate
.response_code_
);
188 EXPECT_EQ("", delegate
.response_
);