Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Side by Side Diff: net/socket/unix_domain_server_socket_posix_unittest.cc

Issue 376323002: Refactor unix domain socket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Applied comments, and shortened socket path for unittests not to overflow sockaddr in mac. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "net/socket/unix_domain_server_socket_posix.h"
6
7 #include <vector>
8
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"
20
21 namespace net {
22 namespace {
23
24 const char kSocketFilename[] = "socket_for_testing";
25 const char kInvalidSocketPath[] = "/invalid/path";
26
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;
32 }
33
34 UnixDomainServerSocket::AuthCallback CreateAuthCallback(bool allow_user) {
35 return base::Bind(&UserCanConnectCallback, allow_user);
36 }
37
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();
43 }
44
45 base::ScopedTempDir temp_dir_;
46 std::string socket_path_;
47 };
48
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));
55 }
56
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
68 }
69
70 TEST_F(UnixDomainServerSocketTest, AcceptWithForbiddenUser) {
71 const bool kUseAbstractNamespace = false;
72
73 UnixDomainServerSocket server_socket(CreateAuthCallback(false),
74 kUseAbstractNamespace);
75 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path_, 0, 1));
76
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);
82
83 UnixDomainClientSocket client_socket(socket_path_, kUseAbstractNamespace);
84 EXPECT_FALSE(client_socket.IsConnected());
85
86 // Connect() will return OK before the server rejects the connection.
87 TestCompletionCallback connect_callback;
88 int rv = client_socket.Connect(connect_callback.callback());
89 if (rv == ERR_IO_PENDING) {
90 rv = connect_callback.WaitForResult();
91 } else {
92 EXPECT_TRUE(client_socket.IsConnected());
93 }
94 EXPECT_EQ(OK, rv);
95
96 // Cannot use accept_callback.WaitForResult() because authentication error is
97 // invisible to the caller.
98 base::RunLoop().RunUntilIdle();
99 // Server disconnects the connection.
100 EXPECT_FALSE(client_socket.IsConnected());
101 // But, server didn't create the accepted socket.
102 EXPECT_FALSE(accepted_socket);
103
104 const int read_buffer_size = 10;
105 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size));
106 TestCompletionCallback read_callback;
107 EXPECT_EQ(0, /* EOF */
108 client_socket.Read(read_buffer, read_buffer_size,
109 read_callback.callback()));
110 }
111
112 // Normal cases including read/write are tested by UnixDomainClientSocketTest.
113
114 } // namespace
115 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698