Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/tests/test_broker.h" | 5 #include "ppapi/tests/test_broker.h" |
| 6 | 6 |
| 7 #include <cstdio> | |
| 8 #include <cstring> | |
| 9 #include <fstream> | |
| 10 #include <limits> | |
| 11 | |
| 12 #include "base/memory/scoped_ptr.h" | |
| 7 #include "ppapi/c/pp_errors.h" | 13 #include "ppapi/c/pp_errors.h" |
| 14 #include "ppapi/c/trusted/ppp_broker.h" | |
| 8 #include "ppapi/c/trusted/ppb_broker_trusted.h" | 15 #include "ppapi/c/trusted/ppb_broker_trusted.h" |
| 9 #include "ppapi/cpp/module.h" | 16 #include "ppapi/tests/test_utils.h" |
| 10 #include "ppapi/tests/testing_instance.h" | 17 #include "ppapi/tests/testing_instance.h" |
| 11 | 18 |
| 19 #if defined(OS_WIN) | |
| 20 #include "Windows.h" | |
| 21 #elif !defined(OS_POSIX) | |
| 22 #error Not implemented. Don't remove as it's only checked here in this file. | |
| 23 #endif | |
| 24 | |
| 12 REGISTER_TEST_CASE(Broker); | 25 REGISTER_TEST_CASE(Broker); |
| 13 | 26 |
| 27 namespace { | |
| 28 | |
| 29 const char kHelloMessage[] = "Hello Plugin! This is Broker!"; | |
| 30 const char kBrokerUnsandboxed[] = "Broker is Unsandboxed!"; | |
| 31 | |
| 32 #if defined(OS_WIN) | |
| 33 typedef HANDLE PlatformFile; | |
| 34 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; | |
| 35 const int32_t kInvalidHandle = static_cast<int32_t>( | |
| 36 reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE)); | |
| 37 #elif defined(OS_POSIX) | |
| 38 typedef int PlatformFile; | |
| 39 const PlatformFile kInvalidPlatformFileValue = -1; | |
| 40 const int32_t kInvalidHandle = -1; | |
| 41 #endif | |
| 42 | |
| 43 PlatformFile IntToPlatformFile(int32_t handle) { | |
| 44 #if defined(OS_WIN) | |
| 45 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); | |
| 46 #elif defined(OS_POSIX) | |
| 47 return handle; | |
| 48 #endif | |
| 49 } | |
| 50 | |
| 51 bool ReadFromPlatformFile(PlatformFile file, size_t bytes_to_read, | |
| 52 char* buffer, size_t* bytes_read) { | |
| 53 #if defined(OS_WIN) | |
| 54 assert(bytes_to_read < std::numeric_limits<DWORD>::max()); | |
| 55 DWORD read = 0; | |
| 56 if (!::ReadFile(file, buffer, static_cast<DWORD>(bytes_to_read), &read, NULL)) | |
| 57 return false; | |
| 58 *bytes_read = static_cast<size_t>(read); | |
| 59 #elif defined(OS_POSIX) | |
| 60 assert(bytes_to_read < | |
| 61 static_cast<size_t>(std::numeric_limits<ssize_t>::max())); | |
| 62 ssize_t ret = ::read(file, buffer, bytes_to_read); | |
| 63 if (ret == -1) | |
| 64 return false; | |
| 65 *bytes_read = static_cast<size_t>(ret); | |
| 66 #endif | |
| 67 | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 bool WriteToPlatformFile(PlatformFile file, size_t bytes_to_write, | |
| 72 const char* buffer, size_t* bytes_written) { | |
| 73 #if defined(OS_WIN) | |
| 74 assert(bytes_to_write < std::numeric_limits<DWORD>::max()); | |
| 75 DWORD written = 0; | |
| 76 if (!::WriteFile(file, buffer, static_cast<DWORD>(bytes_to_write), &written, | |
| 77 NULL)) | |
| 78 return false; | |
| 79 *bytes_written = static_cast<size_t>(written); | |
| 80 #elif defined(OS_POSIX) | |
| 81 assert(bytes_to_write < | |
| 82 static_cast<size_t>(std::numeric_limits<ssize_t>::max())); | |
| 83 ssize_t ret = ::write(file, buffer, bytes_to_write); | |
| 84 if (ret == -1) | |
| 85 return false; | |
| 86 *bytes_written = static_cast<size_t>(ret); | |
| 87 #endif | |
| 88 | |
| 89 return true; | |
| 90 } | |
| 91 | |
| 92 bool ClosePlatformFile(PlatformFile file) { | |
| 93 #if defined(OS_WIN) | |
| 94 return !!::CloseHandle(file); | |
| 95 #elif defined(OS_POSIX) | |
| 96 return !::close(file); | |
| 97 #endif | |
| 98 } | |
| 99 | |
| 100 bool WriteMessage(PlatformFile file, size_t message_len, const char* message) { | |
| 101 size_t bytes_written = 0; | |
| 102 return WriteToPlatformFile(file, message_len, message, &bytes_written) && | |
| 103 bytes_written == message_len; | |
| 104 } | |
| 105 | |
| 106 bool ReadMessage(PlatformFile file, size_t message_len, char* message) { | |
| 107 size_t bytes_read = 0; | |
| 108 return ReadFromPlatformFile(file, message_len, message, &bytes_read) && | |
| 109 bytes_read == message_len; | |
| 110 } | |
| 111 | |
| 112 bool VerifyMessage(PlatformFile file, size_t message_len, const char* message) { | |
| 113 scoped_array<char> message_received(new char[message_len]); | |
| 114 return ReadMessage(file, message_len, message_received.get()) && | |
| 115 !::strcmp(message_received.get(), message); | |
| 116 } | |
| 117 | |
| 118 // May not delete file if fails. If these tests are ever converted to Google | |
| 119 // Test, use EXPECT_* to ensure the file is deleted. | |
| 120 bool VerifyIsUnsandboxed() { | |
| 121 FILE* file = NULL; | |
| 122 | |
| 123 #if defined(OS_WIN) | |
| 124 wchar_t temp_path[MAX_PATH] = {'\0'}; | |
| 125 wchar_t file_name[MAX_PATH] = {'\0'}; | |
| 126 if (!::GetTempPath(MAX_PATH, temp_path) || | |
| 127 !::GetTempFileName(temp_path, L"test_pepper_broker", 0, file_name) || | |
| 128 ::_wfopen_s(&file, file_name, L"w")) | |
| 129 return false; | |
| 130 #elif defined(OS_POSIX) | |
| 131 char file_name[] = "/tmp/test_pepper_broker_XXXXXX"; | |
| 132 int fd = ::mkstemp(file_name); | |
| 133 if (-1 == fd) | |
| 134 return false; | |
| 135 | |
| 136 file = ::fdopen(fd, "w"); | |
| 137 if (!file) { | |
| 138 ::remove(file_name); | |
| 139 ::close(fd); | |
| 140 return false; | |
| 141 } | |
| 142 #endif | |
| 143 | |
| 144 const char text[] = "Verify that the broker is unsandboxed."; | |
| 145 const size_t text_len = sizeof(text); | |
| 146 if (text_len != ::fwrite(text, 1, text_len, file)) { | |
| 147 ::fclose(file); | |
| 148 return false; | |
| 149 } | |
| 150 | |
| 151 if (::fclose(file)) | |
| 152 return false; | |
| 153 | |
| 154 #if defined(OS_WIN) | |
| 155 if (::_wfopen_s(&file, file_name, L"r")) | |
| 156 return false; | |
| 157 #elif defined(OS_POSIX) | |
| 158 file = ::fopen(file_name, "r"); | |
| 159 if (!file) | |
| 160 return false; | |
| 161 #endif | |
| 162 | |
| 163 scoped_array<char> buffer(new char[text_len]); | |
| 164 if (text_len != ::fread(buffer.get(), 1, text_len, file)) { | |
| 165 ::fclose(file); | |
| 166 return false; | |
| 167 } | |
| 168 | |
| 169 if (::fclose(file)) // Will also close |fd| in POSIX. | |
| 170 return false; | |
| 171 | |
| 172 #if defined(OS_WIN) | |
| 173 if (!::DeleteFile(file_name)) | |
| 174 return false; | |
| 175 #elif defined(OS_POSIX) | |
| 176 if (::remove(file_name)) | |
| 177 return false; | |
| 178 #endif | |
| 179 | |
| 180 return true; | |
| 181 } | |
| 182 | |
| 183 // Callback in the broker when a new broker connection occurs. | |
| 184 int32_t OnInstanceConnected(PP_Instance instance, int32_t handle) { | |
| 185 PlatformFile file = IntToPlatformFile(handle); | |
| 186 if (file == kInvalidPlatformFileValue) | |
| 187 return PP_ERROR_FAILED; | |
| 188 | |
| 189 // Send hello message. | |
| 190 if (!WriteMessage(file, sizeof(kHelloMessage), kHelloMessage)) { | |
| 191 ClosePlatformFile(file); | |
| 192 return PP_ERROR_FAILED; | |
| 193 } | |
| 194 | |
| 195 // Verify broker is not sandboxed and send result to plugin over the pipe. | |
| 196 if (VerifyIsUnsandboxed()) { | |
| 197 if (!WriteMessage(file, sizeof(kBrokerUnsandboxed), kBrokerUnsandboxed)) { | |
| 198 ClosePlatformFile(file); | |
| 199 return PP_ERROR_FAILED; | |
| 200 } | |
| 201 } | |
| 202 // If kBrokerUnsandboxed is not received by the plugin, the plugin knows that | |
| 203 // the broker is sandboxed. So no "else" needed here. | |
| 204 | |
| 205 if (!ClosePlatformFile(file)) | |
| 206 return PP_ERROR_FAILED; | |
| 207 | |
| 208 return PP_OK; | |
| 209 } | |
| 210 | |
| 211 } // namespace | |
| 212 | |
| 213 PP_EXPORT int32_t PPP_InitializeBroker( | |
| 214 PP_ConnectInstance_Func* connect_instance_func) { | |
| 215 *connect_instance_func = &OnInstanceConnected; | |
| 216 return PP_OK; | |
| 217 } | |
| 218 | |
| 219 PP_EXPORT void PPP_ShutdownBroker() {} | |
| 220 | |
| 14 TestBroker::TestBroker(TestingInstance* instance) | 221 TestBroker::TestBroker(TestingInstance* instance) |
| 15 : TestCase(instance), | 222 : TestCase(instance), |
| 16 broker_interface_(NULL) { | 223 broker_interface_(NULL) { |
| 17 } | 224 } |
| 18 | 225 |
| 19 bool TestBroker::Init() { | 226 bool TestBroker::Init() { |
| 20 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( | 227 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( |
| 21 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); | 228 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); |
| 22 return !!broker_interface_; | 229 return !!broker_interface_; |
| 23 } | 230 } |
| 24 | 231 |
| 25 void TestBroker::RunTest() { | 232 void TestBroker::RunTest() { |
| 26 RUN_TEST(Create); | 233 RUN_TEST(Create); |
| 234 RUN_TEST(GetHandleFailure); | |
| 235 RUN_TEST(ConnectFailure); | |
| 236 #if !defined(OS_WIN) // This is broken on Windows. See http://crbug.com/103975. | |
| 237 RUN_TEST(ConnectAndPipe); | |
| 238 #endif | |
| 27 } | 239 } |
| 28 | 240 |
| 29 std::string TestBroker::TestCreate() { | 241 std::string TestBroker::TestCreate() { |
| 30 // Very simplistic test to make sure we can create a broker interface. | 242 // Very simplistic test to make sure we can create a broker interface. |
| 31 PP_Resource broker = broker_interface_->CreateTrusted( | 243 PP_Resource broker = broker_interface_->CreateTrusted( |
| 32 instance_->pp_instance()); | 244 instance_->pp_instance()); |
| 33 ASSERT_TRUE(broker); | 245 ASSERT_TRUE(broker); |
| 34 | 246 |
| 35 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); | 247 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); |
| 36 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); | 248 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); |
| 37 | 249 |
| 38 // Test getting the handle for an invalid resource. | 250 PASS(); |
| 39 int32_t handle; | 251 } |
| 40 ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE); | |
| 41 | 252 |
| 42 // Connect hasn't been called so this should fail. | 253 // Test connection on invalid resource. |
| 43 ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED); | 254 std::string TestBroker::TestConnectFailure() { |
| 255 // Callback NOT force asynced. Connect should fail. The callback will not be | |
| 256 // posted so there's no need to wait for the callback to complete. | |
| 257 TestCompletionCallback cb_1(instance_->pp_instance(), false); | |
| 258 ASSERT_EQ(PP_ERROR_BADRESOURCE, | |
| 259 broker_interface_->Connect( | |
| 260 0, pp::CompletionCallback(cb_1).pp_completion_callback())); | |
| 261 | |
| 262 // Callback force asynced. Connect will return PP_OK_COMPLETIONPENDING and the | |
|
ddorwin
2011/11/14 22:34:50
"forced async" is probably better here and above.
xhwang
2011/11/14 22:50:19
Done.
| |
| 263 // callback will be posted. However, the callback should fail. | |
| 264 TestCompletionCallback cb_2(instance_->pp_instance(), true); | |
| 265 ASSERT_EQ(PP_OK_COMPLETIONPENDING, | |
| 266 broker_interface_->Connect( | |
| 267 0, pp::CompletionCallback(cb_2).pp_completion_callback())); | |
| 268 ASSERT_EQ(PP_ERROR_BADRESOURCE, cb_2.WaitForResult()); | |
| 44 | 269 |
| 45 PASS(); | 270 PASS(); |
| 46 } | 271 } |
| 272 | |
| 273 std::string TestBroker::TestGetHandleFailure() { | |
| 274 int32_t handle = kInvalidHandle; | |
| 275 | |
| 276 // Test getting the handle for an invalid resource. | |
| 277 ASSERT_EQ(PP_ERROR_BADRESOURCE, broker_interface_->GetHandle(0, &handle)); | |
| 278 | |
| 279 // Connect hasn't been called so this should fail. | |
| 280 PP_Resource broker = broker_interface_->CreateTrusted( | |
| 281 instance_->pp_instance()); | |
| 282 ASSERT_TRUE(broker); | |
| 283 ASSERT_EQ(PP_ERROR_FAILED, broker_interface_->GetHandle(broker, &handle)); | |
| 284 | |
| 285 PASS(); | |
| 286 } | |
| 287 | |
| 288 std::string TestBroker::TestConnectAndPipe() { | |
| 289 PP_Resource broker = broker_interface_->CreateTrusted( | |
| 290 instance_->pp_instance()); | |
| 291 ASSERT_TRUE(broker); | |
| 292 | |
| 293 TestCompletionCallback cb_3(instance_->pp_instance()); | |
| 294 ASSERT_EQ(PP_OK_COMPLETIONPENDING, | |
| 295 broker_interface_->Connect( | |
| 296 broker, pp::CompletionCallback(cb_3).pp_completion_callback())); | |
| 297 ASSERT_EQ(PP_OK, cb_3.WaitForResult()); | |
| 298 | |
| 299 int32_t handle = kInvalidHandle; | |
| 300 ASSERT_EQ(PP_OK, broker_interface_->GetHandle(broker, &handle)); | |
| 301 ASSERT_NE(kInvalidHandle, handle); | |
| 302 | |
| 303 PlatformFile file = IntToPlatformFile(handle); | |
| 304 ASSERT_TRUE(VerifyMessage(file, sizeof(kHelloMessage), kHelloMessage)); | |
| 305 ASSERT_TRUE(VerifyMessage(file, sizeof(kBrokerUnsandboxed), | |
| 306 kBrokerUnsandboxed)); | |
| 307 | |
| 308 ASSERT_TRUE(ClosePlatformFile(file)); | |
| 309 | |
| 310 PASS(); | |
| 311 } | |
| OLD | NEW |