Exemplo n.º 1
0
 def _get_response(self, msg):
     if msg.query == message.PING:
         return message.OutgoingPingResponse(self._my_id)
     elif msg.query == message.FIND_NODE:
         log_distance = msg.target.log_distance(self._my_id)
         rnodes = self._routing_m.get_closest_rnodes(log_distance,
                                                    NUM_NODES, False)
         return message.OutgoingFindNodeResponse(self._my_id,
                                                 rnodes)
     elif msg.query == message.GET_PEERS:
         token = self._token_m.get()
         log_distance = msg.info_hash.log_distance(self._my_id)
         rnodes = self._routing_m.get_closest_rnodes(log_distance,
                                                    NUM_NODES, False)
         peers = self._tracker.get(msg.info_hash)
         if peers:
             logger.debug('RESPONDING with PEERS:\n%r' % peers)
         return message.OutgoingGetPeersResponse(self._my_id,
                                                 token,
                                                 nodes=rnodes,
                                                 peers=peers)
     elif msg.query == message.ANNOUNCE_PEER:
         peer_addr = (msg.sender_addr[0], msg.bt_port)
         self._tracker.put(msg.info_hash, peer_addr)
         return message.OutgoingAnnouncePeerResponse(self._my_id)
     else:
         logger.debug('Invalid QUERY: %r' % (msg.query))
Exemplo n.º 2
0
 def setup(self):
     self.queries = [
         m.OutgoingPingQuery(tc.CLIENT_ID),
         m.OutgoingFindNodeQuery(tc.CLIENT_ID, tc.TARGET_ID),
         m.OutgoingGetPeersQuery(tc.CLIENT_ID, tc.INFO_HASH),
         m.OutgoingAnnouncePeerQuery(tc.CLIENT_ID, tc.INFO_HASH, tc.BT_PORT,
                                     tc.TOKEN),
     ]
     self.responses = [
         m.OutgoingPingResponse(tc.SERVER_ID),
         m.OutgoingFindNodeResponse(tc.SERVER_ID, tc.NODES),
         m.OutgoingGetPeersResponse(tc.SERVER_ID, tc.TOKEN, tc.NODES,
                                    tc.PEERS),
         m.OutgoingAnnouncePeerResponse(tc.SERVER_ID),
     ]
Exemplo n.º 3
0
 def test_announce_peer(self):
     #client
     outgoing_query = m.OutgoingAnnouncePeerQuery(tc.CLIENT_ID,
                                                  tc.INFO_HASH, tc.BT_PORT,
                                                  tc.TOKEN)
     outgoing_query.tid = tc.TID
     data = outgoing_query.encode(tc.TID)
     #server
     incoming_query = m.IncomingMsg(data, tc.CLIENT_ADDR)
     assert incoming_query.type is m.QUERY
     outgoing_response = m.OutgoingAnnouncePeerResponse(tc.SERVER_ID)
     data = outgoing_response.encode(incoming_query.tid)
     #client
     incoming_response = m.IncomingMsg(data, tc.SERVER_ADDR)
     assert incoming_response.type is m.RESPONSE
Exemplo n.º 4
0
 def test_return_response_for_announce_peer_with_valid_tocken(self):
     # client side
     query_msg = message.OutgoingAnnouncePeerQuery(tc.CLIENT_ID,
                                                   tc.INFO_HASH,
                                                   tc.CLIENT_ADDR[1],
                                                   self.token_m.get())
     # querier.send_query() encodes
     query_data = query_msg.encode(tc.TID)
     # server side
     # rpc_manager.datagram_received() decodes and calls responder (callback)
     query_msg = message.IncomingMsg(query_data)
     assert not self.notification_callback_done
     response_msg = self.responder.on_query_received(
         query_msg, tc.CLIENT_ADDR)
     response_data = response_msg.encode(query_msg.tid)
     assert self.notification_callback_done
     # responder returns to querier
     expected_msg = message.OutgoingAnnouncePeerResponse(tc.SERVER_ID)
     expected_data = expected_msg.encode(tc.TID)
     assert response_data == expected_data
Exemplo n.º 5
0
    def test_msg_exhanges(self):
        self._exchange_msgs(m.OutgoingPingQuery(tc.CLIENT_ID),
                            m.OutgoingPingResponse(tc.SERVER_ID))

        self._exchange_msgs(
            m.OutgoingFindNodeQuery(tc.CLIENT_ID, tc.TARGET_ID),
            m.OutgoingFindNodeResponse(tc.SERVER_ID, tc.NODES))

        # Test different combinations of token, nodes and peers
        self._exchange_msgs(
            m.OutgoingGetPeersQuery(tc.CLIENT_ID, tc.INFO_HASH),
            m.OutgoingGetPeersResponse(tc.SERVER_ID, tc.TOKEN, tc.NODES,
                                       tc.PEERS))
        self._exchange_msgs(
            m.OutgoingGetPeersQuery(tc.CLIENT_ID, tc.INFO_HASH),
            m.OutgoingGetPeersResponse(tc.SERVER_ID, tc.TOKEN, tc.NODES))
        self._exchange_msgs(
            m.OutgoingGetPeersQuery(tc.CLIENT_ID, tc.INFO_HASH),
            m.OutgoingGetPeersResponse(tc.SERVER_ID, tc.TOKEN, peers=tc.PEERS))
        assert_raises(AssertionError, m.OutgoingGetPeersResponse, tc.SERVER_ID,
                      tc.TOKEN)
        self._exchange_msgs(
            m.OutgoingGetPeersQuery(tc.CLIENT_ID, tc.INFO_HASH),
            m.OutgoingGetPeersResponse(tc.SERVER_ID, peers=tc.PEERS))
        self._exchange_msgs(
            m.OutgoingGetPeersQuery(tc.CLIENT_ID, tc.INFO_HASH),
            m.OutgoingGetPeersResponse(tc.SERVER_ID, nodes=tc.NODES))
        self._exchange_msgs(
            m.OutgoingGetPeersQuery(tc.CLIENT_ID, tc.INFO_HASH),
            m.OutgoingGetPeersResponse(tc.SERVER_ID,
                                       nodes=tc.NODES,
                                       peers=tc.PEERS))
        assert_raises(AssertionError, m.OutgoingGetPeersResponse, tc.SERVER_ID)

        self._exchange_msgs(
            m.OutgoingAnnouncePeerQuery(tc.CLIENT_ID, tc.INFO_HASH, tc.BT_PORT,
                                        tc.TOKEN),
            m.OutgoingAnnouncePeerResponse(tc.SERVER_ID))
Exemplo n.º 6
0
        m.IncomingMsg(bencode.encode(self.ping_d), tc.CLIENT_ADDR)

    def test_announce(self):
        # Port must be integer
        self.ap_d[m.ARGS][m.PORT] = 'a'
        assert_raises(m.MsgError, m.IncomingMsg, bencode.encode(self.ap_d),
                      tc.CLIENT_ADDR)


b_ping_r = m.OutgoingPingResponse(tc.CLIENT_ID).encode(tc.TID)
b_fn2_r = m.OutgoingFindNodeResponse(tc.CLIENT_ID, tc.NODES).encode(tc.TID)
b_gp_r = m.OutgoingGetPeersResponse(tc.CLIENT_ID,
                                    tc.TOKEN,
                                    tc.NODES,
                                    peers=tc.PEERS).encode(tc.TID)
b_ap_r = m.OutgoingAnnouncePeerResponse(tc.CLIENT_ID).encode(tc.TID)


class TestSanitizeResponseError:
    def setup(self):
        self.ping_r = m.IncomingMsg(b_ping_r, tc.SERVER_ADDR)
        self.fn2_r = m.IncomingMsg(b_fn2_r, tc.SERVER_ADDR)
        self.gp_r = m.IncomingMsg(b_gp_r, tc.SERVER_ADDR)
        self.ap_r = m.IncomingMsg(b_ap_r, tc.SERVER_ADDR)


'''
    def _test_sanitize(self):
        self.ping_r.sanitize_response(m.PING)

        del self.fn2_r._msg_dict[m.RESPONSE][m.NODES2]
Exemplo n.º 7
0
 def _on_announce_peer(self, query_msg):
     #FIXME: tracker.put()
     return message.OutgoingAnnouncePeerResponse(self.my_id)
Exemplo n.º 8
0
 def _on_announce_peer(self, query_msg):
     return message.OutgoingAnnouncePeerResponse(self.my_id)