github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/socket_generic_test_cases.cc (about) 1 // Copyright 2018 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "test/syscalls/linux/socket_generic.h" 16 17 #include <stdio.h> 18 #include <sys/ioctl.h> 19 #include <sys/socket.h> 20 #include <sys/un.h> 21 22 #include "gtest/gtest.h" 23 #include "absl/strings/str_format.h" 24 #include "absl/strings/string_view.h" 25 #include "test/syscalls/linux/socket_test_util.h" 26 #include "test/syscalls/linux/unix_domain_socket_test_util.h" 27 #include "test/util/test_util.h" 28 29 // This file is a generic socket test file. It must be built with another file 30 // that provides the test types. 31 32 namespace gvisor { 33 namespace testing { 34 35 TEST_P(AllSocketPairTest, BasicReadWrite) { 36 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 37 char buf[20]; 38 const std::string data = "abc"; 39 ASSERT_THAT(WriteFd(sockets->first_fd(), data.c_str(), 3), 40 SyscallSucceedsWithValue(3)); 41 ASSERT_THAT(ReadFd(sockets->second_fd(), buf, 3), 42 SyscallSucceedsWithValue(3)); 43 EXPECT_EQ(data, absl::string_view(buf, 3)); 44 } 45 46 TEST_P(AllSocketPairTest, BasicReadWriteBadBuffer) { 47 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 48 const std::string data = "abc"; 49 ASSERT_THAT(WriteFd(sockets->first_fd(), data.c_str(), 3), 50 SyscallSucceedsWithValue(3)); 51 ASSERT_THAT(ReadFd(sockets->second_fd(), nullptr, 3), 52 SyscallFailsWithErrno(EFAULT)); 53 } 54 55 TEST_P(AllSocketPairTest, BasicSendRecv) { 56 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 57 char sent_data[512]; 58 RandomizeBuffer(sent_data, sizeof(sent_data)); 59 ASSERT_THAT( 60 RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0), 61 SyscallSucceedsWithValue(sizeof(sent_data))); 62 char received_data[sizeof(sent_data)]; 63 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data, 64 sizeof(received_data), 0), 65 SyscallSucceedsWithValue(sizeof(received_data))); 66 EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(sent_data))); 67 } 68 69 TEST_P(AllSocketPairTest, BasicSendmmsg) { 70 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 71 char sent_data[200]; 72 RandomizeBuffer(sent_data, sizeof(sent_data)); 73 74 std::vector<struct mmsghdr> msgs(10); 75 std::vector<struct iovec> iovs(msgs.size()); 76 const int chunk_size = sizeof(sent_data) / msgs.size(); 77 for (size_t i = 0; i < msgs.size(); i++) { 78 iovs[i].iov_len = chunk_size; 79 iovs[i].iov_base = &sent_data[i * chunk_size]; 80 msgs[i].msg_hdr.msg_iov = &iovs[i]; 81 msgs[i].msg_hdr.msg_iovlen = 1; 82 } 83 84 ASSERT_THAT( 85 RetryEINTR(sendmmsg)(sockets->first_fd(), &msgs[0], msgs.size(), 0), 86 SyscallSucceedsWithValue(msgs.size())); 87 88 for (const struct mmsghdr& msg : msgs) { 89 EXPECT_EQ(chunk_size, msg.msg_len); 90 } 91 92 char received_data[sizeof(sent_data)]; 93 for (size_t i = 0; i < msgs.size(); i++) { 94 ASSERT_THAT(ReadFd(sockets->second_fd(), &received_data[i * chunk_size], 95 chunk_size), 96 SyscallSucceedsWithValue(chunk_size)); 97 } 98 EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(sent_data))); 99 } 100 101 TEST_P(AllSocketPairTest, SendmmsgIsLimitedByMAXIOV) { 102 std::unique_ptr<SocketPair> sockets = 103 ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 104 char c = 0; 105 106 std::vector<struct mmsghdr> msgs(UIO_MAXIOV + 1); 107 std::vector<struct iovec> iovs(msgs.size()); 108 for (size_t i = 0; i < msgs.size(); i++) { 109 iovs[i].iov_len = 1; 110 iovs[i].iov_base = &c; 111 msgs[i].msg_hdr.msg_iov = &iovs[i]; 112 msgs[i].msg_hdr.msg_iovlen = 1; 113 } 114 115 int n; 116 ASSERT_THAT(n = RetryEINTR(sendmmsg)(sockets->first_fd(), msgs.data(), 117 msgs.size(), MSG_DONTWAIT), 118 SyscallSucceeds()); 119 EXPECT_LE(n, UIO_MAXIOV); 120 } 121 122 TEST_P(AllSocketPairTest, BasicRecvmmsg) { 123 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 124 char sent_data[200]; 125 RandomizeBuffer(sent_data, sizeof(sent_data)); 126 127 char received_data[sizeof(sent_data)]; 128 std::vector<struct mmsghdr> msgs(10); 129 std::vector<struct iovec> iovs(msgs.size()); 130 const int chunk_size = sizeof(sent_data) / msgs.size(); 131 for (size_t i = 0; i < msgs.size(); i++) { 132 iovs[i].iov_len = chunk_size; 133 iovs[i].iov_base = &received_data[i * chunk_size]; 134 msgs[i].msg_hdr.msg_iov = &iovs[i]; 135 msgs[i].msg_hdr.msg_iovlen = 1; 136 } 137 138 for (size_t i = 0; i < msgs.size(); i++) { 139 ASSERT_THAT( 140 WriteFd(sockets->first_fd(), &sent_data[i * chunk_size], chunk_size), 141 SyscallSucceedsWithValue(chunk_size)); 142 } 143 144 ASSERT_THAT(RetryEINTR(recvmmsg)(sockets->second_fd(), &msgs[0], msgs.size(), 145 0, nullptr), 146 SyscallSucceedsWithValue(msgs.size())); 147 148 EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(sent_data))); 149 150 for (const struct mmsghdr& msg : msgs) { 151 EXPECT_EQ(chunk_size, msg.msg_len); 152 } 153 } 154 155 TEST_P(AllSocketPairTest, SendmsgRecvmsg10KB) { 156 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 157 std::vector<char> sent_data(10 * 1024); 158 RandomizeBuffer(sent_data.data(), sent_data.size()); 159 ASSERT_NO_FATAL_FAILURE( 160 SendNullCmsg(sockets->first_fd(), sent_data.data(), sent_data.size())); 161 162 std::vector<char> received_data(sent_data.size()); 163 ASSERT_NO_FATAL_FAILURE(RecvNoCmsg(sockets->second_fd(), received_data.data(), 164 received_data.size())); 165 166 EXPECT_EQ(0, 167 memcmp(sent_data.data(), received_data.data(), sent_data.size())); 168 } 169 170 // This test validates that a sendmsg/recvmsg w/ MSG_CTRUNC is a no-op on 171 // input flags. 172 TEST_P(AllSocketPairTest, SendmsgRecvmsgMsgCtruncNoop) { 173 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 174 std::vector<char> sent_data(10 * 1024); 175 RandomizeBuffer(sent_data.data(), sent_data.size()); 176 ASSERT_NO_FATAL_FAILURE( 177 SendNullCmsg(sockets->first_fd(), sent_data.data(), sent_data.size())); 178 179 std::vector<char> received_data(sent_data.size()); 180 struct msghdr msg = {}; 181 char control[CMSG_SPACE(sizeof(int)) + CMSG_SPACE(sizeof(struct ucred))]; 182 msg.msg_control = control; 183 msg.msg_controllen = sizeof(control); 184 185 struct iovec iov; 186 iov.iov_base = &received_data[0]; 187 iov.iov_len = received_data.size(); 188 msg.msg_iov = &iov; 189 msg.msg_iovlen = 1; 190 191 // MSG_CTRUNC should be a no-op. 192 ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &msg, MSG_CTRUNC), 193 SyscallSucceedsWithValue(received_data.size())); 194 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); 195 EXPECT_EQ(cmsg, nullptr); 196 EXPECT_EQ(msg.msg_controllen, 0); 197 EXPECT_EQ(0, 198 memcmp(sent_data.data(), received_data.data(), sent_data.size())); 199 } 200 201 TEST_P(AllSocketPairTest, SendmsgRecvmsg16KB) { 202 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 203 std::vector<char> sent_data(16 * 1024); 204 RandomizeBuffer(sent_data.data(), sent_data.size()); 205 ASSERT_NO_FATAL_FAILURE( 206 SendNullCmsg(sockets->first_fd(), sent_data.data(), sent_data.size())); 207 208 std::vector<char> received_data(sent_data.size()); 209 ASSERT_NO_FATAL_FAILURE(RecvNoCmsg(sockets->second_fd(), received_data.data(), 210 received_data.size())); 211 212 EXPECT_EQ(0, 213 memcmp(sent_data.data(), received_data.data(), sent_data.size())); 214 } 215 216 TEST_P(AllSocketPairTest, RecvmsgMsghdrFlagsNotClearedOnFailure) { 217 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 218 219 char received_data[10] = {}; 220 221 struct iovec iov; 222 iov.iov_base = received_data; 223 iov.iov_len = sizeof(received_data); 224 struct msghdr msg = {}; 225 msg.msg_flags = -1; 226 msg.msg_iov = &iov; 227 msg.msg_iovlen = 1; 228 229 ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &msg, MSG_DONTWAIT), 230 SyscallFailsWithErrno(EAGAIN)); 231 232 // Check that msghdr flags were not changed. 233 EXPECT_EQ(msg.msg_flags, -1); 234 } 235 236 TEST_P(AllSocketPairTest, RecvmsgMsghdrFlagsCleared) { 237 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 238 239 char sent_data[10]; 240 RandomizeBuffer(sent_data, sizeof(sent_data)); 241 ASSERT_THAT( 242 RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0), 243 SyscallSucceedsWithValue(sizeof(sent_data))); 244 245 char received_data[sizeof(sent_data)] = {}; 246 247 struct iovec iov; 248 iov.iov_base = received_data; 249 iov.iov_len = sizeof(received_data); 250 struct msghdr msg = {}; 251 msg.msg_flags = -1; 252 msg.msg_iov = &iov; 253 msg.msg_iovlen = 1; 254 255 ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &msg, 0), 256 SyscallSucceedsWithValue(sizeof(sent_data))); 257 EXPECT_EQ(0, memcmp(received_data, sent_data, sizeof(sent_data))); 258 259 // Check that msghdr flags were cleared. 260 EXPECT_EQ(msg.msg_flags, 0); 261 } 262 263 TEST_P(AllSocketPairTest, RecvmsgPeekMsghdrFlagsCleared) { 264 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 265 266 char sent_data[10]; 267 RandomizeBuffer(sent_data, sizeof(sent_data)); 268 ASSERT_THAT( 269 RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0), 270 SyscallSucceedsWithValue(sizeof(sent_data))); 271 272 char received_data[sizeof(sent_data)] = {}; 273 274 struct iovec iov; 275 iov.iov_base = received_data; 276 iov.iov_len = sizeof(received_data); 277 struct msghdr msg = {}; 278 msg.msg_flags = -1; 279 msg.msg_iov = &iov; 280 msg.msg_iovlen = 1; 281 282 ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &msg, MSG_PEEK), 283 SyscallSucceedsWithValue(sizeof(sent_data))); 284 EXPECT_EQ(0, memcmp(received_data, sent_data, sizeof(sent_data))); 285 286 // Check that msghdr flags were cleared. 287 EXPECT_EQ(msg.msg_flags, 0); 288 } 289 290 TEST_P(AllSocketPairTest, RecvmsgIovNotUpdated) { 291 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 292 293 char sent_data[10]; 294 RandomizeBuffer(sent_data, sizeof(sent_data)); 295 ASSERT_THAT( 296 RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0), 297 SyscallSucceedsWithValue(sizeof(sent_data))); 298 299 char received_data[sizeof(sent_data) * 2] = {}; 300 301 struct iovec iov; 302 iov.iov_base = received_data; 303 iov.iov_len = sizeof(received_data); 304 struct msghdr msg = {}; 305 msg.msg_iov = &iov; 306 msg.msg_iovlen = 1; 307 308 ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &msg, 0), 309 SyscallSucceedsWithValue(sizeof(sent_data))); 310 EXPECT_EQ(0, memcmp(received_data, sent_data, sizeof(sent_data))); 311 312 // Check that the iovec length was not updated. 313 EXPECT_EQ(msg.msg_iov->iov_len, sizeof(received_data)); 314 } 315 316 TEST_P(AllSocketPairTest, RecvmmsgInvalidTimeout) { 317 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 318 char buf[10]; 319 struct mmsghdr msg = {}; 320 struct iovec iov = {}; 321 iov.iov_len = sizeof(buf); 322 iov.iov_base = buf; 323 msg.msg_hdr.msg_iov = &iov; 324 msg.msg_hdr.msg_iovlen = 1; 325 struct timespec timeout = {-1, -1}; 326 ASSERT_THAT(RetryEINTR(recvmmsg)(sockets->first_fd(), &msg, 1, 0, &timeout), 327 SyscallFailsWithErrno(EINVAL)); 328 } 329 330 TEST_P(AllSocketPairTest, RecvmmsgTimeoutBeforeRecv) { 331 // There is a known bug in the Linux recvmmsg(2) causing it to block forever 332 // if the timeout expires while blocking for the first message. 333 SKIP_IF(!IsRunningOnGvisor()); 334 335 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 336 char buf[10]; 337 struct mmsghdr msg = {}; 338 struct iovec iov = {}; 339 iov.iov_len = sizeof(buf); 340 iov.iov_base = buf; 341 msg.msg_hdr.msg_iov = &iov; 342 msg.msg_hdr.msg_iovlen = 1; 343 struct timespec timeout = {}; 344 ASSERT_THAT(RetryEINTR(recvmmsg)(sockets->first_fd(), &msg, 1, 0, &timeout), 345 SyscallFailsWithErrno(EAGAIN)); 346 } 347 348 TEST_P(AllSocketPairTest, MsgPeek) { 349 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 350 char sent_data[50]; 351 memset(&sent_data, 0, sizeof(sent_data)); 352 ASSERT_THAT(WriteFd(sockets->first_fd(), sent_data, sizeof(sent_data)), 353 SyscallSucceedsWithValue(sizeof(sent_data))); 354 355 char received_data[sizeof(sent_data)]; 356 for (int i = 0; i < 3; i++) { 357 memset(received_data, 0, sizeof(received_data)); 358 EXPECT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data, 359 sizeof(received_data), MSG_PEEK), 360 SyscallSucceedsWithValue(sizeof(received_data))); 361 EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(received_data))); 362 } 363 364 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data, 365 sizeof(received_data), 0), 366 SyscallSucceedsWithValue(sizeof(received_data))); 367 EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(received_data))); 368 } 369 370 TEST_P(AllSocketPairTest, LingerSocketOption) { 371 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 372 struct linger got_linger = {-1, -1}; 373 socklen_t length = sizeof(struct linger); 374 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 375 &got_linger, &length), 376 SyscallSucceedsWithValue(0)); 377 struct linger want_linger = {}; 378 EXPECT_EQ(0, memcmp(&want_linger, &got_linger, sizeof(struct linger))); 379 EXPECT_EQ(sizeof(struct linger), length); 380 } 381 382 TEST_P(AllSocketPairTest, KeepAliveSocketOption) { 383 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 384 int keepalive = -1; 385 socklen_t length = sizeof(int); 386 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, 387 &keepalive, &length), 388 SyscallSucceedsWithValue(0)); 389 EXPECT_EQ(0, keepalive); 390 EXPECT_EQ(sizeof(int), length); 391 } 392 393 TEST_P(AllSocketPairTest, RcvBufSucceeds) { 394 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 395 int size = 0; 396 socklen_t size_size = sizeof(size); 397 EXPECT_THAT( 398 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVBUF, &size, &size_size), 399 SyscallSucceeds()); 400 EXPECT_GT(size, 0); 401 } 402 403 TEST_P(AllSocketPairTest, GetSndBufSucceeds) { 404 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 405 int size = 0; 406 socklen_t size_size = sizeof(size); 407 EXPECT_THAT( 408 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDBUF, &size, &size_size), 409 SyscallSucceeds()); 410 EXPECT_GT(size, 0); 411 } 412 413 TEST_P(AllSocketPairTest, RecvTimeoutReadSucceeds) { 414 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 415 416 struct timeval tv { 417 .tv_sec = 0, .tv_usec = 10 418 }; 419 EXPECT_THAT( 420 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 421 SyscallSucceeds()); 422 423 char buf[20] = {}; 424 EXPECT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 425 SyscallFailsWithErrno(EAGAIN)); 426 } 427 428 TEST_P(AllSocketPairTest, RecvTimeoutRecvSucceeds) { 429 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 430 431 struct timeval tv { 432 .tv_sec = 0, .tv_usec = 10 433 }; 434 EXPECT_THAT( 435 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 436 SyscallSucceeds()); 437 438 char buf[20] = {}; 439 EXPECT_THAT(RetryEINTR(recv)(sockets->first_fd(), buf, sizeof(buf), 0), 440 SyscallFailsWithErrno(EAGAIN)); 441 } 442 443 TEST_P(AllSocketPairTest, RecvTimeoutRecvOneSecondSucceeds) { 444 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 445 446 struct timeval tv { 447 .tv_sec = 1, .tv_usec = 0 448 }; 449 EXPECT_THAT( 450 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 451 SyscallSucceeds()); 452 453 char buf[20] = {}; 454 EXPECT_THAT(RetryEINTR(recv)(sockets->first_fd(), buf, sizeof(buf), 0), 455 SyscallFailsWithErrno(EAGAIN)); 456 } 457 458 TEST_P(AllSocketPairTest, RecvTimeoutRecvmsgSucceeds) { 459 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 460 461 struct timeval tv { 462 .tv_sec = 0, .tv_usec = 10 463 }; 464 EXPECT_THAT( 465 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 466 SyscallSucceeds()); 467 468 struct msghdr msg = {}; 469 char buf[20] = {}; 470 struct iovec iov; 471 iov.iov_base = buf; 472 iov.iov_len = sizeof(buf); 473 msg.msg_iov = &iov; 474 msg.msg_iovlen = 1; 475 476 EXPECT_THAT(RetryEINTR(recvmsg)(sockets->first_fd(), &msg, 0), 477 SyscallFailsWithErrno(EAGAIN)); 478 } 479 480 TEST_P(AllSocketPairTest, SendTimeoutDefault) { 481 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 482 483 timeval actual_tv = {.tv_sec = -1, .tv_usec = -1}; 484 socklen_t len = sizeof(actual_tv); 485 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDTIMEO, 486 &actual_tv, &len), 487 SyscallSucceeds()); 488 EXPECT_EQ(actual_tv.tv_sec, 0); 489 EXPECT_EQ(actual_tv.tv_usec, 0); 490 } 491 492 TEST_P(AllSocketPairTest, SetGetSendTimeout) { 493 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 494 495 // tv_usec should be a multiple of 4000 to work on most systems. 496 timeval tv = {.tv_sec = 89, .tv_usec = 44000}; 497 EXPECT_THAT( 498 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)), 499 SyscallSucceeds()); 500 501 timeval actual_tv = {}; 502 socklen_t len = sizeof(actual_tv); 503 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDTIMEO, 504 &actual_tv, &len), 505 SyscallSucceeds()); 506 EXPECT_EQ(actual_tv.tv_sec, tv.tv_sec); 507 EXPECT_EQ(actual_tv.tv_usec, tv.tv_usec); 508 } 509 510 TEST_P(AllSocketPairTest, SetGetSendTimeoutLargerArg) { 511 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 512 513 struct timeval_with_extra { 514 struct timeval tv; 515 int64_t extra_data; 516 } ABSL_ATTRIBUTE_PACKED; 517 518 // tv_usec should be a multiple of 4000 to work on most systems. 519 timeval_with_extra tv_extra = { 520 .tv = {.tv_sec = 0, .tv_usec = 124000}, 521 }; 522 523 EXPECT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDTIMEO, 524 &tv_extra, sizeof(tv_extra)), 525 SyscallSucceeds()); 526 527 timeval_with_extra actual_tv = {}; 528 socklen_t len = sizeof(actual_tv); 529 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDTIMEO, 530 &actual_tv, &len), 531 SyscallSucceeds()); 532 EXPECT_EQ(actual_tv.tv.tv_sec, tv_extra.tv.tv_sec); 533 EXPECT_EQ(actual_tv.tv.tv_usec, tv_extra.tv.tv_usec); 534 } 535 536 TEST_P(AllSocketPairTest, SendTimeoutAllowsWrite) { 537 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 538 539 struct timeval tv { 540 .tv_sec = 0, .tv_usec = 10 541 }; 542 EXPECT_THAT( 543 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)), 544 SyscallSucceeds()); 545 546 char buf[20] = {}; 547 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 548 SyscallSucceedsWithValue(sizeof(buf))); 549 } 550 551 TEST_P(AllSocketPairTest, SendTimeoutAllowsSend) { 552 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 553 554 struct timeval tv { 555 .tv_sec = 0, .tv_usec = 10 556 }; 557 EXPECT_THAT( 558 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)), 559 SyscallSucceeds()); 560 561 char buf[20] = {}; 562 ASSERT_THAT(RetryEINTR(send)(sockets->first_fd(), buf, sizeof(buf), 0), 563 SyscallSucceedsWithValue(sizeof(buf))); 564 } 565 566 TEST_P(AllSocketPairTest, SendTimeoutAllowsSendmsg) { 567 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 568 569 struct timeval tv { 570 .tv_sec = 0, .tv_usec = 10 571 }; 572 EXPECT_THAT( 573 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)), 574 SyscallSucceeds()); 575 576 char buf[20] = {}; 577 ASSERT_NO_FATAL_FAILURE(SendNullCmsg(sockets->first_fd(), buf, sizeof(buf))); 578 } 579 580 TEST_P(AllSocketPairTest, RecvTimeoutDefault) { 581 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 582 583 timeval actual_tv = {.tv_sec = -1, .tv_usec = -1}; 584 socklen_t len = sizeof(actual_tv); 585 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, 586 &actual_tv, &len), 587 SyscallSucceeds()); 588 EXPECT_EQ(actual_tv.tv_sec, 0); 589 EXPECT_EQ(actual_tv.tv_usec, 0); 590 } 591 592 TEST_P(AllSocketPairTest, SetGetRecvTimeout) { 593 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 594 595 timeval tv = {.tv_sec = 123, .tv_usec = 456000}; 596 EXPECT_THAT( 597 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 598 SyscallSucceeds()); 599 600 timeval actual_tv = {}; 601 socklen_t len = sizeof(actual_tv); 602 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, 603 &actual_tv, &len), 604 SyscallSucceeds()); 605 EXPECT_EQ(actual_tv.tv_sec, 123); 606 EXPECT_EQ(actual_tv.tv_usec, 456000); 607 } 608 609 TEST_P(AllSocketPairTest, SetGetRecvTimeoutLargerArg) { 610 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 611 612 struct timeval_with_extra { 613 struct timeval tv; 614 int64_t extra_data; 615 } ABSL_ATTRIBUTE_PACKED; 616 617 timeval_with_extra tv_extra = { 618 .tv = {.tv_sec = 0, .tv_usec = 432000}, 619 }; 620 621 EXPECT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, 622 &tv_extra, sizeof(tv_extra)), 623 SyscallSucceeds()); 624 625 timeval_with_extra actual_tv = {}; 626 socklen_t len = sizeof(actual_tv); 627 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, 628 &actual_tv, &len), 629 SyscallSucceeds()); 630 EXPECT_EQ(actual_tv.tv.tv_sec, 0); 631 EXPECT_EQ(actual_tv.tv.tv_usec, 432000); 632 } 633 634 TEST_P(AllSocketPairTest, RecvTimeoutRecvmsgOneSecondSucceeds) { 635 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 636 637 struct timeval tv { 638 .tv_sec = 1, .tv_usec = 0 639 }; 640 EXPECT_THAT( 641 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 642 SyscallSucceeds()); 643 644 struct msghdr msg = {}; 645 char buf[20] = {}; 646 struct iovec iov; 647 iov.iov_base = buf; 648 iov.iov_len = sizeof(buf); 649 msg.msg_iov = &iov; 650 msg.msg_iovlen = 1; 651 652 EXPECT_THAT(RetryEINTR(recvmsg)(sockets->first_fd(), &msg, 0), 653 SyscallFailsWithErrno(EAGAIN)); 654 } 655 656 TEST_P(AllSocketPairTest, RecvTimeoutUsecTooLarge) { 657 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 658 659 struct timeval tv { 660 .tv_sec = 0, .tv_usec = 2000000 // 2 seconds. 661 }; 662 EXPECT_THAT( 663 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 664 SyscallFailsWithErrno(EDOM)); 665 } 666 667 TEST_P(AllSocketPairTest, SendTimeoutUsecTooLarge) { 668 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 669 670 struct timeval tv { 671 .tv_sec = 0, .tv_usec = 2000000 // 2 seconds. 672 }; 673 EXPECT_THAT( 674 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)), 675 SyscallFailsWithErrno(EDOM)); 676 } 677 678 TEST_P(AllSocketPairTest, RecvTimeoutUsecNeg) { 679 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 680 681 struct timeval tv { 682 .tv_sec = 0, .tv_usec = -1 683 }; 684 EXPECT_THAT( 685 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 686 SyscallFailsWithErrno(EDOM)); 687 } 688 689 TEST_P(AllSocketPairTest, SendTimeoutUsecNeg) { 690 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 691 692 struct timeval tv { 693 .tv_sec = 0, .tv_usec = -1 694 }; 695 EXPECT_THAT( 696 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)), 697 SyscallFailsWithErrno(EDOM)); 698 } 699 700 TEST_P(AllSocketPairTest, RecvTimeoutNegSecRead) { 701 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 702 703 struct timeval tv { 704 .tv_sec = -1, .tv_usec = 0 705 }; 706 EXPECT_THAT( 707 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 708 SyscallSucceeds()); 709 710 char buf[20] = {}; 711 EXPECT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 712 SyscallFailsWithErrno(EAGAIN)); 713 } 714 715 TEST_P(AllSocketPairTest, RecvTimeoutNegSecRecv) { 716 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 717 718 struct timeval tv { 719 .tv_sec = -1, .tv_usec = 0 720 }; 721 EXPECT_THAT( 722 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 723 SyscallSucceeds()); 724 725 char buf[20] = {}; 726 EXPECT_THAT(RetryEINTR(recv)(sockets->first_fd(), buf, sizeof(buf), 0), 727 SyscallFailsWithErrno(EAGAIN)); 728 } 729 730 TEST_P(AllSocketPairTest, RecvTimeoutNegSecRecvmsg) { 731 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 732 733 struct timeval tv { 734 .tv_sec = -1, .tv_usec = 0 735 }; 736 EXPECT_THAT( 737 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)), 738 SyscallSucceeds()); 739 740 struct msghdr msg = {}; 741 char buf[20] = {}; 742 struct iovec iov; 743 iov.iov_base = buf; 744 iov.iov_len = sizeof(buf); 745 msg.msg_iov = &iov; 746 msg.msg_iovlen = 1; 747 748 EXPECT_THAT(RetryEINTR(recvmsg)(sockets->first_fd(), &msg, 0), 749 SyscallFailsWithErrno(EAGAIN)); 750 } 751 752 TEST_P(AllSocketPairTest, RecvWaitAll) { 753 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 754 755 char sent_data[100]; 756 RandomizeBuffer(sent_data, sizeof(sent_data)); 757 758 ASSERT_THAT(write(sockets->first_fd(), sent_data, sizeof(sent_data)), 759 SyscallSucceedsWithValue(sizeof(sent_data))); 760 761 char received_data[sizeof(sent_data)] = {}; 762 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data, 763 sizeof(received_data), MSG_WAITALL), 764 SyscallSucceedsWithValue(sizeof(sent_data))); 765 766 EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(sent_data))); 767 } 768 769 TEST_P(AllSocketPairTest, RecvWaitAllDontWait) { 770 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 771 772 char data[100] = {}; 773 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), data, sizeof(data), 774 MSG_WAITALL | MSG_DONTWAIT), 775 SyscallFailsWithErrno(EAGAIN)); 776 } 777 778 TEST_P(AllSocketPairTest, RecvTimeoutWaitAll) { 779 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 780 781 struct timeval tv { 782 .tv_sec = 0, .tv_usec = 200000 // 200ms 783 }; 784 EXPECT_THAT(setsockopt(sockets->second_fd(), SOL_SOCKET, SO_RCVTIMEO, &tv, 785 sizeof(tv)), 786 SyscallSucceeds()); 787 788 char sent_data[100]; 789 RandomizeBuffer(sent_data, sizeof(sent_data)); 790 791 ASSERT_THAT(write(sockets->first_fd(), sent_data, sizeof(sent_data)), 792 SyscallSucceedsWithValue(sizeof(sent_data))); 793 794 char received_data[sizeof(sent_data) * 2] = {}; 795 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data, 796 sizeof(received_data), MSG_WAITALL), 797 SyscallSucceedsWithValue(sizeof(sent_data))); 798 799 EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(sent_data))); 800 } 801 802 TEST_P(AllSocketPairTest, GetSockoptType) { 803 int type = GetParam().type; 804 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 805 for (const int fd : {sockets->first_fd(), sockets->second_fd()}) { 806 int opt; 807 socklen_t optlen = sizeof(opt); 808 EXPECT_THAT(getsockopt(fd, SOL_SOCKET, SO_TYPE, &opt, &optlen), 809 SyscallSucceeds()); 810 811 // Type may have SOCK_NONBLOCK and SOCK_CLOEXEC ORed into it. Remove these 812 // before comparison. 813 type &= ~(SOCK_NONBLOCK | SOCK_CLOEXEC); 814 EXPECT_EQ(opt, type) << absl::StrFormat( 815 "getsockopt(%d, SOL_SOCKET, SO_TYPE, &opt, &optlen) => opt=%d was " 816 "unexpected", 817 fd, opt); 818 } 819 } 820 821 TEST_P(AllSocketPairTest, GetSockoptDomain) { 822 const int domain = GetParam().domain; 823 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 824 for (const int fd : {sockets->first_fd(), sockets->second_fd()}) { 825 int opt; 826 socklen_t optlen = sizeof(opt); 827 EXPECT_THAT(getsockopt(fd, SOL_SOCKET, SO_DOMAIN, &opt, &optlen), 828 SyscallSucceeds()); 829 EXPECT_EQ(opt, domain) << absl::StrFormat( 830 "getsockopt(%d, SOL_SOCKET, SO_DOMAIN, &opt, &optlen) => opt=%d was " 831 "unexpected", 832 fd, opt); 833 } 834 } 835 836 TEST_P(AllSocketPairTest, GetSockoptProtocol) { 837 const int protocol = GetParam().protocol; 838 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 839 for (const int fd : {sockets->first_fd(), sockets->second_fd()}) { 840 int opt; 841 socklen_t optlen = sizeof(opt); 842 EXPECT_THAT(getsockopt(fd, SOL_SOCKET, SO_PROTOCOL, &opt, &optlen), 843 SyscallSucceeds()); 844 EXPECT_EQ(opt, protocol) << absl::StrFormat( 845 "getsockopt(%d, SOL_SOCKET, SO_PROTOCOL, &opt, &optlen) => opt=%d was " 846 "unexpected", 847 fd, opt); 848 } 849 } 850 851 TEST_P(AllSocketPairTest, SetAndGetBooleanSocketOptions) { 852 int sock_opts[] = {SO_BROADCAST, SO_PASSCRED, SO_NO_CHECK, 853 SO_REUSEADDR, SO_REUSEPORT, SO_KEEPALIVE}; 854 for (int sock_opt : sock_opts) { 855 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 856 int enable = -1; 857 socklen_t enableLen = sizeof(enable); 858 859 // Test that the option is initially set to false. 860 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, sock_opt, &enable, 861 &enableLen), 862 SyscallSucceeds()); 863 ASSERT_EQ(enableLen, sizeof(enable)); 864 EXPECT_EQ(enable, 0) << absl::StrFormat( 865 "getsockopt(fd, SOL_SOCKET, %d, &enable, &enableLen) => enable=%d", 866 sock_opt, enable); 867 868 // Test that setting the option to true is reflected in the subsequent 869 // call to getsockopt(2). 870 enable = 1; 871 ASSERT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, sock_opt, &enable, 872 sizeof(enable)), 873 SyscallSucceeds()); 874 enable = -1; 875 enableLen = sizeof(enable); 876 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, sock_opt, &enable, 877 &enableLen), 878 SyscallSucceeds()); 879 ASSERT_EQ(enableLen, sizeof(enable)); 880 EXPECT_EQ(enable, 1) << absl::StrFormat( 881 "getsockopt(fd, SOL_SOCKET, %d, &enable, &enableLen) => enable=%d", 882 sock_opt, enable); 883 } 884 } 885 886 TEST_P(AllSocketPairTest, GetSocketOutOfBandInlineOption) { 887 // We do not support disabling this option. It is always enabled. 888 SKIP_IF(!IsRunningOnGvisor()); 889 890 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 891 int enable = -1; 892 socklen_t enableLen = sizeof(enable); 893 894 int want = 1; 895 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_OOBINLINE, &enable, 896 &enableLen), 897 SyscallSucceeds()); 898 ASSERT_EQ(enableLen, sizeof(enable)); 899 EXPECT_EQ(enable, want); 900 } 901 902 } // namespace testing 903 } // namespace gvisor