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

Side by Side Diff: chrome/browser/download/download_prefs_unittest.cc

Issue 1165893004: [Downloads] Prevent dangerous files from being opened automatically. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use a blacklist instead of including all dangerous file types. Created 5 years, 6 months 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
OLDNEW
(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. If this assumption changes, then we need to
18 // update the tests to match.
19 ASSERT_FALSE(download_util::IsAllowedToOpenAutomatically(
20 base::FilePath(FILE_PATH_LITERAL("a.swf"))));
21 }
22
23 TEST(DownloadPrefsTest, NoAutoOpenForExecutables) {
palmer 2015/06/08 19:05:07 Do we need to do anything special about alternativ
asanka 2015/06/09 00:38:10 Hm. We are making the following assumptions: 1.
24 const base::FilePath kDangerousFilePath(FILE_PATH_LITERAL("/b/very-bad.swf"));
25 const base::FilePath kFileWithNoExtension(FILE_PATH_LITERAL("abcd"));
26
27 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
28 TestingProfile profile;
29 DownloadPrefs prefs(&profile);
30
31 EXPECT_FALSE(prefs.EnableAutoOpenBasedOnExtension(kDangerousFilePath));
32 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kDangerousFilePath));
33 }
34
35 TEST(DownloadPrefsTest, NoAutoOpenForFilesWithNoExtension) {
36 const base::FilePath kFileWithNoExtension(FILE_PATH_LITERAL("abcd"));
37
38 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
39 TestingProfile profile;
40 DownloadPrefs prefs(&profile);
41
42 EXPECT_FALSE(prefs.EnableAutoOpenBasedOnExtension(kFileWithNoExtension));
43 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kFileWithNoExtension));
44 }
45
46 TEST(DownloadPrefsTest, AutoOpenForSafeFiles) {
47 const base::FilePath kSafeFilePath(
48 FILE_PATH_LITERAL("/good/nothing-wrong.txt"));
49 const base::FilePath kAnotherSafeFilePath(
50 FILE_PATH_LITERAL("/ok/not-bad.txt"));
51
52 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
53 TestingProfile profile;
54 DownloadPrefs prefs(&profile);
55
56 EXPECT_TRUE(prefs.EnableAutoOpenBasedOnExtension(kSafeFilePath));
57 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kSafeFilePath));
58 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kAnotherSafeFilePath));
59 }
60
61 TEST(DownloadPrefsTest, AutoOpenPrefSkipsDangerousFileTypesInPrefs) {
62 const base::FilePath kDangerousFilePath(FILE_PATH_LITERAL("/b/very-bad.swf"));
63 const base::FilePath kSafeFilePath(
64 FILE_PATH_LITERAL("/good/nothing-wrong.txt"));
65
66 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
67 TestingProfile profile;
68 // This sets .swf files and .txt files as auto-open file types.
69 profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, "swf:txt");
70 DownloadPrefs prefs(&profile);
71
72 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kDangerousFilePath));
73 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kSafeFilePath));
74 }
75
76 TEST(DownloadPrefsTest, PrefsInitializationSkipsInvalidFileTypes) {
77 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
78 TestingProfile profile;
79 profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen,
80 "swf:txt::.foo:baz");
81 DownloadPrefs prefs(&profile);
82 prefs.DisableAutoOpenBasedOnExtension(
83 base::FilePath(FILE_PATH_LITERAL("x.baz")));
84
85 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(
86 base::FilePath(FILE_PATH_LITERAL("x.swf"))));
87 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
88 base::FilePath(FILE_PATH_LITERAL("x.txt"))));
89 EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(
90 base::FilePath(FILE_PATH_LITERAL("x.foo"))));
91
92 // .swf is skipped because it's not an allowed auto-open file type.
93 // The empty entry and .foo are skipped because they are malformed.
94 // "baz" is removed by the DisableAutoOpenBasedOnExtension() call.
95 // The only entry that should be remaining is 'txt'.
96 EXPECT_STREQ(
97 "txt",
98 profile.GetPrefs()->GetString(prefs::kDownloadExtensionsToOpen).c_str());
99 }
100
101 TEST(DownloadPrefsTest, AutoOpenCheckIsCaseInsensitive) {
102 content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
103 TestingProfile profile;
104 profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen,
105 "txt:Foo:BAR");
106 DownloadPrefs prefs(&profile);
107
108 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
109 base::FilePath(FILE_PATH_LITERAL("x.txt"))));
110 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
111 base::FilePath(FILE_PATH_LITERAL("x.TXT"))));
112 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
113 base::FilePath(FILE_PATH_LITERAL("x.foo"))));
114 EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
115 base::FilePath(FILE_PATH_LITERAL("x.Bar"))));
116 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698