def check_mostly_same(pkt1, pkt2): """ Checks that the important things are the same (seq, ack, TCP flags, packet contents) """ assert pkt1.seq == pkt2.seq assert pkt1.ack == pkt2.ack assert pkt1.sprintf("%TCP.flags%") == pkt2.sprintf("%TCP.flags%") assert get_payload(pkt1) == get_payload(pkt2)
def test_recv_many_packets_out_of_order(): """ We should be able to put multiple packets together, if they come out of order and are repeated. """ packet_log = rdpcap("test/inputs/wget-36000-nums.pcap") listener, conn = create_session(packet_log) _, syn_ack, _, push_ack = packet_log[:4] listener.dispatch(syn_ack) payload = get_payload(push_ack) conn.send(payload) p1, p2, p3 = packet_log[5], packet_log[7], packet_log[8] # Send the packets out of order and repeated listener.dispatch(p2) listener.dispatch(p3) listener.dispatch(p2) listener.dispatch(p1) # Right listener.dispatch(p3) listener.dispatch(p2) # Right listener.dispatch(p3) # Right # Check that the contents of the packet is right # This is a good test because one of the packets starts with a 6 or # something conn.state = "CLOSED" recv = conn.recv(38000) assert recv[-36001:-1] == "1234567890" * 3600
def test_send_push_ack(): """ When we send a message, it should look the same as in the PCAP log """ packet_log = rdpcap("test/inputs/localhost-wget.pcap") listener, conn = create_session(packet_log) _, syn_ack, _, push_ack = packet_log[:4] listener.dispatch(syn_ack) assert conn.state == "ESTABLISHED" # Extract the payload (3 levels down: Ether, IP, TCP) payload = get_payload(push_ack) conn.send(payload) # Check to make sure the PUSH-ACK packet that gets sent looks good our_push_ack = listener.received_packets[-1] check_mostly_same(our_push_ack, push_ack)
def test_recv_many_packets_in_order(): """ We should be able to put multiple packets together, if they come in order """ packet_log = rdpcap("test/inputs/wget-36000-nums.pcap") listener, conn = create_session(packet_log) _, syn_ack, _, push_ack = packet_log[:4] listener.dispatch(syn_ack) payload = get_payload(push_ack) conn.send(payload) check_replay(listener, conn, packet_log[4:], check=False) # Check that the contents of the packet is right conn.state = "CLOSED" recv = conn.recv(40000) assert recv[-36001:-1] == "1234567890" * 3600
def test_recv_one_packet(): """ conn.recv() should get the contents of one packet """ packet_log = rdpcap("test/inputs/localhost-wget.pcap") listener, conn = create_session(packet_log) _, syn_ack, _, push_ack = packet_log[:4] listener.dispatch(syn_ack) payload = get_payload(push_ack) conn.send(payload) # Check that the PUSH/ACK sequence is the same check_replay(listener, conn, packet_log[4:7]) # Check that recv() actually works conn.state = "CLOSED" assert conn.recv(1000) == str(packet_log[5].payload.payload.payload)