OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdio.h> | 5 #include <stdio.h> |
6 #include <string> | 6 #include <string> |
7 #include <sstream> | 7 #include <sstream> |
8 | 8 |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #if defined(OS_LINUX) || defined(OS_MACOSX) | 10 #if defined(OS_LINUX) || defined(OS_MACOSX) |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 // string as was written on the SyncSocket. These are compared | 187 // string as was written on the SyncSocket. These are compared |
188 // and a shutdown message is sent back to the server. | 188 // and a shutdown message is sent back to the server. |
189 void OnMsgClassResponse(const std::string& str) { | 189 void OnMsgClassResponse(const std::string& str) { |
190 // We rely on the order of sync_socket.Send() and chan_->Send() in | 190 // We rely on the order of sync_socket.Send() and chan_->Send() in |
191 // the SyncSocketServerListener object. | 191 // the SyncSocketServerListener object. |
192 EXPECT_EQ(kHelloStringLength, socket_->Peek()); | 192 EXPECT_EQ(kHelloStringLength, socket_->Peek()); |
193 char buf[kHelloStringLength]; | 193 char buf[kHelloStringLength]; |
194 socket_->Receive(static_cast<void*>(buf), kHelloStringLength); | 194 socket_->Receive(static_cast<void*>(buf), kHelloStringLength); |
195 EXPECT_EQ(strcmp(str.c_str(), buf), 0); | 195 EXPECT_EQ(strcmp(str.c_str(), buf), 0); |
196 // After receiving from the socket there should be no bytes left. | 196 // After receiving from the socket there should be no bytes left. |
197 EXPECT_EQ(0, socket_->Peek()); | 197 EXPECT_EQ(0U, socket_->Peek()); |
198 IPC::Message* msg = new MsgClassShutdown(); | 198 IPC::Message* msg = new MsgClassShutdown(); |
199 EXPECT_TRUE(chan_->Send(msg)); | 199 EXPECT_TRUE(chan_->Send(msg)); |
200 MessageLoop::current()->Quit(); | 200 MessageLoop::current()->Quit(); |
201 } | 201 } |
202 | 202 |
203 base::SyncSocket* socket_; | 203 base::SyncSocket* socket_; |
204 IPC::Channel* chan_; | 204 IPC::Channel* chan_; |
205 | 205 |
206 DISALLOW_COPY_AND_ASSIGN(SyncSocketClientListener); | 206 DISALLOW_COPY_AND_ASSIGN(SyncSocketClientListener); |
207 }; | 207 }; |
208 | 208 |
209 class SyncSocketTest : public IPCChannelTest { | 209 class SyncSocketTest : public IPCChannelTest { |
210 }; | 210 }; |
211 | 211 |
212 TEST_F(SyncSocketTest, SanityTest) { | 212 TEST_F(SyncSocketTest, SanityTest) { |
213 SyncSocketClientListener listener; | 213 SyncSocketClientListener listener; |
214 IPC::Channel chan(kSyncSocketChannel, IPC::Channel::MODE_SERVER, | 214 IPC::Channel chan(kSyncSocketChannel, IPC::Channel::MODE_SERVER, |
215 &listener); | 215 &listener); |
216 base::ProcessHandle server_process = SpawnChild(SYNC_SOCKET_SERVER, &chan); | 216 base::ProcessHandle server_process = SpawnChild(SYNC_SOCKET_SERVER, &chan); |
217 ASSERT_TRUE(server_process); | 217 ASSERT_TRUE(server_process); |
218 // Create a pair of SyncSockets. | 218 // Create a pair of SyncSockets. |
219 base::SyncSocket* pair[2]; | 219 base::SyncSocket* pair[2]; |
220 base::SyncSocket::CreatePair(pair); | 220 base::SyncSocket::CreatePair(pair); |
221 // Immediately after creation there should be no pending bytes. | 221 // Immediately after creation there should be no pending bytes. |
222 EXPECT_EQ(0, pair[0]->Peek()); | 222 EXPECT_EQ(0U, pair[0]->Peek()); |
223 EXPECT_EQ(0, pair[1]->Peek()); | 223 EXPECT_EQ(0U, pair[1]->Peek()); |
224 base::SyncSocket::Handle target_handle; | 224 base::SyncSocket::Handle target_handle; |
225 // Connect the channel and listener. | 225 // Connect the channel and listener. |
226 ASSERT_TRUE(chan.Connect()); | 226 ASSERT_TRUE(chan.Connect()); |
227 listener.Init(pair[0], &chan); | 227 listener.Init(pair[0], &chan); |
228 #if defined(OS_WIN) | 228 #if defined(OS_WIN) |
229 // On windows we need to duplicate the handle into the server process. | 229 // On windows we need to duplicate the handle into the server process. |
230 BOOL retval = DuplicateHandle(GetCurrentProcess(), pair[1]->handle(), | 230 BOOL retval = DuplicateHandle(GetCurrentProcess(), pair[1]->handle(), |
231 server_process, &target_handle, | 231 server_process, &target_handle, |
232 0, FALSE, DUPLICATE_SAME_ACCESS); | 232 0, FALSE, DUPLICATE_SAME_ACCESS); |
233 EXPECT_TRUE(retval); | 233 EXPECT_TRUE(retval); |
234 // Set up a message to pass the handle to the server. | 234 // Set up a message to pass the handle to the server. |
235 IPC::Message* msg = new MsgClassSetHandle(target_handle); | 235 IPC::Message* msg = new MsgClassSetHandle(target_handle); |
236 #else | 236 #else |
237 target_handle = pair[1]->handle(); | 237 target_handle = pair[1]->handle(); |
238 // Set up a message to pass the handle to the server. | 238 // Set up a message to pass the handle to the server. |
239 base::FileDescriptor filedesc(target_handle, false); | 239 base::FileDescriptor filedesc(target_handle, false); |
240 IPC::Message* msg = new MsgClassSetHandle(filedesc); | 240 IPC::Message* msg = new MsgClassSetHandle(filedesc); |
241 #endif // defined(OS_WIN) | 241 #endif // defined(OS_WIN) |
242 EXPECT_TRUE(chan.Send(msg)); | 242 EXPECT_TRUE(chan.Send(msg)); |
243 // Use the current thread as the I/O thread. | 243 // Use the current thread as the I/O thread. |
244 MessageLoop::current()->Run(); | 244 MessageLoop::current()->Run(); |
245 // Shut down. | 245 // Shut down. |
246 delete pair[0]; | 246 delete pair[0]; |
247 delete pair[1]; | 247 delete pair[1]; |
248 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000)); | 248 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000)); |
249 base::CloseProcessHandle(server_process); | 249 base::CloseProcessHandle(server_process); |
250 } | 250 } |
OLD | NEW |