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