1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
// Copyright 2018 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "test/syscalls/linux/socket_unix.h"
#include <errno.h>
#include <net/if.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <vector>
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "test/syscalls/linux/socket_test_util.h"
#include "test/syscalls/linux/unix_domain_socket_test_util.h"
#include "test/util/test_util.h"
#include "test/util/thread_util.h"
// This file contains tests specific to Unix domain sockets. It does not contain
// tests for UDS control messages. Those belong in socket_unix_cmsg.cc.
//
// This file is a generic socket test file. It must be built with another file
// that provides the test types.
namespace gvisor {
namespace testing {
namespace {
TEST_P(UnixSocketPairTest, InvalidGetSockOpt) {
auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
int opt;
socklen_t optlen = sizeof(opt);
EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, -1, &opt, &optlen),
SyscallFailsWithErrno(ENOPROTOOPT));
}
TEST_P(UnixSocketPairTest, BindToBadName) {
auto pair =
ASSERT_NO_ERRNO_AND_VALUE(UnixDomainSocketPair(SOCK_SEQPACKET).Create());
constexpr char kBadName[] = "/some/path/that/does/not/exist";
sockaddr_un sockaddr;
sockaddr.sun_family = AF_LOCAL;
memcpy(sockaddr.sun_path, kBadName, sizeof(kBadName));
EXPECT_THAT(
bind(pair->first_fd(), reinterpret_cast<struct sockaddr*>(&sockaddr),
sizeof(sockaddr)),
SyscallFailsWithErrno(ENOENT));
}
TEST_P(UnixSocketPairTest, BindToBadFamily) {
auto pair =
ASSERT_NO_ERRNO_AND_VALUE(UnixDomainSocketPair(SOCK_SEQPACKET).Create());
constexpr char kBadName[] = "/some/path/that/does/not/exist";
sockaddr_un sockaddr;
sockaddr.sun_family = AF_INET;
memcpy(sockaddr.sun_path, kBadName, sizeof(kBadName));
EXPECT_THAT(
bind(pair->first_fd(), reinterpret_cast<struct sockaddr*>(&sockaddr),
sizeof(sockaddr)),
SyscallFailsWithErrno(EINVAL));
}
TEST_P(UnixSocketPairTest, RecvmmsgTimeoutAfterRecv) {
auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
char sent_data[10];
RandomizeBuffer(sent_data, sizeof(sent_data));
char received_data[sizeof(sent_data) * 2];
std::vector<struct mmsghdr> msgs(2);
std::vector<struct iovec> iovs(msgs.size());
const int chunk_size = sizeof(received_data) / msgs.size();
for (size_t i = 0; i < msgs.size(); i++) {
iovs[i].iov_len = chunk_size;
iovs[i].iov_base = &received_data[i * chunk_size];
msgs[i].msg_hdr.msg_iov = &iovs[i];
msgs[i].msg_hdr.msg_iovlen = 1;
}
ASSERT_THAT(WriteFd(sockets->first_fd(), sent_data, sizeof(sent_data)),
SyscallSucceedsWithValue(sizeof(sent_data)));
struct timespec timeout = {0, 1};
ASSERT_THAT(RetryEINTR(recvmmsg)(sockets->second_fd(), &msgs[0], msgs.size(),
0, &timeout),
SyscallSucceedsWithValue(1));
EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(sent_data)));
EXPECT_EQ(chunk_size, msgs[0].msg_len);
}
TEST_P(UnixSocketPairTest, TIOCINQSucceeds) {
auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
if (IsRunningOnGvisor()) {
// TODO(gvisor.dev/issue/273): Inherited host UDS don't support TIOCINQ.
// Skip the test.
int size = -1;
int ret = ioctl(sockets->first_fd(), TIOCINQ, &size);
SKIP_IF(ret == -1 && errno == ENOTTY);
}
int size = -1;
EXPECT_THAT(ioctl(sockets->first_fd(), TIOCINQ, &size), SyscallSucceeds());
EXPECT_EQ(size, 0);
const char some_data[] = "dangerzone";
ASSERT_THAT(
RetryEINTR(send)(sockets->second_fd(), &some_data, sizeof(some_data), 0),
SyscallSucceeds());
EXPECT_THAT(ioctl(sockets->first_fd(), TIOCINQ, &size), SyscallSucceeds());
EXPECT_EQ(size, sizeof(some_data));
// Linux only reports the first message's size, which is wrong. We test for
// the behavior described in the man page.
SKIP_IF(!IsRunningOnGvisor());
ASSERT_THAT(
RetryEINTR(send)(sockets->second_fd(), &some_data, sizeof(some_data), 0),
SyscallSucceeds());
EXPECT_THAT(ioctl(sockets->first_fd(), TIOCINQ, &size), SyscallSucceeds());
EXPECT_EQ(size, sizeof(some_data) * 2);
}
TEST_P(UnixSocketPairTest, TIOCOUTQSucceeds) {
auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
if (IsRunningOnGvisor()) {
// TODO(gvisor.dev/issue/273): Inherited host UDS don't support TIOCOUTQ.
// Skip the test.
int size = -1;
int ret = ioctl(sockets->second_fd(), TIOCOUTQ, &size);
SKIP_IF(ret == -1 && errno == ENOTTY);
}
int size = -1;
EXPECT_THAT(ioctl(sockets->second_fd(), TIOCOUTQ, &size), SyscallSucceeds());
EXPECT_EQ(size, 0);
// Linux reports bogus numbers which are related to its internal allocations.
// We test for the behavior described in the man page.
SKIP_IF(!IsRunningOnGvisor());
const char some_data[] = "dangerzone";
ASSERT_THAT(
RetryEINTR(send)(sockets->second_fd(), &some_data, sizeof(some_data), 0),
SyscallSucceeds());
EXPECT_THAT(ioctl(sockets->second_fd(), TIOCOUTQ, &size), SyscallSucceeds());
EXPECT_EQ(size, sizeof(some_data));
ASSERT_THAT(
RetryEINTR(send)(sockets->second_fd(), &some_data, sizeof(some_data), 0),
SyscallSucceeds());
EXPECT_THAT(ioctl(sockets->second_fd(), TIOCOUTQ, &size), SyscallSucceeds());
EXPECT_EQ(size, sizeof(some_data) * 2);
}
TEST_P(UnixSocketPairTest, NetdeviceIoctlsSucceed) {
auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
// Prepare the request.
struct ifreq ifr;
snprintf(ifr.ifr_name, IFNAMSIZ, "lo");
// Check that the ioctl either succeeds or fails with ENODEV.
int err = ioctl(sockets->first_fd(), SIOCGIFINDEX, &ifr);
if (err < 0) {
ASSERT_EQ(errno, ENODEV);
}
}
TEST_P(UnixSocketPairTest, Shutdown) {
auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
const std::string data = "abc";
ASSERT_THAT(WriteFd(sockets->first_fd(), data.c_str(), data.size()),
SyscallSucceedsWithValue(data.size()));
ASSERT_THAT(shutdown(sockets->first_fd(), SHUT_RDWR), SyscallSucceeds());
ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_RDWR), SyscallSucceeds());
// Shutting down a socket does not clear the buffer.
char buf[3];
ASSERT_THAT(ReadFd(sockets->second_fd(), buf, data.size()),
SyscallSucceedsWithValue(data.size()));
EXPECT_EQ(data, absl::string_view(buf, data.size()));
}
TEST_P(UnixSocketPairTest, ShutdownRead) {
auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
ASSERT_THAT(shutdown(sockets->first_fd(), SHUT_RD), SyscallSucceeds());
// When the socket is shutdown for read, read behavior varies between
// different socket types. This is covered by the various ReadOneSideClosed
// test cases.
// ... and the peer cannot write.
const std::string data = "abc";
EXPECT_THAT(WriteFd(sockets->second_fd(), data.c_str(), data.size()),
SyscallFailsWithErrno(EPIPE));
// ... but the socket can still write.
ASSERT_THAT(WriteFd(sockets->first_fd(), data.c_str(), data.size()),
SyscallSucceedsWithValue(data.size()));
// ... and the peer can still read.
char buf[3];
EXPECT_THAT(ReadFd(sockets->second_fd(), buf, data.size()),
SyscallSucceedsWithValue(data.size()));
EXPECT_EQ(data, absl::string_view(buf, data.size()));
}
TEST_P(UnixSocketPairTest, ShutdownWrite) {
auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
ASSERT_THAT(shutdown(sockets->first_fd(), SHUT_WR), SyscallSucceeds());
// When the socket is shutdown for write, it cannot write.
const std::string data = "abc";
EXPECT_THAT(WriteFd(sockets->first_fd(), data.c_str(), data.size()),
SyscallFailsWithErrno(EPIPE));
// ... and the peer read behavior varies between different socket types. This
// is covered by the various ReadOneSideClosed test cases.
// ... but the peer can still write.
char buf[3];
ASSERT_THAT(WriteFd(sockets->second_fd(), data.c_str(), data.size()),
SyscallSucceedsWithValue(data.size()));
// ... and the socket can still read.
EXPECT_THAT(ReadFd(sockets->first_fd(), buf, data.size()),
SyscallSucceedsWithValue(data.size()));
EXPECT_EQ(data, absl::string_view(buf, data.size()));
}
TEST_P(UnixSocketPairTest, SocketReopenFromProcfs) {
// TODO(b/122310852): We should be returning ENXIO and NOT EIO.
// TODO(github.dev/issue/1624): This should be resolved in VFS2. Verify
// that this is the case and delete the SKIP_IF once we delete VFS1.
SKIP_IF(IsRunningOnGvisor());
auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
// Opening a socket pair via /proc/self/fd/X is a ENXIO.
for (const int fd : {sockets->first_fd(), sockets->second_fd()}) {
ASSERT_THAT(Open(absl::StrCat("/proc/self/fd/", fd), O_WRONLY),
PosixErrorIs(ENXIO, ::testing::_));
}
}
} // namespace
} // namespace testing
} // namespace gvisor
|