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 #ifndef REMOTING_HOST_SECURITY_KEY_FAKE_SECURITY_KEY_IPC_SERVER_H_ |
| 6 #define REMOTING_HOST_SECURITY_KEY_FAKE_SECURITY_KEY_IPC_SERVER_H_ |
| 7 |
| 8 #include "remoting/host/security_key/remote_security_key_ipc_server.h" |
| 9 |
| 10 #include <cstdint> |
| 11 #include <map> |
| 12 #include <memory> |
| 13 #include <string> |
| 14 |
| 15 #include "base/callback_forward.h" |
| 16 #include "base/macros.h" |
| 17 #include "base/memory/weak_ptr.h" |
| 18 #include "ipc/ipc_listener.h" |
| 19 |
| 20 namespace IPC { |
| 21 class Channel; |
| 22 class Message; |
| 23 } // IPC |
| 24 |
| 25 namespace remoting { |
| 26 |
| 27 // Used to send/receive security key messages for testing. It provides a |
| 28 // WeakPtr reference to itself which allows tests to verify its lifetime is |
| 29 // managed properly without interfering with it. |
| 30 class FakeRemoteSecurityKeyIpcServer : public RemoteSecurityKeyIpcServer, |
| 31 public IPC::Listener { |
| 32 public: |
| 33 FakeRemoteSecurityKeyIpcServer( |
| 34 int connection_id, |
| 35 uint32_t peer_session_id, |
| 36 base::TimeDelta initial_connect_timeout, |
| 37 const GnubbyAuthHandler::SendMessageCallback& send_message_callback, |
| 38 const base::Closure& channel_closed_callback); |
| 39 ~FakeRemoteSecurityKeyIpcServer() override; |
| 40 |
| 41 // RemoteSecurityKeyIpcServer interface. |
| 42 bool CreateChannel(const std::string& channel_name, |
| 43 base::TimeDelta request_timeout) override; |
| 44 bool SendResponse(const std::string& message_data) override; |
| 45 |
| 46 // Simulates receipt of a security key request message. |
| 47 void SendRequest(const std::string& message_data); |
| 48 |
| 49 // Simulates the IPC channel being closed. |
| 50 void CloseChannel(); |
| 51 |
| 52 // Returns a WeakPtr reference to this instance. |
| 53 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> AsWeakPtr(); |
| 54 |
| 55 // Returns the payload for the last message received. |
| 56 const std::string& last_message_received() const { |
| 57 return last_message_received_; |
| 58 } |
| 59 |
| 60 // The name of the IPC channel created by this instance. |
| 61 const std::string& channel_name() const { return channel_name_; } |
| 62 |
| 63 // Signaled when a security key response message is received. |
| 64 // NOTE: Ths callback will be used instead of the IPC channel for response |
| 65 // notifications if it is set. |
| 66 void set_send_response_callback(const base::Closure& send_response_callback) { |
| 67 send_response_callback_ = send_response_callback; |
| 68 } |
| 69 |
| 70 private: |
| 71 // IPC::Listener interface. |
| 72 bool OnMessageReceived(const IPC::Message& message) override; |
| 73 void OnChannelConnected(int32_t peer_pid) override; |
| 74 void OnChannelError() override; |
| 75 |
| 76 // The id assigned to this IPC connection. |
| 77 int connection_id_; |
| 78 |
| 79 // Name of the IPC channel this instance was told to connect to. |
| 80 std::string channel_name_; |
| 81 |
| 82 // The payload for the last message received. |
| 83 std::string last_message_received_; |
| 84 |
| 85 // Used to forward security key requests to the remote client. |
| 86 GnubbyAuthHandler::SendMessageCallback send_message_callback_; |
| 87 |
| 88 // Signaled when the IPC channel is closed. |
| 89 base::Closure channel_closed_callback_; |
| 90 |
| 91 // Signaled when a security key response message is received. |
| 92 base::Closure send_response_callback_; |
| 93 |
| 94 // Used for sending/receiving security key messages between processes. |
| 95 std::unique_ptr<IPC::Channel> ipc_channel_; |
| 96 |
| 97 // NOTE: Weak pointers must be invalidated before all other member variables. |
| 98 base::WeakPtrFactory<FakeRemoteSecurityKeyIpcServer> weak_factory_; |
| 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(FakeRemoteSecurityKeyIpcServer); |
| 101 }; |
| 102 |
| 103 // Used to create FakeRemoteSecurityKeyIpcServer instances for testing. |
| 104 // Provides a method which will return a WeakPtr reference to each instance |
| 105 // this factory creates. This allows tests to inject/retrieve messages and |
| 106 // verify the backing instance is destroyed at the appropriate time. |
| 107 class FakeRemoteSecurityKeyIpcServerFactory |
| 108 : public RemoteSecurityKeyIpcServerFactory { |
| 109 public: |
| 110 FakeRemoteSecurityKeyIpcServerFactory(); |
| 111 ~FakeRemoteSecurityKeyIpcServerFactory() override; |
| 112 |
| 113 // RemoteSecurityKeyIpcServerFactory implementation. |
| 114 std::unique_ptr<RemoteSecurityKeyIpcServer> Create( |
| 115 int connection_id, |
| 116 uint32_t peer_session_id, |
| 117 base::TimeDelta initial_connect_timeout, |
| 118 const GnubbyAuthHandler::SendMessageCallback& message_callback, |
| 119 const base::Closure& done_callback) override; |
| 120 |
| 121 // Provide a WeakPtr reference to the FakeRemoteSecurityKeyIpcServer object |
| 122 // created for the |connection_id| IPC channel. |
| 123 base::WeakPtr<FakeRemoteSecurityKeyIpcServer> GetIpcServerObject( |
| 124 int connection_id); |
| 125 |
| 126 private: |
| 127 // Tracks each FakeRemoteSecurityKeyIpcServer instance created by this |
| 128 // factory which allows them to be retrieved and queried for tests. |
| 129 std::map<int, base::WeakPtr<FakeRemoteSecurityKeyIpcServer>> ipc_server_map_; |
| 130 |
| 131 DISALLOW_COPY_AND_ASSIGN(FakeRemoteSecurityKeyIpcServerFactory); |
| 132 }; |
| 133 |
| 134 } // namespace remoting |
| 135 |
| 136 #endif // REMOTING_HOST_SECURITY_KEY_FAKE_SECURITY_KEY_IPC_SERVER_H_ |
OLD | NEW |