コード例 #1
0
 def test_get_guaranteed(self):
     algorithm_and_hasher_factory = [('md5', hashlib.md5),
                                     ('sha1', hashlib.sha1),
                                     ('sha224', hashlib.sha224),
                                     ('sha256', hashlib.sha256),
                                     ('sha384', hashlib.sha384),
                                     ('sha512', hashlib.sha512)]
     assert algorithms_guaranteed == {
         a
         for a, _ in algorithm_and_hasher_factory
     }
     for algorithm, expected_hasher_factory in algorithm_and_hasher_factory:
         hasher_factory = _get_hasher_factory(algorithm)
         assert hasher_factory == expected_hasher_factory
コード例 #2
0
    def test_bypass_hasher_factory(self):

        # test standard hasher
        hasher_factory = _get_hasher_factory(hashlib.sha256)
        assert hasher_factory is hashlib.sha256

        # test raise on custom hasher with bad interface
        class IncompleteMockHasher(object):
            def __init__(self, *args, **kwargs):
                pass

            def update(self, *args, **kwargs):
                pass

        with pytest.raises(ValueError):
            _get_hasher_factory(IncompleteMockHasher)

        # test custom hasher with ok interface
        class MockHasher(IncompleteMockHasher):
            def hexdigest(self):
                return ''

        hasher_factory = _get_hasher_factory(MockHasher)
        assert hasher_factory is MockHasher
コード例 #3
0
 def test_not_available(self):
     with pytest.raises(ValueError):
         _get_hasher_factory('not available')
コード例 #4
0
 def test_get_available(self):
     for algorithm in algorithms_available:
         hasher_factory = _get_hasher_factory(algorithm)
         hasher = hasher_factory()
         assert hasattr(hasher, 'update')
         assert hasattr(hasher, 'hexdigest')