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

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: 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 <vector>
6
7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/run_loop.h"
12 #include "base/stl_util.h"
13 #include "net/base/io_buffer.h"
14 #include "net/base/net_errors.h"
15 #include "net/socket/unix_domain_client_socket_posix.h"
16 #include "net/socket/unix_domain_server_socket_posix.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 namespace net {
20
21 namespace {
22
23 const char kSocketFilename[] = "unix_domain_socket_for_testing";
24 const char kInvalidSocketPath[] = "/invalid/path";
25
26 std::string MakeSocketPathForTesting() {
27 base::FilePath temp_dir;
28 base::GetTempDir(&temp_dir);
29 return temp_dir.Append(kSocketFilename).value();
30 }
31
32 bool UserCanConnectCallback(bool allow_user, uid_t uid, gid_t gid) {
33 return allow_user;
34 }
35
36 UnixDomainServerSocket::AuthCallback GetAuthCallback(bool allow_user) {
37 return base::Bind(&UserCanConnectCallback, allow_user);
38 }
39
40 void CallbackWithExpectedResultValue(int expected_rv, int rv) {
41 EXPECT_EQ(expected_rv, rv);
42 }
43
44 CompletionCallback GetCompletionCallback(int expected_rv) {
45 return base::Bind(&CallbackWithExpectedResultValue, expected_rv);
46 }
47
48 } // namespace
49
50 class UnixDomainServerSocketTest : public testing::Test {
51 protected:
52 virtual void SetUp() OVERRIDE {
53 DeleteSocketPathForTesting();
54 }
55
56 virtual void TearDown() OVERRIDE {
57 DeleteSocketPathForTesting();
58 }
59
60 void DeleteSocketPathForTesting() {
61 base::DeleteFile(base::FilePath(MakeSocketPathForTesting()), false);
62 }
63 };
64
65 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPath) {
66 const std::string socket_path(kInvalidSocketPath);
67 const bool use_abstract_namespace = false;
68 UnixDomainServerSocket server_socket(GetAuthCallback(true),
69 use_abstract_namespace);
70 EXPECT_EQ(ERR_FILE_NOT_FOUND,
71 server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
72 }
73
74 TEST_F(UnixDomainServerSocketTest, ListenWithInvalidPathWithAbstractNamespace) {
75 const std::string socket_path(kInvalidSocketPath);
76 const bool use_abstract_namespace = true;
77 UnixDomainServerSocket server_socket(GetAuthCallback(true),
78 use_abstract_namespace);
79 #if defined(OS_ANDROID) || defined(OS_LINUX)
80 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
81 #else
82 EXPECT_EQ(ERR_ADDRESS_INVALID,
83 server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
84 #endif
85 }
86
87 TEST_F(UnixDomainServerSocketTest, AcceptWithForbiddenUser) {
88 const std::string socket_path(MakeSocketPathForTesting());
89 const bool use_abstract_namespace = false;
90
91 UnixDomainServerSocket server_socket(GetAuthCallback(false),
92 use_abstract_namespace);
93 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
94
95 scoped_ptr<StreamSocket> accepted_socket;
96 EXPECT_EQ(ERR_IO_PENDING,
97 server_socket.Accept(&accepted_socket,
98 GetCompletionCallback(ERR_CONNECTION_CLOSED)));
99 EXPECT_TRUE(!accepted_socket);
100
101 UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace);
102 EXPECT_FALSE(client_socket.IsConnected());
103
104 // Return success first because server socket accepts it.
105 EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK)));
106 EXPECT_TRUE(client_socket.IsConnected());
107
108 base::RunLoop().RunUntilIdle();
109 // Server disconnects the connection.
110 EXPECT_FALSE(client_socket.IsConnected());
111 // But, server didn't create the accepted socket.
112 EXPECT_TRUE(!accepted_socket);
113
114 const int read_buffer_size = 10;
115 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size));
116 EXPECT_EQ(0, /* EOF */
117 client_socket.Read(read_buffer, read_buffer_size,
118 GetCompletionCallback(OK)));
119 }
120
121 // Normal cases including read/write are tested by UnixDomainClientSocketTest.
122
123 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698