| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/two_phase_uploader.h" | 5 #include "chrome/browser/safe_browsing/two_phase_uploader.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 content::TestBrowserThreadBundle thread_bundle_; | 72 content::TestBrowserThreadBundle thread_bundle_; |
| 73 | 73 |
| 74 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; | 74 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 TEST_F(TwoPhaseUploaderTest, UploadFile) { | 77 TEST_F(TwoPhaseUploaderTest, UploadFile) { |
| 78 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; | 78 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; |
| 79 LocalTwoPhaseTestServer test_server; | 79 LocalTwoPhaseTestServer test_server; |
| 80 ASSERT_TRUE(test_server.Start()); | 80 ASSERT_TRUE(test_server.Start()); |
| 81 Delegate delegate; | 81 Delegate delegate; |
| 82 scoped_ptr<TwoPhaseUploader> uploader(TwoPhaseUploader::Create( | 82 std::unique_ptr<TwoPhaseUploader> uploader(TwoPhaseUploader::Create( |
| 83 url_request_context_getter_.get(), | 83 url_request_context_getter_.get(), |
| 84 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), | 84 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), |
| 85 test_server.GetURL("start"), | 85 test_server.GetURL("start"), "metadata", GetTestFilePath(), |
| 86 "metadata", | |
| 87 GetTestFilePath(), | |
| 88 base::Bind(&Delegate::ProgressCallback, base::Unretained(&delegate)), | 86 base::Bind(&Delegate::ProgressCallback, base::Unretained(&delegate)), |
| 89 base::Bind( | 87 base::Bind(&Delegate::FinishCallback, base::Unretained(&delegate), |
| 90 &Delegate::FinishCallback, base::Unretained(&delegate), runner))); | 88 runner))); |
| 91 uploader->Start(); | 89 uploader->Start(); |
| 92 runner->Run(); | 90 runner->Run(); |
| 93 EXPECT_EQ(TwoPhaseUploader::STATE_SUCCESS, delegate.state_); | 91 EXPECT_EQ(TwoPhaseUploader::STATE_SUCCESS, delegate.state_); |
| 94 EXPECT_EQ(net::OK, delegate.net_error_); | 92 EXPECT_EQ(net::OK, delegate.net_error_); |
| 95 EXPECT_EQ(200, delegate.response_code_); | 93 EXPECT_EQ(200, delegate.response_code_); |
| 96 EXPECT_EQ( | 94 EXPECT_EQ( |
| 97 "/start\n" // path of start request | 95 "/start\n" // path of start request |
| 98 "4c24b2612e94e2ae622e54397663f2b7bf0a2e17\n" // sha1sum of "metadata" | 96 "4c24b2612e94e2ae622e54397663f2b7bf0a2e17\n" // sha1sum of "metadata" |
| 99 "944857cc626f2cafe232521986b4c6d3f9993c97\n", // sha1sum of test file | 97 "944857cc626f2cafe232521986b4c6d3f9993c97\n", // sha1sum of test file |
| 100 delegate.response_); | 98 delegate.response_); |
| 101 } | 99 } |
| 102 | 100 |
| 103 TEST_F(TwoPhaseUploaderTest, BadPhaseOneResponse) { | 101 TEST_F(TwoPhaseUploaderTest, BadPhaseOneResponse) { |
| 104 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; | 102 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; |
| 105 LocalTwoPhaseTestServer test_server; | 103 LocalTwoPhaseTestServer test_server; |
| 106 ASSERT_TRUE(test_server.Start()); | 104 ASSERT_TRUE(test_server.Start()); |
| 107 Delegate delegate; | 105 Delegate delegate; |
| 108 scoped_ptr<TwoPhaseUploader> uploader(TwoPhaseUploader::Create( | 106 std::unique_ptr<TwoPhaseUploader> uploader(TwoPhaseUploader::Create( |
| 109 url_request_context_getter_.get(), | 107 url_request_context_getter_.get(), |
| 110 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), | 108 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), |
| 111 test_server.GetURL("start?p1code=500"), | 109 test_server.GetURL("start?p1code=500"), "metadata", GetTestFilePath(), |
| 112 "metadata", | |
| 113 GetTestFilePath(), | |
| 114 base::Bind(&Delegate::ProgressCallback, base::Unretained(&delegate)), | 110 base::Bind(&Delegate::ProgressCallback, base::Unretained(&delegate)), |
| 115 base::Bind( | 111 base::Bind(&Delegate::FinishCallback, base::Unretained(&delegate), |
| 116 &Delegate::FinishCallback, base::Unretained(&delegate), runner))); | 112 runner))); |
| 117 uploader->Start(); | 113 uploader->Start(); |
| 118 runner->Run(); | 114 runner->Run(); |
| 119 EXPECT_EQ(TwoPhaseUploader::UPLOAD_METADATA, delegate.state_); | 115 EXPECT_EQ(TwoPhaseUploader::UPLOAD_METADATA, delegate.state_); |
| 120 EXPECT_EQ(net::OK, delegate.net_error_); | 116 EXPECT_EQ(net::OK, delegate.net_error_); |
| 121 EXPECT_EQ(500, delegate.response_code_); | 117 EXPECT_EQ(500, delegate.response_code_); |
| 122 EXPECT_EQ("", delegate.response_); | 118 EXPECT_EQ("", delegate.response_); |
| 123 } | 119 } |
| 124 | 120 |
| 125 TEST_F(TwoPhaseUploaderTest, BadPhaseTwoResponse) { | 121 TEST_F(TwoPhaseUploaderTest, BadPhaseTwoResponse) { |
| 126 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; | 122 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; |
| 127 LocalTwoPhaseTestServer test_server; | 123 LocalTwoPhaseTestServer test_server; |
| 128 ASSERT_TRUE(test_server.Start()); | 124 ASSERT_TRUE(test_server.Start()); |
| 129 Delegate delegate; | 125 Delegate delegate; |
| 130 scoped_ptr<TwoPhaseUploader> uploader(TwoPhaseUploader::Create( | 126 std::unique_ptr<TwoPhaseUploader> uploader(TwoPhaseUploader::Create( |
| 131 url_request_context_getter_.get(), | 127 url_request_context_getter_.get(), |
| 132 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), | 128 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), |
| 133 test_server.GetURL("start?p2code=500"), | 129 test_server.GetURL("start?p2code=500"), "metadata", GetTestFilePath(), |
| 134 "metadata", | |
| 135 GetTestFilePath(), | |
| 136 base::Bind(&Delegate::ProgressCallback, base::Unretained(&delegate)), | 130 base::Bind(&Delegate::ProgressCallback, base::Unretained(&delegate)), |
| 137 base::Bind( | 131 base::Bind(&Delegate::FinishCallback, base::Unretained(&delegate), |
| 138 &Delegate::FinishCallback, base::Unretained(&delegate), runner))); | 132 runner))); |
| 139 uploader->Start(); | 133 uploader->Start(); |
| 140 runner->Run(); | 134 runner->Run(); |
| 141 EXPECT_EQ(TwoPhaseUploader::UPLOAD_FILE, delegate.state_); | 135 EXPECT_EQ(TwoPhaseUploader::UPLOAD_FILE, delegate.state_); |
| 142 EXPECT_EQ(net::OK, delegate.net_error_); | 136 EXPECT_EQ(net::OK, delegate.net_error_); |
| 143 EXPECT_EQ(500, delegate.response_code_); | 137 EXPECT_EQ(500, delegate.response_code_); |
| 144 EXPECT_EQ( | 138 EXPECT_EQ( |
| 145 "/start\n" // path of start request | 139 "/start\n" // path of start request |
| 146 "4c24b2612e94e2ae622e54397663f2b7bf0a2e17\n" // sha1sum of "metadata" | 140 "4c24b2612e94e2ae622e54397663f2b7bf0a2e17\n" // sha1sum of "metadata" |
| 147 "944857cc626f2cafe232521986b4c6d3f9993c97\n", // sha1sum of test file | 141 "944857cc626f2cafe232521986b4c6d3f9993c97\n", // sha1sum of test file |
| 148 delegate.response_); | 142 delegate.response_); |
| 149 } | 143 } |
| 150 | 144 |
| 151 TEST_F(TwoPhaseUploaderTest, PhaseOneConnectionClosed) { | 145 TEST_F(TwoPhaseUploaderTest, PhaseOneConnectionClosed) { |
| 152 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; | 146 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; |
| 153 LocalTwoPhaseTestServer test_server; | 147 LocalTwoPhaseTestServer test_server; |
| 154 ASSERT_TRUE(test_server.Start()); | 148 ASSERT_TRUE(test_server.Start()); |
| 155 Delegate delegate; | 149 Delegate delegate; |
| 156 scoped_ptr<TwoPhaseUploader> uploader(TwoPhaseUploader::Create( | 150 std::unique_ptr<TwoPhaseUploader> uploader(TwoPhaseUploader::Create( |
| 157 url_request_context_getter_.get(), | 151 url_request_context_getter_.get(), |
| 158 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), | 152 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), |
| 159 test_server.GetURL("start?p1close=1"), | 153 test_server.GetURL("start?p1close=1"), "metadata", GetTestFilePath(), |
| 160 "metadata", | |
| 161 GetTestFilePath(), | |
| 162 base::Bind(&Delegate::ProgressCallback, base::Unretained(&delegate)), | 154 base::Bind(&Delegate::ProgressCallback, base::Unretained(&delegate)), |
| 163 base::Bind( | 155 base::Bind(&Delegate::FinishCallback, base::Unretained(&delegate), |
| 164 &Delegate::FinishCallback, base::Unretained(&delegate), runner))); | 156 runner))); |
| 165 uploader->Start(); | 157 uploader->Start(); |
| 166 runner->Run(); | 158 runner->Run(); |
| 167 EXPECT_EQ(TwoPhaseUploader::UPLOAD_METADATA, delegate.state_); | 159 EXPECT_EQ(TwoPhaseUploader::UPLOAD_METADATA, delegate.state_); |
| 168 EXPECT_EQ(net::ERR_EMPTY_RESPONSE, delegate.net_error_); | 160 EXPECT_EQ(net::ERR_EMPTY_RESPONSE, delegate.net_error_); |
| 169 EXPECT_EQ(net::URLFetcher::RESPONSE_CODE_INVALID, delegate.response_code_); | 161 EXPECT_EQ(net::URLFetcher::RESPONSE_CODE_INVALID, delegate.response_code_); |
| 170 EXPECT_EQ("", delegate.response_); | 162 EXPECT_EQ("", delegate.response_); |
| 171 } | 163 } |
| 172 | 164 |
| 173 TEST_F(TwoPhaseUploaderTest, PhaseTwoConnectionClosed) { | 165 TEST_F(TwoPhaseUploaderTest, PhaseTwoConnectionClosed) { |
| 174 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; | 166 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner; |
| 175 LocalTwoPhaseTestServer test_server; | 167 LocalTwoPhaseTestServer test_server; |
| 176 ASSERT_TRUE(test_server.Start()); | 168 ASSERT_TRUE(test_server.Start()); |
| 177 Delegate delegate; | 169 Delegate delegate; |
| 178 scoped_ptr<TwoPhaseUploader> uploader(TwoPhaseUploader::Create( | 170 std::unique_ptr<TwoPhaseUploader> uploader(TwoPhaseUploader::Create( |
| 179 url_request_context_getter_.get(), | 171 url_request_context_getter_.get(), |
| 180 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), | 172 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::DB).get(), |
| 181 test_server.GetURL("start?p2close=1"), | 173 test_server.GetURL("start?p2close=1"), "metadata", GetTestFilePath(), |
| 182 "metadata", | |
| 183 GetTestFilePath(), | |
| 184 base::Bind(&Delegate::ProgressCallback, base::Unretained(&delegate)), | 174 base::Bind(&Delegate::ProgressCallback, base::Unretained(&delegate)), |
| 185 base::Bind( | 175 base::Bind(&Delegate::FinishCallback, base::Unretained(&delegate), |
| 186 &Delegate::FinishCallback, base::Unretained(&delegate), runner))); | 176 runner))); |
| 187 uploader->Start(); | 177 uploader->Start(); |
| 188 runner->Run(); | 178 runner->Run(); |
| 189 EXPECT_EQ(TwoPhaseUploader::UPLOAD_FILE, delegate.state_); | 179 EXPECT_EQ(TwoPhaseUploader::UPLOAD_FILE, delegate.state_); |
| 190 EXPECT_EQ(net::ERR_EMPTY_RESPONSE, delegate.net_error_); | 180 EXPECT_EQ(net::ERR_EMPTY_RESPONSE, delegate.net_error_); |
| 191 EXPECT_EQ(net::URLFetcher::RESPONSE_CODE_INVALID, delegate.response_code_); | 181 EXPECT_EQ(net::URLFetcher::RESPONSE_CODE_INVALID, delegate.response_code_); |
| 192 EXPECT_EQ("", delegate.response_); | 182 EXPECT_EQ("", delegate.response_); |
| 193 } | 183 } |
| 194 | 184 |
| 195 } // namespace safe_browsing | 185 } // namespace safe_browsing |
| OLD | NEW |