OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "remoting/host/security_key/fake_remote_security_key_ipc_server.h" |
| 6 |
| 7 #include <cstdint> |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
| 14 #include "ipc/ipc_channel.h" |
| 15 #include "ipc/ipc_message.h" |
| 16 #include "ipc/ipc_message_macros.h" |
| 17 #include "remoting/host/chromoting_messages.h" |
| 18 #include "remoting/host/security_key/gnubby_auth_handler.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 |
| 21 namespace remoting { |
| 22 |
| 23 FakeRemoteSecurityKeyIpcServer::FakeRemoteSecurityKeyIpcServer( |
| 24 int connection_id, |
| 25 uint32_t peer_session_id, |
| 26 base::TimeDelta initial_connect_timeout, |
| 27 const GnubbyAuthHandler::SendMessageCallback& send_message_callback, |
| 28 const base::Closure& channel_closed_callback) |
| 29 : connection_id_(connection_id), |
| 30 send_message_callback_(send_message_callback), |
| 31 channel_closed_callback_(channel_closed_callback), |
| 32 weak_factory_(this) {} |
| 33 |
| 34 FakeRemoteSecurityKeyIpcServer::~FakeRemoteSecurityKeyIpcServer() {} |
| 35 |
| 36 void FakeRemoteSecurityKeyIpcServer::SendRequest( |
| 37 const std::string& message_data) { |
| 38 send_message_callback_.Run(connection_id_, message_data); |
| 39 } |
| 40 |
| 41 void FakeRemoteSecurityKeyIpcServer::CloseChannel() { |
| 42 ipc_channel_.reset(); |
| 43 channel_closed_callback_.Run(); |
| 44 } |
| 45 |
| 46 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> |
| 47 FakeRemoteSecurityKeyIpcServer::AsWeakPtr() { |
| 48 return weak_factory_.GetWeakPtr(); |
| 49 } |
| 50 |
| 51 bool FakeRemoteSecurityKeyIpcServer::OnMessageReceived( |
| 52 const IPC::Message& message) { |
| 53 bool handled = true; |
| 54 IPC_BEGIN_MESSAGE_MAP(FakeRemoteSecurityKeyIpcServer, message) |
| 55 IPC_MESSAGE_HANDLER(ChromotingRemoteSecurityKeyToNetworkMsg_Request, |
| 56 SendRequest) |
| 57 IPC_MESSAGE_UNHANDLED(handled = false) |
| 58 IPC_END_MESSAGE_MAP() |
| 59 |
| 60 EXPECT_TRUE(handled); |
| 61 return handled; |
| 62 } |
| 63 |
| 64 void FakeRemoteSecurityKeyIpcServer::OnChannelConnected(int32_t peer_pid) {} |
| 65 |
| 66 void FakeRemoteSecurityKeyIpcServer::OnChannelError() {} |
| 67 |
| 68 bool FakeRemoteSecurityKeyIpcServer::CreateChannel( |
| 69 const std::string& channel_name, |
| 70 base::TimeDelta request_timeout) { |
| 71 channel_name_ = channel_name; |
| 72 |
| 73 ipc_channel_ = |
| 74 IPC::Channel::CreateNamedServer(IPC::ChannelHandle(channel_name_), this); |
| 75 EXPECT_NE(nullptr, ipc_channel_); |
| 76 return ipc_channel_->Connect(); |
| 77 } |
| 78 |
| 79 bool FakeRemoteSecurityKeyIpcServer::SendResponse( |
| 80 const std::string& message_data) { |
| 81 last_message_received_ = message_data; |
| 82 |
| 83 // This class works in two modes: one in which the test wants the IPC channel |
| 84 // to be created and used for notification and the second mode where the test |
| 85 // wants to notified of a response via a callback. If a callback is set then |
| 86 // we use it, otherwise we will use the IPC connection to send a message. |
| 87 if (!send_response_callback_.is_null()) { |
| 88 send_response_callback_.Run(); |
| 89 return true; |
| 90 } |
| 91 |
| 92 return ipc_channel_->Send( |
| 93 new ChromotingNetworkToRemoteSecurityKeyMsg_Response(message_data)); |
| 94 } |
| 95 |
| 96 FakeRemoteSecurityKeyIpcServerFactory::FakeRemoteSecurityKeyIpcServerFactory() { |
| 97 RemoteSecurityKeyIpcServer::SetFactoryForTest(this); |
| 98 } |
| 99 |
| 100 FakeRemoteSecurityKeyIpcServerFactory:: |
| 101 ~FakeRemoteSecurityKeyIpcServerFactory() { |
| 102 RemoteSecurityKeyIpcServer::SetFactoryForTest(nullptr); |
| 103 } |
| 104 |
| 105 std::unique_ptr<RemoteSecurityKeyIpcServer> |
| 106 FakeRemoteSecurityKeyIpcServerFactory::Create( |
| 107 int connection_id, |
| 108 uint32_t peer_session_id, |
| 109 base::TimeDelta initial_connect_timeout, |
| 110 const GnubbyAuthHandler::SendMessageCallback& send_message_callback, |
| 111 const base::Closure& done_callback) { |
| 112 std::unique_ptr<FakeRemoteSecurityKeyIpcServer> fake_ipc_server( |
| 113 new FakeRemoteSecurityKeyIpcServer(connection_id, peer_session_id, |
| 114 initial_connect_timeout, |
| 115 send_message_callback, done_callback)); |
| 116 |
| 117 ipc_server_map_[connection_id] = fake_ipc_server->AsWeakPtr(); |
| 118 |
| 119 return base::WrapUnique(fake_ipc_server.release()); |
| 120 } |
| 121 |
| 122 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> |
| 123 FakeRemoteSecurityKeyIpcServerFactory::GetIpcServerObject(int connection_id) { |
| 124 return ipc_server_map_[connection_id]; |
| 125 } |
| 126 |
| 127 } // namespace remoting |
OLD | NEW |