| 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/remote_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 "remoting/host/security_key/fake_remote_security_key_ipc_client.h" | 17 #include "remoting/host/security_key/fake_security_key_ipc_client.h" |
| 18 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" | 18 #include "remoting/host/security_key/security_key_ipc_constants.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 const int kTestConnectionId = 42; | 22 const int kTestConnectionId = 42; |
| 23 const int kInitialConnectTimeoutMs = 250; | 23 const int kInitialConnectTimeoutMs = 250; |
| 24 const int kConnectionTimeoutErrorDeltaMs = 100; | 24 const int kConnectionTimeoutErrorDeltaMs = 100; |
| 25 const int kLargeMessageSizeBytes = 256 * 1024; | 25 const int kLargeMessageSizeBytes = 256 * 1024; |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 namespace remoting { | 28 namespace remoting { |
| 29 | 29 |
| 30 class RemoteSecurityKeyIpcServerTest : public testing::Test { | 30 class SecurityKeyIpcServerTest : public testing::Test { |
| 31 public: | 31 public: |
| 32 RemoteSecurityKeyIpcServerTest(); | 32 SecurityKeyIpcServerTest(); |
| 33 ~RemoteSecurityKeyIpcServerTest() override; | 33 ~SecurityKeyIpcServerTest() override; |
| 34 | 34 |
| 35 // Passed to the object used for testing to be called back to signal | 35 // Passed to the object used for testing to be called back to signal |
| 36 // completion of an IPC channel state change or reception of an IPC message. | 36 // completion of an IPC channel state change or reception of an IPC message. |
| 37 void OperationComplete(); | 37 void OperationComplete(); |
| 38 | 38 |
| 39 // Used as a callback to signal receipt of a security key request message. | 39 // Used as a callback to signal receipt of a security key request message. |
| 40 void SendRequestToClient(int connection_id, const std::string& data); | 40 void SendRequestToClient(int connection_id, const std::string& data); |
| 41 | 41 |
| 42 protected: | 42 protected: |
| 43 // Returns a unique IPC channel name which prevents conflicts when running | 43 // Returns a unique IPC channel name which prevents conflicts when running |
| 44 // tests concurrently. | 44 // tests concurrently. |
| 45 std::string GetUniqueTestChannelName(); | 45 std::string GetUniqueTestChannelName(); |
| 46 | 46 |
| 47 // Waits until the current |run_loop_| instance is signaled, then resets it. | 47 // Waits until the current |run_loop_| instance is signaled, then resets it. |
| 48 void WaitForOperationComplete(); | 48 void WaitForOperationComplete(); |
| 49 | 49 |
| 50 // IPC tests require a valid MessageLoop to run. | 50 // IPC tests require a valid MessageLoop to run. |
| 51 base::MessageLoopForIO message_loop_; | 51 base::MessageLoopForIO message_loop_; |
| 52 | 52 |
| 53 // Used to allow |message_loop_| to run during tests. The instance is reset | 53 // Used to allow |message_loop_| to run during tests. The instance is reset |
| 54 // after each stage of the tests has been completed. | 54 // after each stage of the tests has been completed. |
| 55 std::unique_ptr<base::RunLoop> run_loop_; | 55 std::unique_ptr<base::RunLoop> run_loop_; |
| 56 | 56 |
| 57 // The object under test. | 57 // The object under test. |
| 58 std::unique_ptr<RemoteSecurityKeyIpcServer> remote_security_key_ipc_server_; | 58 std::unique_ptr<SecurityKeyIpcServer> security_key_ipc_server_; |
| 59 | 59 |
| 60 // Used to validate the object under test uses the correct ID when | 60 // Used to validate the object under test uses the correct ID when |
| 61 // communicating over the IPC channel. | 61 // communicating over the IPC channel. |
| 62 int last_connection_id_received_ = -1; | 62 int last_connection_id_received_ = -1; |
| 63 | 63 |
| 64 // Stores the contents of the last IPC message received for validation. | 64 // Stores the contents of the last IPC message received for validation. |
| 65 std::string last_message_received_; | 65 std::string last_message_received_; |
| 66 | 66 |
| 67 private: | 67 private: |
| 68 DISALLOW_COPY_AND_ASSIGN(RemoteSecurityKeyIpcServerTest); | 68 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcServerTest); |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 RemoteSecurityKeyIpcServerTest::RemoteSecurityKeyIpcServerTest() | 71 SecurityKeyIpcServerTest::SecurityKeyIpcServerTest() |
| 72 : run_loop_(new base::RunLoop()) { | 72 : run_loop_(new base::RunLoop()) { |
| 73 uint32_t peer_session_id = UINT32_MAX; | 73 uint32_t peer_session_id = UINT32_MAX; |
| 74 #if defined(OS_WIN) | 74 #if defined(OS_WIN) |
| 75 EXPECT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), | 75 EXPECT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), |
| 76 reinterpret_cast<DWORD*>(&peer_session_id))); | 76 reinterpret_cast<DWORD*>(&peer_session_id))); |
| 77 #endif // defined(OS_WIN) | 77 #endif // defined(OS_WIN) |
| 78 | 78 |
| 79 remote_security_key_ipc_server_ = | 79 security_key_ipc_server_ = remoting::SecurityKeyIpcServer::Create( |
| 80 remoting::RemoteSecurityKeyIpcServer::Create( | 80 kTestConnectionId, peer_session_id, |
| 81 kTestConnectionId, peer_session_id, | 81 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs), |
| 82 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs), | 82 base::Bind(&SecurityKeyIpcServerTest::SendRequestToClient, |
| 83 base::Bind(&RemoteSecurityKeyIpcServerTest::SendRequestToClient, | 83 base::Unretained(this)), |
| 84 base::Unretained(this)), | 84 base::Bind(&SecurityKeyIpcServerTest::OperationComplete, |
| 85 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, | 85 base::Unretained(this))); |
| 86 base::Unretained(this))); | |
| 87 } | 86 } |
| 88 | 87 |
| 89 RemoteSecurityKeyIpcServerTest::~RemoteSecurityKeyIpcServerTest() {} | 88 SecurityKeyIpcServerTest::~SecurityKeyIpcServerTest() {} |
| 90 | 89 |
| 91 void RemoteSecurityKeyIpcServerTest::OperationComplete() { | 90 void SecurityKeyIpcServerTest::OperationComplete() { |
| 92 run_loop_->Quit(); | 91 run_loop_->Quit(); |
| 93 } | 92 } |
| 94 | 93 |
| 95 void RemoteSecurityKeyIpcServerTest::WaitForOperationComplete() { | 94 void SecurityKeyIpcServerTest::WaitForOperationComplete() { |
| 96 run_loop_->Run(); | 95 run_loop_->Run(); |
| 97 run_loop_.reset(new base::RunLoop()); | 96 run_loop_.reset(new base::RunLoop()); |
| 98 } | 97 } |
| 99 | 98 |
| 100 void RemoteSecurityKeyIpcServerTest::SendRequestToClient( | 99 void SecurityKeyIpcServerTest::SendRequestToClient(int connection_id, |
| 101 int connection_id, | 100 const std::string& data) { |
| 102 const std::string& data) { | |
| 103 last_connection_id_received_ = connection_id; | 101 last_connection_id_received_ = connection_id; |
| 104 last_message_received_ = data; | 102 last_message_received_ = data; |
| 105 OperationComplete(); | 103 OperationComplete(); |
| 106 } | 104 } |
| 107 | 105 |
| 108 std::string RemoteSecurityKeyIpcServerTest::GetUniqueTestChannelName() { | 106 std::string SecurityKeyIpcServerTest::GetUniqueTestChannelName() { |
| 109 return GetChannelNamePathPrefixForTest() + "Super_Awesome_Test_Channel." + | 107 return GetChannelNamePathPrefixForTest() + "Super_Awesome_Test_Channel." + |
| 110 IPC::Channel::GenerateUniqueRandomChannelID(); | 108 IPC::Channel::GenerateUniqueRandomChannelID(); |
| 111 } | 109 } |
| 112 | 110 |
| 113 TEST_F(RemoteSecurityKeyIpcServerTest, HandleSingleGnubbyRequest) { | 111 TEST_F(SecurityKeyIpcServerTest, HandleSingleSecurityKeyRequest) { |
| 114 std::string channel_name(GetUniqueTestChannelName()); | 112 std::string channel_name(GetUniqueTestChannelName()); |
| 115 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( | 113 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( |
| 116 channel_name, | 114 channel_name, |
| 117 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | 115 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 118 | 116 |
| 119 // Create a fake client and connect to the IPC server channel. | 117 // Create a fake client and connect to the IPC server channel. |
| 120 FakeRemoteSecurityKeyIpcClient fake_ipc_client( | 118 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 121 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, | 119 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); |
| 122 base::Unretained(this))); | |
| 123 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); | 120 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 124 WaitForOperationComplete(); | 121 WaitForOperationComplete(); |
| 125 | 122 |
| 126 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); | 123 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 127 | 124 |
| 128 // Send a request from the IPC client to the IPC server. | 125 // Send a request from the IPC client to the IPC server. |
| 129 std::string request_data("Blergh!"); | 126 std::string request_data("Blergh!"); |
| 130 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); | 127 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 131 WaitForOperationComplete(); | 128 WaitForOperationComplete(); |
| 132 | 129 |
| 133 // Verify the request was received. | 130 // Verify the request was received. |
| 134 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); | 131 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 135 ASSERT_EQ(request_data, last_message_received_); | 132 ASSERT_EQ(request_data, last_message_received_); |
| 136 | 133 |
| 137 // Send a response from the IPC server to the IPC client. | 134 // Send a response from the IPC server to the IPC client. |
| 138 std::string response_data("Blargh!"); | 135 std::string response_data("Blargh!"); |
| 139 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data)); | 136 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data)); |
| 140 WaitForOperationComplete(); | 137 WaitForOperationComplete(); |
| 141 | 138 |
| 142 // Verify the request was received. | 139 // Verify the request was received. |
| 143 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); | 140 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); |
| 144 | 141 |
| 145 // Typically the client will be the one to close the connection. | 142 // Typically the client will be the one to close the connection. |
| 146 fake_ipc_client.CloseIpcConnection(); | 143 fake_ipc_client.CloseIpcConnection(); |
| 147 } | 144 } |
| 148 | 145 |
| 149 TEST_F(RemoteSecurityKeyIpcServerTest, HandleLargeGnubbyRequest) { | 146 TEST_F(SecurityKeyIpcServerTest, HandleLargeSecurityKeyRequest) { |
| 150 std::string channel_name(GetUniqueTestChannelName()); | 147 std::string channel_name(GetUniqueTestChannelName()); |
| 151 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( | 148 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( |
| 152 channel_name, | 149 channel_name, |
| 153 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | 150 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 154 | 151 |
| 155 // Create a fake client and connect to the IPC server channel. | 152 // Create a fake client and connect to the IPC server channel. |
| 156 FakeRemoteSecurityKeyIpcClient fake_ipc_client( | 153 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 157 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, | 154 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); |
| 158 base::Unretained(this))); | |
| 159 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); | 155 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 160 WaitForOperationComplete(); | 156 WaitForOperationComplete(); |
| 161 | 157 |
| 162 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); | 158 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 163 | 159 |
| 164 // Send a request from the IPC client to the IPC server. | 160 // Send a request from the IPC client to the IPC server. |
| 165 std::string request_data(kLargeMessageSizeBytes, 'Y'); | 161 std::string request_data(kLargeMessageSizeBytes, 'Y'); |
| 166 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); | 162 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 167 WaitForOperationComplete(); | 163 WaitForOperationComplete(); |
| 168 | 164 |
| 169 // Verify the request was received. | 165 // Verify the request was received. |
| 170 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); | 166 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 171 ASSERT_EQ(request_data, last_message_received_); | 167 ASSERT_EQ(request_data, last_message_received_); |
| 172 | 168 |
| 173 // Send a response from the IPC server to the IPC client. | 169 // Send a response from the IPC server to the IPC client. |
| 174 std::string response_data(kLargeMessageSizeBytes, 'Z'); | 170 std::string response_data(kLargeMessageSizeBytes, 'Z'); |
| 175 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data)); | 171 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data)); |
| 176 WaitForOperationComplete(); | 172 WaitForOperationComplete(); |
| 177 | 173 |
| 178 // Verify the request was received. | 174 // Verify the request was received. |
| 179 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); | 175 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); |
| 180 | 176 |
| 181 // Typically the client will be the one to close the connection. | 177 // Typically the client will be the one to close the connection. |
| 182 fake_ipc_client.CloseIpcConnection(); | 178 fake_ipc_client.CloseIpcConnection(); |
| 183 } | 179 } |
| 184 | 180 |
| 185 TEST_F(RemoteSecurityKeyIpcServerTest, HandleReallyLargeGnubbyRequest) { | 181 TEST_F(SecurityKeyIpcServerTest, HandleReallyLargeSecurityKeyRequest) { |
| 186 std::string channel_name(GetUniqueTestChannelName()); | 182 std::string channel_name(GetUniqueTestChannelName()); |
| 187 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( | 183 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( |
| 188 channel_name, | 184 channel_name, |
| 189 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | 185 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 190 | 186 |
| 191 // Create a fake client and connect to the IPC server channel. | 187 // Create a fake client and connect to the IPC server channel. |
| 192 FakeRemoteSecurityKeyIpcClient fake_ipc_client( | 188 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 193 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, | 189 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); |
| 194 base::Unretained(this))); | |
| 195 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); | 190 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 196 WaitForOperationComplete(); | 191 WaitForOperationComplete(); |
| 197 | 192 |
| 198 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); | 193 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 199 | 194 |
| 200 // Send a request from the IPC client to the IPC server. | 195 // Send a request from the IPC client to the IPC server. |
| 201 std::string request_data(kLargeMessageSizeBytes * 2, 'Y'); | 196 std::string request_data(kLargeMessageSizeBytes * 2, 'Y'); |
| 202 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); | 197 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 203 WaitForOperationComplete(); | 198 WaitForOperationComplete(); |
| 204 | 199 |
| 205 // Verify the request was received. | 200 // Verify the request was received. |
| 206 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); | 201 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 207 ASSERT_EQ(request_data, last_message_received_); | 202 ASSERT_EQ(request_data, last_message_received_); |
| 208 | 203 |
| 209 // Send a response from the IPC server to the IPC client. | 204 // Send a response from the IPC server to the IPC client. |
| 210 std::string response_data(kLargeMessageSizeBytes * 2, 'Z'); | 205 std::string response_data(kLargeMessageSizeBytes * 2, 'Z'); |
| 211 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data)); | 206 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data)); |
| 212 WaitForOperationComplete(); | 207 WaitForOperationComplete(); |
| 213 | 208 |
| 214 // Verify the request was received. | 209 // Verify the request was received. |
| 215 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); | 210 ASSERT_EQ(response_data, fake_ipc_client.last_message_received()); |
| 216 | 211 |
| 217 // Typically the client will be the one to close the connection. | 212 // Typically the client will be the one to close the connection. |
| 218 fake_ipc_client.CloseIpcConnection(); | 213 fake_ipc_client.CloseIpcConnection(); |
| 219 } | 214 } |
| 220 | 215 |
| 221 TEST_F(RemoteSecurityKeyIpcServerTest, HandleMultipleGnubbyRequests) { | 216 TEST_F(SecurityKeyIpcServerTest, HandleMultipleSecurityKeyRequests) { |
| 222 std::string channel_name(GetUniqueTestChannelName()); | 217 std::string channel_name(GetUniqueTestChannelName()); |
| 223 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( | 218 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( |
| 224 channel_name, | 219 channel_name, |
| 225 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | 220 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 226 | 221 |
| 227 // Create a fake client and connect to the IPC server channel. | 222 // Create a fake client and connect to the IPC server channel. |
| 228 FakeRemoteSecurityKeyIpcClient fake_ipc_client( | 223 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 229 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, | 224 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); |
| 230 base::Unretained(this))); | |
| 231 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); | 225 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 232 WaitForOperationComplete(); | 226 WaitForOperationComplete(); |
| 233 | 227 |
| 234 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); | 228 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 235 | 229 |
| 236 // Send a request from the IPC client to the IPC server. | 230 // Send a request from the IPC client to the IPC server. |
| 237 std::string request_data_1("Blergh!"); | 231 std::string request_data_1("Blergh!"); |
| 238 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data_1); | 232 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data_1); |
| 239 WaitForOperationComplete(); | 233 WaitForOperationComplete(); |
| 240 | 234 |
| 241 // Verify the request was received. | 235 // Verify the request was received. |
| 242 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); | 236 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 243 ASSERT_EQ(request_data_1, last_message_received_); | 237 ASSERT_EQ(request_data_1, last_message_received_); |
| 244 | 238 |
| 245 // Send a response from the IPC server to the IPC client. | 239 // Send a response from the IPC server to the IPC client. |
| 246 std::string response_data_1("Blargh!"); | 240 std::string response_data_1("Blargh!"); |
| 247 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data_1)); | 241 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data_1)); |
| 248 WaitForOperationComplete(); | 242 WaitForOperationComplete(); |
| 249 | 243 |
| 250 // Verify the response was received. | 244 // Verify the response was received. |
| 251 ASSERT_EQ(response_data_1, fake_ipc_client.last_message_received()); | 245 ASSERT_EQ(response_data_1, fake_ipc_client.last_message_received()); |
| 252 | 246 |
| 253 // Send a request from the IPC client to the IPC server. | 247 // Send a request from the IPC client to the IPC server. |
| 254 std::string request_data_2("Bleh!"); | 248 std::string request_data_2("Bleh!"); |
| 255 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data_2); | 249 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data_2); |
| 256 WaitForOperationComplete(); | 250 WaitForOperationComplete(); |
| 257 | 251 |
| 258 // Verify the request was received. | 252 // Verify the request was received. |
| 259 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); | 253 ASSERT_EQ(kTestConnectionId, last_connection_id_received_); |
| 260 ASSERT_EQ(request_data_2, last_message_received_); | 254 ASSERT_EQ(request_data_2, last_message_received_); |
| 261 | 255 |
| 262 // Send a response from the IPC server to the IPC client. | 256 // Send a response from the IPC server to the IPC client. |
| 263 std::string response_data_2("Meh!"); | 257 std::string response_data_2("Meh!"); |
| 264 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data_2)); | 258 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data_2)); |
| 265 WaitForOperationComplete(); | 259 WaitForOperationComplete(); |
| 266 | 260 |
| 267 // Verify the response was received. | 261 // Verify the response was received. |
| 268 ASSERT_EQ(response_data_2, fake_ipc_client.last_message_received()); | 262 ASSERT_EQ(response_data_2, fake_ipc_client.last_message_received()); |
| 269 | 263 |
| 270 // Typically the client will be the one to close the connection. | 264 // Typically the client will be the one to close the connection. |
| 271 fake_ipc_client.CloseIpcConnection(); | 265 fake_ipc_client.CloseIpcConnection(); |
| 272 } | 266 } |
| 273 | 267 |
| 274 TEST_F(RemoteSecurityKeyIpcServerTest, InitialIpcConnectionTimeout) { | 268 TEST_F(SecurityKeyIpcServerTest, InitialIpcConnectionTimeout) { |
| 275 // Create a channel, then wait for the done callback to be called indicating | 269 // Create a channel, then wait for the done callback to be called indicating |
| 276 // the connection was closed. This test simulates the IPC Server being | 270 // the connection was closed. This test simulates the IPC Server being |
| 277 // created but the client failing to connect to it. | 271 // created but the client failing to connect to it. |
| 278 std::string channel_name(GetUniqueTestChannelName()); | 272 std::string channel_name(GetUniqueTestChannelName()); |
| 279 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( | 273 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( |
| 280 channel_name, | 274 channel_name, |
| 281 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | 275 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 282 base::Time start_time(base::Time::NowFromSystemTime()); | 276 base::Time start_time(base::Time::NowFromSystemTime()); |
| 283 WaitForOperationComplete(); | 277 WaitForOperationComplete(); |
| 284 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; | 278 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; |
| 285 | 279 |
| 286 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs, | 280 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs, |
| 287 kConnectionTimeoutErrorDeltaMs); | 281 kConnectionTimeoutErrorDeltaMs); |
| 288 } | 282 } |
| 289 | 283 |
| 290 TEST_F(RemoteSecurityKeyIpcServerTest, NoGnubbyRequestTimeout) { | 284 TEST_F(SecurityKeyIpcServerTest, NoSecurityKeyRequestTimeout) { |
| 291 // Create a channel and connect to it via IPC but do not send a request. | 285 // Create a channel and connect to it via IPC but do not send a request. |
| 292 // The channel should be closed and cleaned up if the IPC client does not | 286 // The channel should be closed and cleaned up if the IPC client does not |
| 293 // issue a request within the specified timeout period. | 287 // issue a request within the specified timeout period. |
| 294 std::string channel_name(GetUniqueTestChannelName()); | 288 std::string channel_name(GetUniqueTestChannelName()); |
| 295 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( | 289 ASSERT_TRUE(security_key_ipc_server_->CreateChannel( |
| 296 channel_name, | 290 channel_name, |
| 297 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); | 291 /*request_timeout=*/base::TimeDelta::FromMilliseconds(500))); |
| 298 | 292 |
| 299 // Create a fake client and connect to the IPC server channel. | 293 // Create a fake client and connect to the IPC server channel. |
| 300 FakeRemoteSecurityKeyIpcClient fake_ipc_client( | 294 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 301 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, | 295 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); |
| 302 base::Unretained(this))); | |
| 303 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); | 296 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 304 WaitForOperationComplete(); | 297 WaitForOperationComplete(); |
| 305 | 298 |
| 306 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); | 299 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 307 | 300 |
| 308 // Now that a connection has been established, we wait for the timeout. | 301 // Now that a connection has been established, we wait for the timeout. |
| 309 base::Time start_time(base::Time::NowFromSystemTime()); | 302 base::Time start_time(base::Time::NowFromSystemTime()); |
| 310 WaitForOperationComplete(); | 303 WaitForOperationComplete(); |
| 311 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; | 304 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; |
| 312 | 305 |
| 313 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs, | 306 ASSERT_NEAR(elapsed_time.InMilliseconds(), kInitialConnectTimeoutMs, |
| 314 kConnectionTimeoutErrorDeltaMs); | 307 kConnectionTimeoutErrorDeltaMs); |
| 315 } | 308 } |
| 316 | 309 |
| 317 TEST_F(RemoteSecurityKeyIpcServerTest, GnubbyResponseTimeout) { | 310 TEST_F(SecurityKeyIpcServerTest, SecurityKeyResponseTimeout) { |
| 318 // Create a channel, connect to it via IPC, and issue a request, but do | 311 // 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. | 312 // not send a response. This simulates a client-side timeout. |
| 320 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(50)); | 313 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(50)); |
| 321 std::string channel_name(GetUniqueTestChannelName()); | 314 std::string channel_name(GetUniqueTestChannelName()); |
| 322 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel(channel_name, | 315 ASSERT_TRUE( |
| 323 request_timeout)); | 316 security_key_ipc_server_->CreateChannel(channel_name, request_timeout)); |
| 324 | 317 |
| 325 // Create a fake client and connect to the IPC server channel. | 318 // Create a fake client and connect to the IPC server channel. |
| 326 FakeRemoteSecurityKeyIpcClient fake_ipc_client( | 319 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 327 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, | 320 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); |
| 328 base::Unretained(this))); | |
| 329 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); | 321 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 330 WaitForOperationComplete(); | 322 WaitForOperationComplete(); |
| 331 | 323 |
| 332 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); | 324 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 333 | 325 |
| 334 // Now that a connection has been established, we issue a request and | 326 // Now that a connection has been established, we issue a request and |
| 335 // then wait for the timeout. | 327 // then wait for the timeout. |
| 336 std::string request_data("I can haz Auth?"); | 328 std::string request_data("I can haz Auth?"); |
| 337 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); | 329 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 338 WaitForOperationComplete(); | 330 WaitForOperationComplete(); |
| 339 | 331 |
| 340 // Leave the request hanging until it times out... | 332 // Leave the request hanging until it times out... |
| 341 base::Time start_time(base::Time::NowFromSystemTime()); | 333 base::Time start_time(base::Time::NowFromSystemTime()); |
| 342 WaitForOperationComplete(); | 334 WaitForOperationComplete(); |
| 343 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; | 335 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; |
| 344 | 336 |
| 345 ASSERT_NEAR(elapsed_time.InMilliseconds(), request_timeout.InMilliseconds(), | 337 ASSERT_NEAR(elapsed_time.InMilliseconds(), request_timeout.InMilliseconds(), |
| 346 kConnectionTimeoutErrorDeltaMs); | 338 kConnectionTimeoutErrorDeltaMs); |
| 347 } | 339 } |
| 348 | 340 |
| 349 TEST_F(RemoteSecurityKeyIpcServerTest, SendResponseTimeout) { | 341 TEST_F(SecurityKeyIpcServerTest, SendResponseTimeout) { |
| 350 // Create a channel, connect to it via IPC, issue a request, and send | 342 // Create a channel, connect to it via IPC, issue a request, and send |
| 351 // a response, but do not close the channel after that. The connection | 343 // a response, but do not close the channel after that. The connection |
| 352 // should be terminated after the initial timeout period has elapsed. | 344 // should be terminated after the initial timeout period has elapsed. |
| 353 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500)); | 345 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500)); |
| 354 std::string channel_name(GetUniqueTestChannelName()); | 346 std::string channel_name(GetUniqueTestChannelName()); |
| 355 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel( | 347 ASSERT_TRUE( |
| 356 channel_name, request_timeout)); | 348 security_key_ipc_server_->CreateChannel(channel_name, request_timeout)); |
| 357 | 349 |
| 358 // Create a fake client and connect to the IPC server channel. | 350 // Create a fake client and connect to the IPC server channel. |
| 359 FakeRemoteSecurityKeyIpcClient fake_ipc_client( | 351 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 360 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, | 352 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); |
| 361 base::Unretained(this))); | |
| 362 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); | 353 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 363 WaitForOperationComplete(); | 354 WaitForOperationComplete(); |
| 364 | 355 |
| 365 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); | 356 ASSERT_TRUE(fake_ipc_client.ipc_channel_connected()); |
| 366 | 357 |
| 367 // Issue a request. | 358 // Issue a request. |
| 368 std::string request_data("Auth me yo!"); | 359 std::string request_data("Auth me yo!"); |
| 369 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); | 360 fake_ipc_client.SendSecurityKeyRequestViaIpc(request_data); |
| 370 WaitForOperationComplete(); | 361 WaitForOperationComplete(); |
| 371 | 362 |
| 372 // Send a response from the IPC server to the IPC client. | 363 // Send a response from the IPC server to the IPC client. |
| 373 std::string response_data("OK, the secret code is 1-2-3-4-5"); | 364 std::string response_data("OK, the secret code is 1-2-3-4-5"); |
| 374 ASSERT_TRUE(remote_security_key_ipc_server_->SendResponse(response_data)); | 365 ASSERT_TRUE(security_key_ipc_server_->SendResponse(response_data)); |
| 375 WaitForOperationComplete(); | 366 WaitForOperationComplete(); |
| 376 | 367 |
| 377 // Now wait for the timeout period for the connection to be torn down. | 368 // Now wait for the timeout period for the connection to be torn down. |
| 378 base::Time start_time(base::Time::NowFromSystemTime()); | 369 base::Time start_time(base::Time::NowFromSystemTime()); |
| 379 WaitForOperationComplete(); | 370 WaitForOperationComplete(); |
| 380 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; | 371 base::TimeDelta elapsed_time = base::Time::NowFromSystemTime() - start_time; |
| 381 | 372 |
| 382 ASSERT_NEAR(elapsed_time.InMilliseconds(), request_timeout.InMilliseconds(), | 373 ASSERT_NEAR(elapsed_time.InMilliseconds(), request_timeout.InMilliseconds(), |
| 383 kConnectionTimeoutErrorDeltaMs); | 374 kConnectionTimeoutErrorDeltaMs); |
| 384 } | 375 } |
| 385 | 376 |
| 386 #if defined(OS_WIN) | 377 #if defined(OS_WIN) |
| 387 TEST_F(RemoteSecurityKeyIpcServerTest, IpcConnectionFailsFromInvalidSession) { | 378 TEST_F(SecurityKeyIpcServerTest, IpcConnectionFailsFromInvalidSession) { |
| 388 uint32_t peer_session_id = UINT32_MAX; | 379 uint32_t peer_session_id = UINT32_MAX; |
| 389 ASSERT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), | 380 ASSERT_TRUE(ProcessIdToSessionId(GetCurrentProcessId(), |
| 390 reinterpret_cast<DWORD*>(&peer_session_id))); | 381 reinterpret_cast<DWORD*>(&peer_session_id))); |
| 391 peer_session_id++; | 382 peer_session_id++; |
| 392 | 383 |
| 393 // Reinitialize the object under test. | 384 // Reinitialize the object under test. |
| 394 remote_security_key_ipc_server_ = | 385 security_key_ipc_server_ = remoting::SecurityKeyIpcServer::Create( |
| 395 remoting::RemoteSecurityKeyIpcServer::Create( | 386 kTestConnectionId, peer_session_id, |
| 396 kTestConnectionId, peer_session_id, | 387 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs), |
| 397 base::TimeDelta::FromMilliseconds(kInitialConnectTimeoutMs), | 388 base::Bind(&SecurityKeyIpcServerTest::SendRequestToClient, |
| 398 base::Bind(&RemoteSecurityKeyIpcServerTest::SendRequestToClient, | 389 base::Unretained(this)), |
| 399 base::Unretained(this)), | 390 base::Bind(&base::DoNothing)); |
| 400 base::Bind(&base::DoNothing)); | |
| 401 | 391 |
| 402 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500)); | 392 base::TimeDelta request_timeout(base::TimeDelta::FromMilliseconds(500)); |
| 403 std::string channel_name(GetUniqueTestChannelName()); | 393 std::string channel_name(GetUniqueTestChannelName()); |
| 404 ASSERT_TRUE(remote_security_key_ipc_server_->CreateChannel(channel_name, | 394 ASSERT_TRUE( |
| 405 request_timeout)); | 395 security_key_ipc_server_->CreateChannel(channel_name, request_timeout)); |
| 406 | 396 |
| 407 // Create a fake client and attempt to connect to the IPC server channel. | 397 // Create a fake client and attempt to connect to the IPC server channel. |
| 408 FakeRemoteSecurityKeyIpcClient fake_ipc_client( | 398 FakeSecurityKeyIpcClient fake_ipc_client(base::Bind( |
| 409 base::Bind(&RemoteSecurityKeyIpcServerTest::OperationComplete, | 399 &SecurityKeyIpcServerTest::OperationComplete, base::Unretained(this))); |
| 410 base::Unretained(this))); | |
| 411 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); | 400 ASSERT_TRUE(fake_ipc_client.ConnectViaIpc(channel_name)); |
| 412 WaitForOperationComplete(); | 401 WaitForOperationComplete(); |
| 413 WaitForOperationComplete(); | 402 WaitForOperationComplete(); |
| 414 | 403 |
| 415 // Verify the connection failed. | 404 // Verify the connection failed. |
| 416 ASSERT_FALSE(fake_ipc_client.ipc_channel_connected()); | 405 ASSERT_FALSE(fake_ipc_client.ipc_channel_connected()); |
| 417 } | 406 } |
| 418 #endif // defined(OS_WIN) | 407 #endif // defined(OS_WIN) |
| 419 | 408 |
| 420 } // namespace remoting | 409 } // namespace remoting |
| OLD | NEW |