From 7fcdad0e69fa5dd3bda562c30d8dd4e70a31dbf9 Mon Sep 17 00:00:00 2001 From: Saurav Kumar Date: Mon, 22 Apr 2019 15:36:26 +0530 Subject: [PATCH] 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 --- tests/test_getipaddress.py | 46 +++++++++++++++++++++++ tests/test_newsunpack.py | 41 +++++++++++++++++++++ tests/test_utils/__init__.py | 0 tests/test_utils/test_cert_gen.py | 67 ++++++++++++++++++++++++++++++++++ tests/test_utils/test_diskspeed.py | 33 +++++++++++++++++ tests/test_utils/test_internetspeed.py | 45 +++++++++++++++++++++++ tests/test_utils/test_pystone.py | 32 ++++++++++++++++ 7 files changed, 264 insertions(+) create mode 100644 tests/test_getipaddress.py create mode 100644 tests/test_newsunpack.py create mode 100644 tests/test_utils/__init__.py create mode 100644 tests/test_utils/test_cert_gen.py create mode 100644 tests/test_utils/test_diskspeed.py create mode 100644 tests/test_utils/test_internetspeed.py create mode 100644 tests/test_utils/test_pystone.py diff --git a/tests/test_getipaddress.py b/tests/test_getipaddress.py new file mode 100644 index 0000000..ab9760b --- /dev/null +++ b/tests/test_getipaddress.py @@ -0,0 +1,46 @@ +#!/usr/bin/python3 -OO +# Copyright 2007-2019 The SABnzbd-Team +# +# 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) diff --git a/tests/test_newsunpack.py b/tests/test_newsunpack.py new file mode 100644 index 0000000..38f78f1 --- /dev/null +++ b/tests/test_newsunpack.py @@ -0,0 +1,41 @@ +#!/usr/bin/python3 -OO +# Copyright 2007-2019 The SABnzbd-Team +# +# 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 diff --git a/tests/test_utils/__init__.py b/tests/test_utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_utils/test_cert_gen.py b/tests/test_utils/test_cert_gen.py new file mode 100644 index 0000000..13c2e29 --- /dev/null +++ b/tests/test_utils/test_cert_gen.py @@ -0,0 +1,67 @@ +#!/usr/bin/python3 -OO +# Copyright 2007-2019 The SABnzbd-Team +# +# 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 diff --git a/tests/test_utils/test_diskspeed.py b/tests/test_utils/test_diskspeed.py new file mode 100644 index 0000000..5cb01fb --- /dev/null +++ b/tests/test_utils/test_diskspeed.py @@ -0,0 +1,33 @@ +#!/usr/bin/python3 -OO +# Copyright 2007-2019 The SABnzbd-Team +# +# 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")) diff --git a/tests/test_utils/test_internetspeed.py b/tests/test_utils/test_internetspeed.py new file mode 100644 index 0000000..fcbe418 --- /dev/null +++ b/tests/test_utils/test_internetspeed.py @@ -0,0 +1,45 @@ +#!/usr/bin/python3 -OO +# Copyright 2007-2019 The SABnzbd-Team +# +# 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 diff --git a/tests/test_utils/test_pystone.py b/tests/test_utils/test_pystone.py new file mode 100644 index 0000000..b956fa2 --- /dev/null +++ b/tests/test_utils/test_pystone.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3 -OO +# Copyright 2007-2019 The SABnzbd-Team +# +# 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