| 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/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 FakeSecurityKeyIpcServer : public SecurityKeyIpcServer, | |
| 31 public IPC::Listener { | |
| 32 public: | |
| 33 FakeSecurityKeyIpcServer( | |
| 34 int connection_id, | |
| 35 uint32_t peer_session_id, | |
| 36 base::TimeDelta initial_connect_timeout, | |
| 37 const SecurityKeyAuthHandler::SendMessageCallback& send_message_callback, | |
| 38 const base::Closure& channel_closed_callback); | |
| 39 ~FakeSecurityKeyIpcServer() override; | |
| 40 | |
| 41 // SecurityKeyIpcServer 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<FakeSecurityKeyIpcServer> 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 SecurityKeyAuthHandler::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<FakeSecurityKeyIpcServer> weak_factory_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(FakeSecurityKeyIpcServer); | |
| 101 }; | |
| 102 | |
| 103 // Used to create FakeSecurityKeyIpcServer 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 FakeSecurityKeyIpcServerFactory : public SecurityKeyIpcServerFactory { | |
| 108 public: | |
| 109 FakeSecurityKeyIpcServerFactory(); | |
| 110 ~FakeSecurityKeyIpcServerFactory() override; | |
| 111 | |
| 112 // SecurityKeyIpcServerFactory implementation. | |
| 113 std::unique_ptr<SecurityKeyIpcServer> Create( | |
| 114 int connection_id, | |
| 115 uint32_t peer_session_id, | |
| 116 base::TimeDelta initial_connect_timeout, | |
| 117 const SecurityKeyAuthHandler::SendMessageCallback& message_callback, | |
| 118 const base::Closure& done_callback) override; | |
| 119 | |
| 120 // Provide a WeakPtr reference to the FakeSecurityKeyIpcServer object | |
| 121 // created for the |connection_id| IPC channel. | |
| 122 base::WeakPtr<FakeSecurityKeyIpcServer> GetIpcServerObject(int connection_id); | |
| 123 | |
| 124 private: | |
| 125 // Tracks each FakeSecurityKeyIpcServer instance created by this | |
| 126 // factory which allows them to be retrieved and queried for tests. | |
| 127 std::map<int, base::WeakPtr<FakeSecurityKeyIpcServer>> ipc_server_map_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(FakeSecurityKeyIpcServerFactory); | |
| 130 }; | |
| 131 | |
| 132 } // namespace remoting | |
| 133 | |
| 134 #endif // REMOTING_HOST_SECURITY_KEY_FAKE_SECURITY_KEY_IPC_SERVER_H_ | |
| OLD | NEW |