OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "chrome/browser/download/download_prefs.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/prefs/pref_service.h" |
| 9 #include "chrome/browser/download/download_extensions.h" |
| 10 #include "chrome/common/pref_names.h" |
| 11 #include "chrome/test/base/testing_profile.h" |
| 12 #include "content/public/test/test_browser_thread_bundle.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 TEST(DownloadPrefsTest, Prerequisites) { |
| 16 // Most of the tests below are based on the assumption that .swf files are not |
| 17 // allowed to open automatically, and that .txt files are allowed. If this |
| 18 // assumption changes, then we need to update the tests to match. |
| 19 ASSERT_FALSE(download_util::IsAllowedToOpenAutomatically( |
| 20 base::FilePath(FILE_PATH_LITERAL("a.swf")))); |
| 21 ASSERT_TRUE(download_util::IsAllowedToOpenAutomatically( |
| 22 base::FilePath(FILE_PATH_LITERAL("a.txt")))); |
| 23 } |
| 24 |
| 25 TEST(DownloadPrefsTest, NoAutoOpenForDisallowedFileTypes) { |
| 26 const base::FilePath kDangerousFilePath(FILE_PATH_LITERAL("/b/very-bad.swf")); |
| 27 |
| 28 content::TestBrowserThreadBundle threads_are_required_for_testing_profile; |
| 29 TestingProfile profile; |
| 30 DownloadPrefs prefs(&profile); |
| 31 |
| 32 EXPECT_FALSE(prefs.EnableAutoOpenBasedOnExtension(kDangerousFilePath)); |
| 33 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kDangerousFilePath)); |
| 34 } |
| 35 |
| 36 TEST(DownloadPrefsTest, NoAutoOpenForFilesWithNoExtension) { |
| 37 const base::FilePath kFileWithNoExtension(FILE_PATH_LITERAL("abcd")); |
| 38 |
| 39 content::TestBrowserThreadBundle threads_are_required_for_testing_profile; |
| 40 TestingProfile profile; |
| 41 DownloadPrefs prefs(&profile); |
| 42 |
| 43 EXPECT_FALSE(prefs.EnableAutoOpenBasedOnExtension(kFileWithNoExtension)); |
| 44 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kFileWithNoExtension)); |
| 45 } |
| 46 |
| 47 TEST(DownloadPrefsTest, AutoOpenForSafeFiles) { |
| 48 const base::FilePath kSafeFilePath( |
| 49 FILE_PATH_LITERAL("/good/nothing-wrong.txt")); |
| 50 const base::FilePath kAnotherSafeFilePath( |
| 51 FILE_PATH_LITERAL("/ok/not-bad.txt")); |
| 52 |
| 53 content::TestBrowserThreadBundle threads_are_required_for_testing_profile; |
| 54 TestingProfile profile; |
| 55 DownloadPrefs prefs(&profile); |
| 56 |
| 57 EXPECT_TRUE(prefs.EnableAutoOpenBasedOnExtension(kSafeFilePath)); |
| 58 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kSafeFilePath)); |
| 59 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kAnotherSafeFilePath)); |
| 60 } |
| 61 |
| 62 TEST(DownloadPrefsTest, AutoOpenPrefSkipsDangerousFileTypesInPrefs) { |
| 63 const base::FilePath kDangerousFilePath(FILE_PATH_LITERAL("/b/very-bad.swf")); |
| 64 const base::FilePath kSafeFilePath( |
| 65 FILE_PATH_LITERAL("/good/nothing-wrong.txt")); |
| 66 |
| 67 content::TestBrowserThreadBundle threads_are_required_for_testing_profile; |
| 68 TestingProfile profile; |
| 69 // This sets .swf files and .txt files as auto-open file types. |
| 70 profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, "swf:txt"); |
| 71 DownloadPrefs prefs(&profile); |
| 72 |
| 73 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kDangerousFilePath)); |
| 74 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kSafeFilePath)); |
| 75 } |
| 76 |
| 77 TEST(DownloadPrefsTest, PrefsInitializationSkipsInvalidFileTypes) { |
| 78 content::TestBrowserThreadBundle threads_are_required_for_testing_profile; |
| 79 TestingProfile profile; |
| 80 profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, |
| 81 "swf:txt::.foo:baz"); |
| 82 DownloadPrefs prefs(&profile); |
| 83 prefs.DisableAutoOpenBasedOnExtension( |
| 84 base::FilePath(FILE_PATH_LITERAL("x.baz"))); |
| 85 |
| 86 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension( |
| 87 base::FilePath(FILE_PATH_LITERAL("x.swf")))); |
| 88 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension( |
| 89 base::FilePath(FILE_PATH_LITERAL("x.txt")))); |
| 90 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension( |
| 91 base::FilePath(FILE_PATH_LITERAL("x.foo")))); |
| 92 |
| 93 // .swf is skipped because it's not an allowed auto-open file type. |
| 94 // The empty entry and .foo are skipped because they are malformed. |
| 95 // "baz" is removed by the DisableAutoOpenBasedOnExtension() call. |
| 96 // The only entry that should be remaining is 'txt'. |
| 97 EXPECT_STREQ( |
| 98 "txt", |
| 99 profile.GetPrefs()->GetString(prefs::kDownloadExtensionsToOpen).c_str()); |
| 100 } |
| 101 |
| 102 TEST(DownloadPrefsTest, AutoOpenCheckIsCaseInsensitive) { |
| 103 content::TestBrowserThreadBundle threads_are_required_for_testing_profile; |
| 104 TestingProfile profile; |
| 105 profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, |
| 106 "txt:Foo:BAR"); |
| 107 DownloadPrefs prefs(&profile); |
| 108 |
| 109 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension( |
| 110 base::FilePath(FILE_PATH_LITERAL("x.txt")))); |
| 111 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension( |
| 112 base::FilePath(FILE_PATH_LITERAL("x.TXT")))); |
| 113 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension( |
| 114 base::FilePath(FILE_PATH_LITERAL("x.foo")))); |
| 115 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension( |
| 116 base::FilePath(FILE_PATH_LITERAL("x.Bar")))); |
| 117 } |
OLD | NEW |