From 9974803f431fa20def40a78e241e4736dc00773d Mon Sep 17 00:00:00 2001 From: iosabi Date: Sat, 10 Apr 2021 18:56:12 +0200 Subject: [PATCH] tests/lwip: Add a test for partial TCP reads. Add a new test to check the behavior of `sock_tcp_read` when more data is available in the connection than the buffer passed. This test checks issue #16124 as well as reading from multiple small network packets into a single buffer. --- tests/lwip/tests/01-run.py | 52 +++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/tests/lwip/tests/01-run.py b/tests/lwip/tests/01-run.py index 0f96dc6593..e1375f7b66 100755 --- a/tests/lwip/tests/01-run.py +++ b/tests/lwip/tests/01-run.py @@ -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])