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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_WIN) | |
8 #include <windows.h> | |
9 #elif defined(OS_POSIX) | |
10 #include <sys/types.h> | |
11 #include <unistd.h> | |
12 #endif | |
13 | |
14 #include <utility> | |
15 | |
16 #include "ipc/ipc_test_base.h" | 7 #include "ipc/ipc_test_base.h" |
17 | 8 |
18 #include "base/command_line.h" | 9 #include "base/command_line.h" |
19 #include "base/debug/debug_on_start_win.h" | 10 #include "base/debug/debug_on_start_win.h" |
20 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
21 #include "base/time.h" | 12 #include "base/time.h" |
22 #include "ipc/ipc_descriptors.h" | 13 #include "ipc/ipc_descriptors.h" |
23 #include "ipc/ipc_channel.h" | |
24 #include "ipc/ipc_channel_proxy.h" | |
25 #include "ipc/ipc_message_utils.h" | |
26 #include "ipc/ipc_multiprocess_test.h" | |
27 #include "ipc/ipc_sender.h" | |
28 #include "ipc/ipc_switches.h" | 14 #include "ipc/ipc_switches.h" |
29 | 15 |
30 const char kTestClientChannel[] = "T1"; | 16 // static |
31 const char kReflectorChannel[] = "T2"; | 17 std::string IPCTestBase::GetChannelName(const std::string& test_client_name) { |
32 const char kFuzzerChannel[] = "F3"; | 18 DCHECK(!test_client_name.empty()); |
33 const char kSyncSocketChannel[] = "S4"; | 19 return test_client_name + "__Channel"; |
| 20 } |
| 21 |
| 22 IPCTestBase::IPCTestBase() |
| 23 : client_process_(base::kNullProcessHandle) { |
| 24 } |
| 25 |
| 26 IPCTestBase::~IPCTestBase() { |
| 27 } |
34 | 28 |
35 void IPCTestBase::SetUp() { | 29 void IPCTestBase::SetUp() { |
36 MultiProcessTest::SetUp(); | 30 MultiProcessTest::SetUp(); |
37 | 31 |
38 // Construct a fresh IO Message loop for the duration of each test. | 32 // Construct a fresh IO Message loop for the duration of each test. |
39 message_loop_ = new MessageLoopForIO(); | 33 DCHECK(!message_loop_.get()); |
| 34 message_loop_.reset(new MessageLoopForIO()); |
40 } | 35 } |
41 | 36 |
42 void IPCTestBase::TearDown() { | 37 void IPCTestBase::TearDown() { |
43 delete message_loop_; | 38 DCHECK(message_loop_.get()); |
44 message_loop_ = NULL; | 39 message_loop_.reset(); |
45 | |
46 MultiProcessTest::TearDown(); | 40 MultiProcessTest::TearDown(); |
47 } | 41 } |
48 | 42 |
49 #if defined(OS_WIN) | 43 void IPCTestBase::Init(const std::string& test_client_name) { |
50 base::ProcessHandle IPCTestBase::SpawnChild(IPCTestBase::ChildType child_type, | 44 DCHECK(!test_client_name.empty()); |
51 IPC::Channel* channel) { | 45 DCHECK(test_client_name_.empty()); |
52 // kDebugChildren support. | 46 test_client_name_ = test_client_name; |
| 47 } |
| 48 |
| 49 void IPCTestBase::CreateChannel(IPC::Listener* listener) { |
| 50 return CreateChannelFromChannelHandle(GetChannelName(test_client_name_), |
| 51 listener); |
| 52 } |
| 53 |
| 54 bool IPCTestBase::ConnectChannel() { |
| 55 CHECK(channel_.get()); |
| 56 return channel_->Connect(); |
| 57 } |
| 58 |
| 59 void IPCTestBase::DestroyChannel() { |
| 60 DCHECK(channel_.get()); |
| 61 channel_.reset(); |
| 62 } |
| 63 |
| 64 void IPCTestBase::CreateChannelFromChannelHandle( |
| 65 const IPC::ChannelHandle& channel_handle, |
| 66 IPC::Listener* listener) { |
| 67 CHECK(!channel_.get()); |
| 68 CHECK(!channel_proxy_.get()); |
| 69 channel_.reset(new IPC::Channel(channel_handle, |
| 70 IPC::Channel::MODE_SERVER, |
| 71 listener)); |
| 72 } |
| 73 |
| 74 void IPCTestBase::CreateChannelProxy( |
| 75 IPC::Listener* listener, |
| 76 base::SingleThreadTaskRunner* ipc_task_runner) { |
| 77 CHECK(!channel_.get()); |
| 78 CHECK(!channel_proxy_.get()); |
| 79 channel_proxy_.reset(new IPC::ChannelProxy(GetChannelName(test_client_name_), |
| 80 IPC::Channel::MODE_SERVER, |
| 81 listener, |
| 82 ipc_task_runner)); |
| 83 } |
| 84 |
| 85 void IPCTestBase::DestroyChannelProxy() { |
| 86 CHECK(channel_proxy_.get()); |
| 87 channel_proxy_.reset(); |
| 88 } |
| 89 |
| 90 bool IPCTestBase::StartClient() { |
| 91 DCHECK(client_process_ == base::kNullProcessHandle); |
| 92 |
| 93 std::string test_main = test_client_name_ + "TestClientMain"; |
53 bool debug_on_start = | 94 bool debug_on_start = |
54 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren); | 95 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren); |
55 | 96 |
56 switch (child_type) { | 97 #if defined(OS_WIN) |
57 case TEST_CLIENT: | 98 client_process_ = MultiProcessTest::SpawnChild(test_main, debug_on_start); |
58 return MultiProcessTest::SpawnChild("RunTestClient", debug_on_start); | 99 #elif defined(OS_POSIX) |
59 case TEST_REFLECTOR: | 100 base::FileHandleMappingVector fds_to_map; |
60 return MultiProcessTest::SpawnChild("RunReflector", debug_on_start); | 101 const int ipcfd = channel_.get() ? channel_->GetClientFileDescriptor() : |
61 case FUZZER_SERVER: | 102 channel_proxy_->GetClientFileDescriptor(); |
62 return MultiProcessTest::SpawnChild("RunFuzzServer", debug_on_start); | 103 if (ipcfd > -1) |
63 case SYNC_SOCKET_SERVER: | 104 fds_to_map.push_back(std::pair<int, int>(ipcfd, kPrimaryIPCChannel + 3)); |
64 return MultiProcessTest::SpawnChild("RunSyncSocketServer", debug_on_start); | 105 |
65 default: | 106 client_process_ = MultiProcessTest::SpawnChild(test_main, |
66 return NULL; | 107 fds_to_map, |
67 } | 108 debug_on_start); |
| 109 #endif |
| 110 |
| 111 return client_process_ != base::kNullProcessHandle; |
68 } | 112 } |
69 #elif defined(OS_POSIX) | |
70 base::ProcessHandle IPCTestBase::SpawnChild(IPCTestBase::ChildType child_type, | |
71 IPC::Channel* channel) { | |
72 // kDebugChildren support. | |
73 bool debug_on_start = | |
74 CommandLine::ForCurrentProcess()->HasSwitch(switches::kDebugChildren); | |
75 | 113 |
76 base::FileHandleMappingVector fds_to_map; | 114 bool IPCTestBase::WaitForClientShutdown() { |
77 const int ipcfd = channel->GetClientFileDescriptor(); | 115 DCHECK(client_process_ != base::kNullProcessHandle); |
78 if (ipcfd > -1) { | |
79 fds_to_map.push_back(std::pair<int, int>(ipcfd, kPrimaryIPCChannel + 3)); | |
80 } | |
81 | 116 |
82 base::ProcessHandle ret = base::kNullProcessHandle; | 117 bool rv = base::WaitForSingleProcess(client_process_, |
83 switch (child_type) { | 118 base::TimeDelta::FromSeconds(5)); |
84 case TEST_CLIENT: | 119 base::CloseProcessHandle(client_process_); |
85 ret = MultiProcessTest::SpawnChild("RunTestClient", | 120 client_process_ = base::kNullProcessHandle; |
86 fds_to_map, | 121 return rv; |
87 debug_on_start); | |
88 break; | |
89 case TEST_DESCRIPTOR_CLIENT: | |
90 ret = MultiProcessTest::SpawnChild("RunTestDescriptorClient", | |
91 fds_to_map, | |
92 debug_on_start); | |
93 break; | |
94 case TEST_DESCRIPTOR_CLIENT_SANDBOXED: | |
95 ret = MultiProcessTest::SpawnChild("RunTestDescriptorClientSandboxed", | |
96 fds_to_map, | |
97 debug_on_start); | |
98 break; | |
99 case TEST_REFLECTOR: | |
100 ret = MultiProcessTest::SpawnChild("RunReflector", | |
101 fds_to_map, | |
102 debug_on_start); | |
103 break; | |
104 case FUZZER_SERVER: | |
105 ret = MultiProcessTest::SpawnChild("RunFuzzServer", | |
106 fds_to_map, | |
107 debug_on_start); | |
108 break; | |
109 case SYNC_SOCKET_SERVER: | |
110 ret = MultiProcessTest::SpawnChild("RunSyncSocketServer", | |
111 fds_to_map, | |
112 debug_on_start); | |
113 break; | |
114 default: | |
115 return base::kNullProcessHandle; | |
116 break; | |
117 } | |
118 return ret; | |
119 } | 122 } |
120 #endif // defined(OS_POSIX) | |
OLD | NEW |