OLD | NEW |
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_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" |
24 #include "base/memory/ref_counted.h" | 26 #include "base/memory/ref_counted.h" |
25 #include "base/memory/scoped_ptr.h" | 27 #include "base/memory/scoped_ptr.h" |
26 #include "base/message_loop/message_loop.h" | 28 #include "base/message_loop/message_loop.h" |
27 #include "base/posix/eintr_wrapper.h" | 29 #include "base/posix/eintr_wrapper.h" |
28 #include "base/synchronization/condition_variable.h" | 30 #include "base/synchronization/condition_variable.h" |
29 #include "base/synchronization/lock.h" | 31 #include "base/synchronization/lock.h" |
30 #include "base/threading/platform_thread.h" | 32 #include "base/threading/platform_thread.h" |
31 #include "base/threading/thread.h" | 33 #include "base/threading/thread.h" |
32 #include "net/socket/socket_descriptor.h" | 34 #include "net/socket/socket_descriptor.h" |
33 #include "net/socket/unix_domain_socket_posix.h" | |
34 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
35 | 36 |
36 using std::queue; | 37 using std::queue; |
37 using std::string; | 38 using std::string; |
38 | 39 |
39 namespace net { | 40 namespace net { |
40 namespace { | 41 namespace { |
41 | 42 |
42 const char kSocketFilename[] = "unix_domain_socket_for_testing"; | 43 const char kSocketFilename[] = "unix_domain_socket_for_testing"; |
43 const char kInvalidSocketPath[] = "/invalid/path"; | 44 const char kInvalidSocketPath[] = "/invalid/path"; |
44 const char kMsg[] = "hello"; | 45 const char kMsg[] = "hello"; |
45 | 46 |
46 enum EventType { | 47 enum EventType { |
47 EVENT_ACCEPT, | 48 EVENT_ACCEPT, |
48 EVENT_AUTH_DENIED, | 49 EVENT_AUTH_DENIED, |
49 EVENT_AUTH_GRANTED, | 50 EVENT_AUTH_GRANTED, |
50 EVENT_CLOSE, | 51 EVENT_CLOSE, |
51 EVENT_LISTEN, | 52 EVENT_LISTEN, |
52 EVENT_READ, | 53 EVENT_READ, |
53 }; | 54 }; |
54 | 55 |
55 string MakeSocketPath(const string& socket_file_name) { | 56 string MakeSocketPath(const string& socket_file_name) { |
| 57 // Builds a socket path under a temp directory created newly. |
56 base::FilePath temp_dir; | 58 base::FilePath temp_dir; |
57 base::GetTempDir(&temp_dir); | 59 base::CreateNewTempDirectory("", &temp_dir); |
58 return temp_dir.Append(socket_file_name).value(); | 60 return temp_dir.Append(socket_file_name).value(); |
59 } | 61 } |
60 | 62 |
61 string MakeSocketPath() { | 63 string MakeSocketPath() { |
62 return MakeSocketPath(kSocketFilename); | 64 return MakeSocketPath(kSocketFilename); |
63 } | 65 } |
64 | 66 |
| 67 void DeleteSocketPath(const string& socket_path) { |
| 68 // Deletes the temp dir created by MakeSocketPath(). |
| 69 base::DeleteFile(base::FilePath(socket_path).DirName(), true); |
| 70 } |
| 71 |
65 class EventManager : public base::RefCounted<EventManager> { | 72 class EventManager : public base::RefCounted<EventManager> { |
66 public: | 73 public: |
67 EventManager() : condition_(&mutex_) {} | 74 EventManager() : condition_(&mutex_) {} |
68 | 75 |
69 bool HasPendingEvent() { | 76 bool HasPendingEvent() { |
70 base::AutoLock lock(mutex_); | 77 base::AutoLock lock(mutex_); |
71 return !events_.empty(); | 78 return !events_.empty(); |
72 } | 79 } |
73 | 80 |
74 void Notify(EventType event) { | 81 void Notify(EventType event) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 }; | 151 }; |
145 | 152 |
146 bool UserCanConnectCallback( | 153 bool UserCanConnectCallback( |
147 bool allow_user, const scoped_refptr<EventManager>& event_manager, | 154 bool allow_user, const scoped_refptr<EventManager>& event_manager, |
148 uid_t, gid_t) { | 155 uid_t, gid_t) { |
149 event_manager->Notify( | 156 event_manager->Notify( |
150 allow_user ? EVENT_AUTH_GRANTED : EVENT_AUTH_DENIED); | 157 allow_user ? EVENT_AUTH_GRANTED : EVENT_AUTH_DENIED); |
151 return allow_user; | 158 return allow_user; |
152 } | 159 } |
153 | 160 |
154 class UnixDomainSocketTestHelper : public testing::Test { | 161 class UnixDomainListenSocketTestHelper : public testing::Test { |
155 public: | 162 public: |
156 void CreateAndListen() { | 163 void CreateAndListen() { |
157 socket_ = UnixDomainSocket::CreateAndListen( | 164 socket_ = UnixDomainListenSocket::CreateAndListen( |
158 file_path_.value(), socket_delegate_.get(), MakeAuthCallback()); | 165 file_path_.value(), socket_delegate_.get(), MakeAuthCallback()); |
159 socket_delegate_->OnListenCompleted(); | 166 socket_delegate_->OnListenCompleted(); |
160 } | 167 } |
161 | 168 |
162 protected: | 169 protected: |
163 UnixDomainSocketTestHelper(const string& path, bool allow_user) | 170 UnixDomainListenSocketTestHelper(const string& path, bool allow_user) |
164 : file_path_(path), | 171 : file_path_(path), |
165 allow_user_(allow_user) {} | 172 allow_user_(allow_user) {} |
166 | 173 |
167 virtual void SetUp() OVERRIDE { | 174 virtual void SetUp() OVERRIDE { |
168 event_manager_ = new EventManager(); | 175 event_manager_ = new EventManager(); |
169 socket_delegate_.reset(new TestListenSocketDelegate(event_manager_)); | 176 socket_delegate_.reset(new TestListenSocketDelegate(event_manager_)); |
170 DeleteSocketFile(); | |
171 } | 177 } |
172 | 178 |
173 virtual void TearDown() OVERRIDE { | 179 virtual void TearDown() OVERRIDE { |
174 DeleteSocketFile(); | 180 DeleteSocketFile(); |
175 socket_.reset(); | 181 socket_.reset(); |
176 socket_delegate_.reset(); | 182 socket_delegate_.reset(); |
177 event_manager_ = NULL; | 183 event_manager_ = NULL; |
178 } | 184 } |
179 | 185 |
180 UnixDomainSocket::AuthCallback MakeAuthCallback() { | 186 UnixDomainListenSocket::AuthCallback MakeAuthCallback() { |
181 return base::Bind(&UserCanConnectCallback, allow_user_, event_manager_); | 187 return base::Bind(&UserCanConnectCallback, allow_user_, event_manager_); |
182 } | 188 } |
183 | 189 |
184 void DeleteSocketFile() { | 190 void DeleteSocketFile() { |
185 ASSERT_FALSE(file_path_.empty()); | 191 ASSERT_FALSE(file_path_.empty()); |
186 base::DeleteFile(file_path_, false /* not recursive */); | 192 DeleteSocketPath(file_path_.value()); |
187 } | 193 } |
188 | 194 |
189 SocketDescriptor CreateClientSocket() { | 195 SocketDescriptor CreateClientSocket() { |
190 const SocketDescriptor sock = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0); | 196 const SocketDescriptor sock = CreatePlatformSocket(PF_UNIX, SOCK_STREAM, 0); |
191 if (sock < 0) { | 197 if (sock < 0) { |
192 LOG(ERROR) << "socket() error"; | 198 LOG(ERROR) << "socket() error"; |
193 return kInvalidSocket; | 199 return kInvalidSocket; |
194 } | 200 } |
195 sockaddr_un addr; | 201 sockaddr_un addr; |
196 memset(&addr, 0, sizeof(addr)); | 202 memset(&addr, 0, sizeof(addr)); |
197 addr.sun_family = AF_UNIX; | 203 addr.sun_family = AF_UNIX; |
198 socklen_t addr_len; | 204 socklen_t addr_len; |
199 strncpy(addr.sun_path, file_path_.value().c_str(), sizeof(addr.sun_path)); | 205 strncpy(addr.sun_path, file_path_.value().c_str(), sizeof(addr.sun_path)); |
200 addr_len = sizeof(sockaddr_un); | 206 addr_len = sizeof(sockaddr_un); |
201 if (connect(sock, reinterpret_cast<sockaddr*>(&addr), addr_len) != 0) { | 207 if (connect(sock, reinterpret_cast<sockaddr*>(&addr), addr_len) != 0) { |
202 LOG(ERROR) << "connect() error"; | 208 LOG(ERROR) << "connect() error"; |
203 return kInvalidSocket; | 209 return kInvalidSocket; |
204 } | 210 } |
205 return sock; | 211 return sock; |
206 } | 212 } |
207 | 213 |
208 scoped_ptr<base::Thread> CreateAndRunServerThread() { | 214 scoped_ptr<base::Thread> CreateAndRunServerThread() { |
209 base::Thread::Options options; | 215 base::Thread::Options options; |
210 options.message_loop_type = base::MessageLoop::TYPE_IO; | 216 options.message_loop_type = base::MessageLoop::TYPE_IO; |
211 scoped_ptr<base::Thread> thread(new base::Thread("socketio_test")); | 217 scoped_ptr<base::Thread> thread(new base::Thread("socketio_test")); |
212 thread->StartWithOptions(options); | 218 thread->StartWithOptions(options); |
213 thread->message_loop()->PostTask( | 219 thread->message_loop()->PostTask( |
214 FROM_HERE, | 220 FROM_HERE, |
215 base::Bind(&UnixDomainSocketTestHelper::CreateAndListen, | 221 base::Bind(&UnixDomainListenSocketTestHelper::CreateAndListen, |
216 base::Unretained(this))); | 222 base::Unretained(this))); |
217 return thread.Pass(); | 223 return thread.Pass(); |
218 } | 224 } |
219 | 225 |
220 const base::FilePath file_path_; | 226 const base::FilePath file_path_; |
221 const bool allow_user_; | 227 const bool allow_user_; |
222 scoped_refptr<EventManager> event_manager_; | 228 scoped_refptr<EventManager> event_manager_; |
223 scoped_ptr<TestListenSocketDelegate> socket_delegate_; | 229 scoped_ptr<TestListenSocketDelegate> socket_delegate_; |
224 scoped_ptr<UnixDomainSocket> socket_; | 230 scoped_ptr<UnixDomainListenSocket> socket_; |
225 }; | 231 }; |
226 | 232 |
227 class UnixDomainSocketTest : public UnixDomainSocketTestHelper { | 233 class UnixDomainListenSocketTest : public UnixDomainListenSocketTestHelper { |
228 protected: | 234 protected: |
229 UnixDomainSocketTest() | 235 UnixDomainListenSocketTest() |
230 : UnixDomainSocketTestHelper(MakeSocketPath(), true /* allow user */) {} | 236 : UnixDomainListenSocketTestHelper(MakeSocketPath(), |
| 237 true /* allow user */) {} |
231 }; | 238 }; |
232 | 239 |
233 class UnixDomainSocketTestWithInvalidPath : public UnixDomainSocketTestHelper { | 240 class UnixDomainListenSocketTestWithInvalidPath |
| 241 : public UnixDomainListenSocketTestHelper { |
234 protected: | 242 protected: |
235 UnixDomainSocketTestWithInvalidPath() | 243 UnixDomainListenSocketTestWithInvalidPath() |
236 : UnixDomainSocketTestHelper(kInvalidSocketPath, true) {} | 244 : UnixDomainListenSocketTestHelper(kInvalidSocketPath, true) {} |
237 }; | 245 }; |
238 | 246 |
239 class UnixDomainSocketTestWithForbiddenUser | 247 class UnixDomainListenSocketTestWithForbiddenUser |
240 : public UnixDomainSocketTestHelper { | 248 : public UnixDomainListenSocketTestHelper { |
241 protected: | 249 protected: |
242 UnixDomainSocketTestWithForbiddenUser() | 250 UnixDomainListenSocketTestWithForbiddenUser() |
243 : UnixDomainSocketTestHelper(MakeSocketPath(), false /* forbid user */) {} | 251 : UnixDomainListenSocketTestHelper(MakeSocketPath(), |
| 252 false /* forbid user */) {} |
244 }; | 253 }; |
245 | 254 |
246 TEST_F(UnixDomainSocketTest, CreateAndListen) { | 255 TEST_F(UnixDomainListenSocketTest, CreateAndListen) { |
247 CreateAndListen(); | 256 CreateAndListen(); |
248 EXPECT_FALSE(socket_.get() == NULL); | 257 EXPECT_FALSE(socket_.get() == NULL); |
249 } | 258 } |
250 | 259 |
251 TEST_F(UnixDomainSocketTestWithInvalidPath, CreateAndListenWithInvalidPath) { | 260 TEST_F(UnixDomainListenSocketTestWithInvalidPath, |
| 261 CreateAndListenWithInvalidPath) { |
252 CreateAndListen(); | 262 CreateAndListen(); |
253 EXPECT_TRUE(socket_.get() == NULL); | 263 EXPECT_TRUE(socket_.get() == NULL); |
254 } | 264 } |
255 | 265 |
256 #ifdef SOCKET_ABSTRACT_NAMESPACE_SUPPORTED | 266 #ifdef SOCKET_ABSTRACT_NAMESPACE_SUPPORTED |
257 // Test with an invalid path to make sure that the socket is not backed by a | 267 // Test with an invalid path to make sure that the socket is not backed by a |
258 // file. | 268 // file. |
259 TEST_F(UnixDomainSocketTestWithInvalidPath, | 269 TEST_F(UnixDomainListenSocketTestWithInvalidPath, |
260 CreateAndListenWithAbstractNamespace) { | 270 CreateAndListenWithAbstractNamespace) { |
261 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( | 271 socket_ = UnixDomainListenSocket::CreateAndListenWithAbstractNamespace( |
262 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); | 272 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); |
263 EXPECT_FALSE(socket_.get() == NULL); | 273 EXPECT_FALSE(socket_.get() == NULL); |
264 } | 274 } |
265 | 275 |
266 TEST_F(UnixDomainSocketTest, TestFallbackName) { | 276 TEST_F(UnixDomainListenSocketTest, TestFallbackName) { |
267 scoped_ptr<UnixDomainSocket> existing_socket = | 277 scoped_ptr<UnixDomainListenSocket> existing_socket = |
268 UnixDomainSocket::CreateAndListenWithAbstractNamespace( | 278 UnixDomainListenSocket::CreateAndListenWithAbstractNamespace( |
269 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); | 279 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); |
270 EXPECT_FALSE(existing_socket.get() == NULL); | 280 EXPECT_FALSE(existing_socket.get() == NULL); |
271 // First, try to bind socket with the same name with no fallback name. | 281 // First, try to bind socket with the same name with no fallback name. |
272 socket_ = | 282 socket_ = |
273 UnixDomainSocket::CreateAndListenWithAbstractNamespace( | 283 UnixDomainListenSocket::CreateAndListenWithAbstractNamespace( |
274 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); | 284 file_path_.value(), "", socket_delegate_.get(), MakeAuthCallback()); |
275 EXPECT_TRUE(socket_.get() == NULL); | 285 EXPECT_TRUE(socket_.get() == NULL); |
276 // Now with a fallback name. | 286 // Now with a fallback name. |
277 const char kFallbackSocketName[] = "unix_domain_socket_for_testing_2"; | 287 const char kFallbackSocketName[] = "unix_domain_socket_for_testing_2"; |
278 socket_ = UnixDomainSocket::CreateAndListenWithAbstractNamespace( | 288 std::string fallback_socket_path = MakeSocketPath(kFallbackSocketName); |
| 289 socket_ = UnixDomainListenSocket::CreateAndListenWithAbstractNamespace( |
279 file_path_.value(), | 290 file_path_.value(), |
280 MakeSocketPath(kFallbackSocketName), | 291 fallback_socket_path, |
281 socket_delegate_.get(), | 292 socket_delegate_.get(), |
282 MakeAuthCallback()); | 293 MakeAuthCallback()); |
283 EXPECT_FALSE(socket_.get() == NULL); | 294 EXPECT_FALSE(socket_.get() == NULL); |
| 295 DeleteSocketPath(fallback_socket_path); |
284 } | 296 } |
285 #endif | 297 #endif |
286 | 298 |
287 TEST_F(UnixDomainSocketTest, TestWithClient) { | 299 TEST_F(UnixDomainListenSocketTest, TestWithClient) { |
288 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); | 300 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); |
289 EventType event = event_manager_->WaitForEvent(); | 301 EventType event = event_manager_->WaitForEvent(); |
290 ASSERT_EQ(EVENT_LISTEN, event); | 302 ASSERT_EQ(EVENT_LISTEN, event); |
291 | 303 |
292 // Create the client socket. | 304 // Create the client socket. |
293 const SocketDescriptor sock = CreateClientSocket(); | 305 const SocketDescriptor sock = CreateClientSocket(); |
294 ASSERT_NE(kInvalidSocket, sock); | 306 ASSERT_NE(kInvalidSocket, sock); |
295 event = event_manager_->WaitForEvent(); | 307 event = event_manager_->WaitForEvent(); |
296 ASSERT_EQ(EVENT_AUTH_GRANTED, event); | 308 ASSERT_EQ(EVENT_AUTH_GRANTED, event); |
297 event = event_manager_->WaitForEvent(); | 309 event = event_manager_->WaitForEvent(); |
298 ASSERT_EQ(EVENT_ACCEPT, event); | 310 ASSERT_EQ(EVENT_ACCEPT, event); |
299 | 311 |
300 // Send a message from the client to the server. | 312 // Send a message from the client to the server. |
301 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); | 313 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); |
302 ASSERT_NE(-1, ret); | 314 ASSERT_NE(-1, ret); |
303 ASSERT_EQ(sizeof(kMsg), static_cast<size_t>(ret)); | 315 ASSERT_EQ(sizeof(kMsg), static_cast<size_t>(ret)); |
304 event = event_manager_->WaitForEvent(); | 316 event = event_manager_->WaitForEvent(); |
305 ASSERT_EQ(EVENT_READ, event); | 317 ASSERT_EQ(EVENT_READ, event); |
306 ASSERT_EQ(kMsg, socket_delegate_->ReceivedData()); | 318 ASSERT_EQ(kMsg, socket_delegate_->ReceivedData()); |
307 | 319 |
308 // Close the client socket. | 320 // Close the client socket. |
309 ret = IGNORE_EINTR(close(sock)); | 321 ret = IGNORE_EINTR(close(sock)); |
310 event = event_manager_->WaitForEvent(); | 322 event = event_manager_->WaitForEvent(); |
311 ASSERT_EQ(EVENT_CLOSE, event); | 323 ASSERT_EQ(EVENT_CLOSE, event); |
312 } | 324 } |
313 | 325 |
314 TEST_F(UnixDomainSocketTestWithForbiddenUser, TestWithForbiddenUser) { | 326 TEST_F(UnixDomainListenSocketTestWithForbiddenUser, TestWithForbiddenUser) { |
315 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); | 327 const scoped_ptr<base::Thread> server_thread = CreateAndRunServerThread(); |
316 EventType event = event_manager_->WaitForEvent(); | 328 EventType event = event_manager_->WaitForEvent(); |
317 ASSERT_EQ(EVENT_LISTEN, event); | 329 ASSERT_EQ(EVENT_LISTEN, event); |
318 const SocketDescriptor sock = CreateClientSocket(); | 330 const SocketDescriptor sock = CreateClientSocket(); |
319 ASSERT_NE(kInvalidSocket, sock); | 331 ASSERT_NE(kInvalidSocket, sock); |
320 | 332 |
321 event = event_manager_->WaitForEvent(); | 333 event = event_manager_->WaitForEvent(); |
322 ASSERT_EQ(EVENT_AUTH_DENIED, event); | 334 ASSERT_EQ(EVENT_AUTH_DENIED, event); |
323 | 335 |
324 // Wait until the file descriptor is closed by the server. | 336 // Wait until the file descriptor is closed by the server. |
325 struct pollfd poll_fd; | 337 struct pollfd poll_fd; |
326 poll_fd.fd = sock; | 338 poll_fd.fd = sock; |
327 poll_fd.events = POLLIN; | 339 poll_fd.events = POLLIN; |
328 poll(&poll_fd, 1, -1 /* rely on GTest for timeout handling */); | 340 poll(&poll_fd, 1, -1 /* rely on GTest for timeout handling */); |
329 | 341 |
330 // Send() must fail. | 342 // Send() must fail. |
331 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); | 343 ssize_t ret = HANDLE_EINTR(send(sock, kMsg, sizeof(kMsg), 0)); |
332 ASSERT_EQ(-1, ret); | 344 ASSERT_EQ(-1, ret); |
333 ASSERT_EQ(EPIPE, errno); | 345 ASSERT_EQ(EPIPE, errno); |
334 ASSERT_FALSE(event_manager_->HasPendingEvent()); | 346 ASSERT_FALSE(event_manager_->HasPendingEvent()); |
335 } | 347 } |
336 | 348 |
337 } // namespace | 349 } // namespace |
338 } // namespace net | 350 } // namespace net |
OLD | NEW |