blob: ca9d4c77a2786b19bd1b6f303e44c40c963ae03e [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"
[email protected]afa378f22013-12-02 03:37:5414#include "base/path_service.h"
15#include "base/run_loop.h"
gab7966d312016-05-11 20:35:0116#include "base/threading/thread_task_runner_handle.h"
avi5dd91f82015-12-25 22:30:4617#include "build/build_config.h"
sorin7b8650522016-11-02 18:23:4118#include "components/update_client/update_client_errors.h"
[email protected]afa378f22013-12-02 03:37:5419#include "net/base/net_errors.h"
tommyclieaae5d92014-09-09 06:03:4720#include "net/url_request/test_url_request_interceptor.h"
[email protected]afa378f22013-12-02 03:37:5421#include "net/url_request/url_request_test_util.h"
22#include "testing/gtest/include/gtest/gtest.h"
23
[email protected]148dcfd32014-04-29 00:54:3024using base::ContentsEqual;
[email protected]afa378f22013-12-02 03:37:5425
sorin52ac0882015-01-24 01:15:0026namespace update_client {
[email protected]afa378f22013-12-02 03:37:5427
28namespace {
29
30// Intercepts HTTP GET requests sent to "localhost".
tommyclieaae5d92014-09-09 06:03:4731typedef net::LocalHostTestURLRequestInterceptor GetInterceptor;
[email protected]afa378f22013-12-02 03:37:5432
[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;
tommycli0409b4df2014-08-26 23:31:3240 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
[email protected]afa378f22013-12-02 03:37:5464 protected:
dchengd0fc6aa92016-04-22 18:03:1265 std::unique_ptr<CrxDownloader> crx_downloader_;
[email protected]afa378f22013-12-02 03:37:5466
dchengd0fc6aa92016-04-22 18:03:1267 std::unique_ptr<GetInterceptor> get_interceptor_;
tommyclieaae5d92014-09-09 06:03:4768
[email protected]1b6587dc52014-04-26 00:38:5569 CrxDownloader::DownloadCallback callback_;
[email protected]8a5ebd432014-05-02 00:21:2270 CrxDownloader::ProgressCallback progress_callback_;
[email protected]1b6587dc52014-04-26 00:38:5571
[email protected]afa378f22013-12-02 03:37:5472 int crx_context_;
[email protected]afa378f22013-12-02 03:37:5473
[email protected]148dcfd32014-04-29 00:54:3074 int num_download_complete_calls_;
75 CrxDownloader::Result download_complete_result_;
[email protected]afa378f22013-12-02 03:37:5476
[email protected]8a5ebd432014-05-02 00:21:2277 // These members are updated by DownloadProgress.
78 int num_progress_calls_;
79 CrxDownloader::Result download_progress_result_;
80
[email protected]afa378f22013-12-02 03:37:5481 // A magic value for the context to be used in the tests.
82 static const int kExpectedContext = 0xaabb;
83
84 private:
tommyclieaae5d92014-09-09 06:03:4785 base::MessageLoopForIO loop_;
[email protected]afa378f22013-12-02 03:37:5486 scoped_refptr<net::TestURLRequestContextGetter> context_;
[email protected]afa378f22013-12-02 03:37:5487 base::Closure quit_closure_;
88};
89
90const int CrxDownloaderTest::kExpectedContext;
91
92CrxDownloaderTest::CrxDownloaderTest()
[email protected]1b6587dc52014-04-26 00:38:5593 : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete,
94 base::Unretained(this),
95 kExpectedContext)),
[email protected]8a5ebd432014-05-02 00:21:2296 progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress,
97 base::Unretained(this),
98 kExpectedContext)),
[email protected]1b6587dc52014-04-26 00:38:5599 crx_context_(0),
[email protected]148dcfd32014-04-29 00:54:30100 num_download_complete_calls_(0),
[email protected]8a5ebd432014-05-02 00:21:22101 num_progress_calls_(0),
[email protected]afa378f22013-12-02 03:37:54102 context_(new net::TestURLRequestContextGetter(
skyostilb0daa012015-06-02 19:03:48103 base::ThreadTaskRunnerHandle::Get())) {
[email protected]afa378f22013-12-02 03:37:54104}
105
106CrxDownloaderTest::~CrxDownloaderTest() {
[email protected]3a0092d2013-12-18 03:04:35107 context_ = NULL;
tommyclieaae5d92014-09-09 06:03:47108
109 // The GetInterceptor requires the message loop to run to destruct correctly.
110 get_interceptor_.reset();
111 RunThreadsUntilIdle();
[email protected]afa378f22013-12-02 03:37:54112}
113
114void CrxDownloaderTest::SetUp() {
[email protected]148dcfd32014-04-29 00:54:30115 num_download_complete_calls_ = 0;
116 download_complete_result_ = CrxDownloader::Result();
[email protected]8a5ebd432014-05-02 00:21:22117 num_progress_calls_ = 0;
118 download_progress_result_ = CrxDownloader::Result();
tommyclieaae5d92014-09-09 06:03:47119
sorin9797aba2015-04-17 17:15:03120 // Do not use the background downloader in these tests.
skyostilb0daa012015-06-02 19:03:48121 crx_downloader_.reset(
122 CrxDownloader::Create(false, context_.get(),
sorin33012532015-10-26 21:05:54123 base::ThreadTaskRunnerHandle::Get())
124 .release());
[email protected]8a5ebd432014-05-02 00:21:22125 crx_downloader_->set_progress_callback(progress_callback_);
tommyclieaae5d92014-09-09 06:03:47126
skyostilb0daa012015-06-02 19:03:48127 get_interceptor_.reset(
128 new GetInterceptor(base::ThreadTaskRunnerHandle::Get(),
129 base::ThreadTaskRunnerHandle::Get()));
[email protected]afa378f22013-12-02 03:37:54130}
131
132void CrxDownloaderTest::TearDown() {
[email protected]8a5ebd432014-05-02 00:21:22133 crx_downloader_.reset();
[email protected]afa378f22013-12-02 03:37:54134}
135
136void CrxDownloaderTest::Quit() {
[email protected]da37c1d2013-12-19 01:04:38137 if (!quit_closure_.is_null())
138 quit_closure_.Run();
[email protected]afa378f22013-12-02 03:37:54139}
140
[email protected]148dcfd32014-04-29 00:54:30141void CrxDownloaderTest::DownloadComplete(int crx_context,
142 const CrxDownloader::Result& result) {
143 ++num_download_complete_calls_;
[email protected]afa378f22013-12-02 03:37:54144 crx_context_ = crx_context;
[email protected]148dcfd32014-04-29 00:54:30145 download_complete_result_ = result;
[email protected]afa378f22013-12-02 03:37:54146 Quit();
147}
148
[email protected]8a5ebd432014-05-02 00:21:22149void CrxDownloaderTest::DownloadProgress(int crx_context,
150 const CrxDownloader::Result& result) {
151 ++num_progress_calls_;
152 download_progress_result_ = result;
153}
154
[email protected]afa378f22013-12-02 03:37:54155void CrxDownloaderTest::RunThreads() {
156 base::RunLoop runloop;
157 quit_closure_ = runloop.QuitClosure();
158 runloop.Run();
159
160 // Since some tests need to drain currently enqueued tasks such as network
161 // intercepts on the IO thread, run the threads until they are
162 // idle. The component updater service won't loop again until the loop count
163 // is set and the service is started.
164 RunThreadsUntilIdle();
165}
166
167void CrxDownloaderTest::RunThreadsUntilIdle() {
168 base::RunLoop().RunUntilIdle();
169}
170
[email protected]da37c1d2013-12-19 01:04:38171// Tests that starting a download without a url results in an error.
172TEST_F(CrxDownloaderTest, NoUrl) {
173 std::vector<GURL> urls;
sorin74e70672016-02-03 03:13:10174 crx_downloader_->StartDownload(urls, std::string("abcd"), callback_);
[email protected]da37c1d2013-12-19 01:04:38175 RunThreadsUntilIdle();
[email protected]da37c1d2013-12-19 01:04:38176
[email protected]148dcfd32014-04-29 00:54:30177 EXPECT_EQ(1, num_download_complete_calls_);
178 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41179 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_URL),
180 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10181 EXPECT_TRUE(download_complete_result_.response.empty());
182 EXPECT_EQ(-1, download_complete_result_.downloaded_bytes);
183 EXPECT_EQ(-1, download_complete_result_.total_bytes);
184 EXPECT_EQ(0, num_progress_calls_);
185}
186
187// Tests that starting a download without providing a hash results in an error.
188TEST_F(CrxDownloaderTest, NoHash) {
189 std::vector<GURL> urls(1, GURL("http://somehost/somefile"));
190
191 crx_downloader_->StartDownload(urls, std::string(), callback_);
192 RunThreadsUntilIdle();
193
194 EXPECT_EQ(1, num_download_complete_calls_);
195 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41196 EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_HASH),
197 download_complete_result_.error);
[email protected]148dcfd32014-04-29 00:54:30198 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]8a5ebd432014-05-02 00:21:22199 EXPECT_EQ(-1, download_complete_result_.downloaded_bytes);
200 EXPECT_EQ(-1, download_complete_result_.total_bytes);
201 EXPECT_EQ(0, num_progress_calls_);
[email protected]da37c1d2013-12-19 01:04:38202}
203
[email protected]afa378f22013-12-02 03:37:54204// Tests that downloading from one url is successful.
205TEST_F(CrxDownloaderTest, OneUrl) {
206 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45207 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54208
[email protected]148dcfd32014-04-29 00:54:30209 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47210 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54211
sorin74e70672016-02-03 03:13:10212 crx_downloader_->StartDownloadFromUrl(expected_crx_url,
213 std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54214 RunThreads();
[email protected]afa378f22013-12-02 03:37:54215
tommyclieaae5d92014-09-09 06:03:47216 EXPECT_EQ(1, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30217
218 EXPECT_EQ(1, num_download_complete_calls_);
219 EXPECT_EQ(kExpectedContext, crx_context_);
220 EXPECT_EQ(0, download_complete_result_.error);
[email protected]8a5ebd432014-05-02 00:21:22221 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
222 EXPECT_EQ(1843, download_complete_result_.total_bytes);
[email protected]148dcfd32014-04-29 00:54:30223 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
224
225 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
[email protected]8a5ebd432014-05-02 00:21:22226
227 EXPECT_LE(1, num_progress_calls_);
228 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
229 EXPECT_EQ(1843, download_progress_result_.total_bytes);
[email protected]afa378f22013-12-02 03:37:54230}
231
sorin74e70672016-02-03 03:13:10232// Tests that downloading from one url fails if the actual hash of the file
233// does not match the expected hash.
234TEST_F(CrxDownloaderTest, OneUrlBadHash) {
235 const GURL expected_crx_url =
236 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
237
238 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
239 get_interceptor_->SetResponse(expected_crx_url, test_file);
240
241 crx_downloader_->StartDownloadFromUrl(
242 expected_crx_url,
243 std::string(
244 "813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"),
245 callback_);
246 RunThreads();
247
248 EXPECT_EQ(1, get_interceptor_->GetHitCount());
249
250 EXPECT_EQ(1, num_download_complete_calls_);
251 EXPECT_EQ(kExpectedContext, crx_context_);
sorin7b8650522016-11-02 18:23:41252 EXPECT_EQ(static_cast<int>(CrxDownloaderError::BAD_HASH),
253 download_complete_result_.error);
sorin74e70672016-02-03 03:13:10254 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
255 EXPECT_EQ(1843, download_complete_result_.total_bytes);
256 EXPECT_TRUE(download_complete_result_.response.empty());
257
258 EXPECT_LE(1, num_progress_calls_);
259 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
260 EXPECT_EQ(1843, download_progress_result_.total_bytes);
261}
262
263// Tests that specifying two urls has no side effects. Expect a successful
[email protected]afa378f22013-12-02 03:37:54264// download, and only one download request be made.
[email protected]5bff685d32014-04-23 00:43:03265// This test is flaky on Android. crbug.com/329883
266#if defined(OS_ANDROID)
267#define MAYBE_TwoUrls DISABLED_TwoUrls
268#else
269#define MAYBE_TwoUrls TwoUrls
270#endif
271TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) {
[email protected]afa378f22013-12-02 03:37:54272 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45273 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54274
[email protected]148dcfd32014-04-29 00:54:30275 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47276 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54277
278 std::vector<GURL> urls;
279 urls.push_back(expected_crx_url);
280 urls.push_back(expected_crx_url);
281
sorin74e70672016-02-03 03:13:10282 crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54283 RunThreads();
[email protected]afa378f22013-12-02 03:37:54284
tommyclieaae5d92014-09-09 06:03:47285 EXPECT_EQ(1, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30286
287 EXPECT_EQ(1, num_download_complete_calls_);
288 EXPECT_EQ(kExpectedContext, crx_context_);
289 EXPECT_EQ(0, download_complete_result_.error);
[email protected]8a5ebd432014-05-02 00:21:22290 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
291 EXPECT_EQ(1843, download_complete_result_.total_bytes);
[email protected]148dcfd32014-04-29 00:54:30292 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
293
294 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
[email protected]8a5ebd432014-05-02 00:21:22295
296 EXPECT_LE(1, num_progress_calls_);
297 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
298 EXPECT_EQ(1843, download_progress_result_.total_bytes);
[email protected]afa378f22013-12-02 03:37:54299}
300
301// Tests that an invalid host results in a download error.
302TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) {
303 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45304 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54305
[email protected]148dcfd32014-04-29 00:54:30306 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47307 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54308
309 crx_downloader_->StartDownloadFromUrl(
sorin74e70672016-02-03 03:13:10310 GURL("http://no.such.host"
311 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"),
312 std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54313 RunThreads();
[email protected]afa378f22013-12-02 03:37:54314
tommyclieaae5d92014-09-09 06:03:47315 EXPECT_EQ(0, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30316
317 EXPECT_EQ(1, num_download_complete_calls_);
318 EXPECT_EQ(kExpectedContext, crx_context_);
319 EXPECT_NE(0, download_complete_result_.error);
320 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]afa378f22013-12-02 03:37:54321}
322
323// Tests that an invalid path results in a download error.
324TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) {
325 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45326 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54327
[email protected]148dcfd32014-04-29 00:54:30328 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47329 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54330
[email protected]d0c8b8b42014-05-06 05:11:45331 crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"),
sorin74e70672016-02-03 03:13:10332 std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54333 RunThreads();
[email protected]afa378f22013-12-02 03:37:54334
tommyclieaae5d92014-09-09 06:03:47335 EXPECT_EQ(0, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30336
337 EXPECT_EQ(1, num_download_complete_calls_);
338 EXPECT_EQ(kExpectedContext, crx_context_);
339 EXPECT_NE(0, download_complete_result_.error);
340 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]afa378f22013-12-02 03:37:54341}
342
343// Tests that the fallback to a valid url is successful.
[email protected]5bff685d32014-04-23 00:43:03344// This test is flaky on Android. crbug.com/329883
345#if defined(OS_ANDROID)
346#define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid
347#else
348#define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid
349#endif
350TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) {
[email protected]afa378f22013-12-02 03:37:54351 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45352 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54353
[email protected]148dcfd32014-04-29 00:54:30354 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47355 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54356
357 std::vector<GURL> urls;
358 urls.push_back(GURL("http://localhost/no/such/file"));
359 urls.push_back(expected_crx_url);
360
sorin74e70672016-02-03 03:13:10361 crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54362 RunThreads();
[email protected]afa378f22013-12-02 03:37:54363
tommyclieaae5d92014-09-09 06:03:47364 EXPECT_EQ(1, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30365
366 EXPECT_EQ(1, num_download_complete_calls_);
367 EXPECT_EQ(kExpectedContext, crx_context_);
368 EXPECT_EQ(0, download_complete_result_.error);
[email protected]8a5ebd432014-05-02 00:21:22369 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
370 EXPECT_EQ(1843, download_complete_result_.total_bytes);
[email protected]148dcfd32014-04-29 00:54:30371 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
372
373 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
[email protected]8a5ebd432014-05-02 00:21:22374
375 EXPECT_LE(1, num_progress_calls_);
376 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
377 EXPECT_EQ(1843, download_progress_result_.total_bytes);
[email protected]afa378f22013-12-02 03:37:54378}
379
380// Tests that the download succeeds if the first url is correct and the
381// second bad url does not have a side-effect.
382TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
383 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45384 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54385
[email protected]148dcfd32014-04-29 00:54:30386 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47387 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54388
389 std::vector<GURL> urls;
390 urls.push_back(expected_crx_url);
391 urls.push_back(GURL("http://localhost/no/such/file"));
392
sorin74e70672016-02-03 03:13:10393 crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54394 RunThreads();
[email protected]afa378f22013-12-02 03:37:54395
tommyclieaae5d92014-09-09 06:03:47396 EXPECT_EQ(1, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30397
398 EXPECT_EQ(1, num_download_complete_calls_);
399 EXPECT_EQ(kExpectedContext, crx_context_);
400 EXPECT_EQ(0, download_complete_result_.error);
[email protected]8a5ebd432014-05-02 00:21:22401 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
402 EXPECT_EQ(1843, download_complete_result_.total_bytes);
[email protected]148dcfd32014-04-29 00:54:30403 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
404
405 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
[email protected]8a5ebd432014-05-02 00:21:22406
407 EXPECT_LE(1, num_progress_calls_);
408 EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
409 EXPECT_EQ(1843, download_progress_result_.total_bytes);
[email protected]afa378f22013-12-02 03:37:54410}
411
412// Tests that the download fails if both urls are bad.
413TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
414 const GURL expected_crx_url =
[email protected]d0c8b8b42014-05-06 05:11:45415 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
[email protected]afa378f22013-12-02 03:37:54416
[email protected]148dcfd32014-04-29 00:54:30417 const base::FilePath test_file(MakeTestFilePath(kTestFileName));
tommyclieaae5d92014-09-09 06:03:47418 get_interceptor_->SetResponse(expected_crx_url, test_file);
[email protected]afa378f22013-12-02 03:37:54419
420 std::vector<GURL> urls;
421 urls.push_back(GURL("http://localhost/no/such/file"));
sorin52ac0882015-01-24 01:15:00422 urls.push_back(GURL(
423 "http://no.such.host/"
424 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"));
[email protected]afa378f22013-12-02 03:37:54425
sorin74e70672016-02-03 03:13:10426 crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
[email protected]afa378f22013-12-02 03:37:54427 RunThreads();
[email protected]afa378f22013-12-02 03:37:54428
tommyclieaae5d92014-09-09 06:03:47429 EXPECT_EQ(0, get_interceptor_->GetHitCount());
[email protected]148dcfd32014-04-29 00:54:30430
431 EXPECT_EQ(1, num_download_complete_calls_);
432 EXPECT_EQ(kExpectedContext, crx_context_);
433 EXPECT_NE(0, download_complete_result_.error);
434 EXPECT_TRUE(download_complete_result_.response.empty());
[email protected]afa378f22013-12-02 03:37:54435}
436
sorin52ac0882015-01-24 01:15:00437} // namespace update_client