Exemplo n.º 1
0
    def setUp(self, annotate=True, autoload_discovery=True):
        """
        Setup some classes and files that are used by the tests in this module.
        """
        yield super(BaseTestChannel,
                    self).setUp(autoload_discovery=autoload_discovery)

        if annotate:
            self.annotate(self._testMethodName, start=True)

        self.fake_session = MockObject()
        self.fake_session.get_state_dir = lambda: self.session_base_dir
        self.fake_session.add_observer = lambda a, b, c: False

        fake_notifier = MockObject()
        fake_notifier.add_observer = lambda a, b, c, d: False
        fake_notifier.notify = lambda a, b, c, d: False
        self.fake_session.notifier = fake_notifier

        self.fake_channel_community = MockObject()
        self.fake_channel_community.get_channel_id = lambda: 42
        self.fake_channel_community.cid = 'a' * 20
        self.fake_channel_community.get_channel_name = lambda: "my fancy channel"

        self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)
        self.votecast_db_handler = self.session.open_dbhandler(NTFY_VOTECAST)

        self.session.get_dispersy = lambda: True
        self.session.lm.dispersy = Dispersy(ManualEnpoint(0),
                                            self.getStateDir())
Exemplo n.º 2
0
 def setUp(self):
     super(TestBarterStatistics, self).setUp()
     self.stats = BarterStatistics()
     self._peer1 = "peer1"
     self._peer2 = "peer2"
     self._peer3 = "peer3"
     self._peer4 = "peer4"
     self._peer5 = "peer5"
     self.dispersy = Dispersy(ManualEnpoint(0), self.getStateDir())
Exemplo n.º 3
0
 def setUp(self, annotate=True, autoload_discovery=True):
     yield super(TestBoostingManagerSysChannel, self).setUp()
     self.channel_db_handler = self.session.open_dbhandler(NTFY_CHANNELCAST)
     self.channel_db_handler._get_my_dispersy_cid = lambda: "myfakedispersyid"
     self.session.config.get_dispersy_enabled = lambda: True
     self.session.lm.dispersy = Dispersy(ManualEnpoint(0),
                                         self.getStateDir())
     self.dispersy_cid_hex = "abcd" * 9 + "0012"
     self.dispersy_cid = binascii.unhexlify(self.dispersy_cid_hex)
Exemplo n.º 4
0
    def setUp(self, autoload_discovery=True):
        yield super(TestCircuitDebugEndpoint, self).setUp(autoload_discovery=autoload_discovery)

        self.dispersy = Dispersy(ManualEnpoint(0), self.getStateDir())
        self.dispersy._database.open()
        master_member = DummyMember(self.dispersy, 1, "a" * 20)
        member = self.dispersy.get_new_member(u"curve25519")

        self.tunnel_community = HiddenTunnelCommunity(self.dispersy, master_member, member)
        self.dispersy.get_communities = lambda: [self.tunnel_community]
        self.session.get_dispersy_instance = lambda: self.dispersy
Exemplo n.º 5
0
    def setUp(self, autoload_discovery=True):
        yield super(TestTrustchainStatsEndpoint, self).setUp(autoload_discovery=autoload_discovery)

        self.dispersy = Dispersy(ManualEnpoint(0), self.getStateDir())
        self.dispersy._database.open()
        master_member = DummyMember(self.dispersy, 1, "a" * 20)
        self.member = self.dispersy.get_new_member(u"curve25519")

        self.tc_community = TriblerChainCommunity(self.dispersy, master_member, self.member)
        self.dispersy.get_communities = lambda: [self.tc_community]
        self.session.get_dispersy_instance = lambda: self.dispersy
Exemplo n.º 6
0
    def test_chn_exist_lookup(self):
        """
        testing existing channel as a source.

        It also tests how boosting manager cope with unknown channel with retrying
        the lookup
        """
        self.session.get_dispersy = lambda: True
        self.session.lm.dispersy = Dispersy(ManualEnpoint(0),
                                            self.getStateDir())
        dispersy_cid_hex = "abcd" * 9 + "0012"
        dispersy_cid = binascii.unhexlify(dispersy_cid_hex)

        # create channel and insert torrent
        self.create_fake_allchannel_community()
        self.create_torrents_in_channel(dispersy_cid_hex)

        # channel is exist
        community = ChannelCommunity.init_community(
            self.session.lm.dispersy,
            self.session.lm.dispersy.get_member(mid=dispersy_cid),
            self.session.lm.dispersy._communities['allchannel']._my_member,
            self.session)

        # make the id unknown so boosting manager can test repeating search
        id_tmp = community._channel_id
        community._channel_id = 0

        def _set_id_channel(channel_id):
            """
            set channel id manually (emulate finding)
            """
            community._channel_id = channel_id

        reactor.callLater(5, _set_id_channel, id_tmp)

        self.boosting_manager.add_source(dispersy_cid)
        chn_obj = self.boosting_manager.get_source_object(dispersy_cid)

        chn_obj._load_torrent = self._load

        def clean_community(_):
            """
            cleanly exit the community we are in
            """
            if chn_obj.community:
                chn_obj.community.cancel_all_pending_tasks()

            chn_obj.kill_tasks()

        d = self.check_source(dispersy_cid)
        d.addCallback(clean_community)
        return d
Exemplo n.º 7
0
    def test_chn_native_load(self):
        self.session.get_dispersy = lambda: True
        self.session.lm.dispersy = Dispersy(ManualEnpoint(0),
                                            self.getStateDir())
        dispersy_cid_hex = "abcd" * 9 + "0012"
        dispersy_cid = binascii.unhexlify(dispersy_cid_hex)

        # create channel and insert torrent
        self.create_fake_allchannel_community()
        self.create_torrents_in_channel(dispersy_cid_hex)

        self.session.download_torrentfile = \
            lambda dummy_ihash, function, _: function(binascii.hexlify(TORRENT_UBUNTU_FILE_INFOHASH))

        def get_bin_torrent(_):
            """
            get binary data of a torrent
            """
            f = open(TORRENT_UBUNTU_FILE, "rb")
            bdata = f.read()
            f.close()
            return bdata

        self.session.get_collected_torrent = get_bin_torrent

        self.boosting_manager.add_source(dispersy_cid)

        def _loop_check(_):
            defer_param = defer.Deferred()

            def check_loaded(src):
                """
                check if a torrent has been loaded
                """
                src_obj = self.boosting_manager.get_source_object(src)
                if src_obj.loaded_torrent[
                        TORRENT_UBUNTU_FILE_INFOHASH] is not None:
                    src_obj.community.cancel_all_pending_tasks()
                    src_obj.kill_tasks()
                    self.check_loaded_lc.stop()
                    self.check_loaded_lc = None
                    defer_param.callback(src)

            self.check_loaded_lc = LoopingCall(check_loaded, dispersy_cid)
            self.check_loaded_lc.start(1, now=True)

            return defer_param

        defer_ret = self.check_source(dispersy_cid)
        defer_ret.addCallback(_loop_check)

        return defer_ret
Exemplo n.º 8
0
    def setUp(self):
        """
        The startup method of this class creates a fake Dispersy instance with a fake AllChannel community. It also
        inserts some random channels so we have some data to work with.
        """
        yield super(TestChannelsSubscriptionEndpoint, self).setUp()
        self.expected_votecast_cid = None
        self.expected_votecast_vote = None
        self.create_votecast_called = False

        fake_community = self.create_fake_allchannel_community()
        fake_community.disp_create_votecast = self.on_dispersy_create_votecast
        self.session.config.get_dispersy_enabled = lambda: True
        self.session.lm.dispersy = Dispersy(ManualEnpoint(0), self.getStateDir())
        self.session.lm.dispersy.attach_community(fake_community)
        for i in xrange(0, 10):
            self.insert_channel_in_db('rand%d' % i, 42 + i, 'Test channel %d' % i, 'Test description %d' % i)
Exemplo n.º 9
0
 def setUp(self, annotate=True):
     yield super(AbstractTestCommunity, self).setUp(annotate=annotate)
     self.dispersy = Dispersy(ManualEnpoint(0), self.getStateDir())
     self.dispersy._database.open()
     self.master_member = DummyMember(self.dispersy, 1, "a" * 20)
     self.member = self.dispersy.get_new_member(u"curve25519")
Exemplo n.º 10
0
    def test_chn_max_torrents(self):
        """
        Test the restriction of max_torrents in a source.
        """
        self.session.get_dispersy = lambda: True
        self.session.lm.dispersy = Dispersy(ManualEnpoint(0),
                                            self.getStateDir())
        dispersy_cid_hex = "abcd" * 9 + "0012"
        dispersy_cid = binascii.unhexlify(dispersy_cid_hex)

        # create channel and insert torrent
        self.create_fake_allchannel_community()
        self.create_torrents_in_channel(dispersy_cid_hex)

        pioneer_file = os.path.join(
            TESTS_DATA_DIR, "Pioneer.One.S01E06.720p.x264-VODO.torrent")
        pioneer_tdef = TorrentDef.load(pioneer_file)
        pioneer_ihash = binascii.unhexlify(
            "66ED7F30E3B30FA647ABAA19A36E7503AA071535")

        torrent_list = [[
            self.channel_id, 1, 1, pioneer_ihash, 1460000001, pioneer_file,
            pioneer_tdef.get_files_as_unicode_with_length(),
            pioneer_tdef.get_trackers_as_single_tuple()
        ]]
        self.insert_torrents_into_channel(torrent_list)

        self.boosting_manager.add_source(dispersy_cid)
        chn_obj = self.boosting_manager.get_source_object(dispersy_cid)
        chn_obj.max_torrents = 2
        chn_obj.torrent_mgr.load_torrent = lambda dummy_1, dummy_2: None

        def _load(torrent, callback=None):
            if not isinstance(torrent, CollectedTorrent):
                torrent_id = 0
                if torrent.torrent_id <= 0:
                    torrent_id = self.session.lm.torrent_db.getTorrentID(
                        torrent.infohash)
                if torrent_id:
                    torrent.update_torrent_id(torrent_id)

                infohash_str = binascii.hexlify(torrent.infohash)
                torrent = CollectedTorrent(
                    torrent, self.tdef
                    if infohash_str.startswith("fc") else pioneer_tdef)
            if callback is not None:
                callback(torrent)
            else:
                return torrent

        def activate_mgr():
            """
            activate ltmgr and adjust max torrents to emulate overflow torrents
            """
            chn_obj.max_torrents = 1
            chn_obj.torrent_mgr.load_torrent = _load

        reactor.callLater(5, activate_mgr)

        def check_torrents_channel(src, defer_param=None):
            """
            check if a torrent already in channel and ready to download
            """
            if defer_param is None:
                defer_param = defer.Deferred()

            src_obj = self.boosting_manager.get_source_object(src)
            success = True
            if len(src_obj.unavail_torrent) == 0:
                self.assertLessEqual(len(src_obj.torrents),
                                     src_obj.max_torrents)
            else:
                success = False
                reactor.callLater(1, check_torrents_channel, src, defer_param)

            if success:
                src_obj.community.cancel_all_pending_tasks()
                src_obj.kill_tasks()
                defer_param.callback(src)

            return defer_param

        d = self.check_source(dispersy_cid)
        d.addCallback(check_torrents_channel)
        return d
Exemplo n.º 11
0
    def test_chn_lookup(self):
        """
        testing channel source.

        It includes finding and downloading actual torrent
        """
        self.session.get_dispersy = lambda: True
        self.session.lm.dispersy = Dispersy(ManualEnpoint(0),
                                            self.getStateDir())
        dispersy_cid_hex = "abcd" * 9 + "0012"
        dispersy_cid = binascii.unhexlify(dispersy_cid_hex)

        # create channel and insert torrent
        self.create_fake_allchannel_community()
        self.create_torrents_in_channel(dispersy_cid_hex)

        self.boosting_manager.add_source(dispersy_cid)
        chn_obj = self.boosting_manager.get_source_object(dispersy_cid)

        def _load(torrent, callback=None):
            if not isinstance(torrent, CollectedTorrent):
                torrent_id = 0
                if torrent.torrent_id <= 0:
                    torrent_id = self.session.lm.torrent_db.getTorrentID(
                        torrent.infohash)
                if torrent_id:
                    torrent.update_torrent_id(torrent_id)

                torrent = CollectedTorrent(torrent, self.tdef)
            if callback is not None:
                callback(torrent)
            else:
                return torrent

        def check_torrents_channel(src, defer_param=None, target=1):
            """
            check if a torrent already in channel and ready to download
            """
            if defer_param is None:
                defer_param = defer.Deferred()

            src_obj = self.boosting_manager.get_source_object(src)
            success = True
            if not src_obj or len(src_obj.torrents) < target:
                success = False
                reactor.callLater(1,
                                  check_torrents_channel,
                                  src,
                                  defer_param,
                                  target=target)
            elif not self.boosting_manager.torrents.get(
                    TORRENT_FILE_INFOHASH, None):
                success = False
                reactor.callLater(1,
                                  check_torrents_channel,
                                  src,
                                  defer_param,
                                  target=target)
            elif not self.boosting_manager.torrents[TORRENT_FILE_INFOHASH].get(
                    'download', None):
                success = False
                reactor.callLater(1,
                                  check_torrents_channel,
                                  src,
                                  defer_param,
                                  target=target)

            if success:
                self.boosting_manager.set_enable_mining(src,
                                                        False,
                                                        force_restart=True)
                if src_obj.community:
                    src_obj.community.cancel_all_pending_tasks()

                defer_param.callback(src)

            return defer_param

        chn_obj.torrent_mgr.load_torrent = _load

        d = self.check_source(dispersy_cid)
        d.addCallback(check_torrents_channel, target=1)
        return d
Exemplo n.º 12
0
    def test_chn_lookup(self):
        """
        testing channel source.

        It includes finding and downloading actual torrent
        """
        self.session.get_dispersy = lambda: True
        self.session.lm.dispersy = Dispersy(ManualEnpoint(0),
                                            self.getStateDir())
        dispersy_cid_hex = "abcd" * 9 + "0012"
        dispersy_cid = binascii.unhexlify(dispersy_cid_hex)

        # create channel and insert torrent
        self.create_fake_allchannel_community()
        self.create_torrents_in_channel(dispersy_cid_hex)

        self.boosting_manager.add_source(dispersy_cid)
        chn_obj = self.boosting_manager.get_source_object(dispersy_cid)

        def check_torrents_channel(src, defer_param=None, target=1):
            """
            check if a torrent already in channel and ready to download
            """
            if defer_param is None:
                defer_param = defer.Deferred()

            src_obj = self.boosting_manager.get_source_object(src)
            success = True
            if not src_obj or len(src_obj.torrents) < target:
                success = False
                reactor.callLater(1,
                                  check_torrents_channel,
                                  src,
                                  defer_param,
                                  target=target)
            elif not self.boosting_manager.torrents.get(
                    TORRENT_UBUNTU_FILE_INFOHASH, None):
                success = False
                reactor.callLater(1,
                                  check_torrents_channel,
                                  src,
                                  defer_param,
                                  target=target)
            elif not self.boosting_manager.torrents[
                    TORRENT_UBUNTU_FILE_INFOHASH].get('download', None):
                success = False
                reactor.callLater(1,
                                  check_torrents_channel,
                                  src,
                                  defer_param,
                                  target=target)

            if success:
                self.boosting_manager.set_enable_mining(src,
                                                        False,
                                                        force_restart=True)
                if src_obj.community:
                    src_obj.community.cancel_all_pending_tasks()

                self.assertEqual(src_obj.get_source_text(), 'Simple Channel')

                defer_param.callback(src)

            return defer_param

        chn_obj._load_torrent = self._load

        d = self.check_source(dispersy_cid)
        d.addCallback(check_torrents_channel, target=1)
        return d