| 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 #if defined(_MSC_VER) |
| 8 #define OS_WIN 1 |
| 9 #include <windows.h> |
| 10 #else |
| 11 #define OS_POSIX 1 |
| 12 #include <errno.h> |
| 13 #endif |
| 14 |
| 15 #include <cstdio> |
| 16 #include <cstring> |
| 17 #include <fstream> |
| 18 #include <limits> |
| 19 |
| 7 #include "ppapi/c/pp_errors.h" | 20 #include "ppapi/c/pp_errors.h" |
| 21 #include "ppapi/c/trusted/ppp_broker.h" |
| 8 #include "ppapi/c/trusted/ppb_broker_trusted.h" | 22 #include "ppapi/c/trusted/ppb_broker_trusted.h" |
| 9 #include "ppapi/cpp/module.h" | 23 #include "ppapi/tests/test_utils.h" |
| 10 #include "ppapi/tests/testing_instance.h" | 24 #include "ppapi/tests/testing_instance.h" |
| 11 | 25 |
| 12 REGISTER_TEST_CASE(Broker); | 26 REGISTER_TEST_CASE(Broker); |
| 13 | 27 |
| 28 namespace { |
| 29 |
| 30 const char kHelloMessage[] = "Hello Plugin! This is Broker!"; |
| 31 // Message sent from broker to plugin if the broker is unsandboxed. |
| 32 const char kBrokerUnsandboxed[] = "Broker is Unsandboxed!"; |
| 33 // Message sent from broker to plugin if the broker is sandboxed. This message |
| 34 // needs to be longer than |kBrokerUnsandboxed| because the plugin is expecting |
| 35 // |kBrokerUnsandboxed|. If it's shorter and the broker doesn't close its handle |
| 36 // properly the plugin will hang waiting for all data of |kBrokerUnsandboxed| to |
| 37 // be read. |
| 38 const char kBrokerSandboxed[] = "Broker is Sandboxed! Verification failed!"; |
| 39 |
| 40 #if defined(OS_WIN) |
| 41 typedef HANDLE PlatformFile; |
| 42 const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; |
| 43 const int32_t kInvalidHandle = static_cast<int32_t>( |
| 44 reinterpret_cast<intptr_t>(INVALID_HANDLE_VALUE)); |
| 45 #elif defined(OS_POSIX) |
| 46 typedef int PlatformFile; |
| 47 const PlatformFile kInvalidPlatformFileValue = -1; |
| 48 const int32_t kInvalidHandle = -1; |
| 49 #endif |
| 50 |
| 51 PlatformFile IntToPlatformFile(int32_t handle) { |
| 52 #if defined(OS_WIN) |
| 53 return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle)); |
| 54 #elif defined(OS_POSIX) |
| 55 return handle; |
| 56 #endif |
| 57 } |
| 58 |
| 59 #if defined(OS_POSIX) |
| 60 #define HANDLE_EINTR(x) ({ \ |
| 61 typeof(x) __eintr_result__; \ |
| 62 do { \ |
| 63 __eintr_result__ = x; \ |
| 64 } while (__eintr_result__ == -1 && errno == EINTR); \ |
| 65 __eintr_result__;\ |
| 66 }) |
| 67 #endif |
| 68 |
| 69 bool ReadMessage(PlatformFile file, size_t message_len, char* message) { |
| 70 #if defined(OS_WIN) |
| 71 assert(message_len < std::numeric_limits<DWORD>::max()); |
| 72 DWORD read = 0; |
| 73 const DWORD size = static_cast<DWORD>(message_len); |
| 74 return ::ReadFile(file, message, size, &read, NULL) && read == size; |
| 75 #elif defined(OS_POSIX) |
| 76 assert(message_len < |
| 77 static_cast<size_t>(std::numeric_limits<ssize_t>::max())); |
| 78 size_t total_read = 0; |
| 79 while (total_read < message_len) { |
| 80 ssize_t read = HANDLE_EINTR(::read(file, message + total_read, |
| 81 message_len - total_read)); |
| 82 if (read <= 0) |
| 83 break; |
| 84 total_read += read; |
| 85 } |
| 86 return total_read == message_len; |
| 87 #endif |
| 88 } |
| 89 |
| 90 bool WriteMessage(PlatformFile file, size_t message_len, const char* message) { |
| 91 #if defined(OS_WIN) |
| 92 assert(message_len < std::numeric_limits<DWORD>::max()); |
| 93 DWORD written = 0; |
| 94 const DWORD size = static_cast<DWORD>(message_len); |
| 95 return ::WriteFile(file, message, size, &written, NULL) && written == size; |
| 96 #elif defined(OS_POSIX) |
| 97 assert(message_len < |
| 98 static_cast<size_t>(std::numeric_limits<ssize_t>::max())); |
| 99 size_t total_written = 0; |
| 100 while (total_written < message_len) { |
| 101 ssize_t written = HANDLE_EINTR(::write(file, message + total_written, |
| 102 message_len - total_written)); |
| 103 if (written <= 0) |
| 104 break; |
| 105 total_written += written; |
| 106 } |
| 107 return total_written == message_len; |
| 108 #endif |
| 109 } |
| 110 |
| 111 bool VerifyMessage(PlatformFile file, size_t message_len, const char* message) { |
| 112 char* message_received = new char[message_len]; |
| 113 bool success = ReadMessage(file, message_len, message_received) && |
| 114 !::strcmp(message_received, message); |
| 115 delete [] message_received; |
| 116 return success; |
| 117 } |
| 118 |
| 119 bool ClosePlatformFile(PlatformFile file) { |
| 120 #if defined(OS_WIN) |
| 121 return !!::CloseHandle(file); |
| 122 #elif defined(OS_POSIX) |
| 123 return !::close(file); |
| 124 #endif |
| 125 } |
| 126 |
| 127 bool VerifyIsUnsandboxed() { |
| 128 #if defined(OS_WIN) |
| 129 FILE* file = NULL; |
| 130 wchar_t temp_path[MAX_PATH] = {'\0'}; |
| 131 wchar_t file_name[MAX_PATH] = {'\0'}; |
| 132 if (!::GetTempPath(MAX_PATH, temp_path) || |
| 133 !::GetTempFileName(temp_path, L"test_pepper_broker", 0, file_name) || |
| 134 ::_wfopen_s(&file, file_name, L"w")) |
| 135 return false; |
| 136 |
| 137 if (::fclose(file)) { |
| 138 ::DeleteFile(file_name); |
| 139 return false; |
| 140 } |
| 141 |
| 142 return !!::DeleteFile(file_name); |
| 143 #elif defined(OS_POSIX) |
| 144 char file_name[] = "/tmp/test_pepper_broker_XXXXXX"; |
| 145 int fd = ::mkstemp(file_name); |
| 146 if (-1 == fd) |
| 147 return false; |
| 148 |
| 149 if (::close(fd)) { |
| 150 ::remove(file_name); |
| 151 return false; |
| 152 } |
| 153 |
| 154 return !::remove(file_name); |
| 155 #endif |
| 156 } |
| 157 |
| 158 // Callback in the broker when a new broker connection occurs. |
| 159 int32_t OnInstanceConnected(PP_Instance instance, int32_t handle) { |
| 160 PlatformFile file = IntToPlatformFile(handle); |
| 161 if (file == kInvalidPlatformFileValue) |
| 162 return PP_ERROR_FAILED; |
| 163 |
| 164 // Send hello message. |
| 165 if (!WriteMessage(file, sizeof(kHelloMessage), kHelloMessage)) { |
| 166 ClosePlatformFile(file); |
| 167 return PP_ERROR_FAILED; |
| 168 } |
| 169 |
| 170 // Verify broker is not sandboxed and send result to plugin over the pipe. |
| 171 if (VerifyIsUnsandboxed()) { |
| 172 if (!WriteMessage(file, sizeof(kBrokerUnsandboxed), kBrokerUnsandboxed)) { |
| 173 ClosePlatformFile(file); |
| 174 return PP_ERROR_FAILED; |
| 175 } |
| 176 } else { |
| 177 if (!WriteMessage(file, sizeof(kBrokerSandboxed), kBrokerSandboxed)) { |
| 178 ClosePlatformFile(file); |
| 179 return PP_ERROR_FAILED; |
| 180 } |
| 181 } |
| 182 |
| 183 if (!ClosePlatformFile(file)) |
| 184 return PP_ERROR_FAILED; |
| 185 |
| 186 return PP_OK; |
| 187 } |
| 188 |
| 189 } // namespace |
| 190 |
| 191 PP_EXPORT int32_t PPP_InitializeBroker( |
| 192 PP_ConnectInstance_Func* connect_instance_func) { |
| 193 *connect_instance_func = &OnInstanceConnected; |
| 194 return PP_OK; |
| 195 } |
| 196 |
| 197 PP_EXPORT void PPP_ShutdownBroker() {} |
| 198 |
| 14 TestBroker::TestBroker(TestingInstance* instance) | 199 TestBroker::TestBroker(TestingInstance* instance) |
| 15 : TestCase(instance), | 200 : TestCase(instance), |
| 16 broker_interface_(NULL) { | 201 broker_interface_(NULL) { |
| 17 } | 202 } |
| 18 | 203 |
| 19 bool TestBroker::Init() { | 204 bool TestBroker::Init() { |
| 20 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( | 205 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( |
| 21 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); | 206 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); |
| 22 return !!broker_interface_; | 207 return !!broker_interface_; |
| 23 } | 208 } |
| 24 | 209 |
| 25 void TestBroker::RunTest() { | 210 void TestBroker::RunTest() { |
| 26 RUN_TEST(Create); | 211 RUN_TEST(Create); |
| 212 RUN_TEST(GetHandleFailure); |
| 213 RUN_TEST(ConnectFailure); |
| 214 #if !defined(OS_WIN) // This is broken on Windows. See http://crbug.com/103975. |
| 215 RUN_TEST(ConnectAndPipe); |
| 216 #endif |
| 27 } | 217 } |
| 28 | 218 |
| 29 std::string TestBroker::TestCreate() { | 219 std::string TestBroker::TestCreate() { |
| 30 // Very simplistic test to make sure we can create a broker interface. | 220 // Very simplistic test to make sure we can create a broker interface. |
| 31 PP_Resource broker = broker_interface_->CreateTrusted( | 221 PP_Resource broker = broker_interface_->CreateTrusted( |
| 32 instance_->pp_instance()); | 222 instance_->pp_instance()); |
| 33 ASSERT_TRUE(broker); | 223 ASSERT_TRUE(broker); |
| 34 | 224 |
| 35 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); | 225 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); |
| 36 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); | 226 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); |
| 37 | 227 |
| 38 // Test getting the handle for an invalid resource. | 228 PASS(); |
| 39 int32_t handle; | 229 } |
| 40 ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE); | |
| 41 | 230 |
| 42 // Connect hasn't been called so this should fail. | 231 // Test connection on invalid resource. |
| 43 ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED); | 232 std::string TestBroker::TestConnectFailure() { |
| 233 // Callback NOT force async. Connect should fail. The callback will not be |
| 234 // posted so there's no need to wait for the callback to complete. |
| 235 TestCompletionCallback cb_1(instance_->pp_instance(), false); |
| 236 ASSERT_EQ(PP_ERROR_BADRESOURCE, |
| 237 broker_interface_->Connect( |
| 238 0, pp::CompletionCallback(cb_1).pp_completion_callback())); |
| 239 |
| 240 // Callback force async. Connect will return PP_OK_COMPLETIONPENDING and the |
| 241 // callback will be posted. However, the callback should fail. |
| 242 TestCompletionCallback cb_2(instance_->pp_instance(), true); |
| 243 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 244 broker_interface_->Connect( |
| 245 0, pp::CompletionCallback(cb_2).pp_completion_callback())); |
| 246 ASSERT_EQ(PP_ERROR_BADRESOURCE, cb_2.WaitForResult()); |
| 44 | 247 |
| 45 PASS(); | 248 PASS(); |
| 46 } | 249 } |
| 250 |
| 251 std::string TestBroker::TestGetHandleFailure() { |
| 252 int32_t handle = kInvalidHandle; |
| 253 |
| 254 // Test getting the handle for an invalid resource. |
| 255 ASSERT_EQ(PP_ERROR_BADRESOURCE, broker_interface_->GetHandle(0, &handle)); |
| 256 |
| 257 // Connect hasn't been called so this should fail. |
| 258 PP_Resource broker = broker_interface_->CreateTrusted( |
| 259 instance_->pp_instance()); |
| 260 ASSERT_TRUE(broker); |
| 261 ASSERT_EQ(PP_ERROR_FAILED, broker_interface_->GetHandle(broker, &handle)); |
| 262 |
| 263 PASS(); |
| 264 } |
| 265 |
| 266 std::string TestBroker::TestConnectAndPipe() { |
| 267 PP_Resource broker = broker_interface_->CreateTrusted( |
| 268 instance_->pp_instance()); |
| 269 ASSERT_TRUE(broker); |
| 270 |
| 271 TestCompletionCallback cb_3(instance_->pp_instance()); |
| 272 ASSERT_EQ(PP_OK_COMPLETIONPENDING, |
| 273 broker_interface_->Connect( |
| 274 broker, pp::CompletionCallback(cb_3).pp_completion_callback())); |
| 275 ASSERT_EQ(PP_OK, cb_3.WaitForResult()); |
| 276 |
| 277 int32_t handle = kInvalidHandle; |
| 278 ASSERT_EQ(PP_OK, broker_interface_->GetHandle(broker, &handle)); |
| 279 ASSERT_NE(kInvalidHandle, handle); |
| 280 |
| 281 PlatformFile file = IntToPlatformFile(handle); |
| 282 ASSERT_TRUE(VerifyMessage(file, sizeof(kHelloMessage), kHelloMessage)); |
| 283 ASSERT_TRUE(VerifyMessage(file, sizeof(kBrokerUnsandboxed), |
| 284 kBrokerUnsandboxed)); |
| 285 |
| 286 ASSERT_TRUE(ClosePlatformFile(file)); |
| 287 |
| 288 PASS(); |
| 289 } |
| OLD | NEW |