gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/test/syscalls/linux/socket_ip_tcp_generic.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_ip_tcp_generic.h" 16 17 #include <fcntl.h> 18 #include <netinet/in.h> 19 #include <netinet/tcp.h> 20 #include <poll.h> 21 #include <stdio.h> 22 #include <sys/ioctl.h> 23 #include <sys/socket.h> 24 #include <sys/types.h> 25 #include <sys/un.h> 26 27 #include "gtest/gtest.h" 28 #include "absl/memory/memory.h" 29 #include "absl/time/clock.h" 30 #include "absl/time/time.h" 31 #include "test/util/save_util.h" 32 #include "test/util/socket_util.h" 33 #include "test/util/temp_path.h" 34 #include "test/util/test_util.h" 35 #include "test/util/thread_util.h" 36 37 namespace gvisor { 38 namespace testing { 39 40 using ::testing::AnyOf; 41 using ::testing::Eq; 42 43 TEST_P(TCPSocketPairTest, TcpInfoSucceeds) { 44 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 45 46 struct tcp_info opt = {}; 47 socklen_t optLen = sizeof(opt); 48 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_TCP, TCP_INFO, &opt, &optLen), 49 SyscallSucceeds()); 50 } 51 52 TEST_P(TCPSocketPairTest, ShortTcpInfoSucceeds) { 53 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 54 55 struct tcp_info opt = {}; 56 socklen_t optLen = 1; 57 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_TCP, TCP_INFO, &opt, &optLen), 58 SyscallSucceeds()); 59 } 60 61 TEST_P(TCPSocketPairTest, ZeroTcpInfoSucceeds) { 62 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 63 64 struct tcp_info opt = {}; 65 socklen_t optLen = 0; 66 EXPECT_THAT(getsockopt(sockets->first_fd(), SOL_TCP, TCP_INFO, &opt, &optLen), 67 SyscallSucceeds()); 68 } 69 70 // Copied from include/net/tcp.h. 71 constexpr int TCP_CA_OPEN = 0; 72 73 TEST_P(TCPSocketPairTest, CheckTcpInfoFields) { 74 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 75 76 char buf[10] = {}; 77 ASSERT_THAT(RetryEINTR(send)(sockets->first_fd(), buf, sizeof(buf), 0), 78 SyscallSucceedsWithValue(sizeof(buf))); 79 80 // Wait until second_fd sees the data and then recv it. 81 struct pollfd poll_fd = {sockets->second_fd(), POLLIN, 0}; 82 constexpr int kPollTimeoutMs = 2000; // Wait up to 2 seconds for the data. 83 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 84 SyscallSucceedsWithValue(1)); 85 86 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), buf, sizeof(buf), 0), 87 SyscallSucceedsWithValue(sizeof(buf))); 88 89 struct tcp_info opt = {}; 90 socklen_t optLen = sizeof(opt); 91 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_TCP, TCP_INFO, &opt, &optLen), 92 SyscallSucceeds()); 93 ASSERT_EQ(optLen, sizeof(opt)); 94 95 // Validates the received tcp_info fields. 96 EXPECT_EQ(opt.tcpi_ca_state, TCP_CA_OPEN); 97 EXPECT_GT(opt.tcpi_snd_cwnd, 0); 98 EXPECT_GT(opt.tcpi_rto, 0); 99 } 100 101 // This test validates that an RST is sent instead of a FIN when data is 102 // unread on calls to close(2). 103 TEST_P(TCPSocketPairTest, RSTSentOnCloseWithUnreadData) { 104 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 105 106 char buf[10] = {}; 107 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 108 SyscallSucceedsWithValue(sizeof(buf))); 109 110 // Wait until t_ sees the data on its side but don't read it. 111 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 112 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 113 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 114 SyscallSucceedsWithValue(1)); 115 116 // Now close the connected without reading the data. 117 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 118 119 // Wait for the other end to receive the RST (up to 20 seconds). 120 struct pollfd poll_fd2 = {sockets->first_fd(), POLLIN | POLLHUP, 0}; 121 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollTimeoutMs), 122 SyscallSucceedsWithValue(1)); 123 124 // A shutdown with unread data will cause a RST to be sent instead 125 // of a FIN, per RFC 2525 section 2.17; this is also what Linux does. 126 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 127 SyscallFailsWithErrno(ECONNRESET)); 128 } 129 130 // This test will validate that a RST will cause POLLHUP to trigger. 131 TEST_P(TCPSocketPairTest, RSTCausesPollHUP) { 132 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 133 134 char buf[10] = {}; 135 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 136 SyscallSucceedsWithValue(sizeof(buf))); 137 138 // Wait until second sees the data on its side but don't read it. 139 struct pollfd poll_fd = {sockets->second_fd(), POLLIN, 0}; 140 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 141 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 142 SyscallSucceedsWithValue(1)); 143 EXPECT_EQ(poll_fd.revents & POLLIN, POLLIN); 144 145 // Confirm we at least have one unread byte. 146 int bytes_available = 0; 147 ASSERT_THAT(RetryEINTR([&]() { 148 return ioctl(sockets->second_fd(), FIONREAD, &bytes_available); 149 })(), 150 SyscallSucceeds()); 151 EXPECT_GT(bytes_available, 0); 152 153 // Now close the connected socket without reading the data from the second, 154 // this will cause a RST and we should see that with POLLHUP. 155 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 156 157 // Wait for the other end to receive the RST (up to 20 seconds). 158 struct pollfd poll_fd3 = {sockets->first_fd(), POLLHUP, 0}; 159 ASSERT_THAT(RetryEINTR(poll)(&poll_fd3, 1, kPollTimeoutMs), 160 SyscallSucceedsWithValue(1)); 161 ASSERT_NE(poll_fd3.revents & POLLHUP, 0); 162 } 163 164 // This test validates that even if a RST is sent the other end will not 165 // get an ECONNRESET until it's read all data. 166 TEST_P(TCPSocketPairTest, RSTSentOnCloseWithUnreadDataAllowsReadBuffered) { 167 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 168 169 char buf[10] = {}; 170 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 171 SyscallSucceedsWithValue(sizeof(buf))); 172 ASSERT_THAT(RetryEINTR(write)(sockets->second_fd(), buf, sizeof(buf)), 173 SyscallSucceedsWithValue(sizeof(buf))); 174 175 // Wait until second sees the data on its side but don't read it. 176 struct pollfd poll_fd = {sockets->second_fd(), POLLIN, 0}; 177 constexpr int kPollTimeoutMs = 30000; // Wait up to 30 seconds for the data. 178 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 179 SyscallSucceedsWithValue(1)); 180 181 // Wait until first sees the data on its side but don't read it. 182 struct pollfd poll_fd2 = {sockets->first_fd(), POLLIN, 0}; 183 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollTimeoutMs), 184 SyscallSucceedsWithValue(1)); 185 186 // Now close the connected socket without reading the data from the second. 187 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 188 189 // Wait for the other end to receive the RST (up to 30 seconds). 190 struct pollfd poll_fd3 = {sockets->first_fd(), POLLHUP, 0}; 191 ASSERT_THAT(RetryEINTR(poll)(&poll_fd3, 1, kPollTimeoutMs), 192 SyscallSucceedsWithValue(1)); 193 194 // Since we also have data buffered we should be able to read it before 195 // the syscall will fail with ECONNRESET. 196 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 197 SyscallSucceedsWithValue(sizeof(buf))); 198 199 // A shutdown with unread data will cause a RST to be sent instead 200 // of a FIN, per RFC 2525 section 2.17; this is also what Linux does. 201 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 202 SyscallFailsWithErrno(ECONNRESET)); 203 } 204 205 // This test will verify that a clean shutdown (FIN) is preformed when there 206 // is unread data but only the write side is closed. 207 TEST_P(TCPSocketPairTest, FINSentOnShutdownWrWithUnreadData) { 208 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 209 210 char buf[10] = {}; 211 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 212 SyscallSucceedsWithValue(sizeof(buf))); 213 214 // Wait until t_ sees the data on its side but don't read it. 215 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 216 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 217 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 218 SyscallSucceedsWithValue(1)); 219 220 // Now shutdown the write end leaving the read end open. 221 ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_WR), SyscallSucceeds()); 222 223 // Wait for the other end to receive the FIN (up to 20 seconds). 224 struct pollfd poll_fd2 = {sockets->first_fd(), POLLIN | POLLHUP, 0}; 225 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollTimeoutMs), 226 SyscallSucceedsWithValue(1)); 227 228 // Since we didn't shutdown the read end this will be a clean close. 229 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 230 SyscallSucceedsWithValue(0)); 231 } 232 233 // This test will verify that when data is received by a socket, even if it's 234 // not read SHUT_RD will not cause any packets to be generated. 235 TEST_P(TCPSocketPairTest, ShutdownRdShouldCauseNoPacketsWithUnreadData) { 236 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 237 238 char buf[10] = {}; 239 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 240 SyscallSucceedsWithValue(sizeof(buf))); 241 242 // Wait until t_ sees the data on its side but don't read it. 243 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 244 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 245 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 246 SyscallSucceedsWithValue(1)); 247 248 // Now shutdown the read end, this will generate no packets to the other end. 249 ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_RD), SyscallSucceeds()); 250 251 // We should not receive any events on the other side of the socket. 252 struct pollfd poll_fd2 = {sockets->first_fd(), POLLIN | POLLHUP, 0}; 253 constexpr int kPollNoResponseTimeoutMs = 3000; 254 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollNoResponseTimeoutMs), 255 SyscallSucceedsWithValue(0)); // Timeout. 256 } 257 258 // This test will verify that a socket which has unread data will still allow 259 // the data to be read after shutting down the read side, and once there is no 260 // unread data left, then read will return an EOF. 261 TEST_P(TCPSocketPairTest, ShutdownRdAllowsReadOfReceivedDataBeforeEOF) { 262 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 263 264 char buf[10] = {}; 265 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 266 SyscallSucceedsWithValue(sizeof(buf))); 267 268 // Wait until t_ sees the data on its side but don't read it. 269 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 270 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 271 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 272 SyscallSucceedsWithValue(1)); 273 274 // Now shutdown the read end. 275 ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_RD), SyscallSucceeds()); 276 277 // Even though we did a SHUT_RD on the read end we can still read the data. 278 ASSERT_THAT(RetryEINTR(read)(sockets->second_fd(), buf, sizeof(buf)), 279 SyscallSucceedsWithValue(sizeof(buf))); 280 281 // After reading all of the data, reading the closed read end returns EOF. 282 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 283 SyscallSucceedsWithValue(1)); 284 ASSERT_THAT(RetryEINTR(read)(sockets->second_fd(), buf, sizeof(buf)), 285 SyscallSucceedsWithValue(0)); 286 } 287 288 // This test verifies that a shutdown(wr) by the server after sending 289 // data allows the client to still read() the queued data and a client 290 // close after sending response allows server to read the incoming 291 // response. 292 TEST_P(TCPSocketPairTest, ShutdownWrServerClientClose) { 293 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 294 ScopedThread t([&]() { 295 char sbuf[10] = {}; 296 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), sbuf, sizeof(sbuf)), 297 SyscallSucceedsWithValue(sizeof(sbuf))); 298 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), sbuf, sizeof(sbuf)), 299 SyscallSucceedsWithValue(sizeof(sbuf))); 300 ASSERT_THAT(close(sockets->release_first_fd()), 301 SyscallSucceedsWithValue(0)); 302 }); 303 char buf[10] = {}; 304 ASSERT_THAT(RetryEINTR(write)(sockets->second_fd(), buf, sizeof(buf)), 305 SyscallSucceedsWithValue(sizeof(buf))); 306 ASSERT_THAT(RetryEINTR(shutdown)(sockets->second_fd(), SHUT_WR), 307 SyscallSucceedsWithValue(0)); 308 t.Join(); 309 310 ASSERT_THAT(RetryEINTR(read)(sockets->second_fd(), buf, sizeof(buf)), 311 SyscallSucceedsWithValue(sizeof(buf))); 312 } 313 314 TEST_P(TCPSocketPairTest, ClosedReadNonBlockingSocket) { 315 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 316 317 // Set the read end to O_NONBLOCK. 318 int opts = 0; 319 ASSERT_THAT(opts = fcntl(sockets->second_fd(), F_GETFL), SyscallSucceeds()); 320 ASSERT_THAT(fcntl(sockets->second_fd(), F_SETFL, opts | O_NONBLOCK), 321 SyscallSucceeds()); 322 323 char buf[10] = {}; 324 ASSERT_THAT(RetryEINTR(send)(sockets->first_fd(), buf, sizeof(buf), 0), 325 SyscallSucceedsWithValue(sizeof(buf))); 326 327 // Wait until second_fd sees the data and then recv it. 328 struct pollfd poll_fd = {sockets->second_fd(), POLLIN, 0}; 329 constexpr int kPollTimeoutMs = 2000; // Wait up to 2 seconds for the data. 330 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 331 SyscallSucceedsWithValue(1)); 332 333 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), buf, sizeof(buf), 0), 334 SyscallSucceedsWithValue(sizeof(buf))); 335 336 // Now shutdown the write end leaving the read end open. 337 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 338 339 // Wait for close notification and recv again. 340 struct pollfd poll_fd2 = {sockets->second_fd(), POLLIN, 0}; 341 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollTimeoutMs), 342 SyscallSucceedsWithValue(1)); 343 344 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), buf, sizeof(buf), 0), 345 SyscallSucceedsWithValue(0)); 346 } 347 348 TEST_P(TCPSocketPairTest, 349 ShutdownRdUnreadDataShouldCauseNoPacketsUnlessClosed) { 350 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 351 352 char buf[10] = {}; 353 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 354 SyscallSucceedsWithValue(sizeof(buf))); 355 356 // Wait until t_ sees the data on its side but don't read it. 357 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 358 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 359 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 360 SyscallSucceedsWithValue(1)); 361 362 // Now shutdown the read end, this will generate no packets to the other end. 363 ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_RD), SyscallSucceeds()); 364 365 // We should not receive any events on the other side of the socket. 366 struct pollfd poll_fd2 = {sockets->first_fd(), POLLIN | POLLHUP, 0}; 367 constexpr int kPollNoResponseTimeoutMs = 3000; 368 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollNoResponseTimeoutMs), 369 SyscallSucceedsWithValue(0)); // Timeout. 370 371 // Now since we've fully closed the connection it will generate a RST. 372 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 373 ASSERT_THAT(RetryEINTR(poll)(&poll_fd2, 1, kPollTimeoutMs), 374 SyscallSucceedsWithValue(1)); // The other end has closed. 375 376 // A shutdown with unread data will cause a RST to be sent instead 377 // of a FIN, per RFC 2525 section 2.17; this is also what Linux does. 378 ASSERT_THAT(RetryEINTR(read)(sockets->first_fd(), buf, sizeof(buf)), 379 SyscallFailsWithErrno(ECONNRESET)); 380 } 381 382 TEST_P(TCPSocketPairTest, TCPCorkDefault) { 383 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 384 385 int get = -1; 386 socklen_t get_len = sizeof(get); 387 EXPECT_THAT( 388 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, &get, &get_len), 389 SyscallSucceedsWithValue(0)); 390 EXPECT_EQ(get_len, sizeof(get)); 391 EXPECT_EQ(get, kSockOptOff); 392 } 393 394 TEST_P(TCPSocketPairTest, SetTCPCork) { 395 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 396 397 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, 398 &kSockOptOn, sizeof(kSockOptOn)), 399 SyscallSucceeds()); 400 401 int get = -1; 402 socklen_t get_len = sizeof(get); 403 EXPECT_THAT( 404 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, &get, &get_len), 405 SyscallSucceedsWithValue(0)); 406 EXPECT_EQ(get_len, sizeof(get)); 407 EXPECT_EQ(get, kSockOptOn); 408 409 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, 410 &kSockOptOff, sizeof(kSockOptOff)), 411 SyscallSucceeds()); 412 413 EXPECT_THAT( 414 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, &get, &get_len), 415 SyscallSucceedsWithValue(0)); 416 EXPECT_EQ(get_len, sizeof(get)); 417 EXPECT_EQ(get, kSockOptOff); 418 } 419 420 TEST_P(TCPSocketPairTest, TCPCork) { 421 // Disable save on this test, this test checks if the data is not recv'd by 422 // the receiver after enabling TCP_CORK when the size of the packet < MSS. 423 // But the save/resume may take more than the cork timeout of 200ms causing 424 // all the corked packets to be sent and makes the test flaky. 425 const DisableSave ds; 426 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 427 428 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, 429 &kSockOptOn, sizeof(kSockOptOn)), 430 SyscallSucceeds()); 431 432 constexpr char kData[] = "abc"; 433 ASSERT_THAT(WriteFd(sockets->first_fd(), kData, sizeof(kData)), 434 SyscallSucceedsWithValue(sizeof(kData))); 435 436 ASSERT_NO_FATAL_FAILURE(RecvNoData(sockets->second_fd())); 437 438 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, 439 &kSockOptOff, sizeof(kSockOptOff)), 440 SyscallSucceeds()); 441 442 // Create a receive buffer larger than kData. 443 char buf[(sizeof(kData) + 1) * 2] = {}; 444 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), buf, sizeof(buf), 0), 445 SyscallSucceedsWithValue(sizeof(kData))); 446 EXPECT_EQ(absl::string_view(kData, sizeof(kData)), 447 absl::string_view(buf, sizeof(kData))); 448 } 449 450 TEST_P(TCPSocketPairTest, TCPQuickAckDefault) { 451 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 452 453 int get = -1; 454 socklen_t get_len = sizeof(get); 455 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_QUICKACK, &get, 456 &get_len), 457 SyscallSucceedsWithValue(0)); 458 EXPECT_EQ(get_len, sizeof(get)); 459 EXPECT_EQ(get, kSockOptOn); 460 } 461 462 TEST_P(TCPSocketPairTest, SetTCPQuickAck) { 463 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 464 465 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_QUICKACK, 466 &kSockOptOff, sizeof(kSockOptOff)), 467 SyscallSucceeds()); 468 469 int get = -1; 470 socklen_t get_len = sizeof(get); 471 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_QUICKACK, &get, 472 &get_len), 473 SyscallSucceedsWithValue(0)); 474 EXPECT_EQ(get_len, sizeof(get)); 475 EXPECT_EQ(get, kSockOptOff); 476 477 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_QUICKACK, 478 &kSockOptOn, sizeof(kSockOptOn)), 479 SyscallSucceeds()); 480 481 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_QUICKACK, &get, 482 &get_len), 483 SyscallSucceedsWithValue(0)); 484 EXPECT_EQ(get_len, sizeof(get)); 485 EXPECT_EQ(get, kSockOptOn); 486 } 487 488 TEST_P(TCPSocketPairTest, SoKeepaliveDefault) { 489 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 490 491 int get = -1; 492 socklen_t get_len = sizeof(get); 493 EXPECT_THAT( 494 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, &get, &get_len), 495 SyscallSucceedsWithValue(0)); 496 EXPECT_EQ(get_len, sizeof(get)); 497 EXPECT_EQ(get, kSockOptOff); 498 } 499 500 TEST_P(TCPSocketPairTest, SetSoKeepalive) { 501 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 502 503 ASSERT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, 504 &kSockOptOn, sizeof(kSockOptOn)), 505 SyscallSucceeds()); 506 507 int get = -1; 508 socklen_t get_len = sizeof(get); 509 EXPECT_THAT( 510 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, &get, &get_len), 511 SyscallSucceedsWithValue(0)); 512 EXPECT_EQ(get_len, sizeof(get)); 513 EXPECT_EQ(get, kSockOptOn); 514 515 ASSERT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, 516 &kSockOptOff, sizeof(kSockOptOff)), 517 SyscallSucceeds()); 518 519 EXPECT_THAT( 520 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, &get, &get_len), 521 SyscallSucceedsWithValue(0)); 522 EXPECT_EQ(get_len, sizeof(get)); 523 EXPECT_EQ(get, kSockOptOff); 524 } 525 526 TEST_P(TCPSocketPairTest, SetSoKeepaliveClosed) { 527 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 528 529 // Force a RST to be sent using SO_LINGER. 530 auto linger_opt = linger{.l_onoff = 1, .l_linger = 0}; 531 ASSERT_THAT(setsockopt(sockets->second_fd(), SOL_SOCKET, SO_LINGER, 532 &linger_opt, sizeof(linger_opt)), 533 SyscallSucceeds()); 534 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 535 536 // Wait for the other end to receive the RST (up to 20 seconds). 537 constexpr int kPollTimeoutMs = 20000; 538 auto pfd = pollfd{ 539 .fd = sockets->first_fd(), 540 .events = POLLIN | POLLHUP, 541 .revents = 0, 542 }; 543 ASSERT_THAT(RetryEINTR(poll)(&pfd, 1, kPollTimeoutMs), 544 SyscallSucceedsWithValue(1)); 545 ASSERT_EQ(pfd.revents & POLLHUP, POLLHUP); 546 547 // Now that the connection is closed, we should still be able to set 548 // SO_KEEPALIVE. 549 ASSERT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, 550 &kSockOptOn, sizeof(kSockOptOn)), 551 SyscallSucceeds()); 552 int get = -1; 553 socklen_t get_len = sizeof(get); 554 ASSERT_THAT( 555 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_KEEPALIVE, &get, &get_len), 556 SyscallSucceeds()); 557 ASSERT_EQ(get, kSockOptOn); 558 ASSERT_EQ(get_len, sizeof(get)); 559 } 560 561 TEST_P(TCPSocketPairTest, TCPKeepidleDefault) { 562 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 563 564 int get = -1; 565 socklen_t get_len = sizeof(get); 566 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPIDLE, &get, 567 &get_len), 568 SyscallSucceedsWithValue(0)); 569 EXPECT_EQ(get_len, sizeof(get)); 570 EXPECT_EQ(get, 2 * 60 * 60); // 2 hours. 571 } 572 573 TEST_P(TCPSocketPairTest, TCPKeepintvlDefault) { 574 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 575 576 int get = -1; 577 socklen_t get_len = sizeof(get); 578 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPINTVL, &get, 579 &get_len), 580 SyscallSucceedsWithValue(0)); 581 EXPECT_EQ(get_len, sizeof(get)); 582 EXPECT_EQ(get, 75); // 75 seconds. 583 } 584 585 TEST_P(TCPSocketPairTest, SetTCPKeepidleZero) { 586 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 587 588 constexpr int kZero = 0; 589 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPIDLE, &kZero, 590 sizeof(kZero)), 591 SyscallFailsWithErrno(EINVAL)); 592 } 593 594 TEST_P(TCPSocketPairTest, SetTCPKeepintvlZero) { 595 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 596 597 constexpr int kZero = 0; 598 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPINTVL, 599 &kZero, sizeof(kZero)), 600 SyscallFailsWithErrno(EINVAL)); 601 } 602 603 // Copied from include/net/tcp.h. 604 constexpr int MAX_TCP_KEEPIDLE = 32767; 605 constexpr int MAX_TCP_KEEPINTVL = 32767; 606 constexpr int MAX_TCP_KEEPCNT = 127; 607 608 TEST_P(TCPSocketPairTest, SetTCPKeepidleAboveMax) { 609 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 610 611 constexpr int kAboveMax = MAX_TCP_KEEPIDLE + 1; 612 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPIDLE, 613 &kAboveMax, sizeof(kAboveMax)), 614 SyscallFailsWithErrno(EINVAL)); 615 } 616 617 TEST_P(TCPSocketPairTest, SetTCPKeepintvlAboveMax) { 618 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 619 620 constexpr int kAboveMax = MAX_TCP_KEEPINTVL + 1; 621 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPINTVL, 622 &kAboveMax, sizeof(kAboveMax)), 623 SyscallFailsWithErrno(EINVAL)); 624 } 625 626 TEST_P(TCPSocketPairTest, SetTCPKeepidleToMax) { 627 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 628 629 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPIDLE, 630 &MAX_TCP_KEEPIDLE, sizeof(MAX_TCP_KEEPIDLE)), 631 SyscallSucceedsWithValue(0)); 632 633 int get = -1; 634 socklen_t get_len = sizeof(get); 635 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPIDLE, &get, 636 &get_len), 637 SyscallSucceedsWithValue(0)); 638 EXPECT_EQ(get_len, sizeof(get)); 639 EXPECT_EQ(get, MAX_TCP_KEEPIDLE); 640 } 641 642 TEST_P(TCPSocketPairTest, SetTCPKeepintvlToMax) { 643 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 644 645 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPINTVL, 646 &MAX_TCP_KEEPINTVL, sizeof(MAX_TCP_KEEPINTVL)), 647 SyscallSucceedsWithValue(0)); 648 649 int get = -1; 650 socklen_t get_len = sizeof(get); 651 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPINTVL, &get, 652 &get_len), 653 SyscallSucceedsWithValue(0)); 654 EXPECT_EQ(get_len, sizeof(get)); 655 EXPECT_EQ(get, MAX_TCP_KEEPINTVL); 656 } 657 658 TEST_P(TCPSocketPairTest, TCPKeepcountDefault) { 659 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 660 661 int get = -1; 662 socklen_t get_len = sizeof(get); 663 EXPECT_THAT( 664 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, &get, &get_len), 665 SyscallSucceedsWithValue(0)); 666 EXPECT_EQ(get_len, sizeof(get)); 667 EXPECT_EQ(get, 9); // 9 keepalive probes. 668 } 669 670 TEST_P(TCPSocketPairTest, SetTCPKeepcountZero) { 671 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 672 673 constexpr int kZero = 0; 674 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, &kZero, 675 sizeof(kZero)), 676 SyscallFailsWithErrno(EINVAL)); 677 } 678 679 TEST_P(TCPSocketPairTest, SetTCPKeepcountAboveMax) { 680 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 681 682 constexpr int kAboveMax = MAX_TCP_KEEPCNT + 1; 683 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, 684 &kAboveMax, sizeof(kAboveMax)), 685 SyscallFailsWithErrno(EINVAL)); 686 } 687 688 TEST_P(TCPSocketPairTest, SetTCPKeepcountToMax) { 689 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 690 691 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, 692 &MAX_TCP_KEEPCNT, sizeof(MAX_TCP_KEEPCNT)), 693 SyscallSucceedsWithValue(0)); 694 695 int get = -1; 696 socklen_t get_len = sizeof(get); 697 EXPECT_THAT( 698 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, &get, &get_len), 699 SyscallSucceedsWithValue(0)); 700 EXPECT_EQ(get_len, sizeof(get)); 701 EXPECT_EQ(get, MAX_TCP_KEEPCNT); 702 } 703 704 TEST_P(TCPSocketPairTest, SetTCPKeepcountToOne) { 705 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 706 707 int keepaliveCount = 1; 708 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, 709 &keepaliveCount, sizeof(keepaliveCount)), 710 SyscallSucceedsWithValue(0)); 711 712 int get = -1; 713 socklen_t get_len = sizeof(get); 714 EXPECT_THAT( 715 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, &get, &get_len), 716 SyscallSucceedsWithValue(0)); 717 EXPECT_EQ(get_len, sizeof(get)); 718 EXPECT_EQ(get, keepaliveCount); 719 } 720 721 TEST_P(TCPSocketPairTest, SetTCPKeepcountToNegative) { 722 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 723 724 int keepaliveCount = -5; 725 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_KEEPCNT, 726 &keepaliveCount, sizeof(keepaliveCount)), 727 SyscallFailsWithErrno(EINVAL)); 728 } 729 730 TEST_P(TCPSocketPairTest, SetOOBInline) { 731 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 732 733 EXPECT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_OOBINLINE, 734 &kSockOptOn, sizeof(kSockOptOn)), 735 SyscallSucceeds()); 736 737 int get = -1; 738 socklen_t get_len = sizeof(get); 739 EXPECT_THAT( 740 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_OOBINLINE, &get, &get_len), 741 SyscallSucceedsWithValue(0)); 742 EXPECT_EQ(get_len, sizeof(get)); 743 EXPECT_EQ(get, kSockOptOn); 744 } 745 746 TEST_P(TCPSocketPairTest, MsgTruncMsgPeek) { 747 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 748 749 char sent_data[512]; 750 RandomizeBuffer(sent_data, sizeof(sent_data)); 751 ASSERT_THAT( 752 RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0), 753 SyscallSucceedsWithValue(sizeof(sent_data))); 754 755 // Read half of the data with MSG_TRUNC | MSG_PEEK. This way there will still 756 // be some data left to read in the next step even if the data gets consumed. 757 char received_data1[sizeof(sent_data) / 2] = {}; 758 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data1, 759 sizeof(received_data1), MSG_TRUNC | MSG_PEEK), 760 SyscallSucceedsWithValue(sizeof(received_data1))); 761 762 // Check that we didn't get anything. 763 char zeros[sizeof(received_data1)] = {}; 764 EXPECT_EQ(0, memcmp(zeros, received_data1, sizeof(received_data1))); 765 766 // Check that all of the data is still there. 767 char received_data2[sizeof(sent_data)] = {}; 768 ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data2, 769 sizeof(received_data2), 0), 770 SyscallSucceedsWithValue(sizeof(sent_data))); 771 772 EXPECT_EQ(0, memcmp(received_data2, sent_data, sizeof(sent_data))); 773 } 774 775 TEST_P(TCPSocketPairTest, SetCongestionControlSucceedsForSupported) { 776 // This is Linux's net/tcp.h TCP_CA_NAME_MAX. 777 const int kTcpCaNameMax = 16; 778 779 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 780 // Netstack only supports reno & cubic so we only test these two values here. 781 { 782 const char kSetCC[kTcpCaNameMax] = "reno"; 783 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 784 &kSetCC, strlen(kSetCC)), 785 SyscallSucceedsWithValue(0)); 786 787 char got_cc[kTcpCaNameMax]; 788 memset(got_cc, '1', sizeof(got_cc)); 789 socklen_t optlen = sizeof(got_cc); 790 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 791 &got_cc, &optlen), 792 SyscallSucceedsWithValue(0)); 793 EXPECT_EQ(0, memcmp(got_cc, kSetCC, sizeof(kSetCC))); 794 } 795 { 796 const char kSetCC[kTcpCaNameMax] = "cubic"; 797 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 798 &kSetCC, strlen(kSetCC)), 799 SyscallSucceedsWithValue(0)); 800 801 char got_cc[kTcpCaNameMax]; 802 memset(got_cc, '1', sizeof(got_cc)); 803 socklen_t optlen = sizeof(got_cc); 804 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 805 &got_cc, &optlen), 806 SyscallSucceedsWithValue(0)); 807 EXPECT_EQ(0, memcmp(got_cc, kSetCC, sizeof(kSetCC))); 808 } 809 } 810 811 TEST_P(TCPSocketPairTest, SetGetTCPCongestionShortReadBuffer) { 812 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 813 { 814 // Verify that getsockopt/setsockopt work with buffers smaller than 815 // kTcpCaNameMax. 816 const char kSetCC[] = "cubic"; 817 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 818 &kSetCC, strlen(kSetCC)), 819 SyscallSucceedsWithValue(0)); 820 821 char got_cc[sizeof(kSetCC)]; 822 socklen_t optlen = sizeof(got_cc); 823 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 824 &got_cc, &optlen), 825 SyscallSucceedsWithValue(0)); 826 EXPECT_EQ(0, memcmp(got_cc, kSetCC, sizeof(got_cc))); 827 } 828 } 829 830 TEST_P(TCPSocketPairTest, SetGetTCPCongestionLargeReadBuffer) { 831 // This is Linux's net/tcp.h TCP_CA_NAME_MAX. 832 const int kTcpCaNameMax = 16; 833 834 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 835 { 836 // Verify that getsockopt works with buffers larger than 837 // kTcpCaNameMax. 838 const char kSetCC[] = "cubic"; 839 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 840 &kSetCC, strlen(kSetCC)), 841 SyscallSucceedsWithValue(0)); 842 843 char got_cc[kTcpCaNameMax + 5]; 844 socklen_t optlen = sizeof(got_cc); 845 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 846 &got_cc, &optlen), 847 SyscallSucceedsWithValue(0)); 848 // Linux copies the minimum of kTcpCaNameMax or the length of the passed in 849 // buffer and sets optlen to the number of bytes actually copied 850 // irrespective of the actual length of the congestion control name. 851 EXPECT_EQ(kTcpCaNameMax, optlen); 852 EXPECT_EQ(0, memcmp(got_cc, kSetCC, sizeof(kSetCC))); 853 } 854 } 855 856 TEST_P(TCPSocketPairTest, SetCongestionControlFailsForUnsupported) { 857 // This is Linux's net/tcp.h TCP_CA_NAME_MAX. 858 const int kTcpCaNameMax = 16; 859 860 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 861 char old_cc[kTcpCaNameMax]; 862 socklen_t optlen = sizeof(old_cc); 863 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 864 &old_cc, &optlen), 865 SyscallSucceedsWithValue(0)); 866 867 const char kSetCC[] = "invalid_ca_cc"; 868 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 869 &kSetCC, strlen(kSetCC)), 870 SyscallFailsWithErrno(ENOENT)); 871 872 char got_cc[kTcpCaNameMax]; 873 optlen = sizeof(got_cc); 874 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CONGESTION, 875 &got_cc, &optlen), 876 SyscallSucceedsWithValue(0)); 877 EXPECT_EQ(0, memcmp(got_cc, old_cc, sizeof(old_cc))); 878 } 879 880 // Linux and Netstack both default to a 60s TCP_LINGER2 timeout. 881 constexpr int kDefaultTCPLingerTimeout = 60; 882 // On Linux, the maximum linger2 timeout was changed from 60sec to 120sec. 883 constexpr int kMaxTCPLingerTimeout = 120; 884 constexpr int kOldMaxTCPLingerTimeout = 60; 885 886 TEST_P(TCPSocketPairTest, TCPLingerTimeoutDefault) { 887 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 888 889 int get = -1; 890 socklen_t get_len = sizeof(get); 891 EXPECT_THAT( 892 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &get, &get_len), 893 SyscallSucceedsWithValue(0)); 894 EXPECT_EQ(get_len, sizeof(get)); 895 EXPECT_EQ(get, kDefaultTCPLingerTimeout); 896 } 897 898 TEST_P(TCPSocketPairTest, SetTCPLingerTimeoutLessThanZero) { 899 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 900 901 constexpr int kNegative = -1234; 902 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, 903 &kNegative, sizeof(kNegative)), 904 SyscallSucceedsWithValue(0)); 905 int get = INT_MAX; 906 socklen_t get_len = sizeof(get); 907 EXPECT_THAT( 908 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &get, &get_len), 909 SyscallSucceedsWithValue(0)); 910 EXPECT_EQ(get_len, sizeof(get)); 911 EXPECT_EQ(get, -1); 912 } 913 914 TEST_P(TCPSocketPairTest, SetTCPLingerTimeoutZero) { 915 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 916 917 constexpr int kZero = 0; 918 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &kZero, 919 sizeof(kZero)), 920 SyscallSucceedsWithValue(0)); 921 int get = -1; 922 socklen_t get_len = sizeof(get); 923 EXPECT_THAT( 924 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &get, &get_len), 925 SyscallSucceedsWithValue(0)); 926 EXPECT_EQ(get_len, sizeof(get)); 927 EXPECT_THAT(get, 928 AnyOf(Eq(kMaxTCPLingerTimeout), Eq(kOldMaxTCPLingerTimeout))); 929 } 930 931 TEST_P(TCPSocketPairTest, SoLingerOptionWithReset) { 932 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 933 934 // Set and get SO_LINGER with zero timeout. 935 struct linger sl; 936 sl.l_onoff = 1; 937 sl.l_linger = 0; 938 ASSERT_THAT( 939 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 940 SyscallSucceeds()); 941 char buf[1000] = {}; 942 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 943 SyscallSucceedsWithValue(sizeof(buf))); 944 945 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 946 947 write(sockets->second_fd(), buf, sizeof(buf)); 948 ASSERT_THAT(RetryEINTR(read)(sockets->second_fd(), buf, sizeof(buf)), 949 SyscallSucceedsWithValue(sizeof(buf))); 950 } 951 952 TEST_P(TCPSocketPairTest, SetTCPLingerTimeoutAboveMax) { 953 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 954 955 // Values above the net.ipv4.tcp_fin_timeout are capped to tcp_fin_timeout 956 // on linux (defaults to 60 seconds on linux). 957 constexpr int kAboveDefault = kMaxTCPLingerTimeout + 1; 958 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, 959 &kAboveDefault, sizeof(kAboveDefault)), 960 SyscallSucceedsWithValue(0)); 961 962 int get = -1; 963 socklen_t get_len = sizeof(get); 964 EXPECT_THAT( 965 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &get, &get_len), 966 SyscallSucceedsWithValue(0)); 967 EXPECT_EQ(get_len, sizeof(get)); 968 if (IsRunningOnGvisor() && !IsRunningWithHostinet()) { 969 EXPECT_EQ(get, kMaxTCPLingerTimeout); 970 } else { 971 EXPECT_THAT(get, 972 AnyOf(Eq(kMaxTCPLingerTimeout), Eq(kOldMaxTCPLingerTimeout))); 973 } 974 } 975 976 TEST_P(TCPSocketPairTest, SetTCPLingerTimeout) { 977 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 978 979 // Values above the net.ipv4.tcp_fin_timeout are capped to tcp_fin_timeout 980 // on linux (defaults to 60 seconds on linux). 981 constexpr int kTCPLingerTimeout = kDefaultTCPLingerTimeout - 1; 982 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, 983 &kTCPLingerTimeout, sizeof(kTCPLingerTimeout)), 984 SyscallSucceedsWithValue(0)); 985 986 int get = -1; 987 socklen_t get_len = sizeof(get); 988 EXPECT_THAT( 989 getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_LINGER2, &get, &get_len), 990 SyscallSucceedsWithValue(0)); 991 EXPECT_EQ(get_len, sizeof(get)); 992 EXPECT_EQ(get, kTCPLingerTimeout); 993 } 994 995 TEST_P(TCPSocketPairTest, TestTCPCloseWithData) { 996 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 997 998 ScopedThread t([&]() { 999 // Close one end to trigger sending of a FIN. 1000 ASSERT_THAT(shutdown(sockets->second_fd(), SHUT_WR), SyscallSucceeds()); 1001 char buf[3]; 1002 ASSERT_THAT(read(sockets->second_fd(), buf, 3), 1003 SyscallSucceedsWithValue(3)); 1004 absl::SleepFor(absl::Milliseconds(50)); 1005 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 1006 }); 1007 1008 absl::SleepFor(absl::Milliseconds(50)); 1009 // Send some data then close. 1010 constexpr char kStr[] = "abc"; 1011 ASSERT_THAT(write(sockets->first_fd(), kStr, 3), SyscallSucceedsWithValue(3)); 1012 t.Join(); 1013 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 1014 } 1015 1016 TEST_P(TCPSocketPairTest, TCPUserTimeoutDefault) { 1017 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1018 1019 int get = -1; 1020 socklen_t get_len = sizeof(get); 1021 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 1022 &get, &get_len), 1023 SyscallSucceeds()); 1024 EXPECT_EQ(get_len, sizeof(get)); 1025 EXPECT_EQ(get, 0); // 0 ms (disabled). 1026 } 1027 1028 TEST_P(TCPSocketPairTest, SetTCPUserTimeoutZero) { 1029 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1030 1031 constexpr int kZero = 0; 1032 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 1033 &kZero, sizeof(kZero)), 1034 SyscallSucceeds()); 1035 1036 int get = -1; 1037 socklen_t get_len = sizeof(get); 1038 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 1039 &get, &get_len), 1040 SyscallSucceeds()); 1041 EXPECT_EQ(get_len, sizeof(get)); 1042 EXPECT_EQ(get, 0); // 0 ms (disabled). 1043 } 1044 1045 TEST_P(TCPSocketPairTest, SetTCPUserTimeoutBelowZero) { 1046 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1047 1048 constexpr int kNeg = -10; 1049 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 1050 &kNeg, sizeof(kNeg)), 1051 SyscallFailsWithErrno(EINVAL)); 1052 1053 int get = -1; 1054 socklen_t get_len = sizeof(get); 1055 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 1056 &get, &get_len), 1057 SyscallSucceeds()); 1058 EXPECT_EQ(get_len, sizeof(get)); 1059 EXPECT_EQ(get, 0); // 0 ms (disabled). 1060 } 1061 1062 TEST_P(TCPSocketPairTest, SetTCPUserTimeoutAboveZero) { 1063 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1064 1065 constexpr int kAbove = 10; 1066 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 1067 &kAbove, sizeof(kAbove)), 1068 SyscallSucceeds()); 1069 1070 int get = -1; 1071 socklen_t get_len = sizeof(get); 1072 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_USER_TIMEOUT, 1073 &get, &get_len), 1074 SyscallSucceeds()); 1075 EXPECT_EQ(get_len, sizeof(get)); 1076 EXPECT_EQ(get, kAbove); 1077 } 1078 1079 #ifdef __linux__ 1080 TEST_P(TCPSocketPairTest, SpliceFromPipe) { 1081 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1082 1083 int fds[2]; 1084 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 1085 FileDescriptor rfd(fds[0]); 1086 FileDescriptor wfd(fds[1]); 1087 1088 // Fill with some random data. 1089 std::vector<char> buf(kPageSize / 2); 1090 RandomizeBuffer(buf.data(), buf.size()); 1091 ASSERT_THAT(write(wfd.get(), buf.data(), buf.size()), 1092 SyscallSucceedsWithValue(buf.size())); 1093 1094 EXPECT_THAT( 1095 splice(rfd.get(), nullptr, sockets->first_fd(), nullptr, kPageSize, 0), 1096 SyscallSucceedsWithValue(buf.size())); 1097 1098 std::vector<char> rbuf(buf.size()); 1099 ASSERT_THAT(read(sockets->second_fd(), rbuf.data(), rbuf.size()), 1100 SyscallSucceedsWithValue(buf.size())); 1101 EXPECT_EQ(memcmp(rbuf.data(), buf.data(), buf.size()), 0); 1102 } 1103 1104 TEST_P(TCPSocketPairTest, SpliceToPipe) { 1105 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1106 1107 int fds[2]; 1108 ASSERT_THAT(pipe(fds), SyscallSucceeds()); 1109 FileDescriptor rfd(fds[0]); 1110 FileDescriptor wfd(fds[1]); 1111 1112 // Fill with some random data. 1113 std::vector<char> buf(kPageSize / 2); 1114 RandomizeBuffer(buf.data(), buf.size()); 1115 ASSERT_THAT(write(sockets->first_fd(), buf.data(), buf.size()), 1116 SyscallSucceedsWithValue(buf.size())); 1117 shutdown(sockets->first_fd(), SHUT_WR); 1118 EXPECT_THAT( 1119 splice(sockets->second_fd(), nullptr, wfd.get(), nullptr, kPageSize, 0), 1120 SyscallSucceedsWithValue(buf.size())); 1121 1122 std::vector<char> rbuf(buf.size()); 1123 ASSERT_THAT(read(rfd.get(), rbuf.data(), rbuf.size()), 1124 SyscallSucceedsWithValue(buf.size())); 1125 EXPECT_EQ(memcmp(rbuf.data(), buf.data(), buf.size()), 0); 1126 } 1127 1128 // Regression test for #9932. 1129 TEST_P(TCPSocketPairTest, LargeSpliceFromPipe) { 1130 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1131 1132 // Create a pipe, increase its size from the default 64K, and fill it with 1133 // data. 1134 int pipe_fds[2]; 1135 ASSERT_THAT(pipe(pipe_fds), SyscallSucceeds()); 1136 const FileDescriptor pipe_rfd(pipe_fds[0]); 1137 const FileDescriptor pipe_wfd(pipe_fds[1]); 1138 constexpr size_t kPipeSize = 1 << 20; 1139 ASSERT_THAT(fcntl(pipe_wfd.get(), F_SETPIPE_SZ, kPipeSize), 1140 SyscallSucceeds()); 1141 std::vector<char> orig_data(kPipeSize); 1142 RandomizeBuffer(orig_data.data(), orig_data.size()); 1143 ASSERT_THAT(WriteFd(pipe_wfd.get(), orig_data.data(), orig_data.size()), 1144 SyscallSucceedsWithValue(orig_data.size())); 1145 1146 // Splice all data from the pipe to one end of the TCP socket pair in a 1147 // separate thread, while draining the other end from this thread. 1148 std::vector<char> read_data(orig_data.size()); 1149 ScopedThread reader_thread([&] { 1150 size_t spliced_bytes = 0; 1151 ssize_t n; 1152 while (spliced_bytes < orig_data.size()) { 1153 ASSERT_THAT( 1154 n = RetryEINTR(splice)(pipe_rfd.get(), nullptr, sockets->first_fd(), 1155 nullptr, orig_data.size() - spliced_bytes, 0), 1156 SyscallSucceeds()); 1157 spliced_bytes += n; 1158 } 1159 }); 1160 size_t read_bytes = 0; 1161 while (read_bytes < read_data.size()) { 1162 ssize_t n; 1163 ASSERT_THAT(n = RetryEINTR(read)(sockets->second_fd(), 1164 read_data.data() + read_bytes, 1165 read_data.size() - read_bytes), 1166 SyscallSucceeds()); 1167 read_bytes += n; 1168 } 1169 1170 // Check that correct data was spliced and read. 1171 EXPECT_EQ(0, memcmp(orig_data.data(), read_data.data(), orig_data.size())); 1172 } 1173 1174 #include <sys/sendfile.h> 1175 1176 #include <memory> 1177 1178 TEST_P(TCPSocketPairTest, SendfileFromRegularFileSucceeds) { 1179 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1180 const TempPath in_file = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateFile()); 1181 const FileDescriptor in_fd = 1182 ASSERT_NO_ERRNO_AND_VALUE(Open(in_file.path(), O_RDWR)); 1183 // Fill with some random data. 1184 std::vector<char> buf(kPageSize / 2); 1185 RandomizeBuffer(buf.data(), buf.size()); 1186 ASSERT_THAT(pwrite(in_fd.get(), buf.data(), buf.size(), 0), 1187 SyscallSucceedsWithValue(buf.size())); 1188 1189 EXPECT_THAT( 1190 sendfile(sockets->first_fd(), in_fd.get(), nullptr, buf.size() + 1), 1191 SyscallSucceedsWithValue(buf.size())); 1192 std::vector<char> rbuf(buf.size() + 1); 1193 ASSERT_THAT(read(sockets->second_fd(), rbuf.data(), rbuf.size()), 1194 SyscallSucceedsWithValue(buf.size())); 1195 EXPECT_EQ(memcmp(rbuf.data(), buf.data(), buf.size()), 0); 1196 } 1197 #endif // __linux__ 1198 1199 TEST_P(TCPSocketPairTest, SetTCPWindowClampBelowMinRcvBufConnectedSocket) { 1200 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1201 // Discover minimum receive buf by setting a really low value 1202 // for the receive buffer. 1203 constexpr int kZero = 0; 1204 EXPECT_THAT(setsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVBUF, &kZero, 1205 sizeof(kZero)), 1206 SyscallSucceeds()); 1207 1208 // Now retrieve the minimum value for SO_RCVBUF as the set above should 1209 // have caused SO_RCVBUF for the socket to be set to the minimum. 1210 int get = -1; 1211 socklen_t get_len = sizeof(get); 1212 ASSERT_THAT( 1213 getsockopt(sockets->first_fd(), SOL_SOCKET, SO_RCVBUF, &get, &get_len), 1214 SyscallSucceedsWithValue(0)); 1215 EXPECT_EQ(get_len, sizeof(get)); 1216 int min_so_rcvbuf = get; 1217 1218 { 1219 // Setting TCP_WINDOW_CLAMP to zero for a connected socket is not permitted. 1220 constexpr int kZero = 0; 1221 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_WINDOW_CLAMP, 1222 &kZero, sizeof(kZero)), 1223 SyscallFailsWithErrno(EINVAL)); 1224 1225 // Non-zero clamp values below MIN_SO_RCVBUF/2 should result in the clamp 1226 // being set to MIN_SO_RCVBUF/2. 1227 int below_half_min_so_rcvbuf = min_so_rcvbuf / 2 - 1; 1228 EXPECT_THAT( 1229 setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_WINDOW_CLAMP, 1230 &below_half_min_so_rcvbuf, sizeof(below_half_min_so_rcvbuf)), 1231 SyscallSucceeds()); 1232 1233 int get = -1; 1234 socklen_t get_len = sizeof(get); 1235 1236 ASSERT_THAT(getsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_WINDOW_CLAMP, 1237 &get, &get_len), 1238 SyscallSucceedsWithValue(0)); 1239 EXPECT_EQ(get_len, sizeof(get)); 1240 EXPECT_EQ(min_so_rcvbuf / 2, get); 1241 } 1242 } 1243 1244 TEST_P(TCPSocketPairTest, IpMulticastTtlDefault) { 1245 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1246 1247 int get = -1; 1248 socklen_t get_len = sizeof(get); 1249 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_IP, IP_MULTICAST_TTL, 1250 &get, &get_len), 1251 SyscallSucceedsWithValue(0)); 1252 EXPECT_EQ(get_len, sizeof(get)); 1253 EXPECT_GT(get, 0); 1254 } 1255 1256 TEST_P(TCPSocketPairTest, IpMulticastLoopDefault) { 1257 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1258 1259 int get = -1; 1260 socklen_t get_len = sizeof(get); 1261 EXPECT_THAT(getsockopt(sockets->first_fd(), IPPROTO_IP, IP_MULTICAST_LOOP, 1262 &get, &get_len), 1263 SyscallSucceedsWithValue(0)); 1264 EXPECT_EQ(get_len, sizeof(get)); 1265 EXPECT_EQ(get, 1); 1266 } 1267 1268 TEST_P(TCPSocketPairTest, TCPResetDuringClose) { 1269 DisableSave ds; // Too many syscalls. 1270 constexpr int kThreadCount = 100; 1271 std::unique_ptr<ScopedThread> instances[kThreadCount]; 1272 for (int i = 0; i < kThreadCount; i++) { 1273 instances[i] = std::make_unique<ScopedThread>([&]() { 1274 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1275 1276 ScopedThread t([&]() { 1277 // Close one end to trigger sending of a FIN. 1278 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 1279 // Wait up to 20 seconds for the data. 1280 constexpr int kPollTimeoutMs = 20000; 1281 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 1282 SyscallSucceedsWithValue(1)); 1283 ASSERT_THAT(close(sockets->release_second_fd()), SyscallSucceeds()); 1284 }); 1285 1286 // Send some data then close. 1287 constexpr char kStr[] = "abc"; 1288 ASSERT_THAT(write(sockets->first_fd(), kStr, 3), 1289 SyscallSucceedsWithValue(3)); 1290 absl::SleepFor(absl::Milliseconds(10)); 1291 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 1292 t.Join(); 1293 }); 1294 } 1295 for (int i = 0; i < kThreadCount; i++) { 1296 instances[i]->Join(); 1297 } 1298 } 1299 1300 // Test setsockopt and getsockopt for a socket with SO_LINGER option. 1301 TEST_P(TCPSocketPairTest, SetAndGetLingerOption) { 1302 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1303 1304 // Check getsockopt before SO_LINGER option is set. 1305 struct linger got_linger = {-1, -1}; 1306 socklen_t got_len = sizeof(got_linger); 1307 1308 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1309 &got_linger, &got_len), 1310 SyscallSucceeds()); 1311 ASSERT_THAT(got_len, sizeof(got_linger)); 1312 struct linger want_linger = {}; 1313 EXPECT_EQ(0, memcmp(&want_linger, &got_linger, got_len)); 1314 1315 // Set and get SO_LINGER with negative values. 1316 struct linger sl; 1317 sl.l_onoff = 1; 1318 sl.l_linger = -3; 1319 ASSERT_THAT( 1320 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1321 SyscallSucceeds()); 1322 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1323 &got_linger, &got_len), 1324 SyscallSucceeds()); 1325 ASSERT_EQ(got_len, sizeof(got_linger)); 1326 EXPECT_EQ(sl.l_onoff, got_linger.l_onoff); 1327 // Linux returns a different value as it uses HZ to convert the seconds to 1328 // jiffies which overflows for negative values. We want to be compatible with 1329 // linux for getsockopt return value. 1330 if (IsRunningOnGvisor() && !IsRunningWithHostinet()) { 1331 EXPECT_EQ(sl.l_linger, got_linger.l_linger); 1332 } 1333 1334 // Set and get SO_LINGER option with positive values. 1335 sl.l_onoff = 1; 1336 sl.l_linger = 5; 1337 ASSERT_THAT( 1338 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1339 SyscallSucceeds()); 1340 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1341 &got_linger, &got_len), 1342 SyscallSucceeds()); 1343 ASSERT_EQ(got_len, sizeof(got_linger)); 1344 EXPECT_EQ(0, memcmp(&sl, &got_linger, got_len)); 1345 } 1346 1347 // Test socket to disable SO_LINGER option. 1348 TEST_P(TCPSocketPairTest, SetOffLingerOption) { 1349 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1350 1351 // Set the SO_LINGER option. 1352 struct linger sl; 1353 sl.l_onoff = 1; 1354 sl.l_linger = 5; 1355 ASSERT_THAT( 1356 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1357 SyscallSucceeds()); 1358 1359 // Check getsockopt after SO_LINGER option is set. 1360 struct linger got_linger = {-1, -1}; 1361 socklen_t got_len = sizeof(got_linger); 1362 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1363 &got_linger, &got_len), 1364 SyscallSucceeds()); 1365 ASSERT_EQ(got_len, sizeof(got_linger)); 1366 EXPECT_EQ(0, memcmp(&sl, &got_linger, got_len)); 1367 1368 sl.l_onoff = 0; 1369 sl.l_linger = 5; 1370 ASSERT_THAT( 1371 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1372 SyscallSucceeds()); 1373 1374 // Check getsockopt after SO_LINGER option is set to zero. 1375 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1376 &got_linger, &got_len), 1377 SyscallSucceeds()); 1378 ASSERT_EQ(got_len, sizeof(got_linger)); 1379 EXPECT_EQ(0, memcmp(&sl, &got_linger, got_len)); 1380 } 1381 1382 // Test close on dup'd socket with SO_LINGER option set. 1383 TEST_P(TCPSocketPairTest, CloseWithLingerOption) { 1384 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1385 1386 // Set the SO_LINGER option. 1387 struct linger sl; 1388 sl.l_onoff = 1; 1389 sl.l_linger = 5; 1390 ASSERT_THAT( 1391 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1392 SyscallSucceeds()); 1393 1394 // Check getsockopt after SO_LINGER option is set. 1395 struct linger got_linger = {-1, -1}; 1396 socklen_t got_len = sizeof(got_linger); 1397 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1398 &got_linger, &got_len), 1399 SyscallSucceeds()); 1400 ASSERT_EQ(got_len, sizeof(got_linger)); 1401 EXPECT_EQ(0, memcmp(&sl, &got_linger, got_len)); 1402 1403 FileDescriptor dupFd = FileDescriptor(dup(sockets->first_fd())); 1404 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 1405 char buf[10] = {}; 1406 // Write on dupFd should succeed as socket will not be closed until 1407 // all references are removed. 1408 ASSERT_THAT(RetryEINTR(write)(dupFd.get(), buf, sizeof(buf)), 1409 SyscallSucceedsWithValue(sizeof(buf))); 1410 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 1411 SyscallFailsWithErrno(EBADF)); 1412 1413 // Close the socket. 1414 dupFd.reset(); 1415 // Write on dupFd should fail as all references for socket are removed. 1416 ASSERT_THAT(RetryEINTR(write)(dupFd.get(), buf, sizeof(buf)), 1417 SyscallFailsWithErrno(EBADF)); 1418 } 1419 1420 TEST_P(TCPSocketPairTest, ResetWithSoLingerZeroTimeoutOption) { 1421 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1422 1423 // Check getsockopt before SO_LINGER option is set. 1424 struct linger got_linger = {-1, -1}; 1425 socklen_t got_len = sizeof(got_linger); 1426 1427 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1428 &got_linger, &got_len), 1429 SyscallSucceeds()); 1430 ASSERT_THAT(got_len, sizeof(got_linger)); 1431 struct linger want_linger = {}; 1432 EXPECT_EQ(0, memcmp(&want_linger, &got_linger, got_len)); 1433 1434 char buf[10] = {}; 1435 ASSERT_THAT(RetryEINTR(write)(sockets->first_fd(), buf, sizeof(buf)), 1436 SyscallSucceedsWithValue(sizeof(buf))); 1437 1438 // Set and get SO_LINGER with zero timeout. 1439 struct linger sl; 1440 sl.l_onoff = 1; 1441 sl.l_linger = 0; 1442 ASSERT_THAT( 1443 setsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, &sl, sizeof(sl)), 1444 SyscallSucceeds()); 1445 ASSERT_THAT(getsockopt(sockets->first_fd(), SOL_SOCKET, SO_LINGER, 1446 &got_linger, &got_len), 1447 SyscallSucceeds()); 1448 ASSERT_EQ(got_len, sizeof(got_linger)); 1449 EXPECT_EQ(sl.l_onoff, got_linger.l_onoff); 1450 EXPECT_EQ(sl.l_linger, got_linger.l_linger); 1451 1452 // Wait until the socket sees the data on its side but don't read it. 1453 struct pollfd poll_fd = {sockets->second_fd(), POLLIN | POLLHUP, 0}; 1454 constexpr int kPollTimeoutMs = 20000; // Wait up to 20 seconds for the data. 1455 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 1456 SyscallSucceedsWithValue(1)); 1457 1458 ASSERT_THAT(close(sockets->release_first_fd()), SyscallSucceeds()); 1459 1460 // Attempt to write, but not possible because of connection reset. 1461 poll_fd = {sockets->second_fd(), POLLHUP, 0}; 1462 ASSERT_THAT(RetryEINTR(poll)(&poll_fd, 1, kPollTimeoutMs), 1463 SyscallSucceedsWithValue(1)); 1464 1465 char buffer[10] = {}; 1466 ASSERT_THAT(RetryEINTR(write)(sockets->second_fd(), buffer, sizeof(buffer)), 1467 SyscallFailsWithErrno(ECONNRESET)); 1468 1469 ASSERT_THAT(RetryEINTR(read)(sockets->second_fd(), buf, sizeof(buf)), 1470 SyscallSucceedsWithValue(sizeof(buf))); 1471 } 1472 1473 TEST_P(TCPSocketPairTest, WaitTillMSSWithCorkOption) { 1474 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1475 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, 1476 &kSockOptOn, sizeof(kSockOptOn)), 1477 SyscallSucceeds()); 1478 1479 constexpr int kTCPMaxSeg = 1024; 1480 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_MAXSEG, 1481 &kTCPMaxSeg, sizeof(kTCPMaxSeg)), 1482 SyscallSucceedsWithValue(0)); 1483 1484 // Use the buffer size which is guaranteed to be >= MSS. 1485 char buffer[1024] = {}; 1486 EXPECT_THAT(RetryEINTR(send)(sockets->first_fd(), buffer, sizeof(buffer), 0), 1487 SyscallSucceedsWithValue(sizeof(buffer))); 1488 1489 char buf[1024] = {}; 1490 EXPECT_THAT(RetryEINTR(recv)(sockets->second_fd(), buf, sizeof(buf), 0), 1491 SyscallSucceedsWithValue(sizeof(buf))); 1492 } 1493 1494 TEST_P(TCPSocketPairTest, WaitTillTimeoutWithCorkOption) { 1495 auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair()); 1496 ASSERT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_CORK, 1497 &kSockOptOn, sizeof(kSockOptOn)), 1498 SyscallSucceeds()); 1499 1500 constexpr int kTCPMaxSeg = 1024; 1501 EXPECT_THAT(setsockopt(sockets->first_fd(), IPPROTO_TCP, TCP_MAXSEG, 1502 &kTCPMaxSeg, sizeof(kTCPMaxSeg)), 1503 SyscallSucceedsWithValue(0)); 1504 1505 // Use buffer size less than MSS. 1506 char buffer[512] = {}; 1507 EXPECT_THAT(RetryEINTR(send)(sockets->first_fd(), buffer, sizeof(buffer), 0), 1508 SyscallSucceedsWithValue(sizeof(buffer))); 1509 1510 char buf[512] = {}; 1511 EXPECT_THAT(RetryEINTR(recv)(sockets->second_fd(), buf, sizeof(buf), 0), 1512 SyscallSucceedsWithValue(sizeof(buf))); 1513 } 1514 1515 } // namespace testing 1516 } // namespace gvisor