OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <errno.h> | 5 #include <errno.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <poll.h> | 7 #include <poll.h> |
8 #include <sys/socket.h> | 8 #include <sys/socket.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/time.h> | 10 #include <sys/time.h> |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "net/socket/unix_domain_socket_posix.h" | 32 #include "net/socket/unix_domain_socket_posix.h" |
33 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
34 | 34 |
35 using std::queue; | 35 using std::queue; |
36 using std::string; | 36 using std::string; |
37 | 37 |
38 namespace net { | 38 namespace net { |
39 namespace { | 39 namespace { |
40 | 40 |
41 const char kSocketFilename[] = "unix_domain_socket_for_testing"; | 41 const char kSocketFilename[] = "unix_domain_socket_for_testing"; |
| 42 const char kFallbackSocketName[] = "unix_domain_socket_for_testing_2"; |
42 const char kInvalidSocketPath[] = "/invalid/path"; | 43 const char kInvalidSocketPath[] = "/invalid/path"; |
43 const char kMsg[] = "hello"; | 44 const char kMsg[] = "hello"; |
44 | 45 |
45 enum EventType { | 46 enum EventType { |
46 EVENT_ACCEPT, | 47 EVENT_ACCEPT, |
47 EVENT_AUTH_DENIED, | 48 EVENT_AUTH_DENIED, |
48 EVENT_AUTH_GRANTED, | 49 EVENT_AUTH_GRANTED, |
49 EVENT_CLOSE, | 50 EVENT_CLOSE, |
50 EVENT_LISTEN, | 51 EVENT_LISTEN, |
51 EVENT_READ, | 52 EVENT_READ, |
52 }; | 53 }; |
53 | 54 |
54 string MakeSocketPath() { | 55 string MakeSocketPath(const string& socket_file_name) { |
55 base::FilePath temp_dir; | 56 base::FilePath temp_dir; |
56 file_util::GetTempDir(&temp_dir); | 57 file_util::GetTempDir(&temp_dir); |
57 return temp_dir.Append(kSocketFilename).value(); | 58 return temp_dir.Append(socket_file_name).value(); |
| 59 } |
| 60 |
| 61 string MakeSocketPath() { |
| 62 return MakeSocketPath(kSocketFilename); |
58 } | 63 } |
59 | 64 |
60 class EventManager : public base::RefCounted<EventManager> { | 65 class EventManager : public base::RefCounted<EventManager> { |
61 public: | 66 public: |
62 EventManager() : condition_(&mutex_) {} | 67 EventManager() : condition_(&mutex_) {} |
63 | 68 |
64 bool HasPendingEvent() { | 69 bool HasPendingEvent() { |
65 base::AutoLock lock(mutex_); | 70 base::AutoLock lock(mutex_); |
66 return !events_.empty(); | 71 return !events_.empty(); |
67 } | 72 } |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 CreateAndListen(); | 252 CreateAndListen(); |
248 EXPECT_TRUE(socket_.get() == NULL); | 253 EXPECT_TRUE(socket_.get() == NULL); |
249 } | 254 } |
250 | 255 |
251 #ifdef SOCKET_ABSTRACT_NAMESPACE_SUPPORTED | 256 #ifdef SOCKET_ABSTRACT_NAMESPACE_SUPPORTED |
252 // Test with an invalid path to make sure that the socket is not backed by a | 257 // Test with an invalid path to make sure that the socket is not backed by a |
253 // file. | 258 // file. |
254 TEST_F(UnixDomainSocketTestWithInvalidPath, | 259 TEST_F(UnixDomainSocketTestWithInvalidPath, |
255 CreateAndListenWithAbstractNamespace) { | 260 CreateAndListenWithAbstractNamespace) { |
256 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( | 261 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( |
257 file_path_.value(), socket_delegate_.get(), MakeAuthCallback()); | 262 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); |
258 EXPECT_FALSE(socket_.get() == NULL); | 263 EXPECT_FALSE(socket_.get() == NULL); |
259 } | 264 } |
| 265 |
| 266 TEST_F(UnixDomainSocketTest, TestFallbackName) { |
| 267 scoped_refptr<UnixDomainSocket> existing_socket = |
| 268 UnixDomainSocket::CreateAndListenWithAbstractNamespace( |
| 269 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); |
| 270 EXPECT_FALSE(existing_socket.get() == NULL); |
| 271 // First, try to bind socket with the same name with no fallback name. |
| 272 socket_ = |
| 273 UnixDomainSocket::CreateAndListenWithAbstractNamespace( |
| 274 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); |
| 275 EXPECT_TRUE(socket_.get() == NULL); |
| 276 // Now with a fallback name. |
| 277 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( |
| 278 file_path_.value(), |
| 279 MakeSocketPath(kFallbackSocketName), |
| 280 socket_delegate_.get(), |
| 281 MakeAuthCallback()); |
| 282 EXPECT_FALSE(socket_.get() == NULL); |
| 283 existing_socket = NULL; |
| 284 } |
260 #endif | 285 #endif |
261 | 286 |
262 TEST_F(UnixDomainSocketTest, TestWithClient) { | 287 TEST_F(UnixDomainSocketTest, TestWithClient) { |
263 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); | 288 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); |
264 EventType event = event_manager_->WaitForEvent(); | 289 EventType event = event_manager_->WaitForEvent(); |
265 ASSERT_EQ(EVENT_LISTEN, event); | 290 ASSERT_EQ(EVENT_LISTEN, event); |
266 | 291 |
267 // Create the client socket. | 292 // Create the client socket. |
268 const SocketDescriptor sock = CreateClientSocket(); | 293 const SocketDescriptor sock = CreateClientSocket(); |
269 ASSERT_NE(StreamListenSocket::kInvalidSocket, sock); | 294 ASSERT_NE(StreamListenSocket::kInvalidSocket, sock); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 | 329 |
305 // Send() must fail. | 330 // Send() must fail. |
306 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); | 331 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); |
307 ASSERT_EQ(-1, ret); | 332 ASSERT_EQ(-1, ret); |
308 ASSERT_EQ(EPIPE, errno); | 333 ASSERT_EQ(EPIPE, errno); |
309 ASSERT_FALSE(event_manager_->HasPendingEvent()); | 334 ASSERT_FALSE(event_manager_->HasPendingEvent()); |
310 } | 335 } |
311 | 336 |
312 } // namespace | 337 } // namespace |
313 } // namespace net | 338 } // namespace net |
OLD | NEW |