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

Side by Side Diff: chrome/test/ppapi/ppapi_filechooser_browsertest.cc

Issue 1409003002: [SafeBrowsing] Block dangerous unchecked downloads based on a Finch trial. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove redundant field trial group Created 5 years, 2 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 "base/bind.h"
6 #include "base/command_line.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "chrome/common/chrome_switches.h"
11 #include "chrome/test/ppapi/ppapi_test.h"
12 #include "ppapi/shared_impl/test_utils.h"
13 #include "ui/shell_dialogs/select_file_dialog.h"
14 #include "ui/shell_dialogs/select_file_dialog_factory.h"
15 #include "ui/shell_dialogs/selected_file_info.h"
16
17 // FileChooser tests.
bbudge 2015/10/22 22:35:51 nit: comment is superfluous
asanka 2015/10/23 19:35:03 Ha. yes :) removed
18 namespace {
19
20 class TestSelectFileDialogFactory final : public ui::SelectFileDialogFactory {
21 public:
22 using SelectedFileInfoList = std::vector<ui::SelectedFileInfo>;
23
24 enum Mode {
25 RESPOND_WITH_FILE_LIST,
26 CANCEL,
27 REPLACE_BASENAME,
28 NOT_REACHED,
29 };
30
31 TestSelectFileDialogFactory(Mode mode,
32 const SelectedFileInfoList& selected_file_info)
33 : selected_file_info_(selected_file_info), mode_(mode) {
34 // Only safe because this class is 'final'
35 ui::SelectFileDialog::SetFactory(this);
36 }
37
38 // SelectFileDialogFactory
39 ui::SelectFileDialog* Create(ui::SelectFileDialog::Listener* listener,
40 ui::SelectFilePolicy* policy) override {
41 return new SelectFileDialog(listener, policy, selected_file_info_, mode_);
42 }
43
44 private:
45 class SelectFileDialog : public ui::SelectFileDialog {
46 public:
47 SelectFileDialog(Listener* listener,
48 ui::SelectFilePolicy* policy,
49 const SelectedFileInfoList& selected_file_info,
50 Mode mode)
51 : ui::SelectFileDialog(listener, policy),
52 selected_file_info_(selected_file_info),
53 mode_(mode) {}
54
55 protected:
56 // ui::SelectFileDialog
57 void SelectFileImpl(Type type,
58 const base::string16& title,
59 const base::FilePath& default_path,
60 const FileTypeInfo* file_types,
61 int file_type_index,
62 const base::FilePath::StringType& default_extension,
63 gfx::NativeWindow owning_window,
64 void* params) override {
65 switch (mode_) {
66 case RESPOND_WITH_FILE_LIST:
67 break;
68
69 case CANCEL:
70 EXPECT_EQ(0u, selected_file_info_.size());
71 break;
72
73 case REPLACE_BASENAME:
74 EXPECT_EQ(1u, selected_file_info_.size());
75 for (auto& selected_file : selected_file_info_) {
76 selected_file =
77 ui::SelectedFileInfo(selected_file.file_path.DirName().Append(
78 default_path.BaseName()),
79 selected_file.local_path.DirName().Append(
80 default_path.BaseName()));
81 }
82 break;
83
84 case NOT_REACHED:
85 NOTREACHED();
86 break;
87 }
88
89 base::ThreadTaskRunnerHandle::Get()->PostTask(
90 FROM_HERE,
91 base::Bind(&SelectFileDialog::RespondToFileSelectionRequest, this,
92 params));
93 }
94 bool HasMultipleFileTypeChoicesImpl() override { return false; }
95
96 // BaseShellDialog
97 bool IsRunning(gfx::NativeWindow owning_window) const override {
98 return false;
99 }
100 void ListenerDestroyed() override {}
101
102 private:
103 void RespondToFileSelectionRequest(void* params) {
104 if (selected_file_info_.size() == 0)
105 listener_->FileSelectionCanceled(params);
106 else if (selected_file_info_.size() == 1)
107 listener_->FileSelectedWithExtraInfo(selected_file_info_.front(), 0,
108 params);
109 else
110 listener_->MultiFilesSelectedWithExtraInfo(selected_file_info_, params);
111 }
112
113 SelectedFileInfoList selected_file_info_;
114 Mode mode_;
115 };
116
117 std::vector<ui::SelectedFileInfo> selected_file_info_;
118 Mode mode_;
119 };
120
121 class PPAPIFileChooserTest : public OutOfProcessPPAPITest {};
122
123 } // namespace
124
125 IN_PROC_BROWSER_TEST_F(PPAPIFileChooserTest, FileChooser_Open_Success) {
126 const char kContents[] = "Hello from browser";
127 base::ScopedTempDir temp_dir;
128 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
129
130 base::FilePath existing_filename = temp_dir.path().AppendASCII("foo");
131 ASSERT_EQ(
132 static_cast<int>(sizeof(kContents) - 1),
133 base::WriteFile(existing_filename, kContents, sizeof(kContents) - 1));
134
135 TestSelectFileDialogFactory::SelectedFileInfoList file_info_list;
136 file_info_list.push_back(
137 ui::SelectedFileInfo(existing_filename, existing_filename));
138 TestSelectFileDialogFactory test_dialog_factory(
139 TestSelectFileDialogFactory::RESPOND_WITH_FILE_LIST, file_info_list);
140 RunTestViaHTTP("FileChooser_OpenSimple");
141 }
142
143 IN_PROC_BROWSER_TEST_F(PPAPIFileChooserTest, FileChooser_Open_Cancel) {
144 TestSelectFileDialogFactory test_dialog_factory(
145 TestSelectFileDialogFactory::CANCEL,
146 TestSelectFileDialogFactory::SelectedFileInfoList());
147 RunTestViaHTTP("FileChooser_OpenCancel");
148 }
149
150 IN_PROC_BROWSER_TEST_F(PPAPIFileChooserTest, FileChooser_SaveAs_Success) {
151 base::ScopedTempDir temp_dir;
152 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
153 base::FilePath suggested_filename = temp_dir.path().AppendASCII("foo");
154
155 TestSelectFileDialogFactory::SelectedFileInfoList file_info_list;
156 file_info_list.push_back(
157 ui::SelectedFileInfo(suggested_filename, suggested_filename));
158 TestSelectFileDialogFactory test_dialog_factory(
159 TestSelectFileDialogFactory::RESPOND_WITH_FILE_LIST, file_info_list);
160
161 RunTestViaHTTP("FileChooser_SaveAsSafeDefaultName");
162 ASSERT_TRUE(base::PathExists(suggested_filename));
163 }
164
165 IN_PROC_BROWSER_TEST_F(PPAPIFileChooserTest,
166 FileChooser_SaveAs_SafeDefaultName) {
167 base::ScopedTempDir temp_dir;
168 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
169 base::FilePath suggested_filename = temp_dir.path().AppendASCII("foo");
170
171 TestSelectFileDialogFactory::SelectedFileInfoList file_info_list;
172 file_info_list.push_back(
173 ui::SelectedFileInfo(suggested_filename, suggested_filename));
174 TestSelectFileDialogFactory test_dialog_factory(
175 TestSelectFileDialogFactory::REPLACE_BASENAME, file_info_list);
176
177 RunTestViaHTTP("FileChooser_SaveAsSafeDefaultName");
178 base::FilePath actual_filename = temp_dir.path().AppendASCII("innocuous.txt");
179
180 ASSERT_TRUE(base::PathExists(actual_filename));
181 std::string file_contents;
182 ASSERT_TRUE(base::ReadFileToString(actual_filename, &file_contents, 100));
183 EXPECT_EQ("Hello from PPAPI", file_contents);
184 }
185
186 IN_PROC_BROWSER_TEST_F(PPAPIFileChooserTest,
187 FileChooser_SaveAs_UnsafeDefaultName) {
188 base::ScopedTempDir temp_dir;
189 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
190 base::FilePath suggested_filename = temp_dir.path().AppendASCII("foo");
191
192 TestSelectFileDialogFactory::SelectedFileInfoList file_info_list;
193 file_info_list.push_back(
194 ui::SelectedFileInfo(suggested_filename, suggested_filename));
195 TestSelectFileDialogFactory test_dialog_factory(
196 TestSelectFileDialogFactory::REPLACE_BASENAME, file_info_list);
197
198 RunTestViaHTTP("FileChooser_SaveAsUnsafeDefaultName");
199 base::FilePath actual_filename = temp_dir.path().AppendASCII("unsafe.txt-");
200
201 ASSERT_TRUE(base::PathExists(actual_filename));
202 std::string file_contents;
203 ASSERT_TRUE(base::ReadFileToString(actual_filename, &file_contents, 100));
204 EXPECT_EQ("Hello from PPAPI", file_contents);
205 }
206
207 IN_PROC_BROWSER_TEST_F(PPAPIFileChooserTest, FileChooser_SaveAs_Cancel) {
208 TestSelectFileDialogFactory test_dialog_factory(
209 TestSelectFileDialogFactory::CANCEL,
210 TestSelectFileDialogFactory::SelectedFileInfoList());
211 RunTestViaHTTP("FileChooser_SaveAsCancel");
212 }
213
214 #if defined(FULL_SAFE_BROWSING)
215 // These two tests only make sense when SafeBrowsing is enabled. They verify
216 // that files written via the FileChooser_Trusted API are properly threaded
bbudge 2015/10/22 22:35:51 nit: the word 'threaded' seems slightly confusing
asanka 2015/10/23 19:35:03 Went with "passed"
217 // through Safe Browsing.
218
219 IN_PROC_BROWSER_TEST_F(PPAPIFileChooserTest,
220 FileChooser_SaveAs_DangerousExecutable_Allowed) {
221 base::CommandLine::ForCurrentProcess()->AppendSwitch(
222 switches::kAllowUncheckedDangerousDownloads);
223 base::ScopedTempDir temp_dir;
224 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
225 base::FilePath suggested_filename = temp_dir.path().AppendASCII("foo");
226
227 TestSelectFileDialogFactory::SelectedFileInfoList file_info_list;
228 file_info_list.push_back(
229 ui::SelectedFileInfo(suggested_filename, suggested_filename));
230 TestSelectFileDialogFactory test_dialog_factory(
231 TestSelectFileDialogFactory::REPLACE_BASENAME, file_info_list);
232
233 RunTestViaHTTP("FileChooser_SaveAsDangerousExecutableAllowed");
234 base::FilePath actual_filename = temp_dir.path().AppendASCII("dangerous.exe");
235
236 ASSERT_TRUE(base::PathExists(actual_filename));
237 std::string file_contents;
238 ASSERT_TRUE(base::ReadFileToString(actual_filename, &file_contents, 100));
239 EXPECT_EQ("Hello from PPAPI", file_contents);
240 }
241
242 IN_PROC_BROWSER_TEST_F(PPAPIFileChooserTest,
243 FileChooser_SaveAs_DangerousExecutable_Disallowed) {
244 base::CommandLine::ForCurrentProcess()->AppendSwitch(
245 switches::kDisallowUncheckedDangerousDownloads);
246 TestSelectFileDialogFactory test_dialog_factory(
247 TestSelectFileDialogFactory::NOT_REACHED,
248 TestSelectFileDialogFactory::SelectedFileInfoList());
249 RunTestViaHTTP("FileChooser_SaveAsDangerousExecutableDisallowed");
250 }
251
252 #endif // FULL_SAFE_BROWSING
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698