Browse Source

Added new tests for utils (#1266)

* Added util tests

* FIxed build failures

* Incorportaed review comments

Added new tests

* Test added for generate_key

* Updated module name

* Correct newly added tests
pull/1278/head
Saurav Kumar 6 years ago
committed by Safihre
parent
commit
7fcdad0e69
  1. 46
      tests/test_getipaddress.py
  2. 41
      tests/test_newsunpack.py
  3. 0
      tests/test_utils/__init__.py
  4. 67
      tests/test_utils/test_cert_gen.py
  5. 33
      tests/test_utils/test_diskspeed.py
  6. 45
      tests/test_utils/test_internetspeed.py
  7. 32
      tests/test_utils/test_pystone.py

46
tests/test_getipaddress.py

@ -0,0 +1,46 @@
#!/usr/bin/python3 -OO
# Copyright 2007-2019 The SABnzbd-Team <team@sabnzbd.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
tests.test_utils.test_check_dir - Testing SABnzbd checkdir util
"""
from sabnzbd.getipaddress import *
from sabnzbd.cfg import selftest_host
from sabnzbd.misc import probablyipv4, probablyipv6
class TestGetIpAddress:
def test_addresslookup4(self):
address = addresslookup4(selftest_host())
assert address
for item in address:
assert isinstance(item[0], type(socket.AF_INET))
def test_publicipv4(self):
public_ipv4 = publicipv4()
assert probablyipv4(public_ipv4)
def test_localipv4(self):
local_ipv4 = localipv4()
assert probablyipv4(local_ipv4)
def test_ipv6(self):
test_ipv6 = ipv6()
# Not all systems have IPv6
if test_ipv6:
assert probablyipv6(test_ipv6)

41
tests/test_newsunpack.py

@ -0,0 +1,41 @@
#!/usr/bin/python3 -OO
# Copyright 2007-2019 The SABnzbd-Team <team@sabnzbd.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
tests.test_newsunpack - Tests of various functions in newspack
"""
from sabnzbd.newsunpack import *
import pytest
class TestNewsUnpack:
@pytest.mark.parametrize(
"test_input, expected_output",
[
(["cmd1", 9, "cmd3"], '"cmd1" "9" "cmd3"'), # sending all commands as valid string
(["", "cmd1", "5"], '"" "cmd1" "5"'), # sending blank string
(["cmd1", None, "cmd3", "tail -f"], '"cmd1" "" "cmd3" "tail -f"'), # sending None in command
(["cmd1", 0, "ps ux"], '"cmd1" "" "ps ux"'), # sending 0
],
)
def test_list_to_cmd(self, test_input, expected_output):
""" Test to convert list to a cmd.exe-compatible command string """
res = list2cmdline(test_input)
# Make sure the output is cmd.exe-compatible
assert res == expected_output

0
tests/test_utils/__init__.py

67
tests/test_utils/test_cert_gen.py

@ -0,0 +1,67 @@
#!/usr/bin/python3 -OO
# Copyright 2007-2019 The SABnzbd-Team <team@sabnzbd.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
tests.test_cert_gen - Testing Certificate generation
"""
import datetime
from cryptography import x509
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives.asymmetric import rsa
from sabnzbd.utils.certgen import generate_key, generate_local_cert
from tests.testhelper import *
class TestCertGen:
def test_generate_key_default(self):
# Generate private key with default key_size and file name
private_key = generate_key(output_file=os.path.join(SAB_CACHE_DIR, "test_key.pem"))
assert private_key.key_size == 2048
@pytest.mark.parametrize(
"key_size, file_name", [(512, "test_key.pem"), (1024, "test_123_key.pem"), (4096, "123_key.pem")]
)
def test_generate_key_custom(self, key_size, file_name):
# Generate private key
private_key = generate_key(key_size=key_size, output_file=os.path.join(SAB_CACHE_DIR, file_name))
# validate generated private key
assert private_key.key_size == key_size
assert os.path.isfile(os.path.join(SAB_CACHE_DIR, file_name))
def test_generate_local_cert(self):
# Generate private key
private_key = generate_key(output_file=os.path.join(SAB_CACHE_DIR, "test_key.pem"))
# Generate local certificate using private key
output_file = os.path.join(SAB_CACHE_DIR, "test_cert.cert")
local_cert = generate_local_cert(private_key, output_file=output_file)
assert local_cert
# Validating generated key file
public_key = local_cert.public_key()
assert isinstance(public_key, rsa.RSAPublicKey)
# Validate certificate file
with open(output_file, "rb") as cert_file:
cert_content = cert_file.read()
cert = x509.load_pem_x509_certificate(cert_content, default_backend())
# Validate that the timestamp at which the certificate stops being valid (expiration date) is in future
assert datetime.datetime.now() < cert.not_valid_after

33
tests/test_utils/test_diskspeed.py

@ -0,0 +1,33 @@
#!/usr/bin/python3 -OO
# Copyright 2007-2019 The SABnzbd-Team <team@sabnzbd.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
tests.test_utils.test_diskspeed - Testing SABnzbd diskspeed
"""
from tests.testhelper import *
from sabnzbd.utils.diskspeed import diskspeedmeasure
class TestDiskSpeed:
def test_disk_speed(self):
speed = diskspeedmeasure(SAB_CACHE_DIR)
assert speed
assert isinstance(speed, float)
# Make sure the test-file was cleaned up after the test
assert not os.path.exists(os.path.join(SAB_CACHE_DIR, "outputTESTING.txt"))

45
tests/test_utils/test_internetspeed.py

@ -0,0 +1,45 @@
#!/usr/bin/python3 -OO
# Copyright 2007-2019 The SABnzbd-Team <team@sabnzbd.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
tests.test_utils.test_internetspeed - Testing SABnzbd internetspeed
"""
from sabnzbd.utils.internetspeed import internetspeed, measurespeed, SizeUrlList
class TestInternetSpeed:
""" This class contains tests to measure internet speed
with an active and inactive connection
"""
def test_measurespeed_invalid_url(self):
speed = measurespeed("www.fake-url-9999999.xyz")
assert not speed
def test_measurespeed_valid_url(self):
speed = measurespeed(SizeUrlList[0][1])
assert isinstance(speed, float)
assert speed > 0
def test_internet_speed(self):
curr_speed_mbps = internetspeed()
assert isinstance(curr_speed_mbps, float)
assert curr_speed_mbps > 0

32
tests/test_utils/test_pystone.py

@ -0,0 +1,32 @@
#!/usr/bin/python3 -OO
# Copyright 2007-2019 The SABnzbd-Team <team@sabnzbd.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
tests.test_utils.test_pystone - Testing SABnzbd pystone
"""
from sabnzbd.utils.pystone import pystones
class TestPystone:
def test_pystone(self):
""" Tests for performance with various loop sizes """
loops = [10, 1000, 50000, 100000]
for loop in loops:
benchtime, stones = pystones(loop)
assert benchtime > 0
assert stones > 0
Loading…
Cancel
Save