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