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" | |
|
piman
2011/11/14 23:49:17
Should be written <windows.h>
xhwang
2011/11/15 01:52:49
Done.
| |
| 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); | |
|
piman
2011/11/14 23:49:17
FYI, ::read and ::write may fail to fully read/wri
xhwang
2011/11/15 01:52:49
Done.
| |
| 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 | |
|
piman
2011/11/14 23:49:17
I'd say, at this point, you should know whether yo
xhwang
2011/11/15 01:52:49
Done.
| |
| 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 file = NULL; | |
| 155 | |
| 156 #if defined(OS_WIN) | |
| 157 if (::_wfopen_s(&file, file_name, L"r")) | |
| 158 return false; | |
| 159 #elif defined(OS_POSIX) | |
| 160 file = ::fopen(file_name, "r"); | |
| 161 if (!file) | |
| 162 return false; | |
| 163 #endif | |
| 164 | |
| 165 scoped_array<char> buffer(new char[text_len]); | |
| 166 if (text_len != ::fread(buffer.get(), 1, text_len, file)) { | |
| 167 ::fclose(file); | |
| 168 return false; | |
| 169 } | |
| 170 | |
| 171 if (::fclose(file)) // Will also close |fd| in POSIX. | |
| 172 return false; | |
| 173 | |
| 174 #if defined(OS_WIN) | |
| 175 if (!::DeleteFile(file_name)) | |
| 176 return false; | |
| 177 #elif defined(OS_POSIX) | |
| 178 if (::remove(file_name)) | |
| 179 return false; | |
| 180 #endif | |
| 181 | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 // Callback in the broker when a new broker connection occurs. | |
| 186 int32_t OnInstanceConnected(PP_Instance instance, int32_t handle) { | |
| 187 PlatformFile file = IntToPlatformFile(handle); | |
| 188 if (file == kInvalidPlatformFileValue) | |
| 189 return PP_ERROR_FAILED; | |
| 190 | |
| 191 // Send hello message. | |
| 192 if (!WriteMessage(file, sizeof(kHelloMessage), kHelloMessage)) { | |
| 193 ClosePlatformFile(file); | |
| 194 return PP_ERROR_FAILED; | |
| 195 } | |
| 196 | |
| 197 // Verify broker is not sandboxed and send result to plugin over the pipe. | |
| 198 if (VerifyIsUnsandboxed()) { | |
| 199 if (!WriteMessage(file, sizeof(kBrokerUnsandboxed), kBrokerUnsandboxed)) { | |
|
piman
2011/11/14 23:49:17
It would be better I think to write a response mes
xhwang
2011/11/15 01:52:49
Done.
| |
| 200 ClosePlatformFile(file); | |
| 201 return PP_ERROR_FAILED; | |
| 202 } | |
| 203 } | |
| 204 // If kBrokerUnsandboxed is not received by the plugin, the plugin knows that | |
| 205 // the broker is sandboxed. So no "else" needed here. | |
| 206 | |
| 207 if (!ClosePlatformFile(file)) | |
| 208 return PP_ERROR_FAILED; | |
| 209 | |
| 210 return PP_OK; | |
| 211 } | |
| 212 | |
| 213 } // namespace | |
| 214 | |
| 215 PP_EXPORT int32_t PPP_InitializeBroker( | |
| 216 PP_ConnectInstance_Func* connect_instance_func) { | |
| 217 *connect_instance_func = &OnInstanceConnected; | |
| 218 return PP_OK; | |
| 219 } | |
| 220 | |
| 221 PP_EXPORT void PPP_ShutdownBroker() {} | |
| 222 | |
| 14 TestBroker::TestBroker(TestingInstance* instance) | 223 TestBroker::TestBroker(TestingInstance* instance) |
| 15 : TestCase(instance), | 224 : TestCase(instance), |
| 16 broker_interface_(NULL) { | 225 broker_interface_(NULL) { |
| 17 } | 226 } |
| 18 | 227 |
| 19 bool TestBroker::Init() { | 228 bool TestBroker::Init() { |
| 20 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( | 229 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( |
| 21 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); | 230 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); |
| 22 return !!broker_interface_; | 231 return !!broker_interface_; |
| 23 } | 232 } |
| 24 | 233 |
| 25 void TestBroker::RunTest() { | 234 void TestBroker::RunTest() { |
| 26 RUN_TEST(Create); | 235 RUN_TEST(Create); |
| 236 RUN_TEST(GetHandleFailure); | |
| 237 RUN_TEST(ConnectFailure); | |
| 238 #if !defined(OS_WIN) // This is broken on Windows. See http://crbug.com/103975. | |
| 239 RUN_TEST(ConnectAndPipe); | |
| 240 #endif | |
| 27 } | 241 } |
| 28 | 242 |
| 29 std::string TestBroker::TestCreate() { | 243 std::string TestBroker::TestCreate() { |
| 30 // Very simplistic test to make sure we can create a broker interface. | 244 // Very simplistic test to make sure we can create a broker interface. |
| 31 PP_Resource broker = broker_interface_->CreateTrusted( | 245 PP_Resource broker = broker_interface_->CreateTrusted( |
| 32 instance_->pp_instance()); | 246 instance_->pp_instance()); |
| 33 ASSERT_TRUE(broker); | 247 ASSERT_TRUE(broker); |
| 34 | 248 |
| 35 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); | 249 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); |
| 36 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); | 250 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); |
| 37 | 251 |
| 38 // Test getting the handle for an invalid resource. | 252 PASS(); |
| 39 int32_t handle; | 253 } |
| 40 ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE); | |
| 41 | 254 |
| 42 // Connect hasn't been called so this should fail. | 255 // Test connection on invalid resource. |
| 43 ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED); | 256 std::string TestBroker::TestConnectFailure() { |
| 257 // Callback NOT force async. Connect should fail. The callback will not be | |
| 258 // posted so there's no need to wait for the callback to complete. | |
| 259 TestCompletionCallback cb_1(instance_->pp_instance(), false); | |
| 260 ASSERT_EQ(PP_ERROR_BADRESOURCE, | |
| 261 broker_interface_->Connect( | |
| 262 0, pp::CompletionCallback(cb_1).pp_completion_callback())); | |
| 263 | |
| 264 // Callback force async. Connect will return PP_OK_COMPLETIONPENDING and the | |
| 265 // callback will be posted. However, the callback should fail. | |
| 266 TestCompletionCallback cb_2(instance_->pp_instance(), true); | |
| 267 ASSERT_EQ(PP_OK_COMPLETIONPENDING, | |
| 268 broker_interface_->Connect( | |
| 269 0, pp::CompletionCallback(cb_2).pp_completion_callback())); | |
| 270 ASSERT_EQ(PP_ERROR_BADRESOURCE, cb_2.WaitForResult()); | |
| 44 | 271 |
| 45 PASS(); | 272 PASS(); |
| 46 } | 273 } |
| 274 | |
| 275 std::string TestBroker::TestGetHandleFailure() { | |
| 276 int32_t handle = kInvalidHandle; | |
| 277 | |
| 278 // Test getting the handle for an invalid resource. | |
| 279 ASSERT_EQ(PP_ERROR_BADRESOURCE, broker_interface_->GetHandle(0, &handle)); | |
| 280 | |
| 281 // Connect hasn't been called so this should fail. | |
| 282 PP_Resource broker = broker_interface_->CreateTrusted( | |
| 283 instance_->pp_instance()); | |
| 284 ASSERT_TRUE(broker); | |
| 285 ASSERT_EQ(PP_ERROR_FAILED, broker_interface_->GetHandle(broker, &handle)); | |
| 286 | |
| 287 PASS(); | |
| 288 } | |
| 289 | |
| 290 std::string TestBroker::TestConnectAndPipe() { | |
| 291 PP_Resource broker = broker_interface_->CreateTrusted( | |
| 292 instance_->pp_instance()); | |
| 293 ASSERT_TRUE(broker); | |
| 294 | |
| 295 TestCompletionCallback cb_3(instance_->pp_instance()); | |
| 296 ASSERT_EQ(PP_OK_COMPLETIONPENDING, | |
| 297 broker_interface_->Connect( | |
| 298 broker, pp::CompletionCallback(cb_3).pp_completion_callback())); | |
| 299 ASSERT_EQ(PP_OK, cb_3.WaitForResult()); | |
| 300 | |
| 301 int32_t handle = kInvalidHandle; | |
| 302 ASSERT_EQ(PP_OK, broker_interface_->GetHandle(broker, &handle)); | |
| 303 ASSERT_NE(kInvalidHandle, handle); | |
| 304 | |
| 305 PlatformFile file = IntToPlatformFile(handle); | |
| 306 ASSERT_TRUE(VerifyMessage(file, sizeof(kHelloMessage), kHelloMessage)); | |
| 307 ASSERT_TRUE(VerifyMessage(file, sizeof(kBrokerUnsandboxed), | |
| 308 kBrokerUnsandboxed)); | |
| 309 | |
| 310 ASSERT_TRUE(ClosePlatformFile(file)); | |
| 311 | |
| 312 PASS(); | |
| 313 } | |
| OLD | NEW |