OLD | NEW |
---|---|
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_client.h" | 5 #include "remoting/host/security_key/security_key_ipc_client.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/macros.h" | 11 #include "base/macros.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "ipc/ipc_channel.h" | 14 #include "ipc/ipc_channel.h" |
15 #include "remoting/host/security_key/fake_ipc_security_key_auth_handler.h" | 15 #include "mojo/edk/test/scoped_ipc_support.h" |
16 #include "remoting/host/security_key/fake_security_key_ipc_server.h" | 16 #include "remoting/host/security_key/fake_security_key_ipc_server.h" |
17 #include "remoting/host/security_key/security_key_ipc_constants.h" | 17 #include "remoting/host/security_key/security_key_ipc_constants.h" |
joedow
2016/11/03 22:25:18
Remove this header since |fake_security_key_auth_h
Sam McNally
2016/11/04 02:51:09
I'm not sure what you mean. The fake auth handler
joedow
2016/11/04 15:19:04
I noticed the files hadn't been deleted in that pa
| |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 namespace { | 20 namespace { |
21 const int kTestConnectionId = 1; | 21 const int kTestConnectionId = 1; |
22 const char kNonexistentIpcChannelName[] = "Nonexistent_IPC_Channel"; | 22 const char kNonexistentIpcChannelName[] = "Nonexistent_IPC_Channel"; |
23 const char kValidIpcChannelName[] = "SecurityKeyIpcClientTest"; | 23 const char kValidIpcChannelName[] = "SecurityKeyIpcClientTest"; |
24 const int kLargeMessageSizeBytes = 256 * 1024; | 24 const int kLargeMessageSizeBytes = 256 * 1024; |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 namespace remoting { | 27 namespace remoting { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
59 // a response from |fake_ipc_server_| and verifies the payloads for both. | 59 // a response from |fake_ipc_server_| and verifies the payloads for both. |
60 void SendRequestAndResponse(const std::string& request_data, | 60 void SendRequestAndResponse(const std::string& request_data, |
61 const std::string& response_data); | 61 const std::string& response_data); |
62 | 62 |
63 // Creates a unique IPC channel name to use for testing. | 63 // Creates a unique IPC channel name to use for testing. |
64 std::string GenerateUniqueTestChannelName(); | 64 std::string GenerateUniqueTestChannelName(); |
65 | 65 |
66 // IPC tests require a valid MessageLoop to run. | 66 // IPC tests require a valid MessageLoop to run. |
67 base::MessageLoopForIO message_loop_; | 67 base::MessageLoopForIO message_loop_; |
68 | 68 |
69 mojo::edk::test::ScopedIPCSupport ipc_support_; | |
70 | |
69 // Used to allow |message_loop_| to run during tests. The instance is reset | 71 // Used to allow |message_loop_| to run during tests. The instance is reset |
70 // after each stage of the tests has been completed. | 72 // after each stage of the tests has been completed. |
71 std::unique_ptr<base::RunLoop> run_loop_; | 73 std::unique_ptr<base::RunLoop> run_loop_; |
72 | 74 |
73 // The object under test. | 75 // The object under test. |
74 SecurityKeyIpcClient security_key_ipc_client_; | 76 SecurityKeyIpcClient security_key_ipc_client_; |
75 | 77 |
76 // Provides a connection details message to |security_key_ipc_client_| | |
77 // for testing. | |
78 FakeIpcSecurityKeyAuthHandler fake_security_key_auth_handler_; | |
79 | |
80 // Used to send/receive security key IPC messages for testing. | 78 // Used to send/receive security key IPC messages for testing. |
81 FakeSecurityKeyIpcServer fake_ipc_server_; | 79 FakeSecurityKeyIpcServer fake_ipc_server_; |
82 | 80 |
83 // Stores the current session ID on supported platforms. | 81 // Stores the current session ID on supported platforms. |
84 uint32_t session_id_ = 0; | 82 uint32_t session_id_ = 0; |
85 | 83 |
86 // Tracks the success/failure of the last async operation. | 84 // Tracks the success/failure of the last async operation. |
87 bool operation_failed_ = false; | 85 bool operation_failed_ = false; |
88 | 86 |
89 // Used to validate the object under test uses the correct ID when | 87 // Used to validate the object under test uses the correct ID when |
90 // communicating over the IPC channel. | 88 // communicating over the IPC channel. |
91 int last_connection_id_received_ = -1; | 89 int last_connection_id_received_ = -1; |
92 | 90 |
93 // Stores the contents of the last IPC message received for validation. | 91 // Stores the contents of the last IPC message received for validation. |
94 std::string last_message_received_; | 92 std::string last_message_received_; |
95 | 93 |
96 private: | 94 private: |
97 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcClientTest); | 95 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcClientTest); |
98 }; | 96 }; |
99 | 97 |
100 SecurityKeyIpcClientTest::SecurityKeyIpcClientTest() | 98 SecurityKeyIpcClientTest::SecurityKeyIpcClientTest() |
101 : run_loop_(new base::RunLoop()), | 99 : ipc_support_(message_loop_.task_runner()), |
100 run_loop_(new base::RunLoop()), | |
102 fake_ipc_server_( | 101 fake_ipc_server_( |
103 kTestConnectionId, | 102 kTestConnectionId, |
104 /*client_session_details=*/nullptr, | 103 /*client_session_details=*/nullptr, |
105 /*initial_connect_timeout=*/base::TimeDelta::FromMilliseconds(500), | 104 /*initial_connect_timeout=*/base::TimeDelta::FromMilliseconds(500), |
106 base::Bind(&SecurityKeyIpcClientTest::SendMessageToClient, | 105 base::Bind(&SecurityKeyIpcClientTest::SendMessageToClient, |
107 base::Unretained(this)), | 106 base::Unretained(this)), |
107 base::Bind(&base::DoNothing), | |
108 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 108 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
109 base::Unretained(this), | 109 base::Unretained(this), |
110 /*failed=*/false)) {} | 110 /*failed=*/false)) {} |
111 | 111 |
112 SecurityKeyIpcClientTest::~SecurityKeyIpcClientTest() {} | 112 SecurityKeyIpcClientTest::~SecurityKeyIpcClientTest() {} |
113 | 113 |
114 void SecurityKeyIpcClientTest::SetUp() { | 114 void SecurityKeyIpcClientTest::SetUp() { |
115 #if defined(OS_WIN) | 115 #if defined(OS_WIN) |
116 DWORD session_id = 0; | 116 DWORD session_id = 0; |
117 // If we are on Windows, then we need to set the correct session ID or the | 117 // If we are on Windows, then we need to set the correct session ID or the |
(...skipping 28 matching lines...) Expand all Loading... | |
146 } | 146 } |
147 | 147 |
148 std::string SecurityKeyIpcClientTest::GenerateUniqueTestChannelName() { | 148 std::string SecurityKeyIpcClientTest::GenerateUniqueTestChannelName() { |
149 return GetChannelNamePathPrefixForTest() + kValidIpcChannelName + | 149 return GetChannelNamePathPrefixForTest() + kValidIpcChannelName + |
150 IPC::Channel::GenerateUniqueRandomChannelID(); | 150 IPC::Channel::GenerateUniqueRandomChannelID(); |
151 } | 151 } |
152 | 152 |
153 void SecurityKeyIpcClientTest::EstablishConnection(bool expect_success) { | 153 void SecurityKeyIpcClientTest::EstablishConnection(bool expect_success) { |
154 // Start up the security key forwarding session IPC channel first, that way | 154 // Start up the security key forwarding session IPC channel first, that way |
155 // we can provide the channel using the fake SecurityKeyAuthHandler later on. | 155 // we can provide the channel using the fake SecurityKeyAuthHandler later on. |
156 std::string ipc_session_channel_name = GenerateUniqueTestChannelName(); | 156 mojo::edk::NamedPlatformHandle channel_handle( |
157 GenerateUniqueTestChannelName()); | |
158 security_key_ipc_client_.SetIpcChannelHandleForTest(channel_handle); | |
157 ASSERT_TRUE(fake_ipc_server_.CreateChannel( | 159 ASSERT_TRUE(fake_ipc_server_.CreateChannel( |
158 ipc_session_channel_name, | 160 channel_handle, |
159 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | 161 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
160 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(ipc_session_channel_name)); | |
161 fake_security_key_auth_handler_.set_ipc_security_key_channel_name( | |
162 ipc_session_channel_name); | |
163 | 162 |
164 // Set up the channel name for the initial IPC channel. | |
165 std::string ipc_server_channel_name = GenerateUniqueTestChannelName(); | |
166 fake_security_key_auth_handler_.set_ipc_server_channel_name( | |
167 ipc_server_channel_name); | |
168 security_key_ipc_client_.SetInitialIpcChannelNameForTest( | |
169 ipc_server_channel_name); | |
170 | |
171 // Create the initial IPC channel and verify it was set up correctly. | |
172 ASSERT_FALSE(security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
173 fake_security_key_auth_handler_.CreateSecurityKeyConnection(); | |
174 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(ipc_server_channel_name)); | |
175 ASSERT_TRUE(security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | 163 ASSERT_TRUE(security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); |
176 | 164 |
177 // Establish the IPC channel so we can begin sending and receiving security | 165 // Establish the IPC channel so we can begin sending and receiving security |
178 // key messages. | 166 // key messages. |
179 security_key_ipc_client_.EstablishIpcConnection( | 167 security_key_ipc_client_.EstablishIpcConnection( |
180 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 168 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
181 base::Unretained(this), /*failed=*/false), | 169 base::Unretained(this), /*failed=*/false), |
182 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 170 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
183 base::Unretained(this), /*failed=*/true)); | 171 base::Unretained(this), /*failed=*/true)); |
184 WaitForOperationComplete(); | 172 WaitForOperationComplete(); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
279 | 267 |
280 TEST_F(SecurityKeyIpcClientTest, SendRequestBeforeEstablishingConnection) { | 268 TEST_F(SecurityKeyIpcClientTest, SendRequestBeforeEstablishingConnection) { |
281 // Sending a request will fail since the IPC connection has not been | 269 // Sending a request will fail since the IPC connection has not been |
282 // established. | 270 // established. |
283 ASSERT_FALSE(security_key_ipc_client_.SendSecurityKeyRequest( | 271 ASSERT_FALSE(security_key_ipc_client_.SendSecurityKeyRequest( |
284 "Too soon!!", base::Bind(&SecurityKeyIpcClientTest::ClientMessageReceived, | 272 "Too soon!!", base::Bind(&SecurityKeyIpcClientTest::ClientMessageReceived, |
285 base::Unretained(this)))); | 273 base::Unretained(this)))); |
286 } | 274 } |
287 | 275 |
288 TEST_F(SecurityKeyIpcClientTest, NonExistentMainIpcServerChannel) { | 276 TEST_F(SecurityKeyIpcClientTest, NonExistentMainIpcServerChannel) { |
289 std::string ipc_server_channel_name(kNonexistentIpcChannelName); | 277 security_key_ipc_client_.SetIpcChannelHandleForTest( |
290 security_key_ipc_client_.SetInitialIpcChannelNameForTest( | 278 mojo::edk::NamedPlatformHandle(kNonexistentIpcChannelName)); |
291 ipc_server_channel_name); | |
292 | 279 |
293 // Attempt to establish the conection (should fail since the IPC channel does | 280 // Attempt to establish the conection (should fail since the IPC channel does |
294 // not exist). | 281 // not exist). |
295 security_key_ipc_client_.EstablishIpcConnection( | |
296 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | |
297 base::Unretained(this), /*failed=*/false), | |
298 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | |
299 base::Unretained(this), /*failed=*/true)); | |
300 WaitForOperationComplete(); | |
301 ASSERT_TRUE(operation_failed_); | |
302 } | |
303 | |
304 TEST_F(SecurityKeyIpcClientTest, NonExistentIpcSessionChannel) { | |
305 fake_security_key_auth_handler_.set_ipc_security_key_channel_name( | |
306 kNonexistentIpcChannelName); | |
307 | |
308 // Set up the channel name for the initial IPC channel. | |
309 std::string ipc_server_channel_name = GenerateUniqueTestChannelName(); | |
310 fake_security_key_auth_handler_.set_ipc_server_channel_name( | |
311 ipc_server_channel_name); | |
312 security_key_ipc_client_.SetInitialIpcChannelNameForTest( | |
313 ipc_server_channel_name); | |
314 | |
315 // Create the initial IPC channel and verify it was set up correctly. | |
316 ASSERT_FALSE(security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
317 fake_security_key_auth_handler_.CreateSecurityKeyConnection(); | |
318 ASSERT_TRUE(IPC::Channel::IsNamedServerInitialized(ipc_server_channel_name)); | |
319 ASSERT_TRUE(security_key_ipc_client_.WaitForSecurityKeyIpcServerChannel()); | |
320 | |
321 // Attempt to establish the conection (should fail since the IPC channel does | |
322 // not exist). | |
323 security_key_ipc_client_.EstablishIpcConnection( | 282 security_key_ipc_client_.EstablishIpcConnection( |
324 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 283 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
325 base::Unretained(this), /*failed=*/false), | 284 base::Unretained(this), /*failed=*/false), |
326 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 285 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
327 base::Unretained(this), /*failed=*/true)); | 286 base::Unretained(this), /*failed=*/true)); |
328 WaitForOperationComplete(); | 287 WaitForOperationComplete(); |
329 ASSERT_TRUE(operation_failed_); | 288 ASSERT_TRUE(operation_failed_); |
330 } | 289 } |
331 | 290 |
332 #if defined(OS_WIN) | 291 #if defined(OS_WIN) |
333 TEST_F(SecurityKeyIpcClientTest, SecurityKeyIpcServerRunningInWrongSession) { | 292 TEST_F(SecurityKeyIpcClientTest, SecurityKeyIpcServerRunningInWrongSession) { |
334 // Set the expected session Id to a different session than we are running in. | 293 // Set the expected session Id to a different session than we are running in. |
335 security_key_ipc_client_.SetExpectedIpcServerSessionIdForTest(session_id_ + | 294 security_key_ipc_client_.SetExpectedIpcServerSessionIdForTest(session_id_ + |
336 1); | 295 1); |
337 | 296 |
338 // Attempting to establish a connection should fail here since the IPC Server | 297 // Attempting to establish a connection should fail here since the IPC Server |
339 // is 'running' in a different session than expected. | 298 // is 'running' in a different session than expected. |
340 EstablishConnection(/*expect_success=*/false); | 299 EstablishConnection(/*expect_success=*/false); |
341 } | 300 } |
342 #endif // defined(OS_WIN) | 301 #endif // defined(OS_WIN) |
343 | 302 |
344 } // namespace remoting | 303 } // namespace remoting |
OLD | NEW |