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