| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/sync_socket.h" | 5 #include "base/sync_socket.h" |
| 6 | 6 |
| 7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 | 10 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 base::SyncSocket sync_socket(handle); | 89 base::SyncSocket sync_socket(handle); |
| 90 EXPECT_EQ(sync_socket.Send(kHelloString, kHelloStringLength), | 90 EXPECT_EQ(sync_socket.Send(kHelloString, kHelloStringLength), |
| 91 kHelloStringLength); | 91 kHelloStringLength); |
| 92 IPC::Message* msg = new MsgClassResponse(kHelloString); | 92 IPC::Message* msg = new MsgClassResponse(kHelloString); |
| 93 EXPECT_TRUE(chan_->Send(msg)); | 93 EXPECT_TRUE(chan_->Send(msg)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 // When the client responds, it sends back a shutdown message, | 96 // When the client responds, it sends back a shutdown message, |
| 97 // which causes the message loop to exit. | 97 // which causes the message loop to exit. |
| 98 void OnMsgClassShutdown() { | 98 void OnMsgClassShutdown() { |
| 99 MessageLoop::current()->Quit(); | 99 base::MessageLoop::current()->Quit(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 IPC::Channel* chan_; | 102 IPC::Channel* chan_; |
| 103 | 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(SyncSocketServerListener); | 104 DISALLOW_COPY_AND_ASSIGN(SyncSocketServerListener); |
| 105 }; | 105 }; |
| 106 | 106 |
| 107 // Runs the fuzzing server child mode. Returns when the preset number of | 107 // Runs the fuzzing server child mode. Returns when the preset number of |
| 108 // messages have been received. | 108 // messages have been received. |
| 109 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SyncSocketServerClient) { | 109 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SyncSocketServerClient) { |
| 110 MessageLoopForIO main_message_loop; | 110 base::MessageLoopForIO main_message_loop; |
| 111 SyncSocketServerListener listener; | 111 SyncSocketServerListener listener; |
| 112 IPC::Channel channel(IPCTestBase::GetChannelName("SyncSocketServerClient"), | 112 IPC::Channel channel(IPCTestBase::GetChannelName("SyncSocketServerClient"), |
| 113 IPC::Channel::MODE_CLIENT, | 113 IPC::Channel::MODE_CLIENT, |
| 114 &listener); | 114 &listener); |
| 115 EXPECT_TRUE(channel.Connect()); | 115 EXPECT_TRUE(channel.Connect()); |
| 116 listener.Init(&channel); | 116 listener.Init(&channel); |
| 117 MessageLoop::current()->Run(); | 117 base::MessageLoop::current()->Run(); |
| 118 return 0; | 118 return 0; |
| 119 } | 119 } |
| 120 | 120 |
| 121 // The SyncSocket client listener only processes one sort of message, | 121 // The SyncSocket client listener only processes one sort of message, |
| 122 // a response from the server. | 122 // a response from the server. |
| 123 class SyncSocketClientListener : public IPC::Listener { | 123 class SyncSocketClientListener : public IPC::Listener { |
| 124 public: | 124 public: |
| 125 SyncSocketClientListener() { | 125 SyncSocketClientListener() { |
| 126 } | 126 } |
| 127 | 127 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 147 // We rely on the order of sync_socket.Send() and chan_->Send() in | 147 // We rely on the order of sync_socket.Send() and chan_->Send() in |
| 148 // the SyncSocketServerListener object. | 148 // the SyncSocketServerListener object. |
| 149 EXPECT_EQ(kHelloStringLength, socket_->Peek()); | 149 EXPECT_EQ(kHelloStringLength, socket_->Peek()); |
| 150 char buf[kHelloStringLength]; | 150 char buf[kHelloStringLength]; |
| 151 socket_->Receive(static_cast<void*>(buf), kHelloStringLength); | 151 socket_->Receive(static_cast<void*>(buf), kHelloStringLength); |
| 152 EXPECT_EQ(strcmp(str.c_str(), buf), 0); | 152 EXPECT_EQ(strcmp(str.c_str(), buf), 0); |
| 153 // After receiving from the socket there should be no bytes left. | 153 // After receiving from the socket there should be no bytes left. |
| 154 EXPECT_EQ(0U, socket_->Peek()); | 154 EXPECT_EQ(0U, socket_->Peek()); |
| 155 IPC::Message* msg = new MsgClassShutdown(); | 155 IPC::Message* msg = new MsgClassShutdown(); |
| 156 EXPECT_TRUE(chan_->Send(msg)); | 156 EXPECT_TRUE(chan_->Send(msg)); |
| 157 MessageLoop::current()->Quit(); | 157 base::MessageLoop::current()->Quit(); |
| 158 } | 158 } |
| 159 | 159 |
| 160 base::SyncSocket* socket_; | 160 base::SyncSocket* socket_; |
| 161 IPC::Channel* chan_; | 161 IPC::Channel* chan_; |
| 162 | 162 |
| 163 DISALLOW_COPY_AND_ASSIGN(SyncSocketClientListener); | 163 DISALLOW_COPY_AND_ASSIGN(SyncSocketClientListener); |
| 164 }; | 164 }; |
| 165 | 165 |
| 166 class SyncSocketTest : public IPCTestBase { | 166 class SyncSocketTest : public IPCTestBase { |
| 167 }; | 167 }; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 191 // Set up a message to pass the handle to the server. | 191 // Set up a message to pass the handle to the server. |
| 192 IPC::Message* msg = new MsgClassSetHandle(target_handle); | 192 IPC::Message* msg = new MsgClassSetHandle(target_handle); |
| 193 #else | 193 #else |
| 194 target_handle = pair[1].handle(); | 194 target_handle = pair[1].handle(); |
| 195 // Set up a message to pass the handle to the server. | 195 // Set up a message to pass the handle to the server. |
| 196 base::FileDescriptor filedesc(target_handle, false); | 196 base::FileDescriptor filedesc(target_handle, false); |
| 197 IPC::Message* msg = new MsgClassSetHandle(filedesc); | 197 IPC::Message* msg = new MsgClassSetHandle(filedesc); |
| 198 #endif // defined(OS_WIN) | 198 #endif // defined(OS_WIN) |
| 199 EXPECT_TRUE(sender()->Send(msg)); | 199 EXPECT_TRUE(sender()->Send(msg)); |
| 200 // Use the current thread as the I/O thread. | 200 // Use the current thread as the I/O thread. |
| 201 MessageLoop::current()->Run(); | 201 base::MessageLoop::current()->Run(); |
| 202 // Shut down. | 202 // Shut down. |
| 203 pair[0].Close(); | 203 pair[0].Close(); |
| 204 pair[1].Close(); | 204 pair[1].Close(); |
| 205 EXPECT_TRUE(WaitForClientShutdown()); | 205 EXPECT_TRUE(WaitForClientShutdown()); |
| 206 DestroyChannel(); | 206 DestroyChannel(); |
| 207 } | 207 } |
| 208 | 208 |
| 209 // A blocking read operation that will block the thread until it receives | 209 // A blocking read operation that will block the thread until it receives |
| 210 // |length| bytes of packets or Shutdown() is called on another thread. | 210 // |length| bytes of packets or Shutdown() is called on another thread. |
| 211 static void BlockingRead(base::SyncSocket* socket, char* buf, | 211 static void BlockingRead(base::SyncSocket* socket, char* buf, |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 | 300 |
| 301 // Read from another socket to free some space for a new write. | 301 // Read from another socket to free some space for a new write. |
| 302 char hello[kHelloStringLength] = {0}; | 302 char hello[kHelloStringLength] = {0}; |
| 303 pair[1].Receive(&hello[0], sizeof(hello)); | 303 pair[1].Receive(&hello[0], sizeof(hello)); |
| 304 | 304 |
| 305 // Should be able to write more data to the buffer now. | 305 // Should be able to write more data to the buffer now. |
| 306 EXPECT_EQ(kHelloStringLength, pair[0].Send(kHelloString, kHelloStringLength)); | 306 EXPECT_EQ(kHelloStringLength, pair[0].Send(kHelloString, kHelloStringLength)); |
| 307 } | 307 } |
| 308 | 308 |
| 309 } // namespace | 309 } // namespace |
| OLD | NEW |