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