aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2013-08-07 10:04:32 -0700
committerAlex Gaynor <alex.gaynor@gmail.com>2013-08-07 10:04:32 -0700
commit370043071890c137ff51b21773fa5b1cb3f31806 (patch)
tree800567f5b35bc7a350821b1929f29980397d9041
parent226b29568cecbe5f8214f608c0b9f901b18586ed (diff)
downloadcryptography-370043071890c137ff51b21773fa5b1cb3f31806.tar.gz
cryptography-370043071890c137ff51b21773fa5b1cb3f31806.tar.bz2
cryptography-370043071890c137ff51b21773fa5b1cb3f31806.zip
Less code, more tests.
-rw-r--r--cryptography/c/api.py127
-rw-r--r--tests/__init__.py0
-rw-r--r--tests/test_c.py6
3 files changed, 6 insertions, 127 deletions
diff --git a/cryptography/c/api.py b/cryptography/c/api.py
index a5aa2729..ea8827f1 100644
--- a/cryptography/c/api.py
+++ b/cryptography/c/api.py
@@ -13,136 +13,9 @@
from __future__ import absolute_import, division, print_function
-import atexit
-from collections import namedtuple
-
-from cffi import FFI
-
class API(object):
"""OpenSSL API wrapper."""
- SSLVersion = namedtuple('SSLVersion',
- ['major', 'minor', 'fix', 'patch', 'status']
- )
-
- _modules = [
- ]
-
- def __init__(self):
- self.ffi = FFI()
- self.INCLUDES = []
- self.TYPES = []
- self.FUNCTIONS = []
- self.C_CUSTOMIZATION = []
- self.OVERRIDES = []
- self.SETUP = []
- self.TEARDOWN = []
- self._import()
- self._define()
- self._verify()
- self._override()
- self._populate()
- self._initialise()
-
- def _import(self):
- "import all library definitions"
- for name in self._modules:
- module = __import__(__name__ + '.' + name, fromlist=['*'])
- self._import_definitions(module, 'INCLUDES')
- self._import_definitions(module, 'TYPES')
- self._import_definitions(module, 'FUNCTIONS')
- self._import_definitions(module, 'C_CUSTOMIZATION')
- self._import_definitions(module, 'OVERRIDES')
- self._import_definitions(module, 'SETUP')
- self._import_definitions(module, 'TEARDOWN')
-
- def _import_definitions(self, module, name):
- "import defintions named definitions from module"
- container = getattr(self, name)
- for definition in getattr(module, name, ()):
- if definition not in container:
- container.append(definition)
-
- def _define(self):
- "parse function definitions"
- for typedef in self.TYPES:
- self.ffi.cdef(typedef)
- for function in self.FUNCTIONS:
- self.ffi.cdef(function)
-
- def _verify(self):
- "load openssl, create function attributes"
- self.openssl = self.ffi.verify(
- source="\n".join(self.INCLUDES + self.C_CUSTOMIZATION),
- # ext_package must agree with the value in setup.py
- ext_package="tls",
- extra_compile_args=[
- '-Wno-deprecated-declarations',
- ],
- libraries=['ssl']
- )
-
- def _override(self):
- """
- Create any Python-level overrides of the cffi-based wrappers.
- """
- self._overrides = {}
- for func in self.OVERRIDES:
- name = func.__name__
- from_openssl = getattr(self.openssl, name)
- override = func(self.openssl, from_openssl)
- self._overrides[name] = override
-
- def _populate(self):
- """
- Bind some aliases for FFI APIs on self.
- """
- self.NULL = self.ffi.NULL
- self.buffer = self.ffi.buffer
- self.callback = self.ffi.callback
- self.cast = self.ffi.cast
- self.new = self.ffi.new
- self.gc = self.ffi.gc
- self.string = self.ffi.string
-
- def __getattr__(self, name):
- """
- Try to resolve any attribute that does not exist on self as an
- attribute of the OpenSSL FFI object (in other words, as an OpenSSL
- API).
- """
- return self._overrides.get(name, getattr(self.openssl, name))
-
- def _initialise(self):
- "initialise openssl, schedule cleanup at exit"
- for function in self.SETUP:
- getattr(self, function)()
- for function in self.TEARDOWN:
- atexit.register(getattr(self, function))
-
- def version_info(self):
- "Return SSL version information"
- version = self.SSLeay()
- major = version >> (7 * 4) & 0xFF
- minor = version >> (5 * 4) & 0xFF
- fix = version >> (3 * 4) & 0xFF
- patch = version >> (1 * 4) & 0xFF
- patch = '' if not patch else chr(96 + patch)
- status = version & 0x0F
- if status == 0x0F:
- status = 'release'
- elif status == 0x00:
- status = 'dev'
- else:
- status = 'beta{}'.format(status)
- return self.SSLVersion(major, minor, fix, patch, status)
-
- def version(self, detail=None):
- "Return SSL version string"
- detail = self.SSLEAY_VERSION if detail is None else detail
- buff = self.SSLeay_version(detail)
- return self.string(buff)
-
api = API()
diff --git a/tests/__init__.py b/tests/__init__.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/tests/__init__.py
diff --git a/tests/test_c.py b/tests/test_c.py
new file mode 100644
index 00000000..c50968e2
--- /dev/null
+++ b/tests/test_c.py
@@ -0,0 +1,6 @@
+from cryptography.c import api
+
+
+class TestC(object):
+ def test_api_exists(self):
+ assert api