| 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_server_socket_posix.h" | 5 #include "net/socket/unix_domain_server_socket_posix.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.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 "net/test/gtest_util.h" |
| 20 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 20 | 22 |
| 23 using net::test::IsError; |
| 24 using net::test::IsOk; |
| 25 |
| 21 namespace net { | 26 namespace net { |
| 22 namespace { | 27 namespace { |
| 23 | 28 |
| 24 const char kSocketFilename[] = "socket_for_testing"; | 29 const char kSocketFilename[] = "socket_for_testing"; |
| 25 const char kInvalidSocketPath[] = "/invalid/path"; | 30 const char kInvalidSocketPath[] = "/invalid/path"; |
| 26 | 31 |
| 27 bool UserCanConnectCallback(bool allow_user, | 32 bool UserCanConnectCallback(bool allow_user, |
| 28 const UnixDomainServerSocket::Credentials& credentials) { | 33 const UnixDomainServerSocket::Credentials& credentials) { |
| 29 // Here peers are running in same process. | 34 // Here peers are running in same process. |
| 30 #if defined(OS_LINUX) || defined(OS_ANDROID) | 35 #if defined(OS_LINUX) || defined(OS_ANDROID) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 56 kUseAbstractNamespace); | 61 kUseAbstractNamespace); |
| 57 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 62 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
| 58 server_socket.BindAndListen(kInvalidSocketPath, /*backlog=*/1)); | 63 server_socket.BindAndListen(kInvalidSocketPath, /*backlog=*/1)); |
| 59 } | 64 } |
| 60 | 65 |
| 61 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPathWithAbstractNamespace) { | 66 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPathWithAbstractNamespace) { |
| 62 const bool kUseAbstractNamespace = true; | 67 const bool kUseAbstractNamespace = true; |
| 63 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | 68 UnixDomainServerSocket server_socket(CreateAuthCallback(true), |
| 64 kUseAbstractNamespace); | 69 kUseAbstractNamespace); |
| 65 #if defined(OS_ANDROID) || defined(OS_LINUX) | 70 #if defined(OS_ANDROID) || defined(OS_LINUX) |
| 66 EXPECT_EQ(OK, server_socket.BindAndListen(kInvalidSocketPath, /*backlog=*/1)); | 71 EXPECT_THAT(server_socket.BindAndListen(kInvalidSocketPath, /*backlog=*/1), |
| 72 IsOk()); |
| 67 #else | 73 #else |
| 68 EXPECT_EQ(ERR_ADDRESS_INVALID, | 74 EXPECT_EQ(ERR_ADDRESS_INVALID, |
| 69 server_socket.BindAndListen(kInvalidSocketPath, /*backlog=*/1)); | 75 server_socket.BindAndListen(kInvalidSocketPath, /*backlog=*/1)); |
| 70 #endif | 76 #endif |
| 71 } | 77 } |
| 72 | 78 |
| 73 TEST_F(UnixDomainServerSocketTest, ListenAgainAfterFailureWithInvalidPath) { | 79 TEST_F(UnixDomainServerSocketTest, ListenAgainAfterFailureWithInvalidPath) { |
| 74 const bool kUseAbstractNamespace = false; | 80 const bool kUseAbstractNamespace = false; |
| 75 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | 81 UnixDomainServerSocket server_socket(CreateAuthCallback(true), |
| 76 kUseAbstractNamespace); | 82 kUseAbstractNamespace); |
| 77 EXPECT_EQ(ERR_FILE_NOT_FOUND, | 83 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
| 78 server_socket.BindAndListen(kInvalidSocketPath, /*backlog=*/1)); | 84 server_socket.BindAndListen(kInvalidSocketPath, /*backlog=*/1)); |
| 79 EXPECT_EQ(OK, server_socket.BindAndListen(socket_path_, /*backlog=*/1)); | 85 EXPECT_THAT(server_socket.BindAndListen(socket_path_, /*backlog=*/1), IsOk()); |
| 80 } | 86 } |
| 81 | 87 |
| 82 TEST_F(UnixDomainServerSocketTest, AcceptWithForbiddenUser) { | 88 TEST_F(UnixDomainServerSocketTest, AcceptWithForbiddenUser) { |
| 83 const bool kUseAbstractNamespace = false; | 89 const bool kUseAbstractNamespace = false; |
| 84 | 90 |
| 85 UnixDomainServerSocket server_socket(CreateAuthCallback(false), | 91 UnixDomainServerSocket server_socket(CreateAuthCallback(false), |
| 86 kUseAbstractNamespace); | 92 kUseAbstractNamespace); |
| 87 EXPECT_EQ(OK, server_socket.BindAndListen(socket_path_, /*backlog=*/1)); | 93 EXPECT_THAT(server_socket.BindAndListen(socket_path_, /*backlog=*/1), IsOk()); |
| 88 | 94 |
| 89 std::unique_ptr<StreamSocket> accepted_socket; | 95 std::unique_ptr<StreamSocket> accepted_socket; |
| 90 TestCompletionCallback accept_callback; | 96 TestCompletionCallback accept_callback; |
| 91 EXPECT_EQ(ERR_IO_PENDING, | 97 EXPECT_EQ(ERR_IO_PENDING, |
| 92 server_socket.Accept(&accepted_socket, accept_callback.callback())); | 98 server_socket.Accept(&accepted_socket, accept_callback.callback())); |
| 93 EXPECT_FALSE(accepted_socket); | 99 EXPECT_FALSE(accepted_socket); |
| 94 | 100 |
| 95 UnixDomainClientSocket client_socket(socket_path_, kUseAbstractNamespace); | 101 UnixDomainClientSocket client_socket(socket_path_, kUseAbstractNamespace); |
| 96 EXPECT_FALSE(client_socket.IsConnected()); | 102 EXPECT_FALSE(client_socket.IsConnected()); |
| 97 | 103 |
| 98 // Connect() will return OK before the server rejects the connection. | 104 // Connect() will return OK before the server rejects the connection. |
| 99 TestCompletionCallback connect_callback; | 105 TestCompletionCallback connect_callback; |
| 100 int rv = connect_callback.GetResult( | 106 int rv = connect_callback.GetResult( |
| 101 client_socket.Connect(connect_callback.callback())); | 107 client_socket.Connect(connect_callback.callback())); |
| 102 ASSERT_EQ(OK, rv); | 108 ASSERT_THAT(rv, IsOk()); |
| 103 | 109 |
| 104 // Try to read from the socket. | 110 // Try to read from the socket. |
| 105 const int read_buffer_size = 10; | 111 const int read_buffer_size = 10; |
| 106 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size)); | 112 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size)); |
| 107 TestCompletionCallback read_callback; | 113 TestCompletionCallback read_callback; |
| 108 rv = read_callback.GetResult(client_socket.Read( | 114 rv = read_callback.GetResult(client_socket.Read( |
| 109 read_buffer.get(), read_buffer_size, read_callback.callback())); | 115 read_buffer.get(), read_buffer_size, read_callback.callback())); |
| 110 | 116 |
| 111 // The server should have disconnected gracefully, without sending any data. | 117 // The server should have disconnected gracefully, without sending any data. |
| 112 ASSERT_EQ(0, rv); | 118 ASSERT_EQ(0, rv); |
| 113 EXPECT_FALSE(client_socket.IsConnected()); | 119 EXPECT_FALSE(client_socket.IsConnected()); |
| 114 | 120 |
| 115 // The server socket should not have called |accept_callback| or modified | 121 // The server socket should not have called |accept_callback| or modified |
| 116 // |accepted_socket|. | 122 // |accepted_socket|. |
| 117 EXPECT_FALSE(accept_callback.have_result()); | 123 EXPECT_FALSE(accept_callback.have_result()); |
| 118 EXPECT_FALSE(accepted_socket); | 124 EXPECT_FALSE(accepted_socket); |
| 119 } | 125 } |
| 120 | 126 |
| 121 TEST_F(UnixDomainServerSocketTest, UnimplementedMethodsFail) { | 127 TEST_F(UnixDomainServerSocketTest, UnimplementedMethodsFail) { |
| 122 const bool kUseAbstractNamespace = false; | 128 const bool kUseAbstractNamespace = false; |
| 123 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | 129 UnixDomainServerSocket server_socket(CreateAuthCallback(true), |
| 124 kUseAbstractNamespace); | 130 kUseAbstractNamespace); |
| 125 | 131 |
| 126 IPEndPoint ep; | 132 IPEndPoint ep; |
| 127 EXPECT_EQ(ERR_NOT_IMPLEMENTED, server_socket.Listen(ep, 0)); | 133 EXPECT_THAT(server_socket.Listen(ep, 0), IsError(ERR_NOT_IMPLEMENTED)); |
| 128 EXPECT_EQ(ERR_NOT_IMPLEMENTED, | 134 EXPECT_EQ(ERR_NOT_IMPLEMENTED, |
| 129 server_socket.ListenWithAddressAndPort(kInvalidSocketPath, | 135 server_socket.ListenWithAddressAndPort(kInvalidSocketPath, |
| 130 0, | 136 0, |
| 131 /*backlog=*/1)); | 137 /*backlog=*/1)); |
| 132 | 138 |
| 133 EXPECT_EQ(ERR_ADDRESS_INVALID, server_socket.GetLocalAddress(&ep)); | 139 EXPECT_THAT(server_socket.GetLocalAddress(&ep), IsError(ERR_ADDRESS_INVALID)); |
| 134 } | 140 } |
| 135 | 141 |
| 136 // Normal cases including read/write are tested by UnixDomainClientSocketTest. | 142 // Normal cases including read/write are tested by UnixDomainClientSocketTest. |
| 137 | 143 |
| 138 } // namespace | 144 } // namespace |
| 139 } // namespace net | 145 } // namespace net |
| OLD | NEW |