← Back to team overview

openlp-core team mailing list archive

[Merge] lp:~alisonken1/openlp/hash_fix into lp:openlp

 

Ken Roberts has proposed merging lp:~alisonken1/openlp/hash_fix into lp:openlp.

Requested reviews:
  OpenLP Core (openlp-core)
Related bugs:
  Bug #1417809 in OpenLP: "non-ascii characters in file path of images and presentation causes traceback"
  https://bugs.launchpad.net/openlp/+bug/1417809

For more details, see:
https://code.launchpad.net/~alisonken1/openlp/hash_fix/+merge/248460

Remove default ascii encoding so md5hash can be used to hash other data
(i.e., filepaths)

lp:~alisonken1/openlp/hash_fix (revision 2497)
[SUCCESS] http://ci.openlp.org/job/Branch-01-Pull/930/
[SUCCESS] http://ci.openlp.org/job/Branch-02-Functional-Tests/856/
[FAILURE] http://ci.openlp.org/job/Branch-03-Interface-Tests/801/

NOTE: Crosswalk changed webpage interface - failed test is crosswalk import.

-- 
Your team OpenLP Core is requested to review the proposed merge of lp:~alisonken1/openlp/hash_fix into lp:openlp.
=== modified file 'openlp/core/common/__init__.py'
--- openlp/core/common/__init__.py	2015-01-18 13:39:21 +0000
+++ openlp/core/common/__init__.py	2015-02-03 23:13:23 +0000
@@ -190,31 +190,84 @@
     return True if verify_ipv4(addr) else verify_ipv6(addr)
 
 
-def md5_hash(salt, data):
+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):
     """
     Returns the hashed output of md5sum on salt,data
     using Python3 hashlib
 
     :param salt: Initial salt
-    :param data: Data to hash
+    :param data: OPTIONAL Data to hash
     :returns: str
     """
     log.debug('md5_hash(salt="%s")' % salt)
     hash_obj = hashlib.new('md5')
-    hash_obj.update(salt.encode('ascii'))
-    hash_obj.update(data.encode('ascii'))
+    hash_obj.update(salt)
+    if data:
+        hash_obj.update(data)
     hash_value = hash_obj.hexdigest()
     log.debug('md5_hash() returning "%s"' % hash_value)
     return hash_value
 
 
-def qmd5_hash(salt, data):
+def qmd5_hash(salt, data=None):
     """
     Returns the hashed output of MD5Sum on salt, data
     using PyQt4.QCryptographicHash.
 
     :param salt: Initial salt
-    :param data: Data to hash
+    :param data: OPTIONAL Data to hash
     :returns: str
     """
     log.debug('qmd5_hash(salt="%s"' % salt)
@@ -223,7 +276,7 @@
     hash_obj.addData(data)
     hash_value = hash_obj.result().toHex()
     log.debug('qmd5_hash() returning "%s"' % hash_value)
-    return decode(hash_value.data(), 'ascii')
+    return hash_value.data()
 
 
 def clean_button_text(button_text):

=== modified file 'openlp/core/lib/projector/pjlink1.py'
--- openlp/core/lib/projector/pjlink1.py	2015-01-22 17:42:29 +0000
+++ openlp/core/lib/projector/pjlink1.py	2015-02-03 23:13:23 +0000
@@ -343,7 +343,7 @@
             # Authenticated login with salt
             log.debug('(%s) Setting hash with salt="%s"' % (self.ip, data_check[2]))
             log.debug('(%s) pin="%s"' % (self.ip, self.pin))
-            salt = qmd5_hash(salt=data_check[2], data=self.pin)
+            salt = qmd5_hash(salt=data_check[2], data=self.pin).decode('ascii')
         else:
             salt = None
         # We're connected at this point, so go ahead and do regular I/O

=== modified file 'tests/functional/openlp_core_common/test_projector_utilities.py'
--- tests/functional/openlp_core_common/test_projector_utilities.py	2015-01-18 13:39:21 +0000
+++ tests/functional/openlp_core_common/test_projector_utilities.py	2015-02-03 23:13:23 +0000
@@ -23,9 +23,17 @@
 Package to test the openlp.core.ui.projector.networkutils package.
 """
 
+import os
+
 from unittest import TestCase
 
-from openlp.core.common import verify_ip_address, md5_hash, qmd5_hash
+from openlp.core.common import verify_ip_address, md5_hash, qmd5_hash, md5_filecheck, sha1_filecheck
+
+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'
 pin = 'JBMIAProjectorLink'
@@ -120,7 +128,7 @@
         Test MD5 hash from salt+data pass (python)
         """
         # WHEN: Given a known salt+data
-        hash_ = md5_hash(salt=salt, data=pin)
+        hash_ = md5_hash(salt=salt.encode('ascii'), data=pin.encode('ascii'))
 
         # THEN: Validate return has is same
         self.assertEquals(hash_, test_hash, 'MD5 should have returned a good hash')
@@ -130,7 +138,7 @@
         Test MD5 hash from salt+data fail (python)
         """
         # WHEN: Given a different salt+hash
-        hash_ = md5_hash(salt=pin, data=salt)
+        hash_ = md5_hash(salt=pin.encode('ascii'), data=salt.encode('ascii'))
 
         # THEN: return data is different
         self.assertNotEquals(hash_, test_hash, 'MD5 should have returned a bad hash')
@@ -143,7 +151,7 @@
         hash_ = qmd5_hash(salt=salt, data=pin)
 
         # THEN: Validate return has is same
-        self.assertEquals(hash_, 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')
 
     def test_qmd5_hash_bad(self):
         """
@@ -153,4 +161,30 @@
         hash_ = qmd5_hash(salt=pin, data=salt)
 
         # THEN: return data is different
-        self.assertNotEquals(hash_, 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):
+        """
+        Test file MD5SUM check
+        """
+        # GIVEN: Test file to verify
+        test_file = os.path.join(TEST_PATH, TEST_FILENAME)
+
+        # WHEN: md5_filecheck is called
+        valid = md5_filecheck(test_file, TEST_FILE_HASH)
+
+        # THEN: Check should pass
+        self.assertEqual(valid, True, 'Test file MD5 should match test MD5')
+
+    def sa1sum_filecheck_test(self):
+        """
+        Test file SHA1SUM check
+        """
+        # GIVEN: Test file to verify
+        test_file = os.path.join(TEST_PATH, TEST_FILENAME)
+
+        # WHEN: md5_filecheck is called
+        valid = sha1_filecheck(test_file, TEST_FILE_SHA1)
+
+        # THEN: Check should pass
+        self.assertEqual(valid, True, 'Test file SHA1 should match test SHA1')

=== added file 'tests/resources/projector/Wheat.otz'
Binary files tests/resources/projector/Wheat.otz	1970-01-01 00:00:00 +0000 and tests/resources/projector/Wheat.otz	2015-02-03 23:13:23 +0000 differ

Follow ups