From 108605b01873c4176275cc6bf2ea0d0b7c447a0e Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 4 Jan 2014 13:37:00 -0600 Subject: add commoncrypto mark to skip on non-OS X platforms --- cryptography/hazmat/bindings/openssl/binding.py | 8 ++++++- cryptography/hazmat/bindings/utils.py | 8 +++++++ pytest.ini | 1 + tests/conftest.py | 5 +++- tests/hazmat/bindings/test_bindings.py | 31 +++++++++++++++++++++++++ tests/hazmat/bindings/test_openssl.py | 3 +++ tests/test_utils.py | 31 ++++++++++++++++++++++++- tests/utils.py | 6 +++++ 8 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 tests/hazmat/bindings/test_bindings.py diff --git a/cryptography/hazmat/bindings/openssl/binding.py b/cryptography/hazmat/bindings/openssl/binding.py index 4f6b99ae..2a1e1184 100644 --- a/cryptography/hazmat/bindings/openssl/binding.py +++ b/cryptography/hazmat/bindings/openssl/binding.py @@ -13,7 +13,9 @@ from __future__ import absolute_import, division, print_function -from cryptography.hazmat.bindings.utils import build_ffi +from cryptography.hazmat.bindings.utils import ( + build_ffi, binding_available +) _OSX_PRE_INCLUDE = """ #ifdef __APPLE__ @@ -79,3 +81,7 @@ class Binding(object): cls.ffi, cls.lib = build_ffi(cls._module_prefix, cls._modules, _OSX_PRE_INCLUDE, _OSX_POST_INCLUDE, ["crypto", "ssl"]) + + @classmethod + def is_available(cls): + return binding_available(cls._ensure_ffi_initialized) diff --git a/cryptography/hazmat/bindings/utils.py b/cryptography/hazmat/bindings/utils.py index 9e1d3937..9141c155 100644 --- a/cryptography/hazmat/bindings/utils.py +++ b/cryptography/hazmat/bindings/utils.py @@ -86,3 +86,11 @@ def build_ffi(module_prefix, modules, pre_include, post_include, libraries): delattr(lib, name) return ffi, lib + + +def binding_available(initializer): + try: + initializer() + return True + except cffi.VerificationError: + return False diff --git a/pytest.ini b/pytest.ini index 36d4edc4..4bb9b0f9 100644 --- a/pytest.ini +++ b/pytest.ini @@ -5,3 +5,4 @@ markers = cipher: this test requires a backend providing CipherBackend hash: this test requires a backend providing HashBackend supported: parametrized test requiring only_if and skip_message + binding_available: verifies a given binding is available diff --git a/tests/conftest.py b/tests/conftest.py index 0ddc3338..3ba2425d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,9 @@ from cryptography.hazmat.backends.interfaces import ( HMACBackend, CipherBackend, HashBackend ) -from .utils import check_for_iface, check_backend_support +from .utils import ( + check_for_iface, check_backend_support, check_binding_available +) def pytest_generate_tests(metafunc): @@ -20,3 +22,4 @@ def pytest_runtest_setup(item): check_for_iface("cipher", CipherBackend, item) check_for_iface("hash", HashBackend, item) check_backend_support(item) + check_binding_available(item) diff --git a/tests/hazmat/bindings/test_bindings.py b/tests/hazmat/bindings/test_bindings.py new file mode 100644 index 00000000..927bc8a1 --- /dev/null +++ b/tests/hazmat/bindings/test_bindings.py @@ -0,0 +1,31 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +# implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from __future__ import absolute_import, division, print_function + +import cffi + +from cryptography.hazmat.bindings.utils import binding_available +from cryptography.hazmat.bindings.openssl.binding import Binding + + +def dummy_initializer(): + raise cffi.VerificationError + + +def test_binding_available(): + assert binding_available(Binding._ensure_ffi_initialized) is True + + +def test_binding_unavailable(): + assert binding_available(dummy_initializer) is False diff --git a/tests/hazmat/bindings/test_openssl.py b/tests/hazmat/bindings/test_openssl.py index 31f736ab..d1e85058 100644 --- a/tests/hazmat/bindings/test_openssl.py +++ b/tests/hazmat/bindings/test_openssl.py @@ -20,3 +20,6 @@ class TestOpenSSL(object): assert binding assert binding.lib assert binding.ffi + + def test_is_available(self): + assert Binding.is_available() is True diff --git a/tests/test_utils.py b/tests/test_utils.py index e3e53d63..917e87f0 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -14,14 +14,18 @@ import os import textwrap +import cffi + import pretend import pytest +from cryptography.hazmat.bindings.utils import binding_available + from .utils import ( load_nist_vectors, load_vectors_from_file, load_cryptrec_vectors, load_openssl_vectors, load_hash_vectors, check_for_iface, - check_backend_support + check_backend_support, check_binding_available ) @@ -72,6 +76,31 @@ def test_check_backend_support_no_backend(): check_backend_support(item) +def test_check_binding_available(): + from cryptography.hazmat.bindings.openssl.binding import Binding + kwargs = pretend.stub(kwargs={"binding": Binding}) + item = pretend.stub(keywords={"binding_available": kwargs}) + assert check_binding_available(item) is None + + +def test_check_binding_unavailable(): + class FakeBinding(object): + @classmethod + def _ensure_ffi_initialized(cls): + raise cffi.VerificationError + + @classmethod + def is_available(cls): + return binding_available(cls._ensure_ffi_initialized) + + kwargs = pretend.stub(kwargs={"binding": FakeBinding}) + item = pretend.stub(keywords={"binding_available": kwargs}) + with pytest.raises(pytest.skip.Exception) as exc_info: + check_binding_available(item) + assert exc_info.value.args[0] == ("" + " is not available") + + def test_load_nist_vectors(): vector_data = textwrap.dedent(""" # CAVS 11.1 diff --git a/tests/utils.py b/tests/utils.py index 693a0c8f..6d47a398 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -36,6 +36,12 @@ def check_backend_support(item): "backend") +def check_binding_available(item): + ba = item.keywords.get("binding_available") + if ba and not ba.kwargs["binding"].is_available(): + pytest.skip("{0} is not available".format(ba.kwargs["binding"])) + + def load_vectors_from_file(filename, loader): base = os.path.join( os.path.dirname(__file__), "hazmat", "primitives", "vectors", -- cgit v1.2.3 From 3ae13fc662435d69c7d93e937a675059164a495c Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 4 Jan 2014 17:43:35 -0600 Subject: fix py3 --- tests/test_utils.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/tests/test_utils.py b/tests/test_utils.py index 917e87f0..6f938f58 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -33,6 +33,16 @@ class FakeInterface(object): pass +class FakeBinding(object): + @classmethod + def _ensure_ffi_initialized(cls): + raise cffi.VerificationError + + @classmethod + def is_available(cls): + return binding_available(cls._ensure_ffi_initialized) + + def test_check_for_iface(): item = pretend.stub(keywords=["fake_name"], funcargs={"backend": True}) with pytest.raises(pytest.skip.Exception) as exc_info: @@ -84,15 +94,6 @@ def test_check_binding_available(): def test_check_binding_unavailable(): - class FakeBinding(object): - @classmethod - def _ensure_ffi_initialized(cls): - raise cffi.VerificationError - - @classmethod - def is_available(cls): - return binding_available(cls._ensure_ffi_initialized) - kwargs = pretend.stub(kwargs={"binding": FakeBinding}) item = pretend.stub(keywords={"binding_available": kwargs}) with pytest.raises(pytest.skip.Exception) as exc_info: -- cgit v1.2.3 From 2dd21fec484c85647d73145bd9957fd5326495c3 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 4 Jan 2014 18:13:40 -0600 Subject: remove unneeded mark now that is_available is there --- pytest.ini | 1 - tests/conftest.py | 5 +---- tests/test_utils.py | 32 +------------------------------- tests/utils.py | 6 ------ 4 files changed, 2 insertions(+), 42 deletions(-) diff --git a/pytest.ini b/pytest.ini index 4bb9b0f9..36d4edc4 100644 --- a/pytest.ini +++ b/pytest.ini @@ -5,4 +5,3 @@ markers = cipher: this test requires a backend providing CipherBackend hash: this test requires a backend providing HashBackend supported: parametrized test requiring only_if and skip_message - binding_available: verifies a given binding is available diff --git a/tests/conftest.py b/tests/conftest.py index 3ba2425d..0ddc3338 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,9 +4,7 @@ from cryptography.hazmat.backends.interfaces import ( HMACBackend, CipherBackend, HashBackend ) -from .utils import ( - check_for_iface, check_backend_support, check_binding_available -) +from .utils import check_for_iface, check_backend_support def pytest_generate_tests(metafunc): @@ -22,4 +20,3 @@ def pytest_runtest_setup(item): check_for_iface("cipher", CipherBackend, item) check_for_iface("hash", HashBackend, item) check_backend_support(item) - check_binding_available(item) diff --git a/tests/test_utils.py b/tests/test_utils.py index 6f938f58..e3e53d63 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -14,18 +14,14 @@ import os import textwrap -import cffi - import pretend import pytest -from cryptography.hazmat.bindings.utils import binding_available - from .utils import ( load_nist_vectors, load_vectors_from_file, load_cryptrec_vectors, load_openssl_vectors, load_hash_vectors, check_for_iface, - check_backend_support, check_binding_available + check_backend_support ) @@ -33,16 +29,6 @@ class FakeInterface(object): pass -class FakeBinding(object): - @classmethod - def _ensure_ffi_initialized(cls): - raise cffi.VerificationError - - @classmethod - def is_available(cls): - return binding_available(cls._ensure_ffi_initialized) - - def test_check_for_iface(): item = pretend.stub(keywords=["fake_name"], funcargs={"backend": True}) with pytest.raises(pytest.skip.Exception) as exc_info: @@ -86,22 +72,6 @@ def test_check_backend_support_no_backend(): check_backend_support(item) -def test_check_binding_available(): - from cryptography.hazmat.bindings.openssl.binding import Binding - kwargs = pretend.stub(kwargs={"binding": Binding}) - item = pretend.stub(keywords={"binding_available": kwargs}) - assert check_binding_available(item) is None - - -def test_check_binding_unavailable(): - kwargs = pretend.stub(kwargs={"binding": FakeBinding}) - item = pretend.stub(keywords={"binding_available": kwargs}) - with pytest.raises(pytest.skip.Exception) as exc_info: - check_binding_available(item) - assert exc_info.value.args[0] == ("" - " is not available") - - def test_load_nist_vectors(): vector_data = textwrap.dedent(""" # CAVS 11.1 diff --git a/tests/utils.py b/tests/utils.py index 6d47a398..693a0c8f 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -36,12 +36,6 @@ def check_backend_support(item): "backend") -def check_binding_available(item): - ba = item.keywords.get("binding_available") - if ba and not ba.kwargs["binding"].is_available(): - pytest.skip("{0} is not available".format(ba.kwargs["binding"])) - - def load_vectors_from_file(filename, loader): base = os.path.join( os.path.dirname(__file__), "hazmat", "primitives", "vectors", -- cgit v1.2.3 From fefe3c224353e4e37b02f1df0fc9558f68f8c464 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 4 Jan 2014 18:43:19 -0600 Subject: make the dummy_initializer fail with an actual verify call --- tests/hazmat/bindings/test_bindings.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/hazmat/bindings/test_bindings.py b/tests/hazmat/bindings/test_bindings.py index 927bc8a1..5b13d543 100644 --- a/tests/hazmat/bindings/test_bindings.py +++ b/tests/hazmat/bindings/test_bindings.py @@ -20,7 +20,8 @@ from cryptography.hazmat.bindings.openssl.binding import Binding def dummy_initializer(): - raise cffi.VerificationError + ffi = cffi.FFI() + ffi.verify(source="include ") def test_binding_available(): -- cgit v1.2.3 From 02ed961c963f0d27fe23e9608223ccc8dd3be7f6 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 4 Jan 2014 19:36:39 -0600 Subject: missing # --- tests/hazmat/bindings/test_bindings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/hazmat/bindings/test_bindings.py b/tests/hazmat/bindings/test_bindings.py index 5b13d543..7af1d581 100644 --- a/tests/hazmat/bindings/test_bindings.py +++ b/tests/hazmat/bindings/test_bindings.py @@ -21,7 +21,7 @@ from cryptography.hazmat.bindings.openssl.binding import Binding def dummy_initializer(): ffi = cffi.FFI() - ffi.verify(source="include ") + ffi.verify(source="#include ") def test_binding_available(): -- cgit v1.2.3