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