OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <vector> |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/files/file_path.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/stl_util.h" |
| 13 #include "net/base/io_buffer.h" |
| 14 #include "net/base/net_errors.h" |
| 15 #include "net/socket/unix_domain_client_socket_posix.h" |
| 16 #include "net/socket/unix_domain_server_socket_posix.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace net { |
| 20 |
| 21 namespace { |
| 22 |
| 23 const char kSocketFilename[] = "unix_domain_socket_for_testing"; |
| 24 const char kInvalidSocketPath[] = "/invalid/path"; |
| 25 |
| 26 std::string MakeSocketPathForTesting() { |
| 27 base::FilePath temp_dir; |
| 28 base::GetTempDir(&temp_dir); |
| 29 return temp_dir.Append(kSocketFilename).value(); |
| 30 } |
| 31 |
| 32 bool UserCanConnectCallback(bool allow_user, uid_t uid, gid_t gid) { |
| 33 return allow_user; |
| 34 } |
| 35 |
| 36 UnixDomainServerSocket::AuthCallback GetAuthCallback(bool allow_user) { |
| 37 return base::Bind(&UserCanConnectCallback, allow_user); |
| 38 } |
| 39 |
| 40 void CallbackWithExpectedResultValue(int expected_rv, int rv) { |
| 41 EXPECT_EQ(expected_rv, rv); |
| 42 } |
| 43 |
| 44 CompletionCallback GetCompletionCallback(int expected_rv) { |
| 45 return base::Bind(&CallbackWithExpectedResultValue, expected_rv); |
| 46 } |
| 47 |
| 48 } // namespace |
| 49 |
| 50 class UnixDomainServerSocketTest : public testing::Test { |
| 51 protected: |
| 52 virtual void SetUp() OVERRIDE { |
| 53 DeleteSocketPathForTesting(); |
| 54 } |
| 55 |
| 56 virtual void TearDown() OVERRIDE { |
| 57 DeleteSocketPathForTesting(); |
| 58 } |
| 59 |
| 60 void DeleteSocketPathForTesting() { |
| 61 base::DeleteFile(base::FilePath(MakeSocketPathForTesting()), false); |
| 62 } |
| 63 }; |
| 64 |
| 65 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidBacklog) { |
| 66 const std::string socket_path(MakeSocketPathForTesting()); |
| 67 const bool use_abstract_namespace = false; |
| 68 |
| 69 UnixDomainServerSocket server_socket(GetAuthCallback(true), |
| 70 use_abstract_namespace); |
| 71 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, -1)); |
| 72 |
| 73 UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace); |
| 74 EXPECT_FALSE(client_socket.IsConnected()); |
| 75 EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK))); |
| 76 EXPECT_TRUE(client_socket.IsConnected()); |
| 77 } |
| 78 |
| 79 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPath) { |
| 80 const std::string socket_path(kInvalidSocketPath); |
| 81 const bool use_abstract_namespace = false; |
| 82 UnixDomainServerSocket server_socket(GetAuthCallback(true), |
| 83 use_abstract_namespace); |
| 84 EXPECT_EQ(ERR_FILE_NOT_FOUND, |
| 85 server_socket.ListenWithAddressAndPort(socket_path, 0, 1)); |
| 86 } |
| 87 |
| 88 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPathWithAbstractNamespace) { |
| 89 const std::string socket_path(kInvalidSocketPath); |
| 90 const bool use_abstract_namespace = true; |
| 91 UnixDomainServerSocket server_socket(GetAuthCallback(true), |
| 92 use_abstract_namespace); |
| 93 #if defined(OS_ANDROID) || defined(OS_LINUX) |
| 94 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1)); |
| 95 #else |
| 96 EXPECT_EQ(ERR_ADDRESS_INVALID, |
| 97 server_socket.ListenWithAddressAndPort(socket_path, 0, 1)); |
| 98 #endif |
| 99 } |
| 100 |
| 101 TEST_F(UnixDomainServerSocketTest, AcceptWithForbiddenUser) { |
| 102 const std::string socket_path(MakeSocketPathForTesting()); |
| 103 const bool use_abstract_namespace = false; |
| 104 |
| 105 UnixDomainServerSocket server_socket(GetAuthCallback(false), |
| 106 use_abstract_namespace); |
| 107 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1)); |
| 108 |
| 109 scoped_ptr<StreamSocket> accepted_socket; |
| 110 EXPECT_EQ(ERR_IO_PENDING, server_socket.Accept(&accepted_socket, |
| 111 GetCompletionCallback(OK))); |
| 112 EXPECT_TRUE(!accepted_socket); |
| 113 |
| 114 UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace); |
| 115 EXPECT_FALSE(client_socket.IsConnected()); |
| 116 |
| 117 // Return success first because server socket accepts it. |
| 118 EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK))); |
| 119 EXPECT_TRUE(client_socket.IsConnected()); |
| 120 |
| 121 base::RunLoop().RunUntilIdle(); |
| 122 // Client misunderstood connected. |
| 123 EXPECT_TRUE(client_socket.IsConnected()); |
| 124 // But, server didn't create the accepted socket. |
| 125 EXPECT_TRUE(!accepted_socket); |
| 126 |
| 127 const int read_buffer_size = 10; |
| 128 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size)); |
| 129 EXPECT_EQ(0, /* EOF */ |
| 130 client_socket.Read(read_buffer, read_buffer_size, |
| 131 GetCompletionCallback(OK))); |
| 132 } |
| 133 |
| 134 // Normal cases including read/write are tested by UnixDomainClientSocketTest. |
| 135 |
| 136 } // namespace net |
OLD | NEW |