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

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, 7 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
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..1f7f6f6513c892a1badcdf83455419f1959868d1
--- /dev/null
+++ b/chrome/browser/download/download_prefs_unittest.cc
@@ -0,0 +1,107 @@
+// 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/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, NoAutoOpenForExecutables) {
+ const base::FilePath kDangerousFilePath(FILE_PATH_LITERAL("/b/very-bad.crx"));
+ 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(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.crx"));
+ const base::FilePath kSafeFilePath(
+ FILE_PATH_LITERAL("/good/nothing-wrong.txt"));
+
+ content::TestBrowserThreadBundle threads_are_required_for_testing_profile;
+ TestingProfile profile;
+ // This sets .crx files and .txt files as auto-open file types.
+ profile.GetPrefs()->SetString(prefs::kDownloadExtensionsToOpen, "crx: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,
+ "crx: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.crx"))));
+ EXPECT_TRUE(prefs.IsAutoOpenEnabledBasedOnExtension(
+ base::FilePath(FILE_PATH_LITERAL("x.txt"))));
+ EXPECT_FALSE(prefs.IsAutoOpenEnabledBasedOnExtension(
+ base::FilePath(FILE_PATH_LITERAL("x.foo"))));
+
+ // .crx 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"))));
+}

Powered by Google App Engine
This is Rietveld 408576698