Beispiel #1
0
    def test_raises_NotImplementedError_if_function_not_supported(self):
        _sha1 = hashlib.sha1
        with self.assertRaisesRegexp(NotImplementedError,
                                     "sha-1 not supported by hashlib") as ctx:
            del hashlib.sha1
            try:
                hashes.hash_from_algo("sha-1")
            finally:
                hashlib.sha1 = _sha1

        self.assertIsInstance(ctx.exception.__cause__, AttributeError)
Beispiel #2
0
    def test_raises_NotImplementedError_if_function_not_supported(self):
        _sha1 = hashlib.sha1
        with self.assertRaisesRegexp(
                NotImplementedError,
                "sha-1 not supported by hashlib") as ctx:
            del hashlib.sha1
            try:
                hashes.hash_from_algo("sha-1")
            finally:
                hashlib.sha1 = _sha1

        self.assertIsInstance(ctx.exception.__cause__, AttributeError)
Beispiel #3
0
    def test_selection(self):
        selected = set(hashes.default_hash_algorithms)
        self.assertIn(
            "sha-256",
            selected,
        )

        try:
            hashes.hash_from_algo("sha3-256")
        except NotImplementedError:
            self.assertNotIn("sha3-256", selected)
        else:
            self.assertIn("sha3-256", selected)

        try:
            hashes.hash_from_algo("blake2b-256")
        except NotImplementedError:
            self.assertNotIn("blake2b-256", selected)
        else:
            self.assertIn("blake2b-256", selected)
Beispiel #4
0
    def test_selection(self):
        selected = set(hashes.default_hash_algorithms)
        self.assertIn(
            "sha-256",
            selected,
        )

        try:
            hashes.hash_from_algo("sha3-256")
        except NotImplementedError:
            self.assertNotIn("sha3-256", selected)
        else:
            self.assertIn("sha3-256", selected)

        try:
            hashes.hash_from_algo("blake2b-256")
        except NotImplementedError:
            self.assertNotIn("blake2b-256", selected)
        else:
            self.assertIn("blake2b-256", selected)
Beispiel #5
0
    def test_all_supported(self):
        for algo_name, (enabled, (fun_name, fun_args,
                                  fun_kwargs)) in hashes._HASH_ALGO_MAPPING:
            if not enabled:
                continue

            try:
                impl = hashes.hash_from_algo(algo_name)
            except NotImplementedError:
                continue

            self.assertEqual(algo_name, hashes.algo_of_hash(impl))
Beispiel #6
0
    def test_all_supported(self):
        for algo_name, (enabled, (fun_name, fun_args,
                                  fun_kwargs)) in hashes._HASH_ALGO_MAPPING:
            if not enabled:
                continue

            with unittest.mock.patch("hashlib.{}".format(fun_name),
                                     create=True) as hash_impl:
                result = hashes.hash_from_algo(algo_name)

            hash_impl.assert_called_once_with(*fun_args, **fun_kwargs)

            self.assertEqual(
                result,
                hash_impl(),
            )
Beispiel #7
0
    def test_all_supported(self):
        for algo_name, (enabled,
                        (fun_name,
                         fun_args,
                         fun_kwargs)) in hashes._HASH_ALGO_MAPPING:
            if not enabled:
                continue

            try:
                impl = hashes.hash_from_algo(algo_name)
            except NotImplementedError:
                continue

            self.assertEqual(
                algo_name,
                hashes.algo_of_hash(impl)
            )
Beispiel #8
0
    def test_raise_ValueError_for_MUST_NOT_hashes(self):
        with self.assertRaisesRegexp(ValueError,
                                     "support of md2 in XMPP is forbidden"):
            hashes.hash_from_algo("md2")

        with self.assertRaisesRegexp(ValueError,
                                     "support of md4 in XMPP is forbidden"):
            hashes.hash_from_algo("md4")

        with self.assertRaisesRegexp(ValueError,
                                     "support of md5 in XMPP is forbidden"):
            hashes.hash_from_algo("md5")
Beispiel #9
0
    def test_raise_ValueError_for_MUST_NOT_hashes(self):
        with self.assertRaisesRegexp(
                ValueError,
                "support of md2 in XMPP is forbidden"):
            hashes.hash_from_algo("md2")

        with self.assertRaisesRegexp(
                ValueError,
                "support of md4 in XMPP is forbidden"):
            hashes.hash_from_algo("md4")

        with self.assertRaisesRegexp(
                ValueError,
                "support of md5 in XMPP is forbidden"):
            hashes.hash_from_algo("md5")
Beispiel #10
0
    def test_all_supported(self):
        for algo_name, (enabled,
                        (fun_name,
                         fun_args,
                         fun_kwargs)) in hashes._HASH_ALGO_MAPPING:
            if not enabled:
                continue

            with unittest.mock.patch(
                    "hashlib.{}".format(fun_name),
                    create=True) as hash_impl:
                result = hashes.hash_from_algo(algo_name)

            hash_impl.assert_called_once_with(
                *fun_args,
                **fun_kwargs
            )

            self.assertEqual(
                result,
                hash_impl(),
            )
Beispiel #11
0
 def test_all_selected_can_be_instantiated(self):
     for algo in hashes.default_hash_algorithms:
         hashes.hash_from_algo(algo)
Beispiel #12
0
 def test_raises_NotImplementedError_if_function_not_defined(self):
     with self.assertRaisesRegexp(
             NotImplementedError,
             "hash algorithm 'foobar' unknown"):
         hashes.hash_from_algo("foobar")
Beispiel #13
0
 def test_all_selected_can_be_instantiated(self):
     for algo in hashes.default_hash_algorithms:
         hashes.hash_from_algo(algo)
Beispiel #14
0
 def test_raises_NotImplementedError_if_function_not_defined(self):
     with self.assertRaisesRegexp(NotImplementedError,
                                  "hash algorithm 'foobar' unknown"):
         hashes.hash_from_algo("foobar")