async def setup_tunnel_seeder(self, hops): """ Setup the seeder. """ from tribler_core.session import Session self.seed_config = self.config.copy() self.seed_config._state_dir = self.getRootStateDir(2) self.seed_config.set_libtorrent_enabled(hops == 0) self.seed_config.set_tunnel_community_socks5_listen_ports( self.get_ports(5)) if self.session2 is None: self.session2 = Session(self.seed_config) self.session2.upgrader_enabled = False await self.session2.start() tdef = TorrentDef() tdef.add_content(TESTS_DATA_DIR / "video.avi") tdef.set_tracker("http://localhost/announce") torrentfn = self.session2.config.get_state_dir() / "gen.torrent" tdef.save(torrent_filepath=torrentfn) self.seed_tdef = tdef if hops > 0: # Safe seeding enabled self.tunnel_community_seeder = await self.load_tunnel_community_in_session( self.session2, start_lt=True) self.tunnel_community_seeder.build_tunnels(hops) else: await self.sanitize_network(self.session2) dscfg = DownloadConfig() dscfg.set_dest_dir( TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = self.session2.dlmgr.start_download(tdef=tdef, config=dscfg) d.set_state_callback(self.seeder_state_callback)
def test_tdef(state_dir): tdef = TorrentDef() sourcefn = TESTS_DATA_DIR / 'video.avi' tdef.add_content(sourcefn) tdef.set_tracker("http://localhost/announce") torrentfn = state_dir / "gen.torrent" tdef.save(torrentfn) return tdef
def test_add_single_file(self): """ Test whether adding a single file to a torrent is working correctly """ t = TorrentDef() torrent_dir = TESTS_DATA_DIR / "contentdir" t.add_content(torrent_dir / "file.txt") t.save() metainfo = t.get_metainfo() self.assertEqual(metainfo[b'info'][b'name'], b'file.txt')
def create_tdef(self): """ create and save torrent definition used in this test file """ tdef = TorrentDef() sourcefn = TESTS_DATA_DIR / 'video.avi' tdef.add_content(sourcefn) tdef.set_tracker("http://localhost/announce") torrentfn = self.session.config.get_state_dir() / "gen.torrent" tdef.save(torrentfn) return tdef
def test_add_content_dir(self): """ Test whether adding a single content directory with two files is working correctly """ t = TorrentDef() torrent_dir = TESTS_DATA_DIR / "contentdir" t.add_content(torrent_dir / "file.txt") t.add_content(torrent_dir / "otherfile.txt") t.save() metainfo = t.get_metainfo() self.assertEqual(len(metainfo[b'info'][b'files']), 2)
def test_add_content_piece_length(self): """ Add a single file with piece length to a TorrentDef """ t = TorrentDef() fn = TESTS_DATA_DIR / VIDEO_FILE_NAME t.add_content(fn) t.set_piece_length(2**16) t.save() metainfo = t.get_metainfo() self.assertEqual(metainfo[b'info'][b'piece length'], 2**16)
async def test_multifile_torrent(self): # Achtung! This test is completely and utterly broken, as is the whole libtorrent wrapper! # Don't try to understand it, it is a legacy thing! tdef = TorrentDef() tdef.add_content(TESTS_DATA_DIR / "video.avi") tdef.set_tracker("http://tribler.org/announce") tdef.save() fake_handler = MockObject() fake_handler.is_valid = lambda: True fake_handler.status = lambda: fake_status fake_handler.set_share_mode = lambda _: None fake_handler.set_priority = lambda _: None fake_handler.set_sequential_download = lambda _: None fake_handler.resume = lambda: None fake_handler.set_max_connections = lambda _: None fake_handler.apply_ip_filter = lambda _: None fake_handler.save_resume_data = lambda: None fake_status = MockObject() fake_status.share_mode = False dl = Download(self.session, tdef) dl.set_selected_files = lambda: None dl.future_added = succeed(fake_handler) # Create a dummy download config dl.config = DownloadConfig() dl.config.set_engineresumedata({ b"save_path": path_util.abspath(self.state_dir), b"info-hash": b'\x00' * 20 }) dl.setup() dl.config.set_engineresumedata({ b"save_path": path_util.abspath(self.state_dir), b"info-hash": b'\x00' * 20 }) dl.setup() dl.config.set_engineresumedata({ b"save_path": "some_local_dir", b"info-hash": b'\x00' * 20 }) dl.setup() await dl.shutdown()
def create_dummy_sql_dumb(file_name): """ Create a TorrentDef with a dummy sql dumb file :param file_name: full path to the file :return: tdef """ with open(file_name, 'w') as fp: fp.write("BEGIN TRANSACTION;") fp.write("CREATE TABLE IF NOT EXISTS option(key TEXT PRIMARY KEY, value BLOB);") fp.write("INSERT OR REPLACE INTO option(key, value) VALUES('database_version', '0');") fp.write("COMMIT;") tdef = TorrentDef() tdef.add_content(file_name) tdef.set_piece_length(2 ** 16) tdef.save() return tdef
async def add_torrent(self, piece_length=1024): [srchandle, sourcefn] = mkstemp(dir=TESTS_DIR) data = b''.join([i.to_bytes(2, byteorder='big') for i in range(1000)]) os.write(srchandle, data) os.close(srchandle) tdef = TorrentDef() tdef.add_content(sourcefn) tdef.set_piece_length(piece_length) torrentfn = self.session.config.get_state_dir() / "gen.torrent" tdef.save(torrentfn) dscfg = DownloadConfig() destdir = Path(sourcefn).parent dscfg.set_dest_dir(destdir) download = self.session.dlmgr.start_download(tdef=tdef, config=dscfg) await download.wait_for_status(DLSTATUS_SEEDING) return tdef.get_infohash(), data
async def add_torrent(self): [srchandle, sourcefn] = mkstemp(dir=TESTS_DIR) self.data = b'\xFF' * self.size # pylint: disable=attribute-defined-outside-init os.write(srchandle, self.data) os.close(srchandle) tdef = TorrentDef() tdef.add_content(sourcefn) tdef.set_piece_length(self.piece_len) torrentfn = self.session.config.get_state_dir() / "gen.torrent" tdef.save(torrentfn) dscfg = DownloadConfig() destdir = Path(sourcefn).parent dscfg.set_dest_dir(destdir) self.download = self.session.dlmgr.start_download(tdef=tdef, config=dscfg) # pylint: disable=attribute-defined-outside-init await self.download.wait_for_status(DLSTATUS_SEEDING) self.infohash = tdef.get_infohash() # pylint: disable=attribute-defined-outside-init