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