Skip to content

Commit 1b61de4

Browse files
committed
Test also recv() reading a whole datagram
1 parent 579fbe8 commit 1b61de4

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

‎Lib/test/test_socket.py‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
HOST = socket_helper.HOST
5252
# test unicode string and carriage return
5353
MSG = 'Michael Gilfix was here\u1234\r\n'.encode('utf-8')
54+
MSG2 = 'Monty Python test'.encode('utf-8')
5455

5556
VSOCKPORT = 1234
5657
AIX = platform.system() == "AIX"
@@ -3308,20 +3309,32 @@ def _testRecvFromNegative(self):
33083309
@requireAttrs(socket, "MSG_TRUNC")
33093310
@unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE")
33103311
def testRecvTrunc(self):
3311-
msg = self.serv.recv(2, socket.MSG_TRUNC)
3312-
self.assertEqual(msg, MSG[:2])
3312+
# Truncated read where recv() returns the real size of the datagram
3313+
msg = self.serv.recv(5, socket.MSG_TRUNC)
3314+
self.assertEqual(msg, MSG[:5])
3315+
3316+
# Read the whole second datagram
3317+
msg = self.serv.recv(len(MSG2) + 5, socket.MSG_TRUNC)
3318+
self.assertEqual(msg, MSG2)
33133319

33143320
def _testRecvTrunc(self):
33153321
self.cli.sendto(MSG, 0, (HOST, self.port))
3322+
self.cli.sendto(MSG2, 0, (HOST, self.port))
33163323

33173324
@requireAttrs(socket, "MSG_TRUNC")
33183325
@unittest.skipIf(sys.platform == "win32", "Windows raises WSAEMSGSIZE")
33193326
def testRecvFromTrunc(self):
3320-
msg, addr = self.serv.recvfrom(2, socket.MSG_TRUNC)
3321-
self.assertEqual(msg, MSG[:2])
3327+
# Truncated read where recvfrom() returns the real size of the datagram
3328+
msg, addr = self.serv.recvfrom(5, socket.MSG_TRUNC)
3329+
self.assertEqual(msg, MSG[:5])
3330+
3331+
# Read the whole second datagram
3332+
msg, addr = self.serv.recvfrom(len(MSG2) + 5, socket.MSG_TRUNC)
3333+
self.assertEqual(msg, MSG2)
33223334

33233335
def _testRecvFromTrunc(self):
33243336
self.cli.sendto(MSG, 0, (HOST, self.port))
3337+
self.cli.sendto(MSG2, 0, (HOST, self.port))
33253338

33263339

33273340
@unittest.skipUnless(HAVE_SOCKET_UDPLITE,

‎Modules/socketmodule.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4066,7 +4066,8 @@ _socket_socket_recv_impl(PySocketSockObject *s, Py_ssize_t recvlen,
40664066
PyBytesWriter_Discard(writer);
40674067
return NULL;
40684068
}
4069-
/* With MSG_TRUNC, recv() can return the real length of the datagram,
4069+
4070+
/* With MSG_TRUNC, recv() returns the real size of the datagram,
40704071
which may be larger than the buffer. Only recvlen bytes were
40714072
written. */
40724073
if (outlen > recvlen) {
@@ -4248,7 +4249,10 @@ _socket_socket_recvfrom_impl(PySocketSockObject *s, Py_ssize_t recvlen,
42484249
PyBytesWriter_Discard(writer);
42494250
goto error;
42504251
}
4251-
/* See the comment in _socket_socket_recv_impl(). */
4252+
4253+
/* With MSG_TRUNC, recvfrom() returns the real size of the datagram,
4254+
which may be larger than the buffer. Only recvlen bytes were
4255+
written. */
42524256
if (outlen > recvlen) {
42534257
outlen = recvlen;
42544258
}

0 commit comments

Comments
 (0)