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