def test_add_content_file_and_copy(self): """ Add a single file to a TorrentDef """ t = TorrentDef() fn = os.path.join(TESTS_DATA_DIR, self.VIDEO_FILE_NAME) t.add_content(fn) t.set_tracker(TRACKER) t.finalize() s = os.path.getsize(fn) metainfo = t.get_metainfo() self.general_check(metainfo) self.assertEqual(metainfo['info']['name'], self.VIDEO_FILE_NAME) self.assertEqual(metainfo['info']['length'], s) self.assertTrue(t.get_pieces()) self.assertEqual(len(t.get_infohash()), INFOHASH_LENGTH) self.assertTrue(t.get_name()) # test copy constructor nt = TorrentDef(t.input, t.metainfo, t.infohash) self.assertEqual(nt.input, t.input) self.assertEqual(nt.metainfo, t.metainfo) self.assertEqual(nt.infohash, t.infohash) # test removing content nt.remove_content("/test123") self.assertEqual(len(nt.input['files']), 1) nt.remove_content(unicode(fn)) self.assertEqual(len(nt.input['files']), 0) nt.remove_content(unicode(fn))
def test_get_name_utf8(self): """ Check whether we can successfully get the UTF-8 encoded torrent name when using a different encoding """ t = TorrentDef() t.set_name('\xA1\xC0') self.assertEqual(t.get_name_utf8(), u'\xa1\xc0')
def setup_tunnel_seeder(self, hops): """ Setup the seeder. """ from Tribler.Core.Session import Session self.seed_config = self.config.copy() self.seed_config.set_state_dir(self.getStateDir(2)) self.seed_config.set_megacache_enabled(True) self.seed_config.set_tunnel_community_socks5_listen_ports(self.get_socks5_ports()) if self.session2 is None: self.session2 = Session(self.seed_config, ignore_singleton=True, autoload_discovery=False) self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://localhost/announce") tdef.finalize() torrentfn = os.path.join(self.session2.config.get_state_dir(), "gen.torrent") tdef.save(torrentfn) self.seed_tdef = tdef if hops > 0: # Safe seeding enabled self.tunnel_community_seeder = self.load_tunnel_community_in_session(self.session2) self.tunnel_community_seeder.build_tunnels(hops) from twisted.internet import reactor, task while not list(self.tunnel_community_seeder.active_data_circuits()): yield task.deferLater(reactor, .05, lambda: None) dscfg = DownloadStartupConfig() dscfg.set_dest_dir(TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = self.session2.start_download_from_tdef(tdef, dscfg) d.set_state_callback(self.seeder_state_callback)
def subtest_add_content_dir(self): """ Add a single dir to a TorrentDef """ t = TorrentDef() dn = os.path.join(TESTS_API_DIR, "contentdir") t.add_content(dn, "dirintorrent") t.set_tracker(TRACKER) t.finalize() exps = 0 for f in os.listdir(dn): if f.startswith('.'): continue p = os.path.join(dn, f) s = os.path.getsize(p) exps += s print "test: expected size", f, s print "test: expected total size of files in torrent", exps metainfo = t.get_metainfo() self.general_check(metainfo) self.assert_(metainfo['info']['name'] == 'dirintorrent') reals = 0 for file in metainfo['info']['files']: s = file['length'] print "test: real size", file['path'], s reals += s print "test: real size of files in torrent", reals self.assert_(exps == reals)
def test_multifile_torrent(self): tdef = TorrentDef() dn = os.path.join(TESTS_DATA_DIR, "contentdir") tdef.add_content(dn, "dirintorrent") fn = os.path.join(TESTS_DATA_DIR, "video.avi") tdef.add_content(fn, os.path.join("dirintorrent", "video.avi")) tdef.set_tracker("http://tribler.org/announce") tdef.finalize() impl = LibtorrentDownloadImpl(self.session, tdef) # Override the add_torrent because it will be called impl.ltmgr = MockObject() impl.ltmgr.add_torrent = lambda _, _dummy2: fake_handler impl.set_selected_files = lambda: None fake_handler = MockObject() fake_handler.is_valid = lambda: True fake_handler.status = lambda: fake_status fake_handler.set_share_mode = lambda _: None fake_handler.resume = lambda: None fake_handler.resolve_countries = lambda _: None fake_status = MockObject() fake_status.share_mode = False # Create a dummy download config impl.dlconfig = DownloadStartupConfig().dlconfig.copy() # Create a dummy pstate pstate = CallbackConfigParser() pstate.add_section("state") test_dict = dict() test_dict["a"] = "b" pstate.set("state", "engineresumedata", test_dict) return impl.network_create_engine_wrapper(pstate)
def test_readd_download_safe_seeding(self): """ Test whether a download is re-added when doing safe seeding """ readd_deferred = Deferred() def mocked_update_download_hops(*_): readd_deferred.callback(None) self.lm.update_download_hops = mocked_update_download_hops tdef = TorrentDef() tdef.get_infohash = lambda: 'aaaa' fake_download = MockObject() fake_download.get_def = lambda: tdef fake_download.get_def().get_name_as_unicode = lambda: "test.iso" fake_download.get_hops = lambda: 0 fake_download.get_safe_seeding = lambda: True dl_state = MockObject() dl_state.get_infohash = lambda: 'aaaa' dl_state.get_status = lambda: DLSTATUS_SEEDING dl_state.get_download = lambda: fake_download self.lm.downloads = {'aaaa': fake_download} self.lm.sesscb_states_callback([dl_state]) return readd_deferred
def test_add_content_dir_and_file(self): """ Add a single dir and single file to a TorrentDef """ t = TorrentDef() dn = os.path.join(TESTS_DATA_DIR, "contentdir") t.add_content(dn, "dirintorrent") fn = os.path.join(TESTS_DATA_DIR, self.VIDEO_FILE_NAME) t.add_content(fn, os.path.join("dirintorrent", self.VIDEO_FILE_NAME)) t.set_tracker(TRACKER) t.finalize() # Check exps = os.path.getsize(fn) for f in os.listdir(dn): if f.startswith('.'): continue p = os.path.join(dn, f) exps += os.path.getsize(p) metainfo = t.get_metainfo() self.general_check(metainfo) self.assertEqual(metainfo['info']['name'], 'dirintorrent') reals = 0 for file in metainfo['info']['files']: reals += file['length'] self.assertEqual(exps, reals)
def test_dlstates_cb_seeding(self): """ Testing whether a download is readded when safe seeding in the download states callback in LaunchManyCore """ readd_deferred = Deferred() def mocked_start_download(tdef, dscfg): self.assertEqual(tdef, seed_tdef) self.assertEqual(dscfg, seed_download) readd_deferred.callback(None) def mocked_remove_download(download): self.assertEqual(download, seed_download) self.lm.session.start_download_from_tdef = mocked_start_download self.lm.session.remove_download = mocked_remove_download seed_tdef = TorrentDef() seed_tdef.get_infohash = lambda: 'aaaa' seed_download = MockObject() seed_download.get_def = lambda: seed_tdef seed_download.get_def().get_name_as_unicode = lambda: "test.iso" seed_download.get_hops = lambda: 0 seed_download.get_safe_seeding = lambda: True seed_download.copy = lambda: seed_download seed_download.set_hops = lambda _: None fake_seed_download_state = MockObject() fake_seed_download_state.get_infohash = lambda: 'aaaa' fake_seed_download_state.get_status = lambda: DLSTATUS_SEEDING fake_seed_download_state.get_download = lambda: seed_download self.lm.sesscb_states_callback([fake_seed_download_state]) return readd_deferred
def setupSeeder(self): from Tribler.Core.Session import Session from Tribler.Core.TorrentDef import TorrentDef from Tribler.Core.DownloadConfig import DownloadStartupConfig self.setUpPreSession() self.config.set_libtorrent(True) self.config2 = self.config.copy() self.session2 = Session(self.config2, ignore_singleton=True) upgrader = self.session2.prestart() while not upgrader.is_done: time.sleep(0.1) assert not upgrader.failed, upgrader.current_status self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://fake.net/announce") tdef.finalize() torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent") tdef.save(torrentfn) dscfg = DownloadStartupConfig() dscfg.set_dest_dir( TESTS_DATA_DIR) # basedir of the file we are seeding d = self.session2.start_download(tdef, dscfg) d.set_state_callback(self.seeder_state_callback) return torrentfn
def test_add_content_dir(self): """ Add a single dir to a TorrentDef """ t = TorrentDef() dn = os.path.join(TESTS_DATA_DIR, "contentdir") t.add_content(dn, "dirintorrent") t.set_tracker(TRACKER) t.finalize() exps = 0 for f in os.listdir(dn): if f.startswith('.'): continue p = os.path.join(dn, f) s = os.path.getsize(p) exps += s self._logger.debug("Expected size %s %d", f, s) self._logger.debug("Expected total size of files in torrent %d", exps) metainfo = t.get_metainfo() self.general_check(metainfo) self.assertEqual(metainfo['info']['name'], 'dirintorrent') reals = 0 for file in metainfo['info']['files']: s = file['length'] self._logger.debug("real size %s %d", file['path'], s) reals += s self._logger.debug("Real size of files in torrent %d", reals) self.assertEqual(exps, reals)
def _on_torrent_created(result): """ Success callback :param result: from create_torrent_file """ metainfo_dict = bdecode(result['metainfo']) # Download this torrent if specified if 'download' in request.args and len(request.args['download']) > 0 \ and request.args['download'][0] == "1": download_config = DownloadStartupConfig() download_config.set_dest_dir(result['base_path'] if len( file_path_list) == 1 else result['base_dir']) try: self.session.lm.ltmgr.start_download( tdef=TorrentDef(metainfo=metainfo_dict), dconfig=download_config) except DuplicateDownloadException: self._logger.warning( "The created torrent is already being downloaded.") request.write( json.dumps({"torrent": base64.b64encode(result['metainfo'])})) # If the above request.write failed, the request will have already been finished if not request.finished: request.finish()
def test_dlstates_cb_error(self): """ Testing whether a download is stopped on error in the download states callback in LaunchManyCore """ error_stop_deferred = Deferred() def mocked_stop(): error_stop_deferred.callback(None) error_tdef = TorrentDef() error_tdef.get_infohash = lambda: 'aaaa' fake_error_download = MockObject() fake_error_download.get_def = lambda: error_tdef fake_error_download.get_def().get_name_as_unicode = lambda: "test.iso" fake_error_download.stop = mocked_stop fake_error_state = MockObject() fake_error_state.get_infohash = lambda: 'aaaa' fake_error_state.get_error = lambda: "test error" fake_error_state.get_status = lambda: DLSTATUS_STOPPED_ON_ERROR fake_error_state.get_download = lambda: fake_error_download self.lm.downloads = {'aaaa': fake_error_download} self.lm.sesscb_states_callback([fake_error_state]) return error_stop_deferred
def test_tdef_init(self): """ Test initializing a TorrentDef object """ tdef_params = TorrentDef( torrent_parameters={'announce': 'http://test.com'}) self.assertIn('announce', tdef_params.torrent_parameters)
def setup_tunnel_seeder(self, hops): """ Setup the seeder. """ from Tribler.Core.Session import Session self.seed_config = self.config.copy() self.seed_config.set_state_dir(self.getStateDir(2)) self.seed_config.set_tunnel_community_socks5_listen_ports( self.get_socks5_ports()) if self.session2 is None: self.session2 = Session(self.seed_config) self.session2.start() tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://localhost/announce") torrentfn = os.path.join(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 = self.load_tunnel_community_in_session( self.session2) self.tunnel_community_seeder.build_tunnels(hops) else: self.sanitize_network(self.session2) dscfg = DownloadStartupConfig() dscfg.set_dest_dir( TESTS_DATA_DIR) # basedir of the file we are seeding dscfg.set_hops(hops) d = self.session2.start_download_from_tdef(tdef, dscfg) d.set_state_callback(self.seeder_state_callback)
def create_torrent(self): [srchandle, sourcefn] = mkstemp() self.content = Rand.rand_bytes(self.contentlen) os.write(srchandle, self.content) os.close(srchandle) self.tdef = TorrentDef() self.tdef.add_content(sourcefn) self.tdef.set_piece_length(self.piecelen) self.tdef.set_tracker("http://127.0.0.1:12/announce") self.tdef.finalize() torrentfn = os.path.join(self.session.config.get_state_dir(), "gen.torrent") self.tdef.save(torrentfn) dscfg = DownloadStartupConfig() destdir = os.path.dirname(sourcefn) dscfg.set_dest_dir(destdir) dscfg.set_mode(DLMODE_VOD) download = self.session.start_download_from_tdef(self.tdef, dscfg) download.set_state_callback(self.state_callback) self.session.set_download_states_callback(self.states_callback)
def generate_torrent(self): self.tdef = TorrentDef() self.sourcefn = os.path.join(TESTS_DATA_DIR, 'video.avi') self.tdef.add_content(self.sourcefn) self.tdef.set_tracker("http://localhost/announce") self.tdef.finalize() self.tdef.save(os.path.join(self.session.config.get_state_dir(), "gen.torrent"))
def test_get_name_utf8_unknown(self): """ Add a TorrentDef with non-utf8 encoding""" t = TorrentDef() t.set_name('\xA1\xC0') t.set_tracker(TRACKER) t.finalize() self.assertEqual(t.get_name_utf8(), u'\xa1\xc0')
def test_get_name_utf8_unknown(self): """ Test whether we can succesfully get the UTF-8 name """ t = TorrentDef() t.set_name('\xA1\xC0') t.torrent_parameters['encoding'] = 'euc_kr' self.assertEqual(t.get_name_utf8(), u'\xf7')
def createTorrent(self): tdef = TorrentDef() tdef.add_content(os.path.join(TESTS_DATA_DIR, "video.avi")) tdef.set_tracker("http://fake.net/announce") tdef.finalize() torrentfn = os.path.join(self.session.get_state_dir(), "gen.torrent") tdef.save(torrentfn) return torrentfn
def test_add_duplicate_download(self): """ Testing whether a DuplicateDownloadException is raised when a download is added twice """ self.lm.downloads = {"abcd": None} tdef = TorrentDef() tdef.metainfo_valid = True tdef.infohash = "abcd" self.lm.add(tdef, None)
def test_get_nr_pieces(self): """ Test getting the number of pieces from a TorrentDef """ tdef = TorrentDef() self.assertEqual(tdef.get_nr_pieces(), 0) tdef.metainfo = {'info': {'pieces': 'a' * 40}} self.assertEqual(tdef.get_nr_pieces(), 2)
def test_add_single_file(self): """ Test whether adding a single file to a torrent is working correctly """ t = TorrentDef() torrent_dir = os.path.join(TESTS_DATA_DIR, "contentdir") t.add_content(os.path.join(torrent_dir, "file.txt")) t.save() metainfo = t.get_metainfo() self.assertEqual(metainfo['info']['name'], 'file.txt')
def start_vod_download(self): self.tdef = TorrentDef() self.tdef.add_content(self.sourcefn) self.tdef.set_tracker("http://127.0.0.1:12/announce") self.tdef.finalize() dscfg = DownloadStartupConfig() dscfg.set_dest_dir(os.path.dirname(self.sourcefn)) download = self.session.start_download_from_tdef(self.tdef, dscfg) return download.get_handle()
def test_add_content_dir(self): """ Test whether adding a single content directory with two files is working correctly """ t = TorrentDef() torrent_dir = os.path.join(TESTS_DATA_DIR, "contentdir") t.add_content(os.path.join(torrent_dir, "file.txt")) t.add_content(os.path.join(torrent_dir, "otherfile.txt")) t.save() metainfo = t.get_metainfo() self.assertEqual(len(metainfo['info']['files']), 2)
def test_add_content_piece_length(self): """ Add a single file with piece length to a TorrentDef """ t = TorrentDef() fn = os.path.join(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['info']['piece length'], 2**16)
def test_set_trackers(self): t = TorrentDef() t.set_tracker_hierarchy([[ "http://tracker.org", "http://tracker2.org/", "http/tracker3.org" ]]) self.assertEqual(len(t.get_tracker_hierarchy()[0]), 2) self.assertEqual(t.get_tracker_hierarchy()[0][0], "http://tracker.org") self.assertEqual(t.get_tracker_hierarchy()[0][1], "http://tracker2.org") self.assertEqual(t.get_trackers_as_single_tuple(), ('http://tracker.org', 'http://tracker2.org'))
def test_add_content_piece_length(self): """ Add a single file with piece length to a TorrentDef """ t = TorrentDef() fn = os.path.join(TESTS_DATA_DIR, self.VIDEO_FILE_NAME) t.add_content(fn) t.set_piece_length(2**16) t.set_tracker(TRACKER) t.finalize() metainfo = t.get_metainfo() self.general_check(metainfo) self.assertEqual(metainfo['info']['piece length'], 2**16)
def subtest_add_content_piece_length(self): """ Add a single file with piece length to a TorrentDef """ t = TorrentDef() fn = os.path.join(TESTS_API_DIR, "video.avi") t.add_content(fn) t.set_piece_length(2**16) t.set_tracker(TRACKER) t.finalize() metainfo = t.get_metainfo() self.general_check(metainfo) self.assert_(metainfo['info']['piece length'] == 2**16)
def test_is_multifile(self): """ Test whether a TorrentDef is correctly classified as multifile torrent """ tdef = TorrentDef() self.assertFalse(tdef.is_multifile_torrent()) tdef.metainfo = {} self.assertFalse(tdef.is_multifile_torrent()) tdef.metainfo = {'info': {'files': ['a']}} self.assertTrue(tdef.is_multifile_torrent())
def register_file_stream(self): self.tdef = TorrentDef() self.tdef.add_content(self.sourcefn) self.tdef.set_tracker("http://127.0.0.1:12/announce") self.tdef.finalize() dscfg = DownloadStartupConfig() dscfg.set_dest_dir(os.path.dirname(self.sourcefn)) download = self.session.start_download(self.tdef, dscfg) while not download.handle: time.sleep(1)