[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 1 | // Copyright 2013 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 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 5 | #include "ipc/unix_domain_socket_util.h" |
| 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 8 | #include <sys/socket.h> |
| 9 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 10 | #include <memory> |
| 11 | |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 14 | #include "base/location.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 16 | #include "base/path_service.h" |
[email protected] | 47163d4 | 2013-05-16 02:37:50 | [diff] [blame] | 17 | #include "base/posix/eintr_wrapper.h" |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 18 | #include "base/single_thread_task_runner.h" |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 19 | #include "base/synchronization/waitable_event.h" |
| 20 | #include "base/threading/thread.h" |
| 21 | #include "base/threading/thread_restrictions.h" |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | |
| 24 | namespace { |
| 25 | |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 26 | class SocketAcceptor : public base::MessageLoopForIO::Watcher { |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 27 | public: |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 28 | SocketAcceptor(int fd, base::SingleThreadTaskRunner* target_thread) |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 29 | : server_fd_(-1), |
| 30 | target_thread_(target_thread), |
gab | 90c2c5c | 2016-06-01 20:34:06 | [diff] [blame] | 31 | started_watching_event_( |
| 32 | base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 33 | base::WaitableEvent::InitialState::NOT_SIGNALED), |
| 34 | accepted_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 35 | base::WaitableEvent::InitialState::NOT_SIGNALED) { |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 36 | target_thread->PostTask(FROM_HERE, |
| 37 | base::Bind(&SocketAcceptor::StartWatching, base::Unretained(this), fd)); |
| 38 | } |
| 39 | |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 40 | ~SocketAcceptor() override { |
dcheng | f3076af | 2014-10-21 18:02:42 | [diff] [blame] | 41 | Close(); |
| 42 | } |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 43 | |
| 44 | int server_fd() const { return server_fd_; } |
| 45 | |
| 46 | void WaitUntilReady() { |
| 47 | started_watching_event_.Wait(); |
| 48 | } |
| 49 | |
| 50 | void WaitForAccept() { |
| 51 | accepted_event_.Wait(); |
| 52 | } |
| 53 | |
| 54 | void Close() { |
| 55 | if (watcher_.get()) { |
| 56 | target_thread_->PostTask(FROM_HERE, |
| 57 | base::Bind(&SocketAcceptor::StopWatching, base::Unretained(this), |
| 58 | watcher_.release())); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | private: |
| 63 | void StartWatching(int fd) { |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 64 | watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher); |
| 65 | base::MessageLoopForIO::current()->WatchFileDescriptor( |
| 66 | fd, true, base::MessageLoopForIO::WATCH_READ, watcher_.get(), this); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 67 | started_watching_event_.Signal(); |
| 68 | } |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 69 | void StopWatching(base::MessageLoopForIO::FileDescriptorWatcher* watcher) { |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 70 | watcher->StopWatchingFileDescriptor(); |
| 71 | delete watcher; |
| 72 | } |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 73 | void OnFileCanReadWithoutBlocking(int fd) override { |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 74 | ASSERT_EQ(-1, server_fd_); |
ben | e1bbc00 | 2016-07-05 16:04:36 | [diff] [blame] | 75 | IPC::ServerOnConnect(fd, &server_fd_); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 76 | watcher_->StopWatchingFileDescriptor(); |
| 77 | accepted_event_.Signal(); |
| 78 | } |
dcheng | fe61fca | 2014-10-22 02:29:52 | [diff] [blame] | 79 | void OnFileCanWriteWithoutBlocking(int fd) override {} |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 80 | |
| 81 | int server_fd_; |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 82 | base::SingleThreadTaskRunner* target_thread_; |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 83 | std::unique_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 84 | base::WaitableEvent started_watching_event_; |
| 85 | base::WaitableEvent accepted_event_; |
| 86 | |
| 87 | DISALLOW_COPY_AND_ASSIGN(SocketAcceptor); |
| 88 | }; |
| 89 | |
| 90 | const base::FilePath GetChannelDir() { |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 91 | base::FilePath tmp_dir; |
byungchul | e2295eb | 2015-01-22 17:43:59 | [diff] [blame] | 92 | PathService::Get(base::DIR_TEMP, &tmp_dir); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 93 | return tmp_dir; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | class TestUnixSocketConnection { |
| 97 | public: |
| 98 | TestUnixSocketConnection() |
| 99 | : worker_("WorkerThread"), |
| 100 | server_listen_fd_(-1), |
| 101 | server_fd_(-1), |
| 102 | client_fd_(-1) { |
| 103 | socket_name_ = GetChannelDir().Append("TestSocket"); |
| 104 | base::Thread::Options options; |
[email protected] | fd0a773a | 2013-04-30 20:55:03 | [diff] [blame] | 105 | options.message_loop_type = base::MessageLoop::TYPE_IO; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 106 | worker_.StartWithOptions(options); |
| 107 | } |
| 108 | |
| 109 | bool CreateServerSocket() { |
| 110 | IPC::CreateServerUnixDomainSocket(socket_name_, &server_listen_fd_); |
| 111 | if (server_listen_fd_ < 0) |
| 112 | return false; |
| 113 | struct stat socket_stat; |
| 114 | stat(socket_name_.value().c_str(), &socket_stat); |
| 115 | EXPECT_TRUE(S_ISSOCK(socket_stat.st_mode)); |
skyostil | e687bdff | 2015-05-12 11:29:21 | [diff] [blame] | 116 | acceptor_.reset( |
| 117 | new SocketAcceptor(server_listen_fd_, worker_.task_runner().get())); |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 118 | acceptor_->WaitUntilReady(); |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | bool CreateClientSocket() { |
| 123 | DCHECK(server_listen_fd_ >= 0); |
| 124 | IPC::CreateClientUnixDomainSocket(socket_name_, &client_fd_); |
| 125 | if (client_fd_ < 0) |
| 126 | return false; |
| 127 | acceptor_->WaitForAccept(); |
| 128 | server_fd_ = acceptor_->server_fd(); |
| 129 | return server_fd_ >= 0; |
| 130 | } |
| 131 | |
| 132 | virtual ~TestUnixSocketConnection() { |
| 133 | if (client_fd_ >= 0) |
| 134 | close(client_fd_); |
| 135 | if (server_fd_ >= 0) |
| 136 | close(server_fd_); |
| 137 | if (server_listen_fd_ >= 0) { |
| 138 | close(server_listen_fd_); |
| 139 | unlink(socket_name_.value().c_str()); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | int client_fd() const { return client_fd_; } |
| 144 | int server_fd() const { return server_fd_; } |
| 145 | |
| 146 | private: |
| 147 | base::Thread worker_; |
| 148 | base::FilePath socket_name_; |
| 149 | int server_listen_fd_; |
| 150 | int server_fd_; |
| 151 | int client_fd_; |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 152 | std::unique_ptr<SocketAcceptor> acceptor_; |
[email protected] | bdf9bdc | 2013-03-13 04:23:10 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | // Ensure that IPC::CreateServerUnixDomainSocket creates a socket that |
| 156 | // IPC::CreateClientUnixDomainSocket can successfully connect to. |
| 157 | TEST(UnixDomainSocketUtil, Connect) { |
| 158 | TestUnixSocketConnection connection; |
| 159 | ASSERT_TRUE(connection.CreateServerSocket()); |
| 160 | ASSERT_TRUE(connection.CreateClientSocket()); |
| 161 | } |
| 162 | |
| 163 | // Ensure that messages can be sent across the resulting socket. |
| 164 | TEST(UnixDomainSocketUtil, SendReceive) { |
| 165 | TestUnixSocketConnection connection; |
| 166 | ASSERT_TRUE(connection.CreateServerSocket()); |
| 167 | ASSERT_TRUE(connection.CreateClientSocket()); |
| 168 | |
| 169 | const char buffer[] = "Hello, server!"; |
| 170 | size_t buf_len = sizeof(buffer); |
| 171 | size_t sent_bytes = |
| 172 | HANDLE_EINTR(send(connection.client_fd(), buffer, buf_len, 0)); |
| 173 | ASSERT_EQ(buf_len, sent_bytes); |
| 174 | char recv_buf[sizeof(buffer)]; |
| 175 | size_t received_bytes = |
| 176 | HANDLE_EINTR(recv(connection.server_fd(), recv_buf, buf_len, 0)); |
| 177 | ASSERT_EQ(buf_len, received_bytes); |
| 178 | ASSERT_EQ(0, memcmp(recv_buf, buffer, buf_len)); |
| 179 | } |
| 180 | |
| 181 | } // namespace |