Port 3 more unit tests to Python 3

* barbican_manage.py: only decode bytes, not Unicode.
  six.string_types is str (Unicode) on Python 3.
* Replace long(1) with 1
* test_transport_keys_resource: decode HTTP body from UTF-8 to get
  Unicode.
* Replace ord(bytes[-1]) with ord(bytes[-1:]). On Python 3,
  bytes[int] returns an integer: use bytes[int:int] to get a
  substring.
* Remove following tests from tests-py3-blacklist.txt:

  - crypto.test_pkcs11
  - test_barbican_manage
  - test_transport_keys_resource

Partially implements: blueprint barbican-py3
Change-Id: I9189ac4106d05001ee0aee1299d100dd0d56bce0
This commit is contained in:
Victor Stinner 2016-06-14 11:51:47 +02:00
parent c6342039cc
commit 7a9c13f2e8
5 changed files with 13 additions and 15 deletions

View File

@ -22,7 +22,6 @@
from __future__ import print_function
import argparse
import six
import sys
from oslo_config import cfg
@ -315,7 +314,7 @@ def main():
v = getattr(CONF.category, 'action_kwarg_' + k)
if v is None:
continue
if isinstance(v, six.string_types):
if isinstance(v, bytes):
v = v.decode('utf-8')
fn_kwargs[k] = v

View File

@ -425,11 +425,12 @@ class PKCS11(object):
# between 1 and blocksize, and that there are that many consecutive
# bytes of that value at the end. If all of that is true, we remove
# the found padding.
last_byte = ord(pt[-1:])
if len(iv) == self.blocksize and \
(len(pt) % self.blocksize) == 0 and \
1 <= ord(pt[-1]) <= self.blocksize and \
pt.endswith(pt[-1] * ord(pt[-1])):
pt = pt[:-(ord(pt[-1]))]
1 <= last_byte <= self.blocksize and \
pt.endswith(pt[-1:] * last_byte):
pt = pt[:-last_byte]
return pt

View File

@ -156,17 +156,18 @@ class WhenGettingTransKeysListUsingTransportKeysResource(FunctionalTest):
self.assertIn('previous', resp.namespace)
self.assertIn('next', resp.namespace)
body = resp.body.decode('utf-8')
url_nav_next = self._create_url(self.external_project_id,
self.offset + self.limit, self.limit)
self.assertEqual(1, resp.body.count(url_nav_next))
self.assertEqual(1, body.count(url_nav_next))
url_nav_prev = self._create_url(self.external_project_id,
0, self.limit)
self.assertEqual(1, resp.body.count(url_nav_prev))
self.assertEqual(1, body.count(url_nav_prev))
url_hrefs = self._create_url(self.external_project_id)
self.assertEqual((self.num_keys + 2), resp.body.count(url_hrefs))
self.assertEqual((self.num_keys + 2), body.count(url_hrefs))
def test_response_should_include_total(self):
resp = self.app.get('/transport_keys/',

View File

@ -133,9 +133,9 @@ class TestBarbicanManage(TestBarbicanManageBase):
@mock.patch('barbican.plugin.crypto.pkcs11.PKCS11')
def test_hsm_gen_mkek(self, mock_pkcs11):
mock_pkcs11.return_value.get_session.return_value = long(1)
mock_pkcs11.return_value.get_session.return_value = 1
mock_pkcs11.return_value.get_key_handle.return_value = None
mock_pkcs11.return_value.generate_key.return_value = long(0)
mock_pkcs11.return_value.generate_key.return_value = 0
mock_genkey = mock_pkcs11.return_value.generate_key
self._main_test_helper(
['barbican.cmd.barbican_manage', 'hsm', 'gen_mkek',
@ -145,9 +145,9 @@ class TestBarbicanManage(TestBarbicanManageBase):
@mock.patch('barbican.plugin.crypto.pkcs11.PKCS11')
def test_hsm_gen_hmac(self, mock_pkcs11):
mock_pkcs11.return_value.get_session.return_value = long(1)
mock_pkcs11.return_value.get_session.return_value = 1
mock_pkcs11.return_value.get_key_handle.return_value = None
mock_pkcs11.return_value.generate_key.return_value = long(0)
mock_pkcs11.return_value.generate_key.return_value = 0
mock_genkey = mock_pkcs11.return_value.generate_key
self._main_test_helper(
['barbican.cmd.barbican_manage', 'hsm', 'gen_hmac',

View File

@ -2,7 +2,4 @@ barbican.tests.api.controllers.test_containers
barbican.tests.api.controllers.test_orders
barbican.tests.api.controllers.test_quotas
barbican.tests.api.controllers.test_secrets
barbican.tests.api.test_transport_keys_resource
barbican.tests.cmd.test_barbican_manage
barbican.tests.cmd.test_db_cleanup
barbican.tests.plugin.crypto.test_pkcs11