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