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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/timer/mock_timer.h" | 13 #include "base/timer/mock_timer.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
17 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
18 #include "net/socket/unix_domain_client_socket_posix.h" | 18 #include "net/socket/unix_domain_client_socket_posix.h" |
19 #include "remoting/host/security_key/gnubby_auth_handler.h" | 19 #include "remoting/host/security_key/security_key_auth_handler.h" |
20 #include "remoting/host/security_key/gnubby_socket.h" | 20 #include "remoting/host/security_key/security_key_socket.h" |
21 #include "remoting/proto/internal.pb.h" | 21 #include "remoting/proto/internal.pb.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
24 namespace remoting { | 24 namespace remoting { |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 const char kSocketFilename[] = "socket_for_testing"; | 28 const char kSocketFilename[] = "socket_for_testing"; |
29 | 29 |
30 // Test gnubby request data. | 30 // Test security key request data. |
31 const unsigned char kRequestData[] = { | 31 const unsigned char kRequestData[] = { |
32 0x00, 0x00, 0x00, 0x9a, 0x65, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, | 32 0x00, 0x00, 0x00, 0x9a, 0x65, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, |
33 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0x90, | 33 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x60, 0x90, |
34 0x24, 0x71, 0xf8, 0xf2, 0xe5, 0xdf, 0x7f, 0x81, 0xc7, 0x49, 0xc4, 0xa3, | 34 0x24, 0x71, 0xf8, 0xf2, 0xe5, 0xdf, 0x7f, 0x81, 0xc7, 0x49, 0xc4, 0xa3, |
35 0x58, 0x5c, 0xf6, 0xcc, 0x40, 0x14, 0x28, 0x0c, 0xa0, 0xfa, 0x03, 0x18, | 35 0x58, 0x5c, 0xf6, 0xcc, 0x40, 0x14, 0x28, 0x0c, 0xa0, 0xfa, 0x03, 0x18, |
36 0x38, 0xd8, 0x7d, 0x77, 0x2b, 0x3a, 0x00, 0x00, 0x00, 0x20, 0x64, 0x46, | 36 0x38, 0xd8, 0x7d, 0x77, 0x2b, 0x3a, 0x00, 0x00, 0x00, 0x20, 0x64, 0x46, |
37 0x47, 0x2f, 0xdf, 0x6e, 0xed, 0x7b, 0xf3, 0xc3, 0x37, 0x20, 0xf2, 0x36, | 37 0x47, 0x2f, 0xdf, 0x6e, 0xed, 0x7b, 0xf3, 0xc3, 0x37, 0x20, 0xf2, 0x36, |
38 0x67, 0x6c, 0x36, 0xe1, 0xb4, 0x5e, 0xbe, 0x04, 0x85, 0xdb, 0x89, 0xa3, | 38 0x67, 0x6c, 0x36, 0xe1, 0xb4, 0x5e, 0xbe, 0x04, 0x85, 0xdb, 0x89, 0xa3, |
39 0xcd, 0xfd, 0xd2, 0x4b, 0xd6, 0x9f, 0x00, 0x00, 0x00, 0x40, 0x38, 0x35, | 39 0xcd, 0xfd, 0xd2, 0x4b, 0xd6, 0x9f, 0x00, 0x00, 0x00, 0x40, 0x38, 0x35, |
40 0x05, 0x75, 0x1d, 0x13, 0x6e, 0xb3, 0x6b, 0x1d, 0x29, 0xae, 0xd3, 0x43, | 40 0x05, 0x75, 0x1d, 0x13, 0x6e, 0xb3, 0x6b, 0x1d, 0x29, 0xae, 0xd3, 0x43, |
41 0xe6, 0x84, 0x8f, 0xa3, 0x9d, 0x65, 0x4e, 0x2f, 0x57, 0xe3, 0xf6, 0xe6, | 41 0xe6, 0x84, 0x8f, 0xa3, 0x9d, 0x65, 0x4e, 0x2f, 0x57, 0xe3, 0xf6, 0xe6, |
42 0x20, 0x3c, 0x00, 0xc6, 0xe1, 0x73, 0x34, 0xe2, 0x23, 0x99, 0xc4, 0xfa, | 42 0x20, 0x3c, 0x00, 0xc6, 0xe1, 0x73, 0x34, 0xe2, 0x23, 0x99, 0xc4, 0xfa, |
43 0x91, 0xc2, 0xd5, 0x97, 0xc1, 0x8b, 0xd0, 0x3c, 0x13, 0xba, 0xf0, 0xd7, | 43 0x91, 0xc2, 0xd5, 0x97, 0xc1, 0x8b, 0xd0, 0x3c, 0x13, 0xba, 0xf0, 0xd7, |
44 0x5e, 0xa3, 0xbc, 0x02, 0x5b, 0xec, 0xe4, 0x4b, 0xae, 0x0e, 0xf2, 0xbd, | 44 0x5e, 0xa3, 0xbc, 0x02, 0x5b, 0xec, 0xe4, 0x4b, 0xae, 0x0e, 0xf2, 0xbd, |
45 0xc8, 0xaa}; | 45 0xc8, 0xaa}; |
46 | 46 |
47 } // namespace | 47 } // namespace |
48 | 48 |
49 class GnubbyAuthHandlerLinuxTest : public testing::Test { | 49 class SecurityKeyAuthHandlerLinuxTest : public testing::Test { |
50 public: | 50 public: |
51 GnubbyAuthHandlerLinuxTest() | 51 SecurityKeyAuthHandlerLinuxTest() |
52 : run_loop_(new base::RunLoop()), last_connection_id_received_(-1) { | 52 : run_loop_(new base::RunLoop()), last_connection_id_received_(-1) { |
53 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | 53 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
54 socket_path_ = temp_dir_.path().Append(kSocketFilename); | 54 socket_path_ = temp_dir_.path().Append(kSocketFilename); |
55 remoting::GnubbyAuthHandler::SetGnubbySocketName(socket_path_); | 55 remoting::SecurityKeyAuthHandler::SetSecurityKeySocketName(socket_path_); |
56 | 56 |
57 send_message_callback_ = | 57 send_message_callback_ = |
58 base::Bind(&GnubbyAuthHandlerLinuxTest::SendMessageToClient, | 58 base::Bind(&SecurityKeyAuthHandlerLinuxTest::SendMessageToClient, |
59 base::Unretained(this)); | 59 base::Unretained(this)); |
60 auth_handler_ = | 60 auth_handler_ = remoting::SecurityKeyAuthHandler::Create( |
61 remoting::GnubbyAuthHandler::Create(nullptr, send_message_callback_); | 61 nullptr, send_message_callback_); |
62 } | 62 } |
63 | 63 |
64 void WaitForSendMessageToClient() { | 64 void WaitForSendMessageToClient() { |
65 run_loop_->Run(); | 65 run_loop_->Run(); |
66 run_loop_.reset(new base::RunLoop); | 66 run_loop_.reset(new base::RunLoop); |
67 } | 67 } |
68 | 68 |
69 void SendMessageToClient(int connection_id, const std::string& data) { | 69 void SendMessageToClient(int connection_id, const std::string& data) { |
70 last_connection_id_received_ = connection_id; | 70 last_connection_id_received_ = connection_id; |
71 last_message_received_ = data; | 71 last_message_received_ = data; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 } | 111 } |
112 | 112 |
113 CheckHostDataMessage(connection_id, expected_data); | 113 CheckHostDataMessage(connection_id, expected_data); |
114 } | 114 } |
115 | 115 |
116 protected: | 116 protected: |
117 base::MessageLoopForIO message_loop_; | 117 base::MessageLoopForIO message_loop_; |
118 std::unique_ptr<base::RunLoop> run_loop_; | 118 std::unique_ptr<base::RunLoop> run_loop_; |
119 | 119 |
120 // Object under test. | 120 // Object under test. |
121 std::unique_ptr<GnubbyAuthHandler> auth_handler_; | 121 std::unique_ptr<SecurityKeyAuthHandler> auth_handler_; |
122 | 122 |
123 GnubbyAuthHandler::SendMessageCallback send_message_callback_; | 123 SecurityKeyAuthHandler::SendMessageCallback send_message_callback_; |
124 | 124 |
125 int last_connection_id_received_; | 125 int last_connection_id_received_; |
126 std::string last_message_received_; | 126 std::string last_message_received_; |
127 | 127 |
128 base::ScopedTempDir temp_dir_; | 128 base::ScopedTempDir temp_dir_; |
129 base::FilePath socket_path_; | 129 base::FilePath socket_path_; |
130 base::Closure accept_callback_; | 130 base::Closure accept_callback_; |
131 | 131 |
132 private: | 132 private: |
133 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerLinuxTest); | 133 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerLinuxTest); |
134 }; | 134 }; |
135 | 135 |
136 TEST_F(GnubbyAuthHandlerLinuxTest, NotClosedAfterRequest) { | 136 TEST_F(SecurityKeyAuthHandlerLinuxTest, NotClosedAfterRequest) { |
137 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 137 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
138 | 138 |
139 auth_handler_->CreateGnubbyConnection(); | 139 auth_handler_->CreateSecurityKeyConnection(); |
140 | 140 |
141 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 141 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
142 net::TestCompletionCallback connect_callback; | 142 net::TestCompletionCallback connect_callback; |
143 | 143 |
144 int rv = client_socket.Connect(connect_callback.callback()); | 144 int rv = client_socket.Connect(connect_callback.callback()); |
145 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 145 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
146 | 146 |
147 // Write the request and verify the response. | 147 // Write the request and verify the response. |
148 WriteRequestData(&client_socket); | 148 WriteRequestData(&client_socket); |
149 WaitForAndVerifyHostMessage(1); | 149 WaitForAndVerifyHostMessage(1); |
150 | 150 |
151 // Verify the connection is now valid. | 151 // Verify the connection is now valid. |
152 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); | 152 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); |
153 | 153 |
154 // Verify that completing a request/response cycle didn't close the socket. | 154 // Verify that completing a request/response cycle didn't close the socket. |
155 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); | 155 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); |
156 } | 156 } |
157 | 157 |
158 TEST_F(GnubbyAuthHandlerLinuxTest, HandleTwoRequests) { | 158 TEST_F(SecurityKeyAuthHandlerLinuxTest, HandleTwoRequests) { |
159 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 159 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
160 | 160 |
161 auth_handler_->CreateGnubbyConnection(); | 161 auth_handler_->CreateSecurityKeyConnection(); |
162 | 162 |
163 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 163 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
164 net::TestCompletionCallback connect_callback; | 164 net::TestCompletionCallback connect_callback; |
165 | 165 |
166 int rv = client_socket.Connect(connect_callback.callback()); | 166 int rv = client_socket.Connect(connect_callback.callback()); |
167 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 167 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
168 | 168 |
169 // Write the request and verify the response. | 169 // Write the request and verify the response. |
170 WriteRequestData(&client_socket); | 170 WriteRequestData(&client_socket); |
171 WaitForAndVerifyHostMessage(1); | 171 WaitForAndVerifyHostMessage(1); |
172 | 172 |
173 // Verify the connection is now valid. | 173 // Verify the connection is now valid. |
174 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); | 174 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); |
175 | 175 |
176 // Repeat the request/response cycle. | 176 // Repeat the request/response cycle. |
177 WriteRequestData(&client_socket); | 177 WriteRequestData(&client_socket); |
178 WaitForAndVerifyHostMessage(1); | 178 WaitForAndVerifyHostMessage(1); |
179 | 179 |
180 // Verify the connection is still valid. | 180 // Verify the connection is still valid. |
181 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); | 181 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); |
182 | 182 |
183 // Verify that completing two request/response cycles didn't close the | 183 // Verify that completing two request/response cycles didn't close the |
184 // socket. | 184 // socket. |
185 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); | 185 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); |
186 } | 186 } |
187 | 187 |
188 TEST_F(GnubbyAuthHandlerLinuxTest, HandleTwoIndependentRequests) { | 188 TEST_F(SecurityKeyAuthHandlerLinuxTest, HandleTwoIndependentRequests) { |
189 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 189 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
190 | 190 |
191 auth_handler_->CreateGnubbyConnection(); | 191 auth_handler_->CreateSecurityKeyConnection(); |
192 | 192 |
193 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 193 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
194 net::TestCompletionCallback connect_callback; | 194 net::TestCompletionCallback connect_callback; |
195 | 195 |
196 int rv = client_socket.Connect(connect_callback.callback()); | 196 int rv = client_socket.Connect(connect_callback.callback()); |
197 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 197 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
198 | 198 |
199 // Write the request and verify the response. | 199 // Write the request and verify the response. |
200 WriteRequestData(&client_socket); | 200 WriteRequestData(&client_socket); |
201 WaitForAndVerifyHostMessage(1); | 201 WaitForAndVerifyHostMessage(1); |
(...skipping 11 matching lines...) Expand all Loading... |
213 WaitForAndVerifyHostMessage(2); | 213 WaitForAndVerifyHostMessage(2); |
214 | 214 |
215 // Verify the second connection is valid and the first is not. | 215 // Verify the second connection is valid and the first is not. |
216 ASSERT_TRUE(auth_handler_->IsValidConnectionId(2)); | 216 ASSERT_TRUE(auth_handler_->IsValidConnectionId(2)); |
217 ASSERT_FALSE(auth_handler_->IsValidConnectionId(1)); | 217 ASSERT_FALSE(auth_handler_->IsValidConnectionId(1)); |
218 | 218 |
219 // Verify that the initial socket was released properly. | 219 // Verify that the initial socket was released properly. |
220 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); | 220 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); |
221 } | 221 } |
222 | 222 |
223 TEST_F(GnubbyAuthHandlerLinuxTest, DidReadTimeout) { | 223 TEST_F(SecurityKeyAuthHandlerLinuxTest, DidReadTimeout) { |
224 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 224 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
225 auth_handler_->CreateGnubbyConnection(); | 225 auth_handler_->CreateSecurityKeyConnection(); |
226 | 226 |
227 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 227 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
228 net::TestCompletionCallback connect_callback; | 228 net::TestCompletionCallback connect_callback; |
229 int rv = client_socket.Connect(connect_callback.callback()); | 229 int rv = client_socket.Connect(connect_callback.callback()); |
230 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 230 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
231 auth_handler_->SetRequestTimeoutForTest(base::TimeDelta()); | 231 auth_handler_->SetRequestTimeoutForTest(base::TimeDelta()); |
232 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 232 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
233 } | 233 } |
234 | 234 |
235 TEST_F(GnubbyAuthHandlerLinuxTest, ClientErrorMessageDelivered) { | 235 TEST_F(SecurityKeyAuthHandlerLinuxTest, ClientErrorMessageDelivered) { |
236 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 236 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
237 auth_handler_->CreateGnubbyConnection(); | 237 auth_handler_->CreateSecurityKeyConnection(); |
238 | 238 |
239 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 239 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
240 net::TestCompletionCallback connect_callback; | 240 net::TestCompletionCallback connect_callback; |
241 int rv = client_socket.Connect(connect_callback.callback()); | 241 int rv = client_socket.Connect(connect_callback.callback()); |
242 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 242 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
243 | 243 |
244 auth_handler_->SendErrorAndCloseConnection(1); | 244 auth_handler_->SendErrorAndCloseConnection(1); |
245 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 245 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
246 } | 246 } |
247 | 247 |
248 } // namespace remoting | 248 } // namespace remoting |
OLD | NEW |