From 929c3d1265dcb7af375d433a7e0b44193e19ed2f Mon Sep 17 00:00:00 2001 From: Petr Viktorin Date: Fri, 18 Sep 2015 17:20:08 +0200 Subject: x509: Port to Python 3 In python 3 , `bytes` has the buffer interface, and `buffer` was removed. Also, invalid padding in base64-encoded data raises a ValueError rather than TypeError. In tests, use pytest.assert_raises for more correct exception assertions. Also, get rid of unused imports in the tests Reviewed-By: Tomas Babej --- ipalib/x509.py | 9 ++++++++- ipatests/test_ipalib/test_x509.py | 21 ++++++--------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/ipalib/x509.py b/ipalib/x509.py index e48d3edf7..037d6785c 100644 --- a/ipalib/x509.py +++ b/ipalib/x509.py @@ -37,10 +37,13 @@ import os import sys import base64 import re + import nss.nss as nss from nss.error import NSPRError from pyasn1.type import univ, namedtype, tag from pyasn1.codec.der import decoder, encoder +import six + from ipapython import ipautil from ipalib import api from ipalib import _ @@ -127,7 +130,11 @@ def load_certificate(data, datatype=PEM, dbdir=None): initialize_nss_database(dbdir=dbdir) - return nss.Certificate(buffer(data)) + if six.PY2: + return nss.Certificate(buffer(data)) + else: + # In python 3 , `bytes` has the buffer interface + return nss.Certificate(data) def load_certificate_from_file(filename, dbdir=None): """ diff --git a/ipatests/test_ipalib/test_x509.py b/ipatests/test_ipalib/test_x509.py index c7fafbbd9..d8004c4a0 100644 --- a/ipatests/test_ipalib/test_x509.py +++ b/ipatests/test_ipalib/test_x509.py @@ -21,17 +21,12 @@ Test the `ipalib.x509` module. """ -import os -from os import path -import sys -from ipatests.util import raises, setitem, delitem, ClassChecker -from ipatests.util import getitem, setitem, delitem -from ipatests.util import TempDir, TempHome -from ipalib.constants import TYPE_ERROR, OVERRIDE_ERROR, SET_ERROR, DEL_ERROR -from ipalib.constants import NAME_REGEX, NAME_ERROR import base64 -from ipalib import x509 + +import pytest from nss.error import NSPRError + +from ipalib import x509 from ipapython.dn import DN # certutil - @@ -66,16 +61,12 @@ class test_x509(object): # Load a good cert with bad headers newcert = '-----BEGIN CERTIFICATE-----' + goodcert - try: + with pytest.raises((TypeError, ValueError)): cert = x509.load_certificate(newcert) - except TypeError: - pass # Load a bad cert - try: + with pytest.raises(NSPRError): cert = x509.load_certificate(badcert) - except NSPRError: - pass def test_1_load_der_cert(self): """ -- cgit