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