blob: 3fa4454638fb31672defdbe4ebd6903aa256e422 [file] [log] [blame]
[email protected]bdf9bdc2013-03-13 04:23:101// 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
danakj03de39b22016-04-23 04:21:095#include "ipc/unix_domain_socket_util.h"
6
avi246998d82015-12-22 02:39:047#include <stddef.h>
[email protected]bdf9bdc2013-03-13 04:23:108#include <sys/socket.h>
9
danakj03de39b22016-04-23 04:21:0910#include <memory>
11
[email protected]bdf9bdc2013-03-13 04:23:1012#include "base/bind.h"
[email protected]bdf9bdc2013-03-13 04:23:1013#include "base/files/file_path.h"
skyostile687bdff2015-05-12 11:29:2114#include "base/location.h"
avi246998d82015-12-22 02:39:0415#include "base/macros.h"
[email protected]bdf9bdc2013-03-13 04:23:1016#include "base/path_service.h"
[email protected]47163d42013-05-16 02:37:5017#include "base/posix/eintr_wrapper.h"
skyostile687bdff2015-05-12 11:29:2118#include "base/single_thread_task_runner.h"
[email protected]bdf9bdc2013-03-13 04:23:1019#include "base/synchronization/waitable_event.h"
20#include "base/threading/thread.h"
21#include "base/threading/thread_restrictions.h"
[email protected]bdf9bdc2013-03-13 04:23:1022#include "testing/gtest/include/gtest/gtest.h"
23
24namespace {
25
[email protected]fd0a773a2013-04-30 20:55:0326class SocketAcceptor : public base::MessageLoopForIO::Watcher {
[email protected]bdf9bdc2013-03-13 04:23:1027 public:
skyostile687bdff2015-05-12 11:29:2128 SocketAcceptor(int fd, base::SingleThreadTaskRunner* target_thread)
[email protected]bdf9bdc2013-03-13 04:23:1029 : server_fd_(-1),
30 target_thread_(target_thread),
31 started_watching_event_(false, false),
32 accepted_event_(false, false) {
33 target_thread->PostTask(FROM_HERE,
34 base::Bind(&SocketAcceptor::StartWatching, base::Unretained(this), fd));
35 }
36
dchengfe61fca2014-10-22 02:29:5237 ~SocketAcceptor() override {
dchengf3076af2014-10-21 18:02:4238 Close();
39 }
[email protected]bdf9bdc2013-03-13 04:23:1040
41 int server_fd() const { return server_fd_; }
42
43 void WaitUntilReady() {
44 started_watching_event_.Wait();
45 }
46
47 void WaitForAccept() {
48 accepted_event_.Wait();
49 }
50
51 void Close() {
52 if (watcher_.get()) {
53 target_thread_->PostTask(FROM_HERE,
54 base::Bind(&SocketAcceptor::StopWatching, base::Unretained(this),
55 watcher_.release()));
56 }
57 }
58
59 private:
60 void StartWatching(int fd) {
[email protected]fd0a773a2013-04-30 20:55:0361 watcher_.reset(new base::MessageLoopForIO::FileDescriptorWatcher);
62 base::MessageLoopForIO::current()->WatchFileDescriptor(
63 fd, true, base::MessageLoopForIO::WATCH_READ, watcher_.get(), this);
[email protected]bdf9bdc2013-03-13 04:23:1064 started_watching_event_.Signal();
65 }
[email protected]fd0a773a2013-04-30 20:55:0366 void StopWatching(base::MessageLoopForIO::FileDescriptorWatcher* watcher) {
[email protected]bdf9bdc2013-03-13 04:23:1067 watcher->StopWatchingFileDescriptor();
68 delete watcher;
69 }
dchengfe61fca2014-10-22 02:29:5270 void OnFileCanReadWithoutBlocking(int fd) override {
[email protected]bdf9bdc2013-03-13 04:23:1071 ASSERT_EQ(-1, server_fd_);
72 IPC::ServerAcceptConnection(fd, &server_fd_);
73 watcher_->StopWatchingFileDescriptor();
74 accepted_event_.Signal();
75 }
dchengfe61fca2014-10-22 02:29:5276 void OnFileCanWriteWithoutBlocking(int fd) override {}
[email protected]bdf9bdc2013-03-13 04:23:1077
78 int server_fd_;
skyostile687bdff2015-05-12 11:29:2179 base::SingleThreadTaskRunner* target_thread_;
danakj03de39b22016-04-23 04:21:0980 std::unique_ptr<base::MessageLoopForIO::FileDescriptorWatcher> watcher_;
[email protected]bdf9bdc2013-03-13 04:23:1081 base::WaitableEvent started_watching_event_;
82 base::WaitableEvent accepted_event_;
83
84 DISALLOW_COPY_AND_ASSIGN(SocketAcceptor);
85};
86
87const base::FilePath GetChannelDir() {
[email protected]bdf9bdc2013-03-13 04:23:1088 base::FilePath tmp_dir;
byungchule2295eb2015-01-22 17:43:5989 PathService::Get(base::DIR_TEMP, &tmp_dir);
[email protected]bdf9bdc2013-03-13 04:23:1090 return tmp_dir;
[email protected]bdf9bdc2013-03-13 04:23:1091}
92
93class TestUnixSocketConnection {
94 public:
95 TestUnixSocketConnection()
96 : worker_("WorkerThread"),
97 server_listen_fd_(-1),
98 server_fd_(-1),
99 client_fd_(-1) {
100 socket_name_ = GetChannelDir().Append("TestSocket");
101 base::Thread::Options options;
[email protected]fd0a773a2013-04-30 20:55:03102 options.message_loop_type = base::MessageLoop::TYPE_IO;
[email protected]bdf9bdc2013-03-13 04:23:10103 worker_.StartWithOptions(options);
104 }
105
106 bool CreateServerSocket() {
107 IPC::CreateServerUnixDomainSocket(socket_name_, &server_listen_fd_);
108 if (server_listen_fd_ < 0)
109 return false;
110 struct stat socket_stat;
111 stat(socket_name_.value().c_str(), &socket_stat);
112 EXPECT_TRUE(S_ISSOCK(socket_stat.st_mode));
skyostile687bdff2015-05-12 11:29:21113 acceptor_.reset(
114 new SocketAcceptor(server_listen_fd_, worker_.task_runner().get()));
[email protected]bdf9bdc2013-03-13 04:23:10115 acceptor_->WaitUntilReady();
116 return true;
117 }
118
119 bool CreateClientSocket() {
120 DCHECK(server_listen_fd_ >= 0);
121 IPC::CreateClientUnixDomainSocket(socket_name_, &client_fd_);
122 if (client_fd_ < 0)
123 return false;
124 acceptor_->WaitForAccept();
125 server_fd_ = acceptor_->server_fd();
126 return server_fd_ >= 0;
127 }
128
129 virtual ~TestUnixSocketConnection() {
130 if (client_fd_ >= 0)
131 close(client_fd_);
132 if (server_fd_ >= 0)
133 close(server_fd_);
134 if (server_listen_fd_ >= 0) {
135 close(server_listen_fd_);
136 unlink(socket_name_.value().c_str());
137 }
138 }
139
140 int client_fd() const { return client_fd_; }
141 int server_fd() const { return server_fd_; }
142
143 private:
144 base::Thread worker_;
145 base::FilePath socket_name_;
146 int server_listen_fd_;
147 int server_fd_;
148 int client_fd_;
danakj03de39b22016-04-23 04:21:09149 std::unique_ptr<SocketAcceptor> acceptor_;
[email protected]bdf9bdc2013-03-13 04:23:10150};
151
152// Ensure that IPC::CreateServerUnixDomainSocket creates a socket that
153// IPC::CreateClientUnixDomainSocket can successfully connect to.
154TEST(UnixDomainSocketUtil, Connect) {
155 TestUnixSocketConnection connection;
156 ASSERT_TRUE(connection.CreateServerSocket());
157 ASSERT_TRUE(connection.CreateClientSocket());
158}
159
160// Ensure that messages can be sent across the resulting socket.
161TEST(UnixDomainSocketUtil, SendReceive) {
162 TestUnixSocketConnection connection;
163 ASSERT_TRUE(connection.CreateServerSocket());
164 ASSERT_TRUE(connection.CreateClientSocket());
165
166 const char buffer[] = "Hello, server!";
167 size_t buf_len = sizeof(buffer);
168 size_t sent_bytes =
169 HANDLE_EINTR(send(connection.client_fd(), buffer, buf_len, 0));
170 ASSERT_EQ(buf_len, sent_bytes);
171 char recv_buf[sizeof(buffer)];
172 size_t received_bytes =
173 HANDLE_EINTR(recv(connection.server_fd(), recv_buf, buf_len, 0));
174 ASSERT_EQ(buf_len, received_bytes);
175 ASSERT_EQ(0, memcmp(recv_buf, buffer, buf_len));
176}
177
178} // namespace