Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(953)

Side by Side Diff: ipc/sync_socket_unittest.cc

Issue 12051048: Refactor (many) IPC tests, notably most of the multiprocess tests. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix win Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ipc/ipc_test_base.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/message_loop.h" 12 #include "base/message_loop.h"
13 #include "base/process_util.h" 13 #include "base/process_util.h"
14 #include "base/threading/thread.h" 14 #include "base/threading/thread.h"
15 #include "ipc/ipc_channel_proxy.h"
16 #include "ipc/ipc_multiprocess_test.h"
17 #include "ipc/ipc_test_base.h" 15 #include "ipc/ipc_test_base.h"
18 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
19 17
20 #if defined(OS_POSIX) 18 #if defined(OS_POSIX)
21 #include "base/file_descriptor_posix.h" 19 #include "base/file_descriptor_posix.h"
22 #endif 20 #endif
23 21
24 // IPC messages for testing ---------------------------------------------------- 22 // IPC messages for testing ----------------------------------------------------
25 23
26 #define IPC_MESSAGE_IMPL 24 #define IPC_MESSAGE_IMPL
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 // which causes the message loop to exit. 97 // which causes the message loop to exit.
100 void OnMsgClassShutdown() { 98 void OnMsgClassShutdown() {
101 MessageLoop::current()->Quit(); 99 MessageLoop::current()->Quit();
102 } 100 }
103 101
104 IPC::Channel* chan_; 102 IPC::Channel* chan_;
105 103
106 DISALLOW_COPY_AND_ASSIGN(SyncSocketServerListener); 104 DISALLOW_COPY_AND_ASSIGN(SyncSocketServerListener);
107 }; 105 };
108 106
109 // Runs the fuzzing server child mode. Returns when the preset number 107 // Runs the fuzzing server child mode. Returns when the preset number of
110 // of messages have been received. 108 // messages have been received.
111 MULTIPROCESS_IPC_TEST_MAIN(RunSyncSocketServer) { 109 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SyncSocketServerClient) {
112 MessageLoopForIO main_message_loop; 110 MessageLoopForIO main_message_loop;
113 SyncSocketServerListener listener; 111 SyncSocketServerListener listener;
114 IPC::Channel chan(kSyncSocketChannel, IPC::Channel::MODE_CLIENT, &listener); 112 IPC::Channel channel(IPCTestBase::GetChannelName("SyncSocketServerClient"),
115 EXPECT_TRUE(chan.Connect()); 113 IPC::Channel::MODE_CLIENT,
116 listener.Init(&chan); 114 &listener);
115 EXPECT_TRUE(channel.Connect());
116 listener.Init(&channel);
117 MessageLoop::current()->Run(); 117 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 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 };
168 168
169 TEST_F(SyncSocketTest, SanityTest) { 169 TEST_F(SyncSocketTest, SanityTest) {
170 Init("SyncSocketServerClient");
171
170 SyncSocketClientListener listener; 172 SyncSocketClientListener listener;
171 IPC::Channel chan(kSyncSocketChannel, IPC::Channel::MODE_SERVER, 173 CreateChannel(&listener);
172 &listener); 174 ASSERT_TRUE(StartClient());
173 base::ProcessHandle server_process = SpawnChild(SYNC_SOCKET_SERVER, &chan);
174 ASSERT_TRUE(server_process);
175 // Create a pair of SyncSockets. 175 // Create a pair of SyncSockets.
176 base::SyncSocket pair[2]; 176 base::SyncSocket pair[2];
177 base::SyncSocket::CreatePair(&pair[0], &pair[1]); 177 base::SyncSocket::CreatePair(&pair[0], &pair[1]);
178 // Immediately after creation there should be no pending bytes. 178 // Immediately after creation there should be no pending bytes.
179 EXPECT_EQ(0U, pair[0].Peek()); 179 EXPECT_EQ(0U, pair[0].Peek());
180 EXPECT_EQ(0U, pair[1].Peek()); 180 EXPECT_EQ(0U, pair[1].Peek());
181 base::SyncSocket::Handle target_handle; 181 base::SyncSocket::Handle target_handle;
182 // Connect the channel and listener. 182 // Connect the channel and listener.
183 ASSERT_TRUE(chan.Connect()); 183 ASSERT_TRUE(ConnectChannel());
184 listener.Init(&pair[0], &chan); 184 listener.Init(&pair[0], channel());
185 #if defined(OS_WIN) 185 #if defined(OS_WIN)
186 // On windows we need to duplicate the handle into the server process. 186 // On windows we need to duplicate the handle into the server process.
187 BOOL retval = DuplicateHandle(GetCurrentProcess(), pair[1].handle(), 187 BOOL retval = DuplicateHandle(GetCurrentProcess(), pair[1].handle(),
188 server_process, &target_handle, 188 client_process(), &target_handle,
189 0, FALSE, DUPLICATE_SAME_ACCESS); 189 0, FALSE, DUPLICATE_SAME_ACCESS);
190 EXPECT_TRUE(retval); 190 EXPECT_TRUE(retval);
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(chan.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 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(base::WaitForSingleProcess( 205 EXPECT_TRUE(WaitForClientShutdown());
206 server_process, base::TimeDelta::FromSeconds(5))); 206 DestroyChannel();
207 base::CloseProcessHandle(server_process);
208 } 207 }
209 208
210
211 // 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
212 // |length| bytes of packets or Shutdown() is called on another thread. 210 // |length| bytes of packets or Shutdown() is called on another thread.
213 static void BlockingRead(base::SyncSocket* socket, char* buf, 211 static void BlockingRead(base::SyncSocket* socket, char* buf,
214 size_t length, size_t* received) { 212 size_t length, size_t* received) {
215 DCHECK(buf != NULL); 213 DCHECK(buf != NULL);
216 // Notify the parent thread that we're up and running. 214 // Notify the parent thread that we're up and running.
217 socket->Send(kHelloString, kHelloStringLength); 215 socket->Send(kHelloString, kHelloStringLength);
218 *received = socket->Receive(buf, length); 216 *received = socket->Receive(buf, length);
219 } 217 }
220 218
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 300
303 // 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.
304 char hello[kHelloStringLength] = {0}; 302 char hello[kHelloStringLength] = {0};
305 pair[1].Receive(&hello[0], sizeof(hello)); 303 pair[1].Receive(&hello[0], sizeof(hello));
306 304
307 // Should be able to write more data to the buffer now. 305 // Should be able to write more data to the buffer now.
308 EXPECT_EQ(kHelloStringLength, pair[0].Send(kHelloString, kHelloStringLength)); 306 EXPECT_EQ(kHelloStringLength, pair[0].Send(kHelloString, kHelloStringLength));
309 } 307 }
310 308
311 } // namespace 309 } // namespace
OLDNEW
« no previous file with comments | « ipc/ipc_test_base.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698