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