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

Side by Side Diff: net/socket/unix_domain_listen_socket_posix_unittest.cc

Issue 376323002: Refactor unix domain socket. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved UnixDomainListenSocket to net::deprecated namespace. Created 6 years, 4 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/socket/unix_domain_listen_socket_posix.h"
6
5 #include <errno.h> 7 #include <errno.h>
6 #include <fcntl.h> 8 #include <fcntl.h>
7 #include <poll.h> 9 #include <poll.h>
8 #include <sys/socket.h> 10 #include <sys/socket.h>
9 #include <sys/stat.h> 11 #include <sys/stat.h>
10 #include <sys/time.h> 12 #include <sys/time.h>
11 #include <sys/types.h> 13 #include <sys/types.h>
12 #include <sys/un.h> 14 #include <sys/un.h>
13 #include <unistd.h> 15 #include <unistd.h>
14 16
15 #include <cstring> 17 #include <cstring>
16 #include <queue> 18 #include <queue>
17 #include <string> 19 #include <string>
18 20
19 #include "base/bind.h" 21 #include "base/bind.h"
20 #include "base/callback.h" 22 #include "base/callback.h"
21 #include "base/compiler_specific.h" 23 #include "base/compiler_specific.h"
22 #include "base/file_util.h" 24 #include "base/file_util.h"
23 #include "base/files/file_path.h" 25 #include "base/files/file_path.h"
26 #include "base/files/scoped_temp_dir.h"
24 #include "base/memory/ref_counted.h" 27 #include "base/memory/ref_counted.h"
25 #include "base/memory/scoped_ptr.h" 28 #include "base/memory/scoped_ptr.h"
26 #include "base/message_loop/message_loop.h" 29 #include "base/message_loop/message_loop.h"
27 #include "base/posix/eintr_wrapper.h" 30 #include "base/posix/eintr_wrapper.h"
28 #include "base/synchronization/condition_variable.h" 31 #include "base/synchronization/condition_variable.h"
29 #include "base/synchronization/lock.h" 32 #include "base/synchronization/lock.h"
30 #include "base/threading/platform_thread.h" 33 #include "base/threading/platform_thread.h"
31 #include "base/threading/thread.h" 34 #include "base/threading/thread.h"
32 #include "net/socket/socket_descriptor.h" 35 #include "net/socket/socket_descriptor.h"
33 #include "net/socket/unix_domain_socket_posix.h"
34 #include "testing/gtest/include/gtest/gtest.h" 36 #include "testing/gtest/include/gtest/gtest.h"
35 37
36 using std::queue; 38 using std::queue;
37 using std::string; 39 using std::string;
38 40
39 namespace net { 41 namespace net {
42 namespace deprecated {
40 namespace { 43 namespace {
41 44
42 const char kSocketFilename[] = "unix_domain_socket_for_testing"; 45 const char kSocketFilename[] = "socket_for_testing";
43 const char kInvalidSocketPath[] = "/invalid/path"; 46 const char kInvalidSocketPath[] = "/invalid/path";
44 const char kMsg[] = "hello"; 47 const char kMsg[] = "hello";
45 48
46 enum EventType { 49 enum EventType {
47 EVENT_ACCEPT, 50 EVENT_ACCEPT,
48 EVENT_AUTH_DENIED, 51 EVENT_AUTH_DENIED,
49 EVENT_AUTH_GRANTED, 52 EVENT_AUTH_GRANTED,
50 EVENT_CLOSE, 53 EVENT_CLOSE,
51 EVENT_LISTEN, 54 EVENT_LISTEN,
52 EVENT_READ, 55 EVENT_READ,
53 }; 56 };
54 57
55 string MakeSocketPath(const string& socket_file_name) {
56 base::FilePath temp_dir;
57 base::GetTempDir(&temp_dir);
58 return temp_dir.Append(socket_file_name).value();
59 }
60
61 string MakeSocketPath() {
62 return MakeSocketPath(kSocketFilename);
63 }
64
65 class EventManager : public base::RefCounted<EventManager> { 58 class EventManager : public base::RefCounted<EventManager> {
66 public: 59 public:
67 EventManager() : condition_(&mutex_) {} 60 EventManager() : condition_(&mutex_) {}
68 61
69 bool HasPendingEvent() { 62 bool HasPendingEvent() {
70 base::AutoLock lock(mutex_); 63 base::AutoLock lock(mutex_);
71 return !events_.empty(); 64 return !events_.empty();
72 } 65 }
73 66
74 void Notify(EventType event) { 67 void Notify(EventType event) {
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 }; 137 };
145 138
146 bool UserCanConnectCallback( 139 bool UserCanConnectCallback(
147 bool allow_user, const scoped_refptr<EventManager>& event_manager, 140 bool allow_user, const scoped_refptr<EventManager>& event_manager,
148 uid_t, gid_t) { 141 uid_t, gid_t) {
149 event_manager->Notify( 142 event_manager->Notify(
150 allow_user ? EVENT_AUTH_GRANTED : EVENT_AUTH_DENIED); 143 allow_user ? EVENT_AUTH_GRANTED : EVENT_AUTH_DENIED);
151 return allow_user; 144 return allow_user;
152 } 145 }
153 146
154 class UnixDomainSocketTestHelper : public testing::Test { 147 class UnixDomainListenSocketTestHelper : public testing::Test {
155 public: 148 public:
156 void CreateAndListen() { 149 void CreateAndListen() {
157 socket_ = UnixDomainSocket::CreateAndListen( 150 socket_ = UnixDomainListenSocket::CreateAndListen(
158 file_path_.value(), socket_delegate_.get(), MakeAuthCallback()); 151 file_path_.value(), socket_delegate_.get(), MakeAuthCallback());
159 socket_delegate_->OnListenCompleted(); 152 socket_delegate_->OnListenCompleted();
160 } 153 }
161 154
162 protected: 155 protected:
163 UnixDomainSocketTestHelper(const string& path, bool allow_user) 156 UnixDomainListenSocketTestHelper(const string& path_str, bool allow_user)
164 : file_path_(path), 157 : allow_user_(allow_user) {
165 allow_user_(allow_user) {} 158 file_path_ = base::FilePath(path_str);
159 if (!file_path_.IsAbsolute()) {
160 EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
161 file_path_ = GetTempSocketPath(file_path_.value());
162 }
163 // Beware that if path_str is an absolute path, this class doesn't delete
164 // the file. It must be an invalid path and cannot be created by unittests.
165 }
166
167 base::FilePath GetTempSocketPath(const std::string socket_name) {
168 DCHECK(temp_dir_.IsValid());
169 return temp_dir_.path().Append(socket_name);
170 }
166 171
167 virtual void SetUp() OVERRIDE { 172 virtual void SetUp() OVERRIDE {
168 event_manager_ = new EventManager(); 173 event_manager_ = new EventManager();
169 socket_delegate_.reset(new TestListenSocketDelegate(event_manager_)); 174 socket_delegate_.reset(new TestListenSocketDelegate(event_manager_));
170 DeleteSocketFile();
171 } 175 }
172 176
173 virtual void TearDown() OVERRIDE { 177 virtual void TearDown() OVERRIDE {
174 DeleteSocketFile();
175 socket_.reset(); 178 socket_.reset();
176 socket_delegate_.reset(); 179 socket_delegate_.reset();
177 event_manager_ = NULL; 180 event_manager_ = NULL;
178 } 181 }
179 182
180 UnixDomainSocket::AuthCallback MakeAuthCallback() { 183 UnixDomainListenSocket::AuthCallback MakeAuthCallback() {
181 return base::Bind(&UserCanConnectCallback, allow_user_, event_manager_); 184 return base::Bind(&UserCanConnectCallback, allow_user_, event_manager_);
182 } 185 }
183 186
184 void DeleteSocketFile() {
185 ASSERT_FALSE(file_path_.empty());
186 base::DeleteFile(file_path_, false /* not recursive */);
187 }
188
189 SocketDescriptor CreateClientSocket() { 187 SocketDescriptor CreateClientSocket() {
190 const SocketDescriptor sock = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0); 188 const SocketDescriptor sock = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0);
191 if (sock < 0) { 189 if (sock < 0) {
192 LOG(ERROR) << "socket() error"; 190 LOG(ERROR) << "socket() error";
193 return kInvalidSocket; 191 return kInvalidSocket;
194 } 192 }
195 sockaddr_un addr; 193 sockaddr_un addr;
196 memset(&addr, 0, sizeof(addr)); 194 memset(&addr, 0, sizeof(addr));
197 addr.sun_family = AF_UNIX; 195 addr.sun_family = AF_UNIX;
198 socklen_t addr_len; 196 socklen_t addr_len;
199 strncpy(addr.sun_path, file_path_.value().c_str(), sizeof(addr.sun_path)); 197 strncpy(addr.sun_path, file_path_.value().c_str(), sizeof(addr.sun_path));
200 addr_len = sizeof(sockaddr_un); 198 addr_len = sizeof(sockaddr_un);
201 if (connect(sock, reinterpret_cast<sockaddr*>(&addr), addr_len) != 0) { 199 if (connect(sock, reinterpret_cast<sockaddr*>(&addr), addr_len) != 0) {
202 LOG(ERROR) << "connect() error"; 200 LOG(ERROR) << "connect() error: " << strerror(errno)
201 << ": path=" << file_path_.value();
203 return kInvalidSocket; 202 return kInvalidSocket;
204 } 203 }
205 return sock; 204 return sock;
206 } 205 }
207 206
208 scoped_ptr<base::Thread> CreateAndRunServerThread() { 207 scoped_ptr<base::Thread> CreateAndRunServerThread() {
209 base::Thread::Options options; 208 base::Thread::Options options;
210 options.message_loop_type = base::MessageLoop::TYPE_IO; 209 options.message_loop_type = base::MessageLoop::TYPE_IO;
211 scoped_ptr<base::Thread> thread(new base::Thread("socketio_test")); 210 scoped_ptr<base::Thread> thread(new base::Thread("socketio_test"));
212 thread->StartWithOptions(options); 211 thread->StartWithOptions(options);
213 thread->message_loop()->PostTask( 212 thread->message_loop()->PostTask(
214 FROM_HERE, 213 FROM_HERE,
215 base::Bind(&UnixDomainSocketTestHelper::CreateAndListen, 214 base::Bind(&UnixDomainListenSocketTestHelper::CreateAndListen,
216 base::Unretained(this))); 215 base::Unretained(this)));
217 return thread.Pass(); 216 return thread.Pass();
218 } 217 }
219 218
220 const base::FilePath file_path_; 219 base::ScopedTempDir temp_dir_;
220 base::FilePath file_path_;
221 const bool allow_user_; 221 const bool allow_user_;
222 scoped_refptr<EventManager> event_manager_; 222 scoped_refptr<EventManager> event_manager_;
223 scoped_ptr<TestListenSocketDelegate> socket_delegate_; 223 scoped_ptr<TestListenSocketDelegate> socket_delegate_;
224 scoped_ptr<UnixDomainSocket> socket_; 224 scoped_ptr<UnixDomainListenSocket> socket_;
225 }; 225 };
226 226
227 class UnixDomainSocketTest : public UnixDomainSocketTestHelper { 227 class UnixDomainListenSocketTest : public UnixDomainListenSocketTestHelper {
228 protected: 228 protected:
229 UnixDomainSocketTest() 229 UnixDomainListenSocketTest()
230 : UnixDomainSocketTestHelper(MakeSocketPath(), true /* allow user */) {} 230 : UnixDomainListenSocketTestHelper(kSocketFilename,
231 true /* allow user */) {}
231 }; 232 };
232 233
233 class UnixDomainSocketTestWithInvalidPath : public UnixDomainSocketTestHelper { 234 class UnixDomainListenSocketTestWithInvalidPath
235 : public UnixDomainListenSocketTestHelper {
234 protected: 236 protected:
235 UnixDomainSocketTestWithInvalidPath() 237 UnixDomainListenSocketTestWithInvalidPath()
236 : UnixDomainSocketTestHelper(kInvalidSocketPath, true) {} 238 : UnixDomainListenSocketTestHelper(kInvalidSocketPath, true) {}
237 }; 239 };
238 240
239 class UnixDomainSocketTestWithForbiddenUser 241 class UnixDomainListenSocketTestWithForbiddenUser
240 : public UnixDomainSocketTestHelper { 242 : public UnixDomainListenSocketTestHelper {
241 protected: 243 protected:
242 UnixDomainSocketTestWithForbiddenUser() 244 UnixDomainListenSocketTestWithForbiddenUser()
243 : UnixDomainSocketTestHelper(MakeSocketPath(), false /* forbid user */) {} 245 : UnixDomainListenSocketTestHelper(kSocketFilename,
246 false /* forbid user */) {}
244 }; 247 };
245 248
246 TEST_F(UnixDomainSocketTest, CreateAndListen) { 249 TEST_F(UnixDomainListenSocketTest, CreateAndListen) {
247 CreateAndListen(); 250 CreateAndListen();
248 EXPECT_FALSE(socket_.get() == NULL); 251 EXPECT_FALSE(socket_.get() == NULL);
249 } 252 }
250 253
251 TEST_F(UnixDomainSocketTestWithInvalidPath, CreateAndListenWithInvalidPath) { 254 TEST_F(UnixDomainListenSocketTestWithInvalidPath,
255 CreateAndListenWithInvalidPath) {
252 CreateAndListen(); 256 CreateAndListen();
253 EXPECT_TRUE(socket_.get() == NULL); 257 EXPECT_TRUE(socket_.get() == NULL);
254 } 258 }
255 259
256 #ifdef SOCKET_ABSTRACT_NAMESPACE_SUPPORTED 260 #ifdef SOCKET_ABSTRACT_NAMESPACE_SUPPORTED
257 // Test with an invalid path to make sure that the socket is not backed by a 261 // Test with an invalid path to make sure that the socket is not backed by a
258 // file. 262 // file.
259 TEST_F(UnixDomainSocketTestWithInvalidPath, 263 TEST_F(UnixDomainListenSocketTestWithInvalidPath,
260 CreateAndListenWithAbstractNamespace) { 264 CreateAndListenWithAbstractNamespace) {
261 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( 265 socket_ = UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
262 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); 266 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback());
263 EXPECT_FALSE(socket_.get() == NULL); 267 EXPECT_FALSE(socket_.get() == NULL);
264 } 268 }
265 269
266 TEST_F(UnixDomainSocketTest, TestFallbackName) { 270 TEST_F(UnixDomainListenSocketTest, TestFallbackName) {
267 scoped_ptr<UnixDomainSocket> existing_socket = 271 scoped_ptr<UnixDomainListenSocket> existing_socket =
268 UnixDomainSocket::CreateAndListenWithAbstractNamespace( 272 UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
269 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); 273 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback());
270 EXPECT_FALSE(existing_socket.get() == NULL); 274 EXPECT_FALSE(existing_socket.get() == NULL);
271 // First, try to bind socket with the same name with no fallback name. 275 // First, try to bind socket with the same name with no fallback name.
272 socket_ = 276 socket_ =
273 UnixDomainSocket::CreateAndListenWithAbstractNamespace( 277 UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
274 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); 278 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback());
275 EXPECT_TRUE(socket_.get() == NULL); 279 EXPECT_TRUE(socket_.get() == NULL);
276 // Now with a fallback name. 280 // Now with a fallback name.
277 const char kFallbackSocketName[] = "unix_domain_socket_for_testing_2"; 281 const char kFallbackSocketName[] = "socket_for_testing_2";
278 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( 282 socket_ = UnixDomainListenSocket::CreateAndListenWithAbstractNamespace(
279 file_path_.value(), 283 file_path_.value(),
280 MakeSocketPath(kFallbackSocketName), 284 GetTempSocketPath(kFallbackSocketName).value(),
281 socket_delegate_.get(), 285 socket_delegate_.get(),
282 MakeAuthCallback()); 286 MakeAuthCallback());
283 EXPECT_FALSE(socket_.get() == NULL); 287 EXPECT_FALSE(socket_.get() == NULL);
284 } 288 }
285 #endif 289 #endif
286 290
287 TEST_F(UnixDomainSocketTest, TestWithClient) { 291 TEST_F(UnixDomainListenSocketTest, TestWithClient) {
288 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); 292 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread();
289 EventType event = event_manager_->WaitForEvent(); 293 EventType event = event_manager_->WaitForEvent();
290 ASSERT_EQ(EVENT_LISTEN, event); 294 ASSERT_EQ(EVENT_LISTEN, event);
291 295
292 // Create the client socket. 296 // Create the client socket.
293 const SocketDescriptor sock = CreateClientSocket(); 297 const SocketDescriptor sock = CreateClientSocket();
294 ASSERT_NE(kInvalidSocket, sock); 298 ASSERT_NE(kInvalidSocket, sock);
295 event = event_manager_->WaitForEvent(); 299 event = event_manager_->WaitForEvent();
296 ASSERT_EQ(EVENT_AUTH_GRANTED, event); 300 ASSERT_EQ(EVENT_AUTH_GRANTED, event);
297 event = event_manager_->WaitForEvent(); 301 event = event_manager_->WaitForEvent();
298 ASSERT_EQ(EVENT_ACCEPT, event); 302 ASSERT_EQ(EVENT_ACCEPT, event);
299 303
300 // Send a message from the client to the server. 304 // Send a message from the client to the server.
301 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); 305 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0));
302 ASSERT_NE(-1, ret); 306 ASSERT_NE(-1, ret);
303 ASSERT_EQ(sizeof(kMsg), static_cast<size_t>(ret)); 307 ASSERT_EQ(sizeof(kMsg), static_cast<size_t>(ret));
304 event = event_manager_->WaitForEvent(); 308 event = event_manager_->WaitForEvent();
305 ASSERT_EQ(EVENT_READ, event); 309 ASSERT_EQ(EVENT_READ, event);
306 ASSERT_EQ(kMsg, socket_delegate_->ReceivedData()); 310 ASSERT_EQ(kMsg, socket_delegate_->ReceivedData());
307 311
308 // Close the client socket. 312 // Close the client socket.
309 ret = IGNORE_EINTR(close(sock)); 313 ret = IGNORE_EINTR(close(sock));
310 event = event_manager_->WaitForEvent(); 314 event = event_manager_->WaitForEvent();
311 ASSERT_EQ(EVENT_CLOSE, event); 315 ASSERT_EQ(EVENT_CLOSE, event);
312 } 316 }
313 317
314 TEST_F(UnixDomainSocketTestWithForbiddenUser, TestWithForbiddenUser) { 318 TEST_F(UnixDomainListenSocketTestWithForbiddenUser, TestWithForbiddenUser) {
315 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); 319 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread();
316 EventType event = event_manager_->WaitForEvent(); 320 EventType event = event_manager_->WaitForEvent();
317 ASSERT_EQ(EVENT_LISTEN, event); 321 ASSERT_EQ(EVENT_LISTEN, event);
318 const SocketDescriptor sock = CreateClientSocket(); 322 const SocketDescriptor sock = CreateClientSocket();
319 ASSERT_NE(kInvalidSocket, sock); 323 ASSERT_NE(kInvalidSocket, sock);
320 324
321 event = event_manager_->WaitForEvent(); 325 event = event_manager_->WaitForEvent();
322 ASSERT_EQ(EVENT_AUTH_DENIED, event); 326 ASSERT_EQ(EVENT_AUTH_DENIED, event);
323 327
324 // Wait until the file descriptor is closed by the server. 328 // Wait until the file descriptor is closed by the server.
325 struct pollfd poll_fd; 329 struct pollfd poll_fd;
326 poll_fd.fd = sock; 330 poll_fd.fd = sock;
327 poll_fd.events = POLLIN; 331 poll_fd.events = POLLIN;
328 poll(&poll_fd, 1, -1 /* rely on GTest for timeout handling */); 332 poll(&poll_fd, 1, -1 /* rely on GTest for timeout handling */);
329 333
330 // Send() must fail. 334 // Send() must fail.
331 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); 335 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0));
332 ASSERT_EQ(-1, ret); 336 ASSERT_EQ(-1, ret);
333 ASSERT_EQ(EPIPE, errno); 337 ASSERT_EQ(EPIPE, errno);
334 ASSERT_FALSE(event_manager_->HasPendingEvent()); 338 ASSERT_FALSE(event_manager_->HasPendingEvent());
335 } 339 }
336 340
337 } // namespace 341 } // namespace
342 } // namespace deprecated
338 } // namespace net 343 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/unix_domain_listen_socket_posix.cc ('k') | net/socket/unix_domain_server_socket_posix.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698