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