def initialize(self): # Start upnp self.get_session().start_upnp() if has_bep33_support(): # Also listen to DHT log notifications - we need the dht_pkt_alert and extract the BEP33 bloom filters dht_health_session = self.get_session( self.tribler_session.config.get_default_number_hops()) dht_health_session.set_alert_mask( self.default_alert_mask | lt.alert.category_t.dht_log_notification) self.dht_health_manager = DHTHealthManager(dht_health_session) # Make temporary directory for metadata collecting through DHT self.metadata_tmpdir = mkdtemp(suffix='tribler_metainfo_tmpdir') # Register tasks self.register_task("process_alerts", self._task_process_alerts, interval=1) if self.dht_readiness_timeout > 0: self._dht_ready_task = self.register_task("check_dht_ready", self._check_dht_ready) self.register_task("request_torrent_updates", self._request_torrent_updates, interval=1) self.register_task('task_cleanup_metacache', self._task_cleanup_metainfo_cache, interval=60, delay=0) self.set_download_states_callback(self.sesscb_states_callback)
async def setUp(self): await super(TestDownloadManager, self).setUp() self.tribler_session = MockObject() self.tribler_session.notifier = Notifier() self.tribler_session.state_dir = self.session_base_dir self.tribler_session.trustchain_keypair = MockObject() self.tribler_session.trustchain_keypair.key_to_hash = lambda: b'a' * 20 self.tribler_session.notify_shutdown_state = lambda _: None self.tribler_session.config = MockObject() self.tribler_session.config.get_libtorrent_utp = lambda: True self.tribler_session.config.get_libtorrent_proxy_settings = lambda: (0, None, None) self.tribler_session.config.get_anon_proxy_settings = lambda: (2, ('127.0.0.1', [1338]), None) self.tribler_session.config.get_libtorrent_port = lambda: 1337 self.tribler_session.config.get_anon_listen_port = lambda: 1338 self.tribler_session.config.get_state_dir = lambda: self.session_base_dir self.tribler_session.config.set_listen_port_runtime = lambda: None self.tribler_session.config.get_libtorrent_max_upload_rate = lambda: 100 self.tribler_session.config.get_libtorrent_max_download_rate = lambda: 120 self.tribler_session.config.get_libtorrent_dht_enabled = lambda: False self.tribler_session.config.set_libtorrent_port_runtime = lambda _: None self.tribler_session.config.get_libtorrent_max_conn_download = lambda: 0 self.tribler_session.config.get_default_number_hops = lambda: 1 self.tribler_session.config.get_libtorrent_dht_readiness_timeout = lambda: 0 self.dlmgr = DownloadManager(self.tribler_session) self.dlmgr.metadata_tmpdir = mkdtemp(suffix=u'tribler_metainfo_tmpdir') self.tribler_session.dlmgr = self.dlmgr self.tribler_session.tunnel_community = None
async def test_checkpoint_after_metainfo_cancel(self): """ Test whether there is a checkpoint after starting a download that already a pending metainfo request """ dlcheckpoints_tempdir = mkdtemp(suffix='dlcheckpoints_tmpdir') self.dlmgr.tribler_session = self.tribler_session self.dlmgr.get_checkpoint_dir = lambda: dlcheckpoints_tempdir infohash = b'\x00' * 20 ensure_future(self.dlmgr.get_metainfo(infohash)) await sleep(.1) await self.dlmgr.start_download_from_uri(f'magnet:?xt=urn:btih:{hexlify(infohash)}&dn=name') await sleep(.1) self.assertTrue((dlcheckpoints_tempdir / f'{hexlify(infohash)}.conf').exists())
async def test_load_from_url_404(self): # Setup file server to serve torrent file self.session_base_dir = mkdtemp(suffix="_tribler_test_load_from_url") files_path = self.session_base_dir / 'http_torrent_files' os.mkdir(files_path) # Do not copy the torrent file to produce 404 file_server_port = self.get_port() await self.setUpFileServer(file_server_port, files_path) torrent_url = 'http://localhost:%d/ubuntu.torrent' % file_server_port try: await TorrentDef.load_from_url(torrent_url) except ClientResponseError as e: self.assertEqual(e.status, 404)
async def test_load_from_url(self): # Setup file server to serve torrent file self.session_base_dir = mkdtemp(suffix="_tribler_test_load_from_url") files_path = self.session_base_dir / 'http_torrent_files' os.mkdir(files_path) shutil.copyfile(TORRENT_UBUNTU_FILE, files_path / 'ubuntu.torrent') file_server_port = self.get_port() await self.setUpFileServer(file_server_port, files_path) torrent_url = 'http://localhost:%d/ubuntu.torrent' % file_server_port torrent_def = await TorrentDef.load_from_url(torrent_url) self.assertEqual(torrent_def.get_metainfo(), TorrentDef.load(TORRENT_UBUNTU_FILE).get_metainfo()) self.assertEqual(torrent_def.infohash, TorrentDef.load(TORRENT_UBUNTU_FILE).infohash)
async def test_save_resume_preresolved_magnet(self): """ Test whether a magnet link correctly writes save-resume data before it is resolved. This can happen when a magnet link is added when the user does not have internet. """ self.dlmgr.initialize() dlcheckpoints_tempdir = mkdtemp(suffix=u'dlcheckpoints_tmpdir') self.dlmgr.get_download = lambda _: None self.dlmgr.tribler_session = self.tribler_session self.dlmgr.get_checkpoint_dir = lambda: dlcheckpoints_tempdir self.tribler_session.dlmgr = self.dlmgr download = await self.dlmgr.start_download_from_uri("magnet:?xt=urn:btih:" + ('1' * 40)) basename = hexlify(download.get_def().get_infohash()) + '.conf' filename = self.dlmgr.get_checkpoint_dir() / basename self.assertTrue(filename.is_file())
def create_node(self): mock_ipv8 = MockIPv8( "curve25519", TriblerTunnelCommunity, socks_listen_ports=[], settings={'remove_tunnel_delay': 0}, exitnode_cache=mkdtemp(suffix="_tribler_test_cache") / 'cache.dat') mock_ipv8.overlay.settings.max_circuits = 1 # Load the bandwidth accounting community mock_ipv8.overlay.bandwidth_community = BandwidthAccountingCommunity( mock_ipv8.my_peer, mock_ipv8.endpoint, mock_ipv8.network, database_path=":memory:") mock_ipv8.overlay.dht_provider = MockDHTProvider( Peer(mock_ipv8.overlay.my_peer.key, mock_ipv8.overlay.my_estimated_wan)) return mock_ipv8
def create_node(self): mock_ipv8 = MockIPv8( u"curve25519", TriblerTunnelCommunity, socks_listen_ports=[], exitnode_cache=mkdtemp(suffix="_tribler_test_cache") / 'cache.dat') mock_ipv8.overlay.settings.max_circuits = 1 # Load the TrustChain community mock_ipv8.trustchain = TrustChainCommunity( mock_ipv8.my_peer, mock_ipv8.endpoint, mock_ipv8.network, working_directory=u":memory:") mock_ipv8.overlay.bandwidth_wallet = TrustchainWallet( mock_ipv8.trustchain) mock_ipv8.overlay.dht_provider = MockDHTProvider( Peer(mock_ipv8.overlay.my_peer.key, mock_ipv8.overlay.my_estimated_wan)) return mock_ipv8