コード例 #1
0
    def test_find_node_Received_sendsValidResponseMultipleNodes(self):
        # Create the protocol and populate its
        # routing table with nodes
        kresponder = Patched_KRPC_Responder()
        node_list = []
        node_gen = lambda num: contact.Node(num, ("127.0.0.%d" % num, num))
        for i in range(100):
            n = node_gen(i)
            if kresponder.routing_table.offer_node(n):
                node_list.append(n)

        querying_node = contact.Node(123, test_address)
        incoming_query = Query()
        incoming_query.rpctype = "find_node"
        incoming_query._from = querying_node.node_id
        incoming_query._transaction_id = 15
        incoming_query.target_id = 777777

        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = 15
        expected_response.rpctype = "find_node"
        node_list.sort(
            key=lambda node: node.distance(incoming_query.target_id))
        node_list = node_list[:constants.k]
        expected_response.nodes = node_list

        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)
コード例 #2
0
 def test_encode_validGetPeersResponseWithNodes(self):
     r = Response()
     r._transaction_id = 1903890316316
     r._from = 169031860931900138093217073128059
     r.token = 90831
     r.nodes = [
         Node(2**158, ("127.0.0.1", 890)),
         Node(2**15, ("127.0.0.1", 8890)),
         Node(2**128, ("127.0.0.1", 1890)),
         Node(2**59, ("127.0.0.1", 7890)),
         Node(2**153, ("127.0.0.1", 5830))
     ]
     expected_encoding = (
         'd1:rd2:id20:\x00\x00\x00\x00\x00\x00\x08' +
         'U{fDA\xb0\x88\xe6\x8a\xec\xf8\xe2{5:nodes130:@\x00\x00\x00' +
         '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
         '\x00\x00\x7f\x00\x00\x01\x03z\x00\x00\x00\x00\x00\x00\x00' +
         '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x7f' +
         '\x00\x00\x01"\xba\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00' +
         '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x7f\x00\x00\x01' +
         '\x07b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08' +
         '\x00\x00\x00\x00\x00\x00\x00\x7f\x00\x00\x01\x1e\xd2\x02' +
         '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
         '\x00\x00\x00\x00\x00\x7f\x00\x00\x01\x16\xc65:token' +
         '3:\x01b\xcfe1:t6:\x01\xbbH\xb4\xbc\x1c1:y1:re')
     encoding = encode(r)
     self.assertEquals(expected_encoding, encoding)
コード例 #3
0
ファイル: test_krpc_coder.py プロジェクト: edisonlz/mdht
 def test_encode_validGetPeersResponseWithNodes(self):
     r = Response()
     r._transaction_id = 1903890316316
     r._from = 169031860931900138093217073128059
     r.token = 90831
     r.nodes = []
     r.nodes.append(Node(2**158, ("127.0.0.1", 890)))
     r.nodes.append(Node(2**15, ("127.0.0.1", 8890)))
     r.nodes.append(Node(2**128, ("127.0.0.1", 1890)))
     r.nodes.append(Node(2**59, ("127.0.0.1", 7890)))
     r.nodes.append(Node(2**153, ("127.0.0.1", 5830)))
     expected_encoding = ('d1:rd2:id20:\x00\x00\x00\x00\x00\x00\x08' +
             'U{fDA\xb0\x88\xe6\x8a\xec\xf8\xe2{5:nodes130:@\x00\x00\x00' +
             '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
             '\x00\x00\x7f\x00\x00\x01\x03z\x00\x00\x00\x00\x00\x00\x00' +
             '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x00\x7f' +
             '\x00\x00\x01"\xba\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00' +
             '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x7f\x00\x00\x01' +
             '\x07b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08' +
             '\x00\x00\x00\x00\x00\x00\x00\x7f\x00\x00\x01\x1e\xd2\x02' +
             '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
             '\x00\x00\x00\x00\x00\x7f\x00\x00\x01\x16\xc65:token' +
             '3:\x01b\xcfe1:t6:\x01\xbbH\xb4\xbc\x1c1:y1:re')
     encoding = encode(r)
     self.assertEquals(expected_encoding, encoding)
コード例 #4
0
ファイル: test_krpc_responder.py プロジェクト: a740122/mdht
    def test_find_node_Received_sendsValidResponseMultipleNodes(self):
        # Create the protocol and populate its
        # routing table with nodes
        kresponder = Patched_KRPC_Responder()
        node_list = []
        node_gen = lambda num: contact.Node(num, ("127.0.0.%d" % num, num))
        for i in range(100):
            n = node_gen(i)
            if kresponder.routing_table.offer_node(n):
                node_list.append(n)

        querying_node = contact.Node(123, test_address)
        incoming_query = Query()
        incoming_query.rpctype = "find_node"
        incoming_query._from = querying_node.node_id
        incoming_query._transaction_id = 15
        incoming_query.target_id = 777777

        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = 15
        expected_response.rpctype = "find_node"
        node_list.sort(key = lambda node:
                        node.distance(incoming_query.target_id))
        node_list = node_list[:constants.k]
        expected_response.nodes = node_list

        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)
コード例 #5
0
    def test_get_peers_Received_sendsValidResponseWithNodes(self):
        # Create the protocol and populate its
        # routing table with nodes
        # We need a node_id close to 'target_id' so that our kbuckets split
        # in a way that we will have the target id
        target_id = 76
        our_id = target_id - 1

        kresponder = Patched_KRPC_Responder(node_id=our_id)
        node_list = []
        node_gen = lambda num: contact.Node(num, ("127.0.0.%d" % num, num))
        for i in range(100):
            if i != our_id:
                n = node_gen(i)
                if kresponder.routing_table.offer_node(n):
                    node_list.append(n)

        # simulate that a get_peers query has been
        # received by making a fake get_peers query
        # and feeding it into "datagramReceived()"
        querying_node = contact.Node(123, test_address)
        incoming_query = Query()
        incoming_query.rpctype = "get_peers"
        incoming_query._from = querying_node.node_id
        incoming_query._transaction_id = 15
        # We have this target id in our routing table
        incoming_query.target_id = target_id

        # Create a response object and ensure
        # and the response (that the node sends)
        # matches what we made
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        expected_response.rpctype = "get_peers"
        # the specification calls for the resulting
        # nodes to be sorted by distance
        node_list.sort(
            key=lambda node: node.distance(incoming_query.target_id))
        node_list = node_list[:constants.k]
        expected_response.nodes = node_list

        # simulating the incoming query and capture
        # the outgoing response
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        # Grab the autogenerated token
        expected_response.token = actual_response.token

        self.assertEquals(expected_response, actual_response)
コード例 #6
0
ファイル: test_krpc_responder.py プロジェクト: a740122/mdht
    def test_get_peers_Received_sendsValidResponseWithNodes(self):
        # Create the protocol and populate its
        # routing table with nodes
        # We need a node_id close to 'target_id' so that our kbuckets split
        # in a way that we will have the target id
        target_id = 76
        our_id = target_id - 1

        kresponder = Patched_KRPC_Responder(node_id=our_id)
        node_list = []
        node_gen = lambda num: contact.Node(num, ("127.0.0.%d" % num, num))
        for i in range(100):
            if i != our_id:
                n = node_gen(i)
                if kresponder.routing_table.offer_node(n):
                    node_list.append(n)

        # simulate that a get_peers query has been
        # received by making a fake get_peers query
        # and feeding it into "datagramReceived()"
        querying_node = contact.Node(123, test_address)
        incoming_query = Query()
        incoming_query.rpctype = "get_peers"
        incoming_query._from = querying_node.node_id
        incoming_query._transaction_id = 15
        # We have this target id in our routing table
        incoming_query.target_id = target_id

        # Create a response object and ensure
        # and the response (that the node sends)
        # matches what we made
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        expected_response.rpctype = "get_peers"
        # the specification calls for the resulting
        # nodes to be sorted by distance
        node_list.sort(key = lambda node:
                        node.distance(incoming_query.target_id))
        node_list = node_list[:constants.k]
        expected_response.nodes = node_list

        # simulating the incoming query and capture
        # the outgoing response
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        # Grab the autogenerated token
        expected_response.token = actual_response.token

        self.assertEquals(expected_response, actual_response)
コード例 #7
0
    def test_find_node_Received_sendsValidResponseWithTargetNode(self):
        # Create the protocol and populate its
        # routing table with nodes
        # We need a node_id close to 'target_id' so that our kbuckets split
        # in a way that we will have the target id
        target_id = 76
        our_id = target_id - 1

        kresponder = Patched_KRPC_Responder(node_id=our_id)
        node_list = []
        node_gen = lambda num: contact.Node(num, ("127.0.0.%d" % num, num))
        for i in range(100):
            if i != our_id:
                n = node_gen(i)
                node_was_accepted = kresponder.routing_table.offer_node(n)
                if node_was_accepted:
                    node_list.append(n)

        querying_node = contact.Node(123, test_address)
        incoming_query = Query()
        incoming_query.rpctype = "find_node"
        incoming_query._from = querying_node.node_id
        incoming_query._transaction_id = 15
        # We have this target id in our routing table
        incoming_query.target_id = target_id

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

        # The response node_list should contain only the target node
        node_list = filter(lambda node: node.node_id == target_id, node_list)
        expected_response.nodes = node_list
        if len(node_list) != 1:
            self.fail("Too many or too few nodes!")

        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)
コード例 #8
0
ファイル: test_krpc_responder.py プロジェクト: a740122/mdht
    def test_find_node_Received_sendsValidResponseWithTargetNode(self):
        # Create the protocol and populate its
        # routing table with nodes
        # We need a node_id close to 'target_id' so that our kbuckets split
        # in a way that we will have the target id
        target_id = 76
        our_id = target_id - 1

        kresponder = Patched_KRPC_Responder(node_id=our_id)
        node_list = []
        node_gen = lambda num: contact.Node(num, ("127.0.0.%d" % num, num))
        for i in range(100):
            if i != our_id:
                n = node_gen(i)
                node_was_accepted = kresponder.routing_table.offer_node(n)
                if node_was_accepted:
                    node_list.append(n)

        querying_node = contact.Node(123, test_address)
        incoming_query = Query()
        incoming_query.rpctype = "find_node"
        incoming_query._from = querying_node.node_id
        incoming_query._transaction_id = 15
        # We have this target id in our routing table
        incoming_query.target_id = target_id

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

        # The response node_list should contain only the target node
        node_list = filter(lambda node: node.node_id == target_id, node_list)
        expected_response.nodes = node_list
        if len(node_list) != 1:
            self.fail("Too many or too few nodes!")

        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)
コード例 #9
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
コード例 #10
0
ファイル: krpc_coder.py プロジェクト: a740122/mdht
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