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