| OLD | NEW |
| (Empty) |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/bind.h" | |
| 6 #include "base/bind_helpers.h" | |
| 7 #include "base/files/file_path.h" | |
| 8 #include "base/files/file_util.h" | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/path_service.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "components/update_client/crx_downloader.h" | |
| 15 #include "net/base/net_errors.h" | |
| 16 #include "net/url_request/test_url_request_interceptor.h" | |
| 17 #include "net/url_request/url_request_test_util.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 using base::ContentsEqual; | |
| 21 | |
| 22 namespace update_client { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 // Intercepts HTTP GET requests sent to "localhost". | |
| 27 typedef net::LocalHostTestURLRequestInterceptor GetInterceptor; | |
| 28 | |
| 29 const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx"; | |
| 30 | |
| 31 base::FilePath MakeTestFilePath(const char* file) { | |
| 32 base::FilePath path; | |
| 33 PathService::Get(base::DIR_SOURCE_ROOT, &path); | |
| 34 return path.AppendASCII("components") | |
| 35 .AppendASCII("test") | |
| 36 .AppendASCII("data") | |
| 37 .AppendASCII("update_client") | |
| 38 .AppendASCII(file); | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 43 class CrxDownloaderTest : public testing::Test { | |
| 44 public: | |
| 45 CrxDownloaderTest(); | |
| 46 ~CrxDownloaderTest() override; | |
| 47 | |
| 48 // Overrides from testing::Test. | |
| 49 void SetUp() override; | |
| 50 void TearDown() override; | |
| 51 | |
| 52 void Quit(); | |
| 53 void RunThreads(); | |
| 54 void RunThreadsUntilIdle(); | |
| 55 | |
| 56 void DownloadComplete(int crx_context, const CrxDownloader::Result& result); | |
| 57 | |
| 58 void DownloadProgress(int crx_context, const CrxDownloader::Result& result); | |
| 59 | |
| 60 protected: | |
| 61 scoped_ptr<CrxDownloader> crx_downloader_; | |
| 62 | |
| 63 scoped_ptr<GetInterceptor> get_interceptor_; | |
| 64 | |
| 65 CrxDownloader::DownloadCallback callback_; | |
| 66 CrxDownloader::ProgressCallback progress_callback_; | |
| 67 | |
| 68 int crx_context_; | |
| 69 | |
| 70 int num_download_complete_calls_; | |
| 71 CrxDownloader::Result download_complete_result_; | |
| 72 | |
| 73 // These members are updated by DownloadProgress. | |
| 74 int num_progress_calls_; | |
| 75 CrxDownloader::Result download_progress_result_; | |
| 76 | |
| 77 // A magic value for the context to be used in the tests. | |
| 78 static const int kExpectedContext = 0xaabb; | |
| 79 | |
| 80 private: | |
| 81 base::MessageLoopForIO loop_; | |
| 82 scoped_refptr<net::TestURLRequestContextGetter> context_; | |
| 83 base::Closure quit_closure_; | |
| 84 }; | |
| 85 | |
| 86 const int CrxDownloaderTest::kExpectedContext; | |
| 87 | |
| 88 CrxDownloaderTest::CrxDownloaderTest() | |
| 89 : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete, | |
| 90 base::Unretained(this), | |
| 91 kExpectedContext)), | |
| 92 progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress, | |
| 93 base::Unretained(this), | |
| 94 kExpectedContext)), | |
| 95 crx_context_(0), | |
| 96 num_download_complete_calls_(0), | |
| 97 num_progress_calls_(0), | |
| 98 context_(new net::TestURLRequestContextGetter( | |
| 99 base::MessageLoopProxy::current())) { | |
| 100 } | |
| 101 | |
| 102 CrxDownloaderTest::~CrxDownloaderTest() { | |
| 103 context_ = NULL; | |
| 104 | |
| 105 // The GetInterceptor requires the message loop to run to destruct correctly. | |
| 106 get_interceptor_.reset(); | |
| 107 RunThreadsUntilIdle(); | |
| 108 } | |
| 109 | |
| 110 void CrxDownloaderTest::SetUp() { | |
| 111 num_download_complete_calls_ = 0; | |
| 112 download_complete_result_ = CrxDownloader::Result(); | |
| 113 num_progress_calls_ = 0; | |
| 114 download_progress_result_ = CrxDownloader::Result(); | |
| 115 | |
| 116 // Do not use the background downloader in these tests. | |
| 117 crx_downloader_.reset(CrxDownloader::Create(false, context_.get(), | |
| 118 base::MessageLoopProxy::current(), | |
| 119 NULL).release()); | |
| 120 crx_downloader_->set_progress_callback(progress_callback_); | |
| 121 | |
| 122 get_interceptor_.reset(new GetInterceptor(base::MessageLoopProxy::current(), | |
| 123 base::MessageLoopProxy::current())); | |
| 124 } | |
| 125 | |
| 126 void CrxDownloaderTest::TearDown() { | |
| 127 crx_downloader_.reset(); | |
| 128 } | |
| 129 | |
| 130 void CrxDownloaderTest::Quit() { | |
| 131 if (!quit_closure_.is_null()) | |
| 132 quit_closure_.Run(); | |
| 133 } | |
| 134 | |
| 135 void CrxDownloaderTest::DownloadComplete(int crx_context, | |
| 136 const CrxDownloader::Result& result) { | |
| 137 ++num_download_complete_calls_; | |
| 138 crx_context_ = crx_context; | |
| 139 download_complete_result_ = result; | |
| 140 Quit(); | |
| 141 } | |
| 142 | |
| 143 void CrxDownloaderTest::DownloadProgress(int crx_context, | |
| 144 const CrxDownloader::Result& result) { | |
| 145 ++num_progress_calls_; | |
| 146 download_progress_result_ = result; | |
| 147 } | |
| 148 | |
| 149 void CrxDownloaderTest::RunThreads() { | |
| 150 base::RunLoop runloop; | |
| 151 quit_closure_ = runloop.QuitClosure(); | |
| 152 runloop.Run(); | |
| 153 | |
| 154 // Since some tests need to drain currently enqueued tasks such as network | |
| 155 // intercepts on the IO thread, run the threads until they are | |
| 156 // idle. The component updater service won't loop again until the loop count | |
| 157 // is set and the service is started. | |
| 158 RunThreadsUntilIdle(); | |
| 159 } | |
| 160 | |
| 161 void CrxDownloaderTest::RunThreadsUntilIdle() { | |
| 162 base::RunLoop().RunUntilIdle(); | |
| 163 } | |
| 164 | |
| 165 // Tests that starting a download without a url results in an error. | |
| 166 TEST_F(CrxDownloaderTest, NoUrl) { | |
| 167 std::vector<GURL> urls; | |
| 168 crx_downloader_->StartDownload(urls, callback_); | |
| 169 RunThreadsUntilIdle(); | |
| 170 | |
| 171 EXPECT_EQ(1, num_download_complete_calls_); | |
| 172 EXPECT_EQ(kExpectedContext, crx_context_); | |
| 173 EXPECT_EQ(-1, download_complete_result_.error); | |
| 174 EXPECT_TRUE(download_complete_result_.response.empty()); | |
| 175 EXPECT_EQ(-1, download_complete_result_.downloaded_bytes); | |
| 176 EXPECT_EQ(-1, download_complete_result_.total_bytes); | |
| 177 EXPECT_EQ(0, num_progress_calls_); | |
| 178 } | |
| 179 | |
| 180 // Tests that downloading from one url is successful. | |
| 181 TEST_F(CrxDownloaderTest, OneUrl) { | |
| 182 const GURL expected_crx_url = | |
| 183 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
| 184 | |
| 185 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
| 186 get_interceptor_->SetResponse(expected_crx_url, test_file); | |
| 187 | |
| 188 crx_downloader_->StartDownloadFromUrl(expected_crx_url, callback_); | |
| 189 RunThreads(); | |
| 190 | |
| 191 EXPECT_EQ(1, get_interceptor_->GetHitCount()); | |
| 192 | |
| 193 EXPECT_EQ(1, num_download_complete_calls_); | |
| 194 EXPECT_EQ(kExpectedContext, crx_context_); | |
| 195 EXPECT_EQ(0, download_complete_result_.error); | |
| 196 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | |
| 197 EXPECT_EQ(1843, download_complete_result_.total_bytes); | |
| 198 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | |
| 199 | |
| 200 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | |
| 201 | |
| 202 EXPECT_LE(1, num_progress_calls_); | |
| 203 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); | |
| 204 EXPECT_EQ(1843, download_progress_result_.total_bytes); | |
| 205 } | |
| 206 | |
| 207 // Tests that specifying from two urls has no side effects. Expect a successful | |
| 208 // download, and only one download request be made. | |
| 209 // This test is flaky on Android. crbug.com/329883 | |
| 210 #if defined(OS_ANDROID) | |
| 211 #define MAYBE_TwoUrls DISABLED_TwoUrls | |
| 212 #else | |
| 213 #define MAYBE_TwoUrls TwoUrls | |
| 214 #endif | |
| 215 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) { | |
| 216 const GURL expected_crx_url = | |
| 217 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
| 218 | |
| 219 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
| 220 get_interceptor_->SetResponse(expected_crx_url, test_file); | |
| 221 | |
| 222 std::vector<GURL> urls; | |
| 223 urls.push_back(expected_crx_url); | |
| 224 urls.push_back(expected_crx_url); | |
| 225 | |
| 226 crx_downloader_->StartDownload(urls, callback_); | |
| 227 RunThreads(); | |
| 228 | |
| 229 EXPECT_EQ(1, get_interceptor_->GetHitCount()); | |
| 230 | |
| 231 EXPECT_EQ(1, num_download_complete_calls_); | |
| 232 EXPECT_EQ(kExpectedContext, crx_context_); | |
| 233 EXPECT_EQ(0, download_complete_result_.error); | |
| 234 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | |
| 235 EXPECT_EQ(1843, download_complete_result_.total_bytes); | |
| 236 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | |
| 237 | |
| 238 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | |
| 239 | |
| 240 EXPECT_LE(1, num_progress_calls_); | |
| 241 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); | |
| 242 EXPECT_EQ(1843, download_progress_result_.total_bytes); | |
| 243 } | |
| 244 | |
| 245 // Tests that an invalid host results in a download error. | |
| 246 TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) { | |
| 247 const GURL expected_crx_url = | |
| 248 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
| 249 | |
| 250 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
| 251 get_interceptor_->SetResponse(expected_crx_url, test_file); | |
| 252 | |
| 253 crx_downloader_->StartDownloadFromUrl( | |
| 254 GURL( | |
| 255 "http://no.such.host" | |
| 256 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"), | |
| 257 callback_); | |
| 258 RunThreads(); | |
| 259 | |
| 260 EXPECT_EQ(0, get_interceptor_->GetHitCount()); | |
| 261 | |
| 262 EXPECT_EQ(1, num_download_complete_calls_); | |
| 263 EXPECT_EQ(kExpectedContext, crx_context_); | |
| 264 EXPECT_NE(0, download_complete_result_.error); | |
| 265 EXPECT_TRUE(download_complete_result_.response.empty()); | |
| 266 } | |
| 267 | |
| 268 // Tests that an invalid path results in a download error. | |
| 269 TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) { | |
| 270 const GURL expected_crx_url = | |
| 271 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
| 272 | |
| 273 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
| 274 get_interceptor_->SetResponse(expected_crx_url, test_file); | |
| 275 | |
| 276 crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"), | |
| 277 callback_); | |
| 278 RunThreads(); | |
| 279 | |
| 280 EXPECT_EQ(0, get_interceptor_->GetHitCount()); | |
| 281 | |
| 282 EXPECT_EQ(1, num_download_complete_calls_); | |
| 283 EXPECT_EQ(kExpectedContext, crx_context_); | |
| 284 EXPECT_NE(0, download_complete_result_.error); | |
| 285 EXPECT_TRUE(download_complete_result_.response.empty()); | |
| 286 } | |
| 287 | |
| 288 // Tests that the fallback to a valid url is successful. | |
| 289 // This test is flaky on Android. crbug.com/329883 | |
| 290 #if defined(OS_ANDROID) | |
| 291 #define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid | |
| 292 #else | |
| 293 #define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid | |
| 294 #endif | |
| 295 TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) { | |
| 296 const GURL expected_crx_url = | |
| 297 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
| 298 | |
| 299 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
| 300 get_interceptor_->SetResponse(expected_crx_url, test_file); | |
| 301 | |
| 302 std::vector<GURL> urls; | |
| 303 urls.push_back(GURL("http://localhost/no/such/file")); | |
| 304 urls.push_back(expected_crx_url); | |
| 305 | |
| 306 crx_downloader_->StartDownload(urls, callback_); | |
| 307 RunThreads(); | |
| 308 | |
| 309 EXPECT_EQ(1, get_interceptor_->GetHitCount()); | |
| 310 | |
| 311 EXPECT_EQ(1, num_download_complete_calls_); | |
| 312 EXPECT_EQ(kExpectedContext, crx_context_); | |
| 313 EXPECT_EQ(0, download_complete_result_.error); | |
| 314 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | |
| 315 EXPECT_EQ(1843, download_complete_result_.total_bytes); | |
| 316 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | |
| 317 | |
| 318 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | |
| 319 | |
| 320 EXPECT_LE(1, num_progress_calls_); | |
| 321 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); | |
| 322 EXPECT_EQ(1843, download_progress_result_.total_bytes); | |
| 323 } | |
| 324 | |
| 325 // Tests that the download succeeds if the first url is correct and the | |
| 326 // second bad url does not have a side-effect. | |
| 327 TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) { | |
| 328 const GURL expected_crx_url = | |
| 329 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
| 330 | |
| 331 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
| 332 get_interceptor_->SetResponse(expected_crx_url, test_file); | |
| 333 | |
| 334 std::vector<GURL> urls; | |
| 335 urls.push_back(expected_crx_url); | |
| 336 urls.push_back(GURL("http://localhost/no/such/file")); | |
| 337 | |
| 338 crx_downloader_->StartDownload(urls, callback_); | |
| 339 RunThreads(); | |
| 340 | |
| 341 EXPECT_EQ(1, get_interceptor_->GetHitCount()); | |
| 342 | |
| 343 EXPECT_EQ(1, num_download_complete_calls_); | |
| 344 EXPECT_EQ(kExpectedContext, crx_context_); | |
| 345 EXPECT_EQ(0, download_complete_result_.error); | |
| 346 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | |
| 347 EXPECT_EQ(1843, download_complete_result_.total_bytes); | |
| 348 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | |
| 349 | |
| 350 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | |
| 351 | |
| 352 EXPECT_LE(1, num_progress_calls_); | |
| 353 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); | |
| 354 EXPECT_EQ(1843, download_progress_result_.total_bytes); | |
| 355 } | |
| 356 | |
| 357 // Tests that the download fails if both urls are bad. | |
| 358 TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { | |
| 359 const GURL expected_crx_url = | |
| 360 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
| 361 | |
| 362 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
| 363 get_interceptor_->SetResponse(expected_crx_url, test_file); | |
| 364 | |
| 365 std::vector<GURL> urls; | |
| 366 urls.push_back(GURL("http://localhost/no/such/file")); | |
| 367 urls.push_back(GURL( | |
| 368 "http://no.such.host/" | |
| 369 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); | |
| 370 | |
| 371 crx_downloader_->StartDownload(urls, callback_); | |
| 372 RunThreads(); | |
| 373 | |
| 374 EXPECT_EQ(0, get_interceptor_->GetHitCount()); | |
| 375 | |
| 376 EXPECT_EQ(1, num_download_complete_calls_); | |
| 377 EXPECT_EQ(kExpectedContext, crx_context_); | |
| 378 EXPECT_NE(0, download_complete_result_.error); | |
| 379 EXPECT_TRUE(download_complete_result_.response.empty()); | |
| 380 } | |
| 381 | |
| 382 } // namespace update_client | |
| OLD | NEW |