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 "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 | |
23 namespace { | |
24 | |
25 const char kSocketFilename[] = "unix_domain_socket_for_testing"; | |
26 const char kInvalidSocketPath[] = "/invalid/path"; | |
27 | |
28 bool UserCanConnectCallback(bool allow_user, uid_t uid, gid_t gid) { | |
29 // Here peers are running in same process. Check if they are expected. | |
30 EXPECT_EQ(getuid(), uid); | |
31 EXPECT_EQ(getgid(), gid); | |
32 return allow_user; | |
33 } | |
34 | |
35 UnixDomainServerSocket::AuthCallback CreateAuthCallback(bool allow_user) { | |
36 return base::Bind(&UserCanConnectCallback, allow_user); | |
37 } | |
38 | |
39 } // namespace | |
40 | |
41 class UnixDomainServerSocketTest : public testing::Test { | |
42 protected: | |
43 UnixDomainServerSocketTest() { | |
44 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
45 socket_path_ = temp_dir_.path().Append(kSocketFilename).value(); | |
46 } | |
47 | |
48 base::ScopedTempDir temp_dir_; | |
49 std::string socket_path_; | |
50 }; | |
51 | |
52 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPath) { | |
53 const bool kUseAbstractNamespace = false; | |
54 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | |
55 kUseAbstractNamespace); | |
56 EXPECT_EQ(ERR_FILE_NOT_FOUND, | |
57 server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1)); | |
58 } | |
59 | |
60 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPathWithAbstractNamespace) { | |
61 const bool kUseAbstractNamespace = true; | |
62 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | |
63 kUseAbstractNamespace); | |
64 #if defined(OS_ANDROID) || defined(OS_LINUX) | |
65 EXPECT_EQ(OK, | |
66 server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1)); | |
67 #else | |
68 EXPECT_EQ(ERR_ADDRESS_INVALID, | |
69 server_socket.ListenWithAddressAndPort(kInvalidSocketPath, 0, 1)); | |
70 #endif | |
71 } | |
72 | |
73 TEST_F(UnixDomainServerSocketTest, AcceptWithForbiddenUser) { | |
74 const bool kUseAbstractNamespace = false; | |
75 | |
76 UnixDomainServerSocket server_socket(CreateAuthCallback(false), | |
77 kUseAbstractNamespace); | |
78 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path_, 0, 1)); | |
79 | |
80 scoped_ptr<StreamSocket> accepted_socket; | |
81 TestCompletionCallback accept_callback; | |
82 EXPECT_EQ(ERR_IO_PENDING, | |
83 server_socket.Accept(&accepted_socket, accept_callback.callback())); | |
84 EXPECT_FALSE(accepted_socket); | |
85 | |
86 UnixDomainClientSocket client_socket(socket_path_, kUseAbstractNamespace); | |
87 EXPECT_FALSE(client_socket.IsConnected()); | |
88 | |
89 // Return success first because server socket accepts it. | |
mmenke
2014/07/16 18:30:36
nit: Maybe "Connect will return OK, before the se
byungchul
2014/07/16 22:02:31
Done.
| |
90 TestCompletionCallback connect_callback; | |
91 int rv = client_socket.Connect(connect_callback.callback()); | |
92 if (rv == ERR_IO_PENDING) { | |
93 rv = connect_callback.WaitForResult(); | |
94 } else { | |
95 EXPECT_TRUE(client_socket.IsConnected()); | |
96 } | |
97 EXPECT_EQ(OK, rv); | |
98 | |
99 // Cannot use accept_callback.WaitForResult() because authentication error is | |
100 // invisible to the caller. | |
101 base::RunLoop().RunUntilIdle(); | |
102 // Server disconnects the connection. | |
103 EXPECT_FALSE(client_socket.IsConnected()); | |
104 // But, server didn't create the accepted socket. | |
105 EXPECT_FALSE(accepted_socket); | |
106 | |
107 const int read_buffer_size = 10; | |
108 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size)); | |
109 TestCompletionCallback read_callback; | |
110 EXPECT_EQ(0, /* EOF */ | |
111 client_socket.Read(read_buffer, read_buffer_size, | |
112 read_callback.callback())); | |
113 } | |
114 | |
115 // Normal cases including read/write are tested by UnixDomainClientSocketTest. | |
116 | |
117 } // namespace net | |
OLD | NEW |