| 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 "mojo/edk/embedder/named_platform_handle_utils.h" | 15 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
| 16 #include "mojo/edk/embedder/scoped_ipc_support.h" | |
| 17 #include "remoting/host/security_key/fake_security_key_ipc_server.h" | 16 #include "remoting/host/security_key/fake_security_key_ipc_server.h" |
| 18 #include "remoting/host/security_key/security_key_ipc_constants.h" | 17 #include "remoting/host/security_key/security_key_ipc_constants.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 const int kTestConnectionId = 1; | 21 const int kTestConnectionId = 1; |
| 23 const char kNonexistentIpcChannelName[] = "Nonexistent_IPC_Channel"; | 22 const char kNonexistentIpcChannelName[] = "Nonexistent_IPC_Channel"; |
| 24 const char kValidIpcChannelName[] = "SecurityKeyIpcClientTest"; | 23 const char kValidIpcChannelName[] = "SecurityKeyIpcClientTest"; |
| 25 const int kLargeMessageSizeBytes = 256 * 1024; | 24 const int kLargeMessageSizeBytes = 256 * 1024; |
| 26 } // namespace | 25 } // namespace |
| (...skipping 23 matching lines...) Expand all Loading... |
| 50 // back when a security key response is sent. | 49 // back when a security key response is sent. |
| 51 void ClientMessageReceived(const std::string& response_payload); | 50 void ClientMessageReceived(const std::string& response_payload); |
| 52 | 51 |
| 53 protected: | 52 protected: |
| 54 // testing::Test interface. | 53 // testing::Test interface. |
| 55 void SetUp() override; | 54 void SetUp() override; |
| 56 | 55 |
| 57 // Waits until the current |run_loop_| instance is signaled, then resets it. | 56 // Waits until the current |run_loop_| instance is signaled, then resets it. |
| 58 void WaitForOperationComplete(); | 57 void WaitForOperationComplete(); |
| 59 | 58 |
| 59 // Waits until all tasks have been run on the current message loop. |
| 60 void RunPendingTasks(); |
| 61 |
| 60 // Sets up an active IPC connection between |security_key_ipc_client_| | 62 // Sets up an active IPC connection between |security_key_ipc_client_| |
| 61 // and |fake_ipc_server_|. |expect_connected| defines whether the operation | 63 // and |fake_ipc_server_|. |expect_connected| defines whether the operation |
| 62 // is result in a usable IPC connection. |expect_error| defines whether the | 64 // is result in a usable IPC connection. |expect_error| defines whether the |
| 63 // the error callback should be invoked during the connection process. | 65 // the error callback should be invoked during the connection process. |
| 64 void EstablishConnection(bool expect_connected, bool expect_error); | 66 void EstablishConnection(bool expect_connected, bool expect_error); |
| 65 | 67 |
| 66 // Sends a security key request from |security_key_ipc_client_| and | 68 // Sends a security key request from |security_key_ipc_client_| and |
| 67 // a response from |fake_ipc_server_| and verifies the payloads for both. | 69 // a response from |fake_ipc_server_| and verifies the payloads for both. |
| 68 void SendRequestAndResponse(const std::string& request_data, | 70 void SendRequestAndResponse(const std::string& request_data, |
| 69 const std::string& response_data); | 71 const std::string& response_data); |
| 70 | 72 |
| 71 // Creates a unique IPC channel name to use for testing. | 73 // Creates a unique IPC channel name to use for testing. |
| 72 std::string GenerateUniqueTestChannelName(); | 74 std::string GenerateUniqueTestChannelName(); |
| 73 | 75 |
| 74 // IPC tests require a valid MessageLoop to run. | 76 // IPC tests require a valid MessageLoop to run. |
| 75 base::MessageLoopForIO message_loop_; | 77 base::MessageLoopForIO message_loop_; |
| 76 | 78 |
| 77 mojo::edk::ScopedIPCSupport ipc_support_; | |
| 78 | |
| 79 // Used to allow |message_loop_| to run during tests. The instance is reset | 79 // Used to allow |message_loop_| to run during tests. The instance is reset |
| 80 // after each stage of the tests has been completed. | 80 // after each stage of the tests has been completed. |
| 81 std::unique_ptr<base::RunLoop> run_loop_; | 81 std::unique_ptr<base::RunLoop> run_loop_; |
| 82 | 82 |
| 83 // The object under test. | 83 // The object under test. |
| 84 SecurityKeyIpcClient security_key_ipc_client_; | 84 SecurityKeyIpcClient security_key_ipc_client_; |
| 85 | 85 |
| 86 // Used to send/receive security key IPC messages for testing. | 86 // Used to send/receive security key IPC messages for testing. |
| 87 FakeSecurityKeyIpcServer fake_ipc_server_; | 87 FakeSecurityKeyIpcServer fake_ipc_server_; |
| 88 | 88 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 103 int last_connection_id_received_ = -1; | 103 int last_connection_id_received_ = -1; |
| 104 | 104 |
| 105 // Stores the contents of the last IPC message received for validation. | 105 // Stores the contents of the last IPC message received for validation. |
| 106 std::string last_message_received_; | 106 std::string last_message_received_; |
| 107 | 107 |
| 108 private: | 108 private: |
| 109 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcClientTest); | 109 DISALLOW_COPY_AND_ASSIGN(SecurityKeyIpcClientTest); |
| 110 }; | 110 }; |
| 111 | 111 |
| 112 SecurityKeyIpcClientTest::SecurityKeyIpcClientTest() | 112 SecurityKeyIpcClientTest::SecurityKeyIpcClientTest() |
| 113 : ipc_support_(message_loop_.task_runner(), | 113 : run_loop_(new base::RunLoop()), |
| 114 mojo::edk::ScopedIPCSupport::ShutdownPolicy::FAST), | |
| 115 run_loop_(new base::RunLoop()), | |
| 116 fake_ipc_server_( | 114 fake_ipc_server_( |
| 117 kTestConnectionId, | 115 kTestConnectionId, |
| 118 /*client_session_details=*/nullptr, | 116 /*client_session_details=*/nullptr, |
| 119 /*initial_connect_timeout=*/base::TimeDelta::FromMilliseconds(500), | 117 /*initial_connect_timeout=*/base::TimeDelta::FromMilliseconds(500), |
| 120 base::Bind(&SecurityKeyIpcClientTest::SendMessageToClient, | 118 base::Bind(&SecurityKeyIpcClientTest::SendMessageToClient, |
| 121 base::Unretained(this)), | 119 base::Unretained(this)), |
| 122 base::Bind(&SecurityKeyIpcClientTest::SendConnectionMessage, | 120 base::Bind(&SecurityKeyIpcClientTest::SendConnectionMessage, |
| 123 base::Unretained(this)), | 121 base::Unretained(this)), |
| 124 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 122 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
| 125 base::Unretained(this), | 123 base::Unretained(this), |
| (...skipping 28 matching lines...) Expand all Loading... |
| 154 } else { | 152 } else { |
| 155 fake_ipc_server_.SendConnectionReadyMessage(); | 153 fake_ipc_server_.SendConnectionReadyMessage(); |
| 156 } | 154 } |
| 157 } | 155 } |
| 158 | 156 |
| 159 void SecurityKeyIpcClientTest::WaitForOperationComplete() { | 157 void SecurityKeyIpcClientTest::WaitForOperationComplete() { |
| 160 run_loop_->Run(); | 158 run_loop_->Run(); |
| 161 run_loop_.reset(new base::RunLoop()); | 159 run_loop_.reset(new base::RunLoop()); |
| 162 } | 160 } |
| 163 | 161 |
| 162 void SecurityKeyIpcClientTest::RunPendingTasks() { |
| 163 // Run until there are no pending work items in the queue. |
| 164 base::RunLoop().RunUntilIdle(); |
| 165 } |
| 166 |
| 164 void SecurityKeyIpcClientTest::SendMessageToClient(int connection_id, | 167 void SecurityKeyIpcClientTest::SendMessageToClient(int connection_id, |
| 165 const std::string& data) { | 168 const std::string& data) { |
| 166 last_connection_id_received_ = connection_id; | 169 last_connection_id_received_ = connection_id; |
| 167 last_message_received_ = data; | 170 last_message_received_ = data; |
| 168 OperationComplete(/*failed=*/false); | 171 OperationComplete(/*failed=*/false); |
| 169 } | 172 } |
| 170 | 173 |
| 171 void SecurityKeyIpcClientTest::ClientMessageReceived( | 174 void SecurityKeyIpcClientTest::ClientMessageReceived( |
| 172 const std::string& response_payload) { | 175 const std::string& response_payload) { |
| 173 last_message_received_ = response_payload; | 176 last_message_received_ = response_payload; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 193 ASSERT_TRUE(security_key_ipc_client_.CheckForSecurityKeyIpcServerChannel()); | 196 ASSERT_TRUE(security_key_ipc_client_.CheckForSecurityKeyIpcServerChannel()); |
| 194 | 197 |
| 195 // Establish the IPC channel so we can begin sending and receiving security | 198 // Establish the IPC channel so we can begin sending and receiving security |
| 196 // key messages. | 199 // key messages. |
| 197 security_key_ipc_client_.EstablishIpcConnection( | 200 security_key_ipc_client_.EstablishIpcConnection( |
| 198 base::Bind(&SecurityKeyIpcClientTest::ConnectionStateHandler, | 201 base::Bind(&SecurityKeyIpcClientTest::ConnectionStateHandler, |
| 199 base::Unretained(this)), | 202 base::Unretained(this)), |
| 200 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, | 203 base::Bind(&SecurityKeyIpcClientTest::OperationComplete, |
| 201 base::Unretained(this), /*failed=*/true)); | 204 base::Unretained(this), /*failed=*/true)); |
| 202 WaitForOperationComplete(); | 205 WaitForOperationComplete(); |
| 206 RunPendingTasks(); |
| 207 |
| 203 ASSERT_EQ(expect_connected, connection_established_); | 208 ASSERT_EQ(expect_connected, connection_established_); |
| 204 ASSERT_EQ(expect_error, operation_failed_); | 209 ASSERT_EQ(expect_error, operation_failed_); |
| 205 } | 210 } |
| 206 | 211 |
| 207 void SecurityKeyIpcClientTest::SendRequestAndResponse( | 212 void SecurityKeyIpcClientTest::SendRequestAndResponse( |
| 208 const std::string& request_data, | 213 const std::string& request_data, |
| 209 const std::string& response_data) { | 214 const std::string& response_data) { |
| 210 ASSERT_TRUE(security_key_ipc_client_.SendSecurityKeyRequest( | 215 ASSERT_TRUE(security_key_ipc_client_.SendSecurityKeyRequest( |
| 211 request_data, base::Bind(&SecurityKeyIpcClientTest::ClientMessageReceived, | 216 request_data, base::Bind(&SecurityKeyIpcClientTest::ClientMessageReceived, |
| 212 base::Unretained(this)))); | 217 base::Unretained(this)))); |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 ASSERT_TRUE(operation_failed_); | 405 ASSERT_TRUE(operation_failed_); |
| 401 | 406 |
| 402 // Send a third message to ensure no crash occurs both callbacks will have | 407 // Send a third message to ensure no crash occurs both callbacks will have |
| 403 // been called and cleared so we don't wait for the operation to complete. | 408 // been called and cleared so we don't wait for the operation to complete. |
| 404 SendConnectionMessage(); | 409 SendConnectionMessage(); |
| 405 ASSERT_TRUE(operation_failed_); | 410 ASSERT_TRUE(operation_failed_); |
| 406 } | 411 } |
| 407 #endif // defined(OS_WIN) | 412 #endif // defined(OS_WIN) |
| 408 | 413 |
| 409 } // namespace remoting | 414 } // namespace remoting |
| OLD | NEW |