Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1794)

Unified Diff: ppapi/tests/test_broker.cc

Issue 8400024: Add TestConnectFailure, TestGetHandleFailure and TestConnectAndPipe to PPAPI Broker UI test. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Modified as commented. Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ppapi/tests/test_broker.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ppapi/tests/test_broker.cc
diff --git a/ppapi/tests/test_broker.cc b/ppapi/tests/test_broker.cc
index b1d5b028c58cf2adedbc4f45237335461277dfb4..cb5a8070d4943e895af9bda9d3750940af7b47b2 100644
--- a/ppapi/tests/test_broker.cc
+++ b/ppapi/tests/test_broker.cc
@@ -4,13 +4,121 @@
#include "ppapi/tests/test_broker.h"
+#include <cstring>
+#include <fstream>
+
+#include "base/memory/scoped_ptr.h"
+#include "base/platform_file.h"
+#include "base/scoped_temp_dir.h"
+#include "base/sync_socket.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!";
+
+// TODO(xhwang): merge PlatformFileToInt definitions and move to a common place.
+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
+}
+
+base::PlatformFile IntToPlatformFile(int32_t handle) {
+#if defined(OS_WIN)
+ return reinterpret_cast<HANDLE>(static_cast<intptr_t>(handle));
+#elif defined(OS_POSIX)
+ return handle;
+#else
+ #error Not implemented.
+#endif
+}
+
+const int32_t kInvalidHandle =
+ PlatformFileToInt(base::kInvalidPlatformFileValue);
+
+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("test_pepper_broker.txt"));
+
+ fs.open(test_file_name.value().c_str(), std::fstream::out);
+ if (!fs)
+ return false;
+ fs << "Verify that the broker is unsandboxed.";
+ 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(base::SyncSocket& sync_socket, const char* message_sent,
+ size_t length) {
+ scoped_array<char> message_received(new char[length]);
+ sync_socket.Receive(message_received.get(), length);
ddorwin 2011/10/28 19:05:59 Should we check that the return value is length?
xhwang 2011/10/28 19:39:10 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) {
+ base::PlatformFile platform_file = IntToPlatformFile(handle);
+ if (platform_file == base::kInvalidPlatformFileValue)
+ return PP_ERROR_FAILED;
+
+ base::SyncSocket sync_socket(platform_file);
+
+ // Send hello message.
+ size_t ret = sync_socket.Send(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 = sync_socket.Send(kBrokerIsUnsandboxed, sizeof(kBrokerIsUnsandboxed));
+ if (ret != sizeof(kBrokerIsUnsandboxed))
+ return PP_ERROR_FAILED;
+ }
+
+ 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 +132,9 @@ bool TestBroker::Init() {
void TestBroker::RunTest() {
RUN_TEST(Create);
+ RUN_TEST(GetHandleFailure);
+ RUN_TEST(ConnectFailure);
+ RUN_TEST(ConnectAndPipe);
}
std::string TestBroker::TestCreate() {
@@ -35,12 +146,68 @@ std::string TestBroker::TestCreate() {
ASSERT_FALSE(broker_interface_->IsBrokerTrusted(0));
ASSERT_TRUE(broker_interface_->IsBrokerTrusted(broker));
- // Test getting the handle for an invalid resource.
- int32_t handle;
- ASSERT_TRUE(broker_interface_->GetHandle(0, &handle) == PP_ERROR_BADRESOURCE);
+ PASS();
+}
+
+// Test connection on invalid resource.
+std::string TestBroker::TestConnectFailure() {
+ PP_Resource broker = broker_interface_->CreateTrusted(
+ instance_->pp_instance());
+ ASSERT_TRUE(broker);
+
+ // Callback NOT force asynced. Connect should fail. The callback will not be
+ // posted so there's no need to wait for the callback to complete.
+ TestCompletionCallback cb_1(instance_->pp_instance(), false);
+ ASSERT_EQ(PP_ERROR_BADRESOURCE,
+ broker_interface_->Connect(
+ 0, pp::CompletionCallback(cb_1).pp_completion_callback()));
+ // Callback force aynced. Connect will return PP_OK_COMPLETIONPENDING and the
+ // callback will be posted. However, the callback should fail.
+ TestCompletionCallback cb_2(instance_->pp_instance(), true);
+ ASSERT_EQ(PP_OK_COMPLETIONPENDING,
+ broker_interface_->Connect(
+ 0, pp::CompletionCallback(cb_2).pp_completion_callback()));
+ ASSERT_EQ(PP_ERROR_BADRESOURCE, cb_2.WaitForResult());
+
+ PASS();
+}
+
+std::string TestBroker::TestGetHandleFailure() {
+ PP_Resource broker = broker_interface_->CreateTrusted(
ddorwin 2011/10/28 19:05:59 This _could_ be moved down to 185.
xhwang 2011/10/28 19:39:10 Done.
+ instance_->pp_instance());
+ ASSERT_TRUE(broker);
+
+ int32_t handle = kInvalidHandle;
+ // Test getting the handle for an invalid resource.
+ ASSERT_EQ(PP_ERROR_BADRESOURCE, broker_interface_->GetHandle(0, &handle));
// Connect hasn't been called so this should fail.
- ASSERT_TRUE(broker_interface_->GetHandle(broker, &handle) == PP_ERROR_FAILED);
+ ASSERT_EQ(PP_ERROR_FAILED, broker_interface_->GetHandle(broker, &handle));
+
+ PASS();
+}
+
+std::string TestBroker::TestConnectAndPipe() {
+ PP_Resource broker = broker_interface_->CreateTrusted(
+ instance_->pp_instance());
+ ASSERT_TRUE(broker);
+
+ TestCompletionCallback cb_3(instance_->pp_instance());
+ ASSERT_EQ(PP_OK_COMPLETIONPENDING,
+ broker_interface_->Connect(
+ broker, pp::CompletionCallback(cb_3).pp_completion_callback()));
+ ASSERT_EQ(PP_OK, cb_3.WaitForResult());
+
+ int32_t handle = kInvalidHandle;
+ ASSERT_EQ(PP_OK, broker_interface_->GetHandle(broker, &handle));
+ ASSERT_NE(handle, kInvalidHandle);
ddorwin 2011/10/28 19:05:59 swap. Expected on the left. (That's the GTest conv
+
+ base::PlatformFile platform_file = IntToPlatformFile(handle);
+ base::SyncSocket sync_socket(platform_file);
+
+ ASSERT_TRUE(VerifyMessage(sync_socket, kHelloMessage, sizeof(kHelloMessage)));
+ ASSERT_TRUE(VerifyMessage(sync_socket, kBrokerIsUnsandboxed,
+ sizeof(kBrokerIsUnsandboxed)));
PASS();
}
« no previous file with comments | « ppapi/tests/test_broker.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698