Adding more tests against the X509 CSR class

These tests needed to use Mock, so I have added it to the
test-requirements.txt list

This results in anchor/X509/signing_request.py 100% coverage

Change-Id: I49652ba2bf93959a5c2cda39e13d0d414e40c732
This commit is contained in:
Tim Kelsey 2015-02-11 13:25:40 +00:00
parent d9c503f036
commit f186f9a1af
3 changed files with 33 additions and 5 deletions

View File

@ -30,7 +30,8 @@ class X509Csr(object):
self._ffi = backend._ffi
csrObj = self._lib.X509_REQ_new()
if csrObj == self._ffi.NULL:
raise X509CsrError("Could not create X509 CSR Object.")
raise X509CsrError(
"Could not create X509 CSR Object.") # pragma: no cover
self._csrObj = csrObj
@ -62,7 +63,7 @@ class X509Csr(object):
data = None
with open(path, 'rb') as f:
data = f.read()
self.fromBuffer(data, password)
self.from_buffer(data, password)
def get_pubkey(self):
"""Get the public key from the CSR
@ -71,7 +72,8 @@ class X509Csr(object):
"""
pkey = self._lib.X509_REQ_get_pubkey(self._csrObj)
if pkey == self._ffi.NULL:
raise X509CsrError("Could not get pubkey from X509 CSR Object.")
raise X509CsrError(
"Could not get pubkey from X509 CSR.") # pragma: no cover
return pkey
@ -82,7 +84,8 @@ class X509Csr(object):
"""
subs = self._lib.X509_REQ_get_subject_name(self._csrObj)
if subs == self._ffi.NULL:
raise X509CsrError("Could not get subject from X509 CSR Object.")
raise X509CsrError(
"Could not get subject from X509 CSR.") # pragma: no cover
return name.X509Name(subs)

View File

@ -2,8 +2,8 @@ coverage>=3.6
discover
fixtures>=0.3.14
hacking>=0.9.2,<0.10
mock>=1.0
python-subunit>=0.0.18
testrepository>=0.0.18
testscenarios>=0.4
testtools>=0.9.34

View File

@ -17,10 +17,13 @@
import os
import unittest
import mock
from anchor.X509 import errors as x509_errors
from anchor.X509 import signing_request
from cryptography.hazmat.backends.openssl import backend
class TestX509Csr(unittest.TestCase):
csr_data = (
@ -44,6 +47,28 @@ class TestX509Csr(unittest.TestCase):
def tearDown(self):
pass
def test_get_pubkey_bits(self):
# some OpenSSL gumph to test a reasonable attribute of the pubkey
pubkey = self.csr.get_pubkey()
size = backend._lib.EVP_PKEY_bits(pubkey)
self.assertEqual(size, 384)
def test_get_extensions(self):
exts = self.csr.get_extensions()
self.assertEqual(len(exts), 2)
self.assertEqual(str(exts[0]), "basicConstraints CA:FALSE")
self.assertEqual(str(exts[1]), ("keyUsage Digital Signature, Non "
"Repudiation, Key Encipherment"))
def test_read_from_file(self):
open_name = 'anchor.X509.signing_request.open'
with mock.patch(open_name, create=True) as mock_open:
mock_open.return_value = mock.MagicMock(spec=file)
m_file = mock_open.return_value.__enter__.return_value
m_file.read.return_value = TestX509Csr.csr_data
csr = signing_request.X509Csr()
csr.from_file("some_path")
def test_bad_data_throws(self):
bad_data = (
"some bad data is "