blob: 06a7361b22afec918ebe66d8eb53d0c4a2470a65 [file] [log] [blame]
[email protected]afa378f22013-12-02 03:37:541// 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
dchengd0fc6aa92016-04-22 18:03:125#include "components/update_client/crx_downloader.h"
6
7#include <memory>
Sorin Jianua8ef73d2017-11-02 16:55:178#include <utility>
dchengd0fc6aa92016-04-22 18:03:129
[email protected]afa378f22013-12-02 03:37:5410#include "base/bind.h"
[email protected]afa378f22013-12-02 03:37:5411#include "base/files/file_path.h"
thestig18dfb7a52014-08-26 10:44:0412#include "base/files/file_util.h"
[email protected]afa378f22013-12-02 03:37:5413#include "base/memory/ref_counted.h"
[email protected]afa378f22013-12-02 03:37:5414#include "base/path_service.h"
15#include "base/run_loop.h"
Antonio Gomes0807dd62018-08-10 14:28:4716#include "base/test/bind_test_util.h"
Sorin Jianu72d8fe0d2017-07-11 18:42:1617#include "base/test/scoped_task_environment.h"
gab7966d312016-05-11 20:35:0118#include "base/threading/thread_task_runner_handle.h"
avi5dd91f82015-12-25 22:30:4619#include "build/build_config.h"
sorin7b8650522016-11-02 18:23:4120#include "components/update_client/update_client_errors.h"
Sorin Jianubbd51392017-07-20 01:48:3121#include "components/update_client/utils.h"
[email protected]afa378f22013-12-02 03:37:5422#include "net/base/net_errors.h"
Antonio Gomes0807dd62018-08-10 14:28:4723#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
24#include "services/network/test/test_url_loader_factory.h"
[email protected]afa378f22013-12-02 03:37:5425#include "testing/gtest/include/gtest/gtest.h"
26
[email protected]148dcfd32014-04-29 00:54:3027using base::ContentsEqual;
[email protected]afa378f22013-12-02 03:37:5428
sorin52ac0882015-01-24 01:15:0029namespace update_client {
[email protected]afa378f22013-12-02 03:37:5430
31namespace {
32
[email protected]148dcfd32014-04-29 00:54:3033const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx";
34
sorin74e70672016-02-03 03:13:1035const char hash_jebg[] =
36 "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87";
37
[email protected]148dcfd32014-04-29 00:54:3038base::FilePath MakeTestFilePath(const char* file) {
[email protected]afa378f22013-12-02 03:37:5439 base::FilePath path;
Avi Drissmanf617d012018-05-02 18:48:5340 base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
sorin74e70672016-02-03 03:13:1041 return path.AppendASCII("components/test/data/update_client")
sorin52ac0882015-01-24 01:15:0042 .AppendASCII(file);
[email protected]afa378f22013-12-02 03:37:5443}
44
45} // namespace
46
47class CrxDownloaderTest : public testing::Test {
48 public:
49 CrxDownloaderTest();
dcheng30a1b1542014-10-29 21:27:5050 ~CrxDownloaderTest() override;
[email protected]afa378f22013-12-02 03:37:5451
52 // Overrides from testing::Test.
dcheng30a1b1542014-10-29 21:27:5053 void SetUp() override;
54 void TearDown() override;
[email protected]afa378f22013-12-02 03:37:5455
56 void Quit();
57 void RunThreads();
58 void RunThreadsUntilIdle();
59
[email protected]3cb2a4f2013-12-07 21:54:3460 void DownloadComplete(int crx_context, const CrxDownloader::Result& result);
[email protected]afa378f22013-12-02 03:37:5461
Antonio Gomes31237fb2018-08-27 19:11:0362 void DownloadProgress(int crx_context);
[email protected]8a5ebd432014-05-02 00:21:2263
Antonio Gomes0807dd62018-08-10 14:28:4764 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]afa378f22013-12-02 03:37:5470 protected:
dchengd0fc6aa92016-04-22 18:03:1271 std::unique_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:5472
Antonio Gomes0807dd62018-08-10 14:28:4773 network::TestURLLoaderFactory test_url_loader_factory_;
tommyclieaae5d92014-09-09 06:03:4774
[email protected]1b6587dc52014-04-26 00:38:5575 CrxDownloader::DownloadCallback callback_;
[email protected]8a5ebd432014-05-02 00:21:2276 CrxDownloader::ProgressCallback progress_callback_;
[email protected]1b6587dc52014-04-26 00:38:5577
[email protected]afa378f22013-12-02 03:37:5478 int crx_context_;
[email protected]afa378f22013-12-02 03:37:5479
[email protected]148dcfd32014-04-29 00:54:3080 int num_download_complete_calls_;
81 CrxDownloader::Result download_complete_result_;
[email protected]afa378f22013-12-02 03:37:5482
[email protected]8a5ebd432014-05-02 00:21:2283 // These members are updated by DownloadProgress.
84 int num_progress_calls_;
[email protected]8a5ebd432014-05-02 00:21:2285
Antonio Gomes0807dd62018-08-10 14:28:4786 // Accumulates the number of loads triggered.
87 int interceptor_count_ = 0;
88
[email protected]afa378f22013-12-02 03:37:5489 // A magic value for the context to be used in the tests.
90 static const int kExpectedContext = 0xaabb;
91
92 private:
Sorin Jianu72d8fe0d2017-07-11 18:42:1693 base::test::ScopedTaskEnvironment scoped_task_environment_;
Antonio Gomes0807dd62018-08-10 14:28:4794 scoped_refptr<network::SharedURLLoaderFactory>
95 test_shared_url_loader_factory_;
Sorin Jianua8ef73d2017-11-02 16:55:1796 base::OnceClosure quit_closure_;
[email protected]afa378f22013-12-02 03:37:5497};
98
99const int CrxDownloaderTest::kExpectedContext;
100
101CrxDownloaderTest::CrxDownloaderTest()
Sorin Jianua8ef73d2017-11-02 16:55:17102 : callback_(base::BindOnce(&CrxDownloaderTest::DownloadComplete,
103 base::Unretained(this),
104 kExpectedContext)),
[email protected]8a5ebd432014-05-02 00:21:22105 progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress,
106 base::Unretained(this),
107 kExpectedContext)),
[email protected]1b6587dc52014-04-26 00:38:55108 crx_context_(0),
[email protected]148dcfd32014-04-29 00:54:30109 num_download_complete_calls_(0),
[email protected]8a5ebd432014-05-02 00:21:22110 num_progress_calls_(0),
Sorin Jianu72d8fe0d2017-07-11 18:42:16111 scoped_task_environment_(
112 base::test::ScopedTaskEnvironment::MainThreadType::IO),
Antonio Gomes0807dd62018-08-10 14:28:47113 test_shared_url_loader_factory_(
114 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
115 &test_url_loader_factory_)) {}
[email protected]afa378f22013-12-02 03:37:54116
Antonio Gomes0807dd62018-08-10 14:28:47117CrxDownloaderTest::~CrxDownloaderTest() {}
[email protected]afa378f22013-12-02 03:37:54118
119void CrxDownloaderTest::SetUp() {
[email protected]148dcfd32014-04-29 00:54:30120 num_download_complete_calls_ = 0;
121 download_complete_result_ = CrxDownloader::Result();
[email protected]8a5ebd432014-05-02 00:21:22122 num_progress_calls_ = 0;
tommyclieaae5d92014-09-09 06:03:47123
sorin9797aba2015-04-17 17:15:03124 // Do not use the background downloader in these tests.
Antonio Gomes0807dd62018-08-10 14:28:47125 crx_downloader_ =
126 CrxDownloader::Create(false, test_shared_url_loader_factory_);
[email protected]8a5ebd432014-05-02 00:21:22127 crx_downloader_->set_progress_callback(progress_callback_);
tommyclieaae5d92014-09-09 06:03:47128
Antonio Gomes0807dd62018-08-10 14:28:47129 test_url_loader_factory_.SetInterceptor(base::BindLambdaForTesting(
130 [&](const network::ResourceRequest& request) { interceptor_count_++; }));
[email protected]afa378f22013-12-02 03:37:54131}
132
133void CrxDownloaderTest::TearDown() {
[email protected]8a5ebd432014-05-02 00:21:22134 crx_downloader_.reset();
[email protected]afa378f22013-12-02 03:37:54135}
136
137void CrxDownloaderTest::Quit() {
[email protected]da37c1d2013-12-19 01:04:38138 if (!quit_closure_.is_null())
Sorin Jianua8ef73d2017-11-02 16:55:17139 std::move(quit_closure_).Run();
[email protected]afa378f22013-12-02 03:37:54140}
141
[email protected]148dcfd32014-04-29 00:54:30142void CrxDownloaderTest::DownloadComplete(int crx_context,
143 const CrxDownloader::Result& result) {
144 ++num_download_complete_calls_;
[email protected]afa378f22013-12-02 03:37:54145 crx_context_ = crx_context;
[email protected]148dcfd32014-04-29 00:54:30146 download_complete_result_ = result;
[email protected]afa378f22013-12-02 03:37:54147 Quit();
148}
149
Antonio Gomes31237fb2018-08-27 19:11:03150void CrxDownloaderTest::DownloadProgress(int crx_context) {
[email protected]8a5ebd432014-05-02 00:21:22151 ++num_progress_calls_;
[email protected]8a5ebd432014-05-02 00:21:22152}
153
Antonio Gomes0807dd62018-08-10 14:28:47154void CrxDownloaderTest::AddResponse(const GURL& url,
155 const base::FilePath& file_path,
156 int net_error) {
157 if (net_error == net::OK) {
158 std::string data;
159 EXPECT_TRUE(base::ReadFileToString(file_path, &data));
160 network::ResourceResponseHead head;
161 head.content_length = data.size();
162 network::URLLoaderCompletionStatus status(net_error);
163 status.decoded_body_length = data.size();
164 test_url_loader_factory_.AddResponse(url, head, data, status);
165 return;
166 }
167
168 EXPECT_NE(net_error, net::OK);
169 test_url_loader_factory_.AddResponse(
170 url, network::ResourceResponseHead(), std::string(),
171 network::URLLoaderCompletionStatus(net_error));
172}
173
[email protected]afa378f22013-12-02 03:37:54174void CrxDownloaderTest::RunThreads() {
175 base::RunLoop runloop;
176 quit_closure_ = runloop.QuitClosure();
177 runloop.Run();
178
179 // Since some tests need to drain currently enqueued tasks such as network
180 // intercepts on the IO thread, run the threads until they are
181 // idle. The component updater service won't loop again until the loop count
182 // is set and the service is started.
183 RunThreadsUntilIdle();
184}
185
186void CrxDownloaderTest::RunThreadsUntilIdle() {
Sorin Jianubbd51392017-07-20 01:48:31187 scoped_task_environment_.RunUntilIdle();
[email protected]afa378f22013-12-02 03:37:54188 base::RunLoop().RunUntilIdle();
189}
190
[email protected]da37c1d2013-12-19 01:04:38191// Tests that starting a download without a url results in an error.
192TEST_F(CrxDownloaderTest, NoUrl) {
193 std::vector<GURL> urls;
Sorin Jianua8ef73d2017-11-02 16:55:17194 crx_downloader_->StartDownload(urls, std::string("abcd"),
195 std::move(callback_));
[email protected]da37c1d2013-12-19 01:04:38196 RunThreadsUntilIdle();
[email protected]da37c1d2013-12-19 01:04:38197
[email protected]148dcfd32014-04-29 00:54:30198 EXPECT_EQ(1, num_download_complete_calls_);
199 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41200 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_URL),
201 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10202 EXPECT_TRUE(download_complete_result_.response.empty());
sorin74e70672016-02-03 03:13:10203 EXPECT_EQ(0, num_progress_calls_);
204}
205
206// Tests that starting a download without providing a hash results in an error.
207TEST_F(CrxDownloaderTest, NoHash) {
208 std::vector<GURL> urls(1, GURL("http://somehost/somefile"));
209
Sorin Jianua8ef73d2017-11-02 16:55:17210 crx_downloader_->StartDownload(urls, std::string(), std::move(callback_));
sorin74e70672016-02-03 03:13:10211 RunThreadsUntilIdle();
212
213 EXPECT_EQ(1, num_download_complete_calls_);
214 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41215 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_HASH),
216 download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30217 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]8a5ebd432014-05-02 00:21:22218 EXPECT_EQ(0, num_progress_calls_);
[email protected]da37c1d2013-12-19 01:04:38219}
220
[email protected]afa378f22013-12-02 03:37:54221// Tests that downloading from one url is successful.
222TEST_F(CrxDownloaderTest, OneUrl) {
223 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45224 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54225
[email protected]148dcfd32014-04-29 00:54:30226 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47227 AddResponse(expected_crx_url, test_file, net::OK);
[email protected]afa378f22013-12-02 03:37:54228
Sorin Jianua8ef73d2017-11-02 16:55:17229 crx_downloader_->StartDownloadFromUrl(
230 expected_crx_url, std::string(hash_jebg), std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54231 RunThreads();
[email protected]afa378f22013-12-02 03:37:54232
Antonio Gomes0807dd62018-08-10 14:28:47233 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30234
235 EXPECT_EQ(1, num_download_complete_calls_);
236 EXPECT_EQ(kExpectedContext, crx_context_);
237 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30238 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
239
Sorin Jianubbd51392017-07-20 01:48:31240 EXPECT_TRUE(
241 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22242
243 EXPECT_LE(1, num_progress_calls_);
[email protected]afa378f22013-12-02 03:37:54244}
245
sorin74e70672016-02-03 03:13:10246// Tests that downloading from one url fails if the actual hash of the file
247// does not match the expected hash.
248TEST_F(CrxDownloaderTest, OneUrlBadHash) {
249 const GURL expected_crx_url =
250 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
251
252 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47253 AddResponse(expected_crx_url, test_file, net::OK);
sorin74e70672016-02-03 03:13:10254
255 crx_downloader_->StartDownloadFromUrl(
256 expected_crx_url,
257 std::string(
258 "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"),
Sorin Jianua8ef73d2017-11-02 16:55:17259 std::move(callback_));
sorin74e70672016-02-03 03:13:10260 RunThreads();
261
Antonio Gomes0807dd62018-08-10 14:28:47262 EXPECT_EQ(1, GetInterceptorCount());
sorin74e70672016-02-03 03:13:10263
264 EXPECT_EQ(1, num_download_complete_calls_);
265 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41266 EXPECT_EQ(static_cast<int>(CrxDownloaderError::BAD_HASH),
267 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10268 EXPECT_TRUE(download_complete_result_.response.empty());
269
270 EXPECT_LE(1, num_progress_calls_);
sorin74e70672016-02-03 03:13:10271}
272
273// Tests that specifying two urls has no side effects. Expect a successful
[email protected]afa378f22013-12-02 03:37:54274// download, and only one download request be made.
Sorin Jianuc35bacf2018-01-31 18:25:28275TEST_F(CrxDownloaderTest, TwoUrls) {
[email protected]afa378f22013-12-02 03:37:54276 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45277 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54278
[email protected]148dcfd32014-04-29 00:54:30279 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47280 AddResponse(expected_crx_url, test_file, net::OK);
[email protected]afa378f22013-12-02 03:37:54281
282 std::vector<GURL> urls;
283 urls.push_back(expected_crx_url);
284 urls.push_back(expected_crx_url);
285
Sorin Jianua8ef73d2017-11-02 16:55:17286 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
287 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54288 RunThreads();
[email protected]afa378f22013-12-02 03:37:54289
Antonio Gomes0807dd62018-08-10 14:28:47290 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30291
292 EXPECT_EQ(1, num_download_complete_calls_);
293 EXPECT_EQ(kExpectedContext, crx_context_);
294 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30295 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
296
Sorin Jianubbd51392017-07-20 01:48:31297 EXPECT_TRUE(
298 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22299
300 EXPECT_LE(1, num_progress_calls_);
[email protected]afa378f22013-12-02 03:37:54301}
302
[email protected]afa378f22013-12-02 03:37:54303// Tests that the fallback to a valid url is successful.
Sorin Jianuc35bacf2018-01-31 18:25:28304TEST_F(CrxDownloaderTest, TwoUrls_FirstInvalid) {
[email protected]afa378f22013-12-02 03:37:54305 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45306 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
Sorin Jianuc35bacf2018-01-31 18:25:28307 const GURL no_file_url =
308 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx");
[email protected]afa378f22013-12-02 03:37:54309
[email protected]148dcfd32014-04-29 00:54:30310 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47311 AddResponse(expected_crx_url, test_file, net::OK);
312 AddResponse(no_file_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54313
314 std::vector<GURL> urls;
Sorin Jianuc35bacf2018-01-31 18:25:28315 urls.push_back(no_file_url);
[email protected]afa378f22013-12-02 03:37:54316 urls.push_back(expected_crx_url);
317
Sorin Jianua8ef73d2017-11-02 16:55:17318 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
319 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54320 RunThreads();
[email protected]afa378f22013-12-02 03:37:54321
Antonio Gomes0807dd62018-08-10 14:28:47322 EXPECT_EQ(2, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30323
324 EXPECT_EQ(1, num_download_complete_calls_);
325 EXPECT_EQ(kExpectedContext, crx_context_);
326 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30327 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
328
Sorin Jianubbd51392017-07-20 01:48:31329 EXPECT_TRUE(
330 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22331
Antonio Gomes0807dd62018-08-10 14:28:47332 // Expect at least some progress reported by the loader.
[email protected]8a5ebd432014-05-02 00:21:22333 EXPECT_LE(1, num_progress_calls_);
Sorin Jianuc35bacf2018-01-31 18:25:28334
335 const auto download_metrics = crx_downloader_->download_metrics();
336 ASSERT_EQ(2u, download_metrics.size());
337 EXPECT_EQ(no_file_url, download_metrics[0].url);
338 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[0].error);
339 EXPECT_EQ(-1, download_metrics[0].downloaded_bytes);
340 EXPECT_EQ(-1, download_metrics[0].total_bytes);
341 EXPECT_EQ(expected_crx_url, download_metrics[1].url);
342 EXPECT_EQ(0, download_metrics[1].error);
343 EXPECT_EQ(1843, download_metrics[1].downloaded_bytes);
344 EXPECT_EQ(1843, download_metrics[1].total_bytes);
[email protected]afa378f22013-12-02 03:37:54345}
346
347// Tests that the download succeeds if the first url is correct and the
348// second bad url does not have a side-effect.
349TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
350 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45351 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
Sorin Jianuc35bacf2018-01-31 18:25:28352 const GURL no_file_url =
353 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx");
[email protected]afa378f22013-12-02 03:37:54354
[email protected]148dcfd32014-04-29 00:54:30355 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47356 AddResponse(expected_crx_url, test_file, net::OK);
357 AddResponse(no_file_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54358
359 std::vector<GURL> urls;
360 urls.push_back(expected_crx_url);
Sorin Jianuc35bacf2018-01-31 18:25:28361 urls.push_back(no_file_url);
[email protected]afa378f22013-12-02 03:37:54362
Sorin Jianua8ef73d2017-11-02 16:55:17363 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
364 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54365 RunThreads();
[email protected]afa378f22013-12-02 03:37:54366
Antonio Gomes0807dd62018-08-10 14:28:47367 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30368
369 EXPECT_EQ(1, num_download_complete_calls_);
370 EXPECT_EQ(kExpectedContext, crx_context_);
371 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30372 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
373
Sorin Jianubbd51392017-07-20 01:48:31374 EXPECT_TRUE(
375 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22376
377 EXPECT_LE(1, num_progress_calls_);
Sorin Jianuc35bacf2018-01-31 18:25:28378
379 EXPECT_EQ(1u, crx_downloader_->download_metrics().size());
[email protected]afa378f22013-12-02 03:37:54380}
381
Sorin Jianuc35bacf2018-01-31 18:25:28382// Tests that the download fails if both urls don't serve content.
[email protected]afa378f22013-12-02 03:37:54383TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
384 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45385 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54386
Antonio Gomes0807dd62018-08-10 14:28:47387 AddResponse(expected_crx_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54388
389 std::vector<GURL> urls;
Sorin Jianuc35bacf2018-01-31 18:25:28390 urls.push_back(expected_crx_url);
391 urls.push_back(expected_crx_url);
[email protected]afa378f22013-12-02 03:37:54392
Sorin Jianua8ef73d2017-11-02 16:55:17393 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
394 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54395 RunThreads();
[email protected]afa378f22013-12-02 03:37:54396
Antonio Gomes0807dd62018-08-10 14:28:47397 EXPECT_EQ(2, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30398
399 EXPECT_EQ(1, num_download_complete_calls_);
400 EXPECT_EQ(kExpectedContext, crx_context_);
401 EXPECT_NE(0, download_complete_result_.error);
402 EXPECT_TRUE(download_complete_result_.response.empty());
Sorin Jianuc35bacf2018-01-31 18:25:28403
404 const auto download_metrics = crx_downloader_->download_metrics();
405 ASSERT_EQ(2u, download_metrics.size());
406 EXPECT_EQ(expected_crx_url, download_metrics[0].url);
407 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[0].error);
408 EXPECT_EQ(-1, download_metrics[0].downloaded_bytes);
409 EXPECT_EQ(-1, download_metrics[0].total_bytes);
410 EXPECT_EQ(expected_crx_url, download_metrics[1].url);
411 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[1].error);
412 EXPECT_EQ(-1, download_metrics[1].downloaded_bytes);
413 EXPECT_EQ(-1, download_metrics[1].total_bytes);
[email protected]afa378f22013-12-02 03:37:54414}
415
sorin52ac0882015-01-24 01:15:00416} // namespace update_client