Fix py3 compatibility issue in PKCS#11 plugin

This patch fixes a str to char * type cast that is only valid
in Python 2.

Change-Id: Ia3bcfe125c4e2c49e8a3e2a4cdb2d67e641ceadd
This commit is contained in:
Douglas Mendizábal 2019-08-09 12:28:42 -05:00
parent 7aab8ba837
commit 5018d8502d
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'))