blob: 03c90026294e6207de165f1ba57ebd15b6067898 [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:
Sorin Jianu72d8fe0d2017-07-11 18:42:1694 base::test::ScopedTaskEnvironment scoped_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),
Sorin Jianu72d8fe0d2017-07-11 18:42:16112 scoped_task_environment_(
113 base::test::ScopedTaskEnvironment::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
Antonio Gomes0807dd62018-08-10 14:28:47118CrxDownloaderTest::~CrxDownloaderTest() {}
[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>(
128 test_shared_url_loader_factory_));
[email protected]8a5ebd432014-05-02 00:21:22129 crx_downloader_->set_progress_callback(progress_callback_);
tommyclieaae5d92014-09-09 06:03:47130
Antonio Gomes0807dd62018-08-10 14:28:47131 test_url_loader_factory_.SetInterceptor(base::BindLambdaForTesting(
132 [&](const network::ResourceRequest& request) { interceptor_count_++; }));
[email protected]afa378f22013-12-02 03:37:54133}
134
135void CrxDownloaderTest::TearDown() {
[email protected]8a5ebd432014-05-02 00:21:22136 crx_downloader_.reset();
[email protected]afa378f22013-12-02 03:37:54137}
138
139void CrxDownloaderTest::Quit() {
[email protected]da37c1d2013-12-19 01:04:38140 if (!quit_closure_.is_null())
Sorin Jianua8ef73d2017-11-02 16:55:17141 std::move(quit_closure_).Run();
[email protected]afa378f22013-12-02 03:37:54142}
143
[email protected]148dcfd32014-04-29 00:54:30144void CrxDownloaderTest::DownloadComplete(int crx_context,
145 const CrxDownloader::Result& result) {
146 ++num_download_complete_calls_;
[email protected]afa378f22013-12-02 03:37:54147 crx_context_ = crx_context;
[email protected]148dcfd32014-04-29 00:54:30148 download_complete_result_ = result;
[email protected]afa378f22013-12-02 03:37:54149 Quit();
150}
151
Antonio Gomes31237fb2018-08-27 19:11:03152void CrxDownloaderTest::DownloadProgress(int crx_context) {
[email protected]8a5ebd432014-05-02 00:21:22153 ++num_progress_calls_;
[email protected]8a5ebd432014-05-02 00:21:22154}
155
Antonio Gomes0807dd62018-08-10 14:28:47156void CrxDownloaderTest::AddResponse(const GURL& url,
157 const base::FilePath& file_path,
158 int net_error) {
159 if (net_error == net::OK) {
160 std::string data;
161 EXPECT_TRUE(base::ReadFileToString(file_path, &data));
162 network::ResourceResponseHead head;
163 head.content_length = data.size();
164 network::URLLoaderCompletionStatus status(net_error);
165 status.decoded_body_length = data.size();
166 test_url_loader_factory_.AddResponse(url, head, data, status);
167 return;
168 }
169
170 EXPECT_NE(net_error, net::OK);
171 test_url_loader_factory_.AddResponse(
172 url, network::ResourceResponseHead(), std::string(),
173 network::URLLoaderCompletionStatus(net_error));
174}
175
[email protected]afa378f22013-12-02 03:37:54176void CrxDownloaderTest::RunThreads() {
177 base::RunLoop runloop;
178 quit_closure_ = runloop.QuitClosure();
179 runloop.Run();
180
181 // Since some tests need to drain currently enqueued tasks such as network
182 // intercepts on the IO thread, run the threads until they are
183 // idle. The component updater service won't loop again until the loop count
184 // is set and the service is started.
185 RunThreadsUntilIdle();
186}
187
188void CrxDownloaderTest::RunThreadsUntilIdle() {
Sorin Jianubbd51392017-07-20 01:48:31189 scoped_task_environment_.RunUntilIdle();
[email protected]afa378f22013-12-02 03:37:54190 base::RunLoop().RunUntilIdle();
191}
192
[email protected]da37c1d2013-12-19 01:04:38193// Tests that starting a download without a url results in an error.
194TEST_F(CrxDownloaderTest, NoUrl) {
195 std::vector<GURL> urls;
Sorin Jianua8ef73d2017-11-02 16:55:17196 crx_downloader_->StartDownload(urls, std::string("abcd"),
197 std::move(callback_));
[email protected]da37c1d2013-12-19 01:04:38198 RunThreadsUntilIdle();
[email protected]da37c1d2013-12-19 01:04:38199
[email protected]148dcfd32014-04-29 00:54:30200 EXPECT_EQ(1, num_download_complete_calls_);
201 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41202 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_URL),
203 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10204 EXPECT_TRUE(download_complete_result_.response.empty());
sorin74e70672016-02-03 03:13:10205 EXPECT_EQ(0, num_progress_calls_);
206}
207
208// Tests that starting a download without providing a hash results in an error.
209TEST_F(CrxDownloaderTest, NoHash) {
210 std::vector<GURL> urls(1, GURL("http://somehost/somefile"));
211
Sorin Jianua8ef73d2017-11-02 16:55:17212 crx_downloader_->StartDownload(urls, std::string(), std::move(callback_));
sorin74e70672016-02-03 03:13:10213 RunThreadsUntilIdle();
214
215 EXPECT_EQ(1, num_download_complete_calls_);
216 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41217 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_HASH),
218 download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30219 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]8a5ebd432014-05-02 00:21:22220 EXPECT_EQ(0, num_progress_calls_);
[email protected]da37c1d2013-12-19 01:04:38221}
222
[email protected]afa378f22013-12-02 03:37:54223// Tests that downloading from one url is successful.
224TEST_F(CrxDownloaderTest, OneUrl) {
225 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45226 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54227
[email protected]148dcfd32014-04-29 00:54:30228 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47229 AddResponse(expected_crx_url, test_file, net::OK);
[email protected]afa378f22013-12-02 03:37:54230
Sorin Jianua8ef73d2017-11-02 16:55:17231 crx_downloader_->StartDownloadFromUrl(
232 expected_crx_url, std::string(hash_jebg), std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54233 RunThreads();
[email protected]afa378f22013-12-02 03:37:54234
Antonio Gomes0807dd62018-08-10 14:28:47235 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30236
237 EXPECT_EQ(1, num_download_complete_calls_);
238 EXPECT_EQ(kExpectedContext, crx_context_);
239 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30240 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
241
Sorin Jianubbd51392017-07-20 01:48:31242 EXPECT_TRUE(
243 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22244
245 EXPECT_LE(1, num_progress_calls_);
[email protected]afa378f22013-12-02 03:37:54246}
247
sorin74e70672016-02-03 03:13:10248// Tests that downloading from one url fails if the actual hash of the file
249// does not match the expected hash.
250TEST_F(CrxDownloaderTest, OneUrlBadHash) {
251 const GURL expected_crx_url =
252 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
253
254 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47255 AddResponse(expected_crx_url, test_file, net::OK);
sorin74e70672016-02-03 03:13:10256
257 crx_downloader_->StartDownloadFromUrl(
258 expected_crx_url,
259 std::string(
260 "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"),
Sorin Jianua8ef73d2017-11-02 16:55:17261 std::move(callback_));
sorin74e70672016-02-03 03:13:10262 RunThreads();
263
Antonio Gomes0807dd62018-08-10 14:28:47264 EXPECT_EQ(1, GetInterceptorCount());
sorin74e70672016-02-03 03:13:10265
266 EXPECT_EQ(1, num_download_complete_calls_);
267 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41268 EXPECT_EQ(static_cast<int>(CrxDownloaderError::BAD_HASH),
269 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10270 EXPECT_TRUE(download_complete_result_.response.empty());
271
272 EXPECT_LE(1, num_progress_calls_);
sorin74e70672016-02-03 03:13:10273}
274
275// Tests that specifying two urls has no side effects. Expect a successful
[email protected]afa378f22013-12-02 03:37:54276// download, and only one download request be made.
Sorin Jianuc35bacf2018-01-31 18:25:28277TEST_F(CrxDownloaderTest, TwoUrls) {
[email protected]afa378f22013-12-02 03:37:54278 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45279 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54280
[email protected]148dcfd32014-04-29 00:54:30281 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47282 AddResponse(expected_crx_url, test_file, net::OK);
[email protected]afa378f22013-12-02 03:37:54283
284 std::vector<GURL> urls;
285 urls.push_back(expected_crx_url);
286 urls.push_back(expected_crx_url);
287
Sorin Jianua8ef73d2017-11-02 16:55:17288 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
289 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54290 RunThreads();
[email protected]afa378f22013-12-02 03:37:54291
Antonio Gomes0807dd62018-08-10 14:28:47292 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30293
294 EXPECT_EQ(1, num_download_complete_calls_);
295 EXPECT_EQ(kExpectedContext, crx_context_);
296 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30297 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
298
Sorin Jianubbd51392017-07-20 01:48:31299 EXPECT_TRUE(
300 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22301
302 EXPECT_LE(1, num_progress_calls_);
[email protected]afa378f22013-12-02 03:37:54303}
304
[email protected]afa378f22013-12-02 03:37:54305// Tests that the fallback to a valid url is successful.
Sorin Jianuc35bacf2018-01-31 18:25:28306TEST_F(CrxDownloaderTest, TwoUrls_FirstInvalid) {
[email protected]afa378f22013-12-02 03:37:54307 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45308 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
Sorin Jianuc35bacf2018-01-31 18:25:28309 const GURL no_file_url =
310 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx");
[email protected]afa378f22013-12-02 03:37:54311
[email protected]148dcfd32014-04-29 00:54:30312 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47313 AddResponse(expected_crx_url, test_file, net::OK);
314 AddResponse(no_file_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54315
316 std::vector<GURL> urls;
Sorin Jianuc35bacf2018-01-31 18:25:28317 urls.push_back(no_file_url);
[email protected]afa378f22013-12-02 03:37:54318 urls.push_back(expected_crx_url);
319
Sorin Jianua8ef73d2017-11-02 16:55:17320 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
321 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54322 RunThreads();
[email protected]afa378f22013-12-02 03:37:54323
Antonio Gomes0807dd62018-08-10 14:28:47324 EXPECT_EQ(2, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30325
326 EXPECT_EQ(1, num_download_complete_calls_);
327 EXPECT_EQ(kExpectedContext, crx_context_);
328 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30329 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
330
Sorin Jianubbd51392017-07-20 01:48:31331 EXPECT_TRUE(
332 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22333
Antonio Gomes0807dd62018-08-10 14:28:47334 // Expect at least some progress reported by the loader.
[email protected]8a5ebd432014-05-02 00:21:22335 EXPECT_LE(1, num_progress_calls_);
Sorin Jianuc35bacf2018-01-31 18:25:28336
337 const auto download_metrics = crx_downloader_->download_metrics();
338 ASSERT_EQ(2u, download_metrics.size());
339 EXPECT_EQ(no_file_url, download_metrics[0].url);
340 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[0].error);
341 EXPECT_EQ(-1, download_metrics[0].downloaded_bytes);
342 EXPECT_EQ(-1, download_metrics[0].total_bytes);
343 EXPECT_EQ(expected_crx_url, download_metrics[1].url);
344 EXPECT_EQ(0, download_metrics[1].error);
345 EXPECT_EQ(1843, download_metrics[1].downloaded_bytes);
346 EXPECT_EQ(1843, download_metrics[1].total_bytes);
[email protected]afa378f22013-12-02 03:37:54347}
348
349// Tests that the download succeeds if the first url is correct and the
350// second bad url does not have a side-effect.
351TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
352 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45353 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
Sorin Jianuc35bacf2018-01-31 18:25:28354 const GURL no_file_url =
355 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx");
[email protected]afa378f22013-12-02 03:37:54356
[email protected]148dcfd32014-04-29 00:54:30357 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47358 AddResponse(expected_crx_url, test_file, net::OK);
359 AddResponse(no_file_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54360
361 std::vector<GURL> urls;
362 urls.push_back(expected_crx_url);
Sorin Jianuc35bacf2018-01-31 18:25:28363 urls.push_back(no_file_url);
[email protected]afa378f22013-12-02 03:37:54364
Sorin Jianua8ef73d2017-11-02 16:55:17365 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
366 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54367 RunThreads();
[email protected]afa378f22013-12-02 03:37:54368
Antonio Gomes0807dd62018-08-10 14:28:47369 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30370
371 EXPECT_EQ(1, num_download_complete_calls_);
372 EXPECT_EQ(kExpectedContext, crx_context_);
373 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30374 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
375
Sorin Jianubbd51392017-07-20 01:48:31376 EXPECT_TRUE(
377 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22378
379 EXPECT_LE(1, num_progress_calls_);
Sorin Jianuc35bacf2018-01-31 18:25:28380
381 EXPECT_EQ(1u, crx_downloader_->download_metrics().size());
[email protected]afa378f22013-12-02 03:37:54382}
383
Sorin Jianuc35bacf2018-01-31 18:25:28384// Tests that the download fails if both urls don't serve content.
[email protected]afa378f22013-12-02 03:37:54385TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
386 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45387 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54388
Antonio Gomes0807dd62018-08-10 14:28:47389 AddResponse(expected_crx_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54390
391 std::vector<GURL> urls;
Sorin Jianuc35bacf2018-01-31 18:25:28392 urls.push_back(expected_crx_url);
393 urls.push_back(expected_crx_url);
[email protected]afa378f22013-12-02 03:37:54394
Sorin Jianua8ef73d2017-11-02 16:55:17395 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
396 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54397 RunThreads();
[email protected]afa378f22013-12-02 03:37:54398
Antonio Gomes0807dd62018-08-10 14:28:47399 EXPECT_EQ(2, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30400
401 EXPECT_EQ(1, num_download_complete_calls_);
402 EXPECT_EQ(kExpectedContext, crx_context_);
403 EXPECT_NE(0, download_complete_result_.error);
404 EXPECT_TRUE(download_complete_result_.response.empty());
Sorin Jianuc35bacf2018-01-31 18:25:28405
406 const auto download_metrics = crx_downloader_->download_metrics();
407 ASSERT_EQ(2u, download_metrics.size());
408 EXPECT_EQ(expected_crx_url, download_metrics[0].url);
409 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[0].error);
410 EXPECT_EQ(-1, download_metrics[0].downloaded_bytes);
411 EXPECT_EQ(-1, download_metrics[0].total_bytes);
412 EXPECT_EQ(expected_crx_url, download_metrics[1].url);
413 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[1].error);
414 EXPECT_EQ(-1, download_metrics[1].downloaded_bytes);
415 EXPECT_EQ(-1, download_metrics[1].total_bytes);
[email protected]afa378f22013-12-02 03:37:54416}
417
sorin52ac0882015-01-24 01:15:00418} // namespace update_client