def test_gnrc_tcp_accept_returns_ETIMEDOUT(child): """ gnrc_tcp_accept must return with -ETIMEDOUT if no connection is ready and timeout is not 0 """ riot_srv = RiotTcpServer(child, generate_port_number()) riot_srv.listen() child.sendline('gnrc_tcp_accept 1000') child.expect_exact('gnrc_tcp_accept: returns -ETIMEDOUT')
def test_gnrc_tcp_garbage_packets_short_payload(child): """ Receive unusually short payload with timeout. Verifies fix for https://github.com/RIOT-OS/RIOT/issues/11999 """ # Setup RIOT as server with RiotTcpServer(child, generate_port_number()) as riot_srv: # Setup Host as client with HostTcpClient(riot_srv) as host_cli: # Accept new connection riot_srv.accept(timeout_ms=2000) # Receive 1 byte with timeout, this should block child.sendline('gnrc_tcp_recv 1000000 1') child.expect_exact('gnrc_tcp_recv: argc=3, ' 'argv[0] = gnrc_tcp_recv, ' 'argv[1] = 1000000, argv[2] = 1') # Send 1 byte from host to RIOT assert 1 == host_cli.sock.send(b"f") # Receive 1 byte child.expect_exact('gnrc_tcp_recv: received 1', timeout=20) # Close connection riot_srv.close()
def test_gnrc_tcp_garbage_packets_option_parsing(child): """ This test verfies that malformed option don't break TCP doesn't break GNRC_TCP. See: https://github.com/RIOT-OS/RIOT/issues/12086 """ # Setup RIOT as server with RiotTcpServer(child, generate_port_number()) as riot_srv: # Construct HostTcpClient to lookup node properties host_cli = HostTcpClient(riot_srv) # Try to accept incoming connection from host system. child.sendline('gnrc_tcp_accept 2000') tcp_hdr = TCP(dport=int(riot_srv.listen_port), flags="S", sport=2342, seq=1, dataofs=6) sendp(Ether(dst=riot_srv.mac) / IPv6(src=host_cli.address, dst=riot_srv.address) / tcp_hdr / b"\x50\x00\x00\x00", iface=host_cli.interface, verbose=0) # check if server actually still works with host_cli: child.expect_exact('gnrc_tcp_accept: returns 0') riot_srv.close()
def test_gnrc_tcp_garbage_packets_short_header(child): """ This test verifies fix malformed tcp header. See https://github.com/RIOT-OS/RIOT/issues/12086 """ # Setup RIOT as server with RiotTcpServer(child, generate_port_number()) as riot_srv: # Construct HostTcpClient to lookup node properties host_cli = HostTcpClient(riot_srv) # Try to accept incoming connection from host system. child.sendline('gnrc_tcp_accept 2000') # Build malformed short header with SYN Flag tcp_hdr = TCP(dport=int(riot_srv.listen_port), flags="S", sport=2342, seq=1, dataofs=6) tcp_hdr = raw(tcp_hdr)[:-2] sendp(Ether(dst=riot_srv.mac) / IPv6(src=host_cli.address, dst=riot_srv.address) / tcp_hdr, iface=host_cli.interface, verbose=0) # Establish normal tcp connection from host system to check if RIOT node # was able to recover from malformed packet with host_cli: child.expect_exact('gnrc_tcp_accept: returns 0') riot_srv.close()
def test_connection_lifecycle_as_server(child): """ Open/close a single connection as tcp server """ # Setup RIOT as server with RiotTcpServer(child, generate_port_number()) as riot_srv: # Setup Host as client with HostTcpClient(riot_srv): # Accept and close connection riot_srv.accept(timeout_ms=1000) riot_srv.close()
def test_gnrc_tcp_accept_returns_ENOMEM(child): """ gnrc_tcp_accept must return with -ENOMEM if all TCBs already handle a connection """ with RiotTcpServer(child, generate_port_number()) as riot_srv: # Establish connection to ensure that all TCBs are in use with HostTcpClient(riot_srv): riot_srv.accept(timeout_ms=0) # Out of memory accept should return immediately despite a huge timeout. child.sendline('gnrc_tcp_accept 100000000') child.expect_exact('gnrc_tcp_accept: returns -ENOMEM')
def test_gnrc_tcp_accept_respects_GNRC_TCP_NO_TIMEOUT(child): """ gnrc_tcp_accept timeout mechanism must be disabled if GNRC_TCP_NO_TIMEOUT is set as timeout value. """ pexpect_timeout_sec = _GNRC_TCP_NO_TIMEOUT + 1 riot_srv = RiotTcpServer(child, generate_port_number()) riot_srv.listen() child.sendline('gnrc_tcp_accept {}'.format(_GNRC_TCP_NO_TIMEOUT)) # The test is successful if gnrc_tcp_accept does not return before pexpects timeout. # Afterwards the connection must be still able to accept a connection. returned = True try: child.expect_exact('gnrc_tcp_accept: returns', timeout=pexpect_timeout_sec) except pexpect.TIMEOUT: returned = False finally: if returned: raise RuntimeError('gnrc_tcp_accept returned') # Ensure that gnrc_tcp_accept returns after the test host connects with HostTcpClient(riot_srv): child.expect_exact('gnrc_tcp_accept: returns 0') riot_srv.close()
def test_send_data_from_host_to_riot(child): """ Send Data from Host system to RIOT node """ # Setup RIOT as server with RiotTcpServer(child, generate_port_number()) as riot_srv: # Setup Host as client with HostTcpClient(riot_srv) as host_cli: # Accept and close connection riot_srv.accept(timeout_ms=1000) # Send Data from Host system to RIOT data = '0123456789' * 200 host_cli.send(data) riot_srv.receive(timeout_ms=1000, sent_payload=data) riot_srv.close()
def test_connection_listen_accept_cycle(child, iterations=10): """ This test verifies gnrc_tcp in a typical server role by accepting a connection, exchange data, teardown connection, handle the next one """ # Setup RIOT Node as server with RiotTcpServer(child, generate_port_number()) as riot_srv: # Establish multiple connections iterativly for i in range(iterations): print('\n Running listen/accept iteration {}'.format(i), end='') with HostTcpClient(riot_srv) as host_cli: # Accept connection from host system riot_srv.accept(timeout_ms=0) # Send data from host to RIOT data = '0123456789' host_cli.send(payload_to_send=data) riot_srv.receive(timeout_ms=500, sent_payload=data) # Send data from RIOT to host riot_srv.send(timeout_ms=500, payload_to_send=data) host_cli.receive(sent_payload=data) # Randomize connection teardown: The connections # can't be either closed or aborted from either # side. Regardless the type of the connection teardown # riot_srv must be able to accept the next connection # Note: python sockets don't offer abort... dice_throw = random.randint(0, 3) if dice_throw == 0: riot_srv.close() host_cli.close() elif dice_throw == 1: riot_srv.abort() host_cli.close() elif dice_throw == 2: host_cli.close() riot_srv.close() elif dice_throw == 3: host_cli.close() riot_srv.abort()
def test_gnrc_tcp_garbage_packets_ack_instead_of_sym(child): """ This test verfies that sending and ACK instead of a SYN. doesn't break GNRC_TCP. """ # Setup RIOT as server with RiotTcpServer(child, generate_port_number()) as riot_srv: # Construct HostTcpClient to lookup node properties host_cli = HostTcpClient(riot_srv) # Try to accept incoming connection from host system. # Use timeout of 15s discarding 1000 packages can take a while on smaller platforms child.sendline('gnrc_tcp_accept 15000') # Check if debug output is enabled. Send fewer packets on if it is disabled # To ensure that the amount of generated output doesn't break the test debug = child.expect( [pexpect.TIMEOUT, r'GNRC_TCP: Enter "\S+", File: .+\(\d+\)\s'], timeout=1) if debug: count = 10 else: count = 1000 # see https://github.com/RIOT-OS/RIOT/pull/12001 provided_data = base64.b64decode("rwsQf2pekYLaU+exUBBwgPDKAAA=") tcp_hdr = TCP(provided_data) assert provided_data == raw(tcp_hdr) # set destination port to application specific port tcp_hdr.dport = int(riot_srv.listen_port) sendp(Ether(dst=riot_srv.mac) / IPv6(src=host_cli.address, dst=riot_srv.address) / tcp_hdr, iface=host_cli.interface, verbose=0, count=count) # check if server actually still works with host_cli: child.expect_exact('gnrc_tcp_accept: returns 0') riot_srv.close()
def test_gnrc_tcp_queue_get_local_returns_0(child): """ This test verifies that queue_get_local returns 0 then put after listen. And the endpoint content is as are as expected. """ # Enter listen with accept all address listen_addr = '::' listen_port = generate_port_number() riot_srv = RiotTcpServer(child, listen_port) riot_srv.listen() riot_srv.queue_get_local() child.expect_exact( 'Endpoint: addr.ipv6=:: netif=0 port={}'.format(listen_port)) riot_srv.stop_listen() # Enter listen with specified address listen_addr = 'fe80::4c49:c7ff:fecd:34a3' listen_port = generate_port_number() riot_srv = RiotTcpServer(child, listen_port, listen_addr) riot_srv.listen() riot_srv.queue_get_local() child.expect_exact('Endpoint: addr.ipv6={} netif=0 port={}'.format( listen_addr, listen_port)) riot_srv.stop_listen()