blob: 40440085ab1f1c38d854eb44b22ef5513bbd0f2e [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>
8
[email protected]afa378f22013-12-02 03:37:549#include "base/bind.h"
sorin395c2ac2014-09-16 21:31:0710#include "base/bind_helpers.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"
gabf64a25e2017-05-12 19:42:5614#include "base/message_loop/message_loop.h"
[email protected]afa378f22013-12-02 03:37:5415#include "base/path_service.h"
16#include "base/run_loop.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"
tommyclieaae5d92014-09-09 06:03:4723#include "net/url_request/test_url_request_interceptor.h"
[email protected]afa378f22013-12-02 03:37:5424#include "net/url_request/url_request_test_util.h"
25#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
33// Intercepts HTTP GET requests sent to "localhost".
tommyclieaae5d92014-09-09 06:03:4734typedef net::LocalHostTestURLRequestInterceptor GetInterceptor;
[email protected]afa378f22013-12-02 03:37:5435
[email protected]148dcfd32014-04-29 00:54:3036const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx";
37
sorin74e70672016-02-03 03:13:1038const char hash_jebg[] =
39 "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87";
40
[email protected]148dcfd32014-04-29 00:54:3041base::FilePath MakeTestFilePath(const char* file) {
[email protected]afa378f22013-12-02 03:37:5442 base::FilePath path;
tommycli0409b4df2014-08-26 23:31:3243 PathService::Get(base::DIR_SOURCE_ROOT, &path);
sorin74e70672016-02-03 03:13:1044 return path.AppendASCII("components/test/data/update_client")
sorin52ac0882015-01-24 01:15:0045 .AppendASCII(file);
[email protected]afa378f22013-12-02 03:37:5446}
47
48} // namespace
49
50class CrxDownloaderTest : public testing::Test {
51 public:
52 CrxDownloaderTest();
dcheng30a1b1542014-10-29 21:27:5053 ~CrxDownloaderTest() override;
[email protected]afa378f22013-12-02 03:37:5454
55 // Overrides from testing::Test.
dcheng30a1b1542014-10-29 21:27:5056 void SetUp() override;
57 void TearDown() override;
[email protected]afa378f22013-12-02 03:37:5458
59 void Quit();
60 void RunThreads();
61 void RunThreadsUntilIdle();
62
[email protected]3cb2a4f2013-12-07 21:54:3463 void DownloadComplete(int crx_context, const CrxDownloader::Result& result);
[email protected]afa378f22013-12-02 03:37:5464
[email protected]8a5ebd432014-05-02 00:21:2265 void DownloadProgress(int crx_context, const CrxDownloader::Result& result);
66
[email protected]afa378f22013-12-02 03:37:5467 protected:
dchengd0fc6aa92016-04-22 18:03:1268 std::unique_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:5469
dchengd0fc6aa92016-04-22 18:03:1270 std::unique_ptr<GetInterceptor> get_interceptor_;
tommyclieaae5d92014-09-09 06:03:4771
[email protected]1b6587dc52014-04-26 00:38:5572 CrxDownloader::DownloadCallback callback_;
[email protected]8a5ebd432014-05-02 00:21:2273 CrxDownloader::ProgressCallback progress_callback_;
[email protected]1b6587dc52014-04-26 00:38:5574
[email protected]afa378f22013-12-02 03:37:5475 int crx_context_;
[email protected]afa378f22013-12-02 03:37:5476
[email protected]148dcfd32014-04-29 00:54:3077 int num_download_complete_calls_;
78 CrxDownloader::Result download_complete_result_;
[email protected]afa378f22013-12-02 03:37:5479
[email protected]8a5ebd432014-05-02 00:21:2280 // These members are updated by DownloadProgress.
81 int num_progress_calls_;
82 CrxDownloader::Result download_progress_result_;
83
[email protected]afa378f22013-12-02 03:37:5484 // A magic value for the context to be used in the tests.
85 static const int kExpectedContext = 0xaabb;
86
87 private:
Sorin Jianu72d8fe0d2017-07-11 18:42:1688 base::test::ScopedTaskEnvironment scoped_task_environment_;
[email protected]afa378f22013-12-02 03:37:5489 scoped_refptr<net::TestURLRequestContextGetter> context_;
[email protected]afa378f22013-12-02 03:37:5490 base::Closure quit_closure_;
91};
92
93const int CrxDownloaderTest::kExpectedContext;
94
95CrxDownloaderTest::CrxDownloaderTest()
[email protected]1b6587dc52014-04-26 00:38:5596 : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete,
97 base::Unretained(this),
98 kExpectedContext)),
[email protected]8a5ebd432014-05-02 00:21:2299 progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress,
100 base::Unretained(this),
101 kExpectedContext)),
[email protected]1b6587dc52014-04-26 00:38:55102 crx_context_(0),
[email protected]148dcfd32014-04-29 00:54:30103 num_download_complete_calls_(0),
[email protected]8a5ebd432014-05-02 00:21:22104 num_progress_calls_(0),
Sorin Jianu72d8fe0d2017-07-11 18:42:16105 scoped_task_environment_(
106 base::test::ScopedTaskEnvironment::MainThreadType::IO),
[email protected]afa378f22013-12-02 03:37:54107 context_(new net::TestURLRequestContextGetter(
Sorin Jianu72d8fe0d2017-07-11 18:42:16108 base::ThreadTaskRunnerHandle::Get())) {}
[email protected]afa378f22013-12-02 03:37:54109
110CrxDownloaderTest::~CrxDownloaderTest() {
[email protected]3a0092d2013-12-18 03:04:35111 context_ = NULL;
tommyclieaae5d92014-09-09 06:03:47112
113 // The GetInterceptor requires the message loop to run to destruct correctly.
114 get_interceptor_.reset();
115 RunThreadsUntilIdle();
[email protected]afa378f22013-12-02 03:37:54116}
117
118void CrxDownloaderTest::SetUp() {
[email protected]148dcfd32014-04-29 00:54:30119 num_download_complete_calls_ = 0;
120 download_complete_result_ = CrxDownloader::Result();
[email protected]8a5ebd432014-05-02 00:21:22121 num_progress_calls_ = 0;
122 download_progress_result_ = CrxDownloader::Result();
tommyclieaae5d92014-09-09 06:03:47123
sorin9797aba2015-04-17 17:15:03124 // Do not use the background downloader in these tests.
Sorin Jianu72d8fe0d2017-07-11 18:42:16125 crx_downloader_.reset(CrxDownloader::Create(false, context_.get()).release());
[email protected]8a5ebd432014-05-02 00:21:22126 crx_downloader_->set_progress_callback(progress_callback_);
tommyclieaae5d92014-09-09 06:03:47127
skyostilb0daa012015-06-02 19:03:48128 get_interceptor_.reset(
129 new GetInterceptor(base::ThreadTaskRunnerHandle::Get(),
130 base::ThreadTaskRunnerHandle::Get()));
[email protected]afa378f22013-12-02 03:37:54131}
132
133void CrxDownloaderTest::TearDown() {
[email protected]8a5ebd432014-05-02 00:21:22134 crx_downloader_.reset();
[email protected]afa378f22013-12-02 03:37:54135}
136
137void CrxDownloaderTest::Quit() {
[email protected]da37c1d2013-12-19 01:04:38138 if (!quit_closure_.is_null())
139 quit_closure_.Run();
[email protected]afa378f22013-12-02 03:37:54140}
141
[email protected]148dcfd32014-04-29 00:54:30142void CrxDownloaderTest::DownloadComplete(int crx_context,
143 const CrxDownloader::Result& result) {
144 ++num_download_complete_calls_;
[email protected]afa378f22013-12-02 03:37:54145 crx_context_ = crx_context;
[email protected]148dcfd32014-04-29 00:54:30146 download_complete_result_ = result;
[email protected]afa378f22013-12-02 03:37:54147 Quit();
148}
149
[email protected]8a5ebd432014-05-02 00:21:22150void CrxDownloaderTest::DownloadProgress(int crx_context,
151 const CrxDownloader::Result& result) {
152 ++num_progress_calls_;
153 download_progress_result_ = result;
154}
155
[email protected]afa378f22013-12-02 03:37:54156void CrxDownloaderTest::RunThreads() {
157 base::RunLoop runloop;
158 quit_closure_ = runloop.QuitClosure();
159 runloop.Run();
160
161 // Since some tests need to drain currently enqueued tasks such as network
162 // intercepts on the IO thread, run the threads until they are
163 // idle. The component updater service won't loop again until the loop count
164 // is set and the service is started.
165 RunThreadsUntilIdle();
166}
167
168void CrxDownloaderTest::RunThreadsUntilIdle() {
Sorin Jianubbd51392017-07-20 01:48:31169 scoped_task_environment_.RunUntilIdle();
[email protected]afa378f22013-12-02 03:37:54170 base::RunLoop().RunUntilIdle();
171}
172
[email protected]da37c1d2013-12-19 01:04:38173// Tests that starting a download without a url results in an error.
174TEST_F(CrxDownloaderTest, NoUrl) {
175 std::vector<GURL> urls;
sorin74e70672016-02-03 03:13:10176 crx_downloader_->StartDownload(urls, std::string("abcd"), callback_);
[email protected]da37c1d2013-12-19 01:04:38177 RunThreadsUntilIdle();
[email protected]da37c1d2013-12-19 01:04:38178
[email protected]148dcfd32014-04-29 00:54:30179 EXPECT_EQ(1, num_download_complete_calls_);
180 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41181 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_URL),
182 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10183 EXPECT_TRUE(download_complete_result_.response.empty());
184 EXPECT_EQ(-1, download_complete_result_.downloaded_bytes);
185 EXPECT_EQ(-1, download_complete_result_.total_bytes);
186 EXPECT_EQ(0, num_progress_calls_);
187}
188
189// Tests that starting a download without providing a hash results in an error.
190TEST_F(CrxDownloaderTest, NoHash) {
191 std::vector<GURL> urls(1, GURL("http://somehost/somefile"));
192
193 crx_downloader_->StartDownload(urls, std::string(), callback_);
194 RunThreadsUntilIdle();
195
196 EXPECT_EQ(1, num_download_complete_calls_);
197 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41198 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_HASH),
199 download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30200 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]8a5ebd432014-05-02 00:21:22201 EXPECT_EQ(-1, download_complete_result_.downloaded_bytes);
202 EXPECT_EQ(-1, download_complete_result_.total_bytes);
203 EXPECT_EQ(0, num_progress_calls_);
[email protected]da37c1d2013-12-19 01:04:38204}
205
[email protected]afa378f22013-12-02 03:37:54206// Tests that downloading from one url is successful.
207TEST_F(CrxDownloaderTest, OneUrl) {
208 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45209 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54210
[email protected]148dcfd32014-04-29 00:54:30211 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47212 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54213
sorin74e70672016-02-03 03:13:10214 crx_downloader_->StartDownloadFromUrl(expected_crx_url,
215 std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54216 RunThreads();
[email protected]afa378f22013-12-02 03:37:54217
tommyclieaae5d92014-09-09 06:03:47218 EXPECT_EQ(1, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30219
220 EXPECT_EQ(1, num_download_complete_calls_);
221 EXPECT_EQ(kExpectedContext, crx_context_);
222 EXPECT_EQ(0, download_complete_result_.error);
[email protected]8a5ebd432014-05-02 00:21:22223 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
224 EXPECT_EQ(1843, download_complete_result_.total_bytes);
[email protected]148dcfd32014-04-29 00:54:30225 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
226
Sorin Jianubbd51392017-07-20 01:48:31227 EXPECT_TRUE(
228 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22229
230 EXPECT_LE(1, num_progress_calls_);
231 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
232 EXPECT_EQ(1843, download_progress_result_.total_bytes);
[email protected]afa378f22013-12-02 03:37:54233}
234
sorin74e70672016-02-03 03:13:10235// Tests that downloading from one url fails if the actual hash of the file
236// does not match the expected hash.
237TEST_F(CrxDownloaderTest, OneUrlBadHash) {
238 const GURL expected_crx_url =
239 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
240
241 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
242 get_interceptor_->SetResponse(expected_crx_url, test_file);
243
244 crx_downloader_->StartDownloadFromUrl(
245 expected_crx_url,
246 std::string(
247 "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"),
248 callback_);
249 RunThreads();
250
251 EXPECT_EQ(1, get_interceptor_->GetHitCount());
252
253 EXPECT_EQ(1, num_download_complete_calls_);
254 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41255 EXPECT_EQ(static_cast<int>(CrxDownloaderError::BAD_HASH),
256 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10257 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
258 EXPECT_EQ(1843, download_complete_result_.total_bytes);
259 EXPECT_TRUE(download_complete_result_.response.empty());
260
261 EXPECT_LE(1, num_progress_calls_);
262 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
263 EXPECT_EQ(1843, download_progress_result_.total_bytes);
264}
265
266// Tests that specifying two urls has no side effects. Expect a successful
[email protected]afa378f22013-12-02 03:37:54267// download, and only one download request be made.
[email protected]5bff685d32014-04-23 00:43:03268// This test is flaky on Android. crbug.com/329883
269#if defined(OS_ANDROID)
270#define MAYBE_TwoUrls DISABLED_TwoUrls
271#else
272#define MAYBE_TwoUrls TwoUrls
273#endif
274TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) {
[email protected]afa378f22013-12-02 03:37:54275 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45276 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54277
[email protected]148dcfd32014-04-29 00:54:30278 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47279 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54280
281 std::vector<GURL> urls;
282 urls.push_back(expected_crx_url);
283 urls.push_back(expected_crx_url);
284
sorin74e70672016-02-03 03:13:10285 crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54286 RunThreads();
[email protected]afa378f22013-12-02 03:37:54287
tommyclieaae5d92014-09-09 06:03:47288 EXPECT_EQ(1, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30289
290 EXPECT_EQ(1, num_download_complete_calls_);
291 EXPECT_EQ(kExpectedContext, crx_context_);
292 EXPECT_EQ(0, download_complete_result_.error);
[email protected]8a5ebd432014-05-02 00:21:22293 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
294 EXPECT_EQ(1843, download_complete_result_.total_bytes);
[email protected]148dcfd32014-04-29 00:54:30295 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
296
Sorin Jianubbd51392017-07-20 01:48:31297 EXPECT_TRUE(
298 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22299
300 EXPECT_LE(1, num_progress_calls_);
301 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
302 EXPECT_EQ(1843, download_progress_result_.total_bytes);
[email protected]afa378f22013-12-02 03:37:54303}
304
305// Tests that an invalid host results in a download error.
306TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) {
307 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45308 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54309
[email protected]148dcfd32014-04-29 00:54:30310 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47311 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54312
313 crx_downloader_->StartDownloadFromUrl(
sorin74e70672016-02-03 03:13:10314 GURL("http://no.such.host"
315 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"),
316 std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54317 RunThreads();
[email protected]afa378f22013-12-02 03:37:54318
tommyclieaae5d92014-09-09 06:03:47319 EXPECT_EQ(0, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30320
321 EXPECT_EQ(1, num_download_complete_calls_);
322 EXPECT_EQ(kExpectedContext, crx_context_);
323 EXPECT_NE(0, download_complete_result_.error);
324 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]afa378f22013-12-02 03:37:54325}
326
327// Tests that an invalid path results in a download error.
328TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) {
329 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45330 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54331
[email protected]148dcfd32014-04-29 00:54:30332 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47333 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54334
[email protected]d0c8b8b42014-05-06 05:11:45335 crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"),
sorin74e70672016-02-03 03:13:10336 std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54337 RunThreads();
[email protected]afa378f22013-12-02 03:37:54338
tommyclieaae5d92014-09-09 06:03:47339 EXPECT_EQ(0, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30340
341 EXPECT_EQ(1, num_download_complete_calls_);
342 EXPECT_EQ(kExpectedContext, crx_context_);
343 EXPECT_NE(0, download_complete_result_.error);
344 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]afa378f22013-12-02 03:37:54345}
346
347// Tests that the fallback to a valid url is successful.
[email protected]5bff685d32014-04-23 00:43:03348// This test is flaky on Android. crbug.com/329883
349#if defined(OS_ANDROID)
350#define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid
351#else
352#define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid
353#endif
354TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) {
[email protected]afa378f22013-12-02 03:37:54355 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45356 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54357
[email protected]148dcfd32014-04-29 00:54:30358 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47359 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54360
361 std::vector<GURL> urls;
362 urls.push_back(GURL("http://localhost/no/such/file"));
363 urls.push_back(expected_crx_url);
364
sorin74e70672016-02-03 03:13:10365 crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54366 RunThreads();
[email protected]afa378f22013-12-02 03:37:54367
tommyclieaae5d92014-09-09 06:03:47368 EXPECT_EQ(1, get_interceptor_->GetHitCount());
[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]8a5ebd432014-05-02 00:21:22373 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
374 EXPECT_EQ(1843, download_complete_result_.total_bytes);
[email protected]148dcfd32014-04-29 00:54:30375 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_);
381 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
382 EXPECT_EQ(1843, download_progress_result_.total_bytes);
[email protected]afa378f22013-12-02 03:37:54383}
384
385// Tests that the download succeeds if the first url is correct and the
386// second bad url does not have a side-effect.
387TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
388 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45389 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54390
[email protected]148dcfd32014-04-29 00:54:30391 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47392 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54393
394 std::vector<GURL> urls;
395 urls.push_back(expected_crx_url);
396 urls.push_back(GURL("http://localhost/no/such/file"));
397
sorin74e70672016-02-03 03:13:10398 crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54399 RunThreads();
[email protected]afa378f22013-12-02 03:37:54400
tommyclieaae5d92014-09-09 06:03:47401 EXPECT_EQ(1, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30402
403 EXPECT_EQ(1, num_download_complete_calls_);
404 EXPECT_EQ(kExpectedContext, crx_context_);
405 EXPECT_EQ(0, download_complete_result_.error);
[email protected]8a5ebd432014-05-02 00:21:22406 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
407 EXPECT_EQ(1843, download_complete_result_.total_bytes);
[email protected]148dcfd32014-04-29 00:54:30408 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
409
Sorin Jianubbd51392017-07-20 01:48:31410 EXPECT_TRUE(
411 DeleteFileAndEmptyParentDirectory(download_complete_result_.response));
[email protected]8a5ebd432014-05-02 00:21:22412
413 EXPECT_LE(1, num_progress_calls_);
414 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
415 EXPECT_EQ(1843, download_progress_result_.total_bytes);
[email protected]afa378f22013-12-02 03:37:54416}
417
418// Tests that the download fails if both urls are bad.
419TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
420 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45421 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54422
[email protected]148dcfd32014-04-29 00:54:30423 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47424 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54425
426 std::vector<GURL> urls;
427 urls.push_back(GURL("http://localhost/no/such/file"));
sorin52ac0882015-01-24 01:15:00428 urls.push_back(GURL(
429 "http://no.such.host/"
430 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"));
[email protected]afa378f22013-12-02 03:37:54431
sorin74e70672016-02-03 03:13:10432 crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54433 RunThreads();
[email protected]afa378f22013-12-02 03:37:54434
tommyclieaae5d92014-09-09 06:03:47435 EXPECT_EQ(0, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30436
437 EXPECT_EQ(1, num_download_complete_calls_);
438 EXPECT_EQ(kExpectedContext, crx_context_);
439 EXPECT_NE(0, download_complete_result_.error);
440 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]afa378f22013-12-02 03:37:54441}
442
sorin52ac0882015-01-24 01:15:00443} // namespace update_client