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 "mojo/shell/domain_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/message_loop/message_loop.h" | |
14 #include "base/run_loop.h" | |
15 #include "base/stl_util.h" | |
16 #include "mojo/shell/domain_socket/net_errors.h" | |
17 #include "mojo/shell/domain_socket/test_completion_callback.h" | |
18 #include "mojo/shell/domain_socket/unix_domain_client_socket_posix.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | |
20 | |
21 namespace mojo { | |
22 namespace shell { | |
23 namespace { | |
24 | |
25 const char kSocketFilename[] = "socket_for_testing"; | |
26 const char kInvalidSocketPath[] = "/invalid/path"; | |
27 | |
28 bool UserCanConnectCallback( | |
29 bool allow_user, | |
30 const UnixDomainServerSocket::Credentials& credentials) { | |
31 // Here peers are running in same process. | |
32 EXPECT_EQ(getpid(), credentials.process_id); | |
33 EXPECT_EQ(getuid(), credentials.user_id); | |
34 EXPECT_EQ(getgid(), credentials.group_id); | |
35 return allow_user; | |
36 } | |
37 | |
38 UnixDomainServerSocket::AuthCallback CreateAuthCallback(bool allow_user) { | |
39 return base::Bind(&UserCanConnectCallback, allow_user); | |
40 } | |
41 } // namespace | |
42 | |
43 class UnixDomainServerSocketTest : public testing::Test { | |
44 protected: | |
45 UnixDomainServerSocketTest() { | |
46 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); | |
47 socket_path_ = temp_dir_.path().Append(kSocketFilename).value(); | |
48 } | |
49 | |
50 base::ScopedTempDir temp_dir_; | |
51 std::string socket_path_; | |
52 base::MessageLoopForIO loop_; | |
53 }; | |
54 | |
55 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPath) { | |
56 const bool kUseAbstractNamespace = false; | |
57 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | |
58 kUseAbstractNamespace); | |
59 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, | |
60 server_socket.ListenWithPath(kInvalidSocketPath, 1)); | |
61 } | |
62 | |
63 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPathWithAbstractNamespace) { | |
64 const bool kUseAbstractNamespace = true; | |
65 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | |
66 kUseAbstractNamespace); | |
67 EXPECT_EQ(net::OK, server_socket.ListenWithPath(kInvalidSocketPath, 1)); | |
68 } | |
69 | |
70 TEST_F(UnixDomainServerSocketTest, ListenAgainAfterFailureWithInvalidPath) { | |
71 const bool kUseAbstractNamespace = false; | |
72 UnixDomainServerSocket server_socket(CreateAuthCallback(true), | |
73 kUseAbstractNamespace); | |
74 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, | |
75 server_socket.ListenWithPath(kInvalidSocketPath, 1)); | |
76 EXPECT_EQ(net::OK, server_socket.ListenWithPath(socket_path_, 1)); | |
77 } | |
78 | |
79 TEST_F(UnixDomainServerSocketTest, AcceptWithForbiddenUser) { | |
80 const bool kUseAbstractNamespace = false; | |
81 | |
82 UnixDomainServerSocket server_socket(CreateAuthCallback(false), | |
83 kUseAbstractNamespace); | |
84 EXPECT_EQ(net::OK, server_socket.ListenWithPath(socket_path_, 1)); | |
85 | |
86 SocketDescriptor accepted_socket = kInvalidSocket; | |
87 TestCompletionCallback accept_callback; | |
88 EXPECT_EQ(net::ERR_IO_PENDING, | |
89 server_socket.Accept(&accepted_socket, accept_callback.callback())); | |
90 EXPECT_EQ(accepted_socket, kInvalidSocket); | |
91 | |
92 UnixDomainClientSocket client_socket(socket_path_, kUseAbstractNamespace); | |
93 EXPECT_FALSE(client_socket.IsConnected()); | |
94 | |
95 // Connect() will return net::OK before the server rejects the connection. | |
96 TestCompletionCallback connect_callback; | |
97 int rv = connect_callback.GetResult( | |
98 client_socket.Connect(connect_callback.callback())); | |
99 ASSERT_EQ(net::OK, rv); | |
100 | |
101 // Run message loop so server can process incoming connection attempt. | |
102 { | |
103 base::RunLoop run_loop; | |
104 run_loop.RunUntilIdle(); | |
105 } | |
106 EXPECT_FALSE(client_socket.IsConnected()); | |
107 | |
108 // The server socket should not have called |accept_callback| or modified | |
109 // |accepted_socket|. | |
110 EXPECT_FALSE(accept_callback.have_result()); | |
111 EXPECT_EQ(accepted_socket, kInvalidSocket); | |
112 } | |
113 | |
114 // Normal cases including read/write are tested by UnixDomainClientSocketTest. | |
115 | |
116 } // namespace shell | |
117 } // namespace mojo | |
OLD | NEW |