openlp/tests/openlp_core/projectors/test_projector_utilities.py

180 lines
6.3 KiB
Python
Raw Normal View History

2014-10-06 19:10:03 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
# Copyright (c) 2008-2019 OpenLP Developers #
# ---------------------------------------------------------------------- #
# 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 3 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, see <https://www.gnu.org/licenses/>. #
##########################################################################
2014-10-06 19:10:03 +00:00
"""
Package to test the openlp.core.ui.projector.networkutils package.
"""
from unittest import TestCase
2018-10-02 04:39:42 +00:00
from openlp.core.common import md5_hash, qmd5_hash, verify_ip_address
from tests.resources.projector.data import TEST_HASH, TEST_PIN, TEST_SALT
2016-04-17 18:48:50 +00:00
2016-01-03 08:15:54 +00:00
salt = TEST_SALT
pin = TEST_PIN
test_hash = TEST_HASH
2015-02-04 16:43:04 +00:00
test_non_ascii_string = '이것은 한국어 시험 문자열'
test_non_ascii_hash = 'fc00c7912976f6e9c19099b514ced201'
2014-10-06 19:10:03 +00:00
ip4_loopback = '127.0.0.1'
ip4_local = '192.168.1.1'
ip4_broadcast = '255.255.255.255'
ip4_bad = '192.168.1.256'
ip6_loopback = '::1'
ip6_link_local = 'fe80::223:14ff:fe99:d315'
ip6_bad = 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff'
2017-12-15 16:19:42 +00:00
class TestProjectorUtilities(TestCase):
2014-10-06 19:10:03 +00:00
"""
Validate functions in the projector utilities module
"""
def test_ip4_loopback_valid(self):
"""
Test IPv4 loopbackvalid
"""
# WHEN: Test with a local loopback test
valid = verify_ip_address(addr=ip4_loopback)
# THEN: Verify we received True
2017-12-09 16:29:58 +00:00
assert valid, 'IPv4 loopback address should have been valid'
2014-10-06 19:10:03 +00:00
def test_ip4_local_valid(self):
"""
Test IPv4 local valid
"""
# WHEN: Test with a local loopback test
valid = verify_ip_address(addr=ip4_local)
# THEN: Verify we received True
2017-12-16 08:43:33 +00:00
assert valid is True, 'IPv4 local address should have been valid'
2014-10-06 19:10:03 +00:00
def test_ip4_broadcast_valid(self):
"""
Test IPv4 broadcast valid
"""
# WHEN: Test with a local loopback test
valid = verify_ip_address(addr=ip4_broadcast)
# THEN: Verify we received True
2017-12-16 08:43:33 +00:00
assert valid is True, 'IPv4 broadcast address should have been valid'
2014-10-06 19:10:03 +00:00
def test_ip4_address_invalid(self):
"""
Test IPv4 address invalid
"""
# WHEN: Test with a local loopback test
valid = verify_ip_address(addr=ip4_bad)
# THEN: Verify we received True
2017-12-15 16:19:42 +00:00
assert valid is False, 'Bad IPv4 address should not have been valid'
2014-10-06 19:10:03 +00:00
def test_ip6_loopback_valid(self):
"""
Test IPv6 loopback valid
"""
# WHEN: Test IPv6 loopback address
valid = verify_ip_address(addr=ip6_loopback)
# THEN: Validate return
2017-12-16 08:43:33 +00:00
assert valid is True, 'IPv6 loopback address should have been valid'
2014-10-06 19:10:03 +00:00
def test_ip6_local_valid(self):
"""
Test IPv6 link-local valid
"""
# WHEN: Test IPv6 link-local address
valid = verify_ip_address(addr=ip6_link_local)
# THEN: Validate return
2017-12-16 08:43:33 +00:00
assert valid is True, 'IPv6 link-local address should have been valid'
2014-10-06 19:10:03 +00:00
def test_ip6_address_invalid(self):
"""
Test NetworkUtils IPv6 address invalid
"""
# WHEN: Given an invalid IPv6 address
valid = verify_ip_address(addr=ip6_bad)
# THEN: Validate bad return
2017-12-15 16:19:42 +00:00
assert valid is False, 'IPv6 bad address should have been invalid'
2014-10-06 19:10:03 +00:00
def test_md5_hash(self):
"""
Test MD5 hash from salt+data pass (python)
"""
# WHEN: Given a known salt+data
hash_ = md5_hash(salt=salt.encode('utf-8'), data=pin.encode('utf-8'))
2014-10-06 19:10:03 +00:00
# THEN: Validate return has is same
2017-12-09 16:29:58 +00:00
assert hash_ == test_hash, 'MD5 should have returned a good hash'
2014-10-06 19:10:03 +00:00
def test_md5_hash_bad(self):
"""
Test MD5 hash from salt+data fail (python)
"""
# WHEN: Given a different salt+hash
hash_ = md5_hash(salt=pin.encode('utf-8'), data=salt.encode('utf-8'))
2014-10-06 19:10:03 +00:00
# THEN: return data is different
2017-12-09 16:29:58 +00:00
assert hash_ is not test_hash, 'MD5 should have returned a bad hash'
2014-10-06 19:10:03 +00:00
def test_qmd5_hash(self):
"""
Test MD5 hash from salt+data pass (Qt)
"""
# WHEN: Given a known salt+data
hash_ = qmd5_hash(salt=salt.encode('utf-8'), data=pin.encode('utf-8'))
2014-10-06 19:10:03 +00:00
# THEN: Validate return has is same
2017-12-09 16:29:58 +00:00
assert hash_ == test_hash, 'Qt-MD5 should have returned a good hash'
2014-10-06 19:10:03 +00:00
def test_qmd5_hash_bad(self):
"""
Test MD5 hash from salt+hash fail (Qt)
"""
# WHEN: Given a different salt+hash
hash_ = qmd5_hash(salt=pin.encode('utf-8'), data=salt.encode('utf-8'))
2014-10-06 19:10:03 +00:00
# THEN: return data is different
2017-12-09 16:29:58 +00:00
assert hash_ is not test_hash, 'Qt-MD5 should have returned a bad hash'
2015-02-04 16:43:04 +00:00
def test_md5_non_ascii_string(self):
"""
2015-02-04 16:43:04 +00:00
Test MD5 hash with non-ascii string - bug 1417809
"""
2015-02-04 16:43:04 +00:00
# WHEN: Non-ascii string is hashed
hash_ = md5_hash(salt=test_non_ascii_string.encode('utf-8'), data=None)
2015-02-04 16:43:04 +00:00
# THEN: Valid MD5 hash should be returned
2017-12-09 16:29:58 +00:00
assert hash_ == test_non_ascii_hash, 'MD5 should have returned a valid hash'
2015-02-03 22:59:59 +00:00
2015-02-04 16:43:04 +00:00
def test_qmd5_non_ascii_string(self):
2015-02-03 22:59:59 +00:00
"""
2015-02-04 16:43:04 +00:00
Test MD5 hash with non-ascii string - bug 1417809
2015-02-03 22:59:59 +00:00
"""
2015-02-04 16:43:04 +00:00
# WHEN: Non-ascii string is hashed
hash_ = md5_hash(data=test_non_ascii_string.encode('utf-8'))
2015-02-03 22:59:59 +00:00
2015-02-04 16:43:04 +00:00
# THEN: Valid MD5 hash should be returned
2017-12-09 16:29:58 +00:00
assert hash_ == test_non_ascii_hash, 'Qt-MD5 should have returned a valid hash'