From 34c075e6f331d146a617417e646170e8847c39e4 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 13 Jan 2014 21:52:08 -0500 Subject: support --backend as a pytest flag to limit to one backend for testing --- cryptography/hazmat/backends/openssl/backend.py | 1 + tests/conftest.py | 14 +++++++++++++- tests/test_utils.py | 23 ++++++++++++++++++++++- tests/utils.py | 10 ++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/cryptography/hazmat/backends/openssl/backend.py b/cryptography/hazmat/backends/openssl/backend.py index 07ee58c1..ee82ba71 100644 --- a/cryptography/hazmat/backends/openssl/backend.py +++ b/cryptography/hazmat/backends/openssl/backend.py @@ -37,6 +37,7 @@ class Backend(object): """ OpenSSL API binding interfaces. """ + name = "openssl" def __init__(self): self._binding = Binding() diff --git a/tests/conftest.py b/tests/conftest.py index 1d9f96ed..5cc30428 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,7 +5,12 @@ 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, modify_backend_list + + +def pytest_generate_tests(metafunc): + name = metafunc.config.getoption("--backend") + modify_backend_list(name, _ALL_BACKENDS) @pytest.fixture(params=_ALL_BACKENDS) @@ -19,3 +24,10 @@ def pytest_runtest_setup(item): check_for_iface("cipher", CipherBackend, item) check_for_iface("hash", HashBackend, item) check_backend_support(item) + + +def pytest_addoption(parser): + parser.addoption( + "--backend", action="store", metavar="NAME", + help="Only run tests matching the backend NAME." + ) diff --git a/tests/test_utils.py b/tests/test_utils.py index e3e53d63..e8037c1d 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -21,7 +21,7 @@ import pytest 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, modify_backend_list ) @@ -29,6 +29,27 @@ class FakeInterface(object): pass +def test_modify_backend_list_leave_one(): + b1 = pretend.stub(name="b1") + b2 = pretend.stub(name="b2") + b3 = pretend.stub(name="b3") + backends = [b1, b2, b3] + name = "b2" + modify_backend_list(name, backends) + assert len(backends) == 1 + assert backends[0] == b2 + + +def test_modify_backend_list_error_none(): + b1 = pretend.stub(name="b1") + b2 = pretend.stub(name="b2") + b3 = pretend.stub(name="b3") + backends = [b1, b2, b3] + name = "back!" + with pytest.raises(ValueError): + modify_backend_list(name, backends) + + def test_check_for_iface(): item = pretend.stub(keywords=["fake_name"], funcargs={"backend": True}) with pytest.raises(pytest.skip.Exception) as exc_info: diff --git a/tests/utils.py b/tests/utils.py index 693a0c8f..343f3efe 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -16,6 +16,16 @@ import os import pytest +def modify_backend_list(name, all_backends): + if name is not None: + backends = list(all_backends) + for backend in backends: + if backend.name != name: + all_backends.remove(backend) + if len(all_backends) == 0: + raise ValueError("No backends selected for testing") + + def check_for_iface(name, iface, item): if name in item.keywords and "backend" in item.funcargs: if not isinstance(item.funcargs["backend"], iface): -- cgit v1.2.3 From c1fd2be467947c455aef7687a7ecaa4de1f300b1 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 13 Jan 2014 21:55:12 -0500 Subject: pass posargs via tox so --backend can be used for tox envs --- tox.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index ff5df360..5ff08779 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,7 @@ deps = pretend pytest commands = - coverage run --source=cryptography/,tests/ -m pytest --capture=no --strict + coverage run --source=cryptography/,tests/ -m pytest --capture=no --strict {posargs} coverage report -m [testenv:docs] @@ -28,7 +28,7 @@ commands = # Temporarily disable coverage on pypy because of performance problems with # coverage.py on pypy. [testenv:pypy] -commands = py.test --capture=no --strict +commands = py.test --capture=no --strict {posargs} [testenv:pep8] deps = flake8 -- cgit v1.2.3 From 098579e27f380ce95f620b34984f20342db2395f Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 14 Jan 2014 10:26:44 -0500 Subject: don't mutate _ALL_BACKENDS --- tests/conftest.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 5cc30428..86debe7c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,12 +8,19 @@ from cryptography.hazmat.backends.interfaces import ( from .utils import check_for_iface, check_backend_support, modify_backend_list +# copy all backends so we can mutate it.This variable is used in generate +# tests to allow us to target a single backend without changing _ALL_BACKENDS + +_UPDATED_BACKENDS = list(_ALL_BACKENDS) + + def pytest_generate_tests(metafunc): + global _UPDATED_BACKENDS name = metafunc.config.getoption("--backend") - modify_backend_list(name, _ALL_BACKENDS) + modify_backend_list(name, _UPDATED_BACKENDS) -@pytest.fixture(params=_ALL_BACKENDS) +@pytest.fixture(params=_UPDATED_BACKENDS) def backend(request): return request.param -- cgit v1.2.3 From 681e7a5587e78918fd15af5255204216d0ea7237 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Tue, 14 Jan 2014 10:48:56 -0500 Subject: better name for the variable --- tests/conftest.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 86debe7c..49e178bc 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -10,17 +10,16 @@ from .utils import check_for_iface, check_backend_support, modify_backend_list # copy all backends so we can mutate it.This variable is used in generate # tests to allow us to target a single backend without changing _ALL_BACKENDS - -_UPDATED_BACKENDS = list(_ALL_BACKENDS) +_DESIRED_BACKENDS = list(_ALL_BACKENDS) def pytest_generate_tests(metafunc): - global _UPDATED_BACKENDS + global _DESIRED_BACKENDS name = metafunc.config.getoption("--backend") - modify_backend_list(name, _UPDATED_BACKENDS) + modify_backend_list(name, _DESIRED_BACKENDS) -@pytest.fixture(params=_UPDATED_BACKENDS) +@pytest.fixture(params=_DESIRED_BACKENDS) def backend(request): return request.param -- cgit v1.2.3 From c421e636b15768e1adaf8bf681ecdd12b96c8669 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 18 Jan 2014 09:22:21 -0600 Subject: modify backend selection to allow multiple backends via comma delimiter --- tests/conftest.py | 12 ++++++------ tests/test_utils.py | 31 ++++++++++++++++++++++++++----- tests/utils.py | 25 +++++++++++++++++-------- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 49e178bc..b9879f86 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -5,21 +5,21 @@ from cryptography.hazmat.backends.interfaces import ( HMACBackend, CipherBackend, HashBackend ) -from .utils import check_for_iface, check_backend_support, modify_backend_list +from .utils import check_for_iface, check_backend_support, select_backends # copy all backends so we can mutate it.This variable is used in generate # tests to allow us to target a single backend without changing _ALL_BACKENDS -_DESIRED_BACKENDS = list(_ALL_BACKENDS) +_SELECTED_BACKENDS = list(_ALL_BACKENDS) def pytest_generate_tests(metafunc): - global _DESIRED_BACKENDS - name = metafunc.config.getoption("--backend") - modify_backend_list(name, _DESIRED_BACKENDS) + global _SELECTED_BACKENDS + names = metafunc.config.getoption("--backend") + _SELECTED_BACKENDS = select_backends(names, _SELECTED_BACKENDS) -@pytest.fixture(params=_DESIRED_BACKENDS) +@pytest.fixture(params=_SELECTED_BACKENDS) def backend(request): return request.param diff --git a/tests/test_utils.py b/tests/test_utils.py index e8037c1d..a7da4906 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -21,7 +21,7 @@ import pytest 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, modify_backend_list + check_backend_support, select_backends ) @@ -29,25 +29,46 @@ class FakeInterface(object): pass -def test_modify_backend_list_leave_one(): +def test_select_one_backend(): b1 = pretend.stub(name="b1") b2 = pretend.stub(name="b2") b3 = pretend.stub(name="b3") backends = [b1, b2, b3] name = "b2" - modify_backend_list(name, backends) + select_backends(name, backends) assert len(backends) == 1 assert backends[0] == b2 -def test_modify_backend_list_error_none(): +def test_select_no_backend(): b1 = pretend.stub(name="b1") b2 = pretend.stub(name="b2") b3 = pretend.stub(name="b3") backends = [b1, b2, b3] name = "back!" with pytest.raises(ValueError): - modify_backend_list(name, backends) + select_backends(name, backends) + + +def test_select_backends_none(): + b1 = pretend.stub(name="b1") + b2 = pretend.stub(name="b2") + b3 = pretend.stub(name="b3") + backends = [b1, b2, b3] + name = None + select_backends(name, backends) + assert len(backends) == 3 + + +def test_select_two_backends(): + b1 = pretend.stub(name="b1") + b2 = pretend.stub(name="b2") + b3 = pretend.stub(name="b3") + backends = [b1, b2, b3] + name = "b2 ,b1 " + select_backends(name, backends) + assert len(backends) == 2 + assert backends == [b1, b2] def test_check_for_iface(): diff --git a/tests/utils.py b/tests/utils.py index 343f3efe..ee1675e8 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -16,14 +16,23 @@ import os import pytest -def modify_backend_list(name, all_backends): - if name is not None: - backends = list(all_backends) - for backend in backends: - if backend.name != name: - all_backends.remove(backend) - if len(all_backends) == 0: - raise ValueError("No backends selected for testing") +def select_backends(names, backend_list): + if names is None: + return backend_list + split_names = [x.strip() for x in names.split(',')] + # this must be duplicated and then removed to preserve the metadata + # pytest associates. Appending backends to a new list doesn't seem to work + backends = list(backend_list) + for backend in backends: + if backend.name not in split_names: + backend_list.remove(backend) + + if len(backend_list) > 0: + return backend_list + else: + raise ValueError( + "No backend selected. Tried to select: {0}".format(split_names) + ) def check_for_iface(name, iface, item): -- cgit v1.2.3 From 2502ce599b39509ba9c05b6e553527e5ed5ac43c Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sat, 18 Jan 2014 09:32:47 -0600 Subject: docs for explicit backend selection and document name attribute of backend --- docs/contributing.rst | 10 ++++++++++ docs/hazmat/backends/openssl.rst | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/docs/contributing.rst b/docs/contributing.rst index 8e32c368..3710abee 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -250,6 +250,16 @@ each supported Python version and run the tests. For example: You may not have all the required Python versions installed, in which case you will see one or more ``InterpreterNotFound`` errors. + +Explicit Backend Selection +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +While testing you may want to run tests against a subset of the backends that +cryptography supports. Explicit backend selection can be done via the +``--backend`` flag. This flag should be passed to ``py.test`` with a comma +delimited list of backend names. To use it with ``tox`` you must pass it as +``-- --backend``. + Building Documentation ~~~~~~~~~~~~~~~~~~~~~~ diff --git a/docs/hazmat/backends/openssl.rst b/docs/hazmat/backends/openssl.rst index 404573a3..042ee87d 100644 --- a/docs/hazmat/backends/openssl.rst +++ b/docs/hazmat/backends/openssl.rst @@ -7,7 +7,11 @@ The `OpenSSL`_ C library. .. data:: cryptography.hazmat.backends.openssl.backend - This is the exposed API for the OpenSSL backend. It has no public attributes. + This is the exposed API for the OpenSSL backend. It has one public attribute. + + .. attribute:: name + + The string name of the backend. Using your own OpenSSL on Linux ------------------------------- -- cgit v1.2.3 From aed9e17b6080d540ba5b5aab5e3096581a4bbd13 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 12:09:27 -0600 Subject: revert fixture decorator for now, switch to append. no more globals --- tests/conftest.py | 14 +++----------- tests/test_utils.py | 16 ++++++++-------- tests/utils.py | 12 ++++++------ 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index b9879f86..a9acb54a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,20 +8,12 @@ from cryptography.hazmat.backends.interfaces import ( from .utils import check_for_iface, check_backend_support, select_backends -# copy all backends so we can mutate it.This variable is used in generate -# tests to allow us to target a single backend without changing _ALL_BACKENDS -_SELECTED_BACKENDS = list(_ALL_BACKENDS) - - def pytest_generate_tests(metafunc): - global _SELECTED_BACKENDS names = metafunc.config.getoption("--backend") - _SELECTED_BACKENDS = select_backends(names, _SELECTED_BACKENDS) - + selected_backends = select_backends(names, _ALL_BACKENDS) -@pytest.fixture(params=_SELECTED_BACKENDS) -def backend(request): - return request.param + if "backend" in metafunc.fixturenames: + metafunc.parametrize("backend", selected_backends) @pytest.mark.trylast diff --git a/tests/test_utils.py b/tests/test_utils.py index a7da4906..f852f3ab 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -35,9 +35,9 @@ def test_select_one_backend(): b3 = pretend.stub(name="b3") backends = [b1, b2, b3] name = "b2" - select_backends(name, backends) - assert len(backends) == 1 - assert backends[0] == b2 + selected_backends = select_backends(name, backends) + assert len(selected_backends) == 1 + assert selected_backends[0] == b2 def test_select_no_backend(): @@ -56,8 +56,8 @@ def test_select_backends_none(): b3 = pretend.stub(name="b3") backends = [b1, b2, b3] name = None - select_backends(name, backends) - assert len(backends) == 3 + selected_backends = select_backends(name, backends) + assert len(selected_backends) == 3 def test_select_two_backends(): @@ -66,9 +66,9 @@ def test_select_two_backends(): b3 = pretend.stub(name="b3") backends = [b1, b2, b3] name = "b2 ,b1 " - select_backends(name, backends) - assert len(backends) == 2 - assert backends == [b1, b2] + selected_backends = select_backends(name, backends) + assert len(selected_backends) == 2 + assert selected_backends == [b1, b2] def test_check_for_iface(): diff --git a/tests/utils.py b/tests/utils.py index ee1675e8..a2432256 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -22,13 +22,13 @@ def select_backends(names, backend_list): split_names = [x.strip() for x in names.split(',')] # this must be duplicated and then removed to preserve the metadata # pytest associates. Appending backends to a new list doesn't seem to work - backends = list(backend_list) - for backend in backends: - if backend.name not in split_names: - backend_list.remove(backend) + selected_backends = [] + for backend in backend_list: + if backend.name in split_names: + selected_backends.append(backend) - if len(backend_list) > 0: - return backend_list + if len(selected_backends) > 0: + return selected_backends else: raise ValueError( "No backend selected. Tried to select: {0}".format(split_names) -- cgit v1.2.3 From 5882361dbd8b0f6f9c7933b173bc933cbd90097a Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 13:31:35 -0600 Subject: update docs for name attribute --- docs/hazmat/backends/openssl.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/hazmat/backends/openssl.rst b/docs/hazmat/backends/openssl.rst index 042ee87d..b0bd3ae0 100644 --- a/docs/hazmat/backends/openssl.rst +++ b/docs/hazmat/backends/openssl.rst @@ -9,9 +9,9 @@ The `OpenSSL`_ C library. This is the exposed API for the OpenSSL backend. It has one public attribute. - .. attribute:: name + .. attribute:: name - The string name of the backend. + Returns ``openssl``, the string name of this backend. Using your own OpenSSL on Linux ------------------------------- -- cgit v1.2.3 From cfa2d6275e9c31e6fc61109e4853c5687d16532e Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 19 Jan 2014 14:01:25 -0600 Subject: fix docs --- docs/hazmat/backends/openssl.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/hazmat/backends/openssl.rst b/docs/hazmat/backends/openssl.rst index b0bd3ae0..a1f2d28a 100644 --- a/docs/hazmat/backends/openssl.rst +++ b/docs/hazmat/backends/openssl.rst @@ -9,9 +9,9 @@ The `OpenSSL`_ C library. This is the exposed API for the OpenSSL backend. It has one public attribute. - .. attribute:: name + .. attribute:: name - Returns ``openssl``, the string name of this backend. + The string name of this backend: ``"openssl"`` Using your own OpenSSL on Linux ------------------------------- -- cgit v1.2.3 From defc7f0534914db1b72ad5dfa0250f4fabc3184a Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 19 Jan 2014 23:44:31 -0600 Subject: On OS X at build time compile the CC bindings --- cryptography/hazmat/backends/commoncrypto/backend.py | 6 ++++-- cryptography/hazmat/bindings/openssl/dh.py | 12 ++++++++---- cryptography/hazmat/bindings/openssl/dsa.py | 15 ++++++++++----- setup.py | 13 +++++++++++-- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/cryptography/hazmat/backends/commoncrypto/backend.py b/cryptography/hazmat/backends/commoncrypto/backend.py index 58e57efb..603edc40 100644 --- a/cryptography/hazmat/backends/commoncrypto/backend.py +++ b/cryptography/hazmat/backends/commoncrypto/backend.py @@ -81,16 +81,18 @@ class Backend(object): def hash_supported(self, algorithm): try: self._hash_mapping[algorithm.name] - return True except KeyError: return False + else: + return True def hmac_supported(self, algorithm): try: self._supported_hmac_algorithms[algorithm.name] - return True except KeyError: return False + else: + return True def create_hash_ctx(self, algorithm): return _HashContext(self, algorithm) diff --git a/cryptography/hazmat/bindings/openssl/dh.py b/cryptography/hazmat/bindings/openssl/dh.py index edbe0e39..ecc62e98 100644 --- a/cryptography/hazmat/bindings/openssl/dh.py +++ b/cryptography/hazmat/bindings/openssl/dh.py @@ -17,10 +17,14 @@ INCLUDES = """ TYPES = """ typedef struct dh_st { - BIGNUM *p; // prime number (shared) - BIGNUM *g; // generator of Z_p (shared) - BIGNUM *priv_key; // private DH value x - BIGNUM *pub_key; // public DH value g^x + // prime number (shared) + BIGNUM *p; + // generator of Z_p (shared) + BIGNUM *g; + // private DH value x + BIGNUM *priv_key; + // public DH value g^x + BIGNUM *pub_key; ...; } DH; """ diff --git a/cryptography/hazmat/bindings/openssl/dsa.py b/cryptography/hazmat/bindings/openssl/dsa.py index 9068e057..609a33bf 100644 --- a/cryptography/hazmat/bindings/openssl/dsa.py +++ b/cryptography/hazmat/bindings/openssl/dsa.py @@ -17,11 +17,16 @@ INCLUDES = """ TYPES = """ typedef struct dsa_st { - BIGNUM *p; // prime number (public) - BIGNUM *q; // 160-bit subprime, q | p-1 (public) - BIGNUM *g; // generator of subgroup (public) - BIGNUM *priv_key; // private key x - BIGNUM *pub_key; // public key y = g^x + // prime number (public) + BIGNUM *p; + // 160-bit subprime, q | p-1 (public) + BIGNUM *q; + // generator of subgroup (public) + BIGNUM *g; + // private key x + BIGNUM *priv_key; + // public key y = g^x + BIGNUM *pub_key; ...; } DSA; """ diff --git a/setup.py b/setup.py index e8bcc11f..57a95752 100644 --- a/setup.py +++ b/setup.py @@ -43,14 +43,23 @@ class cffi_build(build): """ def finalize_options(self): - from cryptography.hazmat.bindings.openssl.binding import Binding + from cryptography.hazmat.bindings.commoncrypto.binding import ( + Binding as CommonCryptoBinding + ) + from cryptography.hazmat.bindings.openssl.binding import ( + Binding as OpenSSLBinding + ) from cryptography.hazmat.primitives import constant_time, padding self.distribution.ext_modules = [ - Binding().ffi.verifier.get_extension(), + OpenSSLBinding().ffi.verifier.get_extension(), constant_time._ffi.verifier.get_extension(), padding._ffi.verifier.get_extension() ] + if CommonCryptoBinding.is_available(): + self.distribution.ext_modules.append( + CommonCryptoBinding().ffi.verifier.get_extension() + ) build.finalize_options(self) -- cgit v1.2.3 From ad4f646e685beb38e597bab83ea8e8314a3fd581 Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Mon, 20 Jan 2014 09:36:57 -0600 Subject: expand tox backend example --- docs/contributing.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/contributing.rst b/docs/contributing.rst index 3710abee..4bb1461d 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -258,7 +258,7 @@ While testing you may want to run tests against a subset of the backends that cryptography supports. Explicit backend selection can be done via the ``--backend`` flag. This flag should be passed to ``py.test`` with a comma delimited list of backend names. To use it with ``tox`` you must pass it as -``-- --backend``. +``tox -- --backend=openssl``. Building Documentation ~~~~~~~~~~~~~~~~~~~~~~ -- cgit v1.2.3