summaryrefslogtreecommitdiffstats
path: root/mojo
diff options
context:
space:
mode:
authorviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-05 17:10:27 +0000
committerviettrungluu@chromium.org <viettrungluu@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-03-05 17:10:27 +0000
commitb9793fbdd75eba252f027aa1531efd9d9fa31b6a (patch)
treecce9234981dd59c199e9b9f80f5029a5580cc459 /mojo
parentf292e19dd8227bd0a84312255831673733ec6d00 (diff)
downloadchromium_src-b9793fbdd75eba252f027aa1531efd9d9fa31b6a.zip
chromium_src-b9793fbdd75eba252f027aa1531efd9d9fa31b6a.tar.gz
chromium_src-b9793fbdd75eba252f027aa1531efd9d9fa31b6a.tar.bz2
Mojo: Add a more end-to-end-ish test of handle passing (across a "remote" message pipe).
R=darin@chromium.org Review URL: https://codereview.chromium.org/180163018 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255067 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo')
-rw-r--r--mojo/system/embedder/embedder_unittest.cc132
1 files changed, 126 insertions, 6 deletions
diff --git a/mojo/system/embedder/embedder_unittest.cc b/mojo/system/embedder/embedder_unittest.cc
index 4a0dde7..13e7c8f 100644
--- a/mojo/system/embedder/embedder_unittest.cc
+++ b/mojo/system/embedder/embedder_unittest.cc
@@ -9,6 +9,7 @@
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
+#include "base/macros.h"
#include "mojo/public/system/core.h"
#include "mojo/system/embedder/platform_channel_pair.h"
#include "mojo/system/embedder/test_embedder.h"
@@ -52,7 +53,7 @@ TEST_F(EmbedderTest, ChannelsBasic) {
const char kHello[] = "hello";
EXPECT_EQ(MOJO_RESULT_OK,
MojoWriteMessage(server_mp, kHello,
- static_cast<uint32_t>(sizeof(kHello)), NULL, 0u,
+ static_cast<uint32_t>(sizeof(kHello)), NULL, 0,
MOJO_WRITE_MESSAGE_FLAG_NONE));
// Now wait for the other side to become readable.
@@ -61,16 +62,13 @@ TEST_F(EmbedderTest, ChannelsBasic) {
MOJO_DEADLINE_INDEFINITE));
char buffer[1000] = {};
- uint32_t num_bytes = static_cast<uint32_t>(sizeof(kHello));
+ uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
EXPECT_EQ(MOJO_RESULT_OK,
MojoReadMessage(client_mp, buffer, &num_bytes, NULL, NULL,
MOJO_READ_MESSAGE_FLAG_NONE));
EXPECT_EQ(sizeof(kHello), num_bytes);
- EXPECT_EQ(0, memcmp(buffer, kHello, num_bytes));
+ EXPECT_STREQ(kHello, buffer);
- // TODO(vtl): FIXME -- This rapid-fire closing leads to a warning: "Received a
- // message for nonexistent local destination ID 1". This is due to a race
- // condition (in channel.cc/message_pipe.cc).
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
@@ -89,6 +87,128 @@ TEST_F(EmbedderTest, ChannelsBasic) {
EXPECT_TRUE(test::Shutdown());
}
+TEST_F(EmbedderTest, ChannelsHandlePassing) {
+ Init();
+
+ PlatformChannelPair channel_pair;
+ ScopedPlatformHandle server_handle = channel_pair.PassServerHandle();
+ ScopedPlatformHandle client_handle = channel_pair.PassClientHandle();
+
+ ChannelInfo* server_channel_info = NULL;
+ MojoHandle server_mp = CreateChannel(server_handle.Pass(),
+ io_thread_task_runner(),
+ base::Bind(&StoreChannelInfo,
+ &server_channel_info));
+ EXPECT_NE(server_mp, MOJO_HANDLE_INVALID);
+
+ ChannelInfo* client_channel_info = NULL;
+ MojoHandle client_mp = CreateChannel(client_handle.Pass(),
+ io_thread_task_runner(),
+ base::Bind(&StoreChannelInfo,
+ &client_channel_info));
+ EXPECT_NE(client_mp, MOJO_HANDLE_INVALID);
+
+ MojoHandle h0, h1;
+ EXPECT_EQ(MOJO_RESULT_OK, MojoCreateMessagePipe(&h0, &h1));
+
+ // Write a message to |h0| (attaching nothing).
+ const char kHello[] = "hello";
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoWriteMessage(h0, kHello,
+ static_cast<uint32_t>(sizeof(kHello)), NULL, 0,
+ MOJO_WRITE_MESSAGE_FLAG_NONE));
+
+ // Write one message to |server_mp|, attaching |h1|.
+ const char kWorld[] = "world!!!";
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoWriteMessage(server_mp, kWorld,
+ static_cast<uint32_t>(sizeof(kWorld)), &h1, 1,
+ MOJO_WRITE_MESSAGE_FLAG_NONE));
+ h1 = MOJO_HANDLE_INVALID;
+
+ // Write another message to |h0|.
+ const char kFoo[] = "foo";
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoWriteMessage(h0, kFoo,
+ static_cast<uint32_t>(sizeof(kFoo)), NULL, 0,
+ MOJO_WRITE_MESSAGE_FLAG_NONE));
+
+ // Wait for |client_mp| to become readable.
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoWait(client_mp, MOJO_WAIT_FLAG_READABLE,
+ MOJO_DEADLINE_INDEFINITE));
+
+ // Read a message from |client_mp|.
+ char buffer[1000] = {};
+ uint32_t num_bytes = static_cast<uint32_t>(sizeof(buffer));
+ MojoHandle handles[10] = {};
+ uint32_t num_handles = arraysize(handles);
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoReadMessage(client_mp, buffer, &num_bytes, handles,
+ &num_handles, MOJO_READ_MESSAGE_FLAG_NONE));
+ EXPECT_EQ(sizeof(kWorld), num_bytes);
+ EXPECT_STREQ(kWorld, buffer);
+ EXPECT_EQ(1u, num_handles);
+ EXPECT_NE(handles[0], MOJO_HANDLE_INVALID);
+ h1 = handles[0];
+
+ // Wait for |h1| to become readable.
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoWait(h1, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE));
+
+ // Read a message from |h1|.
+ memset(buffer, 0, sizeof(buffer));
+ num_bytes = static_cast<uint32_t>(sizeof(buffer));
+ memset(handles, 0, sizeof(handles));
+ num_handles = arraysize(handles);
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoReadMessage(h1, buffer, &num_bytes, handles, &num_handles,
+ MOJO_READ_MESSAGE_FLAG_NONE));
+ EXPECT_EQ(sizeof(kHello), num_bytes);
+ EXPECT_STREQ(kHello, buffer);
+ EXPECT_EQ(0u, num_handles);
+
+ // Wait for |h1| to become readable (again).
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoWait(h1, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE));
+
+ // Read the second message from |h1|.
+ memset(buffer, 0, sizeof(buffer));
+ num_bytes = static_cast<uint32_t>(sizeof(buffer));
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoReadMessage(h1, buffer, &num_bytes, NULL, NULL,
+ MOJO_READ_MESSAGE_FLAG_NONE));
+ EXPECT_EQ(sizeof(kFoo), num_bytes);
+ EXPECT_STREQ(kFoo, buffer);
+
+ // Write a message to |h1|.
+ const char kBarBaz[] = "barbaz";
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoWriteMessage(h1, kBarBaz,
+ static_cast<uint32_t>(sizeof(kBarBaz)), NULL, 0,
+ MOJO_WRITE_MESSAGE_FLAG_NONE));
+
+ // Wait for |h0| to become readable.
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoWait(h0, MOJO_WAIT_FLAG_READABLE, MOJO_DEADLINE_INDEFINITE));
+
+ // Read a message from |h0|.
+ memset(buffer, 0, sizeof(buffer));
+ num_bytes = static_cast<uint32_t>(sizeof(buffer));
+ EXPECT_EQ(MOJO_RESULT_OK,
+ MojoReadMessage(h0, buffer, &num_bytes, NULL, NULL,
+ MOJO_READ_MESSAGE_FLAG_NONE));
+ EXPECT_EQ(sizeof(kBarBaz), num_bytes);
+ EXPECT_STREQ(kBarBaz, buffer);
+
+ EXPECT_EQ(MOJO_RESULT_OK, MojoClose(server_mp));
+ EXPECT_EQ(MOJO_RESULT_OK, MojoClose(client_mp));
+ EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h0));
+ EXPECT_EQ(MOJO_RESULT_OK, MojoClose(h1));
+
+ EXPECT_TRUE(test::Shutdown());
+}
+
// TODO(vtl): Test immediate write & close.
// TODO(vtl): Test broken-connection cases.