Add integration browser tests for settings hardening.
[chromium-blink-merge.git] / net / socket / unix_domain_server_socket_posix_unittest.cc
blob07209f6605657757cd97378615cefb8d4de6f4ac
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"
7 #include <vector>
9 #include "base/bind.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"
21 namespace net {
22 namespace {
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);
31 return allow_user;
34 UnixDomainServerSocket::AuthCallback CreateAuthCallback(bool allow_user) {
35 return base::Bind(&UserCanConnectCallback, allow_user);
38 class UnixDomainServerSocketTest : public testing::Test {
39 protected:
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)
62 EXPECT_EQ(OK,
63 server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1));
64 #else
65 EXPECT_EQ(ERR_ADDRESS_INVALID,
66 server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1));
67 #endif
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()));
90 ASSERT_EQ(OK, rv);
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.
100 ASSERT_EQ(0, rv);
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.
111 } // namespace
112 } // namespace net