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