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