1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#endif
#include <string>
#include "base/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));
// Make sure we can handle large messages.
char junk[kLongMessageStringNumBytes];
memset(junk, 'a', sizeof(junk)-1);
junk[sizeof(junk)-1] = 0;
message->WriteString(std::string(junk));
// DEBUG: printf("[%u] sending message [%s]\n", GetCurrentProcessId(), text);
sender->Send(message);
}
// A generic listener that expects messages of a certain type (see
// OnMessageReceived()), and either sends a generic response or quits after the
// 50th message (or on channel error).
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 {
// There is a race when closing the channel so the last message may be lost.
EXPECT_LE(messages_left_, 1);
MessageLoop::current()->Quit();
}
void Init(IPC::Sender* s) {
sender_ = s;
}
protected:
void SendNextMessage() {
if (--messages_left_ <= 0)
MessageLoop::current()->Quit();
else
Send(sender_, "Foo");
}
private:
IPC::Sender* sender_;
int messages_left_;
};
class IPCChannelTest : public IPCTestBase {
};
// TODO(viettrungluu): Move to a separate IPCMessageTest.
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);
// should fail
EXPECT_FALSE(m.ReadInt(&iter, &vi));
EXPECT_FALSE(m.ReadString(&iter, &vs));
EXPECT_FALSE(m.ReadWString(&iter, &vw));
}
TEST_F(IPCChannelTest, ChannelTest) {
Init("GenericClient");
// Set up IPC channel and start client.
GenericChannelListener listener;
CreateChannel(&listener);
listener.Init(sender());
ASSERT_TRUE(ConnectChannel());
ASSERT_TRUE(StartClient());
Send(sender(), "hello from parent");
// Run message loop.
MessageLoop::current()->Run();
// Close the channel so the client's OnChannelError() gets fired.
channel()->Close();
EXPECT_TRUE(WaitForClientShutdown());
DestroyChannel();
}
// TODO(viettrungluu): Move to a separate IPCChannelWinTest.
#if defined(OS_WIN)
TEST_F(IPCChannelTest, ChannelTestExistingPipe) {
Init("GenericClient");
// Create pipe manually using the standard Chromium name and set up IPC
// channel.
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); // The channel duplicates the handle.
listener.Init(sender());
// Connect to channel and start client.
ASSERT_TRUE(ConnectChannel());
ASSERT_TRUE(StartClient());
Send(sender(), "hello from parent");
// Run message loop.
MessageLoop::current()->Run();
// Close the channel so the client's OnChannelError() gets fired.
channel()->Close();
EXPECT_TRUE(WaitForClientShutdown());
DestroyChannel();
}
#endif // defined (OS_WIN)
TEST_F(IPCChannelTest, ChannelProxyTest) {
Init("GenericClient");
base::Thread thread("ChannelProxyTestServer");
base::Thread::Options options;
options.message_loop_type = MessageLoop::TYPE_IO;
thread.StartWithOptions(options);
// Set up IPC channel proxy.
GenericChannelListener listener;
CreateChannelProxy(&listener, thread.message_loop_proxy());
listener.Init(sender());
ASSERT_TRUE(StartClient());
Send(sender(), "hello from parent");
// Run message loop.
MessageLoop::current()->Run();
EXPECT_TRUE(WaitForClientShutdown());
// Destroy the channel proxy before shutting down the thread.
DestroyChannelProxy();
thread.Stop();
}
class ChannelListenerWithOnConnectedSend : public GenericChannelListener {
public:
ChannelListenerWithOnConnectedSend() {}
virtual ~ChannelListenerWithOnConnectedSend() {}
virtual void OnChannelConnected(int32 peer_pid) OVERRIDE {
SendNextMessage();
}
};
#if defined(OS_WIN)
// Acting flakey in Windows. http://crbug.com/129595
#define MAYBE_SendMessageInChannelConnected DISABLED_SendMessageInChannelConnected
#else
#define MAYBE_SendMessageInChannelConnected SendMessageInChannelConnected
#endif
// This tests the case of a listener sending back an event in its
// OnChannelConnected handler.
TEST_F(IPCChannelTest, MAYBE_SendMessageInChannelConnected) {
Init("GenericClient");
// Set up IPC channel and start client.
ChannelListenerWithOnConnectedSend listener;
CreateChannel(&listener);
listener.Init(sender());
ASSERT_TRUE(ConnectChannel());
ASSERT_TRUE(StartClient());
Send(sender(), "hello from parent");
// Run message loop.
MessageLoop::current()->Run();
// Close the channel so the client's OnChannelError() gets fired.
channel()->Close();
EXPECT_TRUE(WaitForClientShutdown());
DestroyChannel();
}
MULTIPROCESS_IPC_TEST_CLIENT_MAIN(GenericClient) {
MessageLoopForIO main_message_loop;
GenericChannelListener listener;
// Set up IPC channel.
IPC::Channel channel(IPCTestBase::GetChannelName("GenericClient"),
IPC::Channel::MODE_CLIENT,
&listener);
CHECK(channel.Connect());
listener.Init(&channel);
Send(&channel, "hello from child");
MessageLoop::current()->Run();
return 0;
}
} // namespace
|