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/eintr_wrapper.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 7 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
| 15 #include "ppapi/c/trusted/ppp_broker.h" | |
| 8 #include "ppapi/c/trusted/ppb_broker_trusted.h" | 16 #include "ppapi/c/trusted/ppb_broker_trusted.h" |
| 9 #include "ppapi/cpp/module.h" | 17 #include "ppapi/tests/test_utils.h" |
| 10 #include "ppapi/tests/testing_instance.h" | 18 #include "ppapi/tests/testing_instance.h" |
| 11 | 19 |
| 20 #if defined(OS_WIN) | |
| 21 #include "windows.h" | |
| 22 #elif !defined(OS_POSIX) | |
| 23 #error Not implemented. Don't remove as it's only checked here in this file. | |
| 24 #endif | |
| 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 bool ReadMessage(PlatformFile file, size_t message_len, char* message) { | |
| 60 #if defined(OS_WIN) | |
| 61 assert(message_len < std::numeric_limits<DWORD>::max()); | |
| 62 DWORD read = 0; | |
| 63 const DWORD size = static_cast<DWORD>(message_len); | |
| 64 return ::ReadFile(file, message, size, &read, NULL) && read == size; | |
| 65 #elif defined(OS_POSIX) | |
| 66 assert(message_len < | |
| 67 static_cast<size_t>(std::numeric_limits<ssize_t>::max())); | |
| 68 size_t total_read = 0; | |
| 69 while (total_read < message_len) { | |
| 70 ssize_t read = HANDLE_EINTR(::read(file, message + total_read, | |
| 71 message_len - total_read)); | |
| 72 if (read <= 0) | |
| 73 break; | |
| 74 total_read += read; | |
| 75 } | |
| 76 return total_read == message_len; | |
| 77 #endif | |
| 78 } | |
| 79 | |
| 80 bool WriteMessage(PlatformFile file, size_t message_len, const char* message) { | |
| 81 #if defined(OS_WIN) | |
| 82 assert(message_len < std::numeric_limits<DWORD>::max()); | |
| 83 DWORD written = 0; | |
| 84 const DWORD size = static_cast<DWORD>(message_len); | |
| 85 return ::WriteFile(file, message, size, &written, NULL) && written == size; | |
| 86 #elif defined(OS_POSIX) | |
| 87 assert(message_len < | |
| 88 static_cast<size_t>(std::numeric_limits<ssize_t>::max())); | |
| 89 size_t total_written = 0; | |
| 90 while (total_written < message_len) { | |
| 91 ssize_t written = HANDLE_EINTR(::write(file, message + total_written, | |
| 92 message_len - total_written)); | |
| 93 if (written <= 0) | |
| 94 break; | |
| 95 total_written += written; | |
| 96 } | |
| 97 return total_written == message_len; | |
| 98 #endif | |
| 99 } | |
| 100 | |
| 101 bool VerifyMessage(PlatformFile file, size_t message_len, const char* message) { | |
| 102 scoped_array<char> message_received(new char[message_len]); | |
| 103 return ReadMessage(file, message_len, message_received.get()) && | |
| 104 !::strcmp(message_received.get(), message); | |
| 105 } | |
| 106 | |
| 107 bool ClosePlatformFile(PlatformFile file) { | |
| 108 #if defined(OS_WIN) | |
| 109 return !!::CloseHandle(file); | |
| 110 #elif defined(OS_POSIX) | |
| 111 return !::close(file); | |
| 112 #endif | |
| 113 } | |
| 114 | |
| 115 bool VerifyIsUnsandboxed() { | |
| 116 FILE* file = NULL; | |
| 117 | |
| 118 #if defined(OS_WIN) | |
| 119 wchar_t temp_path[MAX_PATH] = {'\0'}; | |
| 120 wchar_t file_name[MAX_PATH] = {'\0'}; | |
| 121 if (!::GetTempPath(MAX_PATH, temp_path) || | |
| 122 !::GetTempFileName(temp_path, L"test_pepper_broker", 0, file_name) || | |
| 123 ::_wfopen_s(&file, file_name, L"w")) | |
| 124 return false; | |
| 125 | |
| 126 if (::fclose(file)) { | |
| 127 ::DeleteFile(file_name); | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 131 if (!::DeleteFile(file_name)) | |
| 132 return false; | |
| 133 #elif defined(OS_POSIX) | |
| 134 char file_name[] = "/tmp/test_pepper_broker_XXXXXX"; | |
| 135 int fd = ::mkstemp(file_name); | |
| 136 if (-1 == fd) | |
| 137 return false; | |
| 138 | |
| 139 file = ::fdopen(fd, "w"); | |
|
piman
2011/11/15 01:58:14
I'd simplify it even further: fdopen doesn't do an
xhwang
2011/11/15 04:23:28
Done. Thanks!
| |
| 140 if (!file) { | |
| 141 ::remove(file_name); | |
| 142 ::close(fd); | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 if (::fclose(file)) { // Will also close |fd|. | |
| 147 ::remove(file_name); | |
| 148 return false; | |
| 149 } | |
| 150 | |
| 151 if (::remove(file_name)) | |
| 152 return false; | |
| 153 #endif | |
| 154 | |
| 155 return true; | |
| 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 |