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