def test_serialize_option(self): # prepare test data offset = 0 csum = 0 option = [ tcp.TCPOptionMaximumSegmentSize(max_seg_size=1460), tcp.TCPOptionSACKPermitted(), tcp.TCPOptionTimestamps(ts_val=287454020, ts_ecr=1432778632), tcp.TCPOptionNoOperation(), tcp.TCPOptionWindowScale(shift_cnt=9), ] option_buf = (b'\x02\x04\x05\xb4' b'\x04\x02' b'\x08\x0a\x11\x22\x33\x44\x55\x66\x77\x88' b'\x01' b'\x03\x03\x09') prev = ipv4(4, 5, 0, 0, 0, 0, 0, 64, inet.IPPROTO_TCP, 0, '192.168.10.1', '192.168.100.1') # test serializer t = tcp.tcp(self.src_port, self.dst_port, self.seq, self.ack, offset, self.bits, self.window_size, csum, self.urgent, option) buf = t.serialize(bytearray(), prev) r_option_buf = buf[tcp.tcp._MIN_LEN:tcp.tcp._MIN_LEN + len(option_buf)] eq_(option_buf, r_option_buf) # test parser (r_tcp, _, _) = tcp.tcp.parser(buf) eq_(str(option), str(r_tcp.option))
class Test_TCPOption(unittest.TestCase): # prepare test data input_options = [ tcp.TCPOptionEndOfOptionList(), tcp.TCPOptionNoOperation(), tcp.TCPOptionMaximumSegmentSize(max_seg_size=1460), tcp.TCPOptionWindowScale(shift_cnt=9), tcp.TCPOptionSACKPermitted(), tcp.TCPOptionSACK(blocks=[(1, 2), (3, 4)], length=18), tcp.TCPOptionTimestamps(ts_val=287454020, ts_ecr=1432778632), tcp.TCPOptionUserTimeout(granularity=1, user_timeout=564), tcp.TCPOptionAuthentication(key_id=1, r_next_key_id=2, mac=b'abcdefghijkl', length=16), tcp.TCPOptionUnknown(value=b'foobar', kind=255, length=8), tcp.TCPOptionUnknown(value=b'', kind=255, length=2), ] input_buf = ( b'\x00' # End of Option List b'\x01' # No-Operation b'\x02\x04\x05\xb4' # Maximum Segment Size b'\x03\x03\x09' # Window Scale b'\x04\x02' # SACK Permitted b'\x05\x12' # SACK b'\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04' b'\x08\x0a' # Timestamps b'\x11\x22\x33\x44\x55\x66\x77\x88' b'\x1c\x04\x82\x34' # User Timeout Option b'\x1d\x10\x01\x02' # TCP Authentication Option (TCP-AO) b'abcdefghijkl' b'\xff\x08' # Unknown with body b'foobar' b'\xff\x02' # Unknown ) def test_serialize(self): output_buf = bytearray() for option in self.input_options: output_buf += option.serialize() eq_(self.input_buf, output_buf) def test_parser(self): buf = self.input_buf output_options = [] while buf: opt, buf = tcp.TCPOption.parser(buf) output_options.append(opt) eq_(str(self.input_options), str(output_options)) def test_json(self): for option in self.input_options: json_dict = option.to_jsondict()[option.__class__.__name__] output_option = option.__class__.from_jsondict(json_dict) eq_(str(option), str(output_option))
def send_tcp_pke(self, datapath, port, vlan_id, dst_ip, src_ip, src_mac, dst_mac, dport, sport=61300): seq = 0 ack = 0 offset = 6 window_size = 8192 urgent = 0 option = [tcp.TCPOptionWindowScale(shift_cnt=9), tcp.TCPOptionSACKPermitted(length=2), tcp.TCPOptionTimestamps(ts_val=287454020, ts_ecr=1432778632)] if vlan_id != 0: ether_proto = ether.ETH_TYPE_8021Q vlan_ether = ether.ETH_TYPE_IP v = vlan.vlan(0, 0, vlan_id, vlan_ether) offset += vlan.vlan._MIN_LEN else: ether_proto = ether.ETH_TYPE_IP pkt = packet.Packet() # Add ethernet protocol with ether type IP protocol and mac addresses pkt.add_protocol(ethernet.ethernet(ethertype=ether_proto, dst=dst_mac, src=src_mac)) # Add ipv4 protocol with IP addresses and TCP protocol which is 6 ipv4_pkt = ipv4.ipv4(dst=dst_ip, src=src_ip, proto=6, flags=2) pkt.add_protocol(ipv4_pkt) # Add tcp protocol with port numbers and sequence number tcp_pkt = tcp.tcp(src_port=sport, dst_port=dport, seq=seq, ack=ack, offset=offset, bits=tcp.TCP_SYN, window_size=window_size, urgent=urgent, option=option) tcp_pkt.has_flags(tcp.TCP_SYN) pkt.add_protocol(tcp_pkt) if vlan_id != 0: pkt.add_protocol(v) self._send_packet(datapath, pkt, port)