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