blob: 865c968f907440ab2b3a7a6b2212b3012d419463 [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"
Gabriel Charettec7108742019-08-23 03:31:4017#include "base/test/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[] =
Joshua Pawlickiafaa2922019-09-03 18:50:2337 "7ab32f071cd9b5ef8e0d7913be161f532d98b3e9fa284a7cd8059c3409ce0498";
sorin74e70672016-02-03 03:13:1038
[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)),
Ken Rockot942a6382019-12-18 21:31:41106 progress_callback_(
107 base::BindRepeating(&CrxDownloaderTest::DownloadProgress,
108 base::Unretained(this),
109 kExpectedContext)),
[email protected]1b6587dc52014-04-26 00:38:55110 crx_context_(0),
[email protected]148dcfd32014-04-29 00:54:30111 num_download_complete_calls_(0),
[email protected]8a5ebd432014-05-02 00:21:22112 num_progress_calls_(0),
Gabriel Charettedfa36042019-08-19 17:30:11113 task_environment_(base::test::TaskEnvironment::MainThreadType::IO),
Antonio Gomes0807dd62018-08-10 14:28:47114 test_shared_url_loader_factory_(
115 base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
116 &test_url_loader_factory_)) {}
[email protected]afa378f22013-12-02 03:37:54117
Sorin Jianu30881152020-03-16 14:31:19118CrxDownloaderTest::~CrxDownloaderTest() = default;
[email protected]afa378f22013-12-02 03:37:54119
120void CrxDownloaderTest::SetUp() {
[email protected]148dcfd32014-04-29 00:54:30121 num_download_complete_calls_ = 0;
122 download_complete_result_ = CrxDownloader::Result();
[email protected]8a5ebd432014-05-02 00:21:22123 num_progress_calls_ = 0;
tommyclieaae5d92014-09-09 06:03:47124
sorin9797aba2015-04-17 17:15:03125 // Do not use the background downloader in these tests.
Sorin Jianu4e0b41d2019-02-20 17:21:58126 crx_downloader_ = CrxDownloader::Create(
127 false, base::MakeRefCounted<NetworkFetcherChromiumFactory>(
Joshua Pawlickiaa7fe752019-11-19 14:21:27128 test_shared_url_loader_factory_,
129 base::BindRepeating([](const GURL& url) { return false; })));
[email protected]8a5ebd432014-05-02 00:21:22130 crx_downloader_->set_progress_callback(progress_callback_);
tommyclieaae5d92014-09-09 06:03:47131
Antonio Gomes0807dd62018-08-10 14:28:47132 test_url_loader_factory_.SetInterceptor(base::BindLambdaForTesting(
133 [&](const network::ResourceRequest& request) { interceptor_count_++; }));
[email protected]afa378f22013-12-02 03:37:54134}
135
136void CrxDownloaderTest::TearDown() {
[email protected]8a5ebd432014-05-02 00:21:22137 crx_downloader_.reset();
[email protected]afa378f22013-12-02 03:37:54138}
139
140void CrxDownloaderTest::Quit() {
[email protected]da37c1d2013-12-19 01:04:38141 if (!quit_closure_.is_null())
Sorin Jianua8ef73d2017-11-02 16:55:17142 std::move(quit_closure_).Run();
[email protected]afa378f22013-12-02 03:37:54143}
144
[email protected]148dcfd32014-04-29 00:54:30145void CrxDownloaderTest::DownloadComplete(int crx_context,
146 const CrxDownloader::Result& result) {
147 ++num_download_complete_calls_;
[email protected]afa378f22013-12-02 03:37:54148 crx_context_ = crx_context;
[email protected]148dcfd32014-04-29 00:54:30149 download_complete_result_ = result;
[email protected]afa378f22013-12-02 03:37:54150 Quit();
151}
152
Antonio Gomes31237fb2018-08-27 19:11:03153void CrxDownloaderTest::DownloadProgress(int crx_context) {
[email protected]8a5ebd432014-05-02 00:21:22154 ++num_progress_calls_;
[email protected]8a5ebd432014-05-02 00:21:22155}
156
Antonio Gomes0807dd62018-08-10 14:28:47157void CrxDownloaderTest::AddResponse(const GURL& url,
158 const base::FilePath& file_path,
159 int net_error) {
160 if (net_error == net::OK) {
161 std::string data;
162 EXPECT_TRUE(base::ReadFileToString(file_path, &data));
Lucas Furukawa Gadani06902602019-09-27 20:44:27163 auto head = network::mojom::URLResponseHead::New();
164 head->content_length = data.size();
Antonio Gomes0807dd62018-08-10 14:28:47165 network::URLLoaderCompletionStatus status(net_error);
166 status.decoded_body_length = data.size();
Lucas Furukawa Gadani06902602019-09-27 20:44:27167 test_url_loader_factory_.AddResponse(url, std::move(head), data, status);
Antonio Gomes0807dd62018-08-10 14:28:47168 return;
169 }
170
171 EXPECT_NE(net_error, net::OK);
172 test_url_loader_factory_.AddResponse(
Lucas Furukawa Gadani06902602019-09-27 20:44:27173 url, network::mojom::URLResponseHead::New(), std::string(),
Antonio Gomes0807dd62018-08-10 14:28:47174 network::URLLoaderCompletionStatus(net_error));
175}
176
[email protected]afa378f22013-12-02 03:37:54177void CrxDownloaderTest::RunThreads() {
178 base::RunLoop runloop;
179 quit_closure_ = runloop.QuitClosure();
180 runloop.Run();
181
182 // Since some tests need to drain currently enqueued tasks such as network
183 // intercepts on the IO thread, run the threads until they are
184 // idle. The component updater service won't loop again until the loop count
185 // is set and the service is started.
186 RunThreadsUntilIdle();
187}
188
189void CrxDownloaderTest::RunThreadsUntilIdle() {
Gabriel Charettedfa36042019-08-19 17:30:11190 task_environment_.RunUntilIdle();
[email protected]afa378f22013-12-02 03:37:54191 base::RunLoop().RunUntilIdle();
192}
193
[email protected]da37c1d2013-12-19 01:04:38194// Tests that starting a download without a url results in an error.
195TEST_F(CrxDownloaderTest, NoUrl) {
196 std::vector<GURL> urls;
Sorin Jianua8ef73d2017-11-02 16:55:17197 crx_downloader_->StartDownload(urls, std::string("abcd"),
198 std::move(callback_));
[email protected]da37c1d2013-12-19 01:04:38199 RunThreadsUntilIdle();
[email protected]da37c1d2013-12-19 01:04:38200
[email protected]148dcfd32014-04-29 00:54:30201 EXPECT_EQ(1, num_download_complete_calls_);
202 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41203 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_URL),
204 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10205 EXPECT_TRUE(download_complete_result_.response.empty());
sorin74e70672016-02-03 03:13:10206 EXPECT_EQ(0, num_progress_calls_);
207}
208
209// Tests that starting a download without providing a hash results in an error.
210TEST_F(CrxDownloaderTest, NoHash) {
211 std::vector<GURL> urls(1, GURL("http://somehost/somefile"));
212
Sorin Jianua8ef73d2017-11-02 16:55:17213 crx_downloader_->StartDownload(urls, std::string(), std::move(callback_));
sorin74e70672016-02-03 03:13:10214 RunThreadsUntilIdle();
215
216 EXPECT_EQ(1, num_download_complete_calls_);
217 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41218 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_HASH),
219 download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30220 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]8a5ebd432014-05-02 00:21:22221 EXPECT_EQ(0, num_progress_calls_);
[email protected]da37c1d2013-12-19 01:04:38222}
223
[email protected]afa378f22013-12-02 03:37:54224// Tests that downloading from one url is successful.
225TEST_F(CrxDownloaderTest, OneUrl) {
226 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45227 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54228
[email protected]148dcfd32014-04-29 00:54:30229 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47230 AddResponse(expected_crx_url, test_file, net::OK);
[email protected]afa378f22013-12-02 03:37:54231
Sorin Jianua8ef73d2017-11-02 16:55:17232 crx_downloader_->StartDownloadFromUrl(
233 expected_crx_url, std::string(hash_jebg), std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54234 RunThreads();
[email protected]afa378f22013-12-02 03:37:54235
Antonio Gomes0807dd62018-08-10 14:28:47236 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30237
238 EXPECT_EQ(1, num_download_complete_calls_);
239 EXPECT_EQ(kExpectedContext, crx_context_);
240 EXPECT_EQ(0, download_complete_result_.error);
241 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
242
Sorin Jianubbd51392017-07-20 01:48:31243 EXPECT_TRUE(
244 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22245
246 EXPECT_LE(1, num_progress_calls_);
[email protected]afa378f22013-12-02 03:37:54247}
248
sorin74e70672016-02-03 03:13:10249// Tests that downloading from one url fails if the actual hash of the file
250// does not match the expected hash.
251TEST_F(CrxDownloaderTest, OneUrlBadHash) {
252 const GURL expected_crx_url =
253 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
254
255 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47256 AddResponse(expected_crx_url, test_file, net::OK);
sorin74e70672016-02-03 03:13:10257
258 crx_downloader_->StartDownloadFromUrl(
259 expected_crx_url,
260 std::string(
261 "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"),
Sorin Jianua8ef73d2017-11-02 16:55:17262 std::move(callback_));
sorin74e70672016-02-03 03:13:10263 RunThreads();
264
Antonio Gomes0807dd62018-08-10 14:28:47265 EXPECT_EQ(1, GetInterceptorCount());
sorin74e70672016-02-03 03:13:10266
267 EXPECT_EQ(1, num_download_complete_calls_);
268 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41269 EXPECT_EQ(static_cast<int>(CrxDownloaderError::BAD_HASH),
270 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10271 EXPECT_TRUE(download_complete_result_.response.empty());
272
273 EXPECT_LE(1, num_progress_calls_);
sorin74e70672016-02-03 03:13:10274}
275
276// Tests that specifying two urls has no side effects. Expect a successful
[email protected]afa378f22013-12-02 03:37:54277// download, and only one download request be made.
Sorin Jianuc35bacf2018-01-31 18:25:28278TEST_F(CrxDownloaderTest, TwoUrls) {
[email protected]afa378f22013-12-02 03:37:54279 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45280 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54281
[email protected]148dcfd32014-04-29 00:54:30282 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47283 AddResponse(expected_crx_url, test_file, net::OK);
[email protected]afa378f22013-12-02 03:37:54284
285 std::vector<GURL> urls;
286 urls.push_back(expected_crx_url);
287 urls.push_back(expected_crx_url);
288
Sorin Jianua8ef73d2017-11-02 16:55:17289 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
290 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54291 RunThreads();
[email protected]afa378f22013-12-02 03:37:54292
Antonio Gomes0807dd62018-08-10 14:28:47293 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30294
295 EXPECT_EQ(1, num_download_complete_calls_);
296 EXPECT_EQ(kExpectedContext, crx_context_);
297 EXPECT_EQ(0, download_complete_result_.error);
298 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
299
Sorin Jianubbd51392017-07-20 01:48:31300 EXPECT_TRUE(
301 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22302
303 EXPECT_LE(1, num_progress_calls_);
[email protected]afa378f22013-12-02 03:37:54304}
305
[email protected]afa378f22013-12-02 03:37:54306// Tests that the fallback to a valid url is successful.
Sorin Jianuc35bacf2018-01-31 18:25:28307TEST_F(CrxDownloaderTest, TwoUrls_FirstInvalid) {
[email protected]afa378f22013-12-02 03:37:54308 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45309 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
Sorin Jianuc35bacf2018-01-31 18:25:28310 const GURL no_file_url =
311 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx");
[email protected]afa378f22013-12-02 03:37:54312
[email protected]148dcfd32014-04-29 00:54:30313 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47314 AddResponse(expected_crx_url, test_file, net::OK);
315 AddResponse(no_file_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54316
317 std::vector<GURL> urls;
Sorin Jianuc35bacf2018-01-31 18:25:28318 urls.push_back(no_file_url);
[email protected]afa378f22013-12-02 03:37:54319 urls.push_back(expected_crx_url);
320
Sorin Jianua8ef73d2017-11-02 16:55:17321 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
322 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54323 RunThreads();
[email protected]afa378f22013-12-02 03:37:54324
Antonio Gomes0807dd62018-08-10 14:28:47325 EXPECT_EQ(2, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30326
327 EXPECT_EQ(1, num_download_complete_calls_);
328 EXPECT_EQ(kExpectedContext, crx_context_);
329 EXPECT_EQ(0, download_complete_result_.error);
330 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
331
Sorin Jianubbd51392017-07-20 01:48:31332 EXPECT_TRUE(
333 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22334
Antonio Gomes0807dd62018-08-10 14:28:47335 // Expect at least some progress reported by the loader.
[email protected]8a5ebd432014-05-02 00:21:22336 EXPECT_LE(1, num_progress_calls_);
Sorin Jianuc35bacf2018-01-31 18:25:28337
338 const auto download_metrics = crx_downloader_->download_metrics();
339 ASSERT_EQ(2u, download_metrics.size());
340 EXPECT_EQ(no_file_url, download_metrics[0].url);
341 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[0].error);
342 EXPECT_EQ(-1, download_metrics[0].downloaded_bytes);
343 EXPECT_EQ(-1, download_metrics[0].total_bytes);
344 EXPECT_EQ(expected_crx_url, download_metrics[1].url);
345 EXPECT_EQ(0, download_metrics[1].error);
Joshua Pawlickiafaa2922019-09-03 18:50:23346 EXPECT_EQ(1015, download_metrics[1].downloaded_bytes);
347 EXPECT_EQ(1015, download_metrics[1].total_bytes);
[email protected]afa378f22013-12-02 03:37:54348}
349
350// Tests that the download succeeds if the first url is correct and the
351// second bad url does not have a side-effect.
352TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
353 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45354 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
Sorin Jianuc35bacf2018-01-31 18:25:28355 const GURL no_file_url =
356 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx");
[email protected]afa378f22013-12-02 03:37:54357
[email protected]148dcfd32014-04-29 00:54:30358 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47359 AddResponse(expected_crx_url, test_file, net::OK);
360 AddResponse(no_file_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54361
362 std::vector<GURL> urls;
363 urls.push_back(expected_crx_url);
Sorin Jianuc35bacf2018-01-31 18:25:28364 urls.push_back(no_file_url);
[email protected]afa378f22013-12-02 03:37:54365
Sorin Jianua8ef73d2017-11-02 16:55:17366 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
367 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54368 RunThreads();
[email protected]afa378f22013-12-02 03:37:54369
Antonio Gomes0807dd62018-08-10 14:28:47370 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30371
372 EXPECT_EQ(1, num_download_complete_calls_);
373 EXPECT_EQ(kExpectedContext, crx_context_);
374 EXPECT_EQ(0, download_complete_result_.error);
375 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
376
Sorin Jianubbd51392017-07-20 01:48:31377 EXPECT_TRUE(
378 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22379
380 EXPECT_LE(1, num_progress_calls_);
Sorin Jianuc35bacf2018-01-31 18:25:28381
382 EXPECT_EQ(1u, crx_downloader_->download_metrics().size());
[email protected]afa378f22013-12-02 03:37:54383}
384
Sorin Jianuc35bacf2018-01-31 18:25:28385// Tests that the download fails if both urls don't serve content.
[email protected]afa378f22013-12-02 03:37:54386TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
387 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45388 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54389
Antonio Gomes0807dd62018-08-10 14:28:47390 AddResponse(expected_crx_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54391
392 std::vector<GURL> urls;
Sorin Jianuc35bacf2018-01-31 18:25:28393 urls.push_back(expected_crx_url);
394 urls.push_back(expected_crx_url);
[email protected]afa378f22013-12-02 03:37:54395
Sorin Jianua8ef73d2017-11-02 16:55:17396 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
397 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54398 RunThreads();
[email protected]afa378f22013-12-02 03:37:54399
Antonio Gomes0807dd62018-08-10 14:28:47400 EXPECT_EQ(2, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30401
402 EXPECT_EQ(1, num_download_complete_calls_);
403 EXPECT_EQ(kExpectedContext, crx_context_);
404 EXPECT_NE(0, download_complete_result_.error);
405 EXPECT_TRUE(download_complete_result_.response.empty());
Sorin Jianuc35bacf2018-01-31 18:25:28406
407 const auto download_metrics = crx_downloader_->download_metrics();
408 ASSERT_EQ(2u, download_metrics.size());
409 EXPECT_EQ(expected_crx_url, download_metrics[0].url);
410 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[0].error);
411 EXPECT_EQ(-1, download_metrics[0].downloaded_bytes);
412 EXPECT_EQ(-1, download_metrics[0].total_bytes);
413 EXPECT_EQ(expected_crx_url, download_metrics[1].url);
414 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[1].error);
415 EXPECT_EQ(-1, download_metrics[1].downloaded_bytes);
416 EXPECT_EQ(-1, download_metrics[1].total_bytes);
[email protected]afa378f22013-12-02 03:37:54417}
418
sorin52ac0882015-01-24 01:15:00419} // namespace update_client