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