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