forked from openlp/openlp
Remove new functions - fix tests
This commit is contained in:
parent
fd5b8112f9
commit
a4235aafe9
@ -190,58 +190,6 @@ def verify_ip_address(addr):
|
|||||||
return True if verify_ipv4(addr) else verify_ipv6(addr)
|
return True if verify_ipv4(addr) else verify_ipv6(addr)
|
||||||
|
|
||||||
|
|
||||||
def sha1_filecheck(input_file, check):
|
|
||||||
"""
|
|
||||||
Validate a downloaded file has the same SHA1 as recorded.
|
|
||||||
|
|
||||||
:param input_file: File to test
|
|
||||||
:param check: MD5Sum to validate
|
|
||||||
:return: File MD5 == check
|
|
||||||
"""
|
|
||||||
log.debug('sha1_filecheck(file="{}", check="{}"'.format(input_file, check))
|
|
||||||
try:
|
|
||||||
file_ = open(input_file, 'rb')
|
|
||||||
except OSError as err:
|
|
||||||
log.error('Unable to check "{}"'.format(input_file))
|
|
||||||
log.error('Error code: {}'.format(err.errno))
|
|
||||||
log.error('Error msg: {}'.format(err.strerror))
|
|
||||||
return False
|
|
||||||
hash_obj = hashlib.new('sha1')
|
|
||||||
while True:
|
|
||||||
data = file_.read(1024)
|
|
||||||
if not data:
|
|
||||||
break
|
|
||||||
hash_obj.update(data)
|
|
||||||
log.debug('sha1_filecheck validating "{}"'.format(hash_obj.hexdigest()))
|
|
||||||
return hash_obj.hexdigest() == check
|
|
||||||
|
|
||||||
|
|
||||||
def md5_filecheck(input_file, check):
|
|
||||||
"""
|
|
||||||
Validate a downloaded file has the same MD5 as recorded.
|
|
||||||
|
|
||||||
:param input_file: File to test
|
|
||||||
:param check: MD5Sum to validate
|
|
||||||
:return: File MD5 == check
|
|
||||||
"""
|
|
||||||
log.debug('md5_filecheck(file="{}", check="{}"'.format(input_file, check))
|
|
||||||
try:
|
|
||||||
file_ = open(input_file, 'rb')
|
|
||||||
except OSError as err:
|
|
||||||
log.error('Unable to check "{}"'.format(input_file))
|
|
||||||
log.error('Error code: {}'.format(err.errno))
|
|
||||||
log.error('Error msg: {}'.format(err.strerror))
|
|
||||||
return False
|
|
||||||
hash_obj = hashlib.new('md5')
|
|
||||||
while True:
|
|
||||||
data = file_.read(1024)
|
|
||||||
if not data:
|
|
||||||
break
|
|
||||||
hash_obj.update(data)
|
|
||||||
log.debug('md5_filecheck validating "{}"'.format(hash_obj.hexdigest()))
|
|
||||||
return hash_obj.hexdigest() == check
|
|
||||||
|
|
||||||
|
|
||||||
def md5_hash(salt, data=None):
|
def md5_hash(salt, data=None):
|
||||||
"""
|
"""
|
||||||
Returns the hashed output of md5sum on salt,data
|
Returns the hashed output of md5sum on salt,data
|
||||||
|
@ -343,7 +343,7 @@ class PJLink1(QTcpSocket):
|
|||||||
# Authenticated login with salt
|
# Authenticated login with salt
|
||||||
log.debug('(%s) Setting hash with salt="%s"' % (self.ip, data_check[2]))
|
log.debug('(%s) Setting hash with salt="%s"' % (self.ip, data_check[2]))
|
||||||
log.debug('(%s) pin="%s"' % (self.ip, self.pin))
|
log.debug('(%s) pin="%s"' % (self.ip, self.pin))
|
||||||
salt = qmd5_hash(salt=data_check[2], data=self.pin).decode('ascii')
|
salt = qmd5_hash(salt=data_check[2].endcode('ascii'), data=self.pin.encode('ascii'))
|
||||||
else:
|
else:
|
||||||
salt = None
|
salt = None
|
||||||
# We're connected at this point, so go ahead and do regular I/O
|
# We're connected at this point, so go ahead and do regular I/O
|
||||||
|
@ -27,17 +27,13 @@ import os
|
|||||||
|
|
||||||
from unittest import TestCase
|
from unittest import TestCase
|
||||||
|
|
||||||
from openlp.core.common import verify_ip_address, md5_hash, qmd5_hash, md5_filecheck, sha1_filecheck
|
from openlp.core.common import verify_ip_address, md5_hash, qmd5_hash
|
||||||
|
|
||||||
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__),
|
|
||||||
'..', '..', 'resources', 'projector'))
|
|
||||||
TEST_FILENAME = 'Wheat.otz'
|
|
||||||
TEST_FILE_HASH = '2d7daae6f8ce6dc0963ea8c0fa4d67aa'
|
|
||||||
TEST_FILE_SHA1 = '20a2ca97a479d166ac9f59147054b58304b4d264'
|
|
||||||
|
|
||||||
salt = '498e4a67'
|
salt = '498e4a67'
|
||||||
pin = 'JBMIAProjectorLink'
|
pin = 'JBMIAProjectorLink'
|
||||||
test_hash = '5d8409bc1c3fa39749434aa3a5c38682'
|
test_hash = '5d8409bc1c3fa39749434aa3a5c38682'
|
||||||
|
test_non_ascii_string = '이것은 한국어 시험 문자열'
|
||||||
|
test_non_ascii_hash = 'fc00c7912976f6e9c19099b514ced201'
|
||||||
|
|
||||||
ip4_loopback = '127.0.0.1'
|
ip4_loopback = '127.0.0.1'
|
||||||
ip4_local = '192.168.1.1'
|
ip4_local = '192.168.1.1'
|
||||||
@ -148,7 +144,7 @@ class testProjectorUtilities(TestCase):
|
|||||||
Test MD5 hash from salt+data pass (Qt)
|
Test MD5 hash from salt+data pass (Qt)
|
||||||
"""
|
"""
|
||||||
# WHEN: Given a known salt+data
|
# WHEN: Given a known salt+data
|
||||||
hash_ = qmd5_hash(salt=salt, data=pin)
|
hash_ = qmd5_hash(salt=salt.encode('ascii'), data=pin.encode('ascii'))
|
||||||
|
|
||||||
# THEN: Validate return has is same
|
# THEN: Validate return has is same
|
||||||
self.assertEquals(hash_.decode('ascii'), test_hash, 'Qt-MD5 should have returned a good hash')
|
self.assertEquals(hash_.decode('ascii'), test_hash, 'Qt-MD5 should have returned a good hash')
|
||||||
@ -158,33 +154,27 @@ class testProjectorUtilities(TestCase):
|
|||||||
Test MD5 hash from salt+hash fail (Qt)
|
Test MD5 hash from salt+hash fail (Qt)
|
||||||
"""
|
"""
|
||||||
# WHEN: Given a different salt+hash
|
# WHEN: Given a different salt+hash
|
||||||
hash_ = qmd5_hash(salt=pin, data=salt)
|
hash_ = qmd5_hash(salt=pin.encode('ascii'), data=salt.encode('ascii'))
|
||||||
|
|
||||||
# THEN: return data is different
|
# THEN: return data is different
|
||||||
self.assertNotEquals(hash_.decode('ascii'), test_hash, 'Qt-MD5 should have returned a bad hash')
|
self.assertNotEquals(hash_.decode('ascii'), test_hash, 'Qt-MD5 should have returned a bad hash')
|
||||||
|
|
||||||
def md5sum_filecheck_test(self):
|
def test_md5_non_ascii_string(self):
|
||||||
"""
|
"""
|
||||||
Test file MD5SUM check
|
Test MD5 hash with non-ascii string - bug 1417809
|
||||||
"""
|
"""
|
||||||
# GIVEN: Test file to verify
|
# WHEN: Non-ascii string is hashed
|
||||||
test_file = os.path.join(TEST_PATH, TEST_FILENAME)
|
hash_ = md5_hash(salt=test_non_ascii_string.encode('utf-8'), data=None)
|
||||||
|
|
||||||
# WHEN: md5_filecheck is called
|
# THEN: Valid MD5 hash should be returned
|
||||||
valid = md5_filecheck(test_file, TEST_FILE_HASH)
|
self.assertEqual(hash_, test_non_ascii_hash, 'MD5 should have returned a valid hash')
|
||||||
|
|
||||||
# THEN: Check should pass
|
def test_qmd5_non_ascii_string(self):
|
||||||
self.assertEqual(valid, True, 'Test file MD5 should match test MD5')
|
|
||||||
|
|
||||||
def sa1sum_filecheck_test(self):
|
|
||||||
"""
|
"""
|
||||||
Test file SHA1SUM check
|
Test MD5 hash with non-ascii string - bug 1417809
|
||||||
"""
|
"""
|
||||||
# GIVEN: Test file to verify
|
# WHEN: Non-ascii string is hashed
|
||||||
test_file = os.path.join(TEST_PATH, TEST_FILENAME)
|
hash_ = md5_hash(salt=test_non_ascii_string.encode('utf-8'), data=None)
|
||||||
|
|
||||||
# WHEN: md5_filecheck is called
|
# THEN: Valid MD5 hash should be returned
|
||||||
valid = sha1_filecheck(test_file, TEST_FILE_SHA1)
|
self.assertEqual(hash_, test_non_ascii_hash, 'Qt-MD5 should have returned a valid hash')
|
||||||
|
|
||||||
# THEN: Check should pass
|
|
||||||
self.assertEqual(valid, True, 'Test file SHA1 should match test SHA1')
|
|
||||||
|
Loading…
Reference in New Issue
Block a user