コード例 #1
0
    def test_get_peers_Received_sendsValidResponseWithPeers(self):
        # Create the protocol and populate its
        # routing table with nodes
        kresponder = self._patched_responder()
        peers = [("127.0.0.%d" % peer_num, peer_num) for
                                        peer_num in range(10)]
        incoming_query = Query()
        incoming_query.rpctype = "get_peers"
        incoming_query._from = 555
        incoming_query._transaction_id = 15
        # We have this target id in our routing table
        incoming_query.target_id = 77

        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = 15
        expected_response.peers = peers
        expected_response.rpctype = "get_peers"

        for peer in peers:
            kresponder._datastore.put(incoming_query.target_id, peer)
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        # Grab the autogenerated token and sort the peers to
        # match our expected order
        expected_response.token = actual_response.token
        actual_response.peers.sort(key = lambda (ip, port) : port)
        self.assertEquals(expected_response, actual_response)
コード例 #2
0
    def test_get_peers_Received_sendsValidResponseWithPeers(self):
        # Create the protocol and populate its
        # routing table with nodes
        kresponder = self._patched_responder()
        peers = [("127.0.0.%d" % peer_num, peer_num) for peer_num in range(10)]
        incoming_query = Query()
        incoming_query.rpctype = "get_peers"
        incoming_query._from = 555
        incoming_query._transaction_id = 15
        # We have this target id in our routing table
        incoming_query.target_id = 77

        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = 15
        expected_response.peers = peers
        expected_response.rpctype = "get_peers"

        for peer in peers:
            kresponder._datastore.put(incoming_query.target_id, peer)
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        # Grab the autogenerated token and sort the peers to
        # match our expected order
        expected_response.token = actual_response.token
        actual_response.peers.sort(key=lambda (ip, port): port)
        self.assertEquals(expected_response, actual_response)
コード例 #3
0
ファイル: test_krpc_coder.py プロジェクト: aburan28/DHTBot
 def test_encode_and_decode_validGetPeersResponseWithPeers(self):
     r = Response()
     r._transaction_id = 1903890316316
     r._from = 169031860931900138093217073128059
     r.token = 90831
     r.peers = [("127.0.0.1", 80), ("4.2.2.1", 8905), ("0.0.0.0", 0),
                 ("8.8.8.8", 53), ("255.255.255.255", 65535)]
     processed_response = encode_and_decode(r)
     self.assertEquals(r._transaction_id, processed_response._transaction_id)
     self.assertEquals(r._from, processed_response._from)
     self.assertEquals(r.token, processed_response.token)
     self.assertEquals(r.peers, processed_response.peers)
コード例 #4
0
 def test_encode_and_decode_validGetPeersResponseWithPeers(self):
     r = Response()
     r._transaction_id = 1903890316316
     r._from = 169031860931900138093217073128059
     r.token = 90831
     r.peers = [("127.0.0.1", 80), ("4.2.2.1", 8905), ("0.0.0.0", 0),
                ("8.8.8.8", 53), ("255.255.255.255", 65535)]
     processed_response = encode_and_decode(r)
     self.assertEquals(r._transaction_id,
                       processed_response._transaction_id)
     self.assertEquals(r._from, processed_response._from)
     self.assertEquals(r.token, processed_response.token)
     self.assertEquals(r.peers, processed_response.peers)
コード例 #5
0
 def test_encode_validGetPeersResponseWithPeers(self):
     r = Response()
     r._transaction_id = 1903890316316
     r._from = 169031860931900138093217073128059
     r.token = 90831
     r.peers = [("127.0.0.1", 80), ("4.2.2.1", 8905), ("0.0.0.0", 0),
                ("255.255.255.255", 65535), ("8.8.8.8", 53)]
     expected_encoding = (
         'd1:rd2:id20:\x00\x00\x00\x00\x00\x00\x08U{fDA\xb0\x88\xe6' +
         '\x8a\xec\xf8\xe2{5:token3:\x01b\xcf6:values30:\x7f\x00\x00' +
         '\x01\x00P\x04\x02\x02\x01"\xc9\x00\x00\x00\x00\x00\x00\xff' +
         '\xff\xff\xff\xff\xff\x08\x08\x08\x08\x005e1:t6:\x01\xbbH' +
         '\xb4\xbc\x1c1:y1:re')
     encoding = encode(r)
     self.assertEquals(expected_encoding, encoding)
コード例 #6
0
ファイル: test_krpc_coder.py プロジェクト: aburan28/DHTBot
 def test_encode_validGetPeersResponseWithPeers(self):
     r = Response()
     r._transaction_id = 1903890316316
     r._from = 169031860931900138093217073128059
     r.token = 90831
     r.peers = [("127.0.0.1", 80), ("4.2.2.1", 8905), ("0.0.0.0", 0),
                 ("255.255.255.255", 65535), ("8.8.8.8", 53)]
     expected_encoding = (
             'd1:rd2:id20:\x00\x00\x00\x00\x00\x00\x08U{fDA\xb0\x88\xe6' +
             '\x8a\xec\xf8\xe2{5:token3:\x01b\xcf6:values30:\x7f\x00\x00' +
             '\x01\x00P\x04\x02\x02\x01"\xc9\x00\x00\x00\x00\x00\x00\xff' +
             '\xff\xff\xff\xff\xff\x08\x08\x08\x08\x005e1:t6:\x01\xbbH' + 
             '\xb4\xbc\x1c1:y1:re')
     encoding = encode(r)
     self.assertEquals(expected_encoding, encoding)
コード例 #7
0
def _response_decoder(rpc_dict):
    """
    Decode the given KRPC dictionary into a valid Response

    @see decode
    @return krpc_types.Response

    """
    r = Response()
    # All responses have querier IDs
    r._from = basic_coder.decode_network_id(rpc_dict['r']['id'])
    # find_node always returns a list of nodes
    # get_peers sometimes returns a list of nodes
    if 'nodes' in rpc_dict['r']:
        r.nodes = _decode_nodes(rpc_dict['r']['nodes'])
    # get_peers always returns a list of peers
    if 'values' in rpc_dict['r']:
        r.peers = _decode_addresses(rpc_dict['r']['values'])
    # get_peers returns a token
    if 'token' in rpc_dict['r']:
        r.token = basic_coder.btol(rpc_dict['r']['token'])
    return r
コード例 #8
0
ファイル: krpc_coder.py プロジェクト: aburan28/DHTBot
def _response_decoder(rpc_dict):
    """
    Decode the given KRPC dictionary into a valid Response

    @see decode
    @return krpc_types.Response

    """
    r = Response()
    # All responses have querier IDs
    r._from = basic_coder.decode_network_id(rpc_dict['r']['id'])
    # find_node always returns a list of nodes
    # get_peers sometimes returns a list of nodes
    if 'nodes' in rpc_dict['r']:
        r.nodes = _decode_nodes(rpc_dict['r']['nodes'])
    # get_peers always returns a list of peers
    if 'values' in rpc_dict['r']:
        r.peers = _decode_addresses(rpc_dict['r']['values'])
    # get_peers returns a token
    if 'token' in rpc_dict['r']:
        r.token = basic_coder.btol(rpc_dict['r']['token'])
    return r