OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "net/socket/unix_domain_client_socket_posix.h" | 5 #include "net/socket/unix_domain_client_socket_posix.h" |
6 | 6 |
7 #include <unistd.h> | 7 #include <unistd.h> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
11 #include "base/files/scoped_temp_dir.h" | 11 #include "base/files/scoped_temp_dir.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/posix/eintr_wrapper.h" | 13 #include "base/posix/eintr_wrapper.h" |
14 #include "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
16 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
17 #include "net/socket/socket_libevent.h" | 17 #include "net/socket/socket_posix.h" |
18 #include "net/socket/unix_domain_server_socket_posix.h" | 18 #include "net/socket/unix_domain_server_socket_posix.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace net { | 21 namespace net { |
22 namespace { | 22 namespace { |
23 | 23 |
24 const char kSocketFilename[] = "socket_for_testing"; | 24 const char kSocketFilename[] = "socket_for_testing"; |
25 | 25 |
26 bool UserCanConnectCallback( | 26 bool UserCanConnectCallback( |
27 bool allow_user, const UnixDomainServerSocket::Credentials& credentials) { | 27 bool allow_user, const UnixDomainServerSocket::Credentials& credentials) { |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 EXPECT_EQ(OK, accept_callback.WaitForResult()); | 175 EXPECT_EQ(OK, accept_callback.WaitForResult()); |
176 EXPECT_NE(kInvalidSocket, accepted_socket_fd); | 176 EXPECT_NE(kInvalidSocket, accepted_socket_fd); |
177 | 177 |
178 SocketDescriptor client_socket_fd = client_socket.ReleaseConnectedSocket(); | 178 SocketDescriptor client_socket_fd = client_socket.ReleaseConnectedSocket(); |
179 EXPECT_NE(kInvalidSocket, client_socket_fd); | 179 EXPECT_NE(kInvalidSocket, client_socket_fd); |
180 | 180 |
181 // Now, re-wrap client_socket_fd in a UnixDomainClientSocket and try a read | 181 // Now, re-wrap client_socket_fd in a UnixDomainClientSocket and try a read |
182 // to be sure it hasn't gotten accidentally closed. | 182 // to be sure it hasn't gotten accidentally closed. |
183 SockaddrStorage addr; | 183 SockaddrStorage addr; |
184 ASSERT_TRUE(UnixDomainClientSocket::FillAddress(socket_path_, false, &addr)); | 184 ASSERT_TRUE(UnixDomainClientSocket::FillAddress(socket_path_, false, &addr)); |
185 scoped_ptr<SocketLibevent> adopter(new SocketLibevent); | 185 scoped_ptr<SocketPosix> adopter(new SocketPosix); |
186 adopter->AdoptConnectedSocket(client_socket_fd, addr); | 186 adopter->AdoptConnectedSocket(client_socket_fd, addr); |
187 UnixDomainClientSocket rewrapped_socket(adopter.Pass()); | 187 UnixDomainClientSocket rewrapped_socket(adopter.Pass()); |
188 EXPECT_TRUE(rewrapped_socket.IsConnected()); | 188 EXPECT_TRUE(rewrapped_socket.IsConnected()); |
189 | 189 |
190 // Try to read data. | 190 // Try to read data. |
191 const int kReadDataSize = 10; | 191 const int kReadDataSize = 10; |
192 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadDataSize)); | 192 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadDataSize)); |
193 TestCompletionCallback read_callback; | 193 TestCompletionCallback read_callback; |
194 EXPECT_EQ(ERR_IO_PENDING, | 194 EXPECT_EQ(ERR_IO_PENDING, |
195 rewrapped_socket.Read( | 195 rewrapped_socket.Read( |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 read_buffer.get(), kReadDataSize, read_callback.callback())); | 274 read_buffer.get(), kReadDataSize, read_callback.callback())); |
275 | 275 |
276 // Disconnect from client side. | 276 // Disconnect from client side. |
277 client_socket.Disconnect(); | 277 client_socket.Disconnect(); |
278 EXPECT_FALSE(client_socket.IsConnected()); | 278 EXPECT_FALSE(client_socket.IsConnected()); |
279 EXPECT_FALSE(accepted_socket->IsConnected()); | 279 EXPECT_FALSE(accepted_socket->IsConnected()); |
280 | 280 |
281 // Connection closed by peer. | 281 // Connection closed by peer. |
282 EXPECT_EQ(0 /* EOF */, read_callback.WaitForResult()); | 282 EXPECT_EQ(0 /* EOF */, read_callback.WaitForResult()); |
283 // Note that read callback won't be called when the connection is closed | 283 // Note that read callback won't be called when the connection is closed |
284 // locally before the peer closes it. SocketLibevent just clears callbacks. | 284 // locally before the peer closes it. SocketPosix just clears callbacks. |
285 } | 285 } |
286 | 286 |
287 TEST_F(UnixDomainClientSocketTest, DisconnectFromServer) { | 287 TEST_F(UnixDomainClientSocketTest, DisconnectFromServer) { |
288 UnixDomainServerSocket server_socket(CreateAuthCallback(true), false); | 288 UnixDomainServerSocket server_socket(CreateAuthCallback(true), false); |
289 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path_, 0, 1)); | 289 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path_, 0, 1)); |
290 scoped_ptr<StreamSocket> accepted_socket; | 290 scoped_ptr<StreamSocket> accepted_socket; |
291 TestCompletionCallback accept_callback; | 291 TestCompletionCallback accept_callback; |
292 EXPECT_EQ(ERR_IO_PENDING, | 292 EXPECT_EQ(ERR_IO_PENDING, |
293 server_socket.Accept(&accepted_socket, accept_callback.callback())); | 293 server_socket.Accept(&accepted_socket, accept_callback.callback())); |
294 UnixDomainClientSocket client_socket(socket_path_, false); | 294 UnixDomainClientSocket client_socket(socket_path_, false); |
(...skipping 12 matching lines...) Expand all Loading... |
307 read_buffer.get(), kReadDataSize, read_callback.callback())); | 307 read_buffer.get(), kReadDataSize, read_callback.callback())); |
308 | 308 |
309 // Disconnect from server side. | 309 // Disconnect from server side. |
310 accepted_socket->Disconnect(); | 310 accepted_socket->Disconnect(); |
311 EXPECT_FALSE(accepted_socket->IsConnected()); | 311 EXPECT_FALSE(accepted_socket->IsConnected()); |
312 EXPECT_FALSE(client_socket.IsConnected()); | 312 EXPECT_FALSE(client_socket.IsConnected()); |
313 | 313 |
314 // Connection closed by peer. | 314 // Connection closed by peer. |
315 EXPECT_EQ(0 /* EOF */, read_callback.WaitForResult()); | 315 EXPECT_EQ(0 /* EOF */, read_callback.WaitForResult()); |
316 // Note that read callback won't be called when the connection is closed | 316 // Note that read callback won't be called when the connection is closed |
317 // locally before the peer closes it. SocketLibevent just clears callbacks. | 317 // locally before the peer closes it. SocketPosix just clears callbacks. |
318 } | 318 } |
319 | 319 |
320 TEST_F(UnixDomainClientSocketTest, ReadAfterWrite) { | 320 TEST_F(UnixDomainClientSocketTest, ReadAfterWrite) { |
321 UnixDomainServerSocket server_socket(CreateAuthCallback(true), false); | 321 UnixDomainServerSocket server_socket(CreateAuthCallback(true), false); |
322 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path_, 0, 1)); | 322 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path_, 0, 1)); |
323 scoped_ptr<StreamSocket> accepted_socket; | 323 scoped_ptr<StreamSocket> accepted_socket; |
324 TestCompletionCallback accept_callback; | 324 TestCompletionCallback accept_callback; |
325 EXPECT_EQ(ERR_IO_PENDING, | 325 EXPECT_EQ(ERR_IO_PENDING, |
326 server_socket.Accept(&accepted_socket, accept_callback.callback())); | 326 server_socket.Accept(&accepted_socket, accept_callback.callback())); |
327 UnixDomainClientSocket client_socket(socket_path_, false); | 327 UnixDomainClientSocket client_socket(socket_path_, false); |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 accepted_socket.get(), read_buffer.get(), kReadBufferSize, 0)); | 437 accepted_socket.get(), read_buffer.get(), kReadBufferSize, 0)); |
438 | 438 |
439 // Disconnect from server side after read-write. | 439 // Disconnect from server side after read-write. |
440 accepted_socket->Disconnect(); | 440 accepted_socket->Disconnect(); |
441 EXPECT_FALSE(accepted_socket->IsConnected()); | 441 EXPECT_FALSE(accepted_socket->IsConnected()); |
442 EXPECT_FALSE(client_socket.IsConnected()); | 442 EXPECT_FALSE(client_socket.IsConnected()); |
443 } | 443 } |
444 | 444 |
445 } // namespace | 445 } // namespace |
446 } // namespace net | 446 } // namespace net |
OLD | NEW |