/test/syscalls/linux/socket_non_stream.cc

https://github.com/google/gvisor · C++ · 337 lines · 235 code · 50 blank · 52 comment · 1 complexity · 3a45336b61ef9fd571b4969ad70a1153 MD5 · raw file

  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. #include "test/syscalls/linux/socket_non_stream.h"
  15. #include <stdio.h>
  16. #include <sys/socket.h>
  17. #include <sys/un.h>
  18. #include "gtest/gtest.h"
  19. #include "test/syscalls/linux/ip_socket_test_util.h"
  20. #include "test/syscalls/linux/socket_test_util.h"
  21. #include "test/syscalls/linux/unix_domain_socket_test_util.h"
  22. #include "test/util/test_util.h"
  23. namespace gvisor {
  24. namespace testing {
  25. TEST_P(NonStreamSocketPairTest, SendMsgTooLarge) {
  26. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  27. int sndbuf;
  28. socklen_t length = sizeof(sndbuf);
  29. ASSERT_THAT(
  30. getsockopt(sockets->first_fd(), SOL_SOCKET, SO_SNDBUF, &sndbuf, &length),
  31. SyscallSucceeds());
  32. // Make the call too large to fit in the send buffer.
  33. const int buffer_size = 3 * sndbuf;
  34. EXPECT_THAT(SendLargeSendMsg(sockets, buffer_size, false /* reader */),
  35. SyscallFailsWithErrno(EMSGSIZE));
  36. }
  37. // Stream sockets allow data sent with a single (e.g. write, sendmsg) syscall
  38. // to be read in pieces with multiple (e.g. read, recvmsg) syscalls.
  39. //
  40. // SplitRecv checks that control messages can only be read on the first (e.g.
  41. // read, recvmsg) syscall, even if it doesn't provide space for the control
  42. // message.
  43. TEST_P(NonStreamSocketPairTest, SplitRecv) {
  44. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  45. char sent_data[512];
  46. RandomizeBuffer(sent_data, sizeof(sent_data));
  47. ASSERT_THAT(
  48. RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0),
  49. SyscallSucceedsWithValue(sizeof(sent_data)));
  50. char received_data[sizeof(sent_data) / 2];
  51. ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data,
  52. sizeof(received_data), 0),
  53. SyscallSucceedsWithValue(sizeof(received_data)));
  54. EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(received_data)));
  55. ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data,
  56. sizeof(received_data), MSG_DONTWAIT),
  57. SyscallFailsWithErrno(EWOULDBLOCK));
  58. }
  59. // Stream sockets allow data sent with multiple sends to be read in a single
  60. // recv. Datagram sockets do not.
  61. //
  62. // SingleRecv checks that only a single message is readable in a single recv.
  63. TEST_P(NonStreamSocketPairTest, SingleRecv) {
  64. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  65. char sent_data1[20];
  66. RandomizeBuffer(sent_data1, sizeof(sent_data1));
  67. ASSERT_THAT(
  68. RetryEINTR(send)(sockets->first_fd(), sent_data1, sizeof(sent_data1), 0),
  69. SyscallSucceedsWithValue(sizeof(sent_data1)));
  70. char sent_data2[20];
  71. RandomizeBuffer(sent_data2, sizeof(sent_data2));
  72. ASSERT_THAT(
  73. RetryEINTR(send)(sockets->first_fd(), sent_data2, sizeof(sent_data2), 0),
  74. SyscallSucceedsWithValue(sizeof(sent_data2)));
  75. char received_data[sizeof(sent_data1) + sizeof(sent_data2)];
  76. ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data,
  77. sizeof(received_data), 0),
  78. SyscallSucceedsWithValue(sizeof(sent_data1)));
  79. EXPECT_EQ(0, memcmp(sent_data1, received_data, sizeof(sent_data1)));
  80. }
  81. TEST_P(NonStreamSocketPairTest, RecvmsgMsghdrFlagMsgTrunc) {
  82. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  83. char sent_data[10];
  84. RandomizeBuffer(sent_data, sizeof(sent_data));
  85. ASSERT_THAT(
  86. RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0),
  87. SyscallSucceedsWithValue(sizeof(sent_data)));
  88. char received_data[sizeof(sent_data) / 2] = {};
  89. struct iovec iov;
  90. iov.iov_base = received_data;
  91. iov.iov_len = sizeof(received_data);
  92. struct msghdr msg = {};
  93. msg.msg_flags = -1;
  94. msg.msg_iov = &iov;
  95. msg.msg_iovlen = 1;
  96. ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &msg, 0),
  97. SyscallSucceedsWithValue(sizeof(received_data)));
  98. EXPECT_EQ(0, memcmp(received_data, sent_data, sizeof(received_data)));
  99. // Check that msghdr flags were updated.
  100. EXPECT_EQ(msg.msg_flags & MSG_TRUNC, MSG_TRUNC);
  101. }
  102. // Stream sockets allow data sent with multiple sends to be peeked at in a
  103. // single recv. Datagram sockets (except for unix sockets) do not.
  104. //
  105. // SinglePeek checks that only a single message is peekable in a single recv.
  106. TEST_P(NonStreamSocketPairTest, SinglePeek) {
  107. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  108. char sent_data1[20];
  109. RandomizeBuffer(sent_data1, sizeof(sent_data1));
  110. ASSERT_THAT(
  111. RetryEINTR(send)(sockets->first_fd(), sent_data1, sizeof(sent_data1), 0),
  112. SyscallSucceedsWithValue(sizeof(sent_data1)));
  113. char sent_data2[20];
  114. RandomizeBuffer(sent_data2, sizeof(sent_data2));
  115. ASSERT_THAT(
  116. RetryEINTR(send)(sockets->first_fd(), sent_data2, sizeof(sent_data2), 0),
  117. SyscallSucceedsWithValue(sizeof(sent_data2)));
  118. char received_data[sizeof(sent_data1) + sizeof(sent_data2)];
  119. for (int i = 0; i < 3; i++) {
  120. memset(received_data, 0, sizeof(received_data));
  121. ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data,
  122. sizeof(received_data), MSG_PEEK),
  123. SyscallSucceedsWithValue(sizeof(sent_data1)));
  124. EXPECT_EQ(0, memcmp(sent_data1, received_data, sizeof(sent_data1)));
  125. }
  126. ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data,
  127. sizeof(sent_data1), 0),
  128. SyscallSucceedsWithValue(sizeof(sent_data1)));
  129. EXPECT_EQ(0, memcmp(sent_data1, received_data, sizeof(sent_data1)));
  130. ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data,
  131. sizeof(sent_data2), 0),
  132. SyscallSucceedsWithValue(sizeof(sent_data2)));
  133. EXPECT_EQ(0, memcmp(sent_data2, received_data, sizeof(sent_data2)));
  134. }
  135. TEST_P(NonStreamSocketPairTest, MsgTruncTruncation) {
  136. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  137. char sent_data[512];
  138. RandomizeBuffer(sent_data, sizeof(sent_data));
  139. ASSERT_THAT(
  140. RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0),
  141. SyscallSucceedsWithValue(sizeof(sent_data)));
  142. char received_data[sizeof(sent_data)] = {};
  143. ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data,
  144. sizeof(received_data) / 2, MSG_TRUNC),
  145. SyscallSucceedsWithValue(sizeof(sent_data)));
  146. EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(sent_data) / 2));
  147. // Check that we didn't get any extra data.
  148. EXPECT_NE(0, memcmp(sent_data + sizeof(sent_data) / 2,
  149. received_data + sizeof(received_data) / 2,
  150. sizeof(sent_data) / 2));
  151. }
  152. TEST_P(NonStreamSocketPairTest, MsgTruncTruncationRecvmsgMsghdrFlagMsgTrunc) {
  153. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  154. char sent_data[10];
  155. RandomizeBuffer(sent_data, sizeof(sent_data));
  156. ASSERT_THAT(
  157. RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0),
  158. SyscallSucceedsWithValue(sizeof(sent_data)));
  159. char received_data[sizeof(sent_data) / 2] = {};
  160. struct iovec iov;
  161. iov.iov_base = received_data;
  162. iov.iov_len = sizeof(received_data);
  163. struct msghdr msg = {};
  164. msg.msg_flags = -1;
  165. msg.msg_iov = &iov;
  166. msg.msg_iovlen = 1;
  167. ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &msg, MSG_TRUNC),
  168. SyscallSucceedsWithValue(sizeof(sent_data)));
  169. EXPECT_EQ(0, memcmp(received_data, sent_data, sizeof(received_data)));
  170. // Check that msghdr flags were updated.
  171. EXPECT_EQ(msg.msg_flags & MSG_TRUNC, MSG_TRUNC);
  172. }
  173. TEST_P(NonStreamSocketPairTest, MsgTruncSameSize) {
  174. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  175. char sent_data[512];
  176. RandomizeBuffer(sent_data, sizeof(sent_data));
  177. ASSERT_THAT(
  178. RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0),
  179. SyscallSucceedsWithValue(sizeof(sent_data)));
  180. char received_data[sizeof(sent_data)];
  181. ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data,
  182. sizeof(received_data), MSG_TRUNC),
  183. SyscallSucceedsWithValue(sizeof(received_data)));
  184. EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(sent_data)));
  185. }
  186. TEST_P(NonStreamSocketPairTest, MsgTruncNotFull) {
  187. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  188. char sent_data[512];
  189. RandomizeBuffer(sent_data, sizeof(sent_data));
  190. ASSERT_THAT(
  191. RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0),
  192. SyscallSucceedsWithValue(sizeof(sent_data)));
  193. char received_data[2 * sizeof(sent_data)];
  194. ASSERT_THAT(RetryEINTR(recv)(sockets->second_fd(), received_data,
  195. sizeof(received_data), MSG_TRUNC),
  196. SyscallSucceedsWithValue(sizeof(sent_data)));
  197. EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(sent_data)));
  198. }
  199. // This test tests reading from a socket with MSG_TRUNC and a zero length
  200. // receive buffer. The user should be able to get the message length.
  201. TEST_P(NonStreamSocketPairTest, RecvmsgMsgTruncZeroLen) {
  202. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  203. char sent_data[10];
  204. RandomizeBuffer(sent_data, sizeof(sent_data));
  205. ASSERT_THAT(
  206. RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0),
  207. SyscallSucceedsWithValue(sizeof(sent_data)));
  208. // The receive buffer is of zero length.
  209. char received_data[0] = {};
  210. struct iovec iov;
  211. iov.iov_base = received_data;
  212. iov.iov_len = sizeof(received_data);
  213. struct msghdr msg = {};
  214. msg.msg_flags = -1;
  215. msg.msg_iov = &iov;
  216. msg.msg_iovlen = 1;
  217. // The syscall succeeds returning the full size of the message on the socket.
  218. ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &msg, MSG_TRUNC),
  219. SyscallSucceedsWithValue(sizeof(sent_data)));
  220. // Check that MSG_TRUNC is set on msghdr flags.
  221. EXPECT_EQ(msg.msg_flags & MSG_TRUNC, MSG_TRUNC);
  222. }
  223. // This test tests reading from a socket with MSG_TRUNC | MSG_PEEK and a zero
  224. // length receive buffer. The user should be able to get the message length
  225. // without reading data off the socket.
  226. TEST_P(NonStreamSocketPairTest, RecvmsgMsgTruncMsgPeekZeroLen) {
  227. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  228. char sent_data[10];
  229. RandomizeBuffer(sent_data, sizeof(sent_data));
  230. ASSERT_THAT(
  231. RetryEINTR(send)(sockets->first_fd(), sent_data, sizeof(sent_data), 0),
  232. SyscallSucceedsWithValue(sizeof(sent_data)));
  233. // The receive buffer is of zero length.
  234. char peek_data[0] = {};
  235. struct iovec peek_iov;
  236. peek_iov.iov_base = peek_data;
  237. peek_iov.iov_len = sizeof(peek_data);
  238. struct msghdr peek_msg = {};
  239. peek_msg.msg_flags = -1;
  240. peek_msg.msg_iov = &peek_iov;
  241. peek_msg.msg_iovlen = 1;
  242. // The syscall succeeds returning the full size of the message on the socket.
  243. ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &peek_msg,
  244. MSG_TRUNC | MSG_PEEK),
  245. SyscallSucceedsWithValue(sizeof(sent_data)));
  246. // Check that MSG_TRUNC is set on msghdr flags because the receive buffer is
  247. // smaller than the message size.
  248. EXPECT_EQ(peek_msg.msg_flags & MSG_TRUNC, MSG_TRUNC);
  249. char received_data[sizeof(sent_data)] = {};
  250. struct iovec received_iov;
  251. received_iov.iov_base = received_data;
  252. received_iov.iov_len = sizeof(received_data);
  253. struct msghdr received_msg = {};
  254. received_msg.msg_flags = -1;
  255. received_msg.msg_iov = &received_iov;
  256. received_msg.msg_iovlen = 1;
  257. // Next we can read the actual data.
  258. ASSERT_THAT(
  259. RetryEINTR(recvmsg)(sockets->second_fd(), &received_msg, MSG_TRUNC),
  260. SyscallSucceedsWithValue(sizeof(sent_data)));
  261. EXPECT_EQ(0, memcmp(sent_data, received_data, sizeof(sent_data)));
  262. // Check that MSG_TRUNC is not set on msghdr flags because we read the whole
  263. // message.
  264. EXPECT_EQ(received_msg.msg_flags & MSG_TRUNC, 0);
  265. }
  266. // This test tests reading from a socket with MSG_TRUNC | MSG_PEEK and a zero
  267. // length receive buffer and MSG_DONTWAIT. The user should be able to get an
  268. // EAGAIN or EWOULDBLOCK error response.
  269. TEST_P(NonStreamSocketPairTest, RecvmsgTruncPeekDontwaitZeroLen) {
  270. auto sockets = ASSERT_NO_ERRNO_AND_VALUE(NewSocketPair());
  271. // NOTE: We don't send any data on the socket.
  272. // The receive buffer is of zero length.
  273. char peek_data[0] = {};
  274. struct iovec peek_iov;
  275. peek_iov.iov_base = peek_data;
  276. peek_iov.iov_len = sizeof(peek_data);
  277. struct msghdr peek_msg = {};
  278. peek_msg.msg_flags = -1;
  279. peek_msg.msg_iov = &peek_iov;
  280. peek_msg.msg_iovlen = 1;
  281. // recvmsg fails with EAGAIN because no data is available on the socket.
  282. ASSERT_THAT(RetryEINTR(recvmsg)(sockets->second_fd(), &peek_msg,
  283. MSG_TRUNC | MSG_PEEK | MSG_DONTWAIT),
  284. SyscallFailsWithErrno(EAGAIN));
  285. }
  286. } // namespace testing
  287. } // namespace gvisor