def create_fake_download_and_state(): """ Create a fake download and state which can be passed to the global download callback. """ tdef = TorrentDef() tdef.get_infohash = lambda: b'aaaa' fake_peer = {'extended_version': 'Tribler', 'id': 'a' * 20, 'dtotal': 10 * 1024 * 1024} fake_download = Mock() fake_download.get_def = lambda: tdef fake_download.get_def().get_name_as_unicode = lambda: "test.iso" fake_download.get_peerlist = lambda: [fake_peer] fake_download.hidden = False fake_download.checkpoint = lambda: succeed(None) fake_download.stop = lambda: succeed(None) fake_download.shutdown = lambda: succeed(None) dl_state = Mock() dl_state.get_infohash = lambda: b'aaaa' dl_state.get_status = lambda: DLSTATUS_SEEDING dl_state.get_download = lambda: fake_download fake_config = Mock() fake_config.get_hops = lambda: 0 fake_config.get_safe_seeding = lambda: True fake_download.config = fake_config return fake_download, dl_state
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