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