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. | |
90 TestCompletionCallback connect_callback; | |
91 int rv = client_socket.Connect(connect_callback.callback()); | |
92 if (rv == ERR_IO_PENDING) | |
93 rv = connect_callback.WaitForResult(); | |
mmenke
2014/07/15 15:25:56
If this case ever happens, I think the IsConnected
byungchul
2014/07/15 17:30:45
Sorry, I don't understand. How could IsConnect() f
mmenke
2014/07/15 17:44:41
If in WaitForResult, the accept callback is also c
byungchul
2014/07/15 18:07:01
Even if this happens, accept_callback will not be
mmenke
2014/07/15 19:12:57
I'm not concerned about accept_socket not being NU
byungchul
2014/07/15 22:01:18
Oh, now I understand what you meant. Done.
| |
94 EXPECT_EQ(OK, rv); | |
95 EXPECT_TRUE(client_socket.IsConnected()); | |
96 | |
97 // Cannot use accept_callback.WaitForResult() because authentication error is | |
98 // invisible to the caller. | |
99 base::RunLoop().RunUntilIdle(); | |
100 // Server disconnects the connection. | |
101 EXPECT_FALSE(client_socket.IsConnected()); | |
102 // But, server didn't create the accepted socket. | |
103 EXPECT_FALSE(accepted_socket); | |
104 | |
105 const int read_buffer_size = 10; | |
106 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size)); | |
107 TestCompletionCallback read_callback; | |
108 EXPECT_EQ(0, /* EOF */ | |
109 client_socket.Read(read_buffer, read_buffer_size, | |
110 read_callback.callback())); | |
111 } | |
112 | |
113 // Normal cases including read/write are tested by UnixDomainClientSocketTest. | |
114 | |
115 } // namespace net | |
OLD | NEW |