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