def testfunc(child):
    port = generate_port_number()
    shutdown_event = threading.Event()

    server_handle = threading.Thread(target=tcp_server,
                                     args=(port, shutdown_event))
    server_handle.start()

    target_addr = get_host_ll_addr(
        get_host_tap_device()) + '%' + get_riot_if_id(child)

    # Setup RIOT Node to connect to host systems TCP Server
    child.sendline('gnrc_tcp_tcb_init')
    child.sendline('gnrc_tcp_open_active AF_INET6 ' + target_addr + ' ' +
                   str(port) + ' 0')
    child.expect_exact('gnrc_tcp_open_active: returns 0')

    # Close connection and verify that pktbuf is cleared
    shutdown_event.set()
    child.sendline('gnrc_tcp_close')
    server_handle.join()

    verify_pktbuf_empty(child)

    print(os.path.basename(sys.argv[0]) + ': success')


if __name__ == '__main__':
    sudo_guard()
    sys.exit(run(testfunc, timeout=5, echo=False, traceback=True))
Ejemplo n.º 2
0
    tcp_hdr = TCP(dport=dst_port, flags="S", sport=2342, seq=1, dataofs=6)
    sendp(Ether(dst=dst_l2) / IPv6(src=src_ll, dst=dst_ll) / tcp_hdr /
          b"\x50\x00\x00\x00",
          iface=src_if,
          verbose=0)

    # check if server actually still works
    with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as sock:
        sock.settimeout(child.timeout)
        addr_info = socket.getaddrinfo(dst_ll + '%' + src_if,
                                       dst_port,
                                       type=socket.SOCK_STREAM)
        sock.connect(addr_info[0][-1])
        child.expect_exact('gnrc_tcp_open_passive: returns 0')
    verify_pktbuf_empty(child)


if __name__ == "__main__":
    sudo_guard(uses_scapy=True)
    script = sys.modules[__name__]
    tests = [
        getattr(script, t) for t in script.__dict__
        if type(getattr(script, t)).__name__ == "function"
        and t.startswith("test_")
    ]
    for test in tests:
        res = run(test, timeout=10, echo=False)
        if res != 0:
            sys.exit(res)
    print(os.path.basename(sys.argv[0]) + ": success\n")