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.
5 #include "net/socket/unix_domain_server_socket_posix.h"
10 #include "base/files/file_path.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/run_loop.h"
14 #include "base/stl_util.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/test_completion_callback.h"
18 #include "net/socket/unix_domain_client_socket_posix.h"
19 #include "testing/gtest/include/gtest/gtest.h"
24 const char kSocketFilename
[] = "socket_for_testing";
25 const char kInvalidSocketPath
[] = "/invalid/path";
27 bool UserCanConnectCallback(bool allow_user
, uid_t uid
, gid_t gid
) {
28 // Here peers are running in same process.
29 EXPECT_EQ(getuid(), uid
);
30 EXPECT_EQ(getgid(), gid
);
34 UnixDomainServerSocket::AuthCallback
CreateAuthCallback(bool allow_user
) {
35 return base::Bind(&UserCanConnectCallback
, allow_user
);
38 class UnixDomainServerSocketTest
: public testing::Test
{
40 UnixDomainServerSocketTest() {
41 EXPECT_TRUE(temp_dir_
.CreateUniqueTempDir());
42 socket_path_
= temp_dir_
.path().Append(kSocketFilename
).value();
45 base::ScopedTempDir temp_dir_
;
46 std::string socket_path_
;
49 TEST_F(UnixDomainServerSocketTest
, ListenWithInvalidPath
) {
50 const bool kUseAbstractNamespace
= false;
51 UnixDomainServerSocket
server_socket(CreateAuthCallback(true),
52 kUseAbstractNamespace
);
53 EXPECT_EQ(ERR_FILE_NOT_FOUND
,
54 server_socket
.ListenWithAddressAndPort(kInvalidSocketPath
, 0, 1));
57 TEST_F(UnixDomainServerSocketTest
, ListenWithInvalidPathWithAbstractNamespace
) {
58 const bool kUseAbstractNamespace
= true;
59 UnixDomainServerSocket
server_socket(CreateAuthCallback(true),
60 kUseAbstractNamespace
);
61 #if defined(OS_ANDROID) || defined(OS_LINUX)
63 server_socket
.ListenWithAddressAndPort(kInvalidSocketPath
, 0, 1));
65 EXPECT_EQ(ERR_ADDRESS_INVALID
,
66 server_socket
.ListenWithAddressAndPort(kInvalidSocketPath
, 0, 1));
70 TEST_F(UnixDomainServerSocketTest
, AcceptWithForbiddenUser
) {
71 const bool kUseAbstractNamespace
= false;
73 UnixDomainServerSocket
server_socket(CreateAuthCallback(false),
74 kUseAbstractNamespace
);
75 EXPECT_EQ(OK
, server_socket
.ListenWithAddressAndPort(socket_path_
, 0, 1));
77 scoped_ptr
<StreamSocket
> accepted_socket
;
78 TestCompletionCallback accept_callback
;
79 EXPECT_EQ(ERR_IO_PENDING
,
80 server_socket
.Accept(&accepted_socket
, accept_callback
.callback()));
81 EXPECT_FALSE(accepted_socket
);
83 UnixDomainClientSocket
client_socket(socket_path_
, kUseAbstractNamespace
);
84 EXPECT_FALSE(client_socket
.IsConnected());
86 // Connect() will return OK before the server rejects the connection.
87 TestCompletionCallback connect_callback
;
88 int rv
= connect_callback
.GetResult(
89 client_socket
.Connect(connect_callback
.callback()));
92 // Try to read from the socket.
93 const int read_buffer_size
= 10;
94 scoped_refptr
<IOBuffer
> read_buffer(new IOBuffer(read_buffer_size
));
95 TestCompletionCallback read_callback
;
96 rv
= read_callback
.GetResult(client_socket
.Read(read_buffer
, read_buffer_size
,
97 read_callback
.callback()));
99 // The server should have disconnected gracefully, without sending any data.
101 EXPECT_FALSE(client_socket
.IsConnected());
103 // The server socket should not have called |accept_callback| or modified
104 // |accepted_socket|.
105 EXPECT_FALSE(accept_callback
.have_result());
106 EXPECT_FALSE(accepted_socket
);
109 // Normal cases including read/write are tested by UnixDomainClientSocketTest.