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

Side by Side Diff: remoting/host/security_key/security_key_ipc_server_unittest.cc

Issue 2478443002: Use ChannelMojo for remote security key channels. (Closed)
Patch Set: Created 4 years, 1 month 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
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "remoting/host/security_key/security_key_ipc_server.h" 5 #include "remoting/host/security_key/security_key_ipc_server.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <string> 8 #include <string>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
11 #include "base/bind_helpers.h" 11 #include "base/bind_helpers.h"
12 #include "base/callback.h" 12 #include "base/callback.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/message_loop/message_loop.h" 14 #include "base/message_loop/message_loop.h"
15 #include "base/run_loop.h" 15 #include "base/run_loop.h"
16 #include "ipc/ipc_channel.h" 16 #include "ipc/ipc_channel.h"
17 #include "mojo/edk/embedder/embedder.h"
18 #include "mojo/edk/embedder/named_platform_handle_utils.h"
19 #include "mojo/edk/test/scoped_ipc_support.h"
17 #include "remoting/host/client_session_details.h" 20 #include "remoting/host/client_session_details.h"
18 #include "remoting/host/security_key/fake_security_key_ipc_client.h" 21 #include "remoting/host/security_key/fake_security_key_ipc_client.h"
19 #include "remoting/host/security_key/security_key_ipc_constants.h" 22 #include "remoting/host/security_key/security_key_ipc_constants.h"
20 #include "testing/gtest/include/gtest/gtest.h" 23 #include "testing/gtest/include/gtest/gtest.h"
21 24
22 namespace { 25 namespace {
23 const int kTestConnectionId = 42; 26 const int kTestConnectionId = 42;
24 const int kInitialConnectTimeoutMs = 250; 27 const int kInitialConnectTimeoutMs = 250;
25 const int kConnectionTimeoutErrorDeltaMs = 100; 28 const int kConnectionTimeoutErrorDeltaMs = 100;
26 const int kLargeMessageSizeBytes = 256 * 1024; 29 const int kLargeMessageSizeBytes = 256 * 1024;
(...skipping 22 matching lines...) Expand all
49 // Waits until the current |run_loop_| instance is signaled, then resets it. 52 // Waits until the current |run_loop_| instance is signaled, then resets it.
50 void WaitForOperationComplete(); 53 void WaitForOperationComplete();
51 54
52 // ClientSessionControl overrides: 55 // ClientSessionControl overrides:
53 ClientSessionControl* session_control() override { return nullptr; } 56 ClientSessionControl* session_control() override { return nullptr; }
54 uint32_t desktop_session_id() const override { return peer_session_id_; } 57 uint32_t desktop_session_id() const override { return peer_session_id_; }
55 58
56 // IPC tests require a valid MessageLoop to run. 59 // IPC tests require a valid MessageLoop to run.
57 base::MessageLoopForIO message_loop_; 60 base::MessageLoopForIO message_loop_;
58 61
62 mojo::edk::test::ScopedIPCSupport ipc_support_;
63
59 // Used to allow |message_loop_| to run during tests. The instance is reset 64 // Used to allow |message_loop_| to run during tests. The instance is reset
60 // after each stage of the tests has been completed. 65 // after each stage of the tests has been completed.
61 std::unique_ptr<base::RunLoop> run_loop_; 66 std::unique_ptr<base::RunLoop> run_loop_;
62 67
63 // The object under test. 68 // The object under test.
64 std::unique_ptr<SecurityKeyIpcServer> security_key_ipc_server_; 69 std::unique_ptr<SecurityKeyIpcServer> security_key_ipc_server_;
65 70
66 // Used to validate the object under test uses the correct ID when 71 // Used to validate the object under test uses the correct ID when
67 // communicating over the IPC channel. 72 // communicating over the IPC channel.
68 int last_connection_id_received_ = -1; 73 int last_connection_id_received_ = -1;
69 74
70 // Stores the contents of the last IPC message received for validation. 75 // Stores the contents of the last IPC message received for validation.
71 std::string last_message_received_; 76 std::string last_message_received_;
72 77
73 uint32_t peer_session_id_ = UINT32_MAX; 78 uint32_t peer_session_id_ = UINT32_MAX;
74 79
75 private: 80 private:
76 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcServerTest); 81 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcServerTest);
77 }; 82 };
78 83
79 SecurityKeyIpcServerTest::SecurityKeyIpcServerTest() 84 SecurityKeyIpcServerTest::SecurityKeyIpcServerTest()
80 : run_loop_(new base::RunLoop()) { 85 : ipc_support_(message_loop_.task_runner()),
86 run_loop_(new base::RunLoop()) {
81 #if defined(OS_WIN) 87 #if defined(OS_WIN)
82 EXPECT_TRUE(ProcessIdToSessionId( 88 EXPECT_TRUE(ProcessIdToSessionId(
83 GetCurrentProcessId(), reinterpret_cast<DWORD*>(&peer_session_id_))); 89 GetCurrentProcessId(), reinterpret_cast<DWORD*>(&peer_session_id_)));
84 #endif // defined(OS_WIN) 90 #endif // defined(OS_WIN)
85 91
86 security_key_ipc_server_ = remoting::SecurityKeyIpcServer::Create( 92 security_key_ipc_server_ = remoting::SecurityKeyIpcServer::Create(
87 kTestConnectionId, this, 93 kTestConnectionId, this,
88 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs), 94 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs),
89 base::Bind(&SecurityKeyIpcServerTest::SendRequestToClient, 95 base::Bind(&SecurityKeyIpcServerTest::SendRequestToClient,
90 base::Unretained(this)), 96 base::Unretained(this)),
97 base::Bind(&base::DoNothing),
91 base::Bind(&SecurityKeyIpcServerTest::OperationComplete, 98 base::Bind(&SecurityKeyIpcServerTest::OperationComplete,
92 base::Unretained(this))); 99 base::Unretained(this)));
93 } 100 }
94 101
95 SecurityKeyIpcServerTest::~SecurityKeyIpcServerTest() {} 102 SecurityKeyIpcServerTest::~SecurityKeyIpcServerTest() {}
96 103
97 void SecurityKeyIpcServerTest::OperationComplete() { 104 void SecurityKeyIpcServerTest::OperationComplete() {
98 run_loop_->Quit(); 105 run_loop_->Quit();
99 } 106 }
100 107
101 void SecurityKeyIpcServerTest::WaitForOperationComplete() { 108 void SecurityKeyIpcServerTest::WaitForOperationComplete() {
102 run_loop_->Run(); 109 run_loop_->Run();
103 run_loop_.reset(new base::RunLoop()); 110 run_loop_.reset(new base::RunLoop());
104 } 111 }
105 112
106 void SecurityKeyIpcServerTest::SendRequestToClient(int connection_id, 113 void SecurityKeyIpcServerTest::SendRequestToClient(int connection_id,
107 const std::string& data) { 114 const std::string& data) {
108 last_connection_id_received_ = connection_id; 115 last_connection_id_received_ = connection_id;
109 last_message_received_ = data; 116 last_message_received_ = data;
110 OperationComplete(); 117 OperationComplete();
111 } 118 }
112 119
113 std::string SecurityKeyIpcServerTest::GetUniqueTestChannelName() { 120 std::string SecurityKeyIpcServerTest::GetUniqueTestChannelName() {
114 return GetChannelNamePathPrefixForTest() + "Super_Awesome_Test_Channel." + 121 return GetChannelNamePathPrefixForTest() + "Super_Awesome_Test_Channel." +
115 IPC::Channel::GenerateUniqueRandomChannelID(); 122 IPC::Channel::GenerateUniqueRandomChannelID();
116 } 123 }
117 124
118 TEST_F(SecurityKeyIpcServerTest, HandleSingleSecurityKeyRequest) { 125 TEST_F(SecurityKeyIpcServerTest, HandleSingleSecurityKeyRequest) {
119 std::string channel_name(GetUniqueTestChannelName()); 126 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
120 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( 127 ASSERT_TRUE(security_key_ipc_server_->CreateChannel(
121 channel_name, 128 channel_handle,
122 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); 129 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500)));
123 130
124 // Create a fake client and connect to the IPC server channel. 131 // Create a fake client and connect to the IPC server channel.
125 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( 132 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind(
126 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); 133 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this)));
127 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); 134 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle));
128 WaitForOperationComplete(); 135 WaitForOperationComplete();
129 136
130 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); 137 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected());
131 138
132 // Send a request from the IPC client to the IPC server. 139 // Send a request from the IPC client to the IPC server.
133 std::string request_data("Blergh!"); 140 std::string request_data("Blergh!");
134 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); 141 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data);
135 WaitForOperationComplete(); 142 WaitForOperationComplete();
136 143
137 // Verify the request was received. 144 // Verify the request was received.
138 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); 145 ASSERT_EQ(kTestConnectionId, last_connection_id_received_);
139 ASSERT_EQ(request_data, last_message_received_); 146 ASSERT_EQ(request_data, last_message_received_);
140 147
141 // Send a response from the IPC server to the IPC client. 148 // Send a response from the IPC server to the IPC client.
142 std::string response_data("Blargh!"); 149 std::string response_data("Blargh!");
143 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data)); 150 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data));
144 WaitForOperationComplete(); 151 WaitForOperationComplete();
145 152
146 // Verify the request was received. 153 // Verify the request was received.
147 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); 154 ASSERT_EQ(response_data, fake_ipc_client.last_message_received());
148 155
149 // Typically the client will be the one to close the connection. 156 // Typically the client will be the one to close the connection.
150 fake_ipc_client.CloseIpcConnection(); 157 fake_ipc_client.CloseIpcConnection();
151 } 158 }
152 159
153 TEST_F(SecurityKeyIpcServerTest, HandleLargeSecurityKeyRequest) { 160 TEST_F(SecurityKeyIpcServerTest, HandleLargeSecurityKeyRequest) {
154 std::string channel_name(GetUniqueTestChannelName()); 161 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
155 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( 162 ASSERT_TRUE(security_key_ipc_server_->CreateChannel(
156 channel_name, 163 channel_handle,
157 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); 164 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500)));
158 165
159 // Create a fake client and connect to the IPC server channel. 166 // Create a fake client and connect to the IPC server channel.
160 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( 167 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind(
161 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); 168 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this)));
162 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); 169 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle));
163 WaitForOperationComplete(); 170 WaitForOperationComplete();
164 171
165 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); 172 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected());
166 173
167 // Send a request from the IPC client to the IPC server. 174 // Send a request from the IPC client to the IPC server.
168 std::string request_data(kLargeMessageSizeBytes, 'Y'); 175 std::string request_data(kLargeMessageSizeBytes, 'Y');
169 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); 176 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data);
170 WaitForOperationComplete(); 177 WaitForOperationComplete();
171 178
172 // Verify the request was received. 179 // Verify the request was received.
173 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); 180 ASSERT_EQ(kTestConnectionId, last_connection_id_received_);
174 ASSERT_EQ(request_data, last_message_received_); 181 ASSERT_EQ(request_data, last_message_received_);
175 182
176 // Send a response from the IPC server to the IPC client. 183 // Send a response from the IPC server to the IPC client.
177 std::string response_data(kLargeMessageSizeBytes, 'Z'); 184 std::string response_data(kLargeMessageSizeBytes, 'Z');
178 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data)); 185 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data));
179 WaitForOperationComplete(); 186 WaitForOperationComplete();
180 187
181 // Verify the request was received. 188 // Verify the request was received.
182 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); 189 ASSERT_EQ(response_data, fake_ipc_client.last_message_received());
183 190
184 // Typically the client will be the one to close the connection. 191 // Typically the client will be the one to close the connection.
185 fake_ipc_client.CloseIpcConnection(); 192 fake_ipc_client.CloseIpcConnection();
186 } 193 }
187 194
188 TEST_F(SecurityKeyIpcServerTest, HandleReallyLargeSecurityKeyRequest) { 195 TEST_F(SecurityKeyIpcServerTest, HandleReallyLargeSecurityKeyRequest) {
189 std::string channel_name(GetUniqueTestChannelName()); 196 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
190 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( 197 ASSERT_TRUE(security_key_ipc_server_->CreateChannel(
191 channel_name, 198 channel_handle,
192 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); 199 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500)));
193 200
194 // Create a fake client and connect to the IPC server channel. 201 // Create a fake client and connect to the IPC server channel.
195 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( 202 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind(
196 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); 203 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this)));
197 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); 204 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle));
198 WaitForOperationComplete(); 205 WaitForOperationComplete();
199 206
200 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); 207 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected());
201 208
202 // Send a request from the IPC client to the IPC server. 209 // Send a request from the IPC client to the IPC server.
203 std::string request_data(kLargeMessageSizeBytes * 2, 'Y'); 210 std::string request_data(kLargeMessageSizeBytes * 2, 'Y');
204 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); 211 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data);
205 WaitForOperationComplete(); 212 WaitForOperationComplete();
206 213
207 // Verify the request was received. 214 // Verify the request was received.
208 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); 215 ASSERT_EQ(kTestConnectionId, last_connection_id_received_);
209 ASSERT_EQ(request_data, last_message_received_); 216 ASSERT_EQ(request_data, last_message_received_);
210 217
211 // Send a response from the IPC server to the IPC client. 218 // Send a response from the IPC server to the IPC client.
212 std::string response_data(kLargeMessageSizeBytes * 2, 'Z'); 219 std::string response_data(kLargeMessageSizeBytes * 2, 'Z');
213 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data)); 220 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data));
214 WaitForOperationComplete(); 221 WaitForOperationComplete();
215 222
216 // Verify the request was received. 223 // Verify the request was received.
217 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); 224 ASSERT_EQ(response_data, fake_ipc_client.last_message_received());
218 225
219 // Typically the client will be the one to close the connection. 226 // Typically the client will be the one to close the connection.
220 fake_ipc_client.CloseIpcConnection(); 227 fake_ipc_client.CloseIpcConnection();
221 } 228 }
222 229
223 TEST_F(SecurityKeyIpcServerTest, HandleMultipleSecurityKeyRequests) { 230 TEST_F(SecurityKeyIpcServerTest, HandleMultipleSecurityKeyRequests) {
224 std::string channel_name(GetUniqueTestChannelName()); 231 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
225 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( 232 ASSERT_TRUE(security_key_ipc_server_->CreateChannel(
226 channel_name, 233 channel_handle,
227 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); 234 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500)));
228 235
229 // Create a fake client and connect to the IPC server channel. 236 // Create a fake client and connect to the IPC server channel.
230 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( 237 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind(
231 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); 238 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this)));
232 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); 239 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle));
233 WaitForOperationComplete(); 240 WaitForOperationComplete();
234 241
235 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); 242 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected());
236 243
237 // Send a request from the IPC client to the IPC server. 244 // Send a request from the IPC client to the IPC server.
238 std::string request_data_1("Blergh!"); 245 std::string request_data_1("Blergh!");
239 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data_1); 246 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data_1);
240 WaitForOperationComplete(); 247 WaitForOperationComplete();
241 248
242 // Verify the request was received. 249 // Verify the request was received.
(...skipping 22 matching lines...) Expand all
265 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data_2)); 272 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data_2));
266 WaitForOperationComplete(); 273 WaitForOperationComplete();
267 274
268 // Verify the response was received. 275 // Verify the response was received.
269 ASSERT_EQ(response_data_2, fake_ipc_client.last_message_received()); 276 ASSERT_EQ(response_data_2, fake_ipc_client.last_message_received());
270 277
271 // Typically the client will be the one to close the connection. 278 // Typically the client will be the one to close the connection.
272 fake_ipc_client.CloseIpcConnection(); 279 fake_ipc_client.CloseIpcConnection();
273 } 280 }
274 281
275 TEST_F(SecurityKeyIpcServerTest, InitialIpcConnectionTimeout) { 282 TEST_F(SecurityKeyIpcServerTest, InitialIpcConnectionTimeout_ConnectOnly) {
276 // Create a channel, then wait for the done callback to be called indicating 283 // Create a channel, then wait for the done callback to be called indicating
277 // the connection was closed. This test simulates the IPC Server being 284 // the connection was closed. This test simulates the IPC Server being
278 // created but the client failing to connect to it. 285 // created, the client connecting to the OS channel, but never communicating
279 std::string channel_name(GetUniqueTestChannelName()); 286 // over the channel.
287 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
280 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( 288 ASSERT_TRUE(security_key_ipc_server_->CreateChannel(
281 channel_name, 289 channel_handle,
282 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); 290 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500)));
283 base::Time start_time(base::Time::NowFromSystemTime()); 291 base::Time start_time(base::Time::NowFromSystemTime());
292 mojo::edk::ScopedPlatformHandle client_handle =
293 mojo::edk::CreateClientHandle(channel_handle);
284 WaitForOperationComplete(); 294 WaitForOperationComplete();
285 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; 295 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time;
286 296
297 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs,
298 kConnectionTimeoutErrorDeltaMs);
299 }
300
301 TEST_F(SecurityKeyIpcServerTest,
302 InitialIpcConnectionTimeout_ConnectAndEstablishMojoConnection) {
303 // Create a channel, then wait for the done callback to be called indicating
304 // the connection was closed. This test simulates the IPC Server being
305 // created, the client establishing a mojo connection, but never constructing
306 // an IPC::Channel over it.
307 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
308 ASSERT_TRUE(security_key_ipc_server_->CreateChannel(
309 channel_handle,
310 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500)));
311 base::Time start_time(base::Time::NowFromSystemTime());
312 mojo::ScopedMessagePipeHandle client_pipe = mojo::edk::ConnectToPeerProcess(
313 mojo::edk::CreateClientHandle(channel_handle));
314 WaitForOperationComplete();
315 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time;
316
287 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs, 317 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs,
288 kConnectionTimeoutErrorDeltaMs); 318 kConnectionTimeoutErrorDeltaMs);
289 } 319 }
290 320
291 TEST_F(SecurityKeyIpcServerTest, NoSecurityKeyRequestTimeout) { 321 TEST_F(SecurityKeyIpcServerTest, NoSecurityKeyRequestTimeout) {
292 // Create a channel and connect to it via IPC but do not send a request. 322 // Create a channel and connect to it via IPC but do not send a request.
293 // The channel should be closed and cleaned up if the IPC client does not 323 // The channel should be closed and cleaned up if the IPC client does not
294 // issue a request within the specified timeout period. 324 // issue a request within the specified timeout period.
295 std::string channel_name(GetUniqueTestChannelName()); 325 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
296 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( 326 ASSERT_TRUE(security_key_ipc_server_->CreateChannel(
297 channel_name, 327 channel_handle,
298 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); 328 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500)));
299 329
300 // Create a fake client and connect to the IPC server channel. 330 // Create a fake client and connect to the IPC server channel.
301 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( 331 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind(
302 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); 332 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this)));
303 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); 333 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle));
304 WaitForOperationComplete(); 334 WaitForOperationComplete();
305 335
306 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); 336 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected());
307 337
308 // Now that a connection has been established, we wait for the timeout. 338 // Now that a connection has been established, we wait for the timeout.
309 base::Time start_time(base::Time::NowFromSystemTime()); 339 base::Time start_time(base::Time::NowFromSystemTime());
310 WaitForOperationComplete(); 340 WaitForOperationComplete();
311 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; 341 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time;
312 342
313 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs, 343 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs,
314 kConnectionTimeoutErrorDeltaMs); 344 kConnectionTimeoutErrorDeltaMs);
315 } 345 }
316 346
317 TEST_F(SecurityKeyIpcServerTest, SecurityKeyResponseTimeout) { 347 TEST_F(SecurityKeyIpcServerTest, SecurityKeyResponseTimeout) {
318 // Create a channel, connect to it via IPC, and issue a request, but do 348 // Create a channel, connect to it via IPC, and issue a request, but do
319 // not send a response. This simulates a client-side timeout. 349 // not send a response. This simulates a client-side timeout.
320 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(50)); 350 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(50));
321 std::string channel_name(GetUniqueTestChannelName()); 351 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
322 ASSERT_TRUE( 352 ASSERT_TRUE(
323 security_key_ipc_server_->CreateChannel(channel_name, request_timeout)); 353 security_key_ipc_server_->CreateChannel(channel_handle, request_timeout));
324 354
325 // Create a fake client and connect to the IPC server channel. 355 // Create a fake client and connect to the IPC server channel.
326 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( 356 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind(
327 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); 357 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this)));
328 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); 358 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle));
329 WaitForOperationComplete(); 359 WaitForOperationComplete();
330 360
331 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); 361 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected());
332 362
333 // Now that a connection has been established, we issue a request and 363 // Now that a connection has been established, we issue a request and
334 // then wait for the timeout. 364 // then wait for the timeout.
335 std::string request_data("I can haz Auth?"); 365 std::string request_data("I can haz Auth?");
336 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); 366 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data);
337 WaitForOperationComplete(); 367 WaitForOperationComplete();
338 368
339 // Leave the request hanging until it times out... 369 // Leave the request hanging until it times out...
340 base::Time start_time(base::Time::NowFromSystemTime()); 370 base::Time start_time(base::Time::NowFromSystemTime());
341 WaitForOperationComplete(); 371 WaitForOperationComplete();
342 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; 372 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time;
343 373
344 ASSERT_NEAR(elapsed_time.InMilliseconds(), request_timeout.InMilliseconds(), 374 ASSERT_NEAR(elapsed_time.InMilliseconds(), request_timeout.InMilliseconds(),
345 kConnectionTimeoutErrorDeltaMs); 375 kConnectionTimeoutErrorDeltaMs);
346 } 376 }
347 377
348 TEST_F(SecurityKeyIpcServerTest, SendResponseTimeout) { 378 TEST_F(SecurityKeyIpcServerTest, SendResponseTimeout) {
349 // Create a channel, connect to it via IPC, issue a request, and send 379 // Create a channel, connect to it via IPC, issue a request, and send
350 // a response, but do not close the channel after that. The connection 380 // a response, but do not close the channel after that. The connection
351 // should be terminated after the initial timeout period has elapsed. 381 // should be terminated after the initial timeout period has elapsed.
352 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500)); 382 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500));
353 std::string channel_name(GetUniqueTestChannelName()); 383 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
354 ASSERT_TRUE( 384 ASSERT_TRUE(
355 security_key_ipc_server_->CreateChannel(channel_name, request_timeout)); 385 security_key_ipc_server_->CreateChannel(channel_handle, request_timeout));
356 386
357 // Create a fake client and connect to the IPC server channel. 387 // Create a fake client and connect to the IPC server channel.
358 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( 388 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind(
359 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); 389 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this)));
360 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); 390 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle));
361 WaitForOperationComplete(); 391 WaitForOperationComplete();
362 392
363 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); 393 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected());
364 394
365 // Issue a request. 395 // Issue a request.
366 std::string request_data("Auth me yo!"); 396 std::string request_data("Auth me yo!");
367 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); 397 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data);
368 WaitForOperationComplete(); 398 WaitForOperationComplete();
369 399
370 // Send a response from the IPC server to the IPC client. 400 // Send a response from the IPC server to the IPC client.
371 std::string response_data("OK, the secret code is 1-2-3-4-5"); 401 std::string response_data("OK, the secret code is 1-2-3-4-5");
372 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data)); 402 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data));
373 WaitForOperationComplete(); 403 WaitForOperationComplete();
374 404
375 // Now wait for the timeout period for the connection to be torn down. 405 // Now wait for the timeout period for the connection to be torn down.
376 base::Time start_time(base::Time::NowFromSystemTime()); 406 base::Time start_time(base::Time::NowFromSystemTime());
377 WaitForOperationComplete(); 407 WaitForOperationComplete();
378 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; 408 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time;
379 409
380 ASSERT_NEAR(elapsed_time.InMilliseconds(), request_timeout.InMilliseconds(), 410 ASSERT_NEAR(elapsed_time.InMilliseconds(), request_timeout.InMilliseconds(),
381 kConnectionTimeoutErrorDeltaMs); 411 kConnectionTimeoutErrorDeltaMs);
382 } 412 }
383 413
414 TEST_F(SecurityKeyIpcServerTest, CleanupPendingConnection) {
415 // Test that servers correctly close pending OS connections on
416 // |channel_handle|. If multiple servers do remain, the client may happen to
417 // connect to the correct server, so create and delete many servers.
418 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
419 for (int i = 0; i < 100; i++) {
420 security_key_ipc_server_ = remoting::SecurityKeyIpcServer::Create(
421 kTestConnectionId, this,
422 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs),
423 base::Bind(&SecurityKeyIpcServerTest::SendRequestToClient,
424 base::Unretained(this)),
425 base::Bind(&base::DoNothing),
426 base::Bind(&SecurityKeyIpcServerTest::OperationComplete,
427 base::Unretained(this)));
428 ASSERT_TRUE(security_key_ipc_server_->CreateChannel(
429 channel_handle,
430 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500)));
431 }
432 // The mojo system posts tasks as part of its cleanup, so run them all.
433 base::RunLoop().RunUntilIdle();
434
435 // Create a fake client and connect to the IPC server channel.
436 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind(
437 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this)));
438 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle));
439 WaitForOperationComplete();
440
441 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected());
442
443 // Send a request from the IPC client to the IPC server.
444 std::string request_data("Blergh!");
445 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data);
446 WaitForOperationComplete();
447
448 // Verify the request was received.
449 ASSERT_EQ(kTestConnectionId, last_connection_id_received_);
450 ASSERT_EQ(request_data, last_message_received_);
451
452 // Send a response from the IPC server to the IPC client.
453 std::string response_data("Blargh!");
454 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data));
455 WaitForOperationComplete();
456
457 // Verify the request was received.
458 ASSERT_EQ(response_data, fake_ipc_client.last_message_received());
459
460 // Typically the client will be the one to close the connection.
461 fake_ipc_client.CloseIpcConnection();
462 }
463
384 #if defined(OS_WIN) 464 #if defined(OS_WIN)
385 TEST_F(SecurityKeyIpcServerTest, IpcConnectionFailsFromInvalidSession) { 465 TEST_F(SecurityKeyIpcServerTest, IpcConnectionFailsFromInvalidSession) {
386 // Change the expected session ID to not match the current session. 466 // Change the expected session ID to not match the current session.
387 peer_session_id_++; 467 peer_session_id_++;
388 468
389 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500)); 469 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500));
390 std::string channel_name(GetUniqueTestChannelName()); 470 mojo::edk::NamedPlatformHandle channel_handle(GetUniqueTestChannelName());
391 ASSERT_TRUE( 471 ASSERT_TRUE(
392 security_key_ipc_server_->CreateChannel(channel_name, request_timeout)); 472 security_key_ipc_server_->CreateChannel(channel_handle, request_timeout));
393 473
394 // Create a fake client and attempt to connect to the IPC server channel. 474 // Create a fake client and attempt to connect to the IPC server channel.
395 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( 475 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind(
396 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); 476 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this)));
397 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); 477 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_handle));
398 WaitForOperationComplete(); 478 WaitForOperationComplete();
399 WaitForOperationComplete(); 479 WaitForOperationComplete();
400 WaitForOperationComplete(); 480 WaitForOperationComplete();
401 481
402 // Verify the connection failed. 482 // Verify the connection failed.
403 ASSERT_FALSE(fake_ipc_client.ipc_channel_connected()); 483 ASSERT_FALSE(fake_ipc_client.ipc_channel_connected());
404 } 484 }
405 #endif // defined(OS_WIN) 485 #endif // defined(OS_WIN)
406 486
407 } // namespace remoting 487 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/host/security_key/security_key_ipc_server_impl.cc ('k') | remoting/host/security_key/security_key_message_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698