[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 1 | // 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 | |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 5 | #include "components/update_client/crx_downloader.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 9 | #include "base/bind.h" |
sorin | 395c2ac | 2014-09-16 21:31:07 | [diff] [blame] | 10 | #include "base/bind_helpers.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
gab | f64a25e | 2017-05-12 19:42:56 | [diff] [blame] | 14 | #include "base/message_loop/message_loop.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 15 | #include "base/path_service.h" |
| 16 | #include "base/run_loop.h" |
Sorin Jianu | 72d8fe0d | 2017-07-11 18:42:16 | [diff] [blame] | 17 | #include "base/test/scoped_task_environment.h" |
gab | 7966d31 | 2016-05-11 20:35:01 | [diff] [blame] | 18 | #include "base/threading/thread_task_runner_handle.h" |
avi | 5dd91f8 | 2015-12-25 22:30:46 | [diff] [blame] | 19 | #include "build/build_config.h" |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 20 | #include "components/update_client/update_client_errors.h" |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame^] | 21 | #include "components/update_client/utils.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 22 | #include "net/base/net_errors.h" |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 23 | #include "net/url_request/test_url_request_interceptor.h" |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 24 | #include "net/url_request/url_request_test_util.h" |
| 25 | #include "testing/gtest/include/gtest/gtest.h" |
| 26 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 27 | using base::ContentsEqual; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 28 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 29 | namespace update_client { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
| 33 | // Intercepts HTTP GET requests sent to "localhost". |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 34 | typedef net::LocalHostTestURLRequestInterceptor GetInterceptor; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 35 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 36 | const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx"; |
| 37 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 38 | const char hash_jebg[] = |
| 39 | "6fc4b93fd11134de1300c2c0bb88c12b644a4ec0fd7c9b12cb7cc067667bde87"; |
| 40 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 41 | base::FilePath MakeTestFilePath(const char* file) { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 42 | base::FilePath path; |
tommycli | 0409b4df | 2014-08-26 23:31:32 | [diff] [blame] | 43 | PathService::Get(base::DIR_SOURCE_ROOT, &path); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 44 | return path.AppendASCII("components/test/data/update_client") |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 45 | .AppendASCII(file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | } // namespace |
| 49 | |
| 50 | class CrxDownloaderTest : public testing::Test { |
| 51 | public: |
| 52 | CrxDownloaderTest(); |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 53 | ~CrxDownloaderTest() override; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 54 | |
| 55 | // Overrides from testing::Test. |
dcheng | 30a1b154 | 2014-10-29 21:27:50 | [diff] [blame] | 56 | void SetUp() override; |
| 57 | void TearDown() override; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 58 | |
| 59 | void Quit(); |
| 60 | void RunThreads(); |
| 61 | void RunThreadsUntilIdle(); |
| 62 | |
[email protected] | 3cb2a4f | 2013-12-07 21:54:34 | [diff] [blame] | 63 | void DownloadComplete(int crx_context, const CrxDownloader::Result& result); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 64 | |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 65 | void DownloadProgress(int crx_context, const CrxDownloader::Result& result); |
| 66 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 67 | protected: |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 68 | std::unique_ptr<CrxDownloader> crx_downloader_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 69 | |
dcheng | d0fc6aa9 | 2016-04-22 18:03:12 | [diff] [blame] | 70 | std::unique_ptr<GetInterceptor> get_interceptor_; |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 71 | |
[email protected] | 1b6587dc5 | 2014-04-26 00:38:55 | [diff] [blame] | 72 | CrxDownloader::DownloadCallback callback_; |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 73 | CrxDownloader::ProgressCallback progress_callback_; |
[email protected] | 1b6587dc5 | 2014-04-26 00:38:55 | [diff] [blame] | 74 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 75 | int crx_context_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 76 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 77 | int num_download_complete_calls_; |
| 78 | CrxDownloader::Result download_complete_result_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 79 | |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 80 | // These members are updated by DownloadProgress. |
| 81 | int num_progress_calls_; |
| 82 | CrxDownloader::Result download_progress_result_; |
| 83 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 84 | // A magic value for the context to be used in the tests. |
| 85 | static const int kExpectedContext = 0xaabb; |
| 86 | |
| 87 | private: |
Sorin Jianu | 72d8fe0d | 2017-07-11 18:42:16 | [diff] [blame] | 88 | base::test::ScopedTaskEnvironment scoped_task_environment_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 89 | scoped_refptr<net::TestURLRequestContextGetter> context_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 90 | base::Closure quit_closure_; |
| 91 | }; |
| 92 | |
| 93 | const int CrxDownloaderTest::kExpectedContext; |
| 94 | |
| 95 | CrxDownloaderTest::CrxDownloaderTest() |
[email protected] | 1b6587dc5 | 2014-04-26 00:38:55 | [diff] [blame] | 96 | : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete, |
| 97 | base::Unretained(this), |
| 98 | kExpectedContext)), |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 99 | progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress, |
| 100 | base::Unretained(this), |
| 101 | kExpectedContext)), |
[email protected] | 1b6587dc5 | 2014-04-26 00:38:55 | [diff] [blame] | 102 | crx_context_(0), |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 103 | num_download_complete_calls_(0), |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 104 | num_progress_calls_(0), |
Sorin Jianu | 72d8fe0d | 2017-07-11 18:42:16 | [diff] [blame] | 105 | scoped_task_environment_( |
| 106 | base::test::ScopedTaskEnvironment::MainThreadType::IO), |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 107 | context_(new net::TestURLRequestContextGetter( |
Sorin Jianu | 72d8fe0d | 2017-07-11 18:42:16 | [diff] [blame] | 108 | base::ThreadTaskRunnerHandle::Get())) {} |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 109 | |
| 110 | CrxDownloaderTest::~CrxDownloaderTest() { |
[email protected] | 3a0092d | 2013-12-18 03:04:35 | [diff] [blame] | 111 | context_ = NULL; |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 112 | |
| 113 | // The GetInterceptor requires the message loop to run to destruct correctly. |
| 114 | get_interceptor_.reset(); |
| 115 | RunThreadsUntilIdle(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void CrxDownloaderTest::SetUp() { |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 119 | num_download_complete_calls_ = 0; |
| 120 | download_complete_result_ = CrxDownloader::Result(); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 121 | num_progress_calls_ = 0; |
| 122 | download_progress_result_ = CrxDownloader::Result(); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 123 | |
sorin | 9797aba | 2015-04-17 17:15:03 | [diff] [blame] | 124 | // Do not use the background downloader in these tests. |
Sorin Jianu | 72d8fe0d | 2017-07-11 18:42:16 | [diff] [blame] | 125 | crx_downloader_.reset(CrxDownloader::Create(false, context_.get()).release()); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 126 | crx_downloader_->set_progress_callback(progress_callback_); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 127 | |
skyostil | b0daa01 | 2015-06-02 19:03:48 | [diff] [blame] | 128 | get_interceptor_.reset( |
| 129 | new GetInterceptor(base::ThreadTaskRunnerHandle::Get(), |
| 130 | base::ThreadTaskRunnerHandle::Get())); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void CrxDownloaderTest::TearDown() { |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 134 | crx_downloader_.reset(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void CrxDownloaderTest::Quit() { |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 138 | if (!quit_closure_.is_null()) |
| 139 | quit_closure_.Run(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 142 | void CrxDownloaderTest::DownloadComplete(int crx_context, |
| 143 | const CrxDownloader::Result& result) { |
| 144 | ++num_download_complete_calls_; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 145 | crx_context_ = crx_context; |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 146 | download_complete_result_ = result; |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 147 | Quit(); |
| 148 | } |
| 149 | |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 150 | void CrxDownloaderTest::DownloadProgress(int crx_context, |
| 151 | const CrxDownloader::Result& result) { |
| 152 | ++num_progress_calls_; |
| 153 | download_progress_result_ = result; |
| 154 | } |
| 155 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 156 | void 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 | |
| 168 | void CrxDownloaderTest::RunThreadsUntilIdle() { |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame^] | 169 | scoped_task_environment_.RunUntilIdle(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 170 | base::RunLoop().RunUntilIdle(); |
| 171 | } |
| 172 | |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 173 | // Tests that starting a download without a url results in an error. |
| 174 | TEST_F(CrxDownloaderTest, NoUrl) { |
| 175 | std::vector<GURL> urls; |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 176 | crx_downloader_->StartDownload(urls, std::string("abcd"), callback_); |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 177 | RunThreadsUntilIdle(); |
[email protected] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 178 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 179 | EXPECT_EQ(1, num_download_complete_calls_); |
| 180 | EXPECT_EQ(kExpectedContext, crx_context_); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 181 | EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_URL), |
| 182 | download_complete_result_.error); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 183 | 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. |
| 190 | TEST_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_); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 198 | EXPECT_EQ(static_cast<int>(CrxDownloaderError::NO_HASH), |
| 199 | download_complete_result_.error); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 200 | EXPECT_TRUE(download_complete_result_.response.empty()); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 201 | 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] | da37c1d | 2013-12-19 01:04:38 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 206 | // Tests that downloading from one url is successful. |
| 207 | TEST_F(CrxDownloaderTest, OneUrl) { |
| 208 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 209 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 210 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 211 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 212 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 213 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 214 | crx_downloader_->StartDownloadFromUrl(expected_crx_url, |
| 215 | std::string(hash_jebg), callback_); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 216 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 217 | |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 218 | EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 219 | |
| 220 | EXPECT_EQ(1, num_download_complete_calls_); |
| 221 | EXPECT_EQ(kExpectedContext, crx_context_); |
| 222 | EXPECT_EQ(0, download_complete_result_.error); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 223 | EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 224 | EXPECT_EQ(1843, download_complete_result_.total_bytes); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 225 | EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 226 | |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame^] | 227 | EXPECT_TRUE( |
| 228 | DeleteFileAndEmptyParentDirectory(download_complete_result_.response)); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 229 | |
| 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] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 233 | } |
| 234 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 235 | // Tests that downloading from one url fails if the actual hash of the file |
| 236 | // does not match the expected hash. |
| 237 | TEST_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_); |
sorin | 7b865052 | 2016-11-02 18:23:41 | [diff] [blame] | 255 | EXPECT_EQ(static_cast<int>(CrxDownloaderError::BAD_HASH), |
| 256 | download_complete_result_.error); |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 257 | 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] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 267 | // download, and only one download request be made. |
[email protected] | 5bff685d3 | 2014-04-23 00:43:03 | [diff] [blame] | 268 | // 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 |
| 274 | TEST_F(CrxDownloaderTest, MAYBE_TwoUrls) { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 275 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 276 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 277 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 278 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 279 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 280 | |
| 281 | std::vector<GURL> urls; |
| 282 | urls.push_back(expected_crx_url); |
| 283 | urls.push_back(expected_crx_url); |
| 284 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 285 | crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 286 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 287 | |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 288 | EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 289 | |
| 290 | EXPECT_EQ(1, num_download_complete_calls_); |
| 291 | EXPECT_EQ(kExpectedContext, crx_context_); |
| 292 | EXPECT_EQ(0, download_complete_result_.error); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 293 | EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 294 | EXPECT_EQ(1843, download_complete_result_.total_bytes); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 295 | EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 296 | |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame^] | 297 | EXPECT_TRUE( |
| 298 | DeleteFileAndEmptyParentDirectory(download_complete_result_.response)); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 299 | |
| 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] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | // Tests that an invalid host results in a download error. |
| 306 | TEST_F(CrxDownloaderTest, OneUrl_InvalidHost) { |
| 307 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 308 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 309 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 310 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 311 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 312 | |
| 313 | crx_downloader_->StartDownloadFromUrl( |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 314 | GURL("http://no.such.host" |
| 315 | "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"), |
| 316 | std::string(hash_jebg), callback_); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 317 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 318 | |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 319 | EXPECT_EQ(0, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 320 | |
| 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] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | // Tests that an invalid path results in a download error. |
| 328 | TEST_F(CrxDownloaderTest, OneUrl_InvalidPath) { |
| 329 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 330 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 331 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 332 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 333 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 334 | |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 335 | crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"), |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 336 | std::string(hash_jebg), callback_); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 337 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 338 | |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 339 | EXPECT_EQ(0, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 340 | |
| 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] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | // Tests that the fallback to a valid url is successful. |
[email protected] | 5bff685d3 | 2014-04-23 00:43:03 | [diff] [blame] | 348 | // 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 |
| 354 | TEST_F(CrxDownloaderTest, MAYBE_TwoUrls_FirstInvalid) { |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 355 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 356 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 357 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 358 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 359 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 360 | |
| 361 | std::vector<GURL> urls; |
| 362 | urls.push_back(GURL("http://localhost/no/such/file")); |
| 363 | urls.push_back(expected_crx_url); |
| 364 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 365 | crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 366 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 367 | |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 368 | EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 369 | |
| 370 | EXPECT_EQ(1, num_download_complete_calls_); |
| 371 | EXPECT_EQ(kExpectedContext, crx_context_); |
| 372 | EXPECT_EQ(0, download_complete_result_.error); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 373 | EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 374 | EXPECT_EQ(1843, download_complete_result_.total_bytes); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 375 | EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 376 | |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame^] | 377 | EXPECT_TRUE( |
| 378 | DeleteFileAndEmptyParentDirectory(download_complete_result_.response)); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 379 | |
| 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] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 383 | } |
| 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. |
| 387 | TEST_F(CrxDownloaderTest, TwoUrls_SecondInvalid) { |
| 388 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 389 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 390 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 391 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 392 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 393 | |
| 394 | std::vector<GURL> urls; |
| 395 | urls.push_back(expected_crx_url); |
| 396 | urls.push_back(GURL("http://localhost/no/such/file")); |
| 397 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 398 | crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 399 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 400 | |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 401 | EXPECT_EQ(1, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 402 | |
| 403 | EXPECT_EQ(1, num_download_complete_calls_); |
| 404 | EXPECT_EQ(kExpectedContext, crx_context_); |
| 405 | EXPECT_EQ(0, download_complete_result_.error); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 406 | EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); |
| 407 | EXPECT_EQ(1843, download_complete_result_.total_bytes); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 408 | EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); |
| 409 | |
Sorin Jianu | bbd5139 | 2017-07-20 01:48:31 | [diff] [blame^] | 410 | EXPECT_TRUE( |
| 411 | DeleteFileAndEmptyParentDirectory(download_complete_result_.response)); |
[email protected] | 8a5ebd43 | 2014-05-02 00:21:22 | [diff] [blame] | 412 | |
| 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] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | // Tests that the download fails if both urls are bad. |
| 419 | TEST_F(CrxDownloaderTest, TwoUrls_BothInvalid) { |
| 420 | const GURL expected_crx_url = |
[email protected] | d0c8b8b4 | 2014-05-06 05:11:45 | [diff] [blame] | 421 | GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 422 | |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 423 | const base::FilePath test_file(MakeTestFilePath(kTestFileName)); |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 424 | get_interceptor_->SetResponse(expected_crx_url, test_file); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 425 | |
| 426 | std::vector<GURL> urls; |
| 427 | urls.push_back(GURL("http://localhost/no/such/file")); |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 428 | urls.push_back(GURL( |
| 429 | "http://no.such.host/" |
| 430 | "/download/jebgalgnebhfojomionfpkfelancnnkf.crx")); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 431 | |
sorin | 74e7067 | 2016-02-03 03:13:10 | [diff] [blame] | 432 | crx_downloader_->StartDownload(urls, std::string(hash_jebg), callback_); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 433 | RunThreads(); |
[email protected] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 434 | |
tommycli | eaae5d9 | 2014-09-09 06:03:47 | [diff] [blame] | 435 | EXPECT_EQ(0, get_interceptor_->GetHitCount()); |
[email protected] | 148dcfd3 | 2014-04-29 00:54:30 | [diff] [blame] | 436 | |
| 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] | afa378f2 | 2013-12-02 03:37:54 | [diff] [blame] | 441 | } |
| 442 | |
sorin | 52ac088 | 2015-01-24 01:15:00 | [diff] [blame] | 443 | } // namespace update_client |