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