OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // These tests are POSIX only. | |
6 | |
7 #include <fcntl.h> | |
8 #include <sys/socket.h> | |
9 #include <sys/un.h> | |
10 #include <unistd.h> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/eintr_wrapper.h" | |
14 #include "base/message_loop.h" | |
15 #include "base/scoped_ptr.h" | |
16 #include "base/test/multiprocess_test.h" | |
17 #include "ipc/ipc_channel_posix.h" | |
18 #include "testing/multiprocess_func_list.h" | |
19 | |
20 namespace { | |
21 | |
22 static const int kIPCPosixTestTimeOutInMS = 2000; | |
Paweł Hajdan Jr.
2010/12/10 18:48:15
Please use base/test/test_timeouts.
dmac
2010/12/13 18:32:29
Done. Thanks.. didn't know about those.
| |
23 enum { | |
24 QUIT_MESSAGE = 47 | |
25 }; | |
26 | |
27 class IPCChannelPosixTestListener : public IPC::Channel::Listener { | |
28 public: | |
29 enum STATUS { | |
30 DISCONNECTED, | |
31 MESSAGE_RECEIVED, | |
32 CHANNEL_ERROR, | |
33 CONNECTED, | |
34 DENIED, | |
35 LISTEN_ERROR | |
36 }; | |
37 | |
38 IPCChannelPosixTestListener() : status_(DISCONNECTED) {} | |
39 virtual ~IPCChannelPosixTestListener() {} | |
40 virtual void OnMessageReceived(const IPC::Message& message) { | |
41 EXPECT_EQ(message.type(), QUIT_MESSAGE); | |
42 status_ = MESSAGE_RECEIVED; | |
43 QuitRunLoop(); | |
44 } | |
45 | |
46 virtual void OnChannelConnected(int32 peer_pid) { | |
47 status_ = CONNECTED; | |
48 QuitRunLoop(); | |
49 } | |
50 | |
51 virtual void OnChannelError() { | |
52 status_ = CHANNEL_ERROR; | |
53 QuitRunLoop(); | |
54 } | |
55 | |
56 virtual void OnChannelDenied() { | |
57 status_ = DENIED; | |
58 QuitRunLoop(); | |
59 } | |
60 | |
61 virtual void OnChannelListenError() { | |
62 status_ = LISTEN_ERROR; | |
63 QuitRunLoop(); | |
64 } | |
65 | |
66 STATUS status() { return status_; } | |
67 void QuitRunLoop() { | |
68 MessageLoopForIO::current()->QuitNow(); | |
69 } | |
70 | |
71 private: | |
72 STATUS status_; | |
73 }; | |
74 | |
75 } // namespace | |
76 | |
77 class IPCChannelPosixTest : public base::MultiProcessTest { | |
78 public: | |
79 static const char kConnectionSocketTestName[]; | |
80 static void SetUpSocket(IPC::ChannelHandle *handle, | |
81 IPC::Channel::Mode mode); | |
82 static void SpinRunLoop(int milliseconds); | |
83 | |
84 protected: | |
85 virtual void SetUp(); | |
86 virtual void TearDown(); | |
87 | |
88 private: | |
89 scoped_ptr<MessageLoopForIO> message_loop_; | |
90 }; | |
91 | |
92 const char IPCChannelPosixTest::kConnectionSocketTestName[] = | |
93 "/var/tmp/chrome_IPCChannelPosixTest__AdvancedConnected"; | |
94 | |
95 void IPCChannelPosixTest::SetUp() { | |
96 MultiProcessTest::SetUp(); | |
97 // Construct a fresh IO Message loop for the duration of each test. | |
98 message_loop_.reset(new MessageLoopForIO()); | |
99 } | |
100 | |
101 void IPCChannelPosixTest::TearDown() { | |
102 message_loop_.reset(NULL); | |
103 MultiProcessTest::TearDown(); | |
104 } | |
105 | |
106 // Create up a socket and bind and listen to it, or connect it | |
107 // depending on the |mode|. | |
108 void IPCChannelPosixTest::SetUpSocket(IPC::ChannelHandle *handle, | |
109 IPC::Channel::Mode mode) { | |
110 const std::string& name = handle->name; | |
111 int socket_fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
112 ASSERT_GE(socket_fd, 0) << name; | |
113 ASSERT_GE(fcntl(socket_fd, F_SETFL, O_NONBLOCK), 0); | |
114 struct sockaddr_un server_address = { 0 }; | |
115 server_address.sun_family = AF_UNIX; | |
116 memcpy(server_address.sun_path, name.c_str(), name.length() + 1); | |
117 if (mode == IPC::Channel::MODE_NAMED_SERVER) { | |
118 // Only one server at a time. Cleanup garbage if it exists. | |
119 unlink(name.c_str()); | |
120 ASSERT_GE(bind(socket_fd, | |
121 reinterpret_cast<struct sockaddr *>(&server_address), | |
122 SUN_LEN(&server_address)), 0) << name; | |
123 ASSERT_GE(listen(socket_fd, SOMAXCONN), 0) << name; | |
124 } else if (mode == IPC::Channel::MODE_NAMED_CLIENT) { | |
125 ASSERT_GE(connect(socket_fd, | |
126 reinterpret_cast<struct sockaddr *>(&server_address), | |
127 SUN_LEN(&server_address)), 0) << name; | |
128 } else { | |
129 FAIL() << "Unknown mode " << mode; | |
130 } | |
131 handle->socket.fd = socket_fd; | |
132 } | |
133 | |
134 void IPCChannelPosixTest::SpinRunLoop(int milliseconds) { | |
135 MessageLoopForIO *loop = MessageLoopForIO::current(); | |
136 // Post a quit task so that this loop eventually ends and we don't hang | |
137 // in the case of a bad test. | |
Nico
2010/12/10 18:08:03
"Usually, the run loop will quit sooner than that
dmac
2010/12/13 18:32:29
Done.
dmac
2010/12/13 18:32:29
Done. But I don't know of any good way of enforcin
| |
138 loop->PostDelayedTask(FROM_HERE, new MessageLoop::QuitTask(), milliseconds); | |
139 loop->Run(); | |
140 } | |
141 | |
142 TEST_F(IPCChannelPosixTest, BasicListen) { | |
143 // Test creating a socket that is listening. | |
144 IPC::ChannelHandle handle("/var/tmp/IPCChannelPosixTest::BasicListen"); | |
145 SetUpSocket(&handle, IPC::Channel::MODE_NAMED_SERVER); | |
146 unlink(handle.name.c_str()); | |
147 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL); | |
148 ASSERT_TRUE(channel.Connect()); | |
149 ASSERT_TRUE(channel.AcceptsConnections()); | |
150 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
151 channel.ResetToAcceptingConnectionState(); | |
152 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
153 } | |
154 | |
155 TEST_F(IPCChannelPosixTest, BasicConnected) { | |
156 // Test creating a socket that is connected. | |
157 int pipe_fds[2]; | |
158 ASSERT_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds), 0); | |
Nico
2010/12/10 18:08:03
the argument order for ASSERT is expected, actual
dmac
2010/12/13 18:32:29
Yes... which always bugs me ;-) Fixed.
| |
159 std::string socket_name("/var/tmp/IPCChannelPosixTest::BasicConnected"); | |
160 base::FileDescriptor fd(pipe_fds[0], false); | |
161 IPC::ChannelHandle handle(socket_name, fd); | |
162 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_SERVER, NULL); | |
163 ASSERT_TRUE(channel.Connect()); | |
164 ASSERT_FALSE(channel.AcceptsConnections()); | |
165 channel.Close(); | |
166 ASSERT_TRUE(HANDLE_EINTR(close(pipe_fds[1])) == 0); | |
167 | |
168 // Make sure that we can use the socket that is created for us by | |
169 // a standard channel. | |
170 IPC::Channel channel2(socket_name, IPC::Channel::MODE_SERVER, NULL); | |
171 ASSERT_TRUE(channel2.Connect()); | |
172 ASSERT_FALSE(channel2.AcceptsConnections()); | |
173 } | |
174 | |
175 TEST_F(IPCChannelPosixTest, AdvancedConnected) { | |
176 // Test creating a connection to an external process. | |
177 IPCChannelPosixTestListener listener; | |
178 IPC::ChannelHandle chan_handle(kConnectionSocketTestName); | |
179 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | |
180 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | |
181 ASSERT_TRUE(channel.Connect()); | |
182 ASSERT_TRUE(channel.AcceptsConnections()); | |
183 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
184 | |
185 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", | |
186 false); | |
187 ASSERT_TRUE(handle); | |
188 SpinRunLoop(kIPCPosixTestTimeOutInMS); | |
189 ASSERT_EQ(listener.status(), IPCChannelPosixTestListener::CONNECTED); | |
190 ASSERT_TRUE(channel.HasAcceptedConnection()); | |
191 IPC::Message* message = new IPC::Message(0, // routing_id | |
192 QUIT_MESSAGE, // message type | |
193 IPC::Message::PRIORITY_NORMAL); | |
194 channel.Send(message); | |
195 SpinRunLoop(kIPCPosixTestTimeOutInMS); | |
196 int exit_code = 0; | |
197 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | |
198 EXPECT_EQ(exit_code, 0); | |
199 ASSERT_EQ(listener.status(), IPCChannelPosixTestListener::CHANNEL_ERROR); | |
200 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
201 } | |
202 | |
203 TEST_F(IPCChannelPosixTest, ResetState) { | |
204 // Test creating a connection to an external process. Close the connection, | |
205 // but continue to listen and make sure another external process can connect | |
206 // to us. | |
207 IPCChannelPosixTestListener listener; | |
208 IPC::ChannelHandle chan_handle(kConnectionSocketTestName); | |
209 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | |
210 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | |
211 ASSERT_TRUE(channel.Connect()); | |
212 ASSERT_TRUE(channel.AcceptsConnections()); | |
213 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
214 | |
215 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", | |
216 false); | |
217 ASSERT_TRUE(handle); | |
218 SpinRunLoop(kIPCPosixTestTimeOutInMS); | |
219 ASSERT_EQ(listener.status(), IPCChannelPosixTestListener::CONNECTED); | |
220 ASSERT_TRUE(channel.HasAcceptedConnection()); | |
221 channel.ResetToAcceptingConnectionState(); | |
222 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
223 | |
224 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixTestConnectionProc", | |
225 false); | |
226 ASSERT_TRUE(handle2); | |
227 SpinRunLoop(kIPCPosixTestTimeOutInMS); | |
228 ASSERT_EQ(listener.status(), IPCChannelPosixTestListener::CONNECTED); | |
229 ASSERT_TRUE(channel.HasAcceptedConnection()); | |
230 IPC::Message* message = new IPC::Message(0, // routing_id | |
231 QUIT_MESSAGE, // message type | |
232 IPC::Message::PRIORITY_NORMAL); | |
233 channel.Send(message); | |
234 SpinRunLoop(kIPCPosixTestTimeOutInMS); | |
235 EXPECT_TRUE(base::KillProcess(handle, 0, false)); | |
236 int exit_code = 0; | |
237 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); | |
238 EXPECT_EQ(exit_code, 0); | |
239 ASSERT_EQ(listener.status(), IPCChannelPosixTestListener::CHANNEL_ERROR); | |
240 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
241 } | |
242 | |
243 TEST_F(IPCChannelPosixTest, MultiConnection) { | |
244 // Test setting up a connection to an external process, and then have | |
245 // another external process attempt to connect to us. | |
246 IPCChannelPosixTestListener listener; | |
247 IPC::ChannelHandle chan_handle(kConnectionSocketTestName); | |
248 SetUpSocket(&chan_handle, IPC::Channel::MODE_NAMED_SERVER); | |
249 IPC::Channel channel(chan_handle, IPC::Channel::MODE_NAMED_SERVER, &listener); | |
250 ASSERT_TRUE(channel.Connect()); | |
251 ASSERT_TRUE(channel.AcceptsConnections()); | |
252 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
253 | |
254 base::ProcessHandle handle = SpawnChild("IPCChannelPosixTestConnectionProc", | |
255 false); | |
256 ASSERT_TRUE(handle); | |
257 SpinRunLoop(kIPCPosixTestTimeOutInMS); | |
258 ASSERT_EQ(listener.status(), IPCChannelPosixTestListener::CONNECTED); | |
259 ASSERT_TRUE(channel.HasAcceptedConnection()); | |
260 base::ProcessHandle handle2 = SpawnChild("IPCChannelPosixFailConnectionProc", | |
261 false); | |
262 ASSERT_TRUE(handle2); | |
263 SpinRunLoop(kIPCPosixTestTimeOutInMS); | |
264 int exit_code = 0; | |
265 EXPECT_TRUE(base::WaitForExitCode(handle2, &exit_code)); | |
266 EXPECT_EQ(exit_code, 0); | |
267 ASSERT_EQ(listener.status(), IPCChannelPosixTestListener::DENIED); | |
268 ASSERT_TRUE(channel.HasAcceptedConnection()); | |
269 IPC::Message* message = new IPC::Message(0, // routing_id | |
270 QUIT_MESSAGE, // message type | |
271 IPC::Message::PRIORITY_NORMAL); | |
272 channel.Send(message); | |
273 SpinRunLoop(kIPCPosixTestTimeOutInMS); | |
274 EXPECT_TRUE(base::WaitForExitCode(handle, &exit_code)); | |
275 EXPECT_EQ(exit_code, 0); | |
276 ASSERT_EQ(listener.status(), IPCChannelPosixTestListener::CHANNEL_ERROR); | |
277 ASSERT_FALSE(channel.HasAcceptedConnection()); | |
278 } | |
279 | |
280 // A long running process that connects to us | |
281 MULTIPROCESS_TEST_MAIN(IPCChannelPosixTestConnectionProc) { | |
282 MessageLoopForIO message_loop; | |
283 IPCChannelPosixTestListener listener; | |
284 IPC::ChannelHandle handle(IPCChannelPosixTest::kConnectionSocketTestName); | |
285 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); | |
286 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); | |
287 EXPECT_TRUE(channel.Connect()); | |
288 while (true) { | |
Paweł Hajdan Jr.
2010/12/10 18:48:15
Why do we spin the loop multiple times? Can't we d
dmac
2010/12/13 18:32:29
No, because some of the tests will return a variet
Paweł Hajdan Jr.
2010/12/13 20:48:40
I still don't understand. It's the test quitting t
| |
289 IPCChannelPosixTest::SpinRunLoop(kIPCPosixTestTimeOutInMS); | |
290 if (listener.status() == IPCChannelPosixTestListener::MESSAGE_RECEIVED) { | |
291 break; | |
292 } | |
293 } | |
294 return 0; | |
295 } | |
296 | |
297 // Simple external process that shouldn't be able to connect to us. | |
298 MULTIPROCESS_TEST_MAIN(IPCChannelPosixFailConnectionProc) { | |
299 MessageLoopForIO message_loop; | |
300 IPCChannelPosixTestListener listener; | |
301 IPC::ChannelHandle handle(IPCChannelPosixTest::kConnectionSocketTestName); | |
302 IPCChannelPosixTest::SetUpSocket(&handle, IPC::Channel::MODE_NAMED_CLIENT); | |
303 IPC::Channel channel(handle, IPC::Channel::MODE_NAMED_CLIENT, &listener); | |
304 | |
305 // In this case connect may succeed or fail depending on if the packet | |
306 // actually gets sent at sendmsg. Since we never delay on send, we may not | |
307 // see the error. However even if connect succeeds, eventually we will get an | |
308 // error back since the channel will be closed when we attempt to read from | |
309 // it. | |
310 bool connected = channel.Connect(); | |
311 if (connected) { | |
312 IPCChannelPosixTest::SpinRunLoop(kIPCPosixTestTimeOutInMS); | |
313 EXPECT_EQ(listener.status(), IPCChannelPosixTestListener::CHANNEL_ERROR); | |
314 } else { | |
315 EXPECT_EQ(listener.status(), IPCChannelPosixTestListener::DISCONNECTED); | |
316 } | |
317 return 0; | |
318 } | |
319 | |
OLD | NEW |