def announce_peer(self, address, target_id, token, port, timeout=constants.rpctimeout): query = Query() query.rpctype = "announce_peer" query.target_id = target_id query.token = token query.port = port return self.sendQuery(query, address, timeout)
def _query_decoder(rpc_dict): """ Decode the given KRPC dictionary into a valid Query @see decode @return krpc_types.Query """ q = Query() q._from = basic_coder.decode_network_id(rpc_dict['a']['id']) q.rpctype = rpctype = rpc_dict['q'] if rpctype == 'ping': pass elif rpctype == 'find_node': q.target_id = basic_coder.decode_network_id(rpc_dict['a']['target']) elif rpctype == 'get_peers': q.target_id = basic_coder.decode_network_id(rpc_dict['a']['info_hash']) elif rpctype == 'announce_peer': q.target_id = basic_coder.decode_network_id(rpc_dict['a']['info_hash']) # Try encoding the port (to ensure it is within range) basic_coder.encode_port(rpc_dict['a']['port']) q.port = rpc_dict['a']['port'] q.token = basic_coder.btol(rpc_dict['a']['token']) else: raise _ProtocolFormatError() return q
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)
def announce_peer(self, address, target_id, token, port, timeout=None): log.msg("make a announce_peers request to node(%s:%s)" % address) timeout = timeout or constants.rpctimeout query = Query() query.rpctype = "announce_peer" query.target_id = target_id query.token = token query.port = port return self.sendQuery(query, address, timeout)
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)
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)