Esempio n. 1
0
def verify_image_checksum(image_location, expected_checksum):
    """Verifies checksum (md5) of image file against the expected one.

    This method generates the checksum of the image file on the fly and
    verifies it against the expected checksum provided as argument.

    :param image_location: location of image file whose checksum is verified.
    :param expected_checksum: checksum to be checked against
    :raises: ImageRefValidationFailed, if invalid file path or
             verification fails.
    """
    try:
        with open(image_location, 'rb') as fd:
            actual_checksum = utils.hash_file(fd)
    except IOError as e:
        LOG.error(_LE("Error opening file: %(file)s"),
                  {'file': image_location})
        raise exception.ImageRefValidationFailed(image_href=image_location,
                                                 reason=e)

    if actual_checksum != expected_checksum:
        msg = (_('Error verifying image checksum. Image %(image)s failed to '
                 'verify against checksum %(checksum)s. Actual checksum is: '
                 '%(actual_checksum)s') % {
                     'image': image_location,
                     'checksum': expected_checksum,
                     'actual_checksum': actual_checksum
                 })
        LOG.error(msg)
        raise exception.ImageRefValidationFailed(image_href=image_location,
                                                 reason=msg)
Esempio n. 2
0
def verify_image_checksum(image_location, expected_checksum):
    """Verifies checksum (md5) of image file against the expected one.

    This method generates the checksum of the image file on the fly and
    verifies it against the expected checksum provided as argument.

    :param image_location: location of image file whose checksum is verified.
    :param expected_checksum: checksum to be checked against
    :raises: ImageRefValidationFailed, if invalid file path or
             verification fails.
    """
    try:
        with open(image_location, 'rb') as fd:
            actual_checksum = utils.hash_file(fd)
    except IOError as e:
        LOG.error(_LE("Error opening file: %(file)s"),
                  {'file': image_location})
        raise exception.ImageRefValidationFailed(image_href=image_location,
                                                 reason=six.text_type(e))

    if actual_checksum != expected_checksum:
        msg = (_('Error verifying image checksum. Image %(image)s failed to '
                 'verify against checksum %(checksum)s. Actual checksum is: '
                 '%(actual_checksum)s') %
               {'image': image_location, 'checksum': expected_checksum,
                'actual_checksum': actual_checksum})
        LOG.error(msg)
        raise exception.ImageRefValidationFailed(image_href=image_location,
                                                 reason=msg)
Esempio n. 3
0
 def test_hash_file_for_sha512(self):
     # | GIVEN |
     data = b'Mary had a little lamb, its fleece as white as snow'
     file_like_object = six.BytesIO(data)
     expected = hashlib.sha512(data).hexdigest()
     # | WHEN |
     actual = utils.hash_file(file_like_object, 'sha512')
     # | THEN |
     self.assertEqual(expected, actual)
Esempio n. 4
0
 def test_hash_file_for_md5_not_binary(self):
     # | GIVEN |
     data = u'Mary had a little lamb, its fleece as white as sno\u0449'
     file_like_object = six.StringIO(data)
     expected = hashlib.md5(data.encode('utf-8')).hexdigest()
     # | WHEN |
     actual = utils.hash_file(file_like_object)  # using default, 'md5'
     # | THEN |
     self.assertEqual(expected, actual)
Esempio n. 5
0
 def test_hash_file_for_sha512(self):
     # | GIVEN |
     data = b"Mary had a little lamb, its fleece as white as snow"
     file_like_object = six.BytesIO(data)
     expected = hashlib.sha512(data).hexdigest()
     # | WHEN |
     actual = utils.hash_file(file_like_object, "sha512")
     # | THEN |
     self.assertEqual(expected, actual)
Esempio n. 6
0
 def test_hash_file_for_md5(self):
     # | GIVEN |
     data = b"Mary had a little lamb, its fleece as white as snow"
     file_like_object = six.BytesIO(data)
     expected = hashlib.md5(data).hexdigest()
     # | WHEN |
     actual = utils.hash_file(file_like_object)  # using default, 'md5'
     # | THEN |
     self.assertEqual(expected, actual)
Esempio n. 7
0
 def test_hash_file_for_md5_not_binary(self):
     # | GIVEN |
     data = u'Mary had a little lamb, its fleece as white as sno\u0449'
     file_like_object = six.StringIO(data)
     expected = hashlib.md5(data.encode('utf-8')).hexdigest()
     # | WHEN |
     actual = utils.hash_file(file_like_object)  # using default, 'md5'
     # | THEN |
     self.assertEqual(expected, actual)
Esempio n. 8
0
 def test_hash_file(self):
     data = 'Mary had a little lamb, its fleece as white as snow'
     flo = six.StringIO(data)
     h1 = utils.hash_file(flo)
     h2 = hashlib.sha1(data).hexdigest()
     self.assertEqual(h1, h2)
Esempio n. 9
0
 def test_hash_file(self):
     data = 'Mary had a little lamb, its fleece as white as snow'
     flo = six.StringIO(data)
     h1 = utils.hash_file(flo)
     h2 = hashlib.sha1(data).hexdigest()
     self.assertEqual(h1, h2)