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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/download/download_prefs.cc ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/download/download_prefs_unittest.cc
diff --git a/chrome/browser/download/download_prefs_unittest.cc b/chrome/browser/download/download_prefs_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..88674145ee0dd68fd22dffa0fac2f936fe5ae536
--- /dev/null
+++ b/chrome/browser/download/download_prefs_unittest.cc
@@ -0,0 +1,117 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/download/download_prefs.h"
+
+#include "base/files/file_path.h"
+#include "base/prefs/pref_service.h"
+#include "chrome/browser/download/download_extensions.h"
+#include "chrome/common/pref_names.h"
+#include "chrome/test/base/testing_profile.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+TEST(DownloadPrefsTest, Prerequisites) {
+ // Most of the tests below are based on the assumption that .swf files are not
+ // allowed to open automatically, and that .txt files are allowed. If this
+ // assumption changes, then we need to update the tests to match.
+ ASSERT_FALSE(download_util::IsAllowedToOpenAutomatically(
+ base::FilePath(FILE_PATH_LITERAL("a.swf"))));
+ ASSERT_TRUE(download_util::IsAllowedToOpenAutomatically(
+ base::FilePath(FILE_PATH_LITERAL("a.txt"))));
+}
+
+TEST(DownloadPrefsTest, NoAutoOpenForDisallowedFileTypes) {
+ const base::FilePath kDangerousFilePath(FILE_PATH_LITERAL("/b/very-bad.swf"));
+
+ content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
+ TestingProfile profile;
+ DownloadPrefs prefs(&profile);
+
+ EXPECT_FALSE(prefs.EnableAutoOpenBasedOnExtension(kDangerousFilePath));
+ EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kDangerousFilePath));
+}
+
+TEST(DownloadPrefsTest, NoAutoOpenForFilesWithNoExtension) {
+ const base::FilePath kFileWithNoExtension(FILE_PATH_LITERAL("abcd"));
+
+ content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
+ TestingProfile profile;
+ DownloadPrefs prefs(&profile);
+
+ EXPECT_FALSE(prefs.EnableAutoOpenBasedOnExtension(kFileWithNoExtension));
+ EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kFileWithNoExtension));
+}
+
+TEST(DownloadPrefsTest, AutoOpenForSafeFiles) {
+ const base::FilePath kSafeFilePath(
+ FILE_PATH_LITERAL("/good/nothing-wrong.txt"));
+ const base::FilePath kAnotherSafeFilePath(
+ FILE_PATH_LITERAL("/ok/not-bad.txt"));
+
+ content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
+ TestingProfile profile;
+ DownloadPrefs prefs(&profile);
+
+ EXPECT_TRUE(prefs.EnableAutoOpenBasedOnExtension(kSafeFilePath));
+ EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kSafeFilePath));
+ EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kAnotherSafeFilePath));
+}
+
+TEST(DownloadPrefsTest, AutoOpenPrefSkipsDangerousFileTypesInPrefs) {
+ const base::FilePath kDangerousFilePath(FILE_PATH_LITERAL("/b/very-bad.swf"));
+ const base::FilePath kSafeFilePath(
+ FILE_PATH_LITERAL("/good/nothing-wrong.txt"));
+
+ content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
+ TestingProfile profile;
+ // This sets .swf files and .txt files as auto-open file types.
+ profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, "swf:txt");
+ DownloadPrefs prefs(&profile);
+
+ EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(kDangerousFilePath));
+ EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(kSafeFilePath));
+}
+
+TEST(DownloadPrefsTest, PrefsInitializationSkipsInvalidFileTypes) {
+ content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
+ TestingProfile profile;
+ profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen,
+ "swf:txt::.foo:baz");
+ DownloadPrefs prefs(&profile);
+ prefs.DisableAutoOpenBasedOnExtension(
+ base::FilePath(FILE_PATH_LITERAL("x.baz")));
+
+ EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(
+ base::FilePath(FILE_PATH_LITERAL("x.swf"))));
+ EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
+ base::FilePath(FILE_PATH_LITERAL("x.txt"))));
+ EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(
+ base::FilePath(FILE_PATH_LITERAL("x.foo"))));
+
+ // .swf is skipped because it's not an allowed auto-open file type.
+ // The empty entry and .foo are skipped because they are malformed.
+ // "baz" is removed by the DisableAutoOpenBasedOnExtension() call.
+ // The only entry that should be remaining is 'txt'.
+ EXPECT_STREQ(
+ "txt",
+ profile.GetPrefs()->GetString(prefs::kDownloadExtensionsToOpen).c_str());
+}
+
+TEST(DownloadPrefsTest, AutoOpenCheckIsCaseInsensitive) {
+ content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
+ TestingProfile profile;
+ profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen,
+ "txt:Foo:BAR");
+ DownloadPrefs prefs(&profile);
+
+ EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
+ base::FilePath(FILE_PATH_LITERAL("x.txt"))));
+ EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
+ base::FilePath(FILE_PATH_LITERAL("x.TXT"))));
+ EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
+ base::FilePath(FILE_PATH_LITERAL("x.foo"))));
+ EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
+ base::FilePath(FILE_PATH_LITERAL("x.Bar"))));
+}
« no previous file with comments | « chrome/browser/download/download_prefs.cc ('k') | chrome/chrome_tests_unit.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698