blob: 40bbd3bb4aa8baecb76fbd8a90ab1aa29d88c96c [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"
Sorin Jianu4e0b41d2019-02-20 17:21:5820#include "components/update_client/net/network_chromium.h"
sorin7b8650522016-11-02 18:23:4121#include "components/update_client/update_client_errors.h"
Sorin Jianubbd51392017-07-20 01:48:3122#include "components/update_client/utils.h"
[email protected]afa378f22013-12-02 03:37:5423#include "net/base/net_errors.h"
Antonio Gomes0807dd62018-08-10 14:28:4724#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
25#include "services/network/test/test_url_loader_factory.h"
[email protected]afa378f22013-12-02 03:37:5426#include "testing/gtest/include/gtest/gtest.h"
27
[email protected]148dcfd32014-04-29 00:54:3028using base::ContentsEqual;
[email protected]afa378f22013-12-02 03:37:5429
sorin52ac0882015-01-24 01:15:0030namespace update_client {
[email protected]afa378f22013-12-02 03:37:5431
32namespace {
33
[email protected]148dcfd32014-04-29 00:54:3034const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx";
35
sorin74e70672016-02-03 03:13:1036const char hash_jebg[] =
37 "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87";
38
[email protected]148dcfd32014-04-29 00:54:3039base::FilePath MakeTestFilePath(const char* file) {
[email protected]afa378f22013-12-02 03:37:5440 base::FilePath path;
Avi Drissmanf617d012018-05-02 18:48:5341 base::PathService::Get(base::DIR_SOURCE_ROOT, &path);
sorin74e70672016-02-03 03:13:1042 return path.AppendASCII("components/test/data/update_client")
sorin52ac0882015-01-24 01:15:0043 .AppendASCII(file);
[email protected]afa378f22013-12-02 03:37:5444}
45
46} // namespace
47
48class CrxDownloaderTest : public testing::Test {
49 public:
50 CrxDownloaderTest();
dcheng30a1b1542014-10-29 21:27:5051 ~CrxDownloaderTest() override;
[email protected]afa378f22013-12-02 03:37:5452
53 // Overrides from testing::Test.
dcheng30a1b1542014-10-29 21:27:5054 void SetUp() override;
55 void TearDown() override;
[email protected]afa378f22013-12-02 03:37:5456
57 void Quit();
58 void RunThreads();
59 void RunThreadsUntilIdle();
60
[email protected]3cb2a4f2013-12-07 21:54:3461 void DownloadComplete(int crx_context, const CrxDownloader::Result& result);
[email protected]afa378f22013-12-02 03:37:5462
Antonio Gomes31237fb2018-08-27 19:11:0363 void DownloadProgress(int crx_context);
[email protected]8a5ebd432014-05-02 00:21:2264
Antonio Gomes0807dd62018-08-10 14:28:4765 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]afa378f22013-12-02 03:37:5471 protected:
dchengd0fc6aa92016-04-22 18:03:1272 std::unique_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:5473
Antonio Gomes0807dd62018-08-10 14:28:4774 network::TestURLLoaderFactory test_url_loader_factory_;
tommyclieaae5d92014-09-09 06:03:4775
[email protected]1b6587dc52014-04-26 00:38:5576 CrxDownloader::DownloadCallback callback_;
[email protected]8a5ebd432014-05-02 00:21:2277 CrxDownloader::ProgressCallback progress_callback_;
[email protected]1b6587dc52014-04-26 00:38:5578
[email protected]afa378f22013-12-02 03:37:5479 int crx_context_;
[email protected]afa378f22013-12-02 03:37:5480
[email protected]148dcfd32014-04-29 00:54:3081 int num_download_complete_calls_;
82 CrxDownloader::Result download_complete_result_;
[email protected]afa378f22013-12-02 03:37:5483
[email protected]8a5ebd432014-05-02 00:21:2284 // These members are updated by DownloadProgress.
85 int num_progress_calls_;
[email protected]8a5ebd432014-05-02 00:21:2286
Antonio Gomes0807dd62018-08-10 14:28:4787 // Accumulates the number of loads triggered.
88 int interceptor_count_ = 0;
89
[email protected]afa378f22013-12-02 03:37:5490 // A magic value for the context to be used in the tests.
91 static const int kExpectedContext = 0xaabb;
92
93 private:
Gabriel Charettedfa36042019-08-19 17:30:1194 base::test::TaskEnvironment task_environment_;
Antonio Gomes0807dd62018-08-10 14:28:4795 scoped_refptr<network::SharedURLLoaderFactory>
96 test_shared_url_loader_factory_;
Sorin Jianua8ef73d2017-11-02 16:55:1797 base::OnceClosure quit_closure_;
[email protected]afa378f22013-12-02 03:37:5498};
99
100const int CrxDownloaderTest::kExpectedContext;
101
102CrxDownloaderTest::CrxDownloaderTest()
Sorin Jianua8ef73d2017-11-02 16:55:17103 : callback_(base::BindOnce(&CrxDownloaderTest::DownloadComplete,
104 base::Unretained(this),
105 kExpectedContext)),
[email protected]8a5ebd432014-05-02 00:21:22106 progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress,
107 base::Unretained(this),
108 kExpectedContext)),
[email protected]1b6587dc52014-04-26 00:38:55109 crx_context_(0),
[email protected]148dcfd32014-04-29 00:54:30110 num_download_complete_calls_(0),
[email protected]8a5ebd432014-05-02 00:21:22111 num_progress_calls_(0),
Gabriel Charettedfa36042019-08-19 17:30:11112 task_environment_(base::test::TaskEnvironment::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.
Sorin Jianu4e0b41d2019-02-20 17:21:58125 crx_downloader_ = CrxDownloader::Create(
126 false, base::MakeRefCounted<NetworkFetcherChromiumFactory>(
127 test_shared_url_loader_factory_));
[email protected]8a5ebd432014-05-02 00:21:22128 crx_downloader_->set_progress_callback(progress_callback_);
tommyclieaae5d92014-09-09 06:03:47129
Antonio Gomes0807dd62018-08-10 14:28:47130 test_url_loader_factory_.SetInterceptor(base::BindLambdaForTesting(
131 [&](const network::ResourceRequest& request) { interceptor_count_++; }));
[email protected]afa378f22013-12-02 03:37:54132}
133
134void CrxDownloaderTest::TearDown() {
[email protected]8a5ebd432014-05-02 00:21:22135 crx_downloader_.reset();
[email protected]afa378f22013-12-02 03:37:54136}
137
138void CrxDownloaderTest::Quit() {
[email protected]da37c1d2013-12-19 01:04:38139 if (!quit_closure_.is_null())
Sorin Jianua8ef73d2017-11-02 16:55:17140 std::move(quit_closure_).Run();
[email protected]afa378f22013-12-02 03:37:54141}
142
[email protected]148dcfd32014-04-29 00:54:30143void CrxDownloaderTest::DownloadComplete(int crx_context,
144 const CrxDownloader::Result& result) {
145 ++num_download_complete_calls_;
[email protected]afa378f22013-12-02 03:37:54146 crx_context_ = crx_context;
[email protected]148dcfd32014-04-29 00:54:30147 download_complete_result_ = result;
[email protected]afa378f22013-12-02 03:37:54148 Quit();
149}
150
Antonio Gomes31237fb2018-08-27 19:11:03151void CrxDownloaderTest::DownloadProgress(int crx_context) {
[email protected]8a5ebd432014-05-02 00:21:22152 ++num_progress_calls_;
[email protected]8a5ebd432014-05-02 00:21:22153}
154
Antonio Gomes0807dd62018-08-10 14:28:47155void 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]afa378f22013-12-02 03:37:54175void 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
187void CrxDownloaderTest::RunThreadsUntilIdle() {
Gabriel Charettedfa36042019-08-19 17:30:11188 task_environment_.RunUntilIdle();
[email protected]afa378f22013-12-02 03:37:54189 base::RunLoop().RunUntilIdle();
190}
191
[email protected]da37c1d2013-12-19 01:04:38192// Tests that starting a download without a url results in an error.
193TEST_F(CrxDownloaderTest, NoUrl) {
194 std::vector<GURL> urls;
Sorin Jianua8ef73d2017-11-02 16:55:17195 crx_downloader_->StartDownload(urls, std::string("abcd"),
196 std::move(callback_));
[email protected]da37c1d2013-12-19 01:04:38197 RunThreadsUntilIdle();
[email protected]da37c1d2013-12-19 01:04:38198
[email protected]148dcfd32014-04-29 00:54:30199 EXPECT_EQ(1, num_download_complete_calls_);
200 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41201 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_URL),
202 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10203 EXPECT_TRUE(download_complete_result_.response.empty());
sorin74e70672016-02-03 03:13:10204 EXPECT_EQ(0, num_progress_calls_);
205}
206
207// Tests that starting a download without providing a hash results in an error.
208TEST_F(CrxDownloaderTest, NoHash) {
209 std::vector<GURL> urls(1, GURL("http://somehost/somefile"));
210
Sorin Jianua8ef73d2017-11-02 16:55:17211 crx_downloader_->StartDownload(urls, std::string(), std::move(callback_));
sorin74e70672016-02-03 03:13:10212 RunThreadsUntilIdle();
213
214 EXPECT_EQ(1, num_download_complete_calls_);
215 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41216 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_HASH),
217 download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30218 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]8a5ebd432014-05-02 00:21:22219 EXPECT_EQ(0, num_progress_calls_);
[email protected]da37c1d2013-12-19 01:04:38220}
221
[email protected]afa378f22013-12-02 03:37:54222// Tests that downloading from one url is successful.
223TEST_F(CrxDownloaderTest, OneUrl) {
224 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45225 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54226
[email protected]148dcfd32014-04-29 00:54:30227 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47228 AddResponse(expected_crx_url, test_file, net::OK);
[email protected]afa378f22013-12-02 03:37:54229
Sorin Jianua8ef73d2017-11-02 16:55:17230 crx_downloader_->StartDownloadFromUrl(
231 expected_crx_url, std::string(hash_jebg), std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54232 RunThreads();
[email protected]afa378f22013-12-02 03:37:54233
Antonio Gomes0807dd62018-08-10 14:28:47234 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30235
236 EXPECT_EQ(1, num_download_complete_calls_);
237 EXPECT_EQ(kExpectedContext, crx_context_);
238 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30239 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
240
Sorin Jianubbd51392017-07-20 01:48:31241 EXPECT_TRUE(
242 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22243
244 EXPECT_LE(1, num_progress_calls_);
[email protected]afa378f22013-12-02 03:37:54245}
246
sorin74e70672016-02-03 03:13:10247// Tests that downloading from one url fails if the actual hash of the file
248// does not match the expected hash.
249TEST_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 Gomes0807dd62018-08-10 14:28:47254 AddResponse(expected_crx_url, test_file, net::OK);
sorin74e70672016-02-03 03:13:10255
256 crx_downloader_->StartDownloadFromUrl(
257 expected_crx_url,
258 std::string(
259 "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"),
Sorin Jianua8ef73d2017-11-02 16:55:17260 std::move(callback_));
sorin74e70672016-02-03 03:13:10261 RunThreads();
262
Antonio Gomes0807dd62018-08-10 14:28:47263 EXPECT_EQ(1, GetInterceptorCount());
sorin74e70672016-02-03 03:13:10264
265 EXPECT_EQ(1, num_download_complete_calls_);
266 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41267 EXPECT_EQ(static_cast<int>(CrxDownloaderError::BAD_HASH),
268 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10269 EXPECT_TRUE(download_complete_result_.response.empty());
270
271 EXPECT_LE(1, num_progress_calls_);
sorin74e70672016-02-03 03:13:10272}
273
274// Tests that specifying two urls has no side effects. Expect a successful
[email protected]afa378f22013-12-02 03:37:54275// download, and only one download request be made.
Sorin Jianuc35bacf2018-01-31 18:25:28276TEST_F(CrxDownloaderTest, TwoUrls) {
[email protected]afa378f22013-12-02 03:37:54277 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45278 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54279
[email protected]148dcfd32014-04-29 00:54:30280 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47281 AddResponse(expected_crx_url, test_file, net::OK);
[email protected]afa378f22013-12-02 03:37:54282
283 std::vector<GURL> urls;
284 urls.push_back(expected_crx_url);
285 urls.push_back(expected_crx_url);
286
Sorin Jianua8ef73d2017-11-02 16:55:17287 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
288 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54289 RunThreads();
[email protected]afa378f22013-12-02 03:37:54290
Antonio Gomes0807dd62018-08-10 14:28:47291 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30292
293 EXPECT_EQ(1, num_download_complete_calls_);
294 EXPECT_EQ(kExpectedContext, crx_context_);
295 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30296 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
297
Sorin Jianubbd51392017-07-20 01:48:31298 EXPECT_TRUE(
299 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22300
301 EXPECT_LE(1, num_progress_calls_);
[email protected]afa378f22013-12-02 03:37:54302}
303
[email protected]afa378f22013-12-02 03:37:54304// Tests that the fallback to a valid url is successful.
Sorin Jianuc35bacf2018-01-31 18:25:28305TEST_F(CrxDownloaderTest, TwoUrls_FirstInvalid) {
[email protected]afa378f22013-12-02 03:37:54306 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45307 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
Sorin Jianuc35bacf2018-01-31 18:25:28308 const GURL no_file_url =
309 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx");
[email protected]afa378f22013-12-02 03:37:54310
[email protected]148dcfd32014-04-29 00:54:30311 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47312 AddResponse(expected_crx_url, test_file, net::OK);
313 AddResponse(no_file_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54314
315 std::vector<GURL> urls;
Sorin Jianuc35bacf2018-01-31 18:25:28316 urls.push_back(no_file_url);
[email protected]afa378f22013-12-02 03:37:54317 urls.push_back(expected_crx_url);
318
Sorin Jianua8ef73d2017-11-02 16:55:17319 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
320 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54321 RunThreads();
[email protected]afa378f22013-12-02 03:37:54322
Antonio Gomes0807dd62018-08-10 14:28:47323 EXPECT_EQ(2, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30324
325 EXPECT_EQ(1, num_download_complete_calls_);
326 EXPECT_EQ(kExpectedContext, crx_context_);
327 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30328 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
329
Sorin Jianubbd51392017-07-20 01:48:31330 EXPECT_TRUE(
331 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22332
Antonio Gomes0807dd62018-08-10 14:28:47333 // Expect at least some progress reported by the loader.
[email protected]8a5ebd432014-05-02 00:21:22334 EXPECT_LE(1, num_progress_calls_);
Sorin Jianuc35bacf2018-01-31 18:25:28335
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]afa378f22013-12-02 03:37:54346}
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.
350TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
351 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45352 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
Sorin Jianuc35bacf2018-01-31 18:25:28353 const GURL no_file_url =
354 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx");
[email protected]afa378f22013-12-02 03:37:54355
[email protected]148dcfd32014-04-29 00:54:30356 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47357 AddResponse(expected_crx_url, test_file, net::OK);
358 AddResponse(no_file_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54359
360 std::vector<GURL> urls;
361 urls.push_back(expected_crx_url);
Sorin Jianuc35bacf2018-01-31 18:25:28362 urls.push_back(no_file_url);
[email protected]afa378f22013-12-02 03:37:54363
Sorin Jianua8ef73d2017-11-02 16:55:17364 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
365 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54366 RunThreads();
[email protected]afa378f22013-12-02 03:37:54367
Antonio Gomes0807dd62018-08-10 14:28:47368 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30369
370 EXPECT_EQ(1, num_download_complete_calls_);
371 EXPECT_EQ(kExpectedContext, crx_context_);
372 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30373 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
374
Sorin Jianubbd51392017-07-20 01:48:31375 EXPECT_TRUE(
376 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22377
378 EXPECT_LE(1, num_progress_calls_);
Sorin Jianuc35bacf2018-01-31 18:25:28379
380 EXPECT_EQ(1u, crx_downloader_->download_metrics().size());
[email protected]afa378f22013-12-02 03:37:54381}
382
Sorin Jianuc35bacf2018-01-31 18:25:28383// Tests that the download fails if both urls don't serve content.
[email protected]afa378f22013-12-02 03:37:54384TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
385 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45386 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54387
Antonio Gomes0807dd62018-08-10 14:28:47388 AddResponse(expected_crx_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54389
390 std::vector<GURL> urls;
Sorin Jianuc35bacf2018-01-31 18:25:28391 urls.push_back(expected_crx_url);
392 urls.push_back(expected_crx_url);
[email protected]afa378f22013-12-02 03:37:54393
Sorin Jianua8ef73d2017-11-02 16:55:17394 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
395 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54396 RunThreads();
[email protected]afa378f22013-12-02 03:37:54397
Antonio Gomes0807dd62018-08-10 14:28:47398 EXPECT_EQ(2, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30399
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 Jianuc35bacf2018-01-31 18:25:28404
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]afa378f22013-12-02 03:37:54415}
416
sorin52ac0882015-01-24 01:15:00417} // namespace update_client