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

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

Issue 2090013006: [Downloads] Correctly test page transition when calculating danger level. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge with ToT Created 4 years, 5 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
« no previous file with comments | « chrome/browser/download/download_target_determiner.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 7
8 #include "base/at_exit.h" 8 #include "base/at_exit.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h" 10 #include "base/files/scoped_temp_dir.h"
(...skipping 1012 matching lines...) Expand 10 before | Expand all | Expand 10 after
1023 history::HistoryService* history_service = 1023 history::HistoryService* history_service =
1024 HistoryServiceFactory::GetForProfile(profile(), 1024 HistoryServiceFactory::GetForProfile(profile(),
1025 ServiceAccessType::EXPLICIT_ACCESS); 1025 ServiceAccessType::EXPLICIT_ACCESS);
1026 ASSERT_TRUE(history_service); 1026 ASSERT_TRUE(history_service);
1027 history_service->AddPage(url, time_of_visit, history::SOURCE_BROWSED); 1027 history_service->AddPage(url, time_of_visit, history::SOURCE_BROWSED);
1028 1028
1029 RunTestCasesWithActiveItem(kVisitedReferrerCases, 1029 RunTestCasesWithActiveItem(kVisitedReferrerCases,
1030 arraysize(kVisitedReferrerCases)); 1030 arraysize(kVisitedReferrerCases));
1031 } 1031 }
1032 1032
1033 TEST_F(DownloadTargetDeterminerTest, TransitionType) {
1034 const DownloadTestCase kSafeFile = {
1035 AUTOMATIC,
1036 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1037 DownloadFileType::NOT_DANGEROUS,
1038 "http://example.com/foo.txt",
1039 "text/plain",
1040 FILE_PATH_LITERAL(""),
1041
1042 FILE_PATH_LITERAL("foo.txt"),
1043 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1044
1045 EXPECT_CRDOWNLOAD};
1046
1047 const DownloadTestCase kAllowOnUserGesture = {
1048 AUTOMATIC,
1049 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1050 DownloadFileType::ALLOW_ON_USER_GESTURE,
1051 "http://example.com/foo.crx",
1052 "application/octet-stream",
1053 FILE_PATH_LITERAL(""),
1054
1055 FILE_PATH_LITERAL("foo.crx"),
1056 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1057
1058 EXPECT_UNCONFIRMED};
1059
1060 const DownloadTestCase kDangerousFile = {
1061 AUTOMATIC,
1062 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1063 DownloadFileType::DANGEROUS,
1064 "http://example.com/foo.swf",
1065 "application/octet-stream",
1066 FILE_PATH_LITERAL(""),
1067
1068 FILE_PATH_LITERAL("foo.swf"),
1069 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1070
1071 EXPECT_UNCONFIRMED};
1072
1073 const struct {
1074 ui::PageTransition page_transition;
1075 content::DownloadDangerType expected_danger_type;
1076 const DownloadTestCase& template_download_test_case;
1077 } kTestCases[] = {
1078 {// Benign file type. Results in a danger type of NOT_DANGEROUS. Page
1079 // transition type is irrelevant.
1080 ui::PAGE_TRANSITION_LINK, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1081 kSafeFile},
1082
1083 {// File type is ALLOW_ON_USER_GESTURE. PAGE_TRANSITION_LINK doesn't
1084 // cause file to be marked as safe.
1085 ui::PAGE_TRANSITION_LINK, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1086 kAllowOnUserGesture},
1087
1088 {// File type is ALLOW_ON_USER_GESTURE. PAGE_TRANSITION_TYPED doesn't
1089 // cause file to be marked as safe. TYPED can be used for certain
1090 // types of explicit page transitions that aren't necessarily
1091 // initiated by a user. Hence a resulting download may not be
1092 // intentional.
1093 ui::PAGE_TRANSITION_TYPED, content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1094 kAllowOnUserGesture},
1095
1096 {// File type is ALLOW_ON_USER_GESTURE.
1097 // PAGE_TRANSITION_FROM_ADDRESS_BAR causes file to be marked as safe.
1098 static_cast<ui::PageTransition>(ui::PAGE_TRANSITION_TYPED |
1099 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR),
1100 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, kAllowOnUserGesture},
1101
1102 {// File type is ALLOW_ON_USER_GESTURE.
1103 // PAGE_TRANSITION_FROM_ADDRESS_BAR causes file to be marked as safe.
1104 static_cast<ui::PageTransition>(ui::PAGE_TRANSITION_GENERATED |
1105 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR),
1106 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, kAllowOnUserGesture},
1107
1108 {// File type is ALLOW_ON_USER_GESTURE.
1109 // PAGE_TRANSITION_FROM_ADDRESS_BAR causes file to be marked as safe.
1110 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR,
1111 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, kAllowOnUserGesture},
1112
1113 {// File type is DANGEROUS. PageTransition is irrelevant.
1114 static_cast<ui::PageTransition>(ui::PAGE_TRANSITION_TYPED |
1115 ui::PAGE_TRANSITION_FROM_ADDRESS_BAR),
1116 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, kDangerousFile},
1117 };
1118
1119 // Test assumptions:
1120 ASSERT_EQ(DownloadFileType::ALLOW_ON_USER_GESTURE,
1121 Policies()->GetFileDangerLevel(
1122 base::FilePath(FILE_PATH_LITERAL("foo.crx"))));
1123 ASSERT_EQ(DownloadFileType::DANGEROUS,
1124 Policies()->GetFileDangerLevel(
1125 base::FilePath(FILE_PATH_LITERAL("foo.swf"))));
1126 ASSERT_EQ(DownloadFileType::NOT_DANGEROUS,
1127 Policies()->GetFileDangerLevel(
1128 base::FilePath(FILE_PATH_LITERAL("foo.txt"))));
1129
1130 for (const auto& test_case : kTestCases) {
1131 // The template download test case describes what to expect if the page
1132 // transition was LINK. If the expectation is that the page transition type
1133 // causes the download to be considered safe, then download_test_case needs
1134 // to be adjusted accordingly.
1135 DownloadTestCase download_test_case = test_case.template_download_test_case;
1136 download_test_case.expected_danger_type = test_case.expected_danger_type;
1137 if (test_case.expected_danger_type ==
1138 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS) {
1139 download_test_case.expected_danger_level =
1140 DownloadFileType::NOT_DANGEROUS;
1141 download_test_case.expected_intermediate = EXPECT_CRDOWNLOAD;
1142 }
1143
1144 std::unique_ptr<content::MockDownloadItem> item(
1145 CreateActiveDownloadItem(1, download_test_case));
1146 EXPECT_CALL(*item, GetTransitionType())
1147 .WillRepeatedly(Return(test_case.page_transition));
1148 RunTestCase(download_test_case, base::FilePath(), item.get());
1149 }
1150 }
1151
1033 // These test cases are run with "Prompt for download" user preference set to 1152 // These test cases are run with "Prompt for download" user preference set to
1034 // true. 1153 // true.
1035 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_PromptAlways) { 1154 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_PromptAlways) {
1036 const DownloadTestCase kPromptingTestCases[] = { 1155 const DownloadTestCase kPromptingTestCases[] = {
1037 {// 0: Safe Automatic - Should prompt because of "Prompt for download" 1156 {// 0: Safe Automatic - Should prompt because of "Prompt for download"
1038 // preference setting. 1157 // preference setting.
1039 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 1158 AUTOMATIC, content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1040 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt", 1159 DownloadFileType::NOT_DANGEROUS, "http://example.com/foo.txt",
1041 "text/plain", FILE_PATH_LITERAL(""), 1160 "text/plain", FILE_PATH_LITERAL(""),
1042 1161
(...skipping 976 matching lines...) Expand 10 before | Expand all | Expand 10 after
2019 EXPECT_CALL(mock_plugin_filter_, MockPluginAvailable(browser_plugin.path())) 2138 EXPECT_CALL(mock_plugin_filter_, MockPluginAvailable(browser_plugin.path()))
2020 .WillRepeatedly(Return(false)); 2139 .WillRepeatedly(Return(false));
2021 target_info = RunDownloadTargetDeterminer( 2140 target_info = RunDownloadTargetDeterminer(
2022 GetPathInDownloadDir(kInitialPath), item.get()); 2141 GetPathInDownloadDir(kInitialPath), item.get());
2023 EXPECT_FALSE(target_info->is_filetype_handled_safely); 2142 EXPECT_FALSE(target_info->is_filetype_handled_safely);
2024 } 2143 }
2025 2144
2026 #endif // defined(ENABLE_PLUGINS) 2145 #endif // defined(ENABLE_PLUGINS)
2027 2146
2028 } // namespace 2147 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/download/download_target_determiner.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698