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