async def run(self): await super().run() config = self.session.config key_component = await self.require_component(KeyComponent) socks_ports = [] if not config.gui_test_mode: socks_servers_component = await self.require_component( SocksServersComponent) socks_ports = socks_servers_component.socks_ports self.download_manager = DownloadManager( config=config.libtorrent, state_dir=config.state_dir, notifier=self.session.notifier, peer_mid=key_component.primary_key.key_to_hash(), download_defaults=config.download_defaults, bootstrap_infohash=config.bootstrap.infohash, socks_listen_ports=socks_ports, dummy_mode=config.gui_test_mode) self.download_manager.initialize() await self.download_manager.load_checkpoints() if config.gui_test_mode: from tribler_core.tests.tools.common import TORRENT_WITH_DIRS # pylint: disable=import-outside-toplevel uri = path_to_uri(TORRENT_WITH_DIRS) await self.download_manager.start_download_from_uri(uri)
async def test_download_torrent_from_file_with_escaped_characters( download_manager, tmp_path): destination = tmp_path / 'ubuntu%20%21 15.04.torrent' shutil.copyfile(TORRENT_UBUNTU_FILE, destination) uri = path_to_uri(destination) d = await download_manager.start_download_from_uri(uri) await d.wait_for_status(DLSTATUS_DOWNLOADING)
def test_add_download_url(window): go_to_and_wait_for_downloads(window) window.on_add_torrent_from_url() screenshot(window, name="add_torrent_url_dialog") uri = path_to_uri(TORRENT_WITH_DIRS) window.dialog.dialog_widget.dialog_input.setText(uri) QTest.mouseClick(window.dialog.buttons[0], Qt.LeftButton) QTest.qWait(200) screenshot(window, name="add_torrent_url_startdownload_dialog") # set the download directory to a writable path download_dir = os.path.join(os.path.expanduser("~"), "downloads") window.dialog.dialog_widget.destination_input.setCurrentText(download_dir) dfl = window.dialog.dialog_widget.files_list_view wait_for_list_populated(dfl) item = dfl.topLevelItem(0) item2 = item.child(0) dfl.expand(dfl.indexFromItem(item2)) clickItem(dfl, item2, CHECKBOX_COL) screenshot(window, name="add_torrent_url_startdownload_dialog_files") QTest.mouseClick(window.dialog.dialog_widget.download_button, Qt.LeftButton) wait_for_signal(window.downloads_page.received_downloads)
async def test_get_torrentinfo_escaped_characters(tmp_path, rest_api): # test for the bug fix: https://github.com/Tribler/tribler/issues/6700 source = TORRENT_UBUNTU_FILE destination = tmp_path / 'ubuntu%20%21 15.04.torrent' shutil.copyfile(source, destination) uri = path_to_uri(destination) response = await do_request(rest_api, url='torrentinfo', params={'uri': uri}, expected_code=200) assert 'metainfo' in response
async def test_start_download_from_file(test_download, mock_dlmgr, rest_api): """ Testing whether we can start a download from a file """ mock_dlmgr.start_download_from_uri = lambda *_, **__: succeed(test_download ) uri = path_to_uri(TESTS_DATA_DIR / 'video.avi.torrent') expected_json = { 'started': True, 'infohash': 'c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5' } await do_request(rest_api, 'downloads', expected_code=200, request_type='PUT', post_data={'uri': uri}, expected_json=expected_json)
async def test_start_download_with_selected_files(test_download, mock_dlmgr, rest_api): """ Testing whether we can start a download with the selected_files parameter set """ def mocked_start_download(*_, config=None): assert config.get_selected_files() == [0] return succeed(test_download) mock_dlmgr.start_download_from_uri = mocked_start_download uri = path_to_uri(TESTS_DATA_DIR / 'video.avi.torrent') post_data = {'uri': uri, 'selected_files': [0]} expected_json = { 'started': True, 'infohash': 'c9a19e7fe5d9a6c106d6ea3c01746ac88ca3c7a5' } await do_request(rest_api, 'downloads', expected_code=200, request_type='PUT', post_data=post_data, expected_json=expected_json)
def parse_sys_args(self, args): for arg in args[1:]: if os.path.exists(arg): file_path = ensure_unicode(arg, 'utf8') uri = path_to_uri(file_path) self.handle_uri(uri) elif arg.startswith('magnet'): self.handle_uri(arg) if '--allow-code-injection' in sys.argv[1:]: variables = globals().copy() variables.update(locals()) variables['window'] = self.activation_window() self.code_executor = CodeExecutor(5500, shell_variables=variables) connect(self.activation_window().tribler_crashed, self.code_executor.on_crash) if '--testnet' in sys.argv[1:]: os.environ['TESTNET'] = "YES" if '--trustchain-testnet' in sys.argv[1:]: os.environ['TRUSTCHAIN_TESTNET'] = "YES" if '--chant-testnet' in sys.argv[1:]: os.environ['CHANT_TESTNET'] = "YES" if '--tunnel-testnet' in sys.argv[1:]: os.environ['TUNNEL_TESTNET'] = "YES"
def test_uri_is_valid_file(tmpdir): file_path = tmpdir / '1.txt' file_path.write('test') file_uri = path_to_uri(file_path) assert uri_is_valid_file(file_uri) assert not uri_is_valid_file(file_uri + '/*')
def test_path_to_uri_win(path, uri): assert path_to_uri(path) == uri
async def test_download_torrent_from_file(download_manager): uri = path_to_uri(TORRENT_UBUNTU_FILE) d = await download_manager.start_download_from_uri(uri) await d.wait_for_status(DLSTATUS_DOWNLOADING)
def event(self, event): if event.type() == QEvent.FileOpen and event.file().endswith(".torrent"): uri = path_to_uri(event.file()) self.handle_uri(uri) return QtSingleApplication.event(self, event)
def _path(file): return path_to_uri(TESTS_DATA_DIR / file)
def run_gui(api_port, api_key, root_state_dir, parsed_args): logger.info('Running GUI' + ' in gui_test_mode' if parsed_args.gui_test_mode else '') # Workaround for macOS Big Sur, see https://github.com/Tribler/tribler/issues/5728 if sys.platform == "darwin": logger.info('Enabling a workaround for macOS Big Sur') os.environ["QT_MAC_WANTS_LAYER"] = "1" # Workaround for Ubuntu 21.04+, see https://github.com/Tribler/tribler/issues/6701 elif sys.platform == "linux": logger.info( 'Enabling a workaround for Ubuntu 21.04+ wayland environment') os.environ["GDK_BACKEND"] = "x11" # Set up logging load_logger_config('tribler-gui', root_state_dir) # Enable tracer using commandline args: --trace-debug or --trace-exceptions trace_logger = check_and_enable_code_tracing('gui', root_state_dir) try: enable_fault_handler(root_state_dir) # Exit if we cant read/write files, etc. check_environment() check_free_space() app_name = os.environ.get('TRIBLER_APP_NAME', 'triblerapp') app = TriblerApplication(app_name, sys.argv) # Note (@ichorid): translator MUST BE created and assigned to a separate variable # before calling installTranslator on app. Otherwise, it won't work for some reason settings = QSettings('nl.tudelft.tribler') translator = get_translator(settings.value('translation', None)) app.installTranslator(translator) if app.is_running(): # if an application is already running, then send the command line # argument to it and close the current instance logger.info( 'GUI Application is already running. Passing a torrent file path to it.' ) for arg in sys.argv[1:]: if os.path.exists(arg) and arg.endswith(".torrent"): app.send_message(path_to_uri(arg)) elif arg.startswith('magnet'): app.send_message(arg) logger.info('Close the current application.') sys.exit(1) logger.info('Start Tribler Window') window = TriblerWindow(settings, root_state_dir, api_port=api_port, api_key=api_key) window.setWindowTitle("Tribler") app.set_activation_window(window) app.parse_sys_args(sys.argv) sys.exit(app.exec_()) except ImportError as ie: logger.exception(ie) error_and_exit("Import Error", f"Import error: {ie}") except TriblerException as te: logger.exception(te) error_and_exit("Tribler Exception", f"{te}") except SystemExit: logger.info("Shutting down Tribler") if trace_logger: trace_logger.close() # Flush all the logs to make sure it is written to file before it exits for handler in logging.getLogger().handlers: handler.flush() gui_sentry_reporter.global_strategy = SentryStrategy.SEND_SUPPRESSED raise