This source file includes following definitions.
- Send
- messages_left_
- OnMessageReceived
- OnChannelError
- Init
- SendNextMessage
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- OnChannelConnected
- TEST_F
- MULTIPROCESS_IPC_TEST_CLIENT_MAIN
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
#include <string>
#include "base/message_loop/message_loop.h"
#include "base/pickle.h"
#include "base/threading/thread.h"
#include "ipc/ipc_message.h"
#include "ipc/ipc_test_base.h"
namespace {
const size_t kLongMessageStringNumBytes = 50000;
static void Send(IPC::Sender* sender, const char* text) {
static int message_index = 0;
IPC::Message* message = new IPC::Message(0,
2,
IPC::Message::PRIORITY_NORMAL);
message->WriteInt(message_index++);
message->WriteString(std::string(text));
char junk[kLongMessageStringNumBytes];
memset(junk, 'a', sizeof(junk)-1);
junk[sizeof(junk)-1] = 0;
message->WriteString(std::string(junk));
sender->Send(message);
}
class GenericChannelListener : public IPC::Listener {
public:
GenericChannelListener() : sender_(NULL), messages_left_(50) {}
virtual ~GenericChannelListener() {}
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
PickleIterator iter(message);
int ignored;
EXPECT_TRUE(iter.ReadInt(&ignored));
std::string data;
EXPECT_TRUE(iter.ReadString(&data));
std::string big_string;
EXPECT_TRUE(iter.ReadString(&big_string));
EXPECT_EQ(kLongMessageStringNumBytes - 1, big_string.length());
SendNextMessage();
return true;
}
virtual void OnChannelError() OVERRIDE {
EXPECT_LE(messages_left_, 1);
base::MessageLoop::current()->Quit();
}
void Init(IPC::Sender* s) {
sender_ = s;
}
protected:
void SendNextMessage() {
if (--messages_left_ <= 0)
base::MessageLoop::current()->Quit();
else
Send(sender_, "Foo");
}
private:
IPC::Sender* sender_;
int messages_left_;
};
class IPCChannelTest : public IPCTestBase {
};
TEST_F(IPCChannelTest, BasicMessageTest) {
int v1 = 10;
std::string v2("foobar");
std::wstring v3(L"hello world");
IPC::Message m(0, 1, IPC::Message::PRIORITY_NORMAL);
EXPECT_TRUE(m.WriteInt(v1));
EXPECT_TRUE(m.WriteString(v2));
EXPECT_TRUE(m.WriteWString(v3));
PickleIterator iter(m);
int vi;
std::string vs;
std::wstring vw;
EXPECT_TRUE(m.ReadInt(&iter, &vi));
EXPECT_EQ(v1, vi);
EXPECT_TRUE(m.ReadString(&iter, &vs));
EXPECT_EQ(v2, vs);
EXPECT_TRUE(m.ReadWString(&iter, &vw));
EXPECT_EQ(v3, vw);
EXPECT_FALSE(m.ReadInt(&iter, &vi));
EXPECT_FALSE(m.ReadString(&iter, &vs));
EXPECT_FALSE(m.ReadWString(&iter, &vw));
}
TEST_F(IPCChannelTest, ChannelTest) {
Init("GenericClient");
GenericChannelListener listener;
CreateChannel(&listener);
listener.Init(sender());
ASSERT_TRUE(ConnectChannel());
ASSERT_TRUE(StartClient());
Send(sender(), "hello from parent");
base::MessageLoop::current()->Run();
channel()->Close();
EXPECT_TRUE(WaitForClientShutdown());
DestroyChannel();
}
#if defined(OS_WIN)
TEST_F(IPCChannelTest, ChannelTestExistingPipe) {
Init("GenericClient");
GenericChannelListener listener;
std::string name("\\\\.\\pipe\\chrome.");
name.append(GetChannelName("GenericClient"));
HANDLE pipe = CreateNamedPipeA(name.c_str(),
PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED |
FILE_FLAG_FIRST_PIPE_INSTANCE,
PIPE_TYPE_BYTE | PIPE_READMODE_BYTE,
1,
4096,
4096,
5000,
NULL);
CreateChannelFromChannelHandle(IPC::ChannelHandle(pipe), &listener);
CloseHandle(pipe);
listener.Init(sender());
ASSERT_TRUE(ConnectChannel());
ASSERT_TRUE(StartClient());
Send(sender(), "hello from parent");
base::MessageLoop::current()->Run();
channel()->Close();
EXPECT_TRUE(WaitForClientShutdown());
DestroyChannel();
}
#endif
TEST_F(IPCChannelTest, ChannelProxyTest) {
Init("GenericClient");
base::Thread thread("ChannelProxyTestServer");
base::Thread::Options options;
options.message_loop_type = base::MessageLoop::TYPE_IO;
thread.StartWithOptions(options);
GenericChannelListener listener;
CreateChannelProxy(&listener, thread.message_loop_proxy().get());
listener.Init(sender());
ASSERT_TRUE(StartClient());
Send(sender(), "hello from parent");
base::MessageLoop::current()->Run();
EXPECT_TRUE(WaitForClientShutdown());
DestroyChannelProxy();
thread.Stop();
}
class ChannelListenerWithOnConnectedSend : public GenericChannelListener {
public:
ChannelListenerWithOnConnectedSend() {}
virtual ~ChannelListenerWithOnConnectedSend() {}
virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
SendNextMessage();
}
};
#if defined(OS_WIN)
#define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
#else
#define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
#endif
TEST_F(IPCChannelTest, MAYBE_SendMessageInChannelConnected) {
Init("GenericClient");
ChannelListenerWithOnConnectedSend listener;
CreateChannel(&listener);
listener.Init(sender());
ASSERT_TRUE(ConnectChannel());
ASSERT_TRUE(StartClient());
Send(sender(), "hello from parent");
base::MessageLoop::current()->Run();
channel()->Close();
EXPECT_TRUE(WaitForClientShutdown());
DestroyChannel();
}
MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient) {
base::MessageLoopForIO main_message_loop;
GenericChannelListener listener;
IPC::Channel channel(IPCTestBase::GetChannelName("GenericClient"),
IPC::Channel::MODE_CLIENT,
&listener);
CHECK(channel.Connect());
listener.Init(&channel);
Send(&channel, "hello from child");
base::MessageLoop::current()->Run();
return 0;
}
}