<commit_before>from clarent.certificate import makeCertificate, generateKey
from inspect import getargspec
from OpenSSL.crypto import PKey, TYPE_RSA, load_privatekey, FILETYPE_PEM
from twisted.trial.unittest import SynchronousTestCase


class FakePKey(object):
    """A fake key.

    """
    def generate_key(self, keyType, keyLength):
        self.keyType, self.keyLength = keyType, keyLength



class KeyGenerationTests(SynchronousTestCase):
    """Tests for the key generation routine.

    """
    def test_pkeyDefault(self):
        """The key generation routine uses PKey by default.

        """
        argspec = getargspec(generateKey)
        default = argspec.defaults[argspec.args.index("_PKey")]
        self.assertIdentical(default, PKey)


    def test_generateKey(self):
        """The key generation routine generates 4096-bit RSA keys.

        """
        key = generateKey(_PKey=FakePKey)
        self.assertIdentical(key.keyType, TYPE_RSA)
        self.assertEqual(key.keyLength, 4096)



# This ugly private key is in here so that we can avoid generating one
# every test run.
pemTestKey = """
-----BEGIN RSA PRIVATE KEY-----
<KEY>
-----END RSA PRIVATE KEY-----
"""
testKey = load_privatekey(FILETYPE_PEM, pemTestKey)



class CertificateTests(SynchronousTestCase):
    """Tests for generating certificates.

    """
    def test_makeCertificate(self):
        """A produced certificate has the appropriate common name and e-mail
        address, and is self-signed with the given key and SHA-512.

        """
        cert = makeCertificate(testKey, u"<EMAIL>")

        subj = cert.get_subject()
        self.assertEqual(subj.CN, u"Crypto 101 Client")
        self.assertEqual(subj.emailAddress, u"<EMAIL>")

        self.assertEqual(cert.get_issuer(), cert.get_subject())
        self.assertEqual(cert.get_signature_algorithm(),
                         "sha512WithRSAEncryption")
<commit_msg>Add a failing certificate roundtrip test<commit_after>from clarent.certificate import makeCertificate, generateKey
from inspect import getargspec
from OpenSSL.crypto import PKey, TYPE_RSA, load_privatekey
from OpenSSL.crypto import dump_certificate, load_certificate, FILETYPE_PEM
from twisted.trial.unittest import SynchronousTestCase


class FakePKey(object):
    """A fake key.

    """
    def generate_key(self, keyType, keyLength):
        self.keyType, self.keyLength = keyType, keyLength



class KeyGenerationTests(SynchronousTestCase):
    """Tests for the key generation routine.

    """
    def test_pkeyDefault(self):
        """The key generation routine uses PKey by default.

        """
        argspec = getargspec(generateKey)
        default = argspec.defaults[argspec.args.index("_PKey")]
        self.assertIdentical(default, PKey)


    def test_generateKey(self):
        """The key generation routine generates 4096-bit RSA keys.

        """
        key = generateKey(_PKey=FakePKey)
        self.assertIdentical(key.keyType, TYPE_RSA)
        self.assertEqual(key.keyLength, 4096)



# This ugly private key is in here so that we can avoid generating one
# every test run.
pemTestKey = """
-----<KEY>
"""
testKey = load_privatekey(FILETYPE_PEM, pemTestKey)



class CertificateTests(SynchronousTestCase):
    """Tests for generating certificates.

    """
    def test_makeCertificate(self):
        """A produced certificate has the appropriate common name and e-mail
        address, and is self-signed with the given key and SHA-512.

        """
        cert = makeCertificate(testKey, u"<EMAIL>")

        subj = cert.get_subject()
        self.assertEqual(subj.CN, u"Crypto 101 Client")
        self.assertEqual(subj.emailAddress, u"<EMAIL>")

        self.assertEqual(cert.get_issuer(), cert.get_subject())
        self.assertEqual(cert.get_signature_algorithm(),
                         "sha512WithRSAEncryption")


    def test_certificateRoundtrip(self):
        """A certificate can be dumped to a string and read again.

        """
        cert = makeCertificate(testKey, u"<EMAIL>")
        data = dump_certificate(FILETYPE_PEM, cert)
        load_certificate(FILETYPE_PEM, data)

{'fraction_non_alphanumeric': 0.09946885562530179, 'fraction_numerical': 0.008208594881699663, 'mean_word_length': 3.1806256306760847, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2208610', 'n_tokens_mistral': 1239, 'n_tokens_neox': 1156, 'n_words': 315}