Esempio n. 1
0
    def test_on_torrent_health_response_from_unknown_peer(self):
        """
        Tests receiving torrent health response from unknown peer
        """
        original_logger = self.nodes[0].overlay.logger
        self.nodes[
            0].overlay.logger.error = lambda *args, **kw: self.fake_logger_error(
                self.nodes[0], *args)

        infohash = 'a' * 20
        num_seeders = 10
        num_leechers = 5
        timestamp = 123123123

        payload = TorrentHealthPayload(infohash, num_seeders, num_leechers,
                                       timestamp)
        source_address = ('1.1.1.1', 1024)
        data = self.nodes[0].overlay.create_message_packet(
            MSG_TORRENT_HEALTH_RESPONSE, payload)

        self.nodes[0].unknown_response = False
        self.nodes[0].overlay.on_torrent_health_response(source_address, data)
        yield self.deliver_messages()

        self.assertTrue(self.nodes[0].unknown_response)

        # Restore logger
        self.nodes[0].overlay.logger = original_logger
Esempio n. 2
0
    def test_on_torrent_health_response(self):
        """
        Tests receiving torrent health response from unknown peer
        """
        def fake_update_torrent(peer):
            peer.called_update_torrent = True

        self.nodes[0].overlay.content_repository = MockRepository()
        self.nodes[0].overlay.content_repository.update_torrent_health = lambda payload, peer_trust: \
            fake_update_torrent(self.nodes[0])

        infohash = 'a' * 20
        num_seeders = 10
        num_leechers = 5
        timestamp = 123123123

        payload = TorrentHealthPayload(infohash, num_seeders, num_leechers,
                                       timestamp)
        data = self.nodes[1].overlay.create_message_packet(
            MSG_TORRENT_HEALTH_RESPONSE, payload)

        yield self.introduce_nodes()

        # Add node 1 in publisher list of node 0
        self.nodes[0].overlay.publishers.add(self.nodes[1].my_peer)
        self.nodes[0].overlay.on_torrent_health_response(
            self.nodes[1].my_peer.address, data)
        yield self.deliver_messages()

        self.assertTrue(self.nodes[0].called_update_torrent)
Esempio n. 3
0
    def try_torrent_update_with_options(self, db_last_check_time, peer_trust):
        """
        Tries updating torrent considering the given last check time of existing torrent and a new response
        obtained from a peer with given peer_trust value.
        """
        sample_infohash, seeders, leechers, timestamp = 'a' * 20, 10, 5, db_last_check_time
        sample_payload = TorrentHealthPayload(sample_infohash, seeders,
                                              leechers, timestamp)

        def update_torrent(content_repo, _):
            content_repo.update_torrent_called = True

        def get_torrent(infohash):
            return {
                'infohash': infohash,
                'num_seeders': seeders,
                'num_leechers': leechers,
                'last_tracker_check': timestamp
            }

        self.content_repository.torrent_db.getTorrent = lambda infohash, **kw: get_torrent(
            infohash)
        self.content_repository.torrent_db.hasTorrent = lambda infohash: infohash == sample_infohash
        self.content_repository.torrent_db.updateTorrent = \
            lambda infohash, *args, **kw: update_torrent(self.content_repository, infohash)

        self.content_repository.update_torrent_called = False
        self.content_repository.update_torrent_health(sample_payload,
                                                      peer_trust=peer_trust)

        return self.content_repository.update_torrent_called
Esempio n. 4
0
    def test_torrent_health_payload(self):
        """ Test serialization/deserialization of Torrent health payload """
        infohash = b'a' * 20
        num_seeders = 10
        num_leechers = 5
        timestamp = 123123123

        health_payload = TorrentHealthPayload(infohash, num_seeders, num_leechers, timestamp)
        serialized = self.serializer.pack_multiple(health_payload.to_pack_list())[0]

        # Deserialize and test it
        (deserialized, _) = self.serializer.unpack_multiple(TorrentHealthPayload.format_list, serialized)
        deserialized_payload = TorrentHealthPayload.from_unpack_list(*deserialized)

        self.assertEqual(infohash, deserialized_payload.infohash)
        self.assertEqual(num_seeders, deserialized_payload.num_seeders)
        self.assertEqual(num_leechers, deserialized_payload.num_leechers)
        self.assertEqual(timestamp, deserialized_payload.timestamp)
Esempio n. 5
0
    def test_torrent_health_payload(self):
        """ Test serialization/deserialization of Torrent health payload """
        infohash = 'a' * 20
        num_seeders = 10
        num_leechers = 5
        timestamp = 123123123

        health_payload = TorrentHealthPayload(infohash, num_seeders, num_leechers, timestamp)
        serialized = self.serializer.pack_multiple(health_payload.to_pack_list())[0]

        # Deserialize and test it
        (deserialized, _) = self.serializer.unpack_multiple(TorrentHealthPayload.format_list, serialized)
        deserialized_payload = TorrentHealthPayload.from_unpack_list(*deserialized)

        self.assertEqual(infohash, deserialized_payload.infohash)
        self.assertEqual(num_seeders, deserialized_payload.num_seeders)
        self.assertEqual(num_leechers, deserialized_payload.num_leechers)
        self.assertEqual(timestamp, deserialized_payload.timestamp)
Esempio n. 6
0
 def publish_latest_torrents(self, peer):
     """
     Publishes the latest torrents in local database to the given peer.
     """
     torrents = self.content_repository.get_top_torrents()
     self.logger.info("Publishing %d torrents to peer %s", len(torrents), peer)
     for torrent in torrents:
         infohash, seeders, leechers, timestamp = torrent[:4]
         payload = TorrentHealthPayload(infohash, seeders, leechers, timestamp)
         self.send_torrent_health_response(payload, peer=peer)
Esempio n. 7
0
    def test_update_torrent_health(self):
        """
        Tests update torrent health.
        """
        fake_torrent_health_payload = TorrentHealthPayload('0' * 20, 10, 4, time.time())
        self.content_repository.update_torrent_health(fake_torrent_health_payload, peer_trust=0)

        with db_session:
            torrent = self.content_repository.get_torrent('0' * 20)
            self.assertEqual(torrent.health.seeders, 10)
            self.assertEqual(torrent.health.leechers, 4)
Esempio n. 8
0
    def publish_latest_torrents(self, peer):
        """
        Publishes the latest torrents in local database to the given peer.
        """
        with db_session:
            torrents = self.content_repository.get_top_torrents()
            self.logger.info("Publishing %d torrents to peer %s", len(torrents), peer)

            to_send = [TorrentHealthPayload(bytes(torrent.infohash), torrent.health.seeders, torrent.health.leechers,
                                            torrent.health.last_check) for torrent in torrents]
        for payload in to_send:
            self.send_torrent_health_response(payload, peer=peer)
Esempio n. 9
0
    def publish_next_content(self):
        """
        Publishes the next content from the queue to the subscribers.
        Does nothing if there are no subscribers.
        Only Torrent health response is published at the moment.
        """
        self.logger.info("Content to publish: %d", self.content_repository.queue_length())
        if not self.subscribers:
            self.logger.info("No subscribers found. Not publishing anything")
            return

        content = self.content_repository.pop_content()
        if content:
            infohash, seeders, leechers, timestamp = content
            payload = TorrentHealthPayload(bytes(infohash), seeders, leechers, timestamp)
            self.send_torrent_health_response(payload)
Esempio n. 10
0
    def test_update_torrent_health(self):
        """
        Tests update torrent health.
        """
        def update_torrent(repo, _):
            repo.update_torrent_called = True

        # Assume a fake torrent response
        fake_torrent_health_payload = TorrentHealthPayload('a' * 20, 10, 4, time.time())

        self.content_repository.torrent_db = MockObject()
        self.content_repository.torrent_db.updateTorrent = lambda infohash, *args, **kw: \
            update_torrent(self.content_repository, infohash)

        # If torrent does not exist in the database, then it should be added to the database
        self.content_repository.has_torrent = lambda infohash: False
        self.content_repository.update_torrent_health(fake_torrent_health_payload, peer_trust=0)

        self.assertTrue(self.content_repository.update_torrent_called)
Esempio n. 11
0
    def publish_next_content(self):
        """
        Publishes the next content from the queue to the subscribers.
        Does nothing if there are none subscribers.
        Only Torrent health response is published at the moment.
        """
        self.logger.info("Content to publish: %d", self.content_repository.count_content())
        if not self.subscribers:
            self.logger.info("No subscribers found. Not publishing anything")
            return

        content_type, content = self.content_repository.pop_content()
        if content_type is None:
            self.logger.error(ERROR_NO_CONTENT)
            return

        self.logger.info("Publishing content[type:%d]", content_type)
        if content_type == TYPE_TORRENT_HEALTH:
            infohash, seeders, leechers, timestamp = content
            payload = TorrentHealthPayload(infohash, seeders, leechers, timestamp)
            self.send_torrent_health_response(payload)