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_linux.h" | 19 #include "remoting/host/security_key/gnubby_auth_handler.h" |
20 #include "remoting/host/security_key/gnubby_socket.h" | 20 #include "remoting/host/security_key/gnubby_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 |
(...skipping 20 matching lines...) Expand all Loading... |
50 public: | 50 public: |
51 GnubbyAuthHandlerLinuxTest() | 51 GnubbyAuthHandlerLinuxTest() |
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::GnubbyAuthHandler::SetGnubbySocketName(socket_path_); |
56 | 56 |
57 send_message_callback_ = | 57 send_message_callback_ = |
58 base::Bind(&GnubbyAuthHandlerLinuxTest::SendMessageToClient, | 58 base::Bind(&GnubbyAuthHandlerLinuxTest::SendMessageToClient, |
59 base::Unretained(this)); | 59 base::Unretained(this)); |
60 auth_handler_linux_.reset(new GnubbyAuthHandlerLinux()); | 60 auth_handler_ = remoting::GnubbyAuthHandler::Create(send_message_callback_); |
61 auth_handler_ = auth_handler_linux_.get(); | |
62 auth_handler_->SetSendMessageCallback(send_message_callback_); | |
63 } | 61 } |
64 | 62 |
65 void WaitForSendMessageToClient() { | 63 void WaitForSendMessageToClient() { |
66 run_loop_->Run(); | 64 run_loop_->Run(); |
67 run_loop_.reset(new base::RunLoop); | 65 run_loop_.reset(new base::RunLoop); |
68 } | 66 } |
69 | 67 |
70 void SendMessageToClient(int connection_id, const std::string& data) { | 68 void SendMessageToClient(int connection_id, const std::string& data) { |
71 last_connection_id_received_ = connection_id; | 69 last_connection_id_received_ = connection_id; |
72 last_message_received_ = data; | 70 last_message_received_ = data; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 } | 110 } |
113 | 111 |
114 CheckHostDataMessage(connection_id, expected_data); | 112 CheckHostDataMessage(connection_id, expected_data); |
115 } | 113 } |
116 | 114 |
117 protected: | 115 protected: |
118 base::MessageLoopForIO message_loop_; | 116 base::MessageLoopForIO message_loop_; |
119 scoped_ptr<base::RunLoop> run_loop_; | 117 scoped_ptr<base::RunLoop> run_loop_; |
120 | 118 |
121 // Object under test. | 119 // Object under test. |
122 scoped_ptr<GnubbyAuthHandlerLinux> auth_handler_linux_; | 120 scoped_ptr<GnubbyAuthHandler> auth_handler_; |
123 | |
124 // GnubbyAuthHandler interface for |auth_handler_linux_|. | |
125 GnubbyAuthHandler* auth_handler_; | |
126 | 121 |
127 GnubbyAuthHandler::SendMessageCallback send_message_callback_; | 122 GnubbyAuthHandler::SendMessageCallback send_message_callback_; |
128 | 123 |
129 int last_connection_id_received_; | 124 int last_connection_id_received_; |
130 std::string last_message_received_; | 125 std::string last_message_received_; |
131 | 126 |
132 base::ScopedTempDir temp_dir_; | 127 base::ScopedTempDir temp_dir_; |
133 base::FilePath socket_path_; | 128 base::FilePath socket_path_; |
134 base::Closure accept_callback_; | 129 base::Closure accept_callback_; |
135 | 130 |
136 private: | 131 private: |
137 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerLinuxTest); | 132 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerLinuxTest); |
138 }; | 133 }; |
139 | 134 |
140 TEST_F(GnubbyAuthHandlerLinuxTest, NotClosedAfterRequest) { | 135 TEST_F(GnubbyAuthHandlerLinuxTest, NotClosedAfterRequest) { |
141 ASSERT_EQ(0u, auth_handler_linux_->GetActiveSocketsMapSizeForTest()); | 136 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
142 | 137 |
143 auth_handler_->CreateGnubbyConnection(); | 138 auth_handler_->CreateGnubbyConnection(); |
144 | 139 |
145 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 140 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
146 net::TestCompletionCallback connect_callback; | 141 net::TestCompletionCallback connect_callback; |
147 | 142 |
148 int rv = client_socket.Connect(connect_callback.callback()); | 143 int rv = client_socket.Connect(connect_callback.callback()); |
149 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 144 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
150 | 145 |
151 // Write the request and verify the response. | 146 // Write the request and verify the response. |
152 WriteRequestData(&client_socket); | 147 WriteRequestData(&client_socket); |
153 WaitForAndVerifyHostMessage(1); | 148 WaitForAndVerifyHostMessage(1); |
154 | 149 |
155 // Verify the connection is now valid. | 150 // Verify the connection is now valid. |
156 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); | 151 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); |
157 | 152 |
158 // Verify that completing a request/response cycle didn't close the socket. | 153 // Verify that completing a request/response cycle didn't close the socket. |
159 ASSERT_EQ(1u, auth_handler_linux_->GetActiveSocketsMapSizeForTest()); | 154 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); |
160 } | 155 } |
161 | 156 |
162 TEST_F(GnubbyAuthHandlerLinuxTest, HandleTwoRequests) { | 157 TEST_F(GnubbyAuthHandlerLinuxTest, HandleTwoRequests) { |
163 ASSERT_EQ(0u, auth_handler_linux_->GetActiveSocketsMapSizeForTest()); | 158 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
164 | 159 |
165 auth_handler_->CreateGnubbyConnection(); | 160 auth_handler_->CreateGnubbyConnection(); |
166 | 161 |
167 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 162 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
168 net::TestCompletionCallback connect_callback; | 163 net::TestCompletionCallback connect_callback; |
169 | 164 |
170 int rv = client_socket.Connect(connect_callback.callback()); | 165 int rv = client_socket.Connect(connect_callback.callback()); |
171 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 166 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
172 | 167 |
173 // Write the request and verify the response. | 168 // Write the request and verify the response. |
174 WriteRequestData(&client_socket); | 169 WriteRequestData(&client_socket); |
175 WaitForAndVerifyHostMessage(1); | 170 WaitForAndVerifyHostMessage(1); |
176 | 171 |
177 // Verify the connection is now valid. | 172 // Verify the connection is now valid. |
178 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); | 173 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); |
179 | 174 |
180 // Repeat the request/response cycle. | 175 // Repeat the request/response cycle. |
181 WriteRequestData(&client_socket); | 176 WriteRequestData(&client_socket); |
182 WaitForAndVerifyHostMessage(1); | 177 WaitForAndVerifyHostMessage(1); |
183 | 178 |
184 // Verify the connection is still valid. | 179 // Verify the connection is still valid. |
185 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); | 180 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); |
186 | 181 |
187 // Verify that completing two request/response cycles didn't close the | 182 // Verify that completing two request/response cycles didn't close the |
188 // socket. | 183 // socket. |
189 ASSERT_EQ(1u, auth_handler_linux_->GetActiveSocketsMapSizeForTest()); | 184 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); |
190 } | 185 } |
191 | 186 |
192 TEST_F(GnubbyAuthHandlerLinuxTest, HandleTwoIndependentRequests) { | 187 TEST_F(GnubbyAuthHandlerLinuxTest, HandleTwoIndependentRequests) { |
193 ASSERT_EQ(0u, auth_handler_linux_->GetActiveSocketsMapSizeForTest()); | 188 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
194 | 189 |
195 auth_handler_->CreateGnubbyConnection(); | 190 auth_handler_->CreateGnubbyConnection(); |
196 | 191 |
197 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 192 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
198 net::TestCompletionCallback connect_callback; | 193 net::TestCompletionCallback connect_callback; |
199 | 194 |
200 int rv = client_socket.Connect(connect_callback.callback()); | 195 int rv = client_socket.Connect(connect_callback.callback()); |
201 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 196 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
202 | 197 |
203 // Write the request and verify the response. | 198 // Write the request and verify the response. |
(...skipping 10 matching lines...) Expand all Loading... |
214 | 209 |
215 // Repeat the request/response cycle. | 210 // Repeat the request/response cycle. |
216 WriteRequestData(&client_socket); | 211 WriteRequestData(&client_socket); |
217 WaitForAndVerifyHostMessage(2); | 212 WaitForAndVerifyHostMessage(2); |
218 | 213 |
219 // Verify the second connection is valid and the first is not. | 214 // Verify the second connection is valid and the first is not. |
220 ASSERT_TRUE(auth_handler_->IsValidConnectionId(2)); | 215 ASSERT_TRUE(auth_handler_->IsValidConnectionId(2)); |
221 ASSERT_FALSE(auth_handler_->IsValidConnectionId(1)); | 216 ASSERT_FALSE(auth_handler_->IsValidConnectionId(1)); |
222 | 217 |
223 // Verify that the initial socket was released properly. | 218 // Verify that the initial socket was released properly. |
224 ASSERT_EQ(1u, auth_handler_linux_->GetActiveSocketsMapSizeForTest()); | 219 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); |
225 } | 220 } |
226 | 221 |
227 TEST_F(GnubbyAuthHandlerLinuxTest, DidReadTimeout) { | 222 TEST_F(GnubbyAuthHandlerLinuxTest, DidReadTimeout) { |
228 ASSERT_EQ(0u, auth_handler_linux_->GetActiveSocketsMapSizeForTest()); | 223 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
229 auth_handler_->CreateGnubbyConnection(); | 224 auth_handler_->CreateGnubbyConnection(); |
230 | 225 |
231 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 226 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
232 net::TestCompletionCallback connect_callback; | 227 net::TestCompletionCallback connect_callback; |
233 int rv = client_socket.Connect(connect_callback.callback()); | 228 int rv = client_socket.Connect(connect_callback.callback()); |
234 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 229 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
235 auth_handler_linux_->SetRequestTimeoutForTest(base::TimeDelta()); | 230 auth_handler_->SetRequestTimeoutForTest(base::TimeDelta()); |
236 ASSERT_EQ(0u, auth_handler_linux_->GetActiveSocketsMapSizeForTest()); | 231 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
237 } | 232 } |
238 | 233 |
239 TEST_F(GnubbyAuthHandlerLinuxTest, ClientErrorMessageDelivered) { | 234 TEST_F(GnubbyAuthHandlerLinuxTest, ClientErrorMessageDelivered) { |
240 ASSERT_EQ(0u, auth_handler_linux_->GetActiveSocketsMapSizeForTest()); | 235 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
241 auth_handler_->CreateGnubbyConnection(); | 236 auth_handler_->CreateGnubbyConnection(); |
242 | 237 |
243 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 238 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
244 net::TestCompletionCallback connect_callback; | 239 net::TestCompletionCallback connect_callback; |
245 int rv = client_socket.Connect(connect_callback.callback()); | 240 int rv = client_socket.Connect(connect_callback.callback()); |
246 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 241 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
247 | 242 |
248 auth_handler_->SendErrorAndCloseConnection(1); | 243 auth_handler_->SendErrorAndCloseConnection(1); |
249 ASSERT_EQ(0u, auth_handler_linux_->GetActiveSocketsMapSizeForTest()); | 244 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
250 } | 245 } |
251 | 246 |
252 } // namespace remoting | 247 } // namespace remoting |
OLD | NEW |