def datagramReceived(self, data, address): """ This method is called by twisted when a datagram is received This implementation tries to decode the datagram. If it succeeds, it is passed onto self.krpcReceived for further processing, otherwise the encoding exception is captured and logged @see krpcReceived """ try: krpc = krpc_coder.decode(data) except InvalidKRPCError: log.msg("Malformed packet received from %s:%d" % address) return self.krpcReceived(krpc, address)
from twisted.trial import unittest from dhtbot.coding.krpc_coder import (encode, decode, _chunkify, _decode_addresses, InvalidKRPCError) from dhtbot.coding import basic_coder from dhtbot.krpc_types import Query, Response, Error from dhtbot.contact import Node encode_and_decode = lambda krpc: decode(encode(krpc)) class KRPCHelperFunctionTestCase(unittest.TestCase): def test_chunkify_nodestrings(self): # Node strings are of length 26 node_string = "01" * 13 node_strings = node_string * 10 # _chunkify returns a generator split_node_strings = _chunkify(node_strings, 26) self.assertEquals(10, len(list(split_node_strings))) def test_chunkify_peerstrings(self): # Addresses are of length 6 address_string = "012345" address_strings = address_string * 10 # _chunkify returns a generator split_address_strings = _chunkify(address_strings, 6) self.assertEquals(10, len(list(split_address_strings))) def test_decode_addresses(self): address_string = "".join( ["\x00\x00\x00\x00\x00\x00", "\xff\xff\xff\xff\xff\xff"])
from twisted.trial import unittest from dhtbot.coding.krpc_coder import ( encode, decode, _chunkify, _decode_addresses, InvalidKRPCError) from dhtbot.coding import basic_coder from dhtbot.krpc_types import Query, Response, Error from dhtbot.contact import Node encode_and_decode = lambda krpc: decode(encode(krpc)) class KRPCHelperFunctionTestCase(unittest.TestCase): def test_chunkify_nodestrings(self): # Node strings are of length 26 node_string = "01" * 13 node_strings = node_string * 10 # _chunkify returns a generator split_node_strings = _chunkify(node_strings, 26) self.assertEquals(10, len(list(split_node_strings))) def test_chunkify_peerstrings(self): # Addresses are of length 6 address_string = "012345" address_strings = address_string * 10 # _chunkify returns a generator split_address_strings = _chunkify(address_strings, 6) self.assertEquals(10, len(list(split_address_strings))) def test_decode_addresses(self): address_string = "".join(["\x00\x00\x00\x00\x00\x00", "\xff\xff\xff\xff\xff\xff"])