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