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/security_key_message_handler.h" | |
6 | |
7 #include <memory> | |
8 #include <string> | |
9 #include <utility> | |
10 | |
11 #include "base/bind.h" | |
12 #include "base/memory/weak_ptr.h" | |
13 #include "base/message_loop/message_loop.h" | |
14 #include "base/run_loop.h" | |
15 #include "remoting/host/security_key/fake_security_key_ipc_client.h" | |
16 #include "remoting/host/security_key/fake_security_key_message_reader.h" | |
17 #include "remoting/host/security_key/fake_security_key_message_writer.h" | |
18 #include "remoting/host/security_key/security_key_ipc_constants.h" | |
19 #include "remoting/host/security_key/security_key_message.h" | |
20 #include "remoting/host/setup/test_util.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 namespace remoting { | |
24 | |
25 class SecurityKeyMessageHandlerTest : public testing::Test { | |
26 public: | |
27 SecurityKeyMessageHandlerTest(); | |
28 ~SecurityKeyMessageHandlerTest() override; | |
29 | |
30 // Passed to the object used for testing to be called back to signal | |
31 // completion of an action. | |
32 void OperationComplete(); | |
33 | |
34 protected: | |
35 // testing::Test interface. | |
36 void SetUp() override; | |
37 | |
38 // Waits until the current |run_loop_| instance is signaled, then resets it. | |
39 void WaitForOperationComplete(); | |
40 | |
41 // Passed to |message_channel_| and called back when a message is received. | |
42 void OnSecurityKeyMessage(SecurityKeyMessageType message_type, | |
43 const std::string& message_payload); | |
44 | |
45 bool last_operation_failed_ = false; | |
46 SecurityKeyMessageType last_message_type_received_ = | |
47 SecurityKeyMessageType::INVALID; | |
48 std::string last_message_payload_received_; | |
49 | |
50 base::WeakPtr<FakeSecurityKeyIpcClient> ipc_client_weak_ptr_; | |
51 base::WeakPtr<FakeSecurityKeyMessageReader> reader_weak_ptr_; | |
52 base::WeakPtr<FakeSecurityKeyMessageWriter> writer_weak_ptr_; | |
53 std::unique_ptr<SecurityKeyMessageHandler> message_handler_; | |
54 | |
55 private: | |
56 base::MessageLoopForIO message_loop_; | |
57 std::unique_ptr<base::RunLoop> run_loop_; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(SecurityKeyMessageHandlerTest); | |
60 }; | |
61 | |
62 SecurityKeyMessageHandlerTest::SecurityKeyMessageHandlerTest() {} | |
63 | |
64 SecurityKeyMessageHandlerTest::~SecurityKeyMessageHandlerTest() {} | |
65 | |
66 void SecurityKeyMessageHandlerTest::OperationComplete() { | |
67 run_loop_->Quit(); | |
68 } | |
69 | |
70 void SecurityKeyMessageHandlerTest::SetUp() { | |
71 run_loop_.reset(new base::RunLoop()); | |
72 message_handler_.reset(new SecurityKeyMessageHandler()); | |
73 | |
74 std::unique_ptr<FakeSecurityKeyIpcClient> ipc_client( | |
75 new FakeSecurityKeyIpcClient( | |
76 base::Bind(&SecurityKeyMessageHandlerTest::OperationComplete, | |
77 base::Unretained(this)))); | |
78 ipc_client_weak_ptr_ = ipc_client->AsWeakPtr(); | |
79 | |
80 std::unique_ptr<FakeSecurityKeyMessageReader> reader( | |
81 new FakeSecurityKeyMessageReader()); | |
82 reader_weak_ptr_ = reader->AsWeakPtr(); | |
83 | |
84 std::unique_ptr<FakeSecurityKeyMessageWriter> writer( | |
85 new FakeSecurityKeyMessageWriter( | |
86 base::Bind(&SecurityKeyMessageHandlerTest::OperationComplete, | |
87 base::Unretained(this)))); | |
88 writer_weak_ptr_ = writer->AsWeakPtr(); | |
89 | |
90 message_handler_->SetSecurityKeyMessageReaderForTest(std::move(reader)); | |
91 | |
92 message_handler_->SetSecurityKeyMessageWriterForTest(std::move(writer)); | |
93 | |
94 base::File read_file; | |
95 base::File write_file; | |
96 ASSERT_TRUE(MakePipe(&read_file, &write_file)); | |
97 message_handler_->Start( | |
98 std::move(read_file), std::move(write_file), std::move(ipc_client), | |
99 base::Bind(&SecurityKeyMessageHandlerTest::OperationComplete, | |
100 base::Unretained(this))); | |
101 } | |
102 | |
103 void SecurityKeyMessageHandlerTest::WaitForOperationComplete() { | |
104 run_loop_->Run(); | |
105 run_loop_.reset(new base::RunLoop()); | |
106 } | |
107 | |
108 void SecurityKeyMessageHandlerTest::OnSecurityKeyMessage( | |
109 SecurityKeyMessageType message_type, | |
110 const std::string& message_payload) { | |
111 last_message_type_received_ = message_type; | |
112 last_message_payload_received_ = message_payload; | |
113 | |
114 OperationComplete(); | |
115 } | |
116 | |
117 TEST_F(SecurityKeyMessageHandlerTest, | |
118 ProcessConnectMessage_SessionExists_ConnectionAttemptSuccess) { | |
119 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); | |
120 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(true); | |
121 | |
122 reader_weak_ptr_->message_callback().Run( | |
123 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::CONNECT, | |
124 std::string())); | |
125 WaitForOperationComplete(); | |
126 | |
127 ASSERT_EQ(SecurityKeyMessageType::CONNECT_RESPONSE, | |
128 writer_weak_ptr_->last_message_type()); | |
129 ASSERT_EQ(std::string(1, kConnectResponseActiveSession), | |
130 writer_weak_ptr_->last_message_payload()); | |
131 } | |
132 | |
133 TEST_F(SecurityKeyMessageHandlerTest, | |
134 ProcessConnectMessage_SessionExists_WriteFails) { | |
135 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); | |
136 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(true); | |
137 writer_weak_ptr_->set_write_request_succeeded(/*should_succeed=*/false); | |
138 | |
139 reader_weak_ptr_->message_callback().Run( | |
140 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::CONNECT, | |
141 std::string())); | |
142 WaitForOperationComplete(); | |
143 | |
144 ASSERT_FALSE(ipc_client_weak_ptr_.get()); | |
145 ASSERT_FALSE(reader_weak_ptr_.get()); | |
146 ASSERT_FALSE(writer_weak_ptr_.get()); | |
147 } | |
148 | |
149 TEST_F(SecurityKeyMessageHandlerTest, | |
150 ProcessConnectMessage_SessionExists_ConnectionAttemptFailure) { | |
151 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); | |
152 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(false); | |
153 | |
154 reader_weak_ptr_->message_callback().Run( | |
155 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::CONNECT, | |
156 std::string())); | |
157 WaitForOperationComplete(); | |
158 | |
159 ASSERT_EQ(SecurityKeyMessageType::CONNECT_ERROR, | |
160 writer_weak_ptr_->last_message_type()); | |
161 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
162 } | |
163 | |
164 TEST_F(SecurityKeyMessageHandlerTest, ProcessConnectMessage_NoSessionExists) { | |
165 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(false); | |
166 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(false); | |
167 | |
168 reader_weak_ptr_->message_callback().Run( | |
169 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::CONNECT, | |
170 std::string())); | |
171 WaitForOperationComplete(); | |
172 | |
173 ASSERT_EQ(SecurityKeyMessageType::CONNECT_RESPONSE, | |
174 writer_weak_ptr_->last_message_type()); | |
175 ASSERT_EQ(std::string(1, kConnectResponseNoSession), | |
176 writer_weak_ptr_->last_message_payload()); | |
177 } | |
178 | |
179 TEST_F(SecurityKeyMessageHandlerTest, ProcessConnectMessage_IncorrectPayload) { | |
180 ipc_client_weak_ptr_->set_wait_for_ipc_channel_return_value(true); | |
181 ipc_client_weak_ptr_->set_establish_ipc_connection_should_succeed(false); | |
182 | |
183 reader_weak_ptr_->message_callback().Run( | |
184 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::CONNECT, | |
185 "Invalid request payload")); | |
186 WaitForOperationComplete(); | |
187 | |
188 ASSERT_EQ(SecurityKeyMessageType::CONNECT_ERROR, | |
189 writer_weak_ptr_->last_message_type()); | |
190 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
191 } | |
192 | |
193 TEST_F(SecurityKeyMessageHandlerTest, | |
194 ProcessRequestMessage_ValidPayload_IpcSendSuccess) { | |
195 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); | |
196 std::string response_payload("I AM A VALID RESPONSE PAYLOAD!"); | |
197 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); | |
198 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); | |
199 | |
200 reader_weak_ptr_->message_callback().Run( | |
201 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::REQUEST, | |
202 request_payload)); | |
203 WaitForOperationComplete(); | |
204 | |
205 ASSERT_EQ(SecurityKeyMessageType::REQUEST_RESPONSE, | |
206 writer_weak_ptr_->last_message_type()); | |
207 ASSERT_EQ(response_payload, writer_weak_ptr_->last_message_payload()); | |
208 } | |
209 | |
210 TEST_F(SecurityKeyMessageHandlerTest, ProcessRequestMessage_WriteFails) { | |
211 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); | |
212 std::string response_payload("I AM A VALID RESPONSE PAYLOAD!"); | |
213 | |
214 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); | |
215 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); | |
216 writer_weak_ptr_->set_write_request_succeeded(/*should_succeed=*/false); | |
217 | |
218 reader_weak_ptr_->message_callback().Run( | |
219 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::REQUEST, | |
220 request_payload)); | |
221 WaitForOperationComplete(); | |
222 | |
223 ASSERT_FALSE(ipc_client_weak_ptr_.get()); | |
224 ASSERT_FALSE(reader_weak_ptr_.get()); | |
225 ASSERT_FALSE(writer_weak_ptr_.get()); | |
226 } | |
227 | |
228 TEST_F(SecurityKeyMessageHandlerTest, | |
229 ProcessRequestMessage_ValidPayload_IpcSendFailure) { | |
230 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); | |
231 ipc_client_weak_ptr_->set_send_security_request_should_succeed(false); | |
232 | |
233 reader_weak_ptr_->message_callback().Run( | |
234 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::REQUEST, | |
235 request_payload)); | |
236 WaitForOperationComplete(); | |
237 | |
238 ASSERT_EQ(SecurityKeyMessageType::REQUEST_ERROR, | |
239 writer_weak_ptr_->last_message_type()); | |
240 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
241 } | |
242 | |
243 TEST_F(SecurityKeyMessageHandlerTest, | |
244 ProcessRequestMessage_ValidPayload_EmptyClientResponse) { | |
245 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); | |
246 std::string response_payload(""); | |
247 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); | |
248 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); | |
249 | |
250 reader_weak_ptr_->message_callback().Run( | |
251 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::REQUEST, | |
252 request_payload)); | |
253 WaitForOperationComplete(); | |
254 | |
255 ASSERT_EQ(SecurityKeyMessageType::REQUEST_ERROR, | |
256 writer_weak_ptr_->last_message_type()); | |
257 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
258 } | |
259 | |
260 TEST_F(SecurityKeyMessageHandlerTest, | |
261 ProcessRequestMessage_ValidPayload_ClientResponseError) { | |
262 std::string request_payload("I AM A VALID REQUEST PAYLOAD!"); | |
263 std::string response_payload(kSecurityKeyConnectionError); | |
264 ipc_client_weak_ptr_->set_send_security_request_should_succeed(true); | |
265 ipc_client_weak_ptr_->set_security_key_response_payload(response_payload); | |
266 | |
267 reader_weak_ptr_->message_callback().Run( | |
268 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::REQUEST, | |
269 request_payload)); | |
270 WaitForOperationComplete(); | |
271 | |
272 ASSERT_EQ(SecurityKeyMessageType::REQUEST_ERROR, | |
273 writer_weak_ptr_->last_message_type()); | |
274 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
275 } | |
276 | |
277 TEST_F(SecurityKeyMessageHandlerTest, ProcessRequestMessage_InvalidPayload) { | |
278 std::string invalid_payload(""); | |
279 reader_weak_ptr_->message_callback().Run( | |
280 SecurityKeyMessage::CreateMessageForTest(SecurityKeyMessageType::REQUEST, | |
281 invalid_payload)); | |
282 WaitForOperationComplete(); | |
283 | |
284 ASSERT_EQ(SecurityKeyMessageType::REQUEST_ERROR, | |
285 writer_weak_ptr_->last_message_type()); | |
286 ASSERT_FALSE(writer_weak_ptr_->last_message_payload().empty()); | |
287 } | |
288 | |
289 TEST_F(SecurityKeyMessageHandlerTest, ProcessUnknownMessage) { | |
290 reader_weak_ptr_->message_callback().Run( | |
291 SecurityKeyMessage::CreateMessageForTest( | |
292 SecurityKeyMessageType::UNKNOWN_ERROR, std::string())); | |
293 WaitForOperationComplete(); | |
294 | |
295 ASSERT_EQ(SecurityKeyMessageType::UNKNOWN_COMMAND, | |
296 writer_weak_ptr_->last_message_type()); | |
297 } | |
298 | |
299 } // namespace remoting | |
OLD | NEW |