Пример #1
0
    def test_announce_peer_Received_sendsValidResponse(self):
        kresponder = Patched_KRPC_Responder()
        # announce_peer queries need a token (the token value
        # comes in response to a get_peers query)
        # So first, we need to "receive" a get_peers query

        # get_peers creation and "receiving"
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 150
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query),
                                    test_address)
        response = kresponder.sendResponse.response
        # announce_peer creation and "receiving"
        incoming_query = Query()
        incoming_query._transaction_id = 999
        incoming_query._from = query._from
        incoming_query.rpctype = "announce_peer"
        incoming_query.token = response.token
        incoming_query.port = 55
        incoming_query.target_id = query.target_id

        # Test the announce_peer response
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        expected_response.rpctype = "announce_peer"
        # Reset the response grabber
        kresponder.sendResponse.response = None
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)
Пример #2
0
    def test_announce_peer_Received_sendsValidResponse(self):
        kresponder = Patched_KRPC_Responder()
        # announce_peer queries need a token (the token value
        # comes in response to a get_peers query)
        # So first, we need to "receive" a get_peers query

        # get_peers creation and "receiving"
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 150
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query), test_address)
        response = kresponder.sendResponse.response
        # announce_peer creation and "receiving"
        incoming_query = Query()
        incoming_query._transaction_id = 999
        incoming_query._from = query._from
        incoming_query.rpctype = "announce_peer"
        incoming_query.token = response.token
        incoming_query.port = 55
        incoming_query.target_id = query.target_id

        # Test the announce_peer response
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        expected_response.rpctype = "announce_peer"
        # Reset the response grabber
        kresponder.sendResponse.response = None
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)
Пример #3
0
    def test_announce_peer_Received_validTokenAddsPeer(self):
        kresponder = Patched_KRPC_Responder()
        # announce_peer queries need a token (the token value
        # comes in response to a get_peers query)
        # So first, we need to "receive" a get_peers query

        # get_peers creation and "receiving"
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 150
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query),
                                    test_address)
        response = kresponder.sendResponse.response
        # announce_peer creation and "receiving"
        incoming_query = Query()
        incoming_query._transaction_id = 999
        incoming_query._from = query._from
        incoming_query.rpctype = "announce_peer"
        incoming_query.token = response.token
        incoming_query.port = 55
        incoming_query.target_id = query.target_id

        # Test the announce_peer response
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        expected_response.rpctype = "announce_peer"
        # Reset the response grabber
        kresponder.sendResponse.response = None
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)

        # Check to see if another get_peers query will return us
        # as a peer
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 9809831
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query),
                                    test_address)
        response = kresponder.sendResponse.response
        self.assertEquals(1, len(response.peers))
        test_ip, test_port = test_address
        returned_peer = list(response.peers)[0]
        self.assertEquals((test_ip, incoming_query.port), returned_peer)
Пример #4
0
    def test_announce_peer_Received_validTokenAddsPeer(self):
        kresponder = Patched_KRPC_Responder()
        # announce_peer queries need a token (the token value
        # comes in response to a get_peers query)
        # So first, we need to "receive" a get_peers query

        # get_peers creation and "receiving"
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 150
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query), test_address)
        response = kresponder.sendResponse.response
        # announce_peer creation and "receiving"
        incoming_query = Query()
        incoming_query._transaction_id = 999
        incoming_query._from = query._from
        incoming_query.rpctype = "announce_peer"
        incoming_query.token = response.token
        incoming_query.port = 55
        incoming_query.target_id = query.target_id

        # Test the announce_peer response
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        expected_response.rpctype = "announce_peer"
        # Reset the response grabber
        kresponder.sendResponse.response = None
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        self.assertEquals(expected_response, actual_response)

        # Check to see if another get_peers query will return us
        # as a peer
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 9809831
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query), test_address)
        response = kresponder.sendResponse.response
        self.assertEquals(1, len(response.peers))
        test_ip, test_port = test_address
        returned_peer = list(response.peers)[0]
        self.assertEquals((test_ip, incoming_query.port), returned_peer)
Пример #5
0
    def test_announce_peer_Received_invalidTokenDoesntAddPeer(self):
        kresponder = Patched_KRPC_Responder()
        # announce_peer queries need a token (the token value
        # comes in response to a get_peers query)
        # So first, we need to "receive" a get_peers query

        # get_peers creation and "receiving"
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 150
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query),
                                    test_address)
        response = kresponder.sendResponse.response
        # announce_peer creation and "receiving"
        incoming_query = Query()
        incoming_query._transaction_id = 999
        incoming_query._from = query._from
        incoming_query.rpctype = "announce_peer"
        incoming_query.token = 5858585858 # this is an invalid token
        incoming_query.port = 55
        incoming_query.target_id = query.target_id

        # Test the announce_peer response
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        # Reset the response grabber
        kresponder.sendResponse.response = None
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        # Make sure we didnt send a response
        self.assertEquals(None, actual_response)

        # Check to see if another get_peers query will return us
        # as a peer
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 9809831
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query),
                                    test_address)
        response = kresponder.sendResponse.response
        # Make sure no peers were returned
        self.assertEquals(None, response.peers)
Пример #6
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)
Пример #7
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)
Пример #8
0
    def test_get_peers_Received_sendsValidResponseWithPeers(self):
        # Create the protocol and populate its
        # routing table with nodes
        kresponder = Patched_KRPC_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"

        ds = kresponder._datastore
        for peer in peers:
            ds[incoming_query.target_id].add(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

        ap = actual_response
        ap.peers = sorted(ap.peers, key=lambda (ip, port): port)

        actual_response.peers.sort(key=lambda (ip, port): port)
        self.assertEquals(expected_response, actual_response)
Пример #9
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)
Пример #10
0
    def test_get_peers_Received_sendsValidResponseWithPeers(self):
        # Create the protocol and populate its
        # routing table with nodes
        kresponder = Patched_KRPC_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"

        ds = kresponder._datastore
        for peer in peers:
            ds[incoming_query.target_id].add(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

        ap = actual_response
        ap.peers = sorted(ap.peers, key=lambda (ip,port): port)

        actual_response.peers.sort(key = lambda (ip, port) : port)
        self.assertEquals(expected_response, actual_response)
Пример #11
0
 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)
Пример #12
0
 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)
Пример #13
0
    def test_announce_peer_Received_invalidTokenDoesntAddPeer(self):
        kresponder = Patched_KRPC_Responder()
        # announce_peer queries need a token (the token value
        # comes in response to a get_peers query)
        # So first, we need to "receive" a get_peers query

        # get_peers creation and "receiving"
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 150
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query), test_address)
        response = kresponder.sendResponse.response
        # announce_peer creation and "receiving"
        incoming_query = Query()
        incoming_query._transaction_id = 999
        incoming_query._from = query._from
        incoming_query.rpctype = "announce_peer"
        incoming_query.token = 5858585858  # this is an invalid token
        incoming_query.port = 55
        incoming_query.target_id = query.target_id

        # Test the announce_peer response
        expected_response = Response()
        expected_response._from = kresponder.node_id
        expected_response._transaction_id = incoming_query._transaction_id
        # Reset the response grabber
        kresponder.sendResponse.response = None
        kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                    test_address)
        actual_response = kresponder.sendResponse.response
        # Make sure we didnt send a response
        self.assertEquals(None, actual_response)

        # Check to see if another get_peers query will return us
        # as a peer
        query = Query()
        query.rpctype = "get_peers"
        query._from = 123
        query._transaction_id = 9809831
        query.target_id = 800
        kresponder.datagramReceived(krpc_coder.encode(query), test_address)
        response = kresponder.sendResponse.response
        # Make sure no peers were returned
        self.assertEquals(None, response.peers)
Пример #14
0
 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)
Пример #15
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:valuesl6:\x7f\x00\x00\x01\x00P6:\x04\x02\x02\x01"\xc96:\x00\x00\x00\x00\x00\x006:\xff\xff\xff\xff\xff\xff6:\x08\x08\x08\x08\x005ee1:t6:\x01\xbbH\xb4\xbc\x1c1:y1:re'
     encoding = encode(r)
     self.assertEquals(expected_encoding, encoding)
Пример #16
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:valuesl6:\x7f\x00\x00\x01\x00P6:\x04\x02\x02\x01"\xc96:\x00\x00\x00\x00\x00\x006:\xff\xff\xff\xff\xff\xff6:\x08\x08\x08\x08\x005ee1:t6:\x01\xbbH\xb4\xbc\x1c1:y1:re'
     encoding = encode(r)
     self.assertEquals(expected_encoding, encoding)
Пример #17
0
 def test_encode_validGetPeers(self):
     q = self.q
     q.target_id = self.test_target_id
     q.rpctype = "get_peers"
     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\x009:info_hash' +
         '20:\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
         '\x00\x00\x00\x08i@e1:q9:get_peers1:t1:\x0f1:y1:qe')
     self.assertEquals(expected_encoding, encoding)
Пример #18
0
 def test_encode_validFindNode(self):
     q = self.q
     q.target_id = self.test_target_id
     q.rpctype = "find_node"
     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\x006:target20:' +
         '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + 
         '\x00\x00\x08i@e1:q9:find_node1:t1:\x0f1:y1:qe')
     self.assertEquals(expected_encoding, encoding)
Пример #19
0
 def test_encode_validGetPeers(self):
     q = self.q
     q.target_id = self.test_target_id
     q.rpctype = "get_peers"
     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\x009:info_hash' +
         '20:\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
         '\x00\x00\x00\x08i@e1:q9:get_peers1:t1:\x0f1:y1:qe')
     self.assertEquals(expected_encoding, encoding)
Пример #20
0
 def test_encode_validFindNode(self):
     q = self.q
     q.target_id = self.test_target_id
     q.rpctype = "find_node"
     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\x006:target20:' +
         '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
         '\x00\x00\x08i@e1:q9:find_node1:t1:\x0f1:y1:qe')
     self.assertEquals(expected_encoding, encoding)
Пример #21
0
    def _patch_counter_and_input_krpc(self, krpc, method_name,
                                      num_calls=1, address=None):
        if address is None:
            address = ("127.0.0.1", 8888)
        k_messenger = KRPC_Sender(TreeRoutingTable, 2**50)
        # Patch in our counter
        counter = Counter()
        setattr(k_messenger, method_name, counter)

        # Pass in the krpc
        k_messenger.datagramReceived(krpc_coder.encode(krpc), address)
        self.assertEquals(num_calls, counter.count)
Пример #22
0
 def test_encode_validAnnouncePeer(self):
     q = self.q
     q.rpctype = "announce_peer"
     q.target_id = self.test_target_id
     q.port = self.test_port
     q.token = self.test_token
     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\x009:info_hash' +
         '20:\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
         '\x00\x00\x00\x08i@4:porti511e5:token2:\x15\xb3e1:q13:'+
         'announce_peer1:t1:\x0f1:y1:qe')
     self.assertEquals(expected_encoding, encoding)
Пример #23
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)
Пример #24
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)
Пример #25
0
 def test_ping_Received_sendsValidResponse(self):
     kresponder = Patched_KRPC_Responder()
     incoming_query = Query()
     incoming_query.rpctype = "ping"
     incoming_query._from = 123
     incoming_query._transaction_id = 15
     expected_response = Response()
     expected_response._from = kresponder.node_id
     expected_response._transaction_id = 15
     expected_response.rpctype = "ping"
     kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                 test_address)
     actual_response = kresponder.sendResponse.response
     self.assertEquals(expected_response, actual_response)
Пример #26
0
 def test_ping_Received_sendsValidResponse(self):
     kresponder = Patched_KRPC_Responder()
     incoming_query = Query()
     incoming_query.rpctype = "ping"
     incoming_query._from = 123
     incoming_query._transaction_id = 15
     expected_response = Response()
     expected_response._from = kresponder.node_id
     expected_response._transaction_id = 15
     expected_response.rpctype = "ping"
     kresponder.datagramReceived(krpc_coder.encode(incoming_query),
                                 test_address)
     actual_response = kresponder.sendResponse.response
     self.assertEquals(expected_response, actual_response)
Пример #27
0
 def test_errback_KRPCError(self):
     counter = Counter()
     d = self.k_messenger.sendQuery(self.query, address, timeout)
     self.assertTrue(self.query._transaction_id in
                     self.k_messenger._transactions)
     # Build the response we will "receive"
     error = self.query.build_error()
     d.addErrback(self._error_equality, error)
     d.addErrback(counter)
     encoded_error = krpc_coder.encode(error)
     self.k_messenger.datagramReceived(encoded_error, address)
     self.assertEquals(1, counter.count)
     self.assertFalse(self.query._transaction_id in
                      self.k_messenger._transactions)
Пример #28
0
 def test_errback_KRPCError(self):
     counter = Counter()
     d = self.k_messenger.sendQuery(self.query, address, timeout)
     self.assertTrue(
         self.query._transaction_id in self.k_messenger._transactions)
     # Build the response we will "receive"
     error = self.query.build_error()
     d.addErrback(self._error_equality, error)
     d.addErrback(counter)
     encoded_error = krpc_coder.encode(error)
     self.k_messenger.datagramReceived(encoded_error, address)
     self.assertEquals(1, counter.count)
     self.assertFalse(
         self.query._transaction_id in self.k_messenger._transactions)
Пример #29
0
 def test_encode_validAnnouncePeer(self):
     q = self.q
     q.rpctype = "announce_peer"
     q.target_id = self.test_target_id
     q.port = self.test_port
     q.token = self.test_token
     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\x009:info_hash' +
         '20:\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' +
         '\x00\x00\x00\x08i@4:porti511e5:token2:\x15\xb3e1:q13:' +
         'announce_peer1:t1:\x0f1:y1:qe')
     self.assertEquals(expected_encoding, encoding)
Пример #30
0
 def test_callback(self):
     counter = Counter()
     d = self.k_messenger.sendQuery(self.query, address, timeout)
     self.assertTrue(
         self.query._transaction_id in self.k_messenger._transactions)
     # Build the response we will "receive"
     response = self.query.build_response()
     response._from = 9
     d.addCallback(self._response_equality, response)
     d.addCallback(counter)
     encoded_response = krpc_coder.encode(response)
     self.k_messenger.datagramReceived(encoded_response, address)
     self.assertEquals(1, counter.count)
     self.assertFalse(
         self.query._transaction_id in self.k_messenger._transactions)
Пример #31
0
 def test_callback(self):
     counter = Counter()
     d = self.k_messenger.sendQuery(self.query, address, timeout)
     self.assertTrue(self.query._transaction_id in
                     self.k_messenger._transactions)
     # Build the response we will "receive"
     response = self.query.build_response()
     response._from = 9
     d.addCallback(self._response_equality, response)
     d.addCallback(counter)
     encoded_response = krpc_coder.encode(response)
     self.k_messenger.datagramReceived(encoded_response, address)
     self.assertEquals(1, counter.count)
     self.assertFalse(self.query._transaction_id in
                      self.k_messenger._transactions)
Пример #32
0
    def _patch_counter_and_input_krpc(self,
                                      krpc,
                                      method_name,
                                      num_calls=1,
                                      address=None):
        if address is None:
            address = ("127.0.0.1", 8888)
        k_messenger = KRPC_Sender(TreeRoutingTable, 2**50)
        # Patch in our counter
        counter = Counter()
        setattr(k_messenger, method_name, counter)

        # Pass in the krpc
        k_messenger.datagramReceived(krpc_coder.encode(krpc), address)
        self.assertEquals(num_calls, counter.count)
Пример #33
0
 def test_responseReceived(self):
     # Make a query that we will "send"
     query = Query()
     query.rpctype = "ping"
     # Make the protocol and patch in our counter, transport, and reactor
     counter = Counter()
     k_messenger = KRPC_Sender(TreeRoutingTable, 2**50)
     k_messenger.transport = HollowTransport()
     k_messenger.responseReceived = counter
     # Send the query and receive the response
     k_messenger.sendQuery(query, address, timeout)
     self.assertTrue(query._transaction_id in k_messenger._transactions)
     # Make a response that we will "receive"
     response = query.build_response()
     response._from = 9
     k_messenger.datagramReceived(krpc_coder.encode(response), address)
     _restore_reactor()
     self.assertEquals(1, counter.count)
Пример #34
0
 def test_responseReceived(self):
     # Make a query that we will "send"
     query = Query()
     query.rpctype = "ping"
     # Make the protocol and patch in our counter, transport, and reactor
     counter = Counter()
     k_messenger = KRPC_Sender(TreeRoutingTable, 2**50)
     k_messenger.transport = HollowTransport()
     k_messenger.responseReceived = counter
     # Send the query and receive the response
     k_messenger.sendQuery(query, address, timeout)
     self.assertTrue(query._transaction_id in k_messenger._transactions)
     # Make a response that we will "receive"
     response = query.build_response()
     response._from = 9
     k_messenger.datagramReceived(krpc_coder.encode(response), address)
     _restore_reactor()
     self.assertEquals(1, counter.count)
Пример #35
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)
Пример #36
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)
Пример #37
0
 def _encode_and_respond(self, krpc):
     responding_node = test_nodes[22]
     krpc._from = responding_node.node_id
     encoded_krpc = krpc_coder.encode(krpc)
     self.ksimple.datagramReceived(encoded_krpc, responding_node.address)
     return responding_node
Пример #38
0
 def sendKRPC(self, krpc, address):
     encoded_packet = krpc_coder.encode(krpc)
     log.msg("sendKRPC", encoded_packet, address, "\n")
     self.transport.write(encoded_packet, address)
Пример #39
0
 def sendKRPC(self, krpc, address):
     encoded_packet = krpc_coder.encode(krpc)
     self.transport.write(encoded_packet, address)
Пример #40
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)
Пример #41
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"])
Пример #42
0
 def sendKRPC(self, krpc, address):
     encoded_packet = krpc_coder.encode(krpc)
     self.transport.write(encoded_packet, address)