summaryrefslogtreecommitdiffstats
path: root/deluge/tests
diff options
context:
space:
mode:
Diffstat (limited to 'deluge/tests')
-rw-r--r--deluge/tests/test_alertmanager.py77
-rw-r--r--deluge/tests/test_client.py22
-rw-r--r--deluge/tests/test_core.py28
-rw-r--r--deluge/tests/test_metafile.py55
4 files changed, 173 insertions, 9 deletions
diff --git a/deluge/tests/test_alertmanager.py b/deluge/tests/test_alertmanager.py
index 683b011..2d18f4b 100644
--- a/deluge/tests/test_alertmanager.py
+++ b/deluge/tests/test_alertmanager.py
@@ -3,21 +3,61 @@
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#
+from dataclasses import dataclass
+
+import pytest
-import deluge.component as component
-from deluge.conftest import BaseTestCase
from deluge.core.core import Core
-class TestAlertManager(BaseTestCase):
- def set_up(self):
+class LtSessionMock:
+ def __init__(self):
+ self.alerts = []
+
+ def push_alerts(self, alerts):
+ self.alerts = alerts
+
+ def wait_for_alert(self, timeout):
+ return self.alerts[0] if len(self.alerts) > 0 else None
+
+ def pop_alerts(self):
+ alerts = self.alerts
+ self.alerts = []
+ return alerts
+
+
+@dataclass
+class LtAlertMock:
+ type: int
+ name: str
+ message: str
+
+ def message(self):
+ return self.message
+
+ def what(self):
+ return self.name
+
+
+@pytest.fixture
+def mock_alert1():
+ return LtAlertMock(type=1, name='mock_alert1', message='Alert 1')
+
+
+@pytest.fixture
+def mock_alert2():
+ return LtAlertMock(type=2, name='mock_alert2', message='Alert 2')
+
+
+class TestAlertManager:
+ @pytest.fixture(autouse=True)
+ def set_up(self, component):
self.core = Core()
self.core.config.config['lsd'] = False
self.am = component.get('AlertManager')
- return component.start(['AlertManager'])
+ self.am.session = LtSessionMock()
- def tear_down(self):
- return component.shutdown()
+ component.start(['AlertManager'])
def test_register_handler(self):
def handler(alert):
@@ -28,6 +68,29 @@ class TestAlertManager(BaseTestCase):
assert self.am.handlers['dummy1'] == [handler]
assert self.am.handlers['dummy2'] == [handler]
+ async def test_pop_alert(self, mock_callback, mock_alert1, mock_alert2):
+ self.am.register_handler('mock_alert1', mock_callback)
+
+ self.am.session.push_alerts([mock_alert1, mock_alert2])
+
+ await mock_callback.deferred
+
+ mock_callback.assert_called_once_with(mock_alert1)
+
+ async def test_pause_not_pop_alert(
+ self, component, mock_alert1, mock_alert2, mock_callback
+ ):
+ await component.pause(['AlertManager'])
+
+ self.am.register_handler('mock_alert1', mock_callback)
+ self.am.session.push_alerts([mock_alert1, mock_alert2])
+
+ await mock_callback.deferred
+
+ mock_callback.assert_not_called()
+ assert not self.am._event.is_set()
+ assert len(self.am.session.alerts) == 2
+
def test_deregister_handler(self):
def handler(alert):
...
diff --git a/deluge/tests/test_client.py b/deluge/tests/test_client.py
index 5a67279..763d43c 100644
--- a/deluge/tests/test_client.py
+++ b/deluge/tests/test_client.py
@@ -8,7 +8,7 @@ import pytest_twisted
from twisted.internet import defer
from deluge import error
-from deluge.common import AUTH_LEVEL_NORMAL, get_localhost_auth
+from deluge.common import AUTH_LEVEL_NORMAL, get_localhost_auth, get_version
from deluge.core.authmanager import AUTH_LEVEL_ADMIN
from deluge.ui.client import Client, DaemonSSLProxy, client
@@ -170,3 +170,23 @@ class TestClient:
d.addCallbacks(self.fail, on_failure)
return d
+
+ @pytest_twisted.inlineCallbacks
+ def test_daemon_version(self):
+ username, password = get_localhost_auth()
+ yield client.connect(
+ 'localhost', self.listen_port, username=username, password=password
+ )
+
+ assert client.daemon_version == get_version()
+
+ @pytest_twisted.inlineCallbacks
+ def test_daemon_version_check_min(self):
+ username, password = get_localhost_auth()
+ yield client.connect(
+ 'localhost', self.listen_port, username=username, password=password
+ )
+
+ assert client.daemon_version_check_min(get_version())
+ assert not client.daemon_version_check_min(f'{get_version()}1')
+ assert client.daemon_version_check_min('0.1.0')
diff --git a/deluge/tests/test_core.py b/deluge/tests/test_core.py
index c2f6333..28b5902 100644
--- a/deluge/tests/test_core.py
+++ b/deluge/tests/test_core.py
@@ -3,7 +3,7 @@
# the additional special exception to link portions of this program with the OpenSSL library.
# See LICENSE for more details.
#
-
+import base64
import os
from base64 import b64encode
from hashlib import sha1 as sha
@@ -483,3 +483,29 @@ class TestCore(BaseTestCase):
assert self.core._create_peer_id('2.0.1rc1') == '-DE201r-'
assert self.core._create_peer_id('2.11.0b2') == '-DE2B0b-'
assert self.core._create_peer_id('2.4.12b2.dev3') == '-DE24CD-'
+
+ @pytest.mark.parametrize(
+ 'path',
+ [
+ common.get_test_data_file('deluge.png'),
+ os.path.dirname(common.get_test_data_file('deluge.png')),
+ ],
+ )
+ @pytest.mark.parametrize('piece_length', [2**14, 2**16])
+ @pytest_twisted.inlineCallbacks
+ def test_create_torrent(self, path, tmp_path, piece_length):
+ target = tmp_path / 'test.torrent'
+
+ filename, filedump = yield self.core.create_torrent(
+ path=path,
+ tracker=None,
+ piece_length=piece_length,
+ target=target,
+ add_to_session=False,
+ )
+ filecontent = base64.b64decode(filedump)
+
+ with open(target, 'rb') as f:
+ assert f.read() == filecontent
+
+ lt.torrent_info(filecontent)
diff --git a/deluge/tests/test_metafile.py b/deluge/tests/test_metafile.py
index fda1cb7..1b16750 100644
--- a/deluge/tests/test_metafile.py
+++ b/deluge/tests/test_metafile.py
@@ -7,7 +7,13 @@
import os
import tempfile
+import pytest
+
from deluge import metafile
+from deluge._libtorrent import LT_VERSION
+from deluge.common import VersionSplit
+
+from . import common
def check_torrent(filename):
@@ -55,3 +61,52 @@ class TestMetafile:
metafile.make_meta_file(tmp_data, '', 32768, target=tmp_torrent)
check_torrent(tmp_torrent)
+
+ @pytest.mark.parametrize(
+ 'path',
+ [
+ common.get_test_data_file('deluge.png'),
+ common.get_test_data_file('unicode_filenames.torrent'),
+ os.path.dirname(common.get_test_data_file('deluge.png')),
+ ],
+ )
+ @pytest.mark.parametrize(
+ 'torrent_format',
+ [
+ metafile.TorrentFormat.V1,
+ metafile.TorrentFormat.V2,
+ metafile.TorrentFormat.HYBRID,
+ ],
+ )
+ @pytest.mark.parametrize('piece_length', [2**14, 2**15, 2**16])
+ @pytest.mark.parametrize('private', [True, False])
+ def test_create_info(self, path, torrent_format, piece_length, private):
+ our_info, our_piece_layers = metafile.makeinfo(
+ path,
+ piece_length,
+ metafile.dummy,
+ private=private,
+ torrent_format=torrent_format,
+ )
+ lt_info, lt_piece_layers = metafile.makeinfo_lt(
+ path,
+ piece_length,
+ private=private,
+ torrent_format=torrent_format,
+ )
+
+ if (
+ torrent_format == metafile.TorrentFormat.HYBRID
+ and os.path.isdir(path)
+ and VersionSplit(LT_VERSION) <= VersionSplit('2.0.7.0')
+ ):
+ # Libtorrent didn't correctly follow the standard until version 2.0.7 included
+ # https://github.com/arvidn/libtorrent/commit/74d82a0cd7c2e9e3c4294901d7eb65e247050df4
+ # If last file is a padding, ignore that file and the last piece.
+ if our_info[b'files'][-1][b'path'][0] == b'.pad':
+ our_info[b'files'] = our_info[b'files'][:-1]
+ our_info[b'pieces'] = our_info[b'pieces'][:-32]
+ lt_info[b'pieces'] = lt_info[b'pieces'][:-32]
+
+ assert our_info == lt_info
+ assert our_piece_layers == lt_piece_layers