Merge pull request #16302 from iosabi/lwip_test

lwip_sock: Make sock_tcp_read return more data if available and test it
This commit is contained in:
Martine Lenders 2021-04-12 12:27:26 +02:00 committed by GitHub
commit 4e3ed19802
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 2 deletions

View File

@ -353,7 +353,12 @@ ssize_t sock_tcp_read(sock_tcp_t *sock, void *data, size_t max_len,
sock->last_buf = NULL;
sock->last_offset = 0;
pbuf_free(buf);
break;
/* Exit the loop only when there's no more data available in the
* connection. This allows to copy more data in a single read if
* available. */
if (!mbox_avail(&sock->base.conn->recvmbox.mbox)) {
break;
}
}
}
if (offset > 0) {

View File

@ -275,6 +275,55 @@ def test_tcpv6_send(board_group, application, env=None):
client.expect_exact(u"could not send")
def test_tcpv6_large_send(board_group, application, env=None):
"""Test that the TCP server can receive a large packet in multiple reads"""
if any(b.name != "native" for b in board_group.boards):
# run test only with native
print("SKIP_TEST INFO found non-native board")
return
env_server = os.environ.copy()
if env is not None:
env_server.update(env)
env_server.update(board_group.boards[1].to_env())
with pexpect.spawnu(MAKE, ["-C", application, "term"], env=env_server,
timeout=DEFAULT_TIMEOUT) as server:
port = random.randint(0x0000, 0xffff)
server_ip = get_ipv6_address(server)
try:
connect_addr = socket.getaddrinfo(
"%s%%tapbr0" % server_ip, port)[0][4]
except socket.gaierror as e:
print("SKIP_TEST INFO", e)
return
server.sendline(u"tcp server start %d" % port)
# wait for neighbor discovery to be done
time.sleep(5)
# Send large amount of data to verify multiple reads.
with socket.socket(socket.AF_INET6) as sock:
sock.connect(connect_addr)
server.expect(u"TCP client \\[[0-9a-f:]+\\]:[0-9]+ connected")
# Default read size in tcp.c SOCK_INBUF_SIZE (256 bytes), so this
# is at least 3 reads.
data = bytearray(random.randint(0, 255) for i in range(608))
sock.send(data)
def data_line(i):
return u' '.join(u'%.2X' % data[i + j] for j in range(16))
# We expect three consecutive reads of 256 byte buffer to print the
# data at positions 0, 256 and 512.
server.expect(u"00000000 %s" % data_line(0))
server.expect(u"00000000 %s" % data_line(256))
server.expect(u"00000000 %s" % data_line(512))
# Last line of the message should be on the third read call,
# together with the rest of the third read.
server.expect(u"00000050 %s" % data_line(len(data) - 16))
sock.close()
server.expect(u"TCP connection to \\[[0-9a-f:]+\\]:[0-9]+ reset")
def test_tcpv6_multiconnect(board_group, application, env=None):
if any(b.name != "native" for b in board_group.boards):
# run test only with native
@ -366,4 +415,5 @@ if __name__ == "__main__":
TestStrategy().execute([BoardGroup((Board("native", "tap0"),
Board("native", "tap1")))],
[test_ipv6_send, test_udpv6_send, test_tcpv6_send,
test_tcpv6_multiconnect, test_triple_send])
test_tcpv6_large_send, test_tcpv6_multiconnect,
test_triple_send])