| 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 "ppapi/tests/test_file_chooser.h" |
| 6 |
| 7 #include "ppapi/c/pp_errors.h" |
| 8 #include "ppapi/c/ppb_file_io.h" |
| 9 #include "ppapi/cpp/dev/file_chooser_dev.h" |
| 10 #include "ppapi/cpp/file_io.h" |
| 11 #include "ppapi/cpp/trusted/file_chooser_trusted.h" |
| 12 #include "ppapi/cpp/var.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Test data written by WriteDefaultContentsToFile(). |
| 17 const char kTestFileContents[] = "Hello from PPAPI"; |
| 18 const size_t kTestFileContentsSizeBytes = sizeof(kTestFileContents) - 1; |
| 19 |
| 20 } // namespace |
| 21 |
| 22 REGISTER_TEST_CASE(FileChooser); |
| 23 |
| 24 bool TestFileChooser::Init() { |
| 25 return CheckTestingInterface() && EnsureRunningOverHTTP(); |
| 26 } |
| 27 |
| 28 void TestFileChooser::RunTests(const std::string& filter) { |
| 29 RUN_TEST(OpenSimple, filter); |
| 30 RUN_TEST(OpenCancel, filter); |
| 31 RUN_TEST(SaveAsSafeDefaultName, filter); |
| 32 RUN_TEST(SaveAsUnsafeDefaultName, filter); |
| 33 RUN_TEST(SaveAsCancel, filter); |
| 34 RUN_TEST(SaveAsDangerousExecutableAllowed, filter); |
| 35 RUN_TEST(SaveAsDangerousExecutableDisallowed, filter); |
| 36 } |
| 37 |
| 38 bool TestFileChooser::WriteDefaultContentsToFile(const pp::FileRef& file_ref) { |
| 39 TestCompletionCallback fileio_callback(instance_->pp_instance(), |
| 40 callback_type()); |
| 41 pp::FileIO fileio(instance()); |
| 42 |
| 43 fileio_callback.WaitForResult( |
| 44 fileio.Open(file_ref, PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_CREATE, |
| 45 fileio_callback.GetCallback())); |
| 46 if (fileio_callback.result() != PP_OK) |
| 47 return false; |
| 48 |
| 49 fileio_callback.WaitForResult(fileio.Write(0, kTestFileContents, |
| 50 kTestFileContentsSizeBytes, |
| 51 fileio_callback.GetCallback())); |
| 52 return fileio_callback.result() == kTestFileContentsSizeBytes; |
| 53 } |
| 54 |
| 55 // Tests that the plugin can invoke a simple file chooser and that the returned |
| 56 // file can be read from. Note that this test doesn't test that the accepted |
| 57 // file type list is honored. |
| 58 std::string TestFileChooser::TestOpenSimple() { |
| 59 pp::FileChooser_Dev file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, "*"); |
| 60 ASSERT_FALSE(file_chooser.is_null()); |
| 61 |
| 62 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> |
| 63 filechooser_callback(instance_->pp_instance(), callback_type()); |
| 64 filechooser_callback.WaitForResult( |
| 65 file_chooser.Show(filechooser_callback.GetCallback())); |
| 66 |
| 67 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); |
| 68 ASSERT_EQ(1u, output_ref.size()); |
| 69 |
| 70 TestCompletionCallback fileio_callback(instance_->pp_instance(), |
| 71 callback_type()); |
| 72 pp::FileIO fileio(instance()); |
| 73 fileio_callback.WaitForResult(fileio.Open( |
| 74 output_ref.front(), PP_FILEOPENFLAG_READ, fileio_callback.GetCallback())); |
| 75 ASSERT_EQ(PP_OK, fileio_callback.result()); |
| 76 PASS(); |
| 77 } |
| 78 |
| 79 // Tests the behavior when the user cancels the file chooser. Browser-side logic |
| 80 // for simulating the cancellation can be found at |
| 81 // ppapi_filechooser_browsertest.cc |
| 82 std::string TestFileChooser::TestOpenCancel() { |
| 83 pp::FileChooser_Dev file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, "*"); |
| 84 ASSERT_FALSE(file_chooser.is_null()); |
| 85 |
| 86 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> |
| 87 filechooser_callback(instance_->pp_instance(), callback_type()); |
| 88 filechooser_callback.WaitForResult( |
| 89 file_chooser.Show(filechooser_callback.GetCallback())); |
| 90 |
| 91 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); |
| 92 ASSERT_EQ(0u, output_ref.size()); |
| 93 PASS(); |
| 94 } |
| 95 |
| 96 // Tests that the plugin can invoke a "Save as" dialog using the |
| 97 // FileChooser_Trusted API and that the returned FileRef can be written to. |
| 98 std::string TestFileChooser::TestSaveAsSafeDefaultName() { |
| 99 pp::FileChooser_Trusted file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, |
| 100 ".txt", true /* save_as */, |
| 101 "innocuous.txt"); |
| 102 ASSERT_FALSE(file_chooser.is_null()); |
| 103 |
| 104 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> |
| 105 filechooser_callback(instance_->pp_instance(), callback_type()); |
| 106 filechooser_callback.WaitForResult( |
| 107 file_chooser.Show(filechooser_callback.GetCallback())); |
| 108 |
| 109 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); |
| 110 ASSERT_EQ(1u, output_ref.size()); |
| 111 |
| 112 ASSERT_TRUE(WriteDefaultContentsToFile(output_ref.front())); |
| 113 PASS(); |
| 114 } |
| 115 |
| 116 // Similar to the previous test, but tests that an unsafe filename passed as the |
| 117 // suggested name is sanitized. |
| 118 std::string TestFileChooser::TestSaveAsUnsafeDefaultName() { |
| 119 pp::FileChooser_Trusted file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, |
| 120 "text/plain,.html", true /* save_as */, |
| 121 "unsafe.txt "); |
| 122 ASSERT_FALSE(file_chooser.is_null()); |
| 123 |
| 124 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> |
| 125 filechooser_callback(instance_->pp_instance(), callback_type()); |
| 126 filechooser_callback.WaitForResult( |
| 127 file_chooser.Show(filechooser_callback.GetCallback())); |
| 128 |
| 129 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); |
| 130 ASSERT_EQ(1u, output_ref.size()); |
| 131 ASSERT_EQ("unsafe.txt-", output_ref.front().GetName().AsString()); |
| 132 |
| 133 ASSERT_TRUE(WriteDefaultContentsToFile(output_ref.front())); |
| 134 PASS(); |
| 135 } |
| 136 |
| 137 // Tests the behavior when the user cancels a Save As file chooser. Requires |
| 138 // that the test runner cancel the Save As dialog. |
| 139 std::string TestFileChooser::TestSaveAsCancel() { |
| 140 pp::FileChooser_Trusted file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, "*", |
| 141 true /* save as */, "anything.txt"); |
| 142 ASSERT_FALSE(file_chooser.is_null()); |
| 143 |
| 144 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> |
| 145 filechooser_callback(instance_->pp_instance(), callback_type()); |
| 146 filechooser_callback.WaitForResult( |
| 147 file_chooser.Show(filechooser_callback.GetCallback())); |
| 148 |
| 149 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); |
| 150 ASSERT_EQ(0u, output_ref.size()); |
| 151 PASS(); |
| 152 } |
| 153 |
| 154 // Checks that a dangerous file is allowed to be downloaded via the |
| 155 // FileChooser_Trusted API. Chrome should delegate the decision of which files |
| 156 // are allowed over to SafeBrowsing (if enabled), and the current SafeBrowsing |
| 157 // configuration should allow downloading of dangerous files for this test to |
| 158 // work. |
| 159 std::string TestFileChooser::TestSaveAsDangerousExecutableAllowed() { |
| 160 pp::FileChooser_Trusted file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, |
| 161 ".exe", true /* save_as */, |
| 162 "dangerous.exe"); |
| 163 ASSERT_FALSE(file_chooser.is_null()); |
| 164 |
| 165 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> |
| 166 filechooser_callback(instance_->pp_instance(), callback_type()); |
| 167 filechooser_callback.WaitForResult( |
| 168 file_chooser.Show(filechooser_callback.GetCallback())); |
| 169 |
| 170 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); |
| 171 ASSERT_EQ(1u, output_ref.size()); |
| 172 ASSERT_EQ("dangerous.exe", output_ref.front().GetName().AsString()); |
| 173 |
| 174 ASSERT_TRUE(WriteDefaultContentsToFile(output_ref.front())); |
| 175 PASS(); |
| 176 } |
| 177 |
| 178 // Checks that a dangerous file is not allowed to be downloaded via the |
| 179 // FileChooser_Trusted API. Chrome should delegate the decision of which files |
| 180 // are allowed over to SafeBrowsing (if enabled), and the current SafeBrowsing |
| 181 // configuration should disallow downloading of dangerous files for this test to |
| 182 // work. |
| 183 std::string TestFileChooser::TestSaveAsDangerousExecutableDisallowed() { |
| 184 pp::FileChooser_Trusted file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, |
| 185 ".exe", true /* save_as */, |
| 186 "dangerous.exe"); |
| 187 ASSERT_FALSE(file_chooser.is_null()); |
| 188 |
| 189 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> |
| 190 filechooser_callback(instance_->pp_instance(), callback_type()); |
| 191 filechooser_callback.WaitForResult( |
| 192 file_chooser.Show(filechooser_callback.GetCallback())); |
| 193 |
| 194 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); |
| 195 ASSERT_EQ(0u, output_ref.size()); |
| 196 PASS(); |
| 197 } |
| OLD | NEW |