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 <cstring> | |
| 8 #include <fstream> | |
| 9 | |
| 10 #include "base/platform_file.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/scoped_temp_dir.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 |
| 12 REGISTER_TEST_CASE(Broker); | 19 REGISTER_TEST_CASE(Broker); |
| 13 | 20 |
| 21 namespace { | |
| 22 | |
| 23 const char kHelloMessage[] = "Hello Plugin! This is Broker!"; | |
| 24 const char kBrokerIsUnsandboxed[] = "Broker is Unsandboxed!"; | |
| 25 | |
| 26 // 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.
| |
| 27 int32_t PlatformFileToInt(base::PlatformFile handle) { | |
| 28 #if defined(OS_WIN) | |
| 29 return static_cast<int32_t>(reinterpret_cast<intptr_t>(handle)); | |
| 30 #elif defined(OS_POSIX) | |
| 31 return handle; | |
| 32 #else | |
| 33 #error Not implemented. | |
| 34 #endif | |
| 35 } | |
| 36 | |
| 37 bool VerifyIsUnsandboxed() { | |
| 38 std::fstream fs; | |
| 39 | |
| 40 // Temporary directory will be cleaned when this object goes out of scope. | |
| 41 ScopedTempDir temp_dir_; | |
| 42 if (!temp_dir_.CreateUniqueTempDir()) | |
| 43 return false; | |
| 44 | |
| 45 FilePath test_file_name = | |
| 46 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.
| |
| 47 | |
| 48 fs.open(test_file_name.value().c_str(), std::fstream::out); | |
| 49 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.
| |
| 50 fs << "Verify if the broker is unsandboxed."; | |
|
ddorwin
2011/10/27 23:21:24
if => that
xhwang
2011/10/28 01:21:01
Done.
| |
| 51 if (!fs) return false; | |
| 52 fs.close(); | |
| 53 | |
| 54 fs.open(test_file_name.value().c_str(), std::fstream::in); | |
| 55 if (!fs) return false; | |
| 56 char buffer[16] = {0}; | |
| 57 fs.read(buffer, 8); | |
| 58 if (!fs) return false; | |
| 59 fs.close(); | |
| 60 | |
| 61 return true; | |
| 62 } | |
| 63 | |
| 64 bool VerifyMessage(int32_t handle, const char* message_sent, size_t length) { | |
| 65 scoped_array<char> message_received(new char[length]); | |
| 66 ::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.
| |
| 67 return !::strcmp(message_received.get(), message_sent); | |
| 68 } | |
| 69 | |
| 70 // Callback in the broker when a new broker connection occurs. | |
| 71 int32_t OnInstanceConnected(PP_Instance instance, int32_t handle) { | |
| 72 // Send test message | |
| 73 ssize_t ret = ::write(handle, kHelloMessage, sizeof(kHelloMessage)); | |
| 74 if (ret != sizeof(kHelloMessage)) | |
| 75 return PP_ERROR_FAILED; | |
| 76 | |
| 77 // Verify broker is not sandboxed and send result to plugin over the pipe. | |
| 78 if (VerifyIsUnsandboxed()) { | |
| 79 ret = ::write(handle, kBrokerIsUnsandboxed, sizeof(kBrokerIsUnsandboxed)); | |
| 80 if (ret != sizeof(kBrokerIsUnsandboxed)) | |
| 81 return PP_ERROR_FAILED; | |
| 82 } | |
| 83 | |
| 84 ::close(handle); | |
| 85 return PP_OK; | |
| 86 } | |
| 87 | |
| 88 } // namespace | |
| 89 | |
| 90 PP_EXPORT int32_t PPP_InitializeBroker( | |
| 91 PP_ConnectInstance_Func* connect_instance_func) { | |
| 92 *connect_instance_func = &OnInstanceConnected; | |
| 93 return PP_OK; | |
| 94 } | |
| 95 | |
| 96 PP_EXPORT void PPP_ShutdownBroker() {} | |
| 97 | |
| 14 TestBroker::TestBroker(TestingInstance* instance) | 98 TestBroker::TestBroker(TestingInstance* instance) |
| 15 : TestCase(instance), | 99 : TestCase(instance), |
| 16 broker_interface_(NULL) { | 100 broker_interface_(NULL) { |
| 17 } | 101 } |
| 18 | 102 |
| 19 bool TestBroker::Init() { | 103 bool TestBroker::Init() { |
| 20 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( | 104 broker_interface_ = static_cast<PPB_BrokerTrusted const*>( |
| 21 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); | 105 pp::Module::Get()->GetBrowserInterface(PPB_BROKER_TRUSTED_INTERFACE)); |
| 22 return !!broker_interface_; | 106 return !!broker_interface_; |
| 23 } | 107 } |
| 24 | 108 |
| 25 void TestBroker::RunTest() { | 109 void TestBroker::RunTest() { |
| 26 RUN_TEST(Create); | 110 RUN_TEST(Create); |
| 111 RUN_TEST(ConnectAndGetHandle); | |
| 27 } | 112 } |
| 28 | 113 |
| 29 std::string TestBroker::TestCreate() { | 114 std::string TestBroker::TestCreate() { |
| 30 // Very simplistic test to make sure we can create a broker interface. | 115 // Very simplistic test to make sure we can create a broker interface. |
| 31 PP_Resource broker = broker_interface_->CreateTrusted( | 116 PP_Resource broker = broker_interface_->CreateTrusted( |
| 32 instance_->pp_instance()); | 117 instance_->pp_instance()); |
| 33 ASSERT_TRUE(broker); | 118 ASSERT_TRUE(broker); |
| 34 | 119 |
| 35 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); | 120 ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0)); |
| 36 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); | 121 ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker)); |
| 37 | 122 |
| 38 // Test getting the handle for an invalid resource. | 123 // Test getting the handle for an invalid resource. |
| 39 int32_t handle; | 124 int32_t handle; |
| 40 ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE); | 125 ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE); |
| 41 | 126 |
| 42 // Connect hasn't been called so this should fail. | 127 // Connect hasn't been called so this should fail. |
| 43 ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED); | 128 ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED); |
| 44 | 129 |
| 45 PASS(); | 130 PASS(); |
| 46 } | 131 } |
| 132 | |
| 133 std::string TestBroker::TestConnectAndGetHandle() { | |
| 134 PP_Resource broker = broker_interface_->CreateTrusted( | |
| 135 instance_->pp_instance()); | |
| 136 ASSERT_TRUE(broker); | |
| 137 | |
| 138 TestCompletionCallback cb(instance_->pp_instance()); | |
| 139 | |
| 140 ASSERT_EQ(PP_OK_COMPLETIONPENDING, broker_interface_->Connect( | |
| 141 broker, pp::CompletionCallback(cb).pp_completion_callback())); | |
| 142 ASSERT_EQ(PP_OK, cb.WaitForResult()); | |
| 143 | |
| 144 int32_t handle = -1; | |
|
ddorwin
2011/10/27 23:21:24
= PlatformFileToInt(base::kInvalidPlatformFileValu
xhwang
2011/10/28 01:21:01
Done.
| |
| 145 ASSERT_EQ(PP_OK, broker_interface_->GetHandle(broker, &handle)); | |
| 146 ASSERT_NE(PlatformFileToInt(base::kInvalidPlatformFileValue), handle); | |
| 147 | |
| 148 ASSERT_TRUE(VerifyMessage(handle, kHelloMessage, sizeof(kHelloMessage))); | |
| 149 ASSERT_TRUE(VerifyMessage( | |
| 150 handle, kBrokerIsUnsandboxed, sizeof(kBrokerIsUnsandboxed))); | |
| 151 | |
| 152 ASSERT_EQ(0, ::close(handle)); | |
| 153 | |
| 154 PASS(); | |
| 155 } | |
| OLD | NEW |