Chromium Code Reviews| Index: ppapi/tests/test_broker.cc |
| diff --git a/ppapi/tests/test_broker.cc b/ppapi/tests/test_broker.cc |
| index b1d5b028c58cf2adedbc4f45237335461277dfb4..ad953394f25a6e2fcd5720e098d46da290003581 100644 |
| --- a/ppapi/tests/test_broker.cc |
| +++ b/ppapi/tests/test_broker.cc |
| @@ -4,13 +4,97 @@ |
| #include "ppapi/tests/test_broker.h" |
| +#include <cstring> |
| +#include <fstream> |
| + |
| +#include "base/platform_file.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/scoped_temp_dir.h" |
| #include "ppapi/c/pp_errors.h" |
| +#include "ppapi/c/trusted/ppp_broker.h" |
| #include "ppapi/c/trusted/ppb_broker_trusted.h" |
| -#include "ppapi/cpp/module.h" |
| +#include "ppapi/tests/test_utils.h" |
| #include "ppapi/tests/testing_instance.h" |
| REGISTER_TEST_CASE(Broker); |
| +namespace { |
| + |
| +const char kHelloMessage[] = "Hello Plugin! This is Broker!"; |
| +const char kBrokerIsUnsandboxed[] = "Broker is Unsandboxed!"; |
| + |
| +// FIXME: merge all PlatformFileToInt definitions and move to a common place. |
|
ddorwin
2011/10/27 23:21:24
Chrome uses TODO(id). WebKit uses FIXME.
xhwang
2011/10/28 01:21:01
Done.
|
| +int32_t PlatformFileToInt(base::PlatformFile handle) { |
| +#if defined(OS_WIN) |
| + return static_cast<int32_t>(reinterpret_cast<intptr_t>(handle)); |
| +#elif defined(OS_POSIX) |
| + return handle; |
| +#else |
| + #error Not implemented. |
| +#endif |
| +} |
| + |
| +bool VerifyIsUnsandboxed() { |
| + std::fstream fs; |
| + |
| + // Temporary directory will be cleaned when this object goes out of scope. |
| + ScopedTempDir temp_dir_; |
| + if (!temp_dir_.CreateUniqueTempDir()) |
| + return false; |
| + |
| + FilePath test_file_name = |
| + temp_dir_.path().Append(FILE_PATH_LITERAL("verify_sandbox.txt")); |
|
ddorwin
2011/10/27 23:21:24
indent 4.
verify implies it should be sandboxed. M
xhwang
2011/10/28 01:21:01
Done.
|
| + |
| + fs.open(test_file_name.value().c_str(), std::fstream::out); |
| + if (!fs) return false; |
|
ddorwin
2011/10/27 23:21:24
While it makes the code harder to read, these shou
xhwang
2011/10/28 01:21:01
Done.
|
| + fs << "Verify if the broker is unsandboxed."; |
|
ddorwin
2011/10/27 23:21:24
if => that
xhwang
2011/10/28 01:21:01
Done.
|
| + if (!fs) return false; |
| + fs.close(); |
| + |
| + fs.open(test_file_name.value().c_str(), std::fstream::in); |
| + if (!fs) return false; |
| + char buffer[16] = {0}; |
| + fs.read(buffer, 8); |
| + if (!fs) return false; |
| + fs.close(); |
| + |
| + return true; |
| +} |
| + |
| +bool VerifyMessage(int32_t handle, const char* message_sent, size_t length) { |
| + scoped_array<char> message_received(new char[length]); |
| + ::read(handle, message_received.get(), length); |
|
ddorwin
2011/10/27 23:21:24
This part is OS-specific. You might be able to use
xhwang
2011/10/28 01:21:01
Done.
|
| + return !::strcmp(message_received.get(), message_sent); |
| +} |
| + |
| +// Callback in the broker when a new broker connection occurs. |
| +int32_t OnInstanceConnected(PP_Instance instance, int32_t handle) { |
| + // Send test message |
| + ssize_t ret = ::write(handle, kHelloMessage, sizeof(kHelloMessage)); |
| + if (ret != sizeof(kHelloMessage)) |
| + return PP_ERROR_FAILED; |
| + |
| + // Verify broker is not sandboxed and send result to plugin over the pipe. |
| + if (VerifyIsUnsandboxed()) { |
| + ret = ::write(handle, kBrokerIsUnsandboxed, sizeof(kBrokerIsUnsandboxed)); |
| + if (ret != sizeof(kBrokerIsUnsandboxed)) |
| + return PP_ERROR_FAILED; |
| + } |
| + |
| + ::close(handle); |
| + return PP_OK; |
| +} |
| + |
| +} // namespace |
| + |
| +PP_EXPORT int32_t PPP_InitializeBroker( |
| + PP_ConnectInstance_Func* connect_instance_func) { |
| + *connect_instance_func = &OnInstanceConnected; |
| + return PP_OK; |
| +} |
| + |
| +PP_EXPORT void PPP_ShutdownBroker() {} |
| + |
| TestBroker::TestBroker(TestingInstance* instance) |
| : TestCase(instance), |
| broker_interface_(NULL) { |
| @@ -24,6 +108,7 @@ bool TestBroker::Init() { |
| void TestBroker::RunTest() { |
| RUN_TEST(Create); |
| + RUN_TEST(ConnectAndGetHandle); |
| } |
| std::string TestBroker::TestCreate() { |
| @@ -44,3 +129,27 @@ std::string TestBroker::TestCreate() { |
| PASS(); |
| } |
| + |
| +std::string TestBroker::TestConnectAndGetHandle() { |
| + PP_Resource broker = broker_interface_->CreateTrusted( |
| + instance_->pp_instance()); |
| + ASSERT_TRUE(broker); |
| + |
| + TestCompletionCallback cb(instance_->pp_instance()); |
| + |
| + ASSERT_EQ(PP_OK_COMPLETIONPENDING, broker_interface_->Connect( |
| + broker, pp::CompletionCallback(cb).pp_completion_callback())); |
| + ASSERT_EQ(PP_OK, cb.WaitForResult()); |
| + |
| + int32_t handle = -1; |
|
ddorwin
2011/10/27 23:21:24
= PlatformFileToInt(base::kInvalidPlatformFileValu
xhwang
2011/10/28 01:21:01
Done.
|
| + ASSERT_EQ(PP_OK, broker_interface_->GetHandle(broker, &handle)); |
| + ASSERT_NE(PlatformFileToInt(base::kInvalidPlatformFileValue), handle); |
| + |
| + ASSERT_TRUE(VerifyMessage(handle, kHelloMessage, sizeof(kHelloMessage))); |
| + ASSERT_TRUE(VerifyMessage( |
| + handle, kBrokerIsUnsandboxed, sizeof(kBrokerIsUnsandboxed))); |
| + |
| + ASSERT_EQ(0, ::close(handle)); |
| + |
| + PASS(); |
| +} |