Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2012 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 17 | #include <netinet/in.h> |
| 18 | #include <netinet/ip.h> |
| 19 | #include <sys/socket.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 21 | |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 22 | #include <algorithm> |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 23 | #include <memory> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 24 | #include <string> |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 25 | #include <utility> |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 26 | #include <vector> |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 27 | |
Alex Deymo | 706a5ab | 2015-11-23 17:48:30 -0300 | [diff] [blame] | 28 | #include <base/bind.h> |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 29 | #include <base/location.h> |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 30 | #include <base/logging.h> |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 31 | #include <base/message_loop/message_loop.h> |
| 32 | #include <base/strings/string_number_conversions.h> |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 33 | #include <base/strings/string_util.h> |
| 34 | #include <base/strings/stringprintf.h> |
| 35 | #include <base/time/time.h> |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 36 | #include <brillo/message_loops/base_message_loop.h> |
| 37 | #include <brillo/message_loops/message_loop.h> |
| 38 | #include <brillo/message_loops/message_loop_utils.h> |
| 39 | #include <brillo/process.h> |
| 40 | #include <brillo/streams/file_stream.h> |
| 41 | #include <brillo/streams/stream.h> |
Andrew de los Reyes | 4516810 | 2010-11-22 11:13:50 -0800 | [diff] [blame] | 42 | #include <gtest/gtest.h> |
| 43 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 44 | #include "update_engine/common/fake_hardware.h" |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 45 | #include "update_engine/common/file_fetcher.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 46 | #include "update_engine/common/http_common.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 47 | #include "update_engine/common/mock_http_fetcher.h" |
Sen Jiang | d8be4b5 | 2018-09-18 16:06:57 -0700 | [diff] [blame] | 48 | #include "update_engine/common/mock_proxy_resolver.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 49 | #include "update_engine/common/multi_range_http_fetcher.h" |
Sen Jiang | d8be4b5 | 2018-09-18 16:06:57 -0700 | [diff] [blame] | 50 | #include "update_engine/common/proxy_resolver.h" |
Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 51 | #include "update_engine/common/test_utils.h" |
| 52 | #include "update_engine/common/utils.h" |
Alex Deymo | 14c0da8 | 2016-07-20 16:45:45 -0700 | [diff] [blame] | 53 | #include "update_engine/libcurl_http_fetcher.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 54 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 55 | using brillo::MessageLoop; |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 56 | using std::make_pair; |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 57 | using std::pair; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 58 | using std::string; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 59 | using std::unique_ptr; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 60 | using std::vector; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 61 | using testing::DoAll; |
| 62 | using testing::Return; |
| 63 | using testing::SaveArg; |
| 64 | using testing::_; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 65 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 66 | namespace { |
| 67 | |
| 68 | const int kBigLength = 100000; |
| 69 | const int kMediumLength = 1000; |
Gilad Arnold | 34bf1ee | 2012-02-09 16:16:02 -0800 | [diff] [blame] | 70 | const int kFlakyTruncateLength = 29000; |
| 71 | const int kFlakySleepEvery = 3; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 72 | const int kFlakySleepSecs = 10; |
| 73 | |
| 74 | } // namespace |
| 75 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 76 | namespace chromeos_update_engine { |
| 77 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 78 | static const char *kUnusedUrl = "unused://unused"; |
| 79 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 80 | static inline string LocalServerUrlForPath(in_port_t port, |
| 81 | const string& path) { |
Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 82 | string port_str = (port ? base::StringPrintf(":%hu", port) : ""); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 83 | return base::StringPrintf("http://127.0.0.1%s%s", port_str.c_str(), |
| 84 | path.c_str()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 87 | // |
| 88 | // Class hierarchy for HTTP server implementations. |
| 89 | // |
| 90 | |
| 91 | class HttpServer { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 92 | public: |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 93 | // This makes it an abstract class (dirty but works). |
| 94 | virtual ~HttpServer() = 0; |
| 95 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 96 | virtual in_port_t GetPort() const { |
| 97 | return 0; |
| 98 | } |
| 99 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 100 | bool started_; |
| 101 | }; |
| 102 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 103 | HttpServer::~HttpServer() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 104 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 105 | |
| 106 | class NullHttpServer : public HttpServer { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 107 | public: |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 108 | NullHttpServer() { |
| 109 | started_ = true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 110 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 113 | |
| 114 | class PythonHttpServer : public HttpServer { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 115 | public: |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 116 | PythonHttpServer() : port_(0) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 117 | started_ = false; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 118 | |
| 119 | // Spawn the server process. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 120 | unique_ptr<brillo::Process> http_server(new brillo::ProcessImpl()); |
Sen Jiang | 260f03b | 2016-03-21 15:34:58 -0700 | [diff] [blame] | 121 | http_server->AddArg(test_utils::GetBuildArtifactsPath("test_http_server")); |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 122 | http_server->RedirectUsingPipe(STDOUT_FILENO, false); |
| 123 | |
| 124 | if (!http_server->Start()) { |
| 125 | ADD_FAILURE() << "failed to spawn http server process"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 126 | return; |
| 127 | } |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 128 | LOG(INFO) << "started http server with pid " << http_server->pid(); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 129 | |
| 130 | // Wait for server to begin accepting connections, obtain its port. |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 131 | brillo::StreamPtr stdout = brillo::FileStream::FromFileDescriptor( |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 132 | http_server->GetPipe(STDOUT_FILENO), false /* own */, nullptr); |
| 133 | if (!stdout) |
| 134 | return; |
| 135 | |
| 136 | vector<char> buf(128); |
| 137 | string line; |
| 138 | while (line.find('\n') == string::npos) { |
| 139 | size_t read; |
| 140 | if (!stdout->ReadBlocking(buf.data(), buf.size(), &read, nullptr)) { |
| 141 | ADD_FAILURE() << "error reading http server stdout"; |
| 142 | return; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 143 | } |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 144 | line.append(buf.data(), read); |
| 145 | if (read == 0) |
| 146 | break; |
| 147 | } |
| 148 | // Parse the port from the output line. |
| 149 | const size_t listening_msg_prefix_len = strlen(kServerListeningMsgPrefix); |
| 150 | if (line.size() < listening_msg_prefix_len) { |
| 151 | ADD_FAILURE() << "server output too short"; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 155 | EXPECT_EQ(kServerListeningMsgPrefix, |
| 156 | line.substr(0, listening_msg_prefix_len)); |
| 157 | string port_str = line.substr(listening_msg_prefix_len); |
| 158 | port_str.resize(port_str.find('\n')); |
| 159 | EXPECT_TRUE(base::StringToUint(port_str, &port_)); |
| 160 | |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 161 | started_ = true; |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 162 | LOG(INFO) << "server running, listening on port " << port_; |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 163 | |
| 164 | // Any failure before this point will SIGKILL the test server if started |
| 165 | // when the |http_server| goes out of scope. |
| 166 | http_server_ = std::move(http_server); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 167 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 168 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 169 | ~PythonHttpServer() { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 170 | // If there's no process, do nothing. |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 171 | if (!http_server_) |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 172 | return; |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 173 | // Wait up to 10 seconds for the process to finish. Destroying the process |
| 174 | // will kill it with a SIGKILL otherwise. |
| 175 | http_server_->Kill(SIGTERM, 10); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 176 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 177 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 178 | in_port_t GetPort() const override { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 179 | return port_; |
| 180 | } |
| 181 | |
| 182 | private: |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 183 | static const char* kServerListeningMsgPrefix; |
| 184 | |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 185 | unique_ptr<brillo::Process> http_server_; |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 186 | unsigned int port_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 187 | }; |
| 188 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 189 | const char* PythonHttpServer::kServerListeningMsgPrefix = "listening on port "; |
| 190 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 191 | // |
| 192 | // Class hierarchy for HTTP fetcher test wrappers. |
| 193 | // |
| 194 | |
| 195 | class AnyHttpFetcherTest { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 196 | public: |
Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 197 | AnyHttpFetcherTest() {} |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 198 | virtual ~AnyHttpFetcherTest() {} |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 199 | |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 200 | virtual HttpFetcher* NewLargeFetcher(ProxyResolver* proxy_resolver) = 0; |
| 201 | HttpFetcher* NewLargeFetcher(size_t num_proxies) { |
| 202 | proxy_resolver_.set_num_proxies(num_proxies); |
| 203 | return NewLargeFetcher(&proxy_resolver_); |
| 204 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 205 | HttpFetcher* NewLargeFetcher() { |
| 206 | return NewLargeFetcher(1); |
| 207 | } |
| 208 | |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 209 | virtual HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) = 0; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 210 | HttpFetcher* NewSmallFetcher() { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 211 | proxy_resolver_.set_num_proxies(1); |
| 212 | return NewSmallFetcher(&proxy_resolver_); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 215 | virtual string BigUrl(in_port_t port) const { return kUnusedUrl; } |
| 216 | virtual string SmallUrl(in_port_t port) const { return kUnusedUrl; } |
| 217 | virtual string ErrorUrl(in_port_t port) const { return kUnusedUrl; } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 218 | |
| 219 | virtual bool IsMock() const = 0; |
| 220 | virtual bool IsMulti() const = 0; |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 221 | virtual bool IsHttpSupported() const = 0; |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 222 | virtual bool IsFileFetcher() const = 0; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 223 | |
| 224 | virtual void IgnoreServerAborting(HttpServer* server) const {} |
| 225 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 226 | virtual HttpServer* CreateServer() = 0; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 227 | |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 228 | FakeHardware* fake_hardware() { |
Alex Deymo | 706a5ab | 2015-11-23 17:48:30 -0300 | [diff] [blame] | 229 | return &fake_hardware_; |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 230 | } |
| 231 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 232 | protected: |
| 233 | DirectProxyResolver proxy_resolver_; |
Alex Deymo | 706a5ab | 2015-11-23 17:48:30 -0300 | [diff] [blame] | 234 | FakeHardware fake_hardware_; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | class MockHttpFetcherTest : public AnyHttpFetcherTest { |
| 238 | public: |
| 239 | // Necessary to unhide the definition in the base class. |
| 240 | using AnyHttpFetcherTest::NewLargeFetcher; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 241 | HttpFetcher* NewLargeFetcher(ProxyResolver* proxy_resolver) override { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 242 | brillo::Blob big_data(1000000); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 243 | return new MockHttpFetcher( |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 244 | big_data.data(), big_data.size(), proxy_resolver); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Necessary to unhide the definition in the base class. |
| 248 | using AnyHttpFetcherTest::NewSmallFetcher; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 249 | HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) override { |
| 250 | return new MockHttpFetcher("x", 1, proxy_resolver); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 253 | bool IsMock() const override { return true; } |
| 254 | bool IsMulti() const override { return false; } |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 255 | bool IsHttpSupported() const override { return true; } |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 256 | bool IsFileFetcher() const override { return false; } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 257 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 258 | HttpServer* CreateServer() override { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 259 | return new NullHttpServer; |
| 260 | } |
| 261 | }; |
| 262 | |
| 263 | class LibcurlHttpFetcherTest : public AnyHttpFetcherTest { |
| 264 | public: |
| 265 | // Necessary to unhide the definition in the base class. |
| 266 | using AnyHttpFetcherTest::NewLargeFetcher; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 267 | HttpFetcher* NewLargeFetcher(ProxyResolver* proxy_resolver) override { |
| 268 | LibcurlHttpFetcher* ret = |
| 269 | new LibcurlHttpFetcher(proxy_resolver, &fake_hardware_); |
Darin Petkov | b83371f | 2010-08-17 09:34:49 -0700 | [diff] [blame] | 270 | // Speed up test execution. |
| 271 | ret->set_idle_seconds(1); |
| 272 | ret->set_retry_seconds(1); |
Alex Deymo | 706a5ab | 2015-11-23 17:48:30 -0300 | [diff] [blame] | 273 | fake_hardware_.SetIsOfficialBuild(false); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 274 | return ret; |
| 275 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 276 | |
| 277 | // Necessary to unhide the definition in the base class. |
| 278 | using AnyHttpFetcherTest::NewSmallFetcher; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 279 | HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) override { |
| 280 | return NewLargeFetcher(proxy_resolver); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 281 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 282 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 283 | string BigUrl(in_port_t port) const override { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 284 | return LocalServerUrlForPath(port, |
| 285 | base::StringPrintf("/download/%d", |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 286 | kBigLength)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 287 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 288 | string SmallUrl(in_port_t port) const override { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 289 | return LocalServerUrlForPath(port, "/foo"); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 290 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 291 | string ErrorUrl(in_port_t port) const override { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 292 | return LocalServerUrlForPath(port, "/error"); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 293 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 294 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 295 | bool IsMock() const override { return false; } |
| 296 | bool IsMulti() const override { return false; } |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 297 | bool IsHttpSupported() const override { return true; } |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 298 | bool IsFileFetcher() const override { return false; } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 299 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 300 | void IgnoreServerAborting(HttpServer* server) const override { |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 301 | // Nothing to do. |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 302 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 303 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 304 | HttpServer* CreateServer() override { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 305 | return new PythonHttpServer; |
| 306 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 307 | }; |
| 308 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 309 | class MultiRangeHttpFetcherTest : public LibcurlHttpFetcherTest { |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 310 | public: |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 311 | // Necessary to unhide the definition in the base class. |
| 312 | using AnyHttpFetcherTest::NewLargeFetcher; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 313 | HttpFetcher* NewLargeFetcher(ProxyResolver* proxy_resolver) override { |
| 314 | MultiRangeHttpFetcher* ret = new MultiRangeHttpFetcher( |
| 315 | new LibcurlHttpFetcher(proxy_resolver, &fake_hardware_)); |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 316 | ret->ClearRanges(); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 317 | ret->AddRange(0); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 318 | // Speed up test execution. |
| 319 | ret->set_idle_seconds(1); |
| 320 | ret->set_retry_seconds(1); |
Alex Deymo | 706a5ab | 2015-11-23 17:48:30 -0300 | [diff] [blame] | 321 | fake_hardware_.SetIsOfficialBuild(false); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 322 | return ret; |
| 323 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 324 | |
| 325 | // Necessary to unhide the definition in the base class. |
| 326 | using AnyHttpFetcherTest::NewSmallFetcher; |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 327 | HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) override { |
| 328 | return NewLargeFetcher(proxy_resolver); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 331 | bool IsMulti() const override { return true; } |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 332 | }; |
| 333 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 334 | class FileFetcherTest : public AnyHttpFetcherTest { |
| 335 | public: |
| 336 | // Necessary to unhide the definition in the base class. |
| 337 | using AnyHttpFetcherTest::NewLargeFetcher; |
| 338 | HttpFetcher* NewLargeFetcher(ProxyResolver* /* proxy_resolver */) override { |
| 339 | return new FileFetcher(); |
| 340 | } |
| 341 | |
| 342 | // Necessary to unhide the definition in the base class. |
| 343 | using AnyHttpFetcherTest::NewSmallFetcher; |
| 344 | HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) override { |
| 345 | return NewLargeFetcher(proxy_resolver); |
| 346 | } |
| 347 | |
| 348 | string BigUrl(in_port_t port) const override { |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 349 | static string big_contents = []() { |
| 350 | string buf; |
| 351 | buf.reserve(kBigLength); |
| 352 | constexpr const char* kBigUrlContent = "abcdefghij"; |
| 353 | for (size_t i = 0; i < kBigLength; i += strlen(kBigUrlContent)) { |
| 354 | buf.append(kBigUrlContent, |
| 355 | std::min(kBigLength - i, strlen(kBigUrlContent))); |
| 356 | } |
| 357 | return buf; |
| 358 | }(); |
| 359 | test_utils::WriteFileString(temp_file_.path(), big_contents); |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 360 | return "file://" + temp_file_.path(); |
| 361 | } |
| 362 | string SmallUrl(in_port_t port) const override { |
| 363 | test_utils::WriteFileString(temp_file_.path(), "small contents"); |
| 364 | return "file://" + temp_file_.path(); |
| 365 | } |
| 366 | string ErrorUrl(in_port_t port) const override { |
| 367 | return "file:///path/to/non-existing-file"; |
| 368 | } |
| 369 | |
| 370 | bool IsMock() const override { return false; } |
| 371 | bool IsMulti() const override { return false; } |
| 372 | bool IsHttpSupported() const override { return false; } |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 373 | bool IsFileFetcher() const override { return true; } |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 374 | |
| 375 | void IgnoreServerAborting(HttpServer* server) const override {} |
| 376 | |
| 377 | HttpServer* CreateServer() override { return new NullHttpServer; } |
| 378 | |
| 379 | private: |
| 380 | test_utils::ScopedTempFile temp_file_{"ue_file_fetcher.XXXXXX"}; |
| 381 | }; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 382 | |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 383 | class MultiRangeHttpFetcherOverFileFetcherTest : public FileFetcherTest { |
| 384 | public: |
| 385 | // Necessary to unhide the definition in the base class. |
| 386 | using AnyHttpFetcherTest::NewLargeFetcher; |
| 387 | HttpFetcher* NewLargeFetcher(ProxyResolver* /* proxy_resolver */) override { |
| 388 | MultiRangeHttpFetcher* ret = new MultiRangeHttpFetcher(new FileFetcher()); |
| 389 | ret->ClearRanges(); |
| 390 | // FileFetcher doesn't support range with unspecified length. |
| 391 | ret->AddRange(0, 1); |
| 392 | // Speed up test execution. |
| 393 | ret->set_idle_seconds(1); |
| 394 | ret->set_retry_seconds(1); |
| 395 | fake_hardware_.SetIsOfficialBuild(false); |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | // Necessary to unhide the definition in the base class. |
| 400 | using AnyHttpFetcherTest::NewSmallFetcher; |
| 401 | HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) override { |
| 402 | return NewLargeFetcher(proxy_resolver); |
| 403 | } |
| 404 | |
| 405 | bool IsMulti() const override { return true; } |
| 406 | }; |
| 407 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 408 | // |
| 409 | // Infrastructure for type tests of HTTP fetcher. |
| 410 | // See: http://code.google.com/p/googletest/wiki/AdvancedGuide#Typed_Tests |
| 411 | // |
| 412 | |
| 413 | // Fixture class template. We use an explicit constraint to guarantee that it |
| 414 | // can only be instantiated with an AnyHttpFetcherTest type, see: |
| 415 | // http://www2.research.att.com/~bs/bs_faq2.html#constraints |
| 416 | template <typename T> |
| 417 | class HttpFetcherTest : public ::testing::Test { |
| 418 | public: |
Alex Deymo | 535f3b7 | 2015-08-07 10:51:32 -0700 | [diff] [blame] | 419 | base::MessageLoopForIO base_loop_; |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 420 | brillo::BaseMessageLoop loop_{&base_loop_}; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 421 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 422 | T test_; |
| 423 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 424 | protected: |
| 425 | HttpFetcherTest() { |
| 426 | loop_.SetAsCurrent(); |
| 427 | } |
| 428 | |
| 429 | void TearDown() override { |
Alex Vakulenko | 3f39d5c | 2015-10-13 09:27:13 -0700 | [diff] [blame] | 430 | EXPECT_EQ(0, brillo::MessageLoopRunMaxIterations(&loop_, 1)); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 433 | private: |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 434 | static void TypeConstraint(T* a) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 435 | AnyHttpFetcherTest *b = a; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 436 | if (b == 0) // Silence compiler warning of unused variable. |
Yunlian Jiang | 2dac576 | 2013-04-12 09:53:09 -0700 | [diff] [blame] | 437 | *b = a; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 438 | } |
| 439 | }; |
| 440 | |
| 441 | // Test case types list. |
| 442 | typedef ::testing::Types<LibcurlHttpFetcherTest, |
| 443 | MockHttpFetcherTest, |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 444 | MultiRangeHttpFetcherTest, |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 445 | FileFetcherTest, |
| 446 | MultiRangeHttpFetcherOverFileFetcherTest> |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 447 | HttpFetcherTestTypes; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 448 | TYPED_TEST_CASE(HttpFetcherTest, HttpFetcherTestTypes); |
| 449 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 450 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 451 | namespace { |
| 452 | class HttpFetcherTestDelegate : public HttpFetcherDelegate { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 453 | public: |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 454 | HttpFetcherTestDelegate() = default; |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 455 | |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 456 | bool ReceivedBytes(HttpFetcher* /* fetcher */, |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 457 | const void* bytes, |
| 458 | size_t length) override { |
| 459 | data.append(reinterpret_cast<const char*>(bytes), length); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 460 | // Update counters |
| 461 | times_received_bytes_called_++; |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 462 | return true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 463 | } |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 464 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 465 | void TransferComplete(HttpFetcher* fetcher, bool successful) override { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 466 | if (is_expect_error_) |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 467 | EXPECT_EQ(kHttpResponseNotFound, fetcher->http_response_code()); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 468 | else |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 469 | EXPECT_EQ(kHttpResponseOk, fetcher->http_response_code()); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 470 | MessageLoop::current()->BreakLoop(); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 471 | |
| 472 | // Update counter |
| 473 | times_transfer_complete_called_++; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 474 | } |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 475 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 476 | void TransferTerminated(HttpFetcher* fetcher) override { |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 477 | times_transfer_terminated_called_++; |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 478 | MessageLoop::current()->BreakLoop(); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 479 | } |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 480 | |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 481 | // Are we expecting an error response? (default: no) |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 482 | bool is_expect_error_{false}; |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 483 | |
| 484 | // Counters for callback invocations. |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 485 | int times_transfer_complete_called_{0}; |
| 486 | int times_transfer_terminated_called_{0}; |
| 487 | int times_received_bytes_called_{0}; |
| 488 | |
| 489 | // The received data bytes. |
| 490 | string data; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 491 | }; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 492 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 493 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 494 | void StartTransfer(HttpFetcher* http_fetcher, const string& url) { |
| 495 | http_fetcher->BeginTransfer(url); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 496 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 497 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 498 | |
| 499 | TYPED_TEST(HttpFetcherTest, SimpleTest) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 500 | HttpFetcherTestDelegate delegate; |
| 501 | unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher()); |
| 502 | fetcher->set_delegate(&delegate); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 503 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 504 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
| 505 | ASSERT_TRUE(server->started_); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 506 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 507 | this->loop_.PostTask(FROM_HERE, base::Bind( |
| 508 | StartTransfer, |
| 509 | fetcher.get(), |
| 510 | this->test_.SmallUrl(server->GetPort()))); |
| 511 | this->loop_.Run(); |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 512 | EXPECT_EQ(0, delegate.times_transfer_terminated_called_); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 515 | TYPED_TEST(HttpFetcherTest, SimpleBigTest) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 516 | HttpFetcherTestDelegate delegate; |
| 517 | unique_ptr<HttpFetcher> fetcher(this->test_.NewLargeFetcher()); |
| 518 | fetcher->set_delegate(&delegate); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 519 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 520 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
| 521 | ASSERT_TRUE(server->started_); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 522 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 523 | this->loop_.PostTask(FROM_HERE, base::Bind( |
| 524 | StartTransfer, |
| 525 | fetcher.get(), |
| 526 | this->test_.BigUrl(server->GetPort()))); |
| 527 | this->loop_.Run(); |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 528 | EXPECT_EQ(0, delegate.times_transfer_terminated_called_); |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 531 | // Issue #9648: when server returns an error HTTP response, the fetcher needs to |
| 532 | // terminate transfer prematurely, rather than try to process the error payload. |
| 533 | TYPED_TEST(HttpFetcherTest, ErrorTest) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 534 | if (this->test_.IsMock() || this->test_.IsMulti()) |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 535 | return; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 536 | HttpFetcherTestDelegate delegate; |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 537 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 538 | // Delegate should expect an error response. |
| 539 | delegate.is_expect_error_ = true; |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 540 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 541 | unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher()); |
| 542 | fetcher->set_delegate(&delegate); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 543 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 544 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
| 545 | ASSERT_TRUE(server->started_); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 546 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 547 | this->loop_.PostTask(FROM_HERE, base::Bind( |
| 548 | StartTransfer, |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 549 | fetcher.get(), |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 550 | this->test_.ErrorUrl(server->GetPort()))); |
| 551 | this->loop_.Run(); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 552 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 553 | // Make sure that no bytes were received. |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 554 | EXPECT_EQ(0, delegate.times_received_bytes_called_); |
| 555 | EXPECT_EQ(0U, fetcher->GetBytesDownloaded()); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 556 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 557 | // Make sure that transfer completion was signaled once, and no termination |
| 558 | // was signaled. |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 559 | EXPECT_EQ(1, delegate.times_transfer_complete_called_); |
| 560 | EXPECT_EQ(0, delegate.times_transfer_terminated_called_); |
Gilad Arnold | 48085ba | 2011-11-16 09:36:08 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 563 | TYPED_TEST(HttpFetcherTest, ExtraHeadersInRequestTest) { |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 564 | if (this->test_.IsMock() || !this->test_.IsHttpSupported()) |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 565 | return; |
| 566 | |
| 567 | HttpFetcherTestDelegate delegate; |
| 568 | unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher()); |
| 569 | fetcher->set_delegate(&delegate); |
| 570 | fetcher->SetHeader("User-Agent", "MyTest"); |
| 571 | fetcher->SetHeader("user-agent", "Override that header"); |
| 572 | fetcher->SetHeader("Authorization", "Basic user:passwd"); |
| 573 | |
| 574 | // Invalid headers. |
| 575 | fetcher->SetHeader("X-Foo", "Invalid\nHeader\nIgnored"); |
| 576 | fetcher->SetHeader("X-Bar: ", "I do not know how to parse"); |
| 577 | |
| 578 | // Hide Accept header normally added by default. |
| 579 | fetcher->SetHeader("Accept", ""); |
| 580 | |
| 581 | PythonHttpServer server; |
| 582 | int port = server.GetPort(); |
| 583 | ASSERT_TRUE(server.started_); |
| 584 | |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 585 | this->loop_.PostTask( |
| 586 | FROM_HERE, |
| 587 | base::Bind(StartTransfer, |
| 588 | fetcher.get(), |
| 589 | LocalServerUrlForPath(port, "/echo-headers"))); |
Alex Deymo | fdd6dec | 2016-03-03 22:35:43 -0800 | [diff] [blame] | 590 | this->loop_.Run(); |
| 591 | |
| 592 | EXPECT_NE(string::npos, |
| 593 | delegate.data.find("user-agent: Override that header\r\n")); |
| 594 | EXPECT_NE(string::npos, |
| 595 | delegate.data.find("Authorization: Basic user:passwd\r\n")); |
| 596 | |
| 597 | EXPECT_EQ(string::npos, delegate.data.find("\nAccept:")); |
| 598 | EXPECT_EQ(string::npos, delegate.data.find("X-Foo: Invalid")); |
| 599 | EXPECT_EQ(string::npos, delegate.data.find("X-Bar: I do not")); |
| 600 | } |
| 601 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 602 | namespace { |
| 603 | class PausingHttpFetcherTestDelegate : public HttpFetcherDelegate { |
| 604 | public: |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 605 | bool ReceivedBytes(HttpFetcher* fetcher, |
| 606 | const void* /* bytes */, |
| 607 | size_t /* length */) override { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 608 | CHECK(!paused_); |
| 609 | paused_ = true; |
| 610 | fetcher->Pause(); |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 611 | return true; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 612 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 613 | void TransferComplete(HttpFetcher* fetcher, bool successful) override { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 614 | MessageLoop::current()->BreakLoop(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 615 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 616 | void TransferTerminated(HttpFetcher* fetcher) override { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 617 | ADD_FAILURE(); |
| 618 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 619 | void Unpause() { |
| 620 | CHECK(paused_); |
| 621 | paused_ = false; |
| 622 | fetcher_->Unpause(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 623 | } |
| 624 | bool paused_; |
| 625 | HttpFetcher* fetcher_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 626 | }; |
| 627 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 628 | void UnpausingTimeoutCallback(PausingHttpFetcherTestDelegate* delegate, |
| 629 | MessageLoop::TaskId* my_id) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 630 | if (delegate->paused_) |
| 631 | delegate->Unpause(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 632 | // Update the task id with the new scheduled callback. |
| 633 | *my_id = MessageLoop::current()->PostDelayedTask( |
| 634 | FROM_HERE, |
| 635 | base::Bind(&UnpausingTimeoutCallback, delegate, my_id), |
| 636 | base::TimeDelta::FromMilliseconds(200)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 637 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 638 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 639 | |
| 640 | TYPED_TEST(HttpFetcherTest, PauseTest) { |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 641 | PausingHttpFetcherTestDelegate delegate; |
| 642 | unique_ptr<HttpFetcher> fetcher(this->test_.NewLargeFetcher()); |
| 643 | delegate.paused_ = false; |
| 644 | delegate.fetcher_ = fetcher.get(); |
| 645 | fetcher->set_delegate(&delegate); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 646 | |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 647 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
| 648 | ASSERT_TRUE(server->started_); |
Andrew de los Reyes | f3ed8e7 | 2011-02-16 10:35:46 -0800 | [diff] [blame] | 649 | |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 650 | MessageLoop::TaskId callback_id; |
| 651 | callback_id = this->loop_.PostDelayedTask( |
| 652 | FROM_HERE, |
| 653 | base::Bind(&UnpausingTimeoutCallback, &delegate, &callback_id), |
| 654 | base::TimeDelta::FromMilliseconds(200)); |
| 655 | fetcher->BeginTransfer(this->test_.BigUrl(server->GetPort())); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 656 | |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 657 | this->loop_.Run(); |
| 658 | EXPECT_TRUE(this->loop_.CancelTask(callback_id)); |
| 659 | } |
| 660 | |
| 661 | // This test will pause the fetcher while the download is not yet started |
| 662 | // because it is waiting for the proxy to be resolved. |
| 663 | TYPED_TEST(HttpFetcherTest, PauseWhileResolvingProxyTest) { |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 664 | if (this->test_.IsMock() || !this->test_.IsHttpSupported()) |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 665 | return; |
| 666 | MockProxyResolver mock_resolver; |
| 667 | unique_ptr<HttpFetcher> fetcher(this->test_.NewLargeFetcher(&mock_resolver)); |
| 668 | |
| 669 | // Saved arguments from the proxy call. |
Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 670 | ProxiesResolvedFn proxy_callback; |
| 671 | EXPECT_CALL(mock_resolver, GetProxiesForUrl("http://fake_url", _)) |
| 672 | .WillOnce(DoAll(SaveArg<1>(&proxy_callback), Return(true))); |
Alex Deymo | f285857 | 2016-02-25 11:20:13 -0800 | [diff] [blame] | 673 | fetcher->BeginTransfer("http://fake_url"); |
| 674 | testing::Mock::VerifyAndClearExpectations(&mock_resolver); |
| 675 | |
| 676 | // Pausing and unpausing while resolving the proxy should not affect anything. |
| 677 | fetcher->Pause(); |
| 678 | fetcher->Unpause(); |
| 679 | fetcher->Pause(); |
| 680 | // Proxy resolver comes back after we paused the fetcher. |
Alex Deymo | c00ec56 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 681 | ASSERT_FALSE(proxy_callback.is_null()); |
Alex Deymo | 3582194 | 2017-02-05 04:36:02 +0000 | [diff] [blame] | 682 | proxy_callback.Run({1, kNoProxy}); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | namespace { |
| 686 | class AbortingHttpFetcherTestDelegate : public HttpFetcherDelegate { |
| 687 | public: |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 688 | bool ReceivedBytes(HttpFetcher* fetcher, |
| 689 | const void* bytes, |
| 690 | size_t length) override { |
| 691 | return true; |
| 692 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 693 | void TransferComplete(HttpFetcher* fetcher, bool successful) override { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 694 | ADD_FAILURE(); // We should never get here |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 695 | MessageLoop::current()->BreakLoop(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 696 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 697 | void TransferTerminated(HttpFetcher* fetcher) override { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 698 | EXPECT_EQ(fetcher, fetcher_.get()); |
| 699 | EXPECT_FALSE(once_); |
| 700 | EXPECT_TRUE(callback_once_); |
| 701 | callback_once_ = false; |
Alex Deymo | c4acdf4 | 2014-05-28 21:07:10 -0700 | [diff] [blame] | 702 | // The fetcher could have a callback scheduled on the ProxyResolver that |
| 703 | // can fire after this callback. We wait until the end of the test to |
| 704 | // delete the fetcher. |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 705 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 706 | void TerminateTransfer() { |
| 707 | CHECK(once_); |
| 708 | once_ = false; |
| 709 | fetcher_->TerminateTransfer(); |
| 710 | } |
| 711 | void EndLoop() { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 712 | MessageLoop::current()->BreakLoop(); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 713 | } |
| 714 | bool once_; |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 715 | bool callback_once_; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 716 | unique_ptr<HttpFetcher> fetcher_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 717 | }; |
| 718 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 719 | void AbortingTimeoutCallback(AbortingHttpFetcherTestDelegate* delegate, |
| 720 | MessageLoop::TaskId* my_id) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 721 | if (delegate->once_) { |
| 722 | delegate->TerminateTransfer(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 723 | *my_id = MessageLoop::current()->PostTask( |
| 724 | FROM_HERE, |
| 725 | base::Bind(AbortingTimeoutCallback, delegate, my_id)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 726 | } else { |
| 727 | delegate->EndLoop(); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 728 | *my_id = MessageLoop::kTaskIdNull; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 729 | } |
| 730 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 731 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 732 | |
| 733 | TYPED_TEST(HttpFetcherTest, AbortTest) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 734 | AbortingHttpFetcherTestDelegate delegate; |
| 735 | delegate.fetcher_.reset(this->test_.NewLargeFetcher()); |
| 736 | delegate.once_ = true; |
| 737 | delegate.callback_once_ = true; |
| 738 | delegate.fetcher_->set_delegate(&delegate); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 739 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 740 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
| 741 | this->test_.IgnoreServerAborting(server.get()); |
| 742 | ASSERT_TRUE(server->started_); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 743 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 744 | MessageLoop::TaskId task_id = MessageLoop::kTaskIdNull; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 745 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 746 | task_id = this->loop_.PostTask( |
| 747 | FROM_HERE, |
| 748 | base::Bind(AbortingTimeoutCallback, &delegate, &task_id)); |
| 749 | delegate.fetcher_->BeginTransfer(this->test_.BigUrl(server->GetPort())); |
| 750 | |
| 751 | this->loop_.Run(); |
| 752 | CHECK(!delegate.once_); |
| 753 | CHECK(!delegate.callback_once_); |
| 754 | this->loop_.CancelTask(task_id); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Alex Deymo | 71f6762 | 2017-02-03 21:30:24 -0800 | [diff] [blame] | 757 | TYPED_TEST(HttpFetcherTest, TerminateTransferWhileResolvingProxyTest) { |
| 758 | if (this->test_.IsMock() || !this->test_.IsHttpSupported()) |
| 759 | return; |
| 760 | MockProxyResolver mock_resolver; |
| 761 | unique_ptr<HttpFetcher> fetcher(this->test_.NewLargeFetcher(&mock_resolver)); |
| 762 | |
| 763 | HttpFetcherTestDelegate delegate; |
| 764 | fetcher->set_delegate(&delegate); |
| 765 | |
| 766 | EXPECT_CALL(mock_resolver, GetProxiesForUrl(_, _)).WillOnce(Return(123)); |
| 767 | fetcher->BeginTransfer("http://fake_url"); |
| 768 | // Run the message loop until idle. This must call the MockProxyResolver with |
| 769 | // the request. |
| 770 | while (this->loop_.RunOnce(false)) { |
| 771 | } |
| 772 | testing::Mock::VerifyAndClearExpectations(&mock_resolver); |
| 773 | |
| 774 | EXPECT_CALL(mock_resolver, CancelProxyRequest(123)).WillOnce(Return(true)); |
| 775 | |
| 776 | // Terminate the transfer right before the proxy resolution response. |
| 777 | fetcher->TerminateTransfer(); |
| 778 | EXPECT_EQ(0, delegate.times_received_bytes_called_); |
| 779 | EXPECT_EQ(0, delegate.times_transfer_complete_called_); |
| 780 | EXPECT_EQ(1, delegate.times_transfer_terminated_called_); |
| 781 | } |
| 782 | |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 783 | namespace { |
| 784 | class FlakyHttpFetcherTestDelegate : public HttpFetcherDelegate { |
| 785 | public: |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 786 | bool ReceivedBytes(HttpFetcher* fetcher, |
| 787 | const void* bytes, |
| 788 | size_t length) override { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 789 | data.append(reinterpret_cast<const char*>(bytes), length); |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 790 | return true; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 791 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 792 | void TransferComplete(HttpFetcher* fetcher, bool successful) override { |
Andrew de los Reyes | fb4ad7d | 2010-07-19 10:43:46 -0700 | [diff] [blame] | 793 | EXPECT_TRUE(successful); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 794 | EXPECT_EQ(kHttpResponsePartialContent, fetcher->http_response_code()); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 795 | MessageLoop::current()->BreakLoop(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 796 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 797 | void TransferTerminated(HttpFetcher* fetcher) override { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 798 | ADD_FAILURE(); |
| 799 | } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 800 | string data; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 801 | }; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 802 | } // namespace |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 803 | |
| 804 | TYPED_TEST(HttpFetcherTest, FlakyTest) { |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 805 | if (this->test_.IsMock() || !this->test_.IsHttpSupported()) |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 806 | return; |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 807 | { |
| 808 | FlakyHttpFetcherTestDelegate delegate; |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 809 | unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher()); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 810 | fetcher->set_delegate(&delegate); |
| 811 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 812 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 813 | ASSERT_TRUE(server->started_); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 814 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 815 | this->loop_.PostTask(FROM_HERE, base::Bind( |
| 816 | &StartTransfer, |
| 817 | fetcher.get(), |
| 818 | LocalServerUrlForPath(server->GetPort(), |
| 819 | base::StringPrintf("/flaky/%d/%d/%d/%d", |
| 820 | kBigLength, |
| 821 | kFlakyTruncateLength, |
| 822 | kFlakySleepEvery, |
| 823 | kFlakySleepSecs)))); |
| 824 | this->loop_.Run(); |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 825 | |
| 826 | // verify the data we get back |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 827 | ASSERT_EQ(kBigLength, static_cast<int>(delegate.data.size())); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 828 | for (int i = 0; i < kBigLength; i += 10) { |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 829 | // Assert so that we don't flood the screen w/ EXPECT errors on failure. |
| 830 | ASSERT_EQ(delegate.data.substr(i, 10), "abcdefghij"); |
| 831 | } |
| 832 | } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 835 | namespace { |
Chris Sosa | 0a364bb | 2014-06-10 18:18:24 -0700 | [diff] [blame] | 836 | // This delegate kills the server attached to it after receiving any bytes. |
| 837 | // This can be used for testing what happens when you try to fetch data and |
| 838 | // the server dies. |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 839 | class FailureHttpFetcherTestDelegate : public HttpFetcherDelegate { |
| 840 | public: |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 841 | explicit FailureHttpFetcherTestDelegate(PythonHttpServer* server) |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 842 | : server_(server) {} |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 843 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 844 | ~FailureHttpFetcherTestDelegate() override { |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 845 | if (server_) { |
| 846 | LOG(INFO) << "Stopping server in destructor"; |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 847 | server_.reset(); |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 848 | LOG(INFO) << "server stopped"; |
| 849 | } |
| 850 | } |
| 851 | |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 852 | bool ReceivedBytes(HttpFetcher* fetcher, |
| 853 | const void* bytes, |
| 854 | size_t length) override { |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 855 | if (server_) { |
Jay Srinivasan | 135a58b | 2012-07-13 12:46:49 -0700 | [diff] [blame] | 856 | LOG(INFO) << "Stopping server in ReceivedBytes"; |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 857 | server_.reset(); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 858 | LOG(INFO) << "server stopped"; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 859 | } |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 860 | return true; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 861 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 862 | void TransferComplete(HttpFetcher* fetcher, bool successful) override { |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 863 | EXPECT_FALSE(successful); |
Chris Sosa | 0a364bb | 2014-06-10 18:18:24 -0700 | [diff] [blame] | 864 | EXPECT_EQ(0, fetcher->http_response_code()); |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 865 | times_transfer_complete_called_++; |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 866 | MessageLoop::current()->BreakLoop(); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 867 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 868 | void TransferTerminated(HttpFetcher* fetcher) override { |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 869 | times_transfer_terminated_called_++; |
| 870 | MessageLoop::current()->BreakLoop(); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 871 | } |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 872 | unique_ptr<PythonHttpServer> server_; |
| 873 | int times_transfer_terminated_called_{0}; |
| 874 | int times_transfer_complete_called_{0}; |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 875 | }; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 876 | } // namespace |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 877 | |
| 878 | |
| 879 | TYPED_TEST(HttpFetcherTest, FailureTest) { |
Chris Sosa | 0a364bb | 2014-06-10 18:18:24 -0700 | [diff] [blame] | 880 | // This test ensures that a fetcher responds correctly when a server isn't |
| 881 | // available at all. |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 882 | if (this->test_.IsMock()) |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 883 | return; |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 884 | FailureHttpFetcherTestDelegate delegate(nullptr); |
| 885 | unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher()); |
| 886 | fetcher->set_delegate(&delegate); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 887 | |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 888 | this->loop_.PostTask( |
| 889 | FROM_HERE, |
| 890 | base::Bind( |
| 891 | StartTransfer, fetcher.get(), "http://host_doesnt_exist99999999")); |
| 892 | this->loop_.Run(); |
| 893 | EXPECT_EQ(1, delegate.times_transfer_complete_called_); |
| 894 | EXPECT_EQ(0, delegate.times_transfer_terminated_called_); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 895 | |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 896 | // Exiting and testing happens in the delegate |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Alex Deymo | f123ae2 | 2015-09-24 14:59:43 -0700 | [diff] [blame] | 899 | TYPED_TEST(HttpFetcherTest, NoResponseTest) { |
| 900 | // This test starts a new http server but the server doesn't respond and just |
| 901 | // closes the connection. |
| 902 | if (this->test_.IsMock()) |
| 903 | return; |
| 904 | |
| 905 | PythonHttpServer* server = new PythonHttpServer(); |
| 906 | int port = server->GetPort(); |
| 907 | ASSERT_TRUE(server->started_); |
| 908 | |
| 909 | // Handles destruction and claims ownership. |
| 910 | FailureHttpFetcherTestDelegate delegate(server); |
| 911 | unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher()); |
| 912 | fetcher->set_delegate(&delegate); |
| 913 | // The server will not reply at all, so we can limit the execution time of the |
| 914 | // test by reducing the low-speed timeout to something small. The test will |
| 915 | // finish once the TimeoutCallback() triggers (every second) and the timeout |
| 916 | // expired. |
| 917 | fetcher->set_low_speed_limit(kDownloadLowSpeedLimitBps, 1); |
| 918 | |
| 919 | this->loop_.PostTask(FROM_HERE, base::Bind( |
| 920 | StartTransfer, |
| 921 | fetcher.get(), |
| 922 | LocalServerUrlForPath(port, "/hang"))); |
| 923 | this->loop_.Run(); |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 924 | EXPECT_EQ(1, delegate.times_transfer_complete_called_); |
| 925 | EXPECT_EQ(0, delegate.times_transfer_terminated_called_); |
Alex Deymo | f123ae2 | 2015-09-24 14:59:43 -0700 | [diff] [blame] | 926 | |
| 927 | // Check that no other callback runs in the next two seconds. That would |
| 928 | // indicate a leaked callback. |
| 929 | bool timeout = false; |
Luis Hector Chavez | f1cf348 | 2016-07-19 14:29:19 -0700 | [diff] [blame] | 930 | auto callback = base::Bind([](bool* timeout) { *timeout = true; }, |
| 931 | base::Unretained(&timeout)); |
Alex Deymo | f123ae2 | 2015-09-24 14:59:43 -0700 | [diff] [blame] | 932 | this->loop_.PostDelayedTask(FROM_HERE, callback, |
| 933 | base::TimeDelta::FromSeconds(2)); |
| 934 | EXPECT_TRUE(this->loop_.RunOnce(true)); |
| 935 | EXPECT_TRUE(timeout); |
| 936 | } |
| 937 | |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 938 | TYPED_TEST(HttpFetcherTest, ServerDiesTest) { |
Chris Sosa | 0a364bb | 2014-06-10 18:18:24 -0700 | [diff] [blame] | 939 | // This test starts a new http server and kills it after receiving its first |
| 940 | // set of bytes. It test whether or not our fetcher eventually gives up on |
| 941 | // retries and aborts correctly. |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 942 | if (this->test_.IsMock()) |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 943 | return; |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 944 | PythonHttpServer* server = new PythonHttpServer(); |
| 945 | int port = server->GetPort(); |
| 946 | ASSERT_TRUE(server->started_); |
Chris Sosa | 0a364bb | 2014-06-10 18:18:24 -0700 | [diff] [blame] | 947 | |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 948 | // Handles destruction and claims ownership. |
| 949 | FailureHttpFetcherTestDelegate delegate(server); |
| 950 | unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher()); |
| 951 | fetcher->set_delegate(&delegate); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 952 | |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 953 | this->loop_.PostTask( |
| 954 | FROM_HERE, |
| 955 | base::Bind(StartTransfer, |
| 956 | fetcher.get(), |
| 957 | LocalServerUrlForPath(port, |
| 958 | base::StringPrintf("/flaky/%d/%d/%d/%d", |
| 959 | kBigLength, |
| 960 | kFlakyTruncateLength, |
| 961 | kFlakySleepEvery, |
| 962 | kFlakySleepSecs)))); |
| 963 | this->loop_.Run(); |
| 964 | EXPECT_EQ(1, delegate.times_transfer_complete_called_); |
| 965 | EXPECT_EQ(0, delegate.times_transfer_terminated_called_); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 966 | |
Alex Deymo | b20de69 | 2017-02-05 07:47:37 +0000 | [diff] [blame] | 967 | // Exiting and testing happens in the delegate |
| 968 | } |
| 969 | |
| 970 | // Test that we can cancel a transfer while it is still trying to connect to the |
| 971 | // server. This test kills the server after a few bytes are received. |
| 972 | TYPED_TEST(HttpFetcherTest, TerminateTransferWhenServerDiedTest) { |
| 973 | if (this->test_.IsMock() || !this->test_.IsHttpSupported()) |
| 974 | return; |
| 975 | |
| 976 | PythonHttpServer* server = new PythonHttpServer(); |
| 977 | int port = server->GetPort(); |
| 978 | ASSERT_TRUE(server->started_); |
| 979 | |
| 980 | // Handles destruction and claims ownership. |
| 981 | FailureHttpFetcherTestDelegate delegate(server); |
| 982 | unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher()); |
| 983 | fetcher->set_delegate(&delegate); |
| 984 | |
| 985 | this->loop_.PostTask( |
| 986 | FROM_HERE, |
| 987 | base::Bind(StartTransfer, |
| 988 | fetcher.get(), |
| 989 | LocalServerUrlForPath(port, |
| 990 | base::StringPrintf("/flaky/%d/%d/%d/%d", |
| 991 | kBigLength, |
| 992 | kFlakyTruncateLength, |
| 993 | kFlakySleepEvery, |
| 994 | kFlakySleepSecs)))); |
| 995 | // Terminating the transfer after 3 seconds gives it a chance to contact the |
| 996 | // server and enter the retry loop. |
| 997 | this->loop_.PostDelayedTask(FROM_HERE, |
| 998 | base::Bind(&HttpFetcher::TerminateTransfer, |
| 999 | base::Unretained(fetcher.get())), |
| 1000 | base::TimeDelta::FromSeconds(3)); |
| 1001 | |
| 1002 | // Exiting and testing happens in the delegate. |
| 1003 | this->loop_.Run(); |
| 1004 | EXPECT_EQ(0, delegate.times_transfer_complete_called_); |
| 1005 | EXPECT_EQ(1, delegate.times_transfer_terminated_called_); |
| 1006 | |
| 1007 | // Check that no other callback runs in the next two seconds. That would |
| 1008 | // indicate a leaked callback. |
| 1009 | bool timeout = false; |
| 1010 | auto callback = base::Bind([](bool* timeout) { *timeout = true; }, |
| 1011 | base::Unretained(&timeout)); |
| 1012 | this->loop_.PostDelayedTask( |
| 1013 | FROM_HERE, callback, base::TimeDelta::FromSeconds(2)); |
| 1014 | EXPECT_TRUE(this->loop_.RunOnce(true)); |
| 1015 | EXPECT_TRUE(timeout); |
Andrew de los Reyes | 9bbd187 | 2010-07-16 14:52:29 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1018 | namespace { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1019 | const HttpResponseCode kRedirectCodes[] = { |
| 1020 | kHttpResponseMovedPermanently, kHttpResponseFound, kHttpResponseSeeOther, |
| 1021 | kHttpResponseTempRedirect |
| 1022 | }; |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1023 | |
| 1024 | class RedirectHttpFetcherTestDelegate : public HttpFetcherDelegate { |
| 1025 | public: |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1026 | explicit RedirectHttpFetcherTestDelegate(bool expected_successful) |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1027 | : expected_successful_(expected_successful) {} |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 1028 | bool ReceivedBytes(HttpFetcher* fetcher, |
| 1029 | const void* bytes, |
| 1030 | size_t length) override { |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1031 | data.append(reinterpret_cast<const char*>(bytes), length); |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 1032 | return true; |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1033 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 1034 | void TransferComplete(HttpFetcher* fetcher, bool successful) override { |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1035 | EXPECT_EQ(expected_successful_, successful); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1036 | if (expected_successful_) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1037 | EXPECT_EQ(kHttpResponseOk, fetcher->http_response_code()); |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1038 | } else { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1039 | EXPECT_GE(fetcher->http_response_code(), kHttpResponseMovedPermanently); |
| 1040 | EXPECT_LE(fetcher->http_response_code(), kHttpResponseTempRedirect); |
Darin Petkov | cb46621 | 2010-08-26 09:40:11 -0700 | [diff] [blame] | 1041 | } |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1042 | MessageLoop::current()->BreakLoop(); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1043 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 1044 | void TransferTerminated(HttpFetcher* fetcher) override { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 1045 | ADD_FAILURE(); |
| 1046 | } |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1047 | bool expected_successful_; |
| 1048 | string data; |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1049 | }; |
| 1050 | |
| 1051 | // RedirectTest takes ownership of |http_fetcher|. |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 1052 | void RedirectTest(const HttpServer* server, |
| 1053 | bool expected_successful, |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1054 | const string& url, |
| 1055 | HttpFetcher* http_fetcher) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1056 | RedirectHttpFetcherTestDelegate delegate(expected_successful); |
| 1057 | unique_ptr<HttpFetcher> fetcher(http_fetcher); |
| 1058 | fetcher->set_delegate(&delegate); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1059 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1060 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 1061 | StartTransfer, |
| 1062 | fetcher.get(), |
| 1063 | LocalServerUrlForPath(server->GetPort(), url))); |
| 1064 | MessageLoop::current()->Run(); |
| 1065 | if (expected_successful) { |
| 1066 | // verify the data we get back |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 1067 | ASSERT_EQ(static_cast<size_t>(kMediumLength), delegate.data.size()); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1068 | for (int i = 0; i < kMediumLength; i += 10) { |
| 1069 | // Assert so that we don't flood the screen w/ EXPECT errors on failure. |
| 1070 | ASSERT_EQ(delegate.data.substr(i, 10), "abcdefghij"); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1071 | } |
| 1072 | } |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1073 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1074 | } // namespace |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1075 | |
| 1076 | TYPED_TEST(HttpFetcherTest, SimpleRedirectTest) { |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 1077 | if (this->test_.IsMock() || !this->test_.IsHttpSupported()) |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1078 | return; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1079 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 1080 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1081 | ASSERT_TRUE(server->started_); |
| 1082 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1083 | for (size_t c = 0; c < arraysize(kRedirectCodes); ++c) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1084 | const string url = base::StringPrintf("/redirect/%d/download/%d", |
| 1085 | kRedirectCodes[c], |
| 1086 | kMediumLength); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 1087 | RedirectTest(server.get(), true, url, this->test_.NewLargeFetcher()); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | TYPED_TEST(HttpFetcherTest, MaxRedirectTest) { |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 1092 | if (this->test_.IsMock() || !this->test_.IsHttpSupported()) |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1093 | return; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1094 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 1095 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1096 | ASSERT_TRUE(server->started_); |
| 1097 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1098 | string url; |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 1099 | for (int r = 0; r < kDownloadMaxRedirects; r++) { |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1100 | url += base::StringPrintf("/redirect/%d", |
| 1101 | kRedirectCodes[r % arraysize(kRedirectCodes)]); |
| 1102 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1103 | url += base::StringPrintf("/download/%d", kMediumLength); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 1104 | RedirectTest(server.get(), true, url, this->test_.NewLargeFetcher()); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | TYPED_TEST(HttpFetcherTest, BeyondMaxRedirectTest) { |
Alex Deymo | 2c131bb | 2016-05-26 16:43:13 -0700 | [diff] [blame] | 1108 | if (this->test_.IsMock() || !this->test_.IsHttpSupported()) |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1109 | return; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1110 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 1111 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1112 | ASSERT_TRUE(server->started_); |
| 1113 | |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1114 | string url; |
David Zeuthen | 34135a9 | 2013-08-06 11:16:16 -0700 | [diff] [blame] | 1115 | for (int r = 0; r < kDownloadMaxRedirects + 1; r++) { |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1116 | url += base::StringPrintf("/redirect/%d", |
| 1117 | kRedirectCodes[r % arraysize(kRedirectCodes)]); |
| 1118 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1119 | url += base::StringPrintf("/download/%d", kMediumLength); |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 1120 | RedirectTest(server.get(), false, url, this->test_.NewLargeFetcher()); |
Darin Petkov | 41c2fcf | 2010-08-25 13:14:48 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1123 | namespace { |
| 1124 | class MultiHttpFetcherTestDelegate : public HttpFetcherDelegate { |
| 1125 | public: |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1126 | explicit MultiHttpFetcherTestDelegate(int expected_response_code) |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1127 | : expected_response_code_(expected_response_code) {} |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1128 | |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 1129 | bool ReceivedBytes(HttpFetcher* fetcher, |
| 1130 | const void* bytes, |
| 1131 | size_t length) override { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 1132 | EXPECT_EQ(fetcher, fetcher_.get()); |
Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 1133 | data.append(reinterpret_cast<const char*>(bytes), length); |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 1134 | return true; |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1135 | } |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1136 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 1137 | void TransferComplete(HttpFetcher* fetcher, bool successful) override { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 1138 | EXPECT_EQ(fetcher, fetcher_.get()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1139 | EXPECT_EQ(expected_response_code_ != kHttpResponseUndefined, successful); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1140 | if (expected_response_code_ != 0) |
| 1141 | EXPECT_EQ(expected_response_code_, fetcher->http_response_code()); |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 1142 | // Destroy the fetcher (because we're allowed to). |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1143 | fetcher_.reset(nullptr); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1144 | MessageLoop::current()->BreakLoop(); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1145 | } |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1146 | |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 1147 | void TransferTerminated(HttpFetcher* fetcher) override { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 1148 | ADD_FAILURE(); |
| 1149 | } |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1150 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 1151 | unique_ptr<HttpFetcher> fetcher_; |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1152 | int expected_response_code_; |
| 1153 | string data; |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1154 | }; |
| 1155 | |
| 1156 | void MultiTest(HttpFetcher* fetcher_in, |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1157 | FakeHardware* fake_hardware, |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1158 | const string& url, |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 1159 | const vector<pair<off_t, off_t>>& ranges, |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1160 | const string& expected_prefix, |
Alex Deymo | 5fe0c4e | 2016-02-16 18:46:24 -0800 | [diff] [blame] | 1161 | size_t expected_size, |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1162 | HttpResponseCode expected_response_code) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1163 | MultiHttpFetcherTestDelegate delegate(expected_response_code); |
| 1164 | delegate.fetcher_.reset(fetcher_in); |
Jay Srinivasan | 4348879 | 2012-06-19 00:25:31 -0700 | [diff] [blame] | 1165 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1166 | MultiRangeHttpFetcher* multi_fetcher = |
Alex Deymo | 80f70ff | 2016-02-10 16:08:11 -0800 | [diff] [blame] | 1167 | static_cast<MultiRangeHttpFetcher*>(fetcher_in); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1168 | ASSERT_TRUE(multi_fetcher); |
| 1169 | multi_fetcher->ClearRanges(); |
| 1170 | for (vector<pair<off_t, off_t>>::const_iterator it = ranges.begin(), |
| 1171 | e = ranges.end(); it != e; ++it) { |
| 1172 | string tmp_str = base::StringPrintf("%jd+", it->first); |
| 1173 | if (it->second > 0) { |
| 1174 | base::StringAppendF(&tmp_str, "%jd", it->second); |
| 1175 | multi_fetcher->AddRange(it->first, it->second); |
| 1176 | } else { |
| 1177 | base::StringAppendF(&tmp_str, "?"); |
| 1178 | multi_fetcher->AddRange(it->first); |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 1179 | } |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1180 | LOG(INFO) << "added range: " << tmp_str; |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1181 | } |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1182 | fake_hardware->SetIsOfficialBuild(false); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1183 | multi_fetcher->set_delegate(&delegate); |
| 1184 | |
| 1185 | MessageLoop::current()->PostTask( |
| 1186 | FROM_HERE, |
| 1187 | base::Bind(StartTransfer, multi_fetcher, url)); |
| 1188 | MessageLoop::current()->Run(); |
| 1189 | |
| 1190 | EXPECT_EQ(expected_size, delegate.data.size()); |
| 1191 | EXPECT_EQ(expected_prefix, |
| 1192 | string(delegate.data.data(), expected_prefix.size())); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1193 | } |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1194 | } // namespace |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1195 | |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 1196 | TYPED_TEST(HttpFetcherTest, MultiHttpFetcherSimpleTest) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1197 | if (!this->test_.IsMulti()) |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1198 | return; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1199 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 1200 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1201 | ASSERT_TRUE(server->started_); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1202 | |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 1203 | vector<pair<off_t, off_t>> ranges; |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1204 | ranges.push_back(make_pair(0, 25)); |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 1205 | ranges.push_back(make_pair(99, 17)); |
| 1206 | MultiTest(this->test_.NewLargeFetcher(), |
| 1207 | this->test_.fake_hardware(), |
| 1208 | this->test_.BigUrl(server->GetPort()), |
| 1209 | ranges, |
| 1210 | "abcdefghijabcdefghijabcdejabcdefghijabcdef", |
| 1211 | 25 + 17, |
| 1212 | this->test_.IsFileFetcher() ? kHttpResponseOk |
| 1213 | : kHttpResponsePartialContent); |
| 1214 | } |
| 1215 | |
| 1216 | TYPED_TEST(HttpFetcherTest, MultiHttpFetcherUnspecifiedEndTest) { |
| 1217 | if (!this->test_.IsMulti() || this->test_.IsFileFetcher()) |
| 1218 | return; |
| 1219 | |
| 1220 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
| 1221 | ASSERT_TRUE(server->started_); |
| 1222 | |
| 1223 | vector<pair<off_t, off_t>> ranges; |
| 1224 | ranges.push_back(make_pair(0, 25)); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1225 | ranges.push_back(make_pair(99, 0)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1226 | MultiTest(this->test_.NewLargeFetcher(), |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1227 | this->test_.fake_hardware(), |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 1228 | this->test_.BigUrl(server->GetPort()), |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1229 | ranges, |
| 1230 | "abcdefghijabcdefghijabcdejabcdefghijabcdef", |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1231 | kBigLength - (99 - 25), |
| 1232 | kHttpResponsePartialContent); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | TYPED_TEST(HttpFetcherTest, MultiHttpFetcherLengthLimitTest) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1236 | if (!this->test_.IsMulti()) |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1237 | return; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1238 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 1239 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1240 | ASSERT_TRUE(server->started_); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1241 | |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 1242 | vector<pair<off_t, off_t>> ranges; |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1243 | ranges.push_back(make_pair(0, 24)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1244 | MultiTest(this->test_.NewLargeFetcher(), |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1245 | this->test_.fake_hardware(), |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 1246 | this->test_.BigUrl(server->GetPort()), |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1247 | ranges, |
| 1248 | "abcdefghijabcdefghijabcd", |
| 1249 | 24, |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 1250 | this->test_.IsFileFetcher() ? kHttpResponseOk |
| 1251 | : kHttpResponsePartialContent); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
| 1254 | TYPED_TEST(HttpFetcherTest, MultiHttpFetcherMultiEndTest) { |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 1255 | if (!this->test_.IsMulti() || this->test_.IsFileFetcher()) |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1256 | return; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1257 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 1258 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1259 | ASSERT_TRUE(server->started_); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1260 | |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 1261 | vector<pair<off_t, off_t>> ranges; |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1262 | ranges.push_back(make_pair(kBigLength - 2, 0)); |
| 1263 | ranges.push_back(make_pair(kBigLength - 3, 0)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1264 | MultiTest(this->test_.NewLargeFetcher(), |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1265 | this->test_.fake_hardware(), |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 1266 | this->test_.BigUrl(server->GetPort()), |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1267 | ranges, |
| 1268 | "ijhij", |
| 1269 | 5, |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1270 | kHttpResponsePartialContent); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | TYPED_TEST(HttpFetcherTest, MultiHttpFetcherInsufficientTest) { |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1274 | if (!this->test_.IsMulti()) |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1275 | return; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1276 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 1277 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1278 | ASSERT_TRUE(server->started_); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1279 | |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 1280 | vector<pair<off_t, off_t>> ranges; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1281 | ranges.push_back(make_pair(kBigLength - 2, 4)); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1282 | for (int i = 0; i < 2; ++i) { |
Andrew de los Reyes | 819fef2 | 2010-12-17 11:33:58 -0800 | [diff] [blame] | 1283 | LOG(INFO) << "i = " << i; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1284 | MultiTest(this->test_.NewLargeFetcher(), |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1285 | this->test_.fake_hardware(), |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 1286 | this->test_.BigUrl(server->GetPort()), |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1287 | ranges, |
| 1288 | "ij", |
| 1289 | 2, |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1290 | kHttpResponseUndefined); |
Andrew de los Reyes | 3fd5d30 | 2010-10-07 20:07:18 -0700 | [diff] [blame] | 1291 | ranges.push_back(make_pair(0, 5)); |
| 1292 | } |
| 1293 | } |
| 1294 | |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1295 | // Issue #18143: when a fetch of a secondary chunk out of a chain, then it |
| 1296 | // should retry with other proxies listed before giving up. |
| 1297 | // |
| 1298 | // (1) successful recovery: The offset fetch will fail twice but succeed with |
| 1299 | // the third proxy. |
| 1300 | TYPED_TEST(HttpFetcherTest, MultiHttpFetcherErrorIfOffsetRecoverableTest) { |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 1301 | if (!this->test_.IsMulti() || this->test_.IsFileFetcher()) |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1302 | return; |
| 1303 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 1304 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1305 | ASSERT_TRUE(server->started_); |
| 1306 | |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 1307 | vector<pair<off_t, off_t>> ranges; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1308 | ranges.push_back(make_pair(0, 25)); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1309 | ranges.push_back(make_pair(99, 0)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1310 | MultiTest(this->test_.NewLargeFetcher(3), |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1311 | this->test_.fake_hardware(), |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 1312 | LocalServerUrlForPath(server->GetPort(), |
| 1313 | base::StringPrintf("/error-if-offset/%d/2", |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1314 | kBigLength)), |
| 1315 | ranges, |
| 1316 | "abcdefghijabcdefghijabcdejabcdefghijabcdef", |
| 1317 | kBigLength - (99 - 25), |
| 1318 | kHttpResponsePartialContent); |
| 1319 | } |
| 1320 | |
| 1321 | // (2) unsuccessful recovery: The offset fetch will fail repeatedly. The |
| 1322 | // fetcher will signal a (failed) completed transfer to the delegate. |
| 1323 | TYPED_TEST(HttpFetcherTest, MultiHttpFetcherErrorIfOffsetUnrecoverableTest) { |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 1324 | if (!this->test_.IsMulti() || this->test_.IsFileFetcher()) |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1325 | return; |
| 1326 | |
Ben Chan | 02f7c1d | 2014-10-18 15:18:02 -0700 | [diff] [blame] | 1327 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1328 | ASSERT_TRUE(server->started_); |
| 1329 | |
Ben Chan | f9cb98c | 2014-09-21 18:31:30 -0700 | [diff] [blame] | 1330 | vector<pair<off_t, off_t>> ranges; |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1331 | ranges.push_back(make_pair(0, 25)); |
Gilad Arnold | e4ad250 | 2011-12-29 17:08:54 -0800 | [diff] [blame] | 1332 | ranges.push_back(make_pair(99, 0)); |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1333 | MultiTest(this->test_.NewLargeFetcher(2), |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1334 | this->test_.fake_hardware(), |
Gilad Arnold | b6c562a | 2013-07-01 02:19:26 -0700 | [diff] [blame] | 1335 | LocalServerUrlForPath(server->GetPort(), |
| 1336 | base::StringPrintf("/error-if-offset/%d/3", |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1337 | kBigLength)), |
| 1338 | ranges, |
| 1339 | "abcdefghijabcdefghijabcde", // only received the first chunk |
| 1340 | 25, |
| 1341 | kHttpResponseUndefined); |
| 1342 | } |
| 1343 | |
Alex Deymo | 4ea2d92 | 2017-02-05 07:35:32 +0000 | [diff] [blame] | 1344 | namespace { |
| 1345 | // This HttpFetcherDelegate calls TerminateTransfer at a configurable point. |
| 1346 | class MultiHttpFetcherTerminateTestDelegate : public HttpFetcherDelegate { |
| 1347 | public: |
| 1348 | explicit MultiHttpFetcherTerminateTestDelegate(size_t terminate_trigger_bytes) |
| 1349 | : terminate_trigger_bytes_(terminate_trigger_bytes) {} |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1350 | |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 1351 | bool ReceivedBytes(HttpFetcher* fetcher, |
Alex Deymo | 4ea2d92 | 2017-02-05 07:35:32 +0000 | [diff] [blame] | 1352 | const void* bytes, |
| 1353 | size_t length) override { |
| 1354 | LOG(INFO) << "ReceivedBytes, " << length << " bytes."; |
| 1355 | EXPECT_EQ(fetcher, fetcher_.get()); |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 1356 | bool should_terminate = false; |
Alex Deymo | 4ea2d92 | 2017-02-05 07:35:32 +0000 | [diff] [blame] | 1357 | if (bytes_downloaded_ < terminate_trigger_bytes_ && |
| 1358 | bytes_downloaded_ + length >= terminate_trigger_bytes_) { |
| 1359 | MessageLoop::current()->PostTask( |
| 1360 | FROM_HERE, |
| 1361 | base::Bind(&HttpFetcher::TerminateTransfer, |
| 1362 | base::Unretained(fetcher_.get()))); |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 1363 | should_terminate = true; |
Alex Deymo | 4ea2d92 | 2017-02-05 07:35:32 +0000 | [diff] [blame] | 1364 | } |
| 1365 | bytes_downloaded_ += length; |
Tao Bao | 642b32b | 2019-01-02 15:33:42 -0800 | [diff] [blame^] | 1366 | return !should_terminate; |
Alex Deymo | 4ea2d92 | 2017-02-05 07:35:32 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | void TransferComplete(HttpFetcher* fetcher, bool successful) override { |
| 1370 | ADD_FAILURE() << "TransferComplete called but expected a failure"; |
| 1371 | // Destroy the fetcher (because we're allowed to). |
| 1372 | fetcher_.reset(nullptr); |
| 1373 | MessageLoop::current()->BreakLoop(); |
| 1374 | } |
| 1375 | |
| 1376 | void TransferTerminated(HttpFetcher* fetcher) override { |
| 1377 | // Destroy the fetcher (because we're allowed to). |
| 1378 | fetcher_.reset(nullptr); |
| 1379 | MessageLoop::current()->BreakLoop(); |
| 1380 | } |
| 1381 | |
| 1382 | unique_ptr<HttpFetcher> fetcher_; |
| 1383 | size_t bytes_downloaded_{0}; |
| 1384 | size_t terminate_trigger_bytes_; |
| 1385 | }; |
| 1386 | } // namespace |
| 1387 | |
| 1388 | TYPED_TEST(HttpFetcherTest, MultiHttpFetcherTerminateBetweenRangesTest) { |
| 1389 | if (!this->test_.IsMulti()) |
| 1390 | return; |
| 1391 | const size_t kRangeTrigger = 1000; |
| 1392 | MultiHttpFetcherTerminateTestDelegate delegate(kRangeTrigger); |
| 1393 | |
| 1394 | unique_ptr<HttpServer> server(this->test_.CreateServer()); |
| 1395 | ASSERT_TRUE(server->started_); |
| 1396 | |
| 1397 | MultiRangeHttpFetcher* multi_fetcher = |
| 1398 | static_cast<MultiRangeHttpFetcher*>(this->test_.NewLargeFetcher()); |
| 1399 | ASSERT_TRUE(multi_fetcher); |
| 1400 | // Transfer ownership of the fetcher to the delegate. |
| 1401 | delegate.fetcher_.reset(multi_fetcher); |
| 1402 | multi_fetcher->set_delegate(&delegate); |
| 1403 | |
| 1404 | multi_fetcher->ClearRanges(); |
| 1405 | multi_fetcher->AddRange(45, kRangeTrigger); |
| 1406 | multi_fetcher->AddRange(2000, 100); |
| 1407 | |
| 1408 | this->test_.fake_hardware()->SetIsOfficialBuild(false); |
| 1409 | |
| 1410 | StartTransfer(multi_fetcher, this->test_.BigUrl(server->GetPort())); |
| 1411 | MessageLoop::current()->Run(); |
| 1412 | |
| 1413 | // Check that the delegate made it to the trigger point. |
| 1414 | EXPECT_EQ(kRangeTrigger, delegate.bytes_downloaded_); |
| 1415 | } |
Gilad Arnold | 9bedeb5 | 2011-11-17 16:19:57 -0800 | [diff] [blame] | 1416 | |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1417 | namespace { |
Darin Petkov | fc7a0ce | 2010-10-25 10:38:37 -0700 | [diff] [blame] | 1418 | class BlockedTransferTestDelegate : public HttpFetcherDelegate { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1419 | public: |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 1420 | bool ReceivedBytes(HttpFetcher* fetcher, |
| 1421 | const void* bytes, |
| 1422 | size_t length) override { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1423 | ADD_FAILURE(); |
Amin Hassani | 0cd9d77 | 2018-07-31 23:55:43 -0700 | [diff] [blame] | 1424 | return true; |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1425 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 1426 | void TransferComplete(HttpFetcher* fetcher, bool successful) override { |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1427 | EXPECT_FALSE(successful); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1428 | MessageLoop::current()->BreakLoop(); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1429 | } |
Alex Deymo | 610277e | 2014-11-11 21:18:11 -0800 | [diff] [blame] | 1430 | void TransferTerminated(HttpFetcher* fetcher) override { |
Darin Petkov | 9ce452b | 2010-11-17 14:33:28 -0800 | [diff] [blame] | 1431 | ADD_FAILURE(); |
| 1432 | } |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1433 | }; |
| 1434 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1435 | void BlockedTransferTestHelper(AnyHttpFetcherTest* fetcher_test, |
| 1436 | bool is_official_build) { |
| 1437 | if (fetcher_test->IsMock() || fetcher_test->IsMulti()) |
| 1438 | return; |
| 1439 | |
| 1440 | unique_ptr<HttpServer> server(fetcher_test->CreateServer()); |
| 1441 | ASSERT_TRUE(server->started_); |
| 1442 | |
| 1443 | BlockedTransferTestDelegate delegate; |
| 1444 | unique_ptr<HttpFetcher> fetcher(fetcher_test->NewLargeFetcher()); |
| 1445 | LOG(INFO) << "is_official_build: " << is_official_build; |
| 1446 | // NewLargeFetcher creates the HttpFetcher* with a FakeSystemState. |
Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 1447 | fetcher_test->fake_hardware()->SetIsOfficialBuild(is_official_build); |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1448 | fetcher->set_delegate(&delegate); |
| 1449 | |
| 1450 | MessageLoop::current()->PostTask(FROM_HERE, base::Bind( |
| 1451 | StartTransfer, |
| 1452 | fetcher.get(), |
| 1453 | LocalServerUrlForPath(server->GetPort(), |
| 1454 | fetcher_test->SmallUrl(server->GetPort())))); |
| 1455 | MessageLoop::current()->Run(); |
| 1456 | } |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1457 | } // namespace |
| 1458 | |
Darin Petkov | fc7a0ce | 2010-10-25 10:38:37 -0700 | [diff] [blame] | 1459 | TYPED_TEST(HttpFetcherTest, BlockedTransferTest) { |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1460 | BlockedTransferTestHelper(&this->test_, false); |
| 1461 | } |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1462 | |
Alex Deymo | 60ca1a7 | 2015-06-18 18:19:15 -0700 | [diff] [blame] | 1463 | TYPED_TEST(HttpFetcherTest, BlockedTransferOfficialBuildTest) { |
| 1464 | BlockedTransferTestHelper(&this->test_, true); |
Andrew de los Reyes | d57d147 | 2010-10-21 13:34:08 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1467 | } // namespace chromeos_update_engine |