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