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