OLD | NEW |
| (Empty) |
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" | |
6 #include "base/files/file_path.h" | |
7 #include "base/files/file_util.h" | |
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" | |
13 #include "chrome/common/chrome_paths.h" | |
14 #include "components/component_updater/crx_downloader.h" | |
15 #include "content/public/browser/browser_thread.h" | |
16 #include "content/public/test/test_browser_thread_bundle.h" | |
17 #include "content/test/net/url_request_prepackaged_interceptor.h" | |
18 #include "net/base/net_errors.h" | |
19 #include "net/url_request/url_request_test_util.h" | |
20 #include "testing/gtest/include/gtest/gtest.h" | |
21 | |
22 using content::BrowserThread; | |
23 using base::ContentsEqual; | |
24 | |
25 namespace component_updater { | |
26 | |
27 namespace { | |
28 | |
29 // Intercepts HTTP GET requests sent to "localhost". | |
30 typedef content::URLLocalHostRequestPrepackagedInterceptor GetInterceptor; | |
31 | |
32 const char kTestFileName[] = "jebgalgnebhfojomionfpkfelancnnkf.crx"; | |
33 | |
34 base::FilePath MakeTestFilePath(const char* file) { | |
35 base::FilePath path; | |
36 PathService::Get(chrome::DIR_TEST_DATA, &path); | |
37 return path.AppendASCII("components").AppendASCII(file); | |
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 | |
55 void DownloadComplete(int crx_context, const CrxDownloader::Result& result); | |
56 | |
57 void DownloadProgress(int crx_context, const CrxDownloader::Result& result); | |
58 | |
59 protected: | |
60 scoped_ptr<CrxDownloader> crx_downloader_; | |
61 | |
62 CrxDownloader::DownloadCallback callback_; | |
63 CrxDownloader::ProgressCallback progress_callback_; | |
64 | |
65 int crx_context_; | |
66 | |
67 int num_download_complete_calls_; | |
68 CrxDownloader::Result download_complete_result_; | |
69 | |
70 // These members are updated by DownloadProgress. | |
71 int num_progress_calls_; | |
72 CrxDownloader::Result download_progress_result_; | |
73 | |
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() | |
87 : callback_(base::Bind(&CrxDownloaderTest::DownloadComplete, | |
88 base::Unretained(this), | |
89 kExpectedContext)), | |
90 progress_callback_(base::Bind(&CrxDownloaderTest::DownloadProgress, | |
91 base::Unretained(this), | |
92 kExpectedContext)), | |
93 crx_context_(0), | |
94 num_download_complete_calls_(0), | |
95 num_progress_calls_(0), | |
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() { | |
106 context_ = NULL; | |
107 } | |
108 | |
109 void CrxDownloaderTest::SetUp() { | |
110 num_download_complete_calls_ = 0; | |
111 download_complete_result_ = CrxDownloader::Result(); | |
112 num_progress_calls_ = 0; | |
113 download_progress_result_ = CrxDownloader::Result(); | |
114 crx_downloader_.reset(CrxDownloader::Create( | |
115 false, // Do not use the background downloader in these tests. | |
116 context_.get(), | |
117 blocking_task_runner_, | |
118 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE))); | |
119 crx_downloader_->set_progress_callback(progress_callback_); | |
120 } | |
121 | |
122 void CrxDownloaderTest::TearDown() { | |
123 crx_downloader_.reset(); | |
124 } | |
125 | |
126 void CrxDownloaderTest::Quit() { | |
127 if (!quit_closure_.is_null()) | |
128 quit_closure_.Run(); | |
129 } | |
130 | |
131 void CrxDownloaderTest::DownloadComplete(int crx_context, | |
132 const CrxDownloader::Result& result) { | |
133 ++num_download_complete_calls_; | |
134 crx_context_ = crx_context; | |
135 download_complete_result_ = result; | |
136 Quit(); | |
137 } | |
138 | |
139 void CrxDownloaderTest::DownloadProgress(int crx_context, | |
140 const CrxDownloader::Result& result) { | |
141 ++num_progress_calls_; | |
142 download_progress_result_ = result; | |
143 } | |
144 | |
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 | |
161 // Tests that starting a download without a url results in an error. | |
162 TEST_F(CrxDownloaderTest, NoUrl) { | |
163 std::vector<GURL> urls; | |
164 crx_downloader_->StartDownload(urls, callback_); | |
165 RunThreadsUntilIdle(); | |
166 | |
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()); | |
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_); | |
174 } | |
175 | |
176 // Tests that downloading from one url is successful. | |
177 TEST_F(CrxDownloaderTest, OneUrl) { | |
178 const GURL expected_crx_url = | |
179 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
180 | |
181 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
182 GetInterceptor interceptor; | |
183 interceptor.SetResponse(expected_crx_url, test_file); | |
184 | |
185 crx_downloader_->StartDownloadFromUrl(expected_crx_url, callback_); | |
186 RunThreads(); | |
187 | |
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); | |
193 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | |
194 EXPECT_EQ(1843, download_complete_result_.total_bytes); | |
195 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | |
196 | |
197 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | |
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); | |
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. | |
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) { | |
213 const GURL expected_crx_url = | |
214 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
215 | |
216 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
217 GetInterceptor interceptor; | |
218 interceptor.SetResponse(expected_crx_url, test_file); | |
219 | |
220 std::vector<GURL> urls; | |
221 urls.push_back(expected_crx_url); | |
222 urls.push_back(expected_crx_url); | |
223 | |
224 crx_downloader_->StartDownload(urls, callback_); | |
225 RunThreads(); | |
226 | |
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); | |
232 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | |
233 EXPECT_EQ(1843, download_complete_result_.total_bytes); | |
234 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | |
235 | |
236 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | |
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); | |
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 = | |
246 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
247 | |
248 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
249 GetInterceptor interceptor; | |
250 interceptor.SetResponse(expected_crx_url, test_file); | |
251 | |
252 crx_downloader_->StartDownloadFromUrl( | |
253 GURL("http://no.such.host" | |
254 "/download/jebgalgnebhfojomionfpkfelancnnkf.crx"), | |
255 callback_); | |
256 RunThreads(); | |
257 | |
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()); | |
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 = | |
269 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
270 | |
271 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
272 GetInterceptor interceptor; | |
273 interceptor.SetResponse(expected_crx_url, test_file); | |
274 | |
275 crx_downloader_->StartDownloadFromUrl(GURL("http://localhost/no/such/file"), | |
276 callback_); | |
277 RunThreads(); | |
278 | |
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()); | |
285 } | |
286 | |
287 // Tests that the fallback to a valid url is successful. | |
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) { | |
295 const GURL expected_crx_url = | |
296 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
297 | |
298 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
299 GetInterceptor interceptor; | |
300 interceptor.SetResponse(expected_crx_url, test_file); | |
301 | |
302 std::vector<GURL> urls; | |
303 urls.push_back(GURL("http://localhost/no/such/file")); | |
304 urls.push_back(expected_crx_url); | |
305 | |
306 crx_downloader_->StartDownload(urls, callback_); | |
307 RunThreads(); | |
308 | |
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); | |
314 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | |
315 EXPECT_EQ(1843, download_complete_result_.total_bytes); | |
316 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | |
317 | |
318 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | |
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); | |
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 = | |
329 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
330 | |
331 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
332 GetInterceptor interceptor; | |
333 interceptor.SetResponse(expected_crx_url, test_file); | |
334 | |
335 std::vector<GURL> urls; | |
336 urls.push_back(expected_crx_url); | |
337 urls.push_back(GURL("http://localhost/no/such/file")); | |
338 | |
339 crx_downloader_->StartDownload(urls, callback_); | |
340 RunThreads(); | |
341 | |
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); | |
347 EXPECT_EQ(1843, download_complete_result_.downloaded_bytes); | |
348 EXPECT_EQ(1843, download_complete_result_.total_bytes); | |
349 EXPECT_TRUE(ContentsEqual(download_complete_result_.response, test_file)); | |
350 | |
351 EXPECT_TRUE(base::DeleteFile(download_complete_result_.response, false)); | |
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); | |
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 = | |
361 GURL("http://localhost/download/jebgalgnebhfojomionfpkfelancnnkf.crx"); | |
362 | |
363 const base::FilePath test_file(MakeTestFilePath(kTestFileName)); | |
364 GetInterceptor interceptor; | |
365 interceptor.SetResponse(expected_crx_url, test_file); | |
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 | |
372 crx_downloader_->StartDownload(urls, callback_); | |
373 RunThreads(); | |
374 | |
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()); | |
381 } | |
382 | |
383 } // namespace component_updater | |
OLD | NEW |