1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/update_client/crx_downloader.h"
#include <memory>
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "base/threading/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "components/update_client/update_client_errors.h"
#include "net/base/net_errors.h"
#include "net/url_request/test_url_request_interceptor.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::ContentsEqual;
namespace update_client {
namespace {
// Intercepts HTTP GET requests sent to "localhost".
typedef net::LocalHostTestURLRequestInterceptor GetInterceptor;
const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx";
const char hash_jebg[] =
"6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87";
base::FilePath MakeTestFilePath(const char* file) {
base::FilePath path;
PathService::Get(base::DIR_SOURCE_ROOT, &path);
return path.AppendASCII("components/test/data/update_client")
.AppendASCII(file);
}
} // namespace
class CrxDownloaderTest : public testing::Test {
public:
CrxDownloaderTest();
~CrxDownloaderTest() override;
// Overrides from testing::Test.
void SetUp() override;
void TearDown() override;
void Quit();
void RunThreads();
void RunThreadsUntilIdle();
void DownloadComplete(int crx_context, const CrxDownloader::Result& result);
void DownloadProgress(int crx_context, const CrxDownloader::Result& result);
protected:
std::unique_ptr<CrxDownloader> crx_downloader_;
std::unique_ptr<GetInterceptor> get_interceptor_;
CrxDownloader::DownloadCallback callback_;
CrxDownloader::ProgressCallback progress_callback_;
int crx_context_;
int num_download_complete_calls_;
CrxDownloader::Result download_complete_result_;
// These members are updated by DownloadProgress.
int num_progress_calls_;
CrxDownloader::Result download_progress_result_;
// A magic value for the context to be used in the tests.
static const int kExpectedContext = 0xaabb;
private:
base::MessageLoopForIO loop_;
scoped_refptr<net::TestURLRequestContextGetter> context_;
base::Closure quit_closure_;
};
const int CrxDownloaderTest::kExpectedContext;
CrxDownloaderTest::CrxDownloaderTest()
: callback_(base::Bind(&CrxDownloaderTest::DownloadComplete,
base::Unretained(this),
kExpectedContext)),
progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress,
base::Unretained(this),
kExpectedContext)),
crx_context_(0),
num_download_complete_calls_(0),
num_progress_calls_(0),
context_(new net::TestURLRequestContextGetter(
base::ThreadTaskRunnerHandle::Get())) {
}
CrxDownloaderTest::~CrxDownloaderTest() {
context_ = NULL;
// The GetInterceptor requires the message loop to run to destruct correctly.
get_interceptor_.reset();
RunThreadsUntilIdle();
}
void CrxDownloaderTest::SetUp() {
num_download_complete_calls_ = 0;
download_complete_result_ = CrxDownloader::Result();
num_progress_calls_ = 0;
download_progress_result_ = CrxDownloader::Result();
// Do not use the background downloader in these tests.
crx_downloader_.reset(
CrxDownloader::Create(false, context_.get(),
base::ThreadTaskRunnerHandle::Get())
.release());
crx_downloader_->set_progress_callback(progress_callback_);
get_interceptor_.reset(
new GetInterceptor(base::ThreadTaskRunnerHandle::Get(),
base::ThreadTaskRunnerHandle::Get()));
}
void CrxDownloaderTest::TearDown() {
crx_downloader_.reset();
}
void CrxDownloaderTest::Quit() {
if (!quit_closure_.is_null())
quit_closure_.Run();
}
void CrxDownloaderTest::DownloadComplete(int crx_context,
const CrxDownloader::Result& result) {
++num_download_complete_calls_;
crx_context_ = crx_context;
download_complete_result_ = result;
Quit();
}
void CrxDownloaderTest::DownloadProgress(int crx_context,
const CrxDownloader::Result& result) {
++num_progress_calls_;
download_progress_result_ = result;
}
void CrxDownloaderTest::RunThreads() {
base::RunLoop runloop;
quit_closure_ = runloop.QuitClosure();
runloop.Run();
// Since some tests need to drain currently enqueued tasks such as network
// intercepts on the IO thread, run the threads until they are
// idle. The component updater service won't loop again until the loop count
// is set and the service is started.
RunThreadsUntilIdle();
}
void CrxDownloaderTest::RunThreadsUntilIdle() {
base::RunLoop().RunUntilIdle();
}
// Tests that starting a download without a url results in an error.
TEST_F(CrxDownloaderTest, NoUrl) {
std::vector<GURL> urls;
crx_downloader_->StartDownload(urls, std::string("abcd"), callback_);
RunThreadsUntilIdle();
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_URL),
download_complete_result_.error);
EXPECT_TRUE(download_complete_result_.response.empty());
EXPECT_EQ(-1, download_complete_result_.downloaded_bytes);
EXPECT_EQ(-1, download_complete_result_.total_bytes);
EXPECT_EQ(0, num_progress_calls_);
}
// Tests that starting a download without providing a hash results in an error.
TEST_F(CrxDownloaderTest, NoHash) {
std::vector<GURL> urls(1, GURL("http://somehost/somefile"));
crx_downloader_->StartDownload(urls, std::string(), callback_);
RunThreadsUntilIdle();
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_HASH),
download_complete_result_.error);
EXPECT_TRUE(download_complete_result_.response.empty());
EXPECT_EQ(-1, download_complete_result_.downloaded_bytes);
EXPECT_EQ(-1, download_complete_result_.total_bytes);
EXPECT_EQ(0, num_progress_calls_);
}
// Tests that downloading from one url is successful.
TEST_F(CrxDownloaderTest, OneUrl) {
const GURL expected_crx_url =
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
get_interceptor_->SetResponse(expected_crx_url, test_file);
crx_downloader_->StartDownloadFromUrl(expected_crx_url,
std::string(hash_jebg), callback_);
RunThreads();
EXPECT_EQ(1, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
EXPECT_EQ(0, download_complete_result_.error);
EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
EXPECT_EQ(1843, download_complete_result_.total_bytes);
EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
EXPECT_LE(1, num_progress_calls_);
EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
EXPECT_EQ(1843, download_progress_result_.total_bytes);
}
// Tests that downloading from one url fails if the actual hash of the file
// does not match the expected hash.
TEST_F(CrxDownloaderTest, OneUrlBadHash) {
const GURL expected_crx_url =
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
get_interceptor_->SetResponse(expected_crx_url, test_file);
crx_downloader_->StartDownloadFromUrl(
expected_crx_url,
std::string(
"813c59747e139a608b3b5fc49633affc6db574373f309f156ea6d27229c0b3f9"),
callback_);
RunThreads();
EXPECT_EQ(1, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
EXPECT_EQ(static_cast<int>(CrxDownloaderError::BAD_HASH),
download_complete_result_.error);
EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
EXPECT_EQ(1843, download_complete_result_.total_bytes);
EXPECT_TRUE(download_complete_result_.response.empty());
EXPECT_LE(1, num_progress_calls_);
EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
EXPECT_EQ(1843, download_progress_result_.total_bytes);
}
// Tests that specifying two urls has no side effects. Expect a successful
// download, and only one download request be made.
// This test is flaky on Android. crbug.com/329883
#if defined(OS_ANDROID)
#define MAYBE_TwoUrls DISABLED_TwoUrls
#else
#define MAYBE_TwoUrls TwoUrls
#endif
TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) {
const GURL expected_crx_url =
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
get_interceptor_->SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(expected_crx_url);
urls.push_back(expected_crx_url);
crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
RunThreads();
EXPECT_EQ(1, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
EXPECT_EQ(0, download_complete_result_.error);
EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
EXPECT_EQ(1843, download_complete_result_.total_bytes);
EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
EXPECT_LE(1, num_progress_calls_);
EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
EXPECT_EQ(1843, download_progress_result_.total_bytes);
}
// Tests that an invalid host results in a download error.
TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) {
const GURL expected_crx_url =
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
get_interceptor_->SetResponse(expected_crx_url, test_file);
crx_downloader_->StartDownloadFromUrl(
GURL("http://no.such.host"
"/download/jebgalgnebhfojomionfpkfelancnnkf.crx"),
std::string(hash_jebg), callback_);
RunThreads();
EXPECT_EQ(0, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
EXPECT_NE(0, download_complete_result_.error);
EXPECT_TRUE(download_complete_result_.response.empty());
}
// Tests that an invalid path results in a download error.
TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) {
const GURL expected_crx_url =
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
get_interceptor_->SetResponse(expected_crx_url, test_file);
crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"),
std::string(hash_jebg), callback_);
RunThreads();
EXPECT_EQ(0, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
EXPECT_NE(0, download_complete_result_.error);
EXPECT_TRUE(download_complete_result_.response.empty());
}
// Tests that the fallback to a valid url is successful.
// This test is flaky on Android. crbug.com/329883
#if defined(OS_ANDROID)
#define MAYBE_TwoUrls_FirstInvalid DISABLED_TwoUrls_FirstInvalid
#else
#define MAYBE_TwoUrls_FirstInvalid TwoUrls_FirstInvalid
#endif
TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) {
const GURL expected_crx_url =
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
get_interceptor_->SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(GURL("http://localhost/no/such/file"));
urls.push_back(expected_crx_url);
crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
RunThreads();
EXPECT_EQ(1, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
EXPECT_EQ(0, download_complete_result_.error);
EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
EXPECT_EQ(1843, download_complete_result_.total_bytes);
EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
EXPECT_LE(1, num_progress_calls_);
EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
EXPECT_EQ(1843, download_progress_result_.total_bytes);
}
// Tests that the download succeeds if the first url is correct and the
// second bad url does not have a side-effect.
TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) {
const GURL expected_crx_url =
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
get_interceptor_->SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(expected_crx_url);
urls.push_back(GURL("http://localhost/no/such/file"));
crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
RunThreads();
EXPECT_EQ(1, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
EXPECT_EQ(0, download_complete_result_.error);
EXPECT_EQ(1843, download_complete_result_.downloaded_bytes);
EXPECT_EQ(1843, download_complete_result_.total_bytes);
EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file));
EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false));
EXPECT_LE(1, num_progress_calls_);
EXPECT_EQ(1843, download_progress_result_.downloaded_bytes);
EXPECT_EQ(1843, download_progress_result_.total_bytes);
}
// Tests that the download fails if both urls are bad.
TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) {
const GURL expected_crx_url =
GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx");
const base::FilePath test_file(MakeTestFilePath(kTestFileName));
get_interceptor_->SetResponse(expected_crx_url, test_file);
std::vector<GURL> urls;
urls.push_back(GURL("http://localhost/no/such/file"));
urls.push_back(GURL(
"http://no.such.host/"
"/download/jebgalgnebhfojomionfpkfelancnnkf.crx"));
crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_);
RunThreads();
EXPECT_EQ(0, get_interceptor_->GetHitCount());
EXPECT_EQ(1, num_download_complete_calls_);
EXPECT_EQ(kExpectedContext, crx_context_);
EXPECT_NE(0, download_complete_result_.error);
EXPECT_TRUE(download_complete_result_.response.empty());
}
} // namespace update_client
|