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

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

Powered by Google App Engine
This is Rietveld 408576698