Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: trunk/src/chrome/browser/download/download_target_determiner_unittest.cc

Issue 61623006: Revert 233460 "Prefer opening PDF downloads in the browser." (Closed) Base URL: svn://svn.chromium.org/chrome/
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/at_exit.h"
6 #include "base/files/file_path.h" 5 #include "base/files/file_path.h"
7 #include "base/files/scoped_temp_dir.h" 6 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
9 #include "base/observer_list.h" 8 #include "base/observer_list.h"
10 #include "base/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
11 #include "base/run_loop.h" 10 #include "base/run_loop.h"
12 #include "base/stl_util.h" 11 #include "base/stl_util.h"
13 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
14 #include "base/value_conversions.h" 13 #include "base/value_conversions.h"
15 #include "chrome/browser/download/chrome_download_manager_delegate.h" 14 #include "chrome/browser/download/chrome_download_manager_delegate.h"
16 #include "chrome/browser/download/download_extensions.h" 15 #include "chrome/browser/download/download_extensions.h"
17 #include "chrome/browser/download/download_prefs.h" 16 #include "chrome/browser/download/download_prefs.h"
18 #include "chrome/browser/download/download_target_determiner.h" 17 #include "chrome/browser/download/download_target_determiner.h"
19 #include "chrome/browser/download/download_target_info.h"
20 #include "chrome/browser/history/history_service.h" 18 #include "chrome/browser/history/history_service.h"
21 #include "chrome/browser/history/history_service_factory.h" 19 #include "chrome/browser/history/history_service_factory.h"
22 #include "chrome/browser/history/history_types.h" 20 #include "chrome/browser/history/history_types.h"
23 #include "chrome/common/extensions/extension.h" 21 #include "chrome/common/extensions/extension.h"
24 #include "chrome/common/pref_names.h" 22 #include "chrome/common/pref_names.h"
25 #include "chrome/test/base/chrome_render_view_host_test_harness.h" 23 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
26 #include "chrome/test/base/testing_pref_service_syncable.h" 24 #include "chrome/test/base/testing_pref_service_syncable.h"
27 #include "chrome/test/base/testing_profile.h" 25 #include "chrome/test/base/testing_profile.h"
28 #include "content/public/browser/download_interrupt_reasons.h" 26 #include "content/public/browser/download_interrupt_reasons.h"
29 #include "content/public/browser/render_process_host.h"
30 #include "content/public/browser/web_contents.h" 27 #include "content/public/browser/web_contents.h"
31 #include "content/public/browser/web_contents_delegate.h" 28 #include "content/public/browser/web_contents_delegate.h"
32 #include "content/public/test/mock_download_item.h" 29 #include "content/public/test/mock_download_item.h"
33 #include "content/public/test/test_browser_thread.h" 30 #include "content/public/test/test_browser_thread.h"
34 #include "content/public/test/test_renderer_host.h" 31 #include "content/public/test/test_renderer_host.h"
35 #include "content/public/test/web_contents_tester.h" 32 #include "content/public/test/web_contents_tester.h"
36 #include "net/base/mime_util.h"
37 #include "testing/gmock/include/gmock/gmock.h" 33 #include "testing/gmock/include/gmock/gmock.h"
38 #include "testing/gtest/include/gtest/gtest.h" 34 #include "testing/gtest/include/gtest/gtest.h"
39 35
40 #if defined(ENABLE_PLUGINS)
41 #include "chrome/browser/plugins/plugin_prefs.h"
42 #include "content/public/browser/plugin_service.h"
43 #include "content/public/common/webplugininfo.h"
44 #endif
45
46 using ::testing::AnyNumber; 36 using ::testing::AnyNumber;
47 using ::testing::Invoke; 37 using ::testing::Invoke;
48 using ::testing::Ref; 38 using ::testing::Ref;
49 using ::testing::Return; 39 using ::testing::Return;
50 using ::testing::ReturnRef; 40 using ::testing::ReturnRef;
51 using ::testing::ReturnRefOfCopy; 41 using ::testing::ReturnRefOfCopy;
52 using ::testing::Truly; 42 using ::testing::Truly;
53 using ::testing::WithArg; 43 using ::testing::WithArg;
54 using ::testing::_; 44 using ::testing::_;
55 using content::DownloadItem; 45 using content::DownloadItem;
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 MOCK_METHOD3(PromptUserForDownloadPath, 136 MOCK_METHOD3(PromptUserForDownloadPath,
147 void(content::DownloadItem*, const base::FilePath&, 137 void(content::DownloadItem*, const base::FilePath&,
148 const FileSelectedCallback&)); 138 const FileSelectedCallback&));
149 MOCK_METHOD3(DetermineLocalPath, 139 MOCK_METHOD3(DetermineLocalPath,
150 void(DownloadItem*, const base::FilePath&, 140 void(DownloadItem*, const base::FilePath&,
151 const LocalPathCallback&)); 141 const LocalPathCallback&));
152 MOCK_METHOD5(ReserveVirtualPath, 142 MOCK_METHOD5(ReserveVirtualPath,
153 void(DownloadItem*, const base::FilePath&, bool, 143 void(DownloadItem*, const base::FilePath&, bool,
154 DownloadPathReservationTracker::FilenameConflictAction, 144 DownloadPathReservationTracker::FilenameConflictAction,
155 const ReservedPathCallback&)); 145 const ReservedPathCallback&));
156 MOCK_METHOD2(GetFileMimeType,
157 void(const base::FilePath&,
158 const GetFileMimeTypeCallback&));
159 146
160 void SetupDefaults() { 147 void SetupDefaults() {
161 ON_CALL(*this, CheckDownloadUrl(_, _, _)) 148 ON_CALL(*this, CheckDownloadUrl(_, _, _))
162 .WillByDefault(WithArg<2>( 149 .WillByDefault(WithArg<2>(
163 ScheduleCallback(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS))); 150 ScheduleCallback(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)));
164 ON_CALL(*this, NotifyExtensions(_, _, _)) 151 ON_CALL(*this, NotifyExtensions(_, _, _))
165 .WillByDefault(WithArg<2>( 152 .WillByDefault(WithArg<2>(
166 ScheduleCallback2(base::FilePath(), 153 ScheduleCallback2(base::FilePath(),
167 DownloadPathReservationTracker::UNIQUIFY))); 154 DownloadPathReservationTracker::UNIQUIFY)));
168 ON_CALL(*this, ReserveVirtualPath(_, _, _, _, _)) 155 ON_CALL(*this, ReserveVirtualPath(_, _, _, _, _))
169 .WillByDefault(Invoke( 156 .WillByDefault(Invoke(
170 &MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath)); 157 &MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath));
171 ON_CALL(*this, PromptUserForDownloadPath(_, _, _)) 158 ON_CALL(*this, PromptUserForDownloadPath(_, _, _))
172 .WillByDefault(Invoke( 159 .WillByDefault(Invoke(
173 &MockDownloadTargetDeterminerDelegate::NullPromptUser)); 160 &MockDownloadTargetDeterminerDelegate::NullPromptUser));
174 ON_CALL(*this, DetermineLocalPath(_, _, _)) 161 ON_CALL(*this, DetermineLocalPath(_, _, _))
175 .WillByDefault(Invoke( 162 .WillByDefault(Invoke(
176 &MockDownloadTargetDeterminerDelegate::NullDetermineLocalPath)); 163 &MockDownloadTargetDeterminerDelegate::NullDetermineLocalPath));
177 ON_CALL(*this, GetFileMimeType(_, _))
178 .WillByDefault(WithArg<1>(
179 ScheduleCallback("")));
180 } 164 }
181 private: 165 private:
182 static void NullReserveVirtualPath( 166 static void NullReserveVirtualPath(
183 DownloadItem* download, 167 DownloadItem* download,
184 const base::FilePath& virtual_path, 168 const base::FilePath& virtual_path,
185 bool create_directory, 169 bool create_directory,
186 DownloadPathReservationTracker::FilenameConflictAction conflict_action, 170 DownloadPathReservationTracker::FilenameConflictAction conflict_action,
187 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback); 171 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback);
188 static void NullPromptUser( 172 static void NullPromptUser(
189 DownloadItem* download, const base::FilePath& suggested_path, 173 DownloadItem* download, const base::FilePath& suggested_path,
(...skipping 25 matching lines...) Expand all
215 199
216 // Given the relative path |path|, returns the full path under the temporary 200 // Given the relative path |path|, returns the full path under the temporary
217 // downloads directory. 201 // downloads directory.
218 base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path); 202 base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path);
219 203
220 // Run |test_case| using |item|. 204 // Run |test_case| using |item|.
221 void RunTestCase(const DownloadTestCase& test_case, 205 void RunTestCase(const DownloadTestCase& test_case,
222 const base::FilePath& initial_virtual_path, 206 const base::FilePath& initial_virtual_path,
223 content::MockDownloadItem* item); 207 content::MockDownloadItem* item);
224 208
225 // Runs |test_case| with |item|. When the DownloadTargetDeterminer is done,
226 // returns the resulting DownloadTargetInfo.
227 scoped_ptr<DownloadTargetInfo> RunDownloadTargetDeterminer(
228 const base::FilePath& initial_virtual_path,
229 content::MockDownloadItem* item);
230
231 // Run through |test_case_count| tests in |test_cases|. A new MockDownloadItem 209 // Run through |test_case_count| tests in |test_cases|. A new MockDownloadItem
232 // will be created for each test case and destroyed when the test case is 210 // will be created for each test case and destroyed when the test case is
233 // complete. 211 // complete.
234 void RunTestCasesWithActiveItem(const DownloadTestCase test_cases[], 212 void RunTestCasesWithActiveItem(const DownloadTestCase test_cases[],
235 size_t test_case_count); 213 size_t test_case_count);
236 214
237 // Verifies that |target_path|, |disposition|, |expected_danger_type| and 215 // Verifies that |target_path|, |disposition|, |expected_danger_type| and
238 // |intermediate_path| matches the expectations of |test_case|. Posts 216 // |intermediate_path| matches the expectations of |test_case|. Posts
239 // |closure| to the current message loop when done. 217 // |closure| to the current message loop when done.
240 void VerifyDownloadTarget(const DownloadTestCase& test_case, 218 void DownloadTargetVerifier(const base::Closure& closure,
241 const DownloadTargetInfo* target_info); 219 const DownloadTestCase& test_case,
220 const base::FilePath& local_path,
221 DownloadItem::TargetDisposition disposition,
222 content::DownloadDangerType danger_type,
223 const base::FilePath& intermediate_path);
242 224
243 const base::FilePath& test_download_dir() const { 225 const base::FilePath& test_download_dir() const {
244 return test_download_dir_.path(); 226 return test_download_dir_.path();
245 } 227 }
246 228
247 const base::FilePath& test_virtual_dir() const { 229 const base::FilePath& test_virtual_dir() const {
248 return test_virtual_dir_; 230 return test_virtual_dir_;
249 } 231 }
250 232
251 MockDownloadTargetDeterminerDelegate* delegate() { 233 MockDownloadTargetDeterminerDelegate* delegate() {
252 return &delegate_; 234 return &delegate_;
253 } 235 }
254 236
255 DownloadPrefs* download_prefs() { 237 DownloadPrefs* download_prefs() {
256 return download_prefs_.get(); 238 return download_prefs_.get();
257 } 239 }
258 240
259 private: 241 private:
260 scoped_ptr<DownloadPrefs> download_prefs_; 242 scoped_ptr<DownloadPrefs> download_prefs_;
261 ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_; 243 ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_;
262 NullWebContentsDelegate web_contents_delegate_; 244 NullWebContentsDelegate web_contents_delegate_;
263 base::ScopedTempDir test_download_dir_; 245 base::ScopedTempDir test_download_dir_;
264 base::FilePath test_virtual_dir_; 246 base::FilePath test_virtual_dir_;
265 }; 247 };
266 248
267 void DownloadTargetDeterminerTest::SetUp() { 249 void DownloadTargetDeterminerTest::SetUp() {
268 SetThreadBundleOptions(content::TestBrowserThreadBundle::REAL_IO_THREAD);
269 ChromeRenderViewHostTestHarness::SetUp(); 250 ChromeRenderViewHostTestHarness::SetUp();
270 CHECK(profile()); 251 CHECK(profile());
271 download_prefs_.reset(new DownloadPrefs(profile())); 252 download_prefs_.reset(new DownloadPrefs(profile()));
272 web_contents()->SetDelegate(&web_contents_delegate_); 253 web_contents()->SetDelegate(&web_contents_delegate_);
273 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); 254 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir());
274 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual")); 255 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual"));
275 download_prefs_->SetDownloadPath(test_download_dir()); 256 download_prefs_->SetDownloadPath(test_download_dir());
276 delegate_.SetupDefaults(); 257 delegate_.SetupDefaults();
277 #if defined(ENABLE_PLUGINS)
278 content::PluginService::GetInstance()->Init();
279 content::PluginService::GetInstance()->DisablePluginsDiscoveryForTesting();
280 #endif
281 } 258 }
282 259
283 void DownloadTargetDeterminerTest::TearDown() { 260 void DownloadTargetDeterminerTest::TearDown() {
284 download_prefs_.reset(); 261 download_prefs_.reset();
285 ChromeRenderViewHostTestHarness::TearDown(); 262 ChromeRenderViewHostTestHarness::TearDown();
286 } 263 }
287 264
288 content::MockDownloadItem* 265 content::MockDownloadItem*
289 DownloadTargetDeterminerTest::CreateActiveDownloadItem( 266 DownloadTargetDeterminerTest::CreateActiveDownloadItem(
290 int32 id, 267 int32 id,
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 if (relative_path.empty()) 343 if (relative_path.empty())
367 return base::FilePath(); 344 return base::FilePath();
368 base::FilePath full_path(test_download_dir().Append(relative_path)); 345 base::FilePath full_path(test_download_dir().Append(relative_path));
369 return full_path.NormalizePathSeparators(); 346 return full_path.NormalizePathSeparators();
370 } 347 }
371 348
372 void DownloadTargetDeterminerTest::RunTestCase( 349 void DownloadTargetDeterminerTest::RunTestCase(
373 const DownloadTestCase& test_case, 350 const DownloadTestCase& test_case,
374 const base::FilePath& initial_virtual_path, 351 const base::FilePath& initial_virtual_path,
375 content::MockDownloadItem* item) { 352 content::MockDownloadItem* item) {
376 scoped_ptr<DownloadTargetInfo> target_info = 353 // Kick off the test.
377 RunDownloadTargetDeterminer(initial_virtual_path, item); 354 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this);
378 VerifyDownloadTarget(test_case, target_info.get());
379 }
380
381 void CompletionCallbackWrapper(
382 const base::Closure& closure,
383 scoped_ptr<DownloadTargetInfo>* target_info_receiver,
384 scoped_ptr<DownloadTargetInfo> target_info) {
385 target_info_receiver->swap(target_info);
386 base::MessageLoop::current()->PostTask(FROM_HERE, closure);
387 }
388
389 scoped_ptr<DownloadTargetInfo>
390 DownloadTargetDeterminerTest::RunDownloadTargetDeterminer(
391 const base::FilePath& initial_virtual_path,
392 content::MockDownloadItem* item) {
393 scoped_ptr<DownloadTargetInfo> target_info;
394 base::RunLoop run_loop; 355 base::RunLoop run_loop;
395 DownloadTargetDeterminer::Start( 356 DownloadTargetDeterminer::Start(
396 item, initial_virtual_path, download_prefs_.get(), delegate(), 357 item, initial_virtual_path, download_prefs_.get(), delegate(),
397 base::Bind(&CompletionCallbackWrapper, 358 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier,
398 run_loop.QuitClosure(), 359 factory.GetWeakPtr(), run_loop.QuitClosure(), test_case));
399 &target_info));
400 run_loop.Run(); 360 run_loop.Run();
401 ::testing::Mock::VerifyAndClearExpectations(delegate()); 361 ::testing::Mock::VerifyAndClearExpectations(delegate());
402 return target_info.Pass();
403 } 362 }
404 363
405 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem( 364 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem(
406 const DownloadTestCase test_cases[], 365 const DownloadTestCase test_cases[],
407 size_t test_case_count) { 366 size_t test_case_count) {
408 for (size_t i = 0; i < test_case_count; ++i) { 367 for (size_t i = 0; i < test_case_count; ++i) {
409 scoped_ptr<content::MockDownloadItem> item( 368 scoped_ptr<content::MockDownloadItem> item(
410 CreateActiveDownloadItem(i, test_cases[i])); 369 CreateActiveDownloadItem(i, test_cases[i]));
411 SCOPED_TRACE(testing::Message() << "Running test case " << i); 370 SCOPED_TRACE(testing::Message() << "Running test case " << i);
412 RunTestCase(test_cases[i], base::FilePath(), item.get()); 371 RunTestCase(test_cases[i], base::FilePath(), item.get());
413 } 372 }
414 } 373 }
415 374
416 void DownloadTargetDeterminerTest::VerifyDownloadTarget( 375 void DownloadTargetDeterminerTest::DownloadTargetVerifier(
376 const base::Closure& closure,
417 const DownloadTestCase& test_case, 377 const DownloadTestCase& test_case,
418 const DownloadTargetInfo* target_info) { 378 const base::FilePath& local_path,
379 DownloadItem::TargetDisposition disposition,
380 content::DownloadDangerType danger_type,
381 const base::FilePath& intermediate_path) {
419 base::FilePath expected_local_path( 382 base::FilePath expected_local_path(
420 GetPathInDownloadDir(test_case.expected_local_path)); 383 GetPathInDownloadDir(test_case.expected_local_path));
421 EXPECT_EQ(expected_local_path.value(), target_info->target_path.value()); 384 EXPECT_EQ(expected_local_path.value(), local_path.value());
422 EXPECT_EQ(test_case.expected_disposition, target_info->target_disposition); 385 EXPECT_EQ(test_case.expected_disposition, disposition);
423 EXPECT_EQ(test_case.expected_danger_type, target_info->danger_type); 386 EXPECT_EQ(test_case.expected_danger_type, danger_type);
424 387
425 switch (test_case.expected_intermediate) { 388 switch (test_case.expected_intermediate) {
426 case EXPECT_CRDOWNLOAD: 389 case EXPECT_CRDOWNLOAD:
427 EXPECT_EQ(DownloadTargetDeterminer::GetCrDownloadPath( 390 EXPECT_EQ(DownloadTargetDeterminer::GetCrDownloadPath(local_path).value(),
428 target_info->target_path).value(), 391 intermediate_path.value());
429 target_info->intermediate_path.value());
430 break; 392 break;
431 393
432 case EXPECT_UNCONFIRMED: 394 case EXPECT_UNCONFIRMED:
433 // The paths (in English) look like: /path/Unconfirmed xxx.crdownload. 395 // The paths (in English) look like: /path/Unconfirmed xxx.crdownload.
434 // Of this, we only check that the path is: 396 // Of this, we only check that the path is:
435 // 1. Not "/path/target.crdownload", 397 // 1. Not "/path/target.crdownload",
436 // 2. Points to the same directory as the target. 398 // 2. Points to the same directory as the target.
437 // 3. Has extension ".crdownload". 399 // 3. Has extension ".crdownload".
438 // 4. Basename starts with "Unconfirmed ". 400 // 4. Basename starts with "Unconfirmed ".
439 EXPECT_NE(DownloadTargetDeterminer::GetCrDownloadPath(expected_local_path) 401 EXPECT_NE(DownloadTargetDeterminer::GetCrDownloadPath(expected_local_path)
440 .value(), 402 .value(),
441 target_info->intermediate_path.value()); 403 intermediate_path.value());
442 EXPECT_EQ(expected_local_path.DirName().value(), 404 EXPECT_EQ(expected_local_path.DirName().value(),
443 target_info->intermediate_path.DirName().value()); 405 intermediate_path.DirName().value());
444 EXPECT_TRUE(target_info->intermediate_path.MatchesExtension( 406 EXPECT_TRUE(intermediate_path.MatchesExtension(
445 FILE_PATH_LITERAL(".crdownload"))); 407 FILE_PATH_LITERAL(".crdownload")));
446 EXPECT_EQ(0u, 408 EXPECT_EQ(0u, intermediate_path.BaseName().value().find(
447 target_info->intermediate_path.BaseName().value().find( 409 FILE_PATH_LITERAL("Unconfirmed ")));
448 FILE_PATH_LITERAL("Unconfirmed ")));
449 break; 410 break;
450 411
451 case EXPECT_LOCAL_PATH: 412 case EXPECT_LOCAL_PATH:
452 EXPECT_EQ(expected_local_path.value(), 413 EXPECT_EQ(expected_local_path.value(), intermediate_path.value());
453 target_info->intermediate_path.value());
454 break; 414 break;
455 } 415 }
416 base::MessageLoop::current()->PostTask(FROM_HERE, closure);
456 } 417 }
457 418
458 // static 419 // static
459 void MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath( 420 void MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath(
460 DownloadItem* download, 421 DownloadItem* download,
461 const base::FilePath& virtual_path, 422 const base::FilePath& virtual_path,
462 bool create_directory, 423 bool create_directory,
463 DownloadPathReservationTracker::FilenameConflictAction conflict_action, 424 DownloadPathReservationTracker::FilenameConflictAction conflict_action,
464 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback) { 425 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback) {
465 callback.Run(virtual_path, true); 426 callback.Run(virtual_path, true);
(...skipping 1270 matching lines...) Expand 10 before | Expand all | Expand 10 after
1736 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) 1697 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1737 .Times(test_case.test_type == AUTOMATIC ? 1 : 0); 1698 .Times(test_case.test_type == AUTOMATIC ? 1 : 0);
1738 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _)); 1699 EXPECT_CALL(*delegate(), ReserveVirtualPath(_, expected_path, false, _, _));
1739 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _)); 1700 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, expected_path, _));
1740 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _)); 1701 EXPECT_CALL(*delegate(), DetermineLocalPath(_, expected_path, _));
1741 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _)); 1702 EXPECT_CALL(*delegate(), CheckDownloadUrl(_, expected_path, _));
1742 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get()); 1703 RunTestCase(test_case, GetPathInDownloadDir(kInitialPath), item.get());
1743 } 1704 }
1744 } 1705 }
1745 1706
1707 // Used with TargetDeterminer_IntermediateNameForResumed test. Verifies that
1708 // |intermediate_path| == |expected_intermediate_path| if the latter is
1709 // non-empty.
1710 void IntermediatePathVerifier(
1711 const base::FilePath& expected_intermediate_path,
1712 const content::DownloadTargetCallback& callback,
1713 const base::FilePath& target_path,
1714 content::DownloadItem::TargetDisposition disposition,
1715 content::DownloadDangerType danger_type,
1716 const base::FilePath& intermediate_path) {
1717 if (!expected_intermediate_path.empty())
1718 EXPECT_EQ(expected_intermediate_path, intermediate_path);
1719 callback.Run(target_path, disposition, danger_type, intermediate_path);
1720 }
1721
1746 // Test intermediate filename generation for resumed downloads. 1722 // Test intermediate filename generation for resumed downloads.
1747 TEST_F(DownloadTargetDeterminerTest, 1723 TEST_F(DownloadTargetDeterminerTest,
1748 TargetDeterminer_IntermediateNameForResumed) { 1724 TargetDeterminer_IntermediateNameForResumed) {
1749 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital 1725 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
1750 // path. 1726 // path.
1751 const base::FilePath::CharType kInitialPath[] = 1727 const base::FilePath::CharType kInitialPath[] =
1752 FILE_PATH_LITERAL("some_path/bar.txt"); 1728 FILE_PATH_LITERAL("some_path/bar.txt");
1753 1729
1754 struct IntermediateNameTestCase { 1730 struct IntermediateNameTestCase {
1755 // General test case settings. 1731 // General test case settings.
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
1872 1848
1873 ON_CALL(*item.get(), GetLastReason()) 1849 ON_CALL(*item.get(), GetLastReason())
1874 .WillByDefault(Return( 1850 .WillByDefault(Return(
1875 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED)); 1851 content::DOWNLOAD_INTERRUPT_REASON_NETWORK_FAILED));
1876 ON_CALL(*item.get(), GetFullPath()) 1852 ON_CALL(*item.get(), GetFullPath())
1877 .WillByDefault(ReturnRefOfCopy( 1853 .WillByDefault(ReturnRefOfCopy(
1878 GetPathInDownloadDir(test_case.initial_intermediate_path))); 1854 GetPathInDownloadDir(test_case.initial_intermediate_path)));
1879 ON_CALL(*item.get(), GetDangerType()) 1855 ON_CALL(*item.get(), GetDangerType())
1880 .WillByDefault(Return(test_case.general.expected_danger_type)); 1856 .WillByDefault(Return(test_case.general.expected_danger_type));
1881 1857
1882 scoped_ptr<DownloadTargetInfo> target_info = 1858 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this);
1883 RunDownloadTargetDeterminer(GetPathInDownloadDir(kInitialPath), 1859 base::RunLoop run_loop;
1884 item.get()); 1860 content::DownloadTargetCallback verifier_callback =
1885 VerifyDownloadTarget(test_case.general, target_info.get()); 1861 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier,
1886 base::FilePath expected_intermediate_path = 1862 factory.GetWeakPtr(),
1887 GetPathInDownloadDir(test_case.expected_intermediate_path); 1863 run_loop.QuitClosure(),
1888 if (!expected_intermediate_path.empty()) 1864 test_case.general);
1889 EXPECT_EQ(expected_intermediate_path, target_info->intermediate_path); 1865 content::DownloadTargetCallback test_callback =
1866 base::Bind(&IntermediatePathVerifier,
1867 GetPathInDownloadDir(test_case.expected_intermediate_path),
1868 verifier_callback);
1869 DownloadTargetDeterminer::Start(item.get(),
1870 GetPathInDownloadDir(kInitialPath),
1871 download_prefs(),
1872 delegate(),
1873 test_callback);
1874 run_loop.Run();
1875 ::testing::Mock::VerifyAndClearExpectations(delegate());
1890 } 1876 }
1891 } 1877 }
1892 1878
1893 // Test MIME type determination based on the target filename.
1894 TEST_F(DownloadTargetDeterminerTest,
1895 TargetDeterminer_MIMETypeDetermination) {
1896 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
1897 // path.
1898 const base::FilePath::CharType kInitialPath[] =
1899 FILE_PATH_LITERAL("some_path/bar.txt");
1900
1901 struct MIMETypeTestCase {
1902 // General test case settings.
1903 DownloadTestCase general;
1904
1905 // Expected MIME type for test case.
1906 const char* expected_mime_type;
1907 } kMIMETypeTestCases[] = {
1908 {
1909 {
1910 // 0:
1911 AUTOMATIC,
1912 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1913 "http://example.com/foo.png", "image/png",
1914 FILE_PATH_LITERAL(""),
1915
1916 FILE_PATH_LITERAL(""),
1917 FILE_PATH_LITERAL("foo.png"),
1918 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1919
1920 EXPECT_CRDOWNLOAD
1921 },
1922 "image/png"
1923 },
1924 {
1925 {
1926 // 1: Empty MIME type in response.
1927 AUTOMATIC,
1928 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1929 "http://example.com/foo.png", "",
1930 FILE_PATH_LITERAL(""),
1931
1932 FILE_PATH_LITERAL(""),
1933 FILE_PATH_LITERAL("foo.png"),
1934 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1935
1936 EXPECT_CRDOWNLOAD
1937 },
1938 "image/png"
1939 },
1940 {
1941 {
1942 // 2: Forced path.
1943 FORCED,
1944 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1945 "http://example.com/foo.abc", "",
1946 FILE_PATH_LITERAL("foo.png"),
1947
1948 FILE_PATH_LITERAL(""),
1949 FILE_PATH_LITERAL("foo.png"),
1950 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1951
1952 EXPECT_CRDOWNLOAD
1953 },
1954 "image/png"
1955 },
1956 {
1957 {
1958 // 3: Unknown file type.
1959 AUTOMATIC,
1960 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1961 "http://example.com/foo.notarealext", "",
1962 FILE_PATH_LITERAL(""),
1963
1964 FILE_PATH_LITERAL(""),
1965 FILE_PATH_LITERAL("foo.notarealext"),
1966 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1967
1968 EXPECT_CRDOWNLOAD
1969 },
1970 ""
1971 },
1972 {
1973 {
1974 // 4: Unknown file type.
1975 AUTOMATIC,
1976 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1977 "http://example.com/foo.notarealext", "image/png",
1978 FILE_PATH_LITERAL(""),
1979
1980 FILE_PATH_LITERAL(""),
1981 FILE_PATH_LITERAL("foo.notarealext"),
1982 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1983
1984 EXPECT_CRDOWNLOAD
1985 },
1986 ""
1987 },
1988 };
1989
1990 ON_CALL(*delegate(), GetFileMimeType(
1991 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.png")), _))
1992 .WillByDefault(WithArg<1>(
1993 ScheduleCallback("image/png")));
1994
1995 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kMIMETypeTestCases); ++i) {
1996 SCOPED_TRACE(testing::Message() << "Running test case " << i);
1997 const MIMETypeTestCase& test_case = kMIMETypeTestCases[i];
1998 scoped_ptr<content::MockDownloadItem> item(
1999 CreateActiveDownloadItem(i, test_case.general));
2000 scoped_ptr<DownloadTargetInfo> target_info =
2001 RunDownloadTargetDeterminer(GetPathInDownloadDir(kInitialPath),
2002 item.get());
2003 EXPECT_EQ(test_case.expected_mime_type, target_info->mime_type);
2004 }
2005 }
2006
2007 #if defined(ENABLE_PLUGINS)
2008
2009 void DummyGetPluginsCallback(
2010 const base::Closure& closure,
2011 const std::vector<content::WebPluginInfo>& plugins) {
2012 closure.Run();
2013 }
2014
2015 void ForceRefreshOfPlugins() {
2016 #if !defined(OS_WIN)
2017 // Prevent creation of a utility process for loading plugins. Doing so breaks
2018 // unit_tests since /proc/self/exe can't be run as a utility process.
2019 content::RenderProcessHost::SetRunRendererInProcess(true);
2020 #endif
2021 base::RunLoop run_loop;
2022 content::PluginService::GetInstance()->GetPlugins(
2023 base::Bind(&DummyGetPluginsCallback, run_loop.QuitClosure()));
2024 run_loop.Run();
2025 #if !defined(OS_WIN)
2026 content::RenderProcessHost::SetRunRendererInProcess(false);
2027 #endif
2028 }
2029
2030 void PluginEnabledCallback(const base::Closure& closure,
2031 bool result) {
2032 EXPECT_TRUE(result);
2033 closure.Run();
2034 }
2035
2036 void EnablePlugin(bool enable, PluginPrefs* prefs, const base::FilePath& path) {
2037 base::RunLoop run_loop;
2038 prefs->EnablePlugin(enable, path,
2039 base::Bind(&PluginEnabledCallback,
2040 run_loop.QuitClosure()));
2041 run_loop.Run();
2042 }
2043
2044 class ScopedRegisterInternalPlugin {
2045 public:
2046 ScopedRegisterInternalPlugin(content::PluginService* plugin_service,
2047 content::WebPluginInfo::PluginType type,
2048 const base::FilePath& path,
2049 const char* mime_type,
2050 const char* extension)
2051 : plugin_service_(plugin_service),
2052 plugin_path_(path) {
2053 content::WebPluginMimeType plugin_mime_type(mime_type,
2054 extension,
2055 "Test file");
2056 content::WebPluginInfo plugin_info(base::string16(),
2057 path,
2058 base::string16(),
2059 base::string16());
2060 plugin_info.mime_types.push_back(plugin_mime_type);
2061 plugin_info.type = type;
2062
2063 plugin_service->RegisterInternalPlugin(plugin_info, true);
2064 plugin_service->RefreshPlugins();
2065 ForceRefreshOfPlugins();
2066 }
2067
2068 ~ScopedRegisterInternalPlugin() {
2069 plugin_service_->UnregisterInternalPlugin(plugin_path_);
2070 plugin_service_->RefreshPlugins();
2071 ForceRefreshOfPlugins();
2072 }
2073
2074 const base::FilePath& path() { return plugin_path_; }
2075
2076 private:
2077 content::PluginService* plugin_service_;
2078 base::FilePath plugin_path_;
2079 };
2080
2081 // Check if secure handling of filetypes is determined correctly for PPAPI
2082 // plugins.
2083 TEST_F(DownloadTargetDeterminerTest,
2084 TargetDeterminer_CheckForSecureHandling_PPAPI) {
2085 // The ShadowingAtExitManager destroys the tainted PluginService instance.
2086 base::ShadowingAtExitManager at_exit_manager;
2087
2088 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
2089 // path.
2090 const base::FilePath::CharType kInitialPath[] =
2091 FILE_PATH_LITERAL("some_path/bar.txt");
2092 const char kTestMIMEType[] = "application/x-example-should-not-exist";
2093
2094 DownloadTestCase kSecureHandlingTestCase = {
2095 AUTOMATIC,
2096 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2097 "http://example.com/foo.fakeext", "",
2098 FILE_PATH_LITERAL(""),
2099
2100 FILE_PATH_LITERAL(""),
2101 FILE_PATH_LITERAL("foo.fakeext"),
2102 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
2103
2104 EXPECT_CRDOWNLOAD
2105 };
2106
2107 content::PluginService* plugin_service =
2108 content::PluginService::GetInstance();
2109 // This creates a PluginPrefs for our TestingProfile.
2110 scoped_refptr<PluginPrefs> plugin_prefs =
2111 PluginPrefs::GetForTestingProfile(profile());
2112
2113 // Verify our test assumptions.
2114 {
2115 ForceRefreshOfPlugins();
2116 std::vector<content::WebPluginInfo> info;
2117 ASSERT_FALSE(plugin_service->GetPluginInfoArray(
2118 GURL(), kTestMIMEType, false, &info, NULL));
2119 ASSERT_EQ(0u, info.size())
2120 << "Name: " << info[0].name << ", Path: " << info[0].path.value();
2121 }
2122
2123 ON_CALL(*delegate(), GetFileMimeType(
2124 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.fakeext")), _))
2125 .WillByDefault(WithArg<1>(
2126 ScheduleCallback(kTestMIMEType)));
2127 scoped_ptr<content::MockDownloadItem> item(
2128 CreateActiveDownloadItem(1, kSecureHandlingTestCase));
2129 scoped_ptr<DownloadTargetInfo> target_info =
2130 RunDownloadTargetDeterminer(GetPathInDownloadDir(kInitialPath),
2131 item.get());
2132 EXPECT_FALSE(target_info->is_filetype_handled_securely);
2133
2134 // Register a PPAPI plugin. This should count as handling the filetype
2135 // securely.
2136 ScopedRegisterInternalPlugin ppapi_plugin(
2137 plugin_service,
2138 content::WebPluginInfo::PLUGIN_TYPE_PEPPER_OUT_OF_PROCESS,
2139 test_download_dir().AppendASCII("ppapi"),
2140 kTestMIMEType,
2141 "fakeext");
2142
2143 target_info = RunDownloadTargetDeterminer(
2144 GetPathInDownloadDir(kInitialPath), item.get());
2145 EXPECT_TRUE(target_info->is_filetype_handled_securely);
2146
2147 // Try disabling the plugin. Handling should no longer be considered secure.
2148 EnablePlugin(false, plugin_prefs, ppapi_plugin.path());
2149 target_info = RunDownloadTargetDeterminer(
2150 GetPathInDownloadDir(kInitialPath), item.get());
2151 EXPECT_FALSE(target_info->is_filetype_handled_securely);
2152
2153 // Now register an unsandboxed PPAPI plug-in. This plugin should not be
2154 // considered secure.
2155 ScopedRegisterInternalPlugin ppapi_unsandboxed_plugin(
2156 plugin_service,
2157 content::WebPluginInfo::PLUGIN_TYPE_PEPPER_UNSANDBOXED,
2158 test_download_dir().AppendASCII("ppapi-nosandbox"),
2159 kTestMIMEType,
2160 "fakeext");
2161
2162 target_info = RunDownloadTargetDeterminer(
2163 GetPathInDownloadDir(kInitialPath), item.get());
2164 EXPECT_FALSE(target_info->is_filetype_handled_securely);
2165 }
2166
2167 // Check if secure handling of filetypes is determined correctly for NPAPI
2168 // plugins.
2169 TEST_F(DownloadTargetDeterminerTest,
2170 TargetDeterminer_CheckForSecureHandling_NPAPI) {
2171 // The ShadowingAtExitManager destroys the tainted PluginService instance.
2172 base::ShadowingAtExitManager at_exit_manager;
2173
2174 // All test cases run with GetPathInDownloadDir(kInitialPath) as the inital
2175 // path.
2176 const base::FilePath::CharType kInitialPath[] =
2177 FILE_PATH_LITERAL("some_path/bar.txt");
2178 const char kTestMIMEType[] = "application/x-example-should-not-exist";
2179
2180 DownloadTestCase kSecureHandlingTestCase = {
2181 AUTOMATIC,
2182 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
2183 "http://example.com/foo.fakeext", "",
2184 FILE_PATH_LITERAL(""),
2185
2186 FILE_PATH_LITERAL(""),
2187 FILE_PATH_LITERAL("foo.fakeext"),
2188 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
2189
2190 EXPECT_CRDOWNLOAD
2191 };
2192
2193 content::PluginService* plugin_service =
2194 content::PluginService::GetInstance();
2195
2196 // Can't run this test if NPAPI isn't supported.
2197 if (!plugin_service->NPAPIPluginsSupported())
2198 return;
2199
2200 // This creates a PluginPrefs for our TestingProfile.
2201 scoped_refptr<PluginPrefs> plugin_prefs =
2202 PluginPrefs::GetForTestingProfile(profile());
2203
2204 // Verify our test assumptions.
2205 {
2206 ForceRefreshOfPlugins();
2207 std::vector<content::WebPluginInfo> info;
2208 ASSERT_FALSE(plugin_service->GetPluginInfoArray(
2209 GURL(), kTestMIMEType, false, &info, NULL));
2210 ASSERT_EQ(0u, info.size())
2211 << "Name: " << info[0].name << ", Path: " << info[0].path.value();
2212 }
2213
2214 ON_CALL(*delegate(), GetFileMimeType(
2215 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.fakeext")), _))
2216 .WillByDefault(WithArg<1>(
2217 ScheduleCallback(kTestMIMEType)));
2218 scoped_ptr<content::MockDownloadItem> item(
2219 CreateActiveDownloadItem(1, kSecureHandlingTestCase));
2220 scoped_ptr<DownloadTargetInfo> target_info =
2221 RunDownloadTargetDeterminer(GetPathInDownloadDir(kInitialPath),
2222 item.get());
2223 EXPECT_FALSE(target_info->is_filetype_handled_securely);
2224
2225 // Register a NPAPI plugin. This should not count as handling the filetype
2226 // securely.
2227 ScopedRegisterInternalPlugin npapi_plugin(
2228 plugin_service,
2229 content::WebPluginInfo::PLUGIN_TYPE_NPAPI,
2230 test_download_dir().AppendASCII("npapi"),
2231 kTestMIMEType,
2232 "fakeext");
2233
2234 target_info = RunDownloadTargetDeterminer(
2235 GetPathInDownloadDir(kInitialPath), item.get());
2236 EXPECT_FALSE(target_info->is_filetype_handled_securely);
2237 }
2238 #endif // ENABLE_PLUGINS
2239
2240 } // namespace 1879 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698