def test_should_set_src_and_dst_port_in_message_info_when_parse_method_is_called( self): # GIVEN message_info = any_message_info() src_port = any_port() dst_port = any_port() checksum = any_checksum() payload = any_payload() payload_length = len(payload) + len(UDPHeader(0, 0)) data = (bytearray([ (src_port >> 8), (src_port & 0xff), (dst_port >> 8), (dst_port & 0xff), (payload_length >> 8), (payload_length & 0xff), (checksum >> 8), (checksum & 0xff), ]) + payload) factory = UDPDatagramFactory(UDPHeaderFactory(), BytesPayloadFactory()) # WHEN factory.parse(io.BytesIO(data), message_info) # THEN self.assertEqual(src_port, message_info.src_port) self.assertEqual(dst_port, message_info.dst_port)
def test_should_produce_UDPDatagram_from_bytes_when_to_bytes_method_is_called_with_data(self): # GIVEN src_port = any_port() dst_port = any_port() checksum = any_checksum() payload = any_payload() payload_length = len(payload) + len(UDPHeader(0, 0)) data = bytearray([(src_port >> 8), (src_port & 0xFF), (dst_port >> 8), (dst_port & 0xFF), (payload_length >> 8), (payload_length & 0xFF), (checksum >> 8), (checksum & 0xFF)]) + payload factory = UDPDatagramFactory(UDPHeaderFactory(), BytesPayloadFactory()) # WHEN udp_dgram = factory.parse(io.BytesIO(data), any_message_info()) # THEN self.assertEqual(udp_dgram.header.src_port, src_port) self.assertEqual(udp_dgram.header.dst_port, dst_port) self.assertEqual(udp_dgram.header.payload_length, payload_length) self.assertEqual(udp_dgram.header.checksum, checksum) self.assertEqual(udp_dgram.payload.data, payload)
def test_should_produce_UDPDatagram_from_bytes_when_to_bytes_method_is_called_with_data( self): # GIVEN src_port = any_port() dst_port = any_port() checksum = any_checksum() payload = any_payload() payload_length = len(payload) + len(UDPHeader(0, 0)) data = bytearray([(src_port >> 8), (src_port & 0xFF), (dst_port >> 8), (dst_port & 0xFF), (payload_length >> 8), (payload_length & 0xFF), (checksum >> 8), (checksum & 0xFF)]) + payload factory = UDPDatagramFactory(UDPHeaderFactory(), BytesPayloadFactory()) # WHEN udp_dgram = factory.parse(io.BytesIO(data), any_message_info()) # THEN self.assertEqual(udp_dgram.header.src_port, src_port) self.assertEqual(udp_dgram.header.dst_port, dst_port) self.assertEqual(udp_dgram.header.payload_length, payload_length) self.assertEqual(udp_dgram.header.checksum, checksum) self.assertEqual(udp_dgram.payload.data, payload)
def test_should_set_src_and_dst_port_in_message_info_when_parse_method_is_called(self): # GIVEN message_info = any_message_info() src_port = any_port() dst_port = any_port() checksum = any_checksum() payload = any_payload() payload_length = len(payload) + len(UDPHeader(0, 0)) data = bytearray([(src_port >> 8), (src_port & 0xFF), (dst_port >> 8), (dst_port & 0xFF), (payload_length >> 8), (payload_length & 0xFF), (checksum >> 8), (checksum & 0xFF)]) + payload factory = UDPDatagramFactory(UDPHeaderFactory(), BytesPayloadFactory()) # WHEN udp_dgram = factory.parse(io.BytesIO(data), message_info) # THEN self.assertEqual(src_port, message_info.src_port) self.assertEqual(dst_port, message_info.dst_port)