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

Side by Side Diff: net/socket/unix_domain_client_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 "base/bind.h"
6 #include "base/file_util.h"
7 #include "base/files/file_path.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/run_loop.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/net_errors.h"
12 #include "net/socket/unix_domain_client_socket_posix.h"
mmenke 2014/07/10 19:07:28 nit: This should go up top
byungchul 2014/07/11 02:56:33 Done.
13 #include "net/socket/unix_domain_server_socket_posix.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace net {
17
18 namespace {
19
20 const char kSocketFilename[] = "unix_domain_socket_for_testing";
21
22 std::string MakeSocketPathForTesting() {
mmenke 2014/07/10 19:07:28 nit: Maybe Make -> Get?
mmenke 2014/07/10 19:07:28 nit: "ForTesting" generally not needed in a unitt
byungchul 2014/07/11 02:56:33 Done.
byungchul 2014/07/11 02:56:33 Done.
23 base::FilePath temp_dir;
24 base::GetTempDir(&temp_dir);
25 return temp_dir.Append(kSocketFilename).value();
mmenke 2014/07/10 19:07:28 This doesn't work - multiple tests should be able
byungchul 2014/07/11 02:56:33 Creates a temp dir instead.
26 }
27
28 bool UserCanConnectCallback(bool allow_user, uid_t uid, gid_t gid) {
29 return allow_user;
30 }
31
32 UnixDomainServerSocket::AuthCallback GetAuthCallback(bool allow_user) {
33 return base::Bind(&UserCanConnectCallback, allow_user);
34 }
35
36 void CallbackWithExpectedResultValue(int expected_rv, int rv) {
37 EXPECT_EQ(expected_rv, rv);
38 }
39
40 CompletionCallback GetCompletionCallback(int expected_rv) {
mmenke 2014/07/10 19:07:28 nit: Maybe Get -> Create?
byungchul 2014/07/11 02:56:33 Done.
41 return base::Bind(&CallbackWithExpectedResultValue, expected_rv);
42 }
43
44 } // namespace
45
46 class UnixDomainClientSocketTest : public testing::Test {
47 protected:
48 virtual void SetUp() OVERRIDE {
49 DeleteSocketPathForTesting();
50 }
51
52 virtual void TearDown() OVERRIDE {
53 DeleteSocketPathForTesting();
54 }
55
56 void DeleteSocketPathForTesting() {
mmenke 2014/07/10 19:07:28 nit: "ForTesting" generally not needed in a unitt
byungchul 2014/07/11 02:56:33 Done.
57 base::DeleteFile(base::FilePath(MakeSocketPathForTesting()), false);
58 }
59 };
60
61 TEST_F(UnixDomainClientSocketTest, Connect) {
62 const std::string socket_path(MakeSocketPathForTesting());
63 const bool use_abstract_namespace = false;
mmenke 2014/07/10 19:07:28 Sure use kConstNamingScheme for consts, I believe.
byungchul 2014/07/11 02:56:33 Done.
64
65 UnixDomainServerSocket server_socket(GetAuthCallback(true),
66 use_abstract_namespace);
67 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
68
69 scoped_ptr<StreamSocket> accepted_socket;
70 EXPECT_EQ(ERR_IO_PENDING, server_socket.Accept(&accepted_socket,
71 GetCompletionCallback(OK)));
72 EXPECT_TRUE(!accepted_socket);
73
74 UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace);
75 EXPECT_FALSE(client_socket.IsConnected());
76
77 EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK)));
78 EXPECT_TRUE(client_socket.IsConnected());
79 // Not yet notified to server.
80 EXPECT_TRUE(!accepted_socket);
81
82 base::RunLoop().RunUntilIdle();
83 EXPECT_TRUE(accepted_socket);
84 EXPECT_TRUE(accepted_socket->IsConnected());
85 }
86
87 TEST_F(UnixDomainClientSocketTest, ConnectWithAbstractNamespace) {
88 const std::string socket_path(MakeSocketPathForTesting());
89 const bool use_abstract_namespace = true;
90
91 UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace);
92 EXPECT_FALSE(client_socket.IsConnected());
93
94 #if defined(OS_ANDROID) || defined(OS_LINUX)
95 UnixDomainServerSocket server_socket(GetAuthCallback(true),
96 use_abstract_namespace);
97 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
98
99 scoped_ptr<StreamSocket> accepted_socket;
100 EXPECT_EQ(ERR_IO_PENDING, server_socket.Accept(&accepted_socket,
101 GetCompletionCallback(OK)));
102 EXPECT_TRUE(!accepted_socket);
103
104 EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK)));
105 EXPECT_TRUE(client_socket.IsConnected());
106 // Not yet notified to server.
107 EXPECT_TRUE(!accepted_socket);
108
109 base::RunLoop().RunUntilIdle();
110 EXPECT_TRUE(accepted_socket);
111 EXPECT_TRUE(accepted_socket->IsConnected());
112 #else
113 EXPECT_EQ(ERR_ADDRESS_INVALID,
114 client_socket.Connect(GetCompletionCallback(OK)));
115 #endif
116 }
117
118 TEST_F(UnixDomainClientSocketTest, ConnectToNonExistingSocket) {
119 const std::string socket_path(MakeSocketPathForTesting());
120 const bool use_abstract_namespace = false;
121
122 UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace);
123 EXPECT_FALSE(client_socket.IsConnected());
124 EXPECT_EQ(ERR_FILE_NOT_FOUND,
125 client_socket.Connect(GetCompletionCallback(OK)));
126 }
127
128 TEST_F(UnixDomainClientSocketTest,
129 ConnectToNonExistingSocketWithAbstractNamespace) {
130 const std::string socket_path(MakeSocketPathForTesting());
131 const bool use_abstract_namespace = true;
132
133 UnixDomainClientSocket client_socket(socket_path, use_abstract_namespace);
134 EXPECT_FALSE(client_socket.IsConnected());
135 #if defined(OS_ANDROID) || defined(OS_LINUX)
136 EXPECT_EQ(ERR_CONNECTION_REFUSED,
137 client_socket.Connect(GetCompletionCallback(OK)));
138 #else
139 EXPECT_EQ(ERR_ADDRESS_INVALID,
140 client_socket.Connect(GetCompletionCallback(OK)));
141 #endif
142 }
143
144 TEST_F(UnixDomainClientSocketTest, ReadWrite) {
145 const std::string socket_path(MakeSocketPathForTesting());
146
147 UnixDomainServerSocket server_socket(GetAuthCallback(true), false);
148 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
149 scoped_ptr<StreamSocket> accepted_socket;
150 EXPECT_EQ(ERR_IO_PENDING, server_socket.Accept(&accepted_socket,
151 GetCompletionCallback(OK)));
152 UnixDomainClientSocket client_socket(socket_path, false);
153 EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK)));
154
155 base::RunLoop().RunUntilIdle();
156 EXPECT_TRUE(accepted_socket->IsConnected());
157 EXPECT_TRUE(client_socket.IsConnected());
158
159 // Sends data from client to server.
160 const int write_data_size = 10;
161 scoped_refptr<IOBuffer> write_buffer(
162 new StringIOBuffer(std::string(write_data_size, 'd')));
163 EXPECT_EQ(write_data_size,
164 client_socket.Write(write_buffer, write_data_size,
165 GetCompletionCallback(OK)));
166 // Buffer bigger than write data size.
167 const int read_buffer_size = write_data_size * 2;
168 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size));
169 EXPECT_EQ(write_data_size,
170 accepted_socket->Read(read_buffer, read_buffer_size,
171 GetCompletionCallback(OK)));
172
173 // Sends data from server and client
174 EXPECT_EQ(write_data_size,
175 accepted_socket->Write(write_buffer, write_data_size,
176 GetCompletionCallback(OK)));
177 // Reads multiple times.
178 const int small_read_buffer_size = write_data_size / 3;
179 EXPECT_EQ(small_read_buffer_size,
180 client_socket.Read(read_buffer, small_read_buffer_size,
181 GetCompletionCallback(OK)));
182 EXPECT_EQ(write_data_size - small_read_buffer_size,
183 client_socket.Read(read_buffer, read_buffer_size,
184 GetCompletionCallback(OK)));
185 // No more data.
186 EXPECT_EQ(ERR_IO_PENDING,
187 client_socket.Read(read_buffer, read_buffer_size,
188 GetCompletionCallback(ERR_CONNECTION_CLOSED)));
189 }
190
191 TEST_F(UnixDomainClientSocketTest, ReadWriteAsynchronously) {
192 const std::string socket_path(MakeSocketPathForTesting());
193
194 UnixDomainServerSocket server_socket(GetAuthCallback(true), false);
195 EXPECT_EQ(OK, server_socket.ListenWithAddressAndPort(socket_path, 0, 1));
196 scoped_ptr<StreamSocket> accepted_socket;
197 EXPECT_EQ(ERR_IO_PENDING, server_socket.Accept(&accepted_socket,
198 GetCompletionCallback(OK)));
199 UnixDomainClientSocket client_socket(socket_path, false);
200 EXPECT_EQ(OK, client_socket.Connect(GetCompletionCallback(OK)));
201
202 base::RunLoop().RunUntilIdle();
203 EXPECT_TRUE(accepted_socket->IsConnected());
204 EXPECT_TRUE(client_socket.IsConnected());
205
206 // Waits for data from client.
207 const int write_data_size = 10;
208 const int read_buffer_size = write_data_size * 2;
209 const int small_read_buffer_size = write_data_size / 3;
210 // Reads smaller than write data size first.
211 scoped_refptr<IOBuffer> read_buffer(new IOBuffer(read_buffer_size));
212 EXPECT_EQ(
213 ERR_IO_PENDING,
214 accepted_socket->Read(read_buffer, small_read_buffer_size,
215 GetCompletionCallback(small_read_buffer_size)));
216
217 scoped_refptr<IOBuffer> write_buffer(
218 new StringIOBuffer(std::string(write_data_size, 'd')));
219 EXPECT_EQ(write_data_size,
220 client_socket.Write(write_buffer, write_data_size,
221 GetCompletionCallback(OK)));
222
223 base::RunLoop().RunUntilIdle();
224 // First read completed.
225
226 // Reads remaining data.
227 EXPECT_EQ(write_data_size - small_read_buffer_size,
228 accepted_socket->Read(read_buffer, read_buffer_size,
229 GetCompletionCallback(OK)));
230 // No more data.
231 EXPECT_EQ(ERR_IO_PENDING,
232 accepted_socket->Read(
233 read_buffer, read_buffer_size,
234 GetCompletionCallback(ERR_CONNECTION_CLOSED)));
235 }
236
237 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698