async def test_get_peers_illegal_fields_ascii(enable_chant, enable_api, test_download, mock_dlmgr, mock_lt_status, session): """ Testing whether illegal fields are stripped from the Libtorrent download info response. """ session.dlmgr.get_downloads = lambda: [test_download] ds = DownloadState(test_download, mock_lt_status, None) ds.get_peerlist = lambda: [{ 'id': '1234', 'have': '5678', 'extended_version': 'uTorrent 1.6.1' }] test_download.get_state = lambda: ds response_dict = await do_request(session, 'downloads?get_peers=1&get_pieces=1', expected_code=200) assert "downloads" in response_dict assert len(response_dict["downloads"]) == 1 assert "peers" in response_dict["downloads"][0] assert len(response_dict["downloads"][0]["peers"]) == 1 assert 'have' not in response_dict["downloads"][0]["peers"][0] assert response_dict["downloads"][0]["peers"][0][ 'extended_version'] == 'uTorrent 1.6.1'
def test_get_files_completion(self): """ Testing whether the right completion of files is returned """ self.mocked_tdef.get_files_with_length = lambda: [("test.txt", 100)] handle = MockObject() handle.file_progress = lambda **_: [60] handle.is_valid = lambda: True self.mock_download.handle = handle download_state = DownloadState(self.mock_download, MockObject(), None) self.assertEqual(download_state.get_files_completion(), [('test.txt', 0.6)]) handle.file_progress = lambda **_: [0] self.assertEqual(download_state.get_files_completion(), [('test.txt', 0.0)]) handle.file_progress = lambda **_: [100] self.assertEqual(download_state.get_files_completion(), [('test.txt', 1.0)]) self.mocked_tdef.get_files_with_length = lambda: [] handle.file_progress = lambda **_: [] self.assertEqual(download_state.get_files_completion(), []) # Test a file with a length of zero self.mocked_tdef.get_files_with_length = lambda: [("test.txt", 0)] handle.file_progress = lambda **_: [0] self.assertEqual(download_state.get_files_completion(), [('test.txt', 1.0)])
def test_get_files_completion(mock_download, mock_tdef): """ Testing whether the right completion of files is returned """ mock_tdef.get_files_with_length = lambda: [("test.txt", 100)] handle = Mock() handle.file_progress = lambda **_: [60] handle.is_valid = lambda: True mock_download.handle = handle download_state = DownloadState(mock_download, Mock(), None) assert download_state.get_files_completion() == [('test.txt', 0.6)] handle.file_progress = lambda **_: [0] assert download_state.get_files_completion() == [('test.txt', 0.0)] handle.file_progress = lambda **_: [100] assert download_state.get_files_completion() == [('test.txt', 1.0)] mock_tdef.get_files_with_length = lambda: [] handle.file_progress = lambda **_: [] assert download_state.get_files_completion() == [] # Test a file with a length of zero mock_tdef.get_files_with_length = lambda: [("test.txt", 0)] handle.file_progress = lambda **_: [0] assert download_state.get_files_completion() == [('test.txt', 1.0)]
async def test_get_peers_illegal_fields_unknown(self): """ Testing whether illegal fields are stripped from the Libtorrent download info response. """ post_data = {'uri': 'file:%s' % (TESTS_DATA_DIR / 'video.avi.torrent')} expected_json = { 'started': True, 'infohash': '9d5b2dbc52807325bfc28d688f2bb03f8d1e7667' } response_dict = await self.do_request('downloads', expected_code=200, request_type='PUT', post_data=post_data, expected_json=expected_json) self.assertTrue(response_dict['started']) self.assertGreaterEqual(len(self.session.dlmgr.get_downloads()), 1) dl = self.session.dlmgr.get_downloads()[0] ds = DownloadState(dl, self.create_mock_status(), None) ds.get_peerlist = lambda: [{ 'id': '1234', 'have': '5678', 'extended_version': None }] dl.get_state = lambda: ds response_dict = await self.do_request( 'downloads?get_peers=1&get_pieces=1', expected_code=200) self.assertIn("downloads", response_dict) self.assertEqual(1, len(response_dict["downloads"])) self.assertIn("peers", response_dict["downloads"][0]) self.assertEqual(1, len(response_dict["downloads"][0]["peers"])) self.assertNotIn('have', response_dict["downloads"][0]["peers"][0]) self.assertEqual( '', response_dict["downloads"][0]["peers"][0]['extended_version'])
def test_getters_setters_2(self): """ Testing various getters and setters in DownloadState """ lt_status = MockObject() lt_status.state = 3 lt_status.upload_rate = 123 lt_status.download_rate = 43 lt_status.total_upload = 100 lt_status.total_download = 200 lt_status.all_time_upload = 100 lt_status.all_time_download = 200 lt_status.list_peers = 10 lt_status.list_seeds = 5 lt_status.progress = 0.75 lt_status.error = False lt_status.paused = False lt_status.state = 3 lt_status.num_pieces = 0 lt_status.pieces = [] lt_status.finished_time = 10 download_state = DownloadState(self.mock_download, lt_status, None) self.assertEqual(download_state.get_status(), DLSTATUS_DOWNLOADING) self.assertEqual(download_state.get_current_speed(UPLOAD), 123) self.assertEqual(download_state.get_current_speed(DOWNLOAD), 43) self.assertEqual(download_state.get_total_transferred(UPLOAD), 100) self.assertEqual(download_state.get_total_transferred(DOWNLOAD), 200) self.assertEqual(download_state.get_seeding_ratio(), 0.5) self.assertEqual(download_state.get_eta(), 0.25) self.assertEqual(download_state.get_num_seeds_peers(), (5, 5)) self.assertEqual(download_state.get_pieces_complete(), []) self.assertEqual(download_state.get_pieces_total_complete(), (0, 0)) self.assertEqual(download_state.get_seeding_time(), 10) lt_status.num_pieces = 6 lt_status.pieces = [1, 1, 1, 0, 0, 0] self.assertEqual(download_state.get_pieces_complete(), [1, 1, 1, 0, 0, 0]) self.assertEqual(download_state.get_pieces_total_complete(), (6, 3)) self.mock_download.config = MockObject() self.mock_download.config.get_selected_files = lambda: ['test'] self.assertEqual(download_state.get_selected_files(), ['test']) self.assertEqual(download_state.get_progress(), 0.75)
def test_getters_setters_1(self): """ Testing various getters and setters in DownloadState """ self.mock_download.get_peerlist = lambda: [] self.mock_download.session = MockObject() self.mock_download.session = MockObject() self.mock_download.session.tunnel_community = MockObject() self.mock_download.session.tunnel_community.get_candidates = lambda _: [] self.mock_download.config = MockObject() self.mock_download.config.get_hops = lambda: 0 download_state = DownloadState(self.mock_download, None, None) self.assertEqual(download_state.get_download(), self.mock_download) self.assertEqual(download_state.get_progress(), 0) self.assertEqual(download_state.get_status(), DLSTATUS_WAITING4HASHCHECK) self.assertIsNone(download_state.get_error()) self.assertEqual(download_state.get_current_speed(UPLOAD), 0) self.assertEqual(download_state.get_total_transferred(UPLOAD), 0) self.assertEqual(download_state.get_num_seeds_peers(), (0, 0)) self.assertEqual(download_state.get_peerlist(), []) self.mock_download.config.get_hops = lambda: 1 download_state = DownloadState(self.mock_download, None, None) self.assertEqual(download_state.get_status(), DLSTATUS_EXIT_NODES)
def test_get_availability(self): """ Testing whether the right availability of a file is returned """ mock_ltstate = MockObject() mock_ltstate.pieces = [True] download_state = DownloadState(self.mock_download, mock_ltstate, 0.6) download_state.get_peerlist = lambda: [] self.assertEqual(download_state.get_availability(), 0) download_state.get_peerlist = lambda: [{'completed': 1.0}] self.assertEqual(download_state.get_availability(), 1.0) download_state.get_peerlist = lambda: [{'completed': 0.6}] self.assertEqual(download_state.get_availability(), 0.0) download_state.lt_status.pieces = [0, 0, 0, 0, 0] download_state.get_peerlist = lambda: [{'completed': 0}, {'have': [1, 1, 1, 1, 0]}] self.assertEqual(download_state.get_availability(), 0.8) # Test whether inaccurate piece information from other peers is ignored download_state.get_peerlist = lambda: [{'completed': 0.5, 'have': [1, 0]}, {'completed': 0.9, 'have': [1, 0, 1]}] self.assertEqual(download_state.get_availability(), 0.0)
def test_getters_setters_2(mock_download, mock_lt_status): """ Testing various getters and setters in DownloadState """ download_state = DownloadState(mock_download, mock_lt_status, None) assert download_state.get_status() == DLSTATUS_DOWNLOADING assert download_state.get_current_speed(UPLOAD) == 123 assert download_state.get_current_speed(DOWNLOAD) == 43 assert download_state.get_total_transferred(UPLOAD) == 100 assert download_state.get_total_transferred(DOWNLOAD) == 200 assert download_state.get_seeding_ratio() == 0.5 assert download_state.get_eta() == 0.25 assert download_state.get_num_seeds_peers() == (5, 5) assert download_state.get_pieces_complete() == [] assert download_state.get_pieces_total_complete() == (0, 0) assert download_state.get_seeding_time() == 10 mock_lt_status.num_pieces = 6 mock_lt_status.pieces = [1, 1, 1, 0, 0, 0] assert download_state.get_pieces_complete() == [1, 1, 1, 0, 0, 0] assert download_state.get_pieces_total_complete() == (6, 3) mock_download.config.get_selected_files = lambda: ['test'] assert download_state.get_selected_files() == ['test'] assert download_state.get_progress() == 0.75
def test_getters_setters_1(mock_download): """ Testing various getters and setters in DownloadState """ mock_download.get_peerlist = lambda: [] mock_download.session.tunnel_community.get_candidates = lambda _: [] mock_download.config.get_hops = lambda: 0 download_state = DownloadState(mock_download, None, None) assert download_state.get_download() == mock_download assert download_state.get_progress() == 0 assert download_state.get_status() == DLSTATUS_WAITING4HASHCHECK assert download_state.get_error() is None assert download_state.get_current_speed(UPLOAD) == 0 assert download_state.get_total_transferred(UPLOAD) == 0 assert download_state.get_num_seeds_peers() == (0, 0) assert download_state.get_peerlist() == [] mock_download.config.get_hops = lambda: 1 download_state = DownloadState(mock_download, None, None) assert download_state.get_status() == DLSTATUS_EXIT_NODES
def get_state(self): """ Returns a snapshot of the current state of the download @return DownloadState """ return DownloadState(self, self.lt_status, self.error)