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 "net/base/io_buffer.h" | 14 #include "net/base/io_buffer.h" |
14 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
15 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
| 17 #include "net/socket/socket_libevent.h" |
16 #include "net/socket/unix_domain_server_socket_posix.h" | 18 #include "net/socket/unix_domain_server_socket_posix.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
18 | 20 |
19 namespace net { | 21 namespace net { |
20 namespace { | 22 namespace { |
21 | 23 |
22 const char kSocketFilename[] = "socket_for_testing"; | 24 const char kSocketFilename[] = "socket_for_testing"; |
23 | 25 |
24 bool UserCanConnectCallback( | 26 bool UserCanConnectCallback( |
25 bool allow_user, const UnixDomainServerSocket::Credentials& credentials) { | 27 bool allow_user, const UnixDomainServerSocket::Credentials& credentials) { |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 EXPECT_EQ(OK, ConnectSynchronously(&client_socket)); | 143 EXPECT_EQ(OK, ConnectSynchronously(&client_socket)); |
142 EXPECT_TRUE(client_socket.IsConnected()); | 144 EXPECT_TRUE(client_socket.IsConnected()); |
143 // Server has not yet been notified of the connection. | 145 // Server has not yet been notified of the connection. |
144 EXPECT_FALSE(accepted_socket); | 146 EXPECT_FALSE(accepted_socket); |
145 | 147 |
146 EXPECT_EQ(OK, accept_callback.WaitForResult()); | 148 EXPECT_EQ(OK, accept_callback.WaitForResult()); |
147 EXPECT_TRUE(accepted_socket); | 149 EXPECT_TRUE(accepted_socket); |
148 EXPECT_TRUE(accepted_socket->IsConnected()); | 150 EXPECT_TRUE(accepted_socket->IsConnected()); |
149 } | 151 } |
150 | 152 |
| 153 TEST_F(UnixDomainClientSocketTest, ConnectWithSocketDescriptor) { |
| 154 const bool kUseAbstractNamespace = false; |
| 155 |
| 156 UnixDomainServerSocket server_socket(CreateAuthCallback(true), |
| 157 kUseAbstractNamespace); |
| 158 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path_, 0, 1)); |
| 159 |
| 160 SocketDescriptor accepted_socket_fd = kInvalidSocket; |
| 161 TestCompletionCallback accept_callback; |
| 162 EXPECT_EQ(ERR_IO_PENDING, |
| 163 server_socket.AcceptSocketDescriptor(&accepted_socket_fd, |
| 164 accept_callback.callback())); |
| 165 EXPECT_EQ(kInvalidSocket, accepted_socket_fd); |
| 166 |
| 167 UnixDomainClientSocket client_socket(socket_path_, kUseAbstractNamespace); |
| 168 EXPECT_FALSE(client_socket.IsConnected()); |
| 169 |
| 170 EXPECT_EQ(OK, ConnectSynchronously(&client_socket)); |
| 171 EXPECT_TRUE(client_socket.IsConnected()); |
| 172 // Server has not yet been notified of the connection. |
| 173 EXPECT_EQ(kInvalidSocket, accepted_socket_fd); |
| 174 |
| 175 EXPECT_EQ(OK, accept_callback.WaitForResult()); |
| 176 EXPECT_NE(kInvalidSocket, accepted_socket_fd); |
| 177 |
| 178 SocketDescriptor client_socket_fd = client_socket.ReleaseConnectedSocket(); |
| 179 EXPECT_NE(kInvalidSocket, client_socket_fd); |
| 180 |
| 181 // Now, re-wrap client_socket_fd in a UnixDomainClientSocket and try a read |
| 182 // to be sure it hasn't gotten accidentally closed. |
| 183 SockaddrStorage addr; |
| 184 ASSERT_TRUE(UnixDomainClientSocket::FillAddress(socket_path_, false, &addr)); |
| 185 scoped_ptr<SocketLibevent> adopter(new SocketLibevent); |
| 186 adopter->AdoptConnectedSocket(client_socket_fd, addr); |
| 187 UnixDomainClientSocket rewrapped_socket(adopter.Pass()); |
| 188 EXPECT_TRUE(rewrapped_socket.IsConnected()); |
| 189 |
| 190 // Try to read data. |
| 191 const int kReadDataSize = 10; |
| 192 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(kReadDataSize)); |
| 193 TestCompletionCallback read_callback; |
| 194 EXPECT_EQ(ERR_IO_PENDING, |
| 195 rewrapped_socket.Read( |
| 196 read_buffer.get(), kReadDataSize, read_callback.callback())); |
| 197 |
| 198 EXPECT_EQ(0, IGNORE_EINTR(close(accepted_socket_fd))); |
| 199 } |
| 200 |
151 TEST_F(UnixDomainClientSocketTest, ConnectWithAbstractNamespace) { | 201 TEST_F(UnixDomainClientSocketTest, ConnectWithAbstractNamespace) { |
152 const bool kUseAbstractNamespace = true; | 202 const bool kUseAbstractNamespace = true; |
153 | 203 |
154 UnixDomainClientSocket client_socket(socket_path_, kUseAbstractNamespace); | 204 UnixDomainClientSocket client_socket(socket_path_, kUseAbstractNamespace); |
155 EXPECT_FALSE(client_socket.IsConnected()); | 205 EXPECT_FALSE(client_socket.IsConnected()); |
156 | 206 |
157 #if defined(OS_ANDROID) || defined(OS_LINUX) | 207 #if defined(OS_ANDROID) || defined(OS_LINUX) |
158 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | 208 UnixDomainServerSocket server_socket(CreateAuthCallback(true), |
159 kUseAbstractNamespace); | 209 kUseAbstractNamespace); |
160 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path_, 0, 1)); | 210 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path_, 0, 1)); |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 accepted_socket.get(), read_buffer.get(), kReadBufferSize, 0)); | 437 accepted_socket.get(), read_buffer.get(), kReadBufferSize, 0)); |
388 | 438 |
389 // Disconnect from server side after read-write. | 439 // Disconnect from server side after read-write. |
390 accepted_socket->Disconnect(); | 440 accepted_socket->Disconnect(); |
391 EXPECT_FALSE(accepted_socket->IsConnected()); | 441 EXPECT_FALSE(accepted_socket->IsConnected()); |
392 EXPECT_FALSE(client_socket.IsConnected()); | 442 EXPECT_FALSE(client_socket.IsConnected()); |
393 } | 443 } |
394 | 444 |
395 } // namespace | 445 } // namespace |
396 } // namespace net | 446 } // namespace net |
OLD | NEW |