Merge "Fix py3 compatibility issue in PKCS#11 plugin"

This commit is contained in:
Zuul 2019-08-12 14:46:43 +00:00 committed by Gerrit Code Review
commit ad7b3e6a21
2 changed files with 15 additions and 1 deletions

View File

@ -16,6 +16,7 @@ import textwrap
import cffi
from cryptography.hazmat.primitives import padding
import six
from barbican.common import exception
from barbican.common import utils
@ -386,7 +387,7 @@ class PKCS11(object):
self._check_error(rv)
# Session options
self.login_passphrase = login_passphrase
self.login_passphrase = _to_bytes(login_passphrase)
self.rw_session = rw_session
self.slot_id = slot_id
@ -795,3 +796,10 @@ class PKCS11(object):
if test_random == b'\x00' * 100:
raise exception.P11CryptoPluginException(
u._("Apparent RNG self-test failure."))
def _to_bytes(string):
if isinstance(string, six.binary_type):
return string
else:
return string.encode('UTF-8')

View File

@ -399,3 +399,9 @@ class WhenTestingPKCS11(utils.BaseTestCase):
def test_check_error_with_token_error(self):
self.assertRaises(exception.P11CryptoTokenException,
self.pkcs11._check_error, 0xe0)
def test_converting_unicode_to_bytes(self):
self.assertEqual(b'foo', pkcs11._to_bytes(u'foo'))
def test_converting_default_str_type_to_bytes(self):
self.assertEqual(b'foo', pkcs11._to_bytes('foo'))