Chromium Code Reviews| 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 REGISTER_TEST_CASE(FileChooser); | |
| 15 | |
| 16 bool TestFileChooser::Init() { | |
| 17 return true; | |
|
bbudge
2015/10/21 21:16:04
The other PPB_File* tests Init like this:
bool Te
asanka
2015/10/22 21:55:12
Done.
| |
| 18 } | |
| 19 | |
| 20 bool TestFileChooser::WriteDefaultContentsToFile(const pp::FileRef& file_ref) { | |
|
bbudge
2015/10/21 21:16:04
nit: Move this below RunTests method to match head
asanka
2015/10/22 21:55:12
Done.
| |
| 21 const char kContents[] = "Hello from PPAPI"; | |
| 22 | |
| 23 TestCompletionCallback fileio_callback(instance_->pp_instance(), | |
| 24 callback_type()); | |
| 25 pp::FileIO fileio(instance()); | |
| 26 | |
| 27 fileio_callback.WaitForResult( | |
| 28 fileio.Open(file_ref, PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_CREATE, | |
| 29 fileio_callback.GetCallback())); | |
| 30 if (fileio_callback.result() != PP_OK) | |
| 31 return false; | |
| 32 | |
| 33 fileio_callback.WaitForResult(fileio.Write( | |
| 34 0, kContents, sizeof(kContents) - 1, fileio_callback.GetCallback())); | |
| 35 return fileio_callback.result() == sizeof(kContents) - 1; | |
| 36 } | |
| 37 | |
| 38 void TestFileChooser::RunTests(const std::string& filter) { | |
| 39 RUN_TEST(OpenSimple, filter); | |
| 40 RUN_TEST(OpenCancel, filter); | |
| 41 RUN_TEST(SaveAsSafeDefaultName, filter); | |
| 42 RUN_TEST(SaveAsUnsafeDefaultName, filter); | |
| 43 RUN_TEST(SaveAsDangerousExecutableAllowed, filter); | |
| 44 RUN_TEST(SaveAsDangerousExecutableDisallowed, filter); | |
| 45 } | |
| 46 | |
| 47 // Tests that the plugin can invoke a simple file chooser and that the returned | |
| 48 // file can be read from. Note that this test doesn't test that the accepted | |
| 49 // file type list is honored. | |
| 50 std::string TestFileChooser::TestOpenSimple() { | |
| 51 pp::FileChooser_Dev file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, | |
| 52 "text/plain,.html"); | |
| 53 ASSERT_FALSE(file_chooser.is_null()); | |
| 54 | |
| 55 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> | |
| 56 filechooser_callback(instance_->pp_instance(), callback_type()); | |
| 57 filechooser_callback.WaitForResult( | |
| 58 file_chooser.Show(filechooser_callback.GetCallback())); | |
| 59 | |
| 60 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); | |
| 61 ASSERT_EQ(1u, output_ref.size()); | |
| 62 | |
| 63 TestCompletionCallback fileio_callback(instance_->pp_instance(), | |
| 64 callback_type()); | |
| 65 pp::FileIO fileio(instance()); | |
| 66 fileio_callback.WaitForResult(fileio.Open( | |
| 67 output_ref.front(), PP_FILEOPENFLAG_READ, fileio_callback.GetCallback())); | |
| 68 ASSERT_EQ(PP_OK, fileio_callback.result()); | |
| 69 PASS(); | |
| 70 } | |
| 71 | |
| 72 // Tests the behavior when the user cancels the file chooser. Browser-side logic | |
| 73 // for simulating the cancellation can be found at ppapi_browsertest.cc | |
| 74 std::string TestFileChooser::TestOpenCancel() { | |
| 75 pp::FileChooser_Dev file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, | |
| 76 "text/plain,.html"); | |
| 77 ASSERT_FALSE(file_chooser.is_null()); | |
| 78 | |
| 79 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> | |
| 80 filechooser_callback(instance_->pp_instance(), callback_type()); | |
| 81 filechooser_callback.WaitForResult( | |
| 82 file_chooser.Show(filechooser_callback.GetCallback())); | |
| 83 | |
| 84 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); | |
| 85 ASSERT_EQ(0u, output_ref.size()); | |
| 86 PASS(); | |
| 87 } | |
| 88 | |
| 89 // Tests that the plugin can invoke a "Save as" dialog using the | |
| 90 // FileChooser_Trusted API and that the returned FileRef can be written to. | |
| 91 std::string TestFileChooser::TestSaveAsSafeDefaultName() { | |
| 92 pp::FileChooser_Trusted file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, | |
| 93 "text/plain,.html", true /* save_as */, | |
| 94 "innocuous.txt"); | |
| 95 ASSERT_FALSE(file_chooser.is_null()); | |
| 96 | |
| 97 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> | |
| 98 filechooser_callback(instance_->pp_instance(), callback_type()); | |
| 99 filechooser_callback.WaitForResult( | |
| 100 file_chooser.Show(filechooser_callback.GetCallback())); | |
| 101 | |
| 102 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); | |
| 103 ASSERT_EQ(1u, output_ref.size()); | |
| 104 | |
| 105 ASSERT_TRUE(WriteDefaultContentsToFile(output_ref.front())); | |
| 106 PASS(); | |
| 107 } | |
| 108 | |
| 109 // Similar to the previous test, but tests that an unsafe filename passed as the | |
| 110 // suggested name is sanitized. | |
| 111 std::string TestFileChooser::TestSaveAsUnsafeDefaultName() { | |
| 112 pp::FileChooser_Trusted file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, | |
| 113 "text/plain,.html", true /* save_as */, | |
| 114 "unsafe.txt "); | |
| 115 ASSERT_FALSE(file_chooser.is_null()); | |
| 116 | |
| 117 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> | |
| 118 filechooser_callback(instance_->pp_instance(), callback_type()); | |
| 119 filechooser_callback.WaitForResult( | |
| 120 file_chooser.Show(filechooser_callback.GetCallback())); | |
| 121 | |
| 122 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); | |
| 123 ASSERT_EQ(1u, output_ref.size()); | |
| 124 ASSERT_EQ("unsafe.txt-", output_ref.front().GetName().AsString()); | |
| 125 | |
| 126 ASSERT_TRUE(WriteDefaultContentsToFile(output_ref.front())); | |
| 127 PASS(); | |
| 128 } | |
| 129 | |
| 130 // Checks that a dangerous file is allowed to be downloaded via the | |
| 131 // FileChooser_Trusted API. Chrome should delegate the decision of which files | |
| 132 // are allowed over to SafeBrowsing (if enabled), and the current SafeBrowsing | |
| 133 // configuration should allow downloading of dangerous files for this test to | |
| 134 // work. | |
| 135 std::string TestFileChooser::TestSaveAsDangerousExecutableAllowed() { | |
| 136 pp::FileChooser_Trusted file_chooser(instance(), PP_FILECHOOSERMODE_OPEN, | |
| 137 ".exe", true /* save_as */, | |
| 138 "dangerous.exe"); | |
| 139 ASSERT_FALSE(file_chooser.is_null()); | |
| 140 | |
| 141 TestCompletionCallbackWithOutput<std::vector<pp::FileRef>> | |
| 142 filechooser_callback(instance_->pp_instance(), callback_type()); | |
| 143 filechooser_callback.WaitForResult( | |
| 144 file_chooser.Show(filechooser_callback.GetCallback())); | |
| 145 | |
| 146 const std::vector<pp::FileRef>& output_ref = filechooser_callback.output(); | |
| 147 ASSERT_EQ(1u, output_ref.size()); | |
| 148 ASSERT_EQ("dangerous.exe", output_ref.front().GetName().AsString()); | |
| 149 | |
| 150 ASSERT_TRUE(WriteDefaultContentsToFile(output_ref.front())); | |
| 151 PASS(); | |
| 152 } | |
| 153 | |
| 154 // Checks that a dangerous file is not 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 disallow downloading of dangerous files for this test to | |
| 158 // work. | |
| 159 std::string TestFileChooser::TestSaveAsDangerousExecutableDisallowed() { | |
| 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(0u, output_ref.size()); | |
| 172 PASS(); | |
| 173 } | |
| OLD | NEW |