This source file includes following definitions.
- IPC_MESSAGE_CONTROL1
- Init
- OnMessageReceived
- OnMsgClassSetHandle
- OnMsgClassSetHandle
- SetHandle
- OnMsgClassShutdown
- Init
- OnMessageReceived
- OnMsgClassResponse
#include "base/sync_socket.h"
#include <stdio.h>
#include <string>
#include <sstream>
#include "base/bind.h"
#include "base/message_loop/message_loop.h"
#include "base/threading/thread.h"
#include "ipc/ipc_test_base.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_POSIX)
#include "base/file_descriptor_posix.h"
#endif
#define IPC_MESSAGE_IMPL
#include "ipc/ipc_message_macros.h"
#define IPC_MESSAGE_START TestMsgStart
#if defined(OS_WIN)
IPC_MESSAGE_CONTROL1(MsgClassSetHandle, base::SyncSocket::Handle)
#elif defined(OS_POSIX)
IPC_MESSAGE_CONTROL1(MsgClassSetHandle, base::FileDescriptor)
#endif
IPC_MESSAGE_CONTROL1(MsgClassResponse, std::string)
IPC_MESSAGE_CONTROL0(MsgClassShutdown)
namespace {
const char kHelloString[] = "Hello, SyncSocket Client";
const size_t kHelloStringLength = arraysize(kHelloString);
class SyncSocketServerListener : public IPC::Listener {
public:
SyncSocketServerListener() : chan_(NULL) {
}
void Init(IPC::Channel* chan) {
chan_ = chan;
}
virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE {
if (msg.routing_id() == MSG_ROUTING_CONTROL) {
IPC_BEGIN_MESSAGE_MAP(SyncSocketServerListener, msg)
IPC_MESSAGE_HANDLER(MsgClassSetHandle, OnMsgClassSetHandle)
IPC_MESSAGE_HANDLER(MsgClassShutdown, OnMsgClassShutdown)
IPC_END_MESSAGE_MAP()
}
return true;
}
private:
#if defined(OS_WIN)
void OnMsgClassSetHandle(const base::SyncSocket::Handle handle) {
SetHandle(handle);
}
#elif defined(OS_POSIX)
void OnMsgClassSetHandle(const base::FileDescriptor& fd_struct) {
SetHandle(fd_struct.fd);
}
#else
# error "What platform?"
#endif
void SetHandle(base::SyncSocket::Handle handle) {
base::SyncSocket sync_socket(handle);
EXPECT_EQ(sync_socket.Send(kHelloString, kHelloStringLength),
kHelloStringLength);
IPC::Message* msg = new MsgClassResponse(kHelloString);
EXPECT_TRUE(chan_->Send(msg));
}
void OnMsgClassShutdown() {
base::MessageLoop::current()->Quit();
}
IPC::Channel* chan_;
DISALLOW_COPY_AND_ASSIGN(SyncSocketServerListener);
};
MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SyncSocketServerClient) {
base::MessageLoopForIO main_message_loop;
SyncSocketServerListener listener;
IPC::Channel channel(IPCTestBase::GetChannelName("SyncSocketServerClient"),
IPC::Channel::MODE_CLIENT,
&listener);
EXPECT_TRUE(channel.Connect());
listener.Init(&channel);
base::MessageLoop::current()->Run();
return 0;
}
class SyncSocketClientListener : public IPC::Listener {
public:
SyncSocketClientListener() {
}
void Init(base::SyncSocket* socket, IPC::Channel* chan) {
socket_ = socket;
chan_ = chan;
}
virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE {
if (msg.routing_id() == MSG_ROUTING_CONTROL) {
IPC_BEGIN_MESSAGE_MAP(SyncSocketClientListener, msg)
IPC_MESSAGE_HANDLER(MsgClassResponse, OnMsgClassResponse)
IPC_END_MESSAGE_MAP()
}
return true;
}
private:
void OnMsgClassResponse(const std::string& str) {
EXPECT_EQ(kHelloStringLength, socket_->Peek());
char buf[kHelloStringLength];
socket_->Receive(static_cast<void*>(buf), kHelloStringLength);
EXPECT_EQ(strcmp(str.c_str(), buf), 0);
EXPECT_EQ(0U, socket_->Peek());
IPC::Message* msg = new MsgClassShutdown();
EXPECT_TRUE(chan_->Send(msg));
base::MessageLoop::current()->Quit();
}
base::SyncSocket* socket_;
IPC::Channel* chan_;
DISALLOW_COPY_AND_ASSIGN(SyncSocketClientListener);
};
class SyncSocketTest : public IPCTestBase {
};
TEST_F(SyncSocketTest, SanityTest) {
Init("SyncSocketServerClient");
SyncSocketClientListener listener;
CreateChannel(&listener);
ASSERT_TRUE(StartClient());
base::SyncSocket pair[2];
base::SyncSocket::CreatePair(&pair[0], &pair[1]);
EXPECT_EQ(0U, pair[0].Peek());
EXPECT_EQ(0U, pair[1].Peek());
base::SyncSocket::Handle target_handle;
ASSERT_TRUE(ConnectChannel());
listener.Init(&pair[0], channel());
#if defined(OS_WIN)
BOOL retval = DuplicateHandle(GetCurrentProcess(), pair[1].handle(),
client_process(), &target_handle,
0, FALSE, DUPLICATE_SAME_ACCESS);
EXPECT_TRUE(retval);
IPC::Message* msg = new MsgClassSetHandle(target_handle);
#else
target_handle = pair[1].handle();
base::FileDescriptor filedesc(target_handle, false);
IPC::Message* msg = new MsgClassSetHandle(filedesc);
#endif
EXPECT_TRUE(sender()->Send(msg));
base::MessageLoop::current()->Run();
pair[0].Close();
pair[1].Close();
EXPECT_TRUE(WaitForClientShutdown());
DestroyChannel();
}
static void BlockingRead(base::SyncSocket* socket, char* buf,
size_t length, size_t* received) {
DCHECK(buf != NULL);
socket->Send(kHelloString, kHelloStringLength);
*received = socket->Receive(buf, length);
}
TEST_F(SyncSocketTest, DisconnectTest) {
base::CancelableSyncSocket pair[2];
ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1]));
base::Thread worker("BlockingThread");
worker.Start();
char buf[0xff];
size_t received = 1U;
worker.message_loop()->PostTask(FROM_HERE,
base::Bind(&BlockingRead, &pair[0], &buf[0], arraysize(buf), &received));
char hello[kHelloStringLength] = {0};
pair[1].Receive(&hello[0], sizeof(hello));
EXPECT_EQ(0, strcmp(hello, kHelloString));
base::PlatformThread::YieldCurrentThread();
pair[0].Shutdown();
worker.Stop();
EXPECT_EQ(0U, received);
}
TEST_F(SyncSocketTest, BlockingReceiveTest) {
base::CancelableSyncSocket pair[2];
ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1]));
base::Thread worker("BlockingThread");
worker.Start();
char buf[kHelloStringLength] = {0};
size_t received = 1U;
worker.message_loop()->PostTask(FROM_HERE,
base::Bind(&BlockingRead, &pair[0], &buf[0],
kHelloStringLength, &received));
char hello[kHelloStringLength] = {0};
pair[1].Receive(&hello[0], sizeof(hello));
EXPECT_EQ(0, strcmp(hello, kHelloString));
base::PlatformThread::YieldCurrentThread();
pair[1].Send(kHelloString, kHelloStringLength);
worker.Stop();
EXPECT_TRUE(strcmp(buf, kHelloString) == 0);
EXPECT_EQ(kHelloStringLength, received);
}
TEST_F(SyncSocketTest, NonBlockingWriteTest) {
base::CancelableSyncSocket pair[2];
ASSERT_TRUE(base::CancelableSyncSocket::CreatePair(&pair[0], &pair[1]));
while (pair[0].Send(kHelloString, kHelloStringLength) != 0) {}
size_t bytes_in_buffer = pair[1].Peek();
EXPECT_NE(bytes_in_buffer, 0U);
EXPECT_EQ(0U, pair[0].Send(kHelloString, kHelloStringLength));
EXPECT_EQ(bytes_in_buffer, pair[1].Peek());
char hello[kHelloStringLength] = {0};
pair[1].Receive(&hello[0], sizeof(hello));
EXPECT_EQ(kHelloStringLength, pair[0].Send(kHelloString, kHelloStringLength));
}
}