blob: cf19532306c03d825c7e8da91e2e6ac87150b5bc [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
[email protected]8a5ebd432014-05-02 00:21:2262 void DownloadProgress(int crx_context, const CrxDownloader::Result& result);
63
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_;
85 CrxDownloader::Result download_progress_result_;
86
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;
124 download_progress_result_ = CrxDownloader::Result();
tommyclieaae5d92014-09-09 06:03:47125
sorin9797aba2015-04-17 17:15:03126 // Do not use the background downloader in these tests.
Antonio Gomes0807dd62018-08-10 14:28:47127 crx_downloader_ =
128 CrxDownloader::Create(false, 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
[email protected]8a5ebd432014-05-02 00:21:22152void CrxDownloaderTest::DownloadProgress(int crx_context,
153 const CrxDownloader::Result& result) {
154 ++num_progress_calls_;
155 download_progress_result_ = result;
156}
157
Antonio Gomes0807dd62018-08-10 14:28:47158void CrxDownloaderTest::AddResponse(const GURL& url,
159 const base::FilePath& file_path,
160 int net_error) {
161 if (net_error == net::OK) {
162 std::string data;
163 EXPECT_TRUE(base::ReadFileToString(file_path, &data));
164 network::ResourceResponseHead head;
165 head.content_length = data.size();
166 network::URLLoaderCompletionStatus status(net_error);
167 status.decoded_body_length = data.size();
168 test_url_loader_factory_.AddResponse(url, head, data, status);
169 return;
170 }
171
172 EXPECT_NE(net_error, net::OK);
173 test_url_loader_factory_.AddResponse(
174 url, network::ResourceResponseHead(), std::string(),
175 network::URLLoaderCompletionStatus(net_error));
176}
177
[email protected]afa378f22013-12-02 03:37:54178void CrxDownloaderTest::RunThreads() {
179 base::RunLoop runloop;
180 quit_closure_ = runloop.QuitClosure();
181 runloop.Run();
182
183 // Since some tests need to drain currently enqueued tasks such as network
184 // intercepts on the IO thread, run the threads until they are
185 // idle. The component updater service won't loop again until the loop count
186 // is set and the service is started.
187 RunThreadsUntilIdle();
188}
189
190void CrxDownloaderTest::RunThreadsUntilIdle() {
Sorin Jianubbd51392017-07-20 01:48:31191 scoped_task_environment_.RunUntilIdle();
[email protected]afa378f22013-12-02 03:37:54192 base::RunLoop().RunUntilIdle();
193}
194
[email protected]da37c1d2013-12-19 01:04:38195// Tests that starting a download without a url results in an error.
196TEST_F(CrxDownloaderTest, NoUrl) {
197 std::vector<GURL> urls;
Sorin Jianua8ef73d2017-11-02 16:55:17198 crx_downloader_->StartDownload(urls, std::string("abcd"),
199 std::move(callback_));
[email protected]da37c1d2013-12-19 01:04:38200 RunThreadsUntilIdle();
[email protected]da37c1d2013-12-19 01:04:38201
[email protected]148dcfd32014-04-29 00:54:30202 EXPECT_EQ(1, num_download_complete_calls_);
203 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41204 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_URL),
205 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10206 EXPECT_TRUE(download_complete_result_.response.empty());
207 EXPECT_EQ(-1, download_complete_result_.downloaded_bytes);
208 EXPECT_EQ(-1, download_complete_result_.total_bytes);
209 EXPECT_EQ(0, num_progress_calls_);
210}
211
212// Tests that starting a download without providing a hash results in an error.
213TEST_F(CrxDownloaderTest, NoHash) {
214 std::vector<GURL> urls(1, GURL("http://somehost/somefile"));
215
Sorin Jianua8ef73d2017-11-02 16:55:17216 crx_downloader_->StartDownload(urls, std::string(), std::move(callback_));
sorin74e70672016-02-03 03:13:10217 RunThreadsUntilIdle();
218
219 EXPECT_EQ(1, num_download_complete_calls_);
220 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41221 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_HASH),
222 download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30223 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]8a5ebd432014-05-02 00:21:22224 EXPECT_EQ(-1, download_complete_result_.downloaded_bytes);
225 EXPECT_EQ(-1, download_complete_result_.total_bytes);
226 EXPECT_EQ(0, num_progress_calls_);
[email protected]da37c1d2013-12-19 01:04:38227}
228
[email protected]afa378f22013-12-02 03:37:54229// Tests that downloading from one url is successful.
230TEST_F(CrxDownloaderTest, OneUrl) {
231 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45232 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54233
[email protected]148dcfd32014-04-29 00:54:30234 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47235 AddResponse(expected_crx_url, test_file, net::OK);
[email protected]afa378f22013-12-02 03:37:54236
Sorin Jianua8ef73d2017-11-02 16:55:17237 crx_downloader_->StartDownloadFromUrl(
238 expected_crx_url, std::string(hash_jebg), std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54239 RunThreads();
[email protected]afa378f22013-12-02 03:37:54240
Antonio Gomes0807dd62018-08-10 14:28:47241 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30242
243 EXPECT_EQ(1, num_download_complete_calls_);
244 EXPECT_EQ(kExpectedContext, crx_context_);
245 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30246 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
247
Sorin Jianubbd51392017-07-20 01:48:31248 EXPECT_TRUE(
249 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22250
251 EXPECT_LE(1, num_progress_calls_);
[email protected]afa378f22013-12-02 03:37:54252}
253
sorin74e70672016-02-03 03:13:10254// Tests that downloading from one url fails if the actual hash of the file
255// does not match the expected hash.
256TEST_F(CrxDownloaderTest, OneUrlBadHash) {
257 const GURL expected_crx_url =
258 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
259
260 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47261 AddResponse(expected_crx_url, test_file, net::OK);
sorin74e70672016-02-03 03:13:10262
263 crx_downloader_->StartDownloadFromUrl(
264 expected_crx_url,
265 std::string(
266 "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"),
Sorin Jianua8ef73d2017-11-02 16:55:17267 std::move(callback_));
sorin74e70672016-02-03 03:13:10268 RunThreads();
269
Antonio Gomes0807dd62018-08-10 14:28:47270 EXPECT_EQ(1, GetInterceptorCount());
sorin74e70672016-02-03 03:13:10271
272 EXPECT_EQ(1, num_download_complete_calls_);
273 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41274 EXPECT_EQ(static_cast<int>(CrxDownloaderError::BAD_HASH),
275 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10276 EXPECT_TRUE(download_complete_result_.response.empty());
277
278 EXPECT_LE(1, num_progress_calls_);
sorin74e70672016-02-03 03:13:10279}
280
281// Tests that specifying two urls has no side effects. Expect a successful
[email protected]afa378f22013-12-02 03:37:54282// download, and only one download request be made.
Sorin Jianuc35bacf2018-01-31 18:25:28283TEST_F(CrxDownloaderTest, TwoUrls) {
[email protected]afa378f22013-12-02 03:37:54284 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45285 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54286
[email protected]148dcfd32014-04-29 00:54:30287 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47288 AddResponse(expected_crx_url, test_file, net::OK);
[email protected]afa378f22013-12-02 03:37:54289
290 std::vector<GURL> urls;
291 urls.push_back(expected_crx_url);
292 urls.push_back(expected_crx_url);
293
Sorin Jianua8ef73d2017-11-02 16:55:17294 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
295 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54296 RunThreads();
[email protected]afa378f22013-12-02 03:37:54297
Antonio Gomes0807dd62018-08-10 14:28:47298 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30299
300 EXPECT_EQ(1, num_download_complete_calls_);
301 EXPECT_EQ(kExpectedContext, crx_context_);
302 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30303 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
304
Sorin Jianubbd51392017-07-20 01:48:31305 EXPECT_TRUE(
306 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22307
308 EXPECT_LE(1, num_progress_calls_);
[email protected]afa378f22013-12-02 03:37:54309}
310
[email protected]afa378f22013-12-02 03:37:54311// Tests that the fallback to a valid url is successful.
Sorin Jianuc35bacf2018-01-31 18:25:28312TEST_F(CrxDownloaderTest, TwoUrls_FirstInvalid) {
[email protected]afa378f22013-12-02 03:37:54313 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45314 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
Sorin Jianuc35bacf2018-01-31 18:25:28315 const GURL no_file_url =
316 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx");
[email protected]afa378f22013-12-02 03:37:54317
[email protected]148dcfd32014-04-29 00:54:30318 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47319 AddResponse(expected_crx_url, test_file, net::OK);
320 AddResponse(no_file_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54321
322 std::vector<GURL> urls;
Sorin Jianuc35bacf2018-01-31 18:25:28323 urls.push_back(no_file_url);
[email protected]afa378f22013-12-02 03:37:54324 urls.push_back(expected_crx_url);
325
Sorin Jianua8ef73d2017-11-02 16:55:17326 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
327 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54328 RunThreads();
[email protected]afa378f22013-12-02 03:37:54329
Antonio Gomes0807dd62018-08-10 14:28:47330 EXPECT_EQ(2, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30331
332 EXPECT_EQ(1, num_download_complete_calls_);
333 EXPECT_EQ(kExpectedContext, crx_context_);
334 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30335 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
336
Sorin Jianubbd51392017-07-20 01:48:31337 EXPECT_TRUE(
338 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22339
Antonio Gomes0807dd62018-08-10 14:28:47340 // Expect at least some progress reported by the loader.
[email protected]8a5ebd432014-05-02 00:21:22341 EXPECT_LE(1, num_progress_calls_);
Sorin Jianuc35bacf2018-01-31 18:25:28342
343 const auto download_metrics = crx_downloader_->download_metrics();
344 ASSERT_EQ(2u, download_metrics.size());
345 EXPECT_EQ(no_file_url, download_metrics[0].url);
346 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[0].error);
347 EXPECT_EQ(-1, download_metrics[0].downloaded_bytes);
348 EXPECT_EQ(-1, download_metrics[0].total_bytes);
349 EXPECT_EQ(expected_crx_url, download_metrics[1].url);
350 EXPECT_EQ(0, download_metrics[1].error);
351 EXPECT_EQ(1843, download_metrics[1].downloaded_bytes);
352 EXPECT_EQ(1843, download_metrics[1].total_bytes);
[email protected]afa378f22013-12-02 03:37:54353}
354
355// Tests that the download succeeds if the first url is correct and the
356// second bad url does not have a side-effect.
357TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
358 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45359 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
Sorin Jianuc35bacf2018-01-31 18:25:28360 const GURL no_file_url =
361 GURL("http://localhost/download/ihfokbkgjpifnbbojhneepfflplebdkc.crx");
[email protected]afa378f22013-12-02 03:37:54362
[email protected]148dcfd32014-04-29 00:54:30363 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
Antonio Gomes0807dd62018-08-10 14:28:47364 AddResponse(expected_crx_url, test_file, net::OK);
365 AddResponse(no_file_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54366
367 std::vector<GURL> urls;
368 urls.push_back(expected_crx_url);
Sorin Jianuc35bacf2018-01-31 18:25:28369 urls.push_back(no_file_url);
[email protected]afa378f22013-12-02 03:37:54370
Sorin Jianua8ef73d2017-11-02 16:55:17371 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
372 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54373 RunThreads();
[email protected]afa378f22013-12-02 03:37:54374
Antonio Gomes0807dd62018-08-10 14:28:47375 EXPECT_EQ(1, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30376
377 EXPECT_EQ(1, num_download_complete_calls_);
378 EXPECT_EQ(kExpectedContext, crx_context_);
379 EXPECT_EQ(0, download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30380 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
381
Sorin Jianubbd51392017-07-20 01:48:31382 EXPECT_TRUE(
383 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22384
385 EXPECT_LE(1, num_progress_calls_);
Sorin Jianuc35bacf2018-01-31 18:25:28386
387 EXPECT_EQ(1u, crx_downloader_->download_metrics().size());
[email protected]afa378f22013-12-02 03:37:54388}
389
Sorin Jianuc35bacf2018-01-31 18:25:28390// Tests that the download fails if both urls don't serve content.
[email protected]afa378f22013-12-02 03:37:54391TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
392 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45393 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54394
Antonio Gomes0807dd62018-08-10 14:28:47395 AddResponse(expected_crx_url, base::FilePath(), net::ERR_FILE_NOT_FOUND);
[email protected]afa378f22013-12-02 03:37:54396
397 std::vector<GURL> urls;
Sorin Jianuc35bacf2018-01-31 18:25:28398 urls.push_back(expected_crx_url);
399 urls.push_back(expected_crx_url);
[email protected]afa378f22013-12-02 03:37:54400
Sorin Jianua8ef73d2017-11-02 16:55:17401 crx_downloader_->StartDownload(urls, std::string(hash_jebg),
402 std::move(callback_));
[email protected]afa378f22013-12-02 03:37:54403 RunThreads();
[email protected]afa378f22013-12-02 03:37:54404
Antonio Gomes0807dd62018-08-10 14:28:47405 EXPECT_EQ(2, GetInterceptorCount());
[email protected]148dcfd32014-04-29 00:54:30406
407 EXPECT_EQ(1, num_download_complete_calls_);
408 EXPECT_EQ(kExpectedContext, crx_context_);
409 EXPECT_NE(0, download_complete_result_.error);
410 EXPECT_TRUE(download_complete_result_.response.empty());
Sorin Jianuc35bacf2018-01-31 18:25:28411
412 const auto download_metrics = crx_downloader_->download_metrics();
413 ASSERT_EQ(2u, download_metrics.size());
414 EXPECT_EQ(expected_crx_url, download_metrics[0].url);
415 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[0].error);
416 EXPECT_EQ(-1, download_metrics[0].downloaded_bytes);
417 EXPECT_EQ(-1, download_metrics[0].total_bytes);
418 EXPECT_EQ(expected_crx_url, download_metrics[1].url);
419 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, download_metrics[1].error);
420 EXPECT_EQ(-1, download_metrics[1].downloaded_bytes);
421 EXPECT_EQ(-1, download_metrics[1].total_bytes);
[email protected]afa378f22013-12-02 03:37:54422}
423
sorin52ac0882015-01-24 01:15:00424} // namespace update_client