From 7a9c13f2e842ffdf49df4146ee1848266b5db6da Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Tue, 14 Jun 2016 11:51:47 +0200 Subject: [PATCH] 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 --- barbican/cmd/barbican_manage.py | 3 +-- barbican/plugin/crypto/pkcs11.py | 7 ++++--- barbican/tests/api/test_transport_keys_resource.py | 7 ++++--- barbican/tests/cmd/test_barbican_manage.py | 8 ++++---- tests-py3-blacklist.txt | 3 --- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/barbican/cmd/barbican_manage.py b/barbican/cmd/barbican_manage.py index 627044e11..6334f7475 100755 --- a/barbican/cmd/barbican_manage.py +++ b/barbican/cmd/barbican_manage.py @@ -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 diff --git a/barbican/plugin/crypto/pkcs11.py b/barbican/plugin/crypto/pkcs11.py index 2a6233ce0..af16d64c5 100644 --- a/barbican/plugin/crypto/pkcs11.py +++ b/barbican/plugin/crypto/pkcs11.py @@ -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 diff --git a/barbican/tests/api/test_transport_keys_resource.py b/barbican/tests/api/test_transport_keys_resource.py index 2cab78bda..0d370e9eb 100644 --- a/barbican/tests/api/test_transport_keys_resource.py +++ b/barbican/tests/api/test_transport_keys_resource.py @@ -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/', diff --git a/barbican/tests/cmd/test_barbican_manage.py b/barbican/tests/cmd/test_barbican_manage.py index 76aaf9113..82b09a088 100644 --- a/barbican/tests/cmd/test_barbican_manage.py +++ b/barbican/tests/cmd/test_barbican_manage.py @@ -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', diff --git a/tests-py3-blacklist.txt b/tests-py3-blacklist.txt index 7613390c2..64f925e2b 100644 --- a/tests-py3-blacklist.txt +++ b/tests-py3-blacklist.txt @@ -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