| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
| 13 #include "build/build_config.h" | |
| 14 #include "ipc/ipc_channel.h" | |
| 15 #include "ipc/ipc_channel_proxy.h" | 13 #include "ipc/ipc_channel_proxy.h" |
| 16 #include "ipc/ipc_message_macros.h" | |
| 17 #include "ipc/ipc_message_utils.h" | |
| 18 #include "ipc/ipc_message_utils_impl.h" | |
| 19 #include "ipc/ipc_tests.h" | 14 #include "ipc/ipc_tests.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "testing/multiprocess_func_list.h" | 16 #include "testing/multiprocess_func_list.h" |
| 22 | 17 |
| 23 #if defined(OS_POSIX) | 18 #if defined(OS_POSIX) |
| 24 #include "base/file_descriptor_posix.h" | 19 #include "base/file_descriptor_posix.h" |
| 25 #endif | 20 #endif |
| 26 | 21 |
| 27 enum IPCMessageIds { | 22 // IPC messages for testing --------------------------------------------------- |
| 28 UNUSED_IPC_TYPE, | 23 |
| 29 SERVER_FIRST_IPC_TYPE, // SetHandle message sent to server. | 24 #define IPC_MESSAGE_IMPL |
| 30 SERVER_SECOND_IPC_TYPE, // Shutdown message sent to server. | 25 #include "ipc/ipc_message_macros.h" |
| 31 CLIENT_FIRST_IPC_TYPE // Response message sent to client. | 26 |
| 32 }; | 27 #define IPC_MESSAGE_START TestMsgStart |
| 28 |
| 29 // Message class to pass a base::SyncSocket::Handle to another process. This |
| 30 // is not as easy as it sounds, because of the differences in transferring |
| 31 // Windows HANDLEs versus posix file descriptors. |
| 32 #if defined(OS_WIN) |
| 33 IPC_MESSAGE_CONTROL1(MsgClassSetHandle, base::SyncSocket::Handle) |
| 34 #elif defined(OS_POSIX) |
| 35 IPC_MESSAGE_CONTROL1(MsgClassSetHandle, base::FileDescriptor) |
| 36 #endif |
| 37 |
| 38 // Message class to pass a response to the server. |
| 39 IPC_MESSAGE_CONTROL1(MsgClassResponse, std::string) |
| 40 |
| 41 // Message class to tell the server to shut down. |
| 42 IPC_MESSAGE_CONTROL0(MsgClassShutdown) |
| 43 |
| 44 // ---------------------------------------------------------------------------- |
| 33 | 45 |
| 34 namespace { | 46 namespace { |
| 35 const char kHelloString[] = "Hello, SyncSocket Client"; | 47 const char kHelloString[] = "Hello, SyncSocket Client"; |
| 36 const size_t kHelloStringLength = arraysize(kHelloString); | 48 const size_t kHelloStringLength = arraysize(kHelloString); |
| 37 } // namespace | 49 } // namespace |
| 38 | 50 |
| 39 // Message class to pass a base::SyncSocket::Handle to another process. | |
| 40 // This is not as easy as it sounds, because of the differences in transferring | |
| 41 // Windows HANDLEs versus posix file descriptors. | |
| 42 #if defined(OS_WIN) | |
| 43 class MsgClassSetHandle | |
| 44 : public IPC::MessageWithTuple< Tuple1<base::SyncSocket::Handle> > { | |
| 45 public: | |
| 46 enum { ID = SERVER_FIRST_IPC_TYPE }; | |
| 47 explicit MsgClassSetHandle(const base::SyncSocket::Handle arg1) | |
| 48 : IPC::MessageWithTuple< Tuple1<base::SyncSocket::Handle> >( | |
| 49 MSG_ROUTING_CONTROL, ID, MakeRefTuple(arg1)) {} | |
| 50 | |
| 51 private: | |
| 52 DISALLOW_COPY_AND_ASSIGN(MsgClassSetHandle); | |
| 53 }; | |
| 54 #elif defined(OS_POSIX) | |
| 55 class MsgClassSetHandle | |
| 56 : public IPC::MessageWithTuple< Tuple1<base::FileDescriptor> > { | |
| 57 public: | |
| 58 enum { ID = SERVER_FIRST_IPC_TYPE }; | |
| 59 explicit MsgClassSetHandle(const base::FileDescriptor& arg1) | |
| 60 : IPC::MessageWithTuple< Tuple1<base::FileDescriptor> >( | |
| 61 MSG_ROUTING_CONTROL, ID, MakeRefTuple(arg1)) {} | |
| 62 | |
| 63 private: | |
| 64 DISALLOW_COPY_AND_ASSIGN(MsgClassSetHandle); | |
| 65 }; | |
| 66 #else | |
| 67 # error "What platform?" | |
| 68 #endif // defined(OS_WIN) | |
| 69 | |
| 70 // Message class to pass a response to the server. | |
| 71 class MsgClassResponse | |
| 72 : public IPC::MessageWithTuple< Tuple1<std::string> > { | |
| 73 public: | |
| 74 enum { ID = CLIENT_FIRST_IPC_TYPE }; | |
| 75 explicit MsgClassResponse(const std::string& arg1) | |
| 76 : IPC::MessageWithTuple< Tuple1<std::string> >( | |
| 77 MSG_ROUTING_CONTROL, ID, MakeRefTuple(arg1)) {} | |
| 78 | |
| 79 private: | |
| 80 DISALLOW_COPY_AND_ASSIGN(MsgClassResponse); | |
| 81 }; | |
| 82 | |
| 83 // Message class to tell the server to shut down. | |
| 84 class MsgClassShutdown | |
| 85 : public IPC::MessageWithTuple< Tuple0 > { | |
| 86 public: | |
| 87 enum { ID = SERVER_SECOND_IPC_TYPE }; | |
| 88 MsgClassShutdown() | |
| 89 : IPC::MessageWithTuple< Tuple0 >( | |
| 90 MSG_ROUTING_CONTROL, ID, MakeTuple()) {} | |
| 91 | |
| 92 private: | |
| 93 DISALLOW_COPY_AND_ASSIGN(MsgClassShutdown); | |
| 94 }; | |
| 95 | |
| 96 // The SyncSocket server listener class processes two sorts of | 51 // The SyncSocket server listener class processes two sorts of |
| 97 // messages from the client. | 52 // messages from the client. |
| 98 class SyncSocketServerListener : public IPC::Channel::Listener { | 53 class SyncSocketServerListener : public IPC::Channel::Listener { |
| 99 public: | 54 public: |
| 100 SyncSocketServerListener() : chan_(NULL) { | 55 SyncSocketServerListener() : chan_(NULL) { |
| 101 } | 56 } |
| 102 | 57 |
| 103 void Init(IPC::Channel* chan) { | 58 void Init(IPC::Channel* chan) { |
| 104 chan_ = chan; | 59 chan_ = chan; |
| 105 } | 60 } |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 241 #endif // defined(OS_WIN) | 196 #endif // defined(OS_WIN) |
| 242 EXPECT_TRUE(chan.Send(msg)); | 197 EXPECT_TRUE(chan.Send(msg)); |
| 243 // Use the current thread as the I/O thread. | 198 // Use the current thread as the I/O thread. |
| 244 MessageLoop::current()->Run(); | 199 MessageLoop::current()->Run(); |
| 245 // Shut down. | 200 // Shut down. |
| 246 delete pair[0]; | 201 delete pair[0]; |
| 247 delete pair[1]; | 202 delete pair[1]; |
| 248 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000)); | 203 EXPECT_TRUE(base::WaitForSingleProcess(server_process, 5000)); |
| 249 base::CloseProcessHandle(server_process); | 204 base::CloseProcessHandle(server_process); |
| 250 } | 205 } |
| OLD | NEW |