def test_syn(): """When we connect, we should send a SYN.""" listener = MockListener() conn = TCPSocket(listener) conn.connect("localhost", 80) assert conn.state == "SYN-SENT" pkts = listener.received_packets assert len(pkts) == 1 assert pkts[0].sprintf("%TCP.flags%") == "S"
def create_session(packet_log): """ Helper function to create a client session from a PCAP log. Sets up the sequence numbers to match with the sequence numbers in the PCAP. """ listener = MockListener() syn = packet_log[0] listener.source_port = syn.sport - 1 conn = TCPSocket(listener) conn.connect(syn.payload.dst, syn.dport) # Change the sequence number so that we can test it conn.seq = syn.seq return listener, conn
def test_tiny_session_server(): """ Test the whole tiny session, from a server POV. """ packet_log = rdpcap("test/inputs/tiniest-session.pcap") syn, syn_ack = packet_log[:2] listener = MockListener() conn = TCPSocket(listener) conn.seq = syn_ack.seq conn.bind(syn.payload.dst, syn.dport) check_replay(listener, conn, packet_log)
def get_page(url, fake_ip): hostname, path = parse(url) request = "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n" % (path, hostname) listener = TCPListener(fake_ip) conn = TCPSocket(listener) conn.connect(hostname, 80) conn.send(request) data = conn.recv(10000, timeout=1) conn.close() return data
def test_bind_handshake(): """ We can do a handshake when we're the server (someone else initiates the SYN). """ packet_log = rdpcap("test/inputs/tiniest-session.pcap") syn, syn_ack, ack, client_fin_ack, server_fin_ack, client_ack = packet_log listener = MockListener() conn = TCPSocket(listener) conn.seq = syn_ack.seq conn.bind(syn.payload.dst, syn.dport) listener.dispatch(syn) listener.dispatch(ack) assert len(listener.received_packets) == 1 assert conn.state == "ESTABLISHED" check_mostly_same(listener.received_packets[0], syn_ack)
def test_has_load(): pkt = Ether() / IP() / TCP() / Padding("\x00\x00") assert not TCPSocket._has_load(pkt)