[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 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 | |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 5 | #include "components/update_client/crx_downloader.h" |
| 6 | |
| 7 | #include <memory> |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 8 | #include <utility> |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 9 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 14 | #include "base/path_service.h" |
| 15 | #include "base/run_loop.h" |
Sorin Jianu | 72d8fe0d | 2017-07-11 18:42:16 | [diff] [blame] | 16 | #include "base/test/scoped_task_environment.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 17 | #include "base/threading/thread_task_runner_handle.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 18 | #include "build/build_config.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 19 | #include "components/update_client/update_client_errors.h" |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame] | 20 | #include "components/update_client/utils.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 21 | #include "net/base/net_errors.h" |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 22 | #include "net/url_request/test_url_request_interceptor.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 23 | #include "net/url_request/url_request_test_util.h" |
| 24 | #include "testing/gtest/include/gtest/gtest.h" |
| 25 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 26 | using base::ContentsEqual; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 27 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 28 | namespace update_client { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | |
| 32 | // Intercepts HTTP GET requests sent to "localhost". |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 33 | typedef net::LocalHostTestURLRequestInterceptor GetInterceptor; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 34 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 35 | const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
| 36 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 37 | const char hash_jebg[] = |
| 38 | "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87"; |
| 39 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 40 | base::FilePath MakeTestFilePath(const char* file) { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 41 | base::FilePath path; |
Avi Drissman | f617d01 | 2018-05-02 18:48:53 | [diff] [blame^] | 42 | base::PathService::Get(base::DIR_SOURCE_ROOT, &path); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 43 | return path.AppendASCII("components/test/data/update_client") |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 44 | .AppendASCII(file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | } // namespace |
| 48 | |
| 49 | class CrxDownloaderTest : public testing::Test { |
| 50 | public: |
| 51 | CrxDownloaderTest(); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 52 | ~CrxDownloaderTest() override; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 53 | |
| 54 | // Overrides from testing::Test. |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 55 | void SetUp() override; |
| 56 | void TearDown() override; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 57 | |
| 58 | void Quit(); |
| 59 | void RunThreads(); |
| 60 | void RunThreadsUntilIdle(); |
| 61 | |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 62 | void DownloadComplete(int crx_context, const CrxDownloader::Result& result); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 63 | |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 64 | void DownloadProgress(int crx_context, const CrxDownloader::Result& result); |
| 65 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 66 | protected: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 67 | std::unique_ptr<CrxDownloader> crx_downloader_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 68 | |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 69 | std::unique_ptr<GetInterceptor> get_interceptor_; |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 70 | |
[email protected] | 1b6587dc5 | 2014-04-26 00:38:55 | [diff] [blame] | 71 | CrxDownloader::DownloadCallback callback_; |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 72 | CrxDownloader::ProgressCallback progress_callback_; |
[email protected] | 1b6587dc5 | 2014-04-26 00:38:55 | [diff] [blame] | 73 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 74 | int crx_context_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 75 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 76 | int num_download_complete_calls_; |
| 77 | CrxDownloader::Result download_complete_result_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 78 | |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 79 | // These members are updated by DownloadProgress. |
| 80 | int num_progress_calls_; |
| 81 | CrxDownloader::Result download_progress_result_; |
| 82 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 83 | // A magic value for the context to be used in the tests. |
| 84 | static const int kExpectedContext = 0xaabb; |
| 85 | |
| 86 | private: |
Sorin Jianu | 72d8fe0d | 2017-07-11 18:42:16 | [diff] [blame] | 87 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 88 | scoped_refptr<net::TestURLRequestContextGetter> context_; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 89 | base::OnceClosure quit_closure_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | const int CrxDownloaderTest::kExpectedContext; |
| 93 | |
| 94 | CrxDownloaderTest::CrxDownloaderTest() |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 95 | : callback_(base::BindOnce(&CrxDownloaderTest::DownloadComplete, |
| 96 | base::Unretained(this), |
| 97 | kExpectedContext)), |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 98 | progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress, |
| 99 | base::Unretained(this), |
| 100 | kExpectedContext)), |
[email protected] | 1b6587dc5 | 2014-04-26 00:38:55 | [diff] [blame] | 101 | crx_context_(0), |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 102 | num_download_complete_calls_(0), |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 103 | num_progress_calls_(0), |
Sorin Jianu | 72d8fe0d | 2017-07-11 18:42:16 | [diff] [blame] | 104 | scoped_task_environment_( |
| 105 | base::test::ScopedTaskEnvironment::MainThreadType::IO), |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 106 | context_(base::MakeRefCounted<net::TestURLRequestContextGetter>( |
Sorin Jianu | 72d8fe0d | 2017-07-11 18:42:16 | [diff] [blame] | 107 | base::ThreadTaskRunnerHandle::Get())) {} |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 108 | |
| 109 | CrxDownloaderTest::~CrxDownloaderTest() { |
Ivan Kotenkov | 75b1c3a | 2017-10-24 14:47:24 | [diff] [blame] | 110 | context_ = nullptr; |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 111 | |
| 112 | // The GetInterceptor requires the message loop to run to destruct correctly. |
| 113 | get_interceptor_.reset(); |
| 114 | RunThreadsUntilIdle(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void CrxDownloaderTest::SetUp() { |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 118 | num_download_complete_calls_ = 0; |
| 119 | download_complete_result_ = CrxDownloader::Result(); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 120 | num_progress_calls_ = 0; |
| 121 | download_progress_result_ = CrxDownloader::Result(); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 122 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 123 | // Do not use the background downloader in these tests. |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 124 | crx_downloader_ = CrxDownloader::Create(false, context_.get()); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 125 | crx_downloader_->set_progress_callback(progress_callback_); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 126 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 127 | get_interceptor_ = std::make_unique<GetInterceptor>( |
| 128 | base::ThreadTaskRunnerHandle::Get(), base::ThreadTaskRunnerHandle::Get()); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void CrxDownloaderTest::TearDown() { |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 132 | crx_downloader_.reset(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void CrxDownloaderTest::Quit() { |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 136 | if (!quit_closure_.is_null()) |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 137 | std::move(quit_closure_).Run(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 140 | void CrxDownloaderTest::DownloadComplete(int crx_context, |
| 141 | const CrxDownloader::Result& result) { |
| 142 | ++num_download_complete_calls_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 143 | crx_context_ = crx_context; |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 144 | download_complete_result_ = result; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 145 | Quit(); |
| 146 | } |
| 147 | |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 148 | void CrxDownloaderTest::DownloadProgress(int crx_context, |
| 149 | const CrxDownloader::Result& result) { |
| 150 | ++num_progress_calls_; |
| 151 | download_progress_result_ = result; |
| 152 | } |
| 153 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 154 | void CrxDownloaderTest::RunThreads() { |
| 155 | base::RunLoop runloop; |
| 156 | quit_closure_ = runloop.QuitClosure(); |
| 157 | runloop.Run(); |
| 158 | |
| 159 | // Since some tests need to drain currently enqueued tasks such as network |
| 160 | // intercepts on the IO thread, run the threads until they are |
| 161 | // idle. The component updater service won't loop again until the loop count |
| 162 | // is set and the service is started. |
| 163 | RunThreadsUntilIdle(); |
| 164 | } |
| 165 | |
| 166 | void CrxDownloaderTest::RunThreadsUntilIdle() { |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame] | 167 | scoped_task_environment_.RunUntilIdle(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 168 | base::RunLoop().RunUntilIdle(); |
| 169 | } |
| 170 | |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 171 | // Tests that starting a download without a url results in an error. |
| 172 | TEST_F(CrxDownloaderTest, NoUrl) { |
| 173 | std::vector<GURL> urls; |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 174 | crx_downloader_->StartDownload(urls, std::string("abcd"), |
| 175 | std::move(callback_)); |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 176 | RunThreadsUntilIdle(); |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 177 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 178 | EXPECT_EQ(1, num_download_complete_calls_); |
| 179 | EXPECT_EQ(kExpectedContext, crx_context_); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 180 | EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_URL), |
| 181 | download_complete_result_.error); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 182 | EXPECT_TRUE(download_complete_result_.response.empty()); |
| 183 | EXPECT_EQ(-1, download_complete_result_.downloaded_bytes); |
| 184 | EXPECT_EQ(-1, download_complete_result_.total_bytes); |
| 185 | EXPECT_EQ(0, num_progress_calls_); |
| 186 | } |
| 187 | |
| 188 | // Tests that starting a download without providing a hash results in an error. |
| 189 | TEST_F(CrxDownloaderTest, NoHash) { |
| 190 | std::vector<GURL> urls(1, GURL("http://somehost/somefile")); |
| 191 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 192 | crx_downloader_->StartDownload(urls, std::string(), std::move(callback_)); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 193 | RunThreadsUntilIdle(); |
| 194 | |
| 195 | EXPECT_EQ(1, num_download_complete_calls_); |
| 196 | EXPECT_EQ(kExpectedContext, crx_context_); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 197 | EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_HASH), |
| 198 | download_complete_result_.error); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 199 | EXPECT_TRUE(download_complete_result_.response.empty()); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 200 | EXPECT_EQ(-1, download_complete_result_.downloaded_bytes); |
| 201 | EXPECT_EQ(-1, download_complete_result_.total_bytes); |
| 202 | EXPECT_EQ(0, num_progress_calls_); |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 205 | // Tests that downloading from one url is successful. |
| 206 | TEST_F(CrxDownloaderTest, OneUrl) { |
| 207 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 208 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 209 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 210 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 211 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 212 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 213 | crx_downloader_->StartDownloadFromUrl( |
| 214 | expected_crx_url, std::string(hash_jebg), std::move(callback_)); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 215 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 216 | |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 217 | EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 218 | |
| 219 | EXPECT_EQ(1, num_download_complete_calls_); |
| 220 | EXPECT_EQ(kExpectedContext, crx_context_); |
| 221 | EXPECT_EQ(0, download_complete_result_.error); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 222 | EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 223 | EXPECT_EQ(1843, download_complete_result_.total_bytes); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 224 | EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 225 | |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame] | 226 | EXPECT_TRUE( |
| 227 | DeleteFileAndEmptyParentDirectory(download_complete_result_.response)); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 228 | |
| 229 | EXPECT_LE(1, num_progress_calls_); |
| 230 | EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); |
| 231 | EXPECT_EQ(1843, download_progress_result_.total_bytes); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 232 | } |
| 233 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 234 | // Tests that downloading from one url fails if the actual hash of the file |
| 235 | // does not match the expected hash. |
| 236 | TEST_F(CrxDownloaderTest, OneUrlBadHash) { |
| 237 | const GURL expected_crx_url = |
| 238 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
| 239 | |
| 240 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
| 241 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
| 242 | |
| 243 | crx_downloader_->StartDownloadFromUrl( |
| 244 | expected_crx_url, |
| 245 | std::string( |
| 246 | "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"), |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 247 | std::move(callback_)); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 248 | RunThreads(); |
| 249 | |
| 250 | EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
| 251 | |
| 252 | EXPECT_EQ(1, num_download_complete_calls_); |
| 253 | EXPECT_EQ(kExpectedContext, crx_context_); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 254 | EXPECT_EQ(static_cast<int>(CrxDownloaderError::BAD_HASH), |
| 255 | download_complete_result_.error); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 256 | EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 257 | EXPECT_EQ(1843, download_complete_result_.total_bytes); |
| 258 | EXPECT_TRUE(download_complete_result_.response.empty()); |
| 259 | |
| 260 | EXPECT_LE(1, num_progress_calls_); |
| 261 | EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); |
| 262 | EXPECT_EQ(1843, download_progress_result_.total_bytes); |
| 263 | } |
| 264 | |
| 265 | // Tests that specifying two urls has no side effects. Expect a successful |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 266 | // download, and only one download request be made. |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 267 | TEST_F(CrxDownloaderTest, TwoUrls) { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 268 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 269 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 270 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 271 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 272 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 273 | |
| 274 | std::vector<GURL> urls; |
| 275 | urls.push_back(expected_crx_url); |
| 276 | urls.push_back(expected_crx_url); |
| 277 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 278 | crx_downloader_->StartDownload(urls, std::string(hash_jebg), |
| 279 | std::move(callback_)); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 280 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 281 | |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 282 | EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 283 | |
| 284 | EXPECT_EQ(1, num_download_complete_calls_); |
| 285 | EXPECT_EQ(kExpectedContext, crx_context_); |
| 286 | EXPECT_EQ(0, download_complete_result_.error); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 287 | EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 288 | EXPECT_EQ(1843, download_complete_result_.total_bytes); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 289 | EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 290 | |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame] | 291 | EXPECT_TRUE( |
| 292 | DeleteFileAndEmptyParentDirectory(download_complete_result_.response)); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 293 | |
| 294 | EXPECT_LE(1, num_progress_calls_); |
| 295 | EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); |
| 296 | EXPECT_EQ(1843, download_progress_result_.total_bytes); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 299 | // Tests that the fallback to a valid url is successful. |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 300 | TEST_F(CrxDownloaderTest, TwoUrls_FirstInvalid) { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 301 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 302 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 303 | const GURL no_file_url = |
| 304 | GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 305 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 306 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 307 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 308 | get_interceptor_->SetResponse(no_file_url, |
| 309 | base::FilePath(FILE_PATH_LITERAL("no-file"))); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 310 | |
| 311 | std::vector<GURL> urls; |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 312 | urls.push_back(no_file_url); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 313 | urls.push_back(expected_crx_url); |
| 314 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 315 | crx_downloader_->StartDownload(urls, std::string(hash_jebg), |
| 316 | std::move(callback_)); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 317 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 318 | |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 319 | EXPECT_EQ(2, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 320 | |
| 321 | EXPECT_EQ(1, num_download_complete_calls_); |
| 322 | EXPECT_EQ(kExpectedContext, crx_context_); |
| 323 | EXPECT_EQ(0, download_complete_result_.error); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 324 | EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 325 | EXPECT_EQ(1843, download_complete_result_.total_bytes); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 326 | EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 327 | |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame] | 328 | EXPECT_TRUE( |
| 329 | DeleteFileAndEmptyParentDirectory(download_complete_result_.response)); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 330 | |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 331 | // Expect at least some progress reported by the fetcher. |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 332 | EXPECT_LE(1, num_progress_calls_); |
| 333 | EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); |
| 334 | EXPECT_EQ(1843, download_progress_result_.total_bytes); |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 335 | |
| 336 | const auto download_metrics = crx_downloader_->download_metrics(); |
| 337 | ASSERT_EQ(2u, download_metrics.size()); |
| 338 | EXPECT_EQ(no_file_url, download_metrics[0].url); |
| 339 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[0].error); |
| 340 | EXPECT_EQ(-1, download_metrics[0].downloaded_bytes); |
| 341 | EXPECT_EQ(-1, download_metrics[0].total_bytes); |
| 342 | EXPECT_EQ(expected_crx_url, download_metrics[1].url); |
| 343 | EXPECT_EQ(0, download_metrics[1].error); |
| 344 | EXPECT_EQ(1843, download_metrics[1].downloaded_bytes); |
| 345 | EXPECT_EQ(1843, download_metrics[1].total_bytes); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // Tests that the download succeeds if the first url is correct and the |
| 349 | // second bad url does not have a side-effect. |
| 350 | TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) { |
| 351 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 352 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 353 | const GURL no_file_url = |
| 354 | GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 355 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 356 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 357 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 358 | get_interceptor_->SetResponse(no_file_url, |
| 359 | base::FilePath(FILE_PATH_LITERAL("no-file"))); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 360 | |
| 361 | std::vector<GURL> urls; |
| 362 | urls.push_back(expected_crx_url); |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 363 | urls.push_back(no_file_url); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 364 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 365 | crx_downloader_->StartDownload(urls, std::string(hash_jebg), |
| 366 | std::move(callback_)); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 367 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 368 | |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 369 | EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 370 | |
| 371 | EXPECT_EQ(1, num_download_complete_calls_); |
| 372 | EXPECT_EQ(kExpectedContext, crx_context_); |
| 373 | EXPECT_EQ(0, download_complete_result_.error); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 374 | EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 375 | EXPECT_EQ(1843, download_complete_result_.total_bytes); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 376 | EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 377 | |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame] | 378 | EXPECT_TRUE( |
| 379 | DeleteFileAndEmptyParentDirectory(download_complete_result_.response)); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 380 | |
| 381 | EXPECT_LE(1, num_progress_calls_); |
| 382 | EXPECT_EQ(1843, download_progress_result_.downloaded_bytes); |
| 383 | EXPECT_EQ(1843, download_progress_result_.total_bytes); |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 384 | |
| 385 | EXPECT_EQ(1u, crx_downloader_->download_metrics().size()); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 386 | } |
| 387 | |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 388 | // Tests that the download fails if both urls don't serve content. |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 389 | TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { |
| 390 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 391 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 392 | |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 393 | get_interceptor_->SetResponse(expected_crx_url, |
| 394 | base::FilePath(FILE_PATH_LITERAL("no-file"))); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 395 | |
| 396 | std::vector<GURL> urls; |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 397 | urls.push_back(expected_crx_url); |
| 398 | urls.push_back(expected_crx_url); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 399 | |
Sorin Jianu | a8ef73d | 2017-11-02 16:55:17 | [diff] [blame] | 400 | crx_downloader_->StartDownload(urls, std::string(hash_jebg), |
| 401 | std::move(callback_)); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 402 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 403 | |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 404 | EXPECT_EQ(2, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 405 | |
| 406 | EXPECT_EQ(1, num_download_complete_calls_); |
| 407 | EXPECT_EQ(kExpectedContext, crx_context_); |
| 408 | EXPECT_NE(0, download_complete_result_.error); |
| 409 | EXPECT_TRUE(download_complete_result_.response.empty()); |
Sorin Jianu | c35bacf | 2018-01-31 18:25:28 | [diff] [blame] | 410 | |
| 411 | const auto download_metrics = crx_downloader_->download_metrics(); |
| 412 | ASSERT_EQ(2u, download_metrics.size()); |
| 413 | EXPECT_EQ(expected_crx_url, download_metrics[0].url); |
| 414 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[0].error); |
| 415 | EXPECT_EQ(-1, download_metrics[0].downloaded_bytes); |
| 416 | EXPECT_EQ(-1, download_metrics[0].total_bytes); |
| 417 | EXPECT_EQ(expected_crx_url, download_metrics[1].url); |
| 418 | EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[1].error); |
| 419 | EXPECT_EQ(-1, download_metrics[1].downloaded_bytes); |
| 420 | EXPECT_EQ(-1, download_metrics[1].total_bytes); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 421 | } |
| 422 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 423 | } // namespace update_client |