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/bind.h" |
| 8 #include "base/bind_helpers.h" |
7 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 10 #include "base/files/scoped_temp_dir.h" |
9 #include "base/macros.h" | 11 #include "base/macros.h" |
10 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
12 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/threading/thread.h" |
13 #include "base/timer/mock_timer.h" | 16 #include "base/timer/mock_timer.h" |
14 #include "base/values.h" | 17 #include "base/values.h" |
15 #include "net/base/io_buffer.h" | 18 #include "net/base/io_buffer.h" |
16 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
17 #include "net/base/test_completion_callback.h" | 20 #include "net/base/test_completion_callback.h" |
18 #include "net/socket/unix_domain_client_socket_posix.h" | 21 #include "net/socket/unix_domain_client_socket_posix.h" |
19 #include "remoting/host/security_key/security_key_auth_handler.h" | 22 #include "remoting/host/security_key/security_key_auth_handler.h" |
20 #include "remoting/host/security_key/security_key_socket.h" | 23 #include "remoting/host/security_key/security_key_socket.h" |
21 #include "remoting/proto/internal.pb.h" | 24 #include "remoting/proto/internal.pb.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
23 | 26 |
24 namespace remoting { | 27 namespace remoting { |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
| 31 const size_t kReadBufferSize = 1; |
| 32 |
28 const char kSocketFilename[] = "socket_for_testing"; | 33 const char kSocketFilename[] = "socket_for_testing"; |
29 | 34 |
30 // Test security key request data. | 35 // Test security key request data. |
31 const unsigned char kRequestData[] = { | 36 const unsigned char kRequestData[] = { |
32 0x00, 0x00, 0x00, 0x9a, 0x65, 0x1e, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, | 37 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, | 38 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, | 39 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, | 40 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, | 41 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, | 42 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, | 43 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, | 44 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, | 45 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, | 46 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, | 47 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, | 48 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, | 49 0x5e, 0xa3, 0xbc, 0x02, 0x5b, 0xec, 0xe4, 0x4b, 0xae, 0x0e, 0xf2, 0xbd, |
45 0xc8, 0xaa}; | 50 0xc8, 0xaa}; |
46 | 51 |
47 } // namespace | 52 } // namespace |
48 | 53 |
49 class SecurityKeyAuthHandlerLinuxTest : public testing::Test { | 54 class SecurityKeyAuthHandlerLinuxTest : public testing::Test { |
50 public: | 55 public: |
51 SecurityKeyAuthHandlerLinuxTest() | 56 SecurityKeyAuthHandlerLinuxTest(); |
52 : run_loop_(new base::RunLoop()), last_connection_id_received_(-1) { | |
53 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
54 socket_path_ = temp_dir_.path().Append(kSocketFilename); | |
55 remoting::SecurityKeyAuthHandler::SetSecurityKeySocketName(socket_path_); | |
56 | 57 |
57 send_message_callback_ = | 58 // Creates a new socket and waits until it is ready to accept new connections. |
58 base::Bind(&SecurityKeyAuthHandlerLinuxTest::SendMessageToClient, | 59 void CreateSocketAndWait(); |
59 base::Unretained(this)); | |
60 auth_handler_ = remoting::SecurityKeyAuthHandler::Create( | |
61 nullptr, send_message_callback_); | |
62 } | |
63 | 60 |
64 void WaitForSendMessageToClient() { | 61 // Used as a callback |auth_handler_|, stores the values received. |
65 run_loop_->Run(); | 62 void SendMessageToClient(int connection_id, const std::string& data); |
66 run_loop_.reset(new base::RunLoop); | |
67 } | |
68 | 63 |
69 void SendMessageToClient(int connection_id, const std::string& data) { | 64 // Writes |kRequestData| to |client_socket|. |
70 last_connection_id_received_ = connection_id; | 65 void WriteRequestData(net::UnixDomainClientSocket* client_socket); |
71 last_message_received_ = data; | |
72 run_loop_->Quit(); | |
73 } | |
74 | 66 |
75 void CheckHostDataMessage(int id, const std::string& expected_data) { | 67 // Helper method which validates message data once it has been received. |
76 ASSERT_EQ(id, last_connection_id_received_); | 68 void WaitForAndVerifyHostMessage(int connection_id); |
77 ASSERT_EQ(expected_data.length(), last_message_received_.length()); | |
78 ASSERT_EQ(expected_data, last_message_received_); | |
79 } | |
80 | 69 |
81 void WriteRequestData(net::UnixDomainClientSocket* client_socket) { | 70 // Waits until all existing posted tasks on |file_thread_| have been run. |
82 int request_len = sizeof(kRequestData); | 71 void WaitForExistingSocketOperations(); |
83 scoped_refptr<net::DrainableIOBuffer> request_buffer( | |
84 new net::DrainableIOBuffer( | |
85 new net::WrappedIOBuffer( | |
86 reinterpret_cast<const char*>(kRequestData)), | |
87 request_len)); | |
88 net::TestCompletionCallback write_callback; | |
89 int bytes_written = 0; | |
90 while (bytes_written < request_len) { | |
91 int write_result = client_socket->Write(request_buffer.get(), | |
92 request_buffer->BytesRemaining(), | |
93 write_callback.callback()); | |
94 write_result = write_callback.GetResult(write_result); | |
95 ASSERT_GT(write_result, 0); | |
96 bytes_written += write_result; | |
97 ASSERT_LE(bytes_written, request_len); | |
98 request_buffer->DidConsume(write_result); | |
99 } | |
100 ASSERT_EQ(request_len, bytes_written); | |
101 } | |
102 | 72 |
103 void WaitForAndVerifyHostMessage(int connection_id) { | 73 // Waits until |client_socket| has been closed. |
104 WaitForSendMessageToClient(); | 74 void WaitForConnectionClosed(net::UnixDomainClientSocket* client_socket); |
105 std::string expected_data; | |
106 expected_data.reserve(sizeof(kRequestData) - 4); | |
107 | |
108 // Skip first four bytes and build up the response string. | |
109 for (size_t i = 4; i < sizeof(kRequestData); ++i) { | |
110 expected_data.append(1, static_cast<unsigned char>(kRequestData[i])); | |
111 } | |
112 | |
113 CheckHostDataMessage(connection_id, expected_data); | |
114 } | |
115 | 75 |
116 protected: | 76 protected: |
| 77 // Waits for |SendMessageToClient()| to be signaled. |
| 78 void WaitForSendMessageToClient(); |
| 79 |
| 80 // Verfies the data passed to the |SendMessageToClient| callback was valid. |
| 81 void CheckHostDataMessage(int id, const std::string& expected_data); |
| 82 |
117 base::MessageLoopForIO message_loop_; | 83 base::MessageLoopForIO message_loop_; |
118 std::unique_ptr<base::RunLoop> run_loop_; | 84 std::unique_ptr<base::RunLoop> run_loop_; |
119 | 85 |
| 86 base::Thread file_thread_; |
| 87 |
120 // Object under test. | 88 // Object under test. |
121 std::unique_ptr<SecurityKeyAuthHandler> auth_handler_; | 89 std::unique_ptr<SecurityKeyAuthHandler> auth_handler_; |
122 | 90 |
123 SecurityKeyAuthHandler::SendMessageCallback send_message_callback_; | 91 SecurityKeyAuthHandler::SendMessageCallback send_message_callback_; |
124 | 92 |
125 int last_connection_id_received_; | 93 int last_connection_id_received_; |
126 std::string last_message_received_; | 94 std::string last_message_received_; |
127 | 95 |
128 base::ScopedTempDir temp_dir_; | 96 base::ScopedTempDir temp_dir_; |
129 base::FilePath socket_path_; | 97 base::FilePath socket_path_; |
130 base::Closure accept_callback_; | 98 base::Closure accept_callback_; |
131 | 99 |
132 private: | 100 private: |
133 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerLinuxTest); | 101 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerLinuxTest); |
134 }; | 102 }; |
135 | 103 |
136 TEST_F(SecurityKeyAuthHandlerLinuxTest, NotClosedAfterRequest) { | 104 SecurityKeyAuthHandlerLinuxTest::SecurityKeyAuthHandlerLinuxTest() |
| 105 : run_loop_(new base::RunLoop()), |
| 106 file_thread_("SecurityKeyAuthHandlerLinuxTest_FileThread"), |
| 107 last_connection_id_received_(-1) { |
| 108 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 109 socket_path_ = temp_dir_.path().Append(kSocketFilename); |
| 110 remoting::SecurityKeyAuthHandler::SetSecurityKeySocketName(socket_path_); |
| 111 |
| 112 file_thread_.StartWithOptions( |
| 113 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| 114 |
| 115 send_message_callback_ = |
| 116 base::Bind(&SecurityKeyAuthHandlerLinuxTest::SendMessageToClient, |
| 117 base::Unretained(this)); |
| 118 |
| 119 auth_handler_ = remoting::SecurityKeyAuthHandler::Create( |
| 120 /*client_session_details=*/nullptr, send_message_callback_, |
| 121 file_thread_.task_runner()); |
| 122 } |
| 123 |
| 124 void SecurityKeyAuthHandlerLinuxTest::WaitForExistingSocketOperations() { |
| 125 ASSERT_TRUE(file_thread_.task_runner()->PostTaskAndReply( |
| 126 FROM_HERE, base::Bind(&base::DoNothing), run_loop_->QuitClosure())); |
| 127 run_loop_->Run(); |
| 128 run_loop_.reset(new base::RunLoop); |
| 129 } |
| 130 |
| 131 void SecurityKeyAuthHandlerLinuxTest::CreateSocketAndWait() { |
137 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 132 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
138 | 133 |
139 auth_handler_->CreateSecurityKeyConnection(); | 134 auth_handler_->CreateSecurityKeyConnection(); |
| 135 WaitForExistingSocketOperations(); |
| 136 |
| 137 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
| 138 } |
| 139 |
| 140 void SecurityKeyAuthHandlerLinuxTest::SendMessageToClient( |
| 141 int connection_id, |
| 142 const std::string& data) { |
| 143 last_connection_id_received_ = connection_id; |
| 144 last_message_received_ = data; |
| 145 run_loop_->Quit(); |
| 146 } |
| 147 |
| 148 void SecurityKeyAuthHandlerLinuxTest::WaitForSendMessageToClient() { |
| 149 run_loop_->Run(); |
| 150 run_loop_.reset(new base::RunLoop); |
| 151 } |
| 152 |
| 153 void SecurityKeyAuthHandlerLinuxTest::WriteRequestData( |
| 154 net::UnixDomainClientSocket* client_socket) { |
| 155 int request_len = sizeof(kRequestData); |
| 156 scoped_refptr<net::DrainableIOBuffer> request_buffer( |
| 157 new net::DrainableIOBuffer( |
| 158 new net::WrappedIOBuffer(reinterpret_cast<const char*>(kRequestData)), |
| 159 request_len)); |
| 160 net::TestCompletionCallback write_callback; |
| 161 int bytes_written = 0; |
| 162 while (bytes_written < request_len) { |
| 163 int write_result = client_socket->Write(request_buffer.get(), |
| 164 request_buffer->BytesRemaining(), |
| 165 write_callback.callback()); |
| 166 write_result = write_callback.GetResult(write_result); |
| 167 ASSERT_GT(write_result, 0); |
| 168 bytes_written += write_result; |
| 169 ASSERT_LE(bytes_written, request_len); |
| 170 request_buffer->DidConsume(write_result); |
| 171 } |
| 172 ASSERT_EQ(request_len, bytes_written); |
| 173 } |
| 174 |
| 175 void SecurityKeyAuthHandlerLinuxTest::CheckHostDataMessage( |
| 176 int id, |
| 177 const std::string& expected_data) { |
| 178 ASSERT_EQ(id, last_connection_id_received_); |
| 179 ASSERT_EQ(expected_data.length(), last_message_received_.length()); |
| 180 ASSERT_EQ(expected_data, last_message_received_); |
| 181 } |
| 182 |
| 183 void SecurityKeyAuthHandlerLinuxTest::WaitForAndVerifyHostMessage( |
| 184 int connection_id) { |
| 185 WaitForSendMessageToClient(); |
| 186 std::string expected_data; |
| 187 expected_data.reserve(sizeof(kRequestData) - 4); |
| 188 |
| 189 // Skip first four bytes and build up the response string. |
| 190 for (size_t i = 4; i < sizeof(kRequestData); ++i) { |
| 191 expected_data.append(1, static_cast<unsigned char>(kRequestData[i])); |
| 192 } |
| 193 |
| 194 CheckHostDataMessage(connection_id, expected_data); |
| 195 } |
| 196 |
| 197 void SecurityKeyAuthHandlerLinuxTest::WaitForConnectionClosed( |
| 198 net::UnixDomainClientSocket* client_socket) { |
| 199 // Ensure there are no pending operations. |
| 200 WaitForExistingSocketOperations(); |
| 201 |
| 202 // Create a blocking read operation to wait until the socket has been closed. |
| 203 scoped_refptr<net::IOBuffer> read_buffer(new net::IOBuffer(kReadBufferSize)); |
| 204 net::TestCompletionCallback read_callback; |
| 205 int read_result = client_socket->Read(read_buffer.get(), kReadBufferSize, |
| 206 read_callback.callback()); |
| 207 |
| 208 // GetResult blocks until the IO is no longer pending. |
| 209 read_result = read_callback.GetResult(read_result); |
| 210 ASSERT_EQ(read_result, static_cast<int>(kReadBufferSize)); |
| 211 ASSERT_TRUE(std::string(read_buffer->data()).empty()); |
| 212 |
| 213 // Ensure all current and pending operations are complete. |
| 214 WaitForExistingSocketOperations(); |
| 215 } |
| 216 |
| 217 TEST_F(SecurityKeyAuthHandlerLinuxTest, NotClosedAfterRequest) { |
| 218 CreateSocketAndWait(); |
140 | 219 |
141 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 220 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
142 net::TestCompletionCallback connect_callback; | 221 net::TestCompletionCallback connect_callback; |
143 | |
144 int rv = client_socket.Connect(connect_callback.callback()); | 222 int rv = client_socket.Connect(connect_callback.callback()); |
145 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 223 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
| 224 WaitForExistingSocketOperations(); |
146 | 225 |
147 // Write the request and verify the response. | 226 // Write the request and verify the response. |
148 WriteRequestData(&client_socket); | 227 WriteRequestData(&client_socket); |
149 WaitForAndVerifyHostMessage(1); | 228 WaitForAndVerifyHostMessage(1); |
150 | 229 |
151 // Verify the connection is now valid. | 230 // Verify the connection is now valid. |
152 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); | 231 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); |
153 | 232 |
154 // Verify that completing a request/response cycle didn't close the socket. | 233 // Verify that completing a request/response cycle didn't close the socket. |
155 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); | 234 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); |
156 } | 235 } |
157 | 236 |
158 TEST_F(SecurityKeyAuthHandlerLinuxTest, HandleTwoRequests) { | 237 TEST_F(SecurityKeyAuthHandlerLinuxTest, HandleTwoRequests) { |
159 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 238 CreateSocketAndWait(); |
160 | |
161 auth_handler_->CreateSecurityKeyConnection(); | |
162 | 239 |
163 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 240 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
164 net::TestCompletionCallback connect_callback; | 241 net::TestCompletionCallback connect_callback; |
165 | |
166 int rv = client_socket.Connect(connect_callback.callback()); | 242 int rv = client_socket.Connect(connect_callback.callback()); |
167 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 243 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
| 244 WaitForExistingSocketOperations(); |
168 | 245 |
169 // Write the request and verify the response. | 246 // Write the request and verify the response. |
170 WriteRequestData(&client_socket); | 247 WriteRequestData(&client_socket); |
171 WaitForAndVerifyHostMessage(1); | 248 WaitForAndVerifyHostMessage(1); |
172 | 249 |
173 // Verify the connection is now valid. | 250 // Verify the connection is now valid. |
174 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); | 251 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); |
175 | 252 |
176 // Repeat the request/response cycle. | 253 // Repeat the request/response cycle. |
177 WriteRequestData(&client_socket); | 254 WriteRequestData(&client_socket); |
178 WaitForAndVerifyHostMessage(1); | 255 WaitForAndVerifyHostMessage(1); |
179 | 256 |
180 // Verify the connection is still valid. | 257 // Verify the connection is still valid. |
181 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); | 258 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); |
182 | 259 |
183 // Verify that completing two request/response cycles didn't close the | 260 // Verify that completing two request/response cycles didn't close the |
184 // socket. | 261 // socket. |
185 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); | 262 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); |
186 } | 263 } |
187 | 264 |
188 TEST_F(SecurityKeyAuthHandlerLinuxTest, HandleTwoIndependentRequests) { | 265 TEST_F(SecurityKeyAuthHandlerLinuxTest, HandleTwoIndependentRequests) { |
189 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 266 CreateSocketAndWait(); |
190 | |
191 auth_handler_->CreateSecurityKeyConnection(); | |
192 | 267 |
193 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 268 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
194 net::TestCompletionCallback connect_callback; | 269 net::TestCompletionCallback connect_callback; |
195 | |
196 int rv = client_socket.Connect(connect_callback.callback()); | 270 int rv = client_socket.Connect(connect_callback.callback()); |
197 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 271 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
| 272 WaitForExistingSocketOperations(); |
198 | 273 |
199 // Write the request and verify the response. | 274 // Write the request and verify the response. |
200 WriteRequestData(&client_socket); | 275 WriteRequestData(&client_socket); |
201 WaitForAndVerifyHostMessage(1); | 276 WaitForAndVerifyHostMessage(1); |
202 | 277 |
203 // Verify the first connection is now valid. | 278 // Verify the first connection is now valid. |
204 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); | 279 ASSERT_TRUE(auth_handler_->IsValidConnectionId(1)); |
205 | 280 |
206 // Disconnect and establish a new connection. | 281 // Disconnect and establish a new connection. |
207 client_socket.Disconnect(); | 282 client_socket.Disconnect(); |
208 rv = client_socket.Connect(connect_callback.callback()); | 283 rv = client_socket.Connect(connect_callback.callback()); |
209 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 284 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
| 285 WaitForExistingSocketOperations(); |
210 | 286 |
211 // Repeat the request/response cycle. | 287 // Repeat the request/response cycle. |
212 WriteRequestData(&client_socket); | 288 WriteRequestData(&client_socket); |
213 WaitForAndVerifyHostMessage(2); | 289 WaitForAndVerifyHostMessage(2); |
214 | 290 |
215 // Verify the second connection is valid and the first is not. | 291 // Verify the second connection is valid and the first is not. |
216 ASSERT_TRUE(auth_handler_->IsValidConnectionId(2)); | 292 ASSERT_TRUE(auth_handler_->IsValidConnectionId(2)); |
217 ASSERT_FALSE(auth_handler_->IsValidConnectionId(1)); | 293 ASSERT_FALSE(auth_handler_->IsValidConnectionId(1)); |
218 | 294 |
219 // Verify that the initial socket was released properly. | 295 // Verify that the initial socket was released properly. |
220 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); | 296 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); |
221 } | 297 } |
222 | 298 |
| 299 TEST_F(SecurityKeyAuthHandlerLinuxTest, HandleNumerousIndependentRequests) { |
| 300 CreateSocketAndWait(); |
| 301 |
| 302 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
| 303 net::TestCompletionCallback connect_callback; |
| 304 |
| 305 // Connection IDs start at 1 so start there to simplify the rest of the code. |
| 306 for (int i = 1; i < 11; i++) { |
| 307 int rv = client_socket.Connect(connect_callback.callback()); |
| 308 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
| 309 WaitForExistingSocketOperations(); |
| 310 |
| 311 // Verify the connection is now valid. |
| 312 ASSERT_TRUE(auth_handler_->IsValidConnectionId(i)); |
| 313 |
| 314 // Write the request and verify the response. |
| 315 WriteRequestData(&client_socket); |
| 316 WaitForAndVerifyHostMessage(i); |
| 317 |
| 318 // Disconnect and establish a new connection. |
| 319 client_socket.Disconnect(); |
| 320 WaitForExistingSocketOperations(); |
| 321 |
| 322 ASSERT_FALSE(auth_handler_->IsValidConnectionId(i)); |
| 323 } |
| 324 |
| 325 // Verify that the initial sockets were released properly. |
| 326 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
| 327 } |
| 328 |
223 TEST_F(SecurityKeyAuthHandlerLinuxTest, DidReadTimeout) { | 329 TEST_F(SecurityKeyAuthHandlerLinuxTest, DidReadTimeout) { |
224 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 330 auth_handler_->SetRequestTimeoutForTest(base::TimeDelta()); |
225 auth_handler_->CreateSecurityKeyConnection(); | 331 |
| 332 CreateSocketAndWait(); |
226 | 333 |
227 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 334 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
228 net::TestCompletionCallback connect_callback; | 335 net::TestCompletionCallback connect_callback; |
229 int rv = client_socket.Connect(connect_callback.callback()); | 336 int rv = client_socket.Connect(connect_callback.callback()); |
230 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 337 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
231 auth_handler_->SetRequestTimeoutForTest(base::TimeDelta()); | 338 WaitForConnectionClosed(&client_socket); |
| 339 |
232 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 340 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
233 } | 341 } |
234 | 342 |
235 TEST_F(SecurityKeyAuthHandlerLinuxTest, ClientErrorMessageDelivered) { | 343 TEST_F(SecurityKeyAuthHandlerLinuxTest, ClientErrorMessageDelivered) { |
236 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 344 CreateSocketAndWait(); |
237 auth_handler_->CreateSecurityKeyConnection(); | |
238 | 345 |
239 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); | 346 net::UnixDomainClientSocket client_socket(socket_path_.value(), false); |
240 net::TestCompletionCallback connect_callback; | 347 net::TestCompletionCallback connect_callback; |
241 int rv = client_socket.Connect(connect_callback.callback()); | 348 int rv = client_socket.Connect(connect_callback.callback()); |
242 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); | 349 ASSERT_EQ(net::OK, connect_callback.GetResult(rv)); |
| 350 WaitForExistingSocketOperations(); |
| 351 |
| 352 ASSERT_EQ(1u, auth_handler_->GetActiveConnectionCountForTest()); |
243 | 353 |
244 auth_handler_->SendErrorAndCloseConnection(1); | 354 auth_handler_->SendErrorAndCloseConnection(1); |
| 355 WaitForConnectionClosed(&client_socket); |
| 356 |
245 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); | 357 ASSERT_EQ(0u, auth_handler_->GetActiveConnectionCountForTest()); |
246 } | 358 } |
247 | 359 |
248 } // namespace remoting | 360 } // namespace remoting |
OLD | NEW |