blob: dcc157368302dd329f7730d825fdc698f79d9a59 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com49fdf182009-10-10 00:57:34 +000016
Gilad Arnoldb6c562a2013-07-01 02:19:26 -070017#include <netinet/in.h>
18#include <netinet/ip.h>
19#include <sys/socket.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +000020#include <unistd.h>
Darin Petkov41c2fcf2010-08-25 13:14:48 -070021
Ben Chan02f7c1d2014-10-18 15:18:02 -070022#include <memory>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000023#include <string>
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070024#include <utility>
adlr@google.comc98a7ed2009-12-04 18:54:03 +000025#include <vector>
Darin Petkov41c2fcf2010-08-25 13:14:48 -070026
Alex Deymo706a5ab2015-11-23 17:48:30 -030027#include <base/bind.h>
Alex Deymo60ca1a72015-06-18 18:19:15 -070028#include <base/location.h>
Andrew de los Reyes45168102010-11-22 11:13:50 -080029#include <base/logging.h>
Alex Deymo535f3b72015-08-07 10:51:32 -070030#include <base/message_loop/message_loop.h>
31#include <base/strings/string_number_conversions.h>
Alex Vakulenko75039d72014-03-25 12:36:28 -070032#include <base/strings/string_util.h>
33#include <base/strings/stringprintf.h>
34#include <base/time/time.h>
Alex Deymo706a5ab2015-11-23 17:48:30 -030035#include <brillo/bind_lambda.h>
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070036#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 Reyes45168102010-11-22 11:13:50 -080042#include <gtest/gtest.h>
43
Alex Deymoc1c17b42015-11-23 03:53:15 -030044#include "update_engine/common/fake_hardware.h"
Alex Deymo2c131bb2016-05-26 16:43:13 -070045#include "update_engine/common/file_fetcher.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080046#include "update_engine/common/http_common.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080047#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"
Alex Deymo14c0da82016-07-20 16:45:45 -070051#include "update_engine/libcurl_http_fetcher.h"
Alex Deymof2858572016-02-25 11:20:13 -080052#include "update_engine/mock_proxy_resolver.h"
Andrew de los Reyes45168102010-11-22 11:13:50 -080053#include "update_engine/proxy_resolver.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000054
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -070055using brillo::MessageLoop;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -070056using std::make_pair;
Andrew de los Reyes819fef22010-12-17 11:33:58 -080057using std::pair;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000058using std::string;
Ben Chan02f7c1d2014-10-18 15:18:02 -070059using std::unique_ptr;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000060using std::vector;
Alex Deymof2858572016-02-25 11:20:13 -080061using testing::DoAll;
62using testing::Return;
63using testing::SaveArg;
64using testing::_;
adlr@google.comc98a7ed2009-12-04 18:54:03 +000065
Gilad Arnold9bedeb52011-11-17 16:19:57 -080066namespace {
67
68const int kBigLength = 100000;
69const int kMediumLength = 1000;
Gilad Arnold34bf1ee2012-02-09 16:16:02 -080070const int kFlakyTruncateLength = 29000;
71const int kFlakySleepEvery = 3;
Gilad Arnold9bedeb52011-11-17 16:19:57 -080072const int kFlakySleepSecs = 10;
73
74} // namespace
75
rspangler@google.com49fdf182009-10-10 00:57:34 +000076namespace chromeos_update_engine {
77
Gilad Arnold9bedeb52011-11-17 16:19:57 -080078static const char *kUnusedUrl = "unused://unused";
79
Gilad Arnoldb6c562a2013-07-01 02:19:26 -070080static inline string LocalServerUrlForPath(in_port_t port,
81 const string& path) {
Alex Vakulenko75039d72014-03-25 12:36:28 -070082 string port_str = (port ? base::StringPrintf(":%hu", port) : "");
Gilad Arnoldb6c562a2013-07-01 02:19:26 -070083 return base::StringPrintf("http://127.0.0.1%s%s", port_str.c_str(),
84 path.c_str());
rspangler@google.com49fdf182009-10-10 00:57:34 +000085}
86
Gilad Arnold9bedeb52011-11-17 16:19:57 -080087//
88// Class hierarchy for HTTP server implementations.
89//
90
91class HttpServer {
rspangler@google.com49fdf182009-10-10 00:57:34 +000092 public:
Gilad Arnold9bedeb52011-11-17 16:19:57 -080093 // This makes it an abstract class (dirty but works).
94 virtual ~HttpServer() = 0;
95
Gilad Arnoldb6c562a2013-07-01 02:19:26 -070096 virtual in_port_t GetPort() const {
97 return 0;
98 }
99
rspangler@google.com49fdf182009-10-10 00:57:34 +0000100 bool started_;
101};
102
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800103HttpServer::~HttpServer() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800105
106class NullHttpServer : public HttpServer {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000107 public:
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800108 NullHttpServer() {
109 started_ = true;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111};
112
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800113
114class PythonHttpServer : public HttpServer {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000115 public:
Alex Deymo535f3b72015-08-07 10:51:32 -0700116 PythonHttpServer() : port_(0) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000117 started_ = false;
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700118
119 // Spawn the server process.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700120 unique_ptr<brillo::Process> http_server(new brillo::ProcessImpl());
Sen Jiang260f03b2016-03-21 15:34:58 -0700121 http_server->AddArg(test_utils::GetBuildArtifactsPath("test_http_server"));
Alex Deymo535f3b72015-08-07 10:51:32 -0700122 http_server->RedirectUsingPipe(STDOUT_FILENO, false);
123
124 if (!http_server->Start()) {
125 ADD_FAILURE() << "failed to spawn http server process";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000126 return;
127 }
Alex Deymo535f3b72015-08-07 10:51:32 -0700128 LOG(INFO) << "started http server with pid " << http_server->pid();
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700129
130 // Wait for server to begin accepting connections, obtain its port.
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700131 brillo::StreamPtr stdout = brillo::FileStream::FromFileDescriptor(
Alex Deymo535f3b72015-08-07 10:51:32 -0700132 http_server->GetPipe(STDOUT_FILENO), false /* own */, nullptr);
133 if (!stdout)
134 return;
135
136 vector<char> buf(128);
137 string line;
138 while (line.find('\n') == string::npos) {
139 size_t read;
140 if (!stdout->ReadBlocking(buf.data(), buf.size(), &read, nullptr)) {
141 ADD_FAILURE() << "error reading http server stdout";
142 return;
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700143 }
Alex Deymo535f3b72015-08-07 10:51:32 -0700144 line.append(buf.data(), read);
145 if (read == 0)
146 break;
147 }
148 // Parse the port from the output line.
149 const size_t listening_msg_prefix_len = strlen(kServerListeningMsgPrefix);
150 if (line.size() < listening_msg_prefix_len) {
151 ADD_FAILURE() << "server output too short";
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700152 return;
153 }
154
Alex Deymo535f3b72015-08-07 10:51:32 -0700155 EXPECT_EQ(kServerListeningMsgPrefix,
156 line.substr(0, listening_msg_prefix_len));
157 string port_str = line.substr(listening_msg_prefix_len);
158 port_str.resize(port_str.find('\n'));
159 EXPECT_TRUE(base::StringToUint(port_str, &port_));
160
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700161 started_ = true;
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700162 LOG(INFO) << "server running, listening on port " << port_;
Alex Deymo535f3b72015-08-07 10:51:32 -0700163
164 // Any failure before this point will SIGKILL the test server if started
165 // when the |http_server| goes out of scope.
166 http_server_ = std::move(http_server);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000167 }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800168
rspangler@google.com49fdf182009-10-10 00:57:34 +0000169 ~PythonHttpServer() {
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700170 // If there's no process, do nothing.
Alex Deymo535f3b72015-08-07 10:51:32 -0700171 if (!http_server_)
rspangler@google.com49fdf182009-10-10 00:57:34 +0000172 return;
Alex Deymo535f3b72015-08-07 10:51:32 -0700173 // Wait up to 10 seconds for the process to finish. Destroying the process
174 // will kill it with a SIGKILL otherwise.
175 http_server_->Kill(SIGTERM, 10);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000176 }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800177
Alex Deymo610277e2014-11-11 21:18:11 -0800178 in_port_t GetPort() const override {
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700179 return port_;
180 }
181
182 private:
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700183 static const char* kServerListeningMsgPrefix;
184
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700185 unique_ptr<brillo::Process> http_server_;
Alex Deymo535f3b72015-08-07 10:51:32 -0700186 unsigned int port_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000187};
188
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700189const char* PythonHttpServer::kServerListeningMsgPrefix = "listening on port ";
190
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800191//
192// Class hierarchy for HTTP fetcher test wrappers.
193//
194
195class AnyHttpFetcherTest {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000196 public:
Chris Sosa77f79e82014-06-02 18:16:24 -0700197 AnyHttpFetcherTest() {}
Alex Deymoc4acdf42014-05-28 21:07:10 -0700198 virtual ~AnyHttpFetcherTest() {}
Jay Srinivasan43488792012-06-19 00:25:31 -0700199
Alex Deymof2858572016-02-25 11:20:13 -0800200 virtual HttpFetcher* NewLargeFetcher(ProxyResolver* proxy_resolver) = 0;
201 HttpFetcher* NewLargeFetcher(size_t num_proxies) {
202 proxy_resolver_.set_num_proxies(num_proxies);
203 return NewLargeFetcher(&proxy_resolver_);
204 }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800205 HttpFetcher* NewLargeFetcher() {
206 return NewLargeFetcher(1);
207 }
208
Alex Deymof2858572016-02-25 11:20:13 -0800209 virtual HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) = 0;
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800210 HttpFetcher* NewSmallFetcher() {
Alex Deymof2858572016-02-25 11:20:13 -0800211 proxy_resolver_.set_num_proxies(1);
212 return NewSmallFetcher(&proxy_resolver_);
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800213 }
214
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700215 virtual string BigUrl(in_port_t port) const { return kUnusedUrl; }
216 virtual string SmallUrl(in_port_t port) const { return kUnusedUrl; }
217 virtual string ErrorUrl(in_port_t port) const { return kUnusedUrl; }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800218
219 virtual bool IsMock() const = 0;
220 virtual bool IsMulti() const = 0;
Alex Deymo2c131bb2016-05-26 16:43:13 -0700221 virtual bool IsHttpSupported() const = 0;
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800222
223 virtual void IgnoreServerAborting(HttpServer* server) const {}
224
Alex Deymo60ca1a72015-06-18 18:19:15 -0700225 virtual HttpServer* CreateServer() = 0;
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800226
Alex Deymoc1c17b42015-11-23 03:53:15 -0300227 FakeHardware* fake_hardware() {
Alex Deymo706a5ab2015-11-23 17:48:30 -0300228 return &fake_hardware_;
Alex Deymoc1c17b42015-11-23 03:53:15 -0300229 }
230
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800231 protected:
232 DirectProxyResolver proxy_resolver_;
Alex Deymo706a5ab2015-11-23 17:48:30 -0300233 FakeHardware fake_hardware_;
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800234};
235
236class MockHttpFetcherTest : public AnyHttpFetcherTest {
237 public:
238 // Necessary to unhide the definition in the base class.
239 using AnyHttpFetcherTest::NewLargeFetcher;
Alex Deymof2858572016-02-25 11:20:13 -0800240 HttpFetcher* NewLargeFetcher(ProxyResolver* proxy_resolver) override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700241 brillo::Blob big_data(1000000);
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800242 return new MockHttpFetcher(
Alex Deymof2858572016-02-25 11:20:13 -0800243 big_data.data(), big_data.size(), proxy_resolver);
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800244 }
245
246 // Necessary to unhide the definition in the base class.
247 using AnyHttpFetcherTest::NewSmallFetcher;
Alex Deymof2858572016-02-25 11:20:13 -0800248 HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) override {
249 return new MockHttpFetcher("x", 1, proxy_resolver);
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800250 }
251
Alex Deymo610277e2014-11-11 21:18:11 -0800252 bool IsMock() const override { return true; }
253 bool IsMulti() const override { return false; }
Alex Deymo2c131bb2016-05-26 16:43:13 -0700254 bool IsHttpSupported() const override { return true; }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800255
Alex Deymo60ca1a72015-06-18 18:19:15 -0700256 HttpServer* CreateServer() override {
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800257 return new NullHttpServer;
258 }
259};
260
261class LibcurlHttpFetcherTest : public AnyHttpFetcherTest {
262 public:
263 // Necessary to unhide the definition in the base class.
264 using AnyHttpFetcherTest::NewLargeFetcher;
Alex Deymof2858572016-02-25 11:20:13 -0800265 HttpFetcher* NewLargeFetcher(ProxyResolver* proxy_resolver) override {
266 LibcurlHttpFetcher* ret =
267 new LibcurlHttpFetcher(proxy_resolver, &fake_hardware_);
Darin Petkovb83371f2010-08-17 09:34:49 -0700268 // Speed up test execution.
269 ret->set_idle_seconds(1);
270 ret->set_retry_seconds(1);
Alex Deymo706a5ab2015-11-23 17:48:30 -0300271 fake_hardware_.SetIsOfficialBuild(false);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000272 return ret;
273 }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800274
275 // Necessary to unhide the definition in the base class.
276 using AnyHttpFetcherTest::NewSmallFetcher;
Alex Deymof2858572016-02-25 11:20:13 -0800277 HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) override {
278 return NewLargeFetcher(proxy_resolver);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000279 }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800280
Alex Deymo610277e2014-11-11 21:18:11 -0800281 string BigUrl(in_port_t port) const override {
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700282 return LocalServerUrlForPath(port,
283 base::StringPrintf("/download/%d",
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800284 kBigLength));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000285 }
Alex Deymo610277e2014-11-11 21:18:11 -0800286 string SmallUrl(in_port_t port) const override {
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700287 return LocalServerUrlForPath(port, "/foo");
rspangler@google.com49fdf182009-10-10 00:57:34 +0000288 }
Alex Deymo610277e2014-11-11 21:18:11 -0800289 string ErrorUrl(in_port_t port) const override {
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700290 return LocalServerUrlForPath(port, "/error");
Gilad Arnold48085ba2011-11-16 09:36:08 -0800291 }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800292
Alex Deymo610277e2014-11-11 21:18:11 -0800293 bool IsMock() const override { return false; }
294 bool IsMulti() const override { return false; }
Alex Deymo2c131bb2016-05-26 16:43:13 -0700295 bool IsHttpSupported() const override { return true; }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800296
Alex Deymo610277e2014-11-11 21:18:11 -0800297 void IgnoreServerAborting(HttpServer* server) const override {
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700298 // Nothing to do.
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700299 }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800300
Alex Deymo60ca1a72015-06-18 18:19:15 -0700301 HttpServer* CreateServer() override {
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800302 return new PythonHttpServer;
303 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000304};
305
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800306class MultiRangeHttpFetcherTest : public LibcurlHttpFetcherTest {
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700307 public:
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800308 // Necessary to unhide the definition in the base class.
309 using AnyHttpFetcherTest::NewLargeFetcher;
Alex Deymof2858572016-02-25 11:20:13 -0800310 HttpFetcher* NewLargeFetcher(ProxyResolver* proxy_resolver) override {
311 MultiRangeHttpFetcher* ret = new MultiRangeHttpFetcher(
312 new LibcurlHttpFetcher(proxy_resolver, &fake_hardware_));
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800313 ret->ClearRanges();
Gilad Arnolde4ad2502011-12-29 17:08:54 -0800314 ret->AddRange(0);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700315 // Speed up test execution.
316 ret->set_idle_seconds(1);
317 ret->set_retry_seconds(1);
Alex Deymo706a5ab2015-11-23 17:48:30 -0300318 fake_hardware_.SetIsOfficialBuild(false);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700319 return ret;
320 }
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800321
322 // Necessary to unhide the definition in the base class.
323 using AnyHttpFetcherTest::NewSmallFetcher;
Alex Deymof2858572016-02-25 11:20:13 -0800324 HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) override {
325 return NewLargeFetcher(proxy_resolver);
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800326 }
327
Alex Deymo610277e2014-11-11 21:18:11 -0800328 bool IsMulti() const override { return true; }
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -0700329};
330
Alex Deymo2c131bb2016-05-26 16:43:13 -0700331class FileFetcherTest : public AnyHttpFetcherTest {
332 public:
333 // Necessary to unhide the definition in the base class.
334 using AnyHttpFetcherTest::NewLargeFetcher;
335 HttpFetcher* NewLargeFetcher(ProxyResolver* /* proxy_resolver */) override {
336 return new FileFetcher();
337 }
338
339 // Necessary to unhide the definition in the base class.
340 using AnyHttpFetcherTest::NewSmallFetcher;
341 HttpFetcher* NewSmallFetcher(ProxyResolver* proxy_resolver) override {
342 return NewLargeFetcher(proxy_resolver);
343 }
344
345 string BigUrl(in_port_t port) const override {
346 return "file://" + temp_file_.path();
347 }
348 string SmallUrl(in_port_t port) const override {
349 test_utils::WriteFileString(temp_file_.path(), "small contents");
350 return "file://" + temp_file_.path();
351 }
352 string ErrorUrl(in_port_t port) const override {
353 return "file:///path/to/non-existing-file";
354 }
355
356 bool IsMock() const override { return false; }
357 bool IsMulti() const override { return false; }
358 bool IsHttpSupported() const override { return false; }
359
360 void IgnoreServerAborting(HttpServer* server) const override {}
361
362 HttpServer* CreateServer() override { return new NullHttpServer; }
363
364 private:
365 test_utils::ScopedTempFile temp_file_{"ue_file_fetcher.XXXXXX"};
366};
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800367
368//
369// Infrastructure for type tests of HTTP fetcher.
370// See: http://code.google.com/p/googletest/wiki/AdvancedGuide#Typed_Tests
371//
372
373// Fixture class template. We use an explicit constraint to guarantee that it
374// can only be instantiated with an AnyHttpFetcherTest type, see:
375// http://www2.research.att.com/~bs/bs_faq2.html#constraints
376template <typename T>
377class HttpFetcherTest : public ::testing::Test {
378 public:
Alex Deymo535f3b72015-08-07 10:51:32 -0700379 base::MessageLoopForIO base_loop_;
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700380 brillo::BaseMessageLoop loop_{&base_loop_};
Alex Deymo60ca1a72015-06-18 18:19:15 -0700381
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800382 T test_;
383
Alex Deymo60ca1a72015-06-18 18:19:15 -0700384 protected:
385 HttpFetcherTest() {
386 loop_.SetAsCurrent();
387 }
388
389 void TearDown() override {
Alex Vakulenko3f39d5c2015-10-13 09:27:13 -0700390 EXPECT_EQ(0, brillo::MessageLoopRunMaxIterations(&loop_, 1));
Alex Deymo60ca1a72015-06-18 18:19:15 -0700391 }
392
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800393 private:
Alex Deymo60ca1a72015-06-18 18:19:15 -0700394 static void TypeConstraint(T* a) {
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800395 AnyHttpFetcherTest *b = a;
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700396 if (b == 0) // Silence compiler warning of unused variable.
Yunlian Jiang2dac5762013-04-12 09:53:09 -0700397 *b = a;
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800398 }
399};
400
401// Test case types list.
402typedef ::testing::Types<LibcurlHttpFetcherTest,
403 MockHttpFetcherTest,
Alex Deymo2c131bb2016-05-26 16:43:13 -0700404 MultiRangeHttpFetcherTest,
405 FileFetcherTest>
406 HttpFetcherTestTypes;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000407TYPED_TEST_CASE(HttpFetcherTest, HttpFetcherTestTypes);
408
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800409
rspangler@google.com49fdf182009-10-10 00:57:34 +0000410namespace {
411class HttpFetcherTestDelegate : public HttpFetcherDelegate {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000412 public:
Alex Deymofdd6dec2016-03-03 22:35:43 -0800413 HttpFetcherTestDelegate() = default;
Gilad Arnold48085ba2011-11-16 09:36:08 -0800414
Alex Deymo610277e2014-11-11 21:18:11 -0800415 void ReceivedBytes(HttpFetcher* /* fetcher */,
Alex Deymofdd6dec2016-03-03 22:35:43 -0800416 const void* bytes,
417 size_t length) override {
418 data.append(reinterpret_cast<const char*>(bytes), length);
Gilad Arnold48085ba2011-11-16 09:36:08 -0800419 // Update counters
420 times_received_bytes_called_++;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000421 }
Gilad Arnold48085ba2011-11-16 09:36:08 -0800422
Alex Deymo610277e2014-11-11 21:18:11 -0800423 void TransferComplete(HttpFetcher* fetcher, bool successful) override {
Gilad Arnold48085ba2011-11-16 09:36:08 -0800424 if (is_expect_error_)
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800425 EXPECT_EQ(kHttpResponseNotFound, fetcher->http_response_code());
Gilad Arnold48085ba2011-11-16 09:36:08 -0800426 else
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800427 EXPECT_EQ(kHttpResponseOk, fetcher->http_response_code());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700428 MessageLoop::current()->BreakLoop();
Gilad Arnold48085ba2011-11-16 09:36:08 -0800429
430 // Update counter
431 times_transfer_complete_called_++;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000432 }
Gilad Arnold48085ba2011-11-16 09:36:08 -0800433
Alex Deymo610277e2014-11-11 21:18:11 -0800434 void TransferTerminated(HttpFetcher* fetcher) override {
Gilad Arnold48085ba2011-11-16 09:36:08 -0800435 times_transfer_terminated_called_++;
Alex Deymo2c131bb2016-05-26 16:43:13 -0700436 MessageLoop::current()->BreakLoop();
Darin Petkov9ce452b2010-11-17 14:33:28 -0800437 }
Gilad Arnold48085ba2011-11-16 09:36:08 -0800438
Gilad Arnold48085ba2011-11-16 09:36:08 -0800439 // Are we expecting an error response? (default: no)
Alex Deymofdd6dec2016-03-03 22:35:43 -0800440 bool is_expect_error_{false};
Gilad Arnold48085ba2011-11-16 09:36:08 -0800441
442 // Counters for callback invocations.
Alex Deymofdd6dec2016-03-03 22:35:43 -0800443 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.com49fdf182009-10-10 00:57:34 +0000449};
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000450
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000451
Alex Deymo60ca1a72015-06-18 18:19:15 -0700452void StartTransfer(HttpFetcher* http_fetcher, const string& url) {
453 http_fetcher->BeginTransfer(url);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000454}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700455} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000456
457TYPED_TEST(HttpFetcherTest, SimpleTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700458 HttpFetcherTestDelegate delegate;
459 unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher());
460 fetcher->set_delegate(&delegate);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000461
Alex Deymo60ca1a72015-06-18 18:19:15 -0700462 unique_ptr<HttpServer> server(this->test_.CreateServer());
463 ASSERT_TRUE(server->started_);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000464
Alex Deymo60ca1a72015-06-18 18:19:15 -0700465 this->loop_.PostTask(FROM_HERE, base::Bind(
466 StartTransfer,
467 fetcher.get(),
468 this->test_.SmallUrl(server->GetPort())));
469 this->loop_.Run();
Alex Deymo71f67622017-02-03 21:30:24 -0800470 EXPECT_EQ(0, delegate.times_transfer_terminated_called_);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000471}
472
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700473TYPED_TEST(HttpFetcherTest, SimpleBigTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700474 HttpFetcherTestDelegate delegate;
475 unique_ptr<HttpFetcher> fetcher(this->test_.NewLargeFetcher());
476 fetcher->set_delegate(&delegate);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700477
Alex Deymo60ca1a72015-06-18 18:19:15 -0700478 unique_ptr<HttpServer> server(this->test_.CreateServer());
479 ASSERT_TRUE(server->started_);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700480
Alex Deymo60ca1a72015-06-18 18:19:15 -0700481 this->loop_.PostTask(FROM_HERE, base::Bind(
482 StartTransfer,
483 fetcher.get(),
484 this->test_.BigUrl(server->GetPort())));
485 this->loop_.Run();
Alex Deymo71f67622017-02-03 21:30:24 -0800486 EXPECT_EQ(0, delegate.times_transfer_terminated_called_);
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700487}
488
Gilad Arnold48085ba2011-11-16 09:36:08 -0800489// 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.
491TYPED_TEST(HttpFetcherTest, ErrorTest) {
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800492 if (this->test_.IsMock() || this->test_.IsMulti())
Gilad Arnold48085ba2011-11-16 09:36:08 -0800493 return;
Alex Deymo60ca1a72015-06-18 18:19:15 -0700494 HttpFetcherTestDelegate delegate;
Gilad Arnold48085ba2011-11-16 09:36:08 -0800495
Alex Deymo60ca1a72015-06-18 18:19:15 -0700496 // Delegate should expect an error response.
497 delegate.is_expect_error_ = true;
Gilad Arnold48085ba2011-11-16 09:36:08 -0800498
Alex Deymo60ca1a72015-06-18 18:19:15 -0700499 unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher());
500 fetcher->set_delegate(&delegate);
Gilad Arnold48085ba2011-11-16 09:36:08 -0800501
Alex Deymo60ca1a72015-06-18 18:19:15 -0700502 unique_ptr<HttpServer> server(this->test_.CreateServer());
503 ASSERT_TRUE(server->started_);
Gilad Arnold48085ba2011-11-16 09:36:08 -0800504
Alex Deymo60ca1a72015-06-18 18:19:15 -0700505 this->loop_.PostTask(FROM_HERE, base::Bind(
506 StartTransfer,
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800507 fetcher.get(),
Alex Deymo60ca1a72015-06-18 18:19:15 -0700508 this->test_.ErrorUrl(server->GetPort())));
509 this->loop_.Run();
Gilad Arnold48085ba2011-11-16 09:36:08 -0800510
Alex Deymo60ca1a72015-06-18 18:19:15 -0700511 // Make sure that no bytes were received.
Alex Deymo71f67622017-02-03 21:30:24 -0800512 EXPECT_EQ(0, delegate.times_received_bytes_called_);
513 EXPECT_EQ(0U, fetcher->GetBytesDownloaded());
Gilad Arnold48085ba2011-11-16 09:36:08 -0800514
Alex Deymo60ca1a72015-06-18 18:19:15 -0700515 // Make sure that transfer completion was signaled once, and no termination
516 // was signaled.
Alex Deymo71f67622017-02-03 21:30:24 -0800517 EXPECT_EQ(1, delegate.times_transfer_complete_called_);
518 EXPECT_EQ(0, delegate.times_transfer_terminated_called_);
Gilad Arnold48085ba2011-11-16 09:36:08 -0800519}
520
Alex Deymofdd6dec2016-03-03 22:35:43 -0800521TYPED_TEST(HttpFetcherTest, ExtraHeadersInRequestTest) {
Alex Deymo2c131bb2016-05-26 16:43:13 -0700522 if (this->test_.IsMock() || !this->test_.IsHttpSupported())
Alex Deymofdd6dec2016-03-03 22:35:43 -0800523 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 Deymo2c131bb2016-05-26 16:43:13 -0700543 this->loop_.PostTask(
544 FROM_HERE,
545 base::Bind(StartTransfer,
546 fetcher.get(),
547 LocalServerUrlForPath(port, "/echo-headers")));
Alex Deymofdd6dec2016-03-03 22:35:43 -0800548 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.com49fdf182009-10-10 00:57:34 +0000560namespace {
561class PausingHttpFetcherTestDelegate : public HttpFetcherDelegate {
562 public:
Alex Deymo610277e2014-11-11 21:18:11 -0800563 void ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800564 const void* /* bytes */, size_t /* length */) override {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000565 CHECK(!paused_);
566 paused_ = true;
567 fetcher->Pause();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000568 }
Alex Deymo610277e2014-11-11 21:18:11 -0800569 void TransferComplete(HttpFetcher* fetcher, bool successful) override {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700570 MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000571 }
Alex Deymo610277e2014-11-11 21:18:11 -0800572 void TransferTerminated(HttpFetcher* fetcher) override {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800573 ADD_FAILURE();
574 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000575 void Unpause() {
576 CHECK(paused_);
577 paused_ = false;
578 fetcher_->Unpause();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000579 }
580 bool paused_;
581 HttpFetcher* fetcher_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000582};
583
Alex Deymo60ca1a72015-06-18 18:19:15 -0700584void UnpausingTimeoutCallback(PausingHttpFetcherTestDelegate* delegate,
585 MessageLoop::TaskId* my_id) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000586 if (delegate->paused_)
587 delegate->Unpause();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700588 // Update the task id with the new scheduled callback.
589 *my_id = MessageLoop::current()->PostDelayedTask(
590 FROM_HERE,
591 base::Bind(&UnpausingTimeoutCallback, delegate, my_id),
592 base::TimeDelta::FromMilliseconds(200));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000593}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700594} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000595
596TYPED_TEST(HttpFetcherTest, PauseTest) {
Alex Deymof2858572016-02-25 11:20:13 -0800597 PausingHttpFetcherTestDelegate delegate;
598 unique_ptr<HttpFetcher> fetcher(this->test_.NewLargeFetcher());
599 delegate.paused_ = false;
600 delegate.fetcher_ = fetcher.get();
601 fetcher->set_delegate(&delegate);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000602
Alex Deymof2858572016-02-25 11:20:13 -0800603 unique_ptr<HttpServer> server(this->test_.CreateServer());
604 ASSERT_TRUE(server->started_);
Andrew de los Reyesf3ed8e72011-02-16 10:35:46 -0800605
Alex Deymof2858572016-02-25 11:20:13 -0800606 MessageLoop::TaskId callback_id;
607 callback_id = this->loop_.PostDelayedTask(
608 FROM_HERE,
609 base::Bind(&UnpausingTimeoutCallback, &delegate, &callback_id),
610 base::TimeDelta::FromMilliseconds(200));
611 fetcher->BeginTransfer(this->test_.BigUrl(server->GetPort()));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000612
Alex Deymof2858572016-02-25 11:20:13 -0800613 this->loop_.Run();
614 EXPECT_TRUE(this->loop_.CancelTask(callback_id));
615}
616
617// This test will pause the fetcher while the download is not yet started
618// because it is waiting for the proxy to be resolved.
619TYPED_TEST(HttpFetcherTest, PauseWhileResolvingProxyTest) {
Alex Deymo2c131bb2016-05-26 16:43:13 -0700620 if (this->test_.IsMock() || !this->test_.IsHttpSupported())
Alex Deymof2858572016-02-25 11:20:13 -0800621 return;
622 MockProxyResolver mock_resolver;
623 unique_ptr<HttpFetcher> fetcher(this->test_.NewLargeFetcher(&mock_resolver));
624
625 // Saved arguments from the proxy call.
Alex Deymo35821942017-02-05 04:36:02 +0000626 ProxiesResolvedFn proxy_callback;
627 EXPECT_CALL(mock_resolver, GetProxiesForUrl("http://fake_url", _))
628 .WillOnce(DoAll(SaveArg<1>(&proxy_callback), Return(true)));
Alex Deymof2858572016-02-25 11:20:13 -0800629 fetcher->BeginTransfer("http://fake_url");
630 testing::Mock::VerifyAndClearExpectations(&mock_resolver);
631
632 // Pausing and unpausing while resolving the proxy should not affect anything.
633 fetcher->Pause();
634 fetcher->Unpause();
635 fetcher->Pause();
636 // Proxy resolver comes back after we paused the fetcher.
637 ASSERT_TRUE(proxy_callback);
Alex Deymo35821942017-02-05 04:36:02 +0000638 proxy_callback.Run({1, kNoProxy});
rspangler@google.com49fdf182009-10-10 00:57:34 +0000639}
640
641namespace {
642class AbortingHttpFetcherTestDelegate : public HttpFetcherDelegate {
643 public:
Alex Deymo610277e2014-11-11 21:18:11 -0800644 void ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800645 const void* bytes, size_t length) override {}
Alex Deymo610277e2014-11-11 21:18:11 -0800646 void TransferComplete(HttpFetcher* fetcher, bool successful) override {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800647 ADD_FAILURE(); // We should never get here
Alex Deymo60ca1a72015-06-18 18:19:15 -0700648 MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000649 }
Alex Deymo610277e2014-11-11 21:18:11 -0800650 void TransferTerminated(HttpFetcher* fetcher) override {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800651 EXPECT_EQ(fetcher, fetcher_.get());
652 EXPECT_FALSE(once_);
653 EXPECT_TRUE(callback_once_);
654 callback_once_ = false;
Alex Deymoc4acdf42014-05-28 21:07:10 -0700655 // The fetcher could have a callback scheduled on the ProxyResolver that
656 // can fire after this callback. We wait until the end of the test to
657 // delete the fetcher.
Andrew de los Reyes819fef22010-12-17 11:33:58 -0800658 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000659 void TerminateTransfer() {
660 CHECK(once_);
661 once_ = false;
662 fetcher_->TerminateTransfer();
663 }
664 void EndLoop() {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700665 MessageLoop::current()->BreakLoop();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000666 }
667 bool once_;
Darin Petkov9ce452b2010-11-17 14:33:28 -0800668 bool callback_once_;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700669 unique_ptr<HttpFetcher> fetcher_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000670};
671
Alex Deymo60ca1a72015-06-18 18:19:15 -0700672void AbortingTimeoutCallback(AbortingHttpFetcherTestDelegate* delegate,
673 MessageLoop::TaskId* my_id) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000674 if (delegate->once_) {
675 delegate->TerminateTransfer();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700676 *my_id = MessageLoop::current()->PostTask(
677 FROM_HERE,
678 base::Bind(AbortingTimeoutCallback, delegate, my_id));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000679 } else {
680 delegate->EndLoop();
Alex Deymo60ca1a72015-06-18 18:19:15 -0700681 *my_id = MessageLoop::kTaskIdNull;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000682 }
683}
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700684} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000685
686TYPED_TEST(HttpFetcherTest, AbortTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -0700687 AbortingHttpFetcherTestDelegate delegate;
688 delegate.fetcher_.reset(this->test_.NewLargeFetcher());
689 delegate.once_ = true;
690 delegate.callback_once_ = true;
691 delegate.fetcher_->set_delegate(&delegate);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000692
Alex Deymo60ca1a72015-06-18 18:19:15 -0700693 unique_ptr<HttpServer> server(this->test_.CreateServer());
694 this->test_.IgnoreServerAborting(server.get());
695 ASSERT_TRUE(server->started_);
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800696
Alex Deymo60ca1a72015-06-18 18:19:15 -0700697 MessageLoop::TaskId task_id = MessageLoop::kTaskIdNull;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000698
Alex Deymo60ca1a72015-06-18 18:19:15 -0700699 task_id = this->loop_.PostTask(
700 FROM_HERE,
701 base::Bind(AbortingTimeoutCallback, &delegate, &task_id));
702 delegate.fetcher_->BeginTransfer(this->test_.BigUrl(server->GetPort()));
703
704 this->loop_.Run();
705 CHECK(!delegate.once_);
706 CHECK(!delegate.callback_once_);
707 this->loop_.CancelTask(task_id);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000708}
709
Alex Deymo71f67622017-02-03 21:30:24 -0800710TYPED_TEST(HttpFetcherTest, TerminateTransferWhileResolvingProxyTest) {
711 if (this->test_.IsMock() || !this->test_.IsHttpSupported())
712 return;
713 MockProxyResolver mock_resolver;
714 unique_ptr<HttpFetcher> fetcher(this->test_.NewLargeFetcher(&mock_resolver));
715
716 HttpFetcherTestDelegate delegate;
717 fetcher->set_delegate(&delegate);
718
719 EXPECT_CALL(mock_resolver, GetProxiesForUrl(_, _)).WillOnce(Return(123));
720 fetcher->BeginTransfer("http://fake_url");
721 // Run the message loop until idle. This must call the MockProxyResolver with
722 // the request.
723 while (this->loop_.RunOnce(false)) {
724 }
725 testing::Mock::VerifyAndClearExpectations(&mock_resolver);
726
727 EXPECT_CALL(mock_resolver, CancelProxyRequest(123)).WillOnce(Return(true));
728
729 // Terminate the transfer right before the proxy resolution response.
730 fetcher->TerminateTransfer();
731 EXPECT_EQ(0, delegate.times_received_bytes_called_);
732 EXPECT_EQ(0, delegate.times_transfer_complete_called_);
733 EXPECT_EQ(1, delegate.times_transfer_terminated_called_);
734}
735
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000736namespace {
737class FlakyHttpFetcherTestDelegate : public HttpFetcherDelegate {
738 public:
Alex Deymo610277e2014-11-11 21:18:11 -0800739 void ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800740 const void* bytes, size_t length) override {
741 data.append(reinterpret_cast<const char*>(bytes), length);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000742 }
Alex Deymo610277e2014-11-11 21:18:11 -0800743 void TransferComplete(HttpFetcher* fetcher, bool successful) override {
Andrew de los Reyesfb4ad7d2010-07-19 10:43:46 -0700744 EXPECT_TRUE(successful);
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800745 EXPECT_EQ(kHttpResponsePartialContent, fetcher->http_response_code());
Alex Deymo60ca1a72015-06-18 18:19:15 -0700746 MessageLoop::current()->BreakLoop();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000747 }
Alex Deymo610277e2014-11-11 21:18:11 -0800748 void TransferTerminated(HttpFetcher* fetcher) override {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800749 ADD_FAILURE();
750 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000751 string data;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000752};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700753} // namespace
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000754
755TYPED_TEST(HttpFetcherTest, FlakyTest) {
Alex Deymo2c131bb2016-05-26 16:43:13 -0700756 if (this->test_.IsMock() || !this->test_.IsHttpSupported())
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000757 return;
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000758 {
759 FlakyHttpFetcherTestDelegate delegate;
Ben Chan02f7c1d2014-10-18 15:18:02 -0700760 unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher());
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000761 fetcher->set_delegate(&delegate);
762
Ben Chan02f7c1d2014-10-18 15:18:02 -0700763 unique_ptr<HttpServer> server(this->test_.CreateServer());
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800764 ASSERT_TRUE(server->started_);
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000765
Alex Deymo60ca1a72015-06-18 18:19:15 -0700766 this->loop_.PostTask(FROM_HERE, base::Bind(
767 &StartTransfer,
768 fetcher.get(),
769 LocalServerUrlForPath(server->GetPort(),
770 base::StringPrintf("/flaky/%d/%d/%d/%d",
771 kBigLength,
772 kFlakyTruncateLength,
773 kFlakySleepEvery,
774 kFlakySleepSecs))));
775 this->loop_.Run();
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000776
777 // verify the data we get back
Alex Deymo80f70ff2016-02-10 16:08:11 -0800778 ASSERT_EQ(kBigLength, static_cast<int>(delegate.data.size()));
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800779 for (int i = 0; i < kBigLength; i += 10) {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000780 // Assert so that we don't flood the screen w/ EXPECT errors on failure.
781 ASSERT_EQ(delegate.data.substr(i, 10), "abcdefghij");
782 }
783 }
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000784}
785
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700786namespace {
Chris Sosa0a364bb2014-06-10 18:18:24 -0700787// This delegate kills the server attached to it after receiving any bytes.
788// This can be used for testing what happens when you try to fetch data and
789// the server dies.
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700790class FailureHttpFetcherTestDelegate : public HttpFetcherDelegate {
791 public:
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700792 explicit FailureHttpFetcherTestDelegate(PythonHttpServer* server)
Alex Deymo60ca1a72015-06-18 18:19:15 -0700793 : server_(server) {}
Jay Srinivasan135a58b2012-07-13 12:46:49 -0700794
Alex Deymo610277e2014-11-11 21:18:11 -0800795 ~FailureHttpFetcherTestDelegate() override {
Jay Srinivasan135a58b2012-07-13 12:46:49 -0700796 if (server_) {
797 LOG(INFO) << "Stopping server in destructor";
Alex Deymob20de692017-02-05 07:47:37 +0000798 server_.reset();
Jay Srinivasan135a58b2012-07-13 12:46:49 -0700799 LOG(INFO) << "server stopped";
800 }
801 }
802
Alex Deymo610277e2014-11-11 21:18:11 -0800803 void ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800804 const void* bytes, size_t length) override {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700805 if (server_) {
Jay Srinivasan135a58b2012-07-13 12:46:49 -0700806 LOG(INFO) << "Stopping server in ReceivedBytes";
Alex Deymob20de692017-02-05 07:47:37 +0000807 server_.reset();
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700808 LOG(INFO) << "server stopped";
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700809 }
810 }
Alex Deymo610277e2014-11-11 21:18:11 -0800811 void TransferComplete(HttpFetcher* fetcher, bool successful) override {
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700812 EXPECT_FALSE(successful);
Chris Sosa0a364bb2014-06-10 18:18:24 -0700813 EXPECT_EQ(0, fetcher->http_response_code());
Alex Deymob20de692017-02-05 07:47:37 +0000814 times_transfer_complete_called_++;
Alex Deymo60ca1a72015-06-18 18:19:15 -0700815 MessageLoop::current()->BreakLoop();
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700816 }
Alex Deymo610277e2014-11-11 21:18:11 -0800817 void TransferTerminated(HttpFetcher* fetcher) override {
Alex Deymob20de692017-02-05 07:47:37 +0000818 times_transfer_terminated_called_++;
819 MessageLoop::current()->BreakLoop();
Darin Petkov9ce452b2010-11-17 14:33:28 -0800820 }
Alex Deymob20de692017-02-05 07:47:37 +0000821 unique_ptr<PythonHttpServer> server_;
822 int times_transfer_terminated_called_{0};
823 int times_transfer_complete_called_{0};
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700824};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700825} // namespace
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700826
827
828TYPED_TEST(HttpFetcherTest, FailureTest) {
Chris Sosa0a364bb2014-06-10 18:18:24 -0700829 // This test ensures that a fetcher responds correctly when a server isn't
830 // available at all.
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800831 if (this->test_.IsMock())
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700832 return;
Alex Deymob20de692017-02-05 07:47:37 +0000833 FailureHttpFetcherTestDelegate delegate(nullptr);
834 unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher());
835 fetcher->set_delegate(&delegate);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700836
Alex Deymob20de692017-02-05 07:47:37 +0000837 this->loop_.PostTask(
838 FROM_HERE,
839 base::Bind(
840 StartTransfer, fetcher.get(), "http://host_doesnt_exist99999999"));
841 this->loop_.Run();
842 EXPECT_EQ(1, delegate.times_transfer_complete_called_);
843 EXPECT_EQ(0, delegate.times_transfer_terminated_called_);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700844
Alex Deymob20de692017-02-05 07:47:37 +0000845 // Exiting and testing happens in the delegate
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700846}
847
Alex Deymof123ae22015-09-24 14:59:43 -0700848TYPED_TEST(HttpFetcherTest, NoResponseTest) {
849 // This test starts a new http server but the server doesn't respond and just
850 // closes the connection.
851 if (this->test_.IsMock())
852 return;
853
854 PythonHttpServer* server = new PythonHttpServer();
855 int port = server->GetPort();
856 ASSERT_TRUE(server->started_);
857
858 // Handles destruction and claims ownership.
859 FailureHttpFetcherTestDelegate delegate(server);
860 unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher());
861 fetcher->set_delegate(&delegate);
862 // The server will not reply at all, so we can limit the execution time of the
863 // test by reducing the low-speed timeout to something small. The test will
864 // finish once the TimeoutCallback() triggers (every second) and the timeout
865 // expired.
866 fetcher->set_low_speed_limit(kDownloadLowSpeedLimitBps, 1);
867
868 this->loop_.PostTask(FROM_HERE, base::Bind(
869 StartTransfer,
870 fetcher.get(),
871 LocalServerUrlForPath(port, "/hang")));
872 this->loop_.Run();
Alex Deymob20de692017-02-05 07:47:37 +0000873 EXPECT_EQ(1, delegate.times_transfer_complete_called_);
874 EXPECT_EQ(0, delegate.times_transfer_terminated_called_);
Alex Deymof123ae22015-09-24 14:59:43 -0700875
876 // Check that no other callback runs in the next two seconds. That would
877 // indicate a leaked callback.
878 bool timeout = false;
Luis Hector Chavezf1cf3482016-07-19 14:29:19 -0700879 auto callback = base::Bind([](bool* timeout) { *timeout = true; },
880 base::Unretained(&timeout));
Alex Deymof123ae22015-09-24 14:59:43 -0700881 this->loop_.PostDelayedTask(FROM_HERE, callback,
882 base::TimeDelta::FromSeconds(2));
883 EXPECT_TRUE(this->loop_.RunOnce(true));
884 EXPECT_TRUE(timeout);
885}
886
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700887TYPED_TEST(HttpFetcherTest, ServerDiesTest) {
Chris Sosa0a364bb2014-06-10 18:18:24 -0700888 // This test starts a new http server and kills it after receiving its first
889 // set of bytes. It test whether or not our fetcher eventually gives up on
890 // retries and aborts correctly.
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800891 if (this->test_.IsMock())
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700892 return;
Alex Deymob20de692017-02-05 07:47:37 +0000893 PythonHttpServer* server = new PythonHttpServer();
894 int port = server->GetPort();
895 ASSERT_TRUE(server->started_);
Chris Sosa0a364bb2014-06-10 18:18:24 -0700896
Alex Deymob20de692017-02-05 07:47:37 +0000897 // Handles destruction and claims ownership.
898 FailureHttpFetcherTestDelegate delegate(server);
899 unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher());
900 fetcher->set_delegate(&delegate);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700901
Alex Deymob20de692017-02-05 07:47:37 +0000902 this->loop_.PostTask(
903 FROM_HERE,
904 base::Bind(StartTransfer,
905 fetcher.get(),
906 LocalServerUrlForPath(port,
907 base::StringPrintf("/flaky/%d/%d/%d/%d",
908 kBigLength,
909 kFlakyTruncateLength,
910 kFlakySleepEvery,
911 kFlakySleepSecs))));
912 this->loop_.Run();
913 EXPECT_EQ(1, delegate.times_transfer_complete_called_);
914 EXPECT_EQ(0, delegate.times_transfer_terminated_called_);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700915
Alex Deymob20de692017-02-05 07:47:37 +0000916 // Exiting and testing happens in the delegate
917}
918
919// Test that we can cancel a transfer while it is still trying to connect to the
920// server. This test kills the server after a few bytes are received.
921TYPED_TEST(HttpFetcherTest, TerminateTransferWhenServerDiedTest) {
922 if (this->test_.IsMock() || !this->test_.IsHttpSupported())
923 return;
924
925 PythonHttpServer* server = new PythonHttpServer();
926 int port = server->GetPort();
927 ASSERT_TRUE(server->started_);
928
929 // Handles destruction and claims ownership.
930 FailureHttpFetcherTestDelegate delegate(server);
931 unique_ptr<HttpFetcher> fetcher(this->test_.NewSmallFetcher());
932 fetcher->set_delegate(&delegate);
933
934 this->loop_.PostTask(
935 FROM_HERE,
936 base::Bind(StartTransfer,
937 fetcher.get(),
938 LocalServerUrlForPath(port,
939 base::StringPrintf("/flaky/%d/%d/%d/%d",
940 kBigLength,
941 kFlakyTruncateLength,
942 kFlakySleepEvery,
943 kFlakySleepSecs))));
944 // Terminating the transfer after 3 seconds gives it a chance to contact the
945 // server and enter the retry loop.
946 this->loop_.PostDelayedTask(FROM_HERE,
947 base::Bind(&HttpFetcher::TerminateTransfer,
948 base::Unretained(fetcher.get())),
949 base::TimeDelta::FromSeconds(3));
950
951 // Exiting and testing happens in the delegate.
952 this->loop_.Run();
953 EXPECT_EQ(0, delegate.times_transfer_complete_called_);
954 EXPECT_EQ(1, delegate.times_transfer_terminated_called_);
955
956 // Check that no other callback runs in the next two seconds. That would
957 // indicate a leaked callback.
958 bool timeout = false;
959 auto callback = base::Bind([](bool* timeout) { *timeout = true; },
960 base::Unretained(&timeout));
961 this->loop_.PostDelayedTask(
962 FROM_HERE, callback, base::TimeDelta::FromSeconds(2));
963 EXPECT_TRUE(this->loop_.RunOnce(true));
964 EXPECT_TRUE(timeout);
Andrew de los Reyes9bbd1872010-07-16 14:52:29 -0700965}
966
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700967namespace {
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800968const HttpResponseCode kRedirectCodes[] = {
969 kHttpResponseMovedPermanently, kHttpResponseFound, kHttpResponseSeeOther,
970 kHttpResponseTempRedirect
971};
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700972
973class RedirectHttpFetcherTestDelegate : public HttpFetcherDelegate {
974 public:
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700975 explicit RedirectHttpFetcherTestDelegate(bool expected_successful)
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700976 : expected_successful_(expected_successful) {}
Alex Deymo610277e2014-11-11 21:18:11 -0800977 void ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -0800978 const void* bytes, size_t length) override {
979 data.append(reinterpret_cast<const char*>(bytes), length);
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700980 }
Alex Deymo610277e2014-11-11 21:18:11 -0800981 void TransferComplete(HttpFetcher* fetcher, bool successful) override {
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700982 EXPECT_EQ(expected_successful_, successful);
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700983 if (expected_successful_) {
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800984 EXPECT_EQ(kHttpResponseOk, fetcher->http_response_code());
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700985 } else {
Gilad Arnold9bedeb52011-11-17 16:19:57 -0800986 EXPECT_GE(fetcher->http_response_code(), kHttpResponseMovedPermanently);
987 EXPECT_LE(fetcher->http_response_code(), kHttpResponseTempRedirect);
Darin Petkovcb466212010-08-26 09:40:11 -0700988 }
Alex Deymo60ca1a72015-06-18 18:19:15 -0700989 MessageLoop::current()->BreakLoop();
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700990 }
Alex Deymo610277e2014-11-11 21:18:11 -0800991 void TransferTerminated(HttpFetcher* fetcher) override {
Darin Petkov9ce452b2010-11-17 14:33:28 -0800992 ADD_FAILURE();
993 }
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700994 bool expected_successful_;
995 string data;
Darin Petkov41c2fcf2010-08-25 13:14:48 -0700996};
997
998// RedirectTest takes ownership of |http_fetcher|.
Gilad Arnoldb6c562a2013-07-01 02:19:26 -0700999void RedirectTest(const HttpServer* server,
1000 bool expected_successful,
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001001 const string& url,
1002 HttpFetcher* http_fetcher) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001003 RedirectHttpFetcherTestDelegate delegate(expected_successful);
1004 unique_ptr<HttpFetcher> fetcher(http_fetcher);
1005 fetcher->set_delegate(&delegate);
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001006
Alex Deymo60ca1a72015-06-18 18:19:15 -07001007 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
1008 StartTransfer,
1009 fetcher.get(),
1010 LocalServerUrlForPath(server->GetPort(), url)));
1011 MessageLoop::current()->Run();
1012 if (expected_successful) {
1013 // verify the data we get back
Alex Deymo80f70ff2016-02-10 16:08:11 -08001014 ASSERT_EQ(static_cast<size_t>(kMediumLength), delegate.data.size());
Alex Deymo60ca1a72015-06-18 18:19:15 -07001015 for (int i = 0; i < kMediumLength; i += 10) {
1016 // Assert so that we don't flood the screen w/ EXPECT errors on failure.
1017 ASSERT_EQ(delegate.data.substr(i, 10), "abcdefghij");
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001018 }
1019 }
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001020}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001021} // namespace
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001022
1023TYPED_TEST(HttpFetcherTest, SimpleRedirectTest) {
Alex Deymo2c131bb2016-05-26 16:43:13 -07001024 if (this->test_.IsMock() || !this->test_.IsHttpSupported())
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001025 return;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001026
Ben Chan02f7c1d2014-10-18 15:18:02 -07001027 unique_ptr<HttpServer> server(this->test_.CreateServer());
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001028 ASSERT_TRUE(server->started_);
1029
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001030 for (size_t c = 0; c < arraysize(kRedirectCodes); ++c) {
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001031 const string url = base::StringPrintf("/redirect/%d/download/%d",
1032 kRedirectCodes[c],
1033 kMediumLength);
Gilad Arnoldb6c562a2013-07-01 02:19:26 -07001034 RedirectTest(server.get(), true, url, this->test_.NewLargeFetcher());
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001035 }
1036}
1037
1038TYPED_TEST(HttpFetcherTest, MaxRedirectTest) {
Alex Deymo2c131bb2016-05-26 16:43:13 -07001039 if (this->test_.IsMock() || !this->test_.IsHttpSupported())
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001040 return;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001041
Ben Chan02f7c1d2014-10-18 15:18:02 -07001042 unique_ptr<HttpServer> server(this->test_.CreateServer());
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001043 ASSERT_TRUE(server->started_);
1044
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001045 string url;
David Zeuthen34135a92013-08-06 11:16:16 -07001046 for (int r = 0; r < kDownloadMaxRedirects; r++) {
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001047 url += base::StringPrintf("/redirect/%d",
1048 kRedirectCodes[r % arraysize(kRedirectCodes)]);
1049 }
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001050 url += base::StringPrintf("/download/%d", kMediumLength);
Gilad Arnoldb6c562a2013-07-01 02:19:26 -07001051 RedirectTest(server.get(), true, url, this->test_.NewLargeFetcher());
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001052}
1053
1054TYPED_TEST(HttpFetcherTest, BeyondMaxRedirectTest) {
Alex Deymo2c131bb2016-05-26 16:43:13 -07001055 if (this->test_.IsMock() || !this->test_.IsHttpSupported())
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001056 return;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001057
Ben Chan02f7c1d2014-10-18 15:18:02 -07001058 unique_ptr<HttpServer> server(this->test_.CreateServer());
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001059 ASSERT_TRUE(server->started_);
1060
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001061 string url;
David Zeuthen34135a92013-08-06 11:16:16 -07001062 for (int r = 0; r < kDownloadMaxRedirects + 1; r++) {
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001063 url += base::StringPrintf("/redirect/%d",
1064 kRedirectCodes[r % arraysize(kRedirectCodes)]);
1065 }
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001066 url += base::StringPrintf("/download/%d", kMediumLength);
Gilad Arnoldb6c562a2013-07-01 02:19:26 -07001067 RedirectTest(server.get(), false, url, this->test_.NewLargeFetcher());
Darin Petkov41c2fcf2010-08-25 13:14:48 -07001068}
1069
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001070namespace {
1071class MultiHttpFetcherTestDelegate : public HttpFetcherDelegate {
1072 public:
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001073 explicit MultiHttpFetcherTestDelegate(int expected_response_code)
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001074 : expected_response_code_(expected_response_code) {}
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001075
Alex Deymo610277e2014-11-11 21:18:11 -08001076 void ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001077 const void* bytes, size_t length) override {
Darin Petkov9ce452b2010-11-17 14:33:28 -08001078 EXPECT_EQ(fetcher, fetcher_.get());
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001079 data.append(reinterpret_cast<const char*>(bytes), length);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001080 }
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001081
Alex Deymo610277e2014-11-11 21:18:11 -08001082 void TransferComplete(HttpFetcher* fetcher, bool successful) override {
Darin Petkov9ce452b2010-11-17 14:33:28 -08001083 EXPECT_EQ(fetcher, fetcher_.get());
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001084 EXPECT_EQ(expected_response_code_ != kHttpResponseUndefined, successful);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001085 if (expected_response_code_ != 0)
1086 EXPECT_EQ(expected_response_code_, fetcher->http_response_code());
Darin Petkov9ce452b2010-11-17 14:33:28 -08001087 // Destroy the fetcher (because we're allowed to).
Alex Vakulenko88b591f2014-08-28 16:48:57 -07001088 fetcher_.reset(nullptr);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001089 MessageLoop::current()->BreakLoop();
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001090 }
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001091
Alex Deymo610277e2014-11-11 21:18:11 -08001092 void TransferTerminated(HttpFetcher* fetcher) override {
Darin Petkov9ce452b2010-11-17 14:33:28 -08001093 ADD_FAILURE();
1094 }
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001095
Ben Chan02f7c1d2014-10-18 15:18:02 -07001096 unique_ptr<HttpFetcher> fetcher_;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001097 int expected_response_code_;
1098 string data;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001099};
1100
1101void MultiTest(HttpFetcher* fetcher_in,
Alex Deymoc1c17b42015-11-23 03:53:15 -03001102 FakeHardware* fake_hardware,
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001103 const string& url,
Ben Chanf9cb98c2014-09-21 18:31:30 -07001104 const vector<pair<off_t, off_t>>& ranges,
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001105 const string& expected_prefix,
Alex Deymo5fe0c4e2016-02-16 18:46:24 -08001106 size_t expected_size,
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001107 HttpResponseCode expected_response_code) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001108 MultiHttpFetcherTestDelegate delegate(expected_response_code);
1109 delegate.fetcher_.reset(fetcher_in);
Jay Srinivasan43488792012-06-19 00:25:31 -07001110
Alex Deymo60ca1a72015-06-18 18:19:15 -07001111 MultiRangeHttpFetcher* multi_fetcher =
Alex Deymo80f70ff2016-02-10 16:08:11 -08001112 static_cast<MultiRangeHttpFetcher*>(fetcher_in);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001113 ASSERT_TRUE(multi_fetcher);
1114 multi_fetcher->ClearRanges();
1115 for (vector<pair<off_t, off_t>>::const_iterator it = ranges.begin(),
1116 e = ranges.end(); it != e; ++it) {
1117 string tmp_str = base::StringPrintf("%jd+", it->first);
1118 if (it->second > 0) {
1119 base::StringAppendF(&tmp_str, "%jd", it->second);
1120 multi_fetcher->AddRange(it->first, it->second);
1121 } else {
1122 base::StringAppendF(&tmp_str, "?");
1123 multi_fetcher->AddRange(it->first);
Andrew de los Reyes819fef22010-12-17 11:33:58 -08001124 }
Alex Deymo60ca1a72015-06-18 18:19:15 -07001125 LOG(INFO) << "added range: " << tmp_str;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001126 }
Alex Deymoc1c17b42015-11-23 03:53:15 -03001127 fake_hardware->SetIsOfficialBuild(false);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001128 multi_fetcher->set_delegate(&delegate);
1129
1130 MessageLoop::current()->PostTask(
1131 FROM_HERE,
1132 base::Bind(StartTransfer, multi_fetcher, url));
1133 MessageLoop::current()->Run();
1134
1135 EXPECT_EQ(expected_size, delegate.data.size());
1136 EXPECT_EQ(expected_prefix,
1137 string(delegate.data.data(), expected_prefix.size()));
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001138}
Alex Vakulenkod2779df2014-06-16 13:19:00 -07001139} // namespace
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001140
Darin Petkov9ce452b2010-11-17 14:33:28 -08001141TYPED_TEST(HttpFetcherTest, MultiHttpFetcherSimpleTest) {
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001142 if (!this->test_.IsMulti())
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001143 return;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001144
Ben Chan02f7c1d2014-10-18 15:18:02 -07001145 unique_ptr<HttpServer> server(this->test_.CreateServer());
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001146 ASSERT_TRUE(server->started_);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001147
Ben Chanf9cb98c2014-09-21 18:31:30 -07001148 vector<pair<off_t, off_t>> ranges;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001149 ranges.push_back(make_pair(0, 25));
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001150 ranges.push_back(make_pair(99, 0));
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001151 MultiTest(this->test_.NewLargeFetcher(),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001152 this->test_.fake_hardware(),
Gilad Arnoldb6c562a2013-07-01 02:19:26 -07001153 this->test_.BigUrl(server->GetPort()),
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001154 ranges,
1155 "abcdefghijabcdefghijabcdejabcdefghijabcdef",
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001156 kBigLength - (99 - 25),
1157 kHttpResponsePartialContent);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001158}
1159
1160TYPED_TEST(HttpFetcherTest, MultiHttpFetcherLengthLimitTest) {
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001161 if (!this->test_.IsMulti())
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001162 return;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001163
Ben Chan02f7c1d2014-10-18 15:18:02 -07001164 unique_ptr<HttpServer> server(this->test_.CreateServer());
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001165 ASSERT_TRUE(server->started_);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001166
Ben Chanf9cb98c2014-09-21 18:31:30 -07001167 vector<pair<off_t, off_t>> ranges;
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001168 ranges.push_back(make_pair(0, 24));
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001169 MultiTest(this->test_.NewLargeFetcher(),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001170 this->test_.fake_hardware(),
Gilad Arnoldb6c562a2013-07-01 02:19:26 -07001171 this->test_.BigUrl(server->GetPort()),
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001172 ranges,
1173 "abcdefghijabcdefghijabcd",
1174 24,
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001175 kHttpResponsePartialContent);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001176}
1177
1178TYPED_TEST(HttpFetcherTest, MultiHttpFetcherMultiEndTest) {
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001179 if (!this->test_.IsMulti())
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001180 return;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001181
Ben Chan02f7c1d2014-10-18 15:18:02 -07001182 unique_ptr<HttpServer> server(this->test_.CreateServer());
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001183 ASSERT_TRUE(server->started_);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001184
Ben Chanf9cb98c2014-09-21 18:31:30 -07001185 vector<pair<off_t, off_t>> ranges;
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001186 ranges.push_back(make_pair(kBigLength - 2, 0));
1187 ranges.push_back(make_pair(kBigLength - 3, 0));
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001188 MultiTest(this->test_.NewLargeFetcher(),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001189 this->test_.fake_hardware(),
Gilad Arnoldb6c562a2013-07-01 02:19:26 -07001190 this->test_.BigUrl(server->GetPort()),
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001191 ranges,
1192 "ijhij",
1193 5,
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001194 kHttpResponsePartialContent);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001195}
1196
1197TYPED_TEST(HttpFetcherTest, MultiHttpFetcherInsufficientTest) {
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001198 if (!this->test_.IsMulti())
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001199 return;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001200
Ben Chan02f7c1d2014-10-18 15:18:02 -07001201 unique_ptr<HttpServer> server(this->test_.CreateServer());
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001202 ASSERT_TRUE(server->started_);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001203
Ben Chanf9cb98c2014-09-21 18:31:30 -07001204 vector<pair<off_t, off_t>> ranges;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001205 ranges.push_back(make_pair(kBigLength - 2, 4));
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001206 for (int i = 0; i < 2; ++i) {
Andrew de los Reyes819fef22010-12-17 11:33:58 -08001207 LOG(INFO) << "i = " << i;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001208 MultiTest(this->test_.NewLargeFetcher(),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001209 this->test_.fake_hardware(),
Gilad Arnoldb6c562a2013-07-01 02:19:26 -07001210 this->test_.BigUrl(server->GetPort()),
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001211 ranges,
1212 "ij",
1213 2,
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001214 kHttpResponseUndefined);
Andrew de los Reyes3fd5d302010-10-07 20:07:18 -07001215 ranges.push_back(make_pair(0, 5));
1216 }
1217}
1218
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001219// Issue #18143: when a fetch of a secondary chunk out of a chain, then it
1220// should retry with other proxies listed before giving up.
1221//
1222// (1) successful recovery: The offset fetch will fail twice but succeed with
1223// the third proxy.
1224TYPED_TEST(HttpFetcherTest, MultiHttpFetcherErrorIfOffsetRecoverableTest) {
1225 if (!this->test_.IsMulti())
1226 return;
1227
Ben Chan02f7c1d2014-10-18 15:18:02 -07001228 unique_ptr<HttpServer> server(this->test_.CreateServer());
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001229 ASSERT_TRUE(server->started_);
1230
Ben Chanf9cb98c2014-09-21 18:31:30 -07001231 vector<pair<off_t, off_t>> ranges;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001232 ranges.push_back(make_pair(0, 25));
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001233 ranges.push_back(make_pair(99, 0));
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001234 MultiTest(this->test_.NewLargeFetcher(3),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001235 this->test_.fake_hardware(),
Gilad Arnoldb6c562a2013-07-01 02:19:26 -07001236 LocalServerUrlForPath(server->GetPort(),
1237 base::StringPrintf("/error-if-offset/%d/2",
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001238 kBigLength)),
1239 ranges,
1240 "abcdefghijabcdefghijabcdejabcdefghijabcdef",
1241 kBigLength - (99 - 25),
1242 kHttpResponsePartialContent);
1243}
1244
1245// (2) unsuccessful recovery: The offset fetch will fail repeatedly. The
1246// fetcher will signal a (failed) completed transfer to the delegate.
1247TYPED_TEST(HttpFetcherTest, MultiHttpFetcherErrorIfOffsetUnrecoverableTest) {
1248 if (!this->test_.IsMulti())
1249 return;
1250
Ben Chan02f7c1d2014-10-18 15:18:02 -07001251 unique_ptr<HttpServer> server(this->test_.CreateServer());
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001252 ASSERT_TRUE(server->started_);
1253
Ben Chanf9cb98c2014-09-21 18:31:30 -07001254 vector<pair<off_t, off_t>> ranges;
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001255 ranges.push_back(make_pair(0, 25));
Gilad Arnolde4ad2502011-12-29 17:08:54 -08001256 ranges.push_back(make_pair(99, 0));
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001257 MultiTest(this->test_.NewLargeFetcher(2),
Alex Deymoc1c17b42015-11-23 03:53:15 -03001258 this->test_.fake_hardware(),
Gilad Arnoldb6c562a2013-07-01 02:19:26 -07001259 LocalServerUrlForPath(server->GetPort(),
1260 base::StringPrintf("/error-if-offset/%d/3",
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001261 kBigLength)),
1262 ranges,
1263 "abcdefghijabcdefghijabcde", // only received the first chunk
1264 25,
1265 kHttpResponseUndefined);
1266}
1267
Alex Deymo4ea2d922017-02-05 07:35:32 +00001268namespace {
1269// This HttpFetcherDelegate calls TerminateTransfer at a configurable point.
1270class MultiHttpFetcherTerminateTestDelegate : public HttpFetcherDelegate {
1271 public:
1272 explicit MultiHttpFetcherTerminateTestDelegate(size_t terminate_trigger_bytes)
1273 : terminate_trigger_bytes_(terminate_trigger_bytes) {}
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001274
Alex Deymo4ea2d922017-02-05 07:35:32 +00001275 void ReceivedBytes(HttpFetcher* fetcher,
1276 const void* bytes,
1277 size_t length) override {
1278 LOG(INFO) << "ReceivedBytes, " << length << " bytes.";
1279 EXPECT_EQ(fetcher, fetcher_.get());
1280 if (bytes_downloaded_ < terminate_trigger_bytes_ &&
1281 bytes_downloaded_ + length >= terminate_trigger_bytes_) {
1282 MessageLoop::current()->PostTask(
1283 FROM_HERE,
1284 base::Bind(&HttpFetcher::TerminateTransfer,
1285 base::Unretained(fetcher_.get())));
1286 }
1287 bytes_downloaded_ += length;
1288 }
1289
1290 void TransferComplete(HttpFetcher* fetcher, bool successful) override {
1291 ADD_FAILURE() << "TransferComplete called but expected a failure";
1292 // Destroy the fetcher (because we're allowed to).
1293 fetcher_.reset(nullptr);
1294 MessageLoop::current()->BreakLoop();
1295 }
1296
1297 void TransferTerminated(HttpFetcher* fetcher) override {
1298 // Destroy the fetcher (because we're allowed to).
1299 fetcher_.reset(nullptr);
1300 MessageLoop::current()->BreakLoop();
1301 }
1302
1303 unique_ptr<HttpFetcher> fetcher_;
1304 size_t bytes_downloaded_{0};
1305 size_t terminate_trigger_bytes_;
1306};
1307} // namespace
1308
1309TYPED_TEST(HttpFetcherTest, MultiHttpFetcherTerminateBetweenRangesTest) {
1310 if (!this->test_.IsMulti())
1311 return;
1312 const size_t kRangeTrigger = 1000;
1313 MultiHttpFetcherTerminateTestDelegate delegate(kRangeTrigger);
1314
1315 unique_ptr<HttpServer> server(this->test_.CreateServer());
1316 ASSERT_TRUE(server->started_);
1317
1318 MultiRangeHttpFetcher* multi_fetcher =
1319 static_cast<MultiRangeHttpFetcher*>(this->test_.NewLargeFetcher());
1320 ASSERT_TRUE(multi_fetcher);
1321 // Transfer ownership of the fetcher to the delegate.
1322 delegate.fetcher_.reset(multi_fetcher);
1323 multi_fetcher->set_delegate(&delegate);
1324
1325 multi_fetcher->ClearRanges();
1326 multi_fetcher->AddRange(45, kRangeTrigger);
1327 multi_fetcher->AddRange(2000, 100);
1328
1329 this->test_.fake_hardware()->SetIsOfficialBuild(false);
1330
1331 StartTransfer(multi_fetcher, this->test_.BigUrl(server->GetPort()));
1332 MessageLoop::current()->Run();
1333
1334 // Check that the delegate made it to the trigger point.
1335 EXPECT_EQ(kRangeTrigger, delegate.bytes_downloaded_);
1336}
Gilad Arnold9bedeb52011-11-17 16:19:57 -08001337
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07001338namespace {
Darin Petkovfc7a0ce2010-10-25 10:38:37 -07001339class BlockedTransferTestDelegate : public HttpFetcherDelegate {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07001340 public:
Alex Deymo610277e2014-11-11 21:18:11 -08001341 void ReceivedBytes(HttpFetcher* fetcher,
Alex Vakulenkof68bbbc2015-02-09 12:53:18 -08001342 const void* bytes, size_t length) override {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07001343 ADD_FAILURE();
1344 }
Alex Deymo610277e2014-11-11 21:18:11 -08001345 void TransferComplete(HttpFetcher* fetcher, bool successful) override {
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07001346 EXPECT_FALSE(successful);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001347 MessageLoop::current()->BreakLoop();
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07001348 }
Alex Deymo610277e2014-11-11 21:18:11 -08001349 void TransferTerminated(HttpFetcher* fetcher) override {
Darin Petkov9ce452b2010-11-17 14:33:28 -08001350 ADD_FAILURE();
1351 }
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07001352};
1353
Alex Deymo60ca1a72015-06-18 18:19:15 -07001354void BlockedTransferTestHelper(AnyHttpFetcherTest* fetcher_test,
1355 bool is_official_build) {
1356 if (fetcher_test->IsMock() || fetcher_test->IsMulti())
1357 return;
1358
1359 unique_ptr<HttpServer> server(fetcher_test->CreateServer());
1360 ASSERT_TRUE(server->started_);
1361
1362 BlockedTransferTestDelegate delegate;
1363 unique_ptr<HttpFetcher> fetcher(fetcher_test->NewLargeFetcher());
1364 LOG(INFO) << "is_official_build: " << is_official_build;
1365 // NewLargeFetcher creates the HttpFetcher* with a FakeSystemState.
Alex Deymoc1c17b42015-11-23 03:53:15 -03001366 fetcher_test->fake_hardware()->SetIsOfficialBuild(is_official_build);
Alex Deymo60ca1a72015-06-18 18:19:15 -07001367 fetcher->set_delegate(&delegate);
1368
1369 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
1370 StartTransfer,
1371 fetcher.get(),
1372 LocalServerUrlForPath(server->GetPort(),
1373 fetcher_test->SmallUrl(server->GetPort()))));
1374 MessageLoop::current()->Run();
1375}
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07001376} // namespace
1377
Darin Petkovfc7a0ce2010-10-25 10:38:37 -07001378TYPED_TEST(HttpFetcherTest, BlockedTransferTest) {
Alex Deymo60ca1a72015-06-18 18:19:15 -07001379 BlockedTransferTestHelper(&this->test_, false);
1380}
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07001381
Alex Deymo60ca1a72015-06-18 18:19:15 -07001382TYPED_TEST(HttpFetcherTest, BlockedTransferOfficialBuildTest) {
1383 BlockedTransferTestHelper(&this->test_, true);
Andrew de los Reyesd57d1472010-10-21 13:34:08 -07001384}
1385
rspangler@google.com49fdf182009-10-10 00:57:34 +00001386} // namespace chromeos_update_engine