Esempio n. 1
0
 def _grab_outbound_get_peers(self):
     encoded_query = self.ksimple.transport.packet
     self.ksimple.transport._reset()
     self.assertNotEquals(None, encoded_query)
     query = krpc_coder.decode(encoded_query)
     self.assertEquals("get_peers", query.rpctype)
     return query 
Esempio n. 2
0
    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, logLevel=logging.WARNING)
            return
        self.krpcReceived(krpc, address)
Esempio n. 3
0
    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("{0}:{1} sent a malformed packet".format(
                address[0], address[1]))
            return
        self.krpcReceived(krpc, address)
Esempio n. 4
0
    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("{0}:{1} sent a malformed packet"
                .format(address[0], address[1]))
            return
        self.krpcReceived(krpc, address)
Esempio n. 5
0
from twisted.trial import unittest

from mdht.coding.krpc_coder import (encode, decode, _chunkify,
                                    _decode_addresses, InvalidKRPCError)
from mdht.coding import basic_coder
from mdht.krpc_types import Query, Response, Error
from mdht.contact import Node

encode_and_decode = lambda krpc: decode(encode(krpc))


class QueryCodingTestCase(unittest.TestCase):

    test_target_id = 551232
    test_port = 511
    test_token = 5555

    def setUp(self):
        q = self.q = Query()
        q._transaction_id = 15
        q._from = 2**120

    def test_encode_validPing(self):
        q = self.q
        q.rpctype = "ping"
        encoding = encode(q)
        expected_encoding = (
            'd1:ad2:id20:\x00\x00\x00\x00\x01\x00\x00' +
            '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00e1:q4:' +
            'ping1:t1:\x0f1:y1:qe')
        self.assertEquals(expected_encoding, encoding)
Esempio n. 6
0
from twisted.trial import unittest

from mdht.coding.krpc_coder import (
        encode, decode, _chunkify, _decode_addresses,
        InvalidKRPCError)
from mdht.coding import basic_coder
from mdht.krpc_types import Query, Response, Error
from mdht.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"])