Merge "Fixes hex decoding related unit tests"

This commit is contained in:
Jenkins 2016-04-11 00:35:02 +00:00 committed by Gerrit Code Review
commit d38410ea6f
9 changed files with 26 additions and 17 deletions

View File

@ -27,6 +27,7 @@ this class.
"""
import array
import codecs
from oslo_log import log as logging
from oslo_utils import uuidutils
@ -39,6 +40,7 @@ from nova import utils
LOG = logging.getLogger(__name__)
decode_hex = codecs.getdecoder("hex_codec")
class MockKeyManager(key_mgr.KeyManager):
@ -60,14 +62,14 @@ class MockKeyManager(key_mgr.KeyManager):
def _generate_hex_key(self, **kwargs):
key_length = kwargs.get('key_length', 256)
# hex digit => 4 bits
hex_encoded = utils.generate_password(length=key_length / 4,
hex_encoded = utils.generate_password(length=key_length // 4,
symbolgroups='0123456789ABCDEF')
return hex_encoded
def _generate_key(self, **kwargs):
_hex = self._generate_hex_key(**kwargs)
return key.SymmetricKey('AES',
array.array('B', _hex.decode('hex')).tolist())
array.array('B', decode_hex(_hex)[0]).tolist())
def create_key(self, ctxt, **kwargs):
"""Creates a key.

View File

@ -49,7 +49,7 @@ class SingleKeyManager(mock_key_mgr.MockKeyManager):
def _generate_hex_key(self, **kwargs):
key_length = kwargs.get('key_length', 256)
return '0' * (key_length / 4) # hex digit => 4 bits
return b'0' * (key_length // 4) # hex digit => 4 bits
def _generate_key_id(self):
return self.key_id

View File

@ -195,7 +195,7 @@ class BarbicanKeyManagerTestCase(test_key_mgr.KeyManagerTestCase):
returned_uuid = self.key_mgr.store_key(self.ctxt, _key, bit_length=32)
self.create.assert_called_once_with('Nova Compute Key',
'AQKgsw==',
b'AQKgsw==',
'application/octet-stream',
'base64',
'AES', 32, 'CBC',

View File

@ -18,6 +18,7 @@ Test cases for the conf key manager.
"""
import array
import codecs
from oslo_config import cfg
@ -28,6 +29,7 @@ from nova.tests.unit.keymgr import test_single_key_mgr
CONF = cfg.CONF
CONF.import_opt('fixed_key', 'nova.keymgr.conf_key_mgr', group='keymgr')
decode_hex = codecs.getdecoder("hex_codec")
class ConfKeyManagerTestCase(test_single_key_mgr.SingleKeyManagerTestCase):
@ -43,7 +45,7 @@ class ConfKeyManagerTestCase(test_single_key_mgr.SingleKeyManagerTestCase):
def setUp(self):
super(ConfKeyManagerTestCase, self).setUp()
encoded_key = array.array('B', self._hex_key.decode('hex')).tolist()
encoded_key = array.array('B', decode_hex(self._hex_key)[0]).tolist()
self.key = key.SymmetricKey('AES', encoded_key)
def test_init(self):

View File

@ -18,10 +18,13 @@ Test cases for the key classes.
"""
import array
import codecs
from nova.keymgr import key
from nova import test
decode_hex = codecs.getdecoder("hex_codec")
class KeyTestCase(test.NoDBTestCase):
@ -41,7 +44,7 @@ class SymmetricKeyTestCase(KeyTestCase):
def setUp(self):
self.algorithm = 'AES'
self.encoded = array.array('B', ('0' * 64).decode('hex')).tolist()
self.encoded = array.array('B', decode_hex('0' * 64)[0]).tolist()
super(SymmetricKeyTestCase, self).setUp()

View File

@ -18,6 +18,7 @@ Test cases for the mock key manager.
"""
import array
import codecs
from nova import context
from nova import exception
@ -25,6 +26,8 @@ from nova.keymgr import key as keymgr_key
from nova.keymgr import mock_key_mgr
from nova.tests.unit.keymgr import test_key_mgr
decode_hex = codecs.getdecoder("hex_codec")
class MockKeyManagerTestCase(test_key_mgr.KeyManagerTestCase):
@ -46,14 +49,14 @@ class MockKeyManagerTestCase(test_key_mgr.KeyManagerTestCase):
for length in [64, 128, 256]:
key_id = self.key_mgr.create_key(self.ctxt, key_length=length)
key = self.key_mgr.get_key(self.ctxt, key_id)
self.assertEqual(length / 8, len(key.get_encoded()))
self.assertEqual(length // 8, len(key.get_encoded()))
def test_create_null_context(self):
self.assertRaises(exception.Forbidden,
self.key_mgr.create_key, None)
def test_store_key(self):
secret_key = array.array('B', ('0' * 64).decode('hex')).tolist()
secret_key = array.array('B', decode_hex('0' * 64)[0]).tolist()
_key = keymgr_key.SymmetricKey('AES', secret_key)
key_id = self.key_mgr.store_key(self.ctxt, _key)

View File

@ -18,12 +18,15 @@ Test cases for the single key manager.
"""
import array
import codecs
from nova import exception
from nova.keymgr import key
from nova.keymgr import single_key_mgr
from nova.tests.unit.keymgr import test_mock_key_mgr
decode_hex = codecs.getdecoder("hex_codec")
class SingleKeyManagerTestCase(test_mock_key_mgr.MockKeyManagerTestCase):
@ -34,7 +37,7 @@ class SingleKeyManagerTestCase(test_mock_key_mgr.MockKeyManagerTestCase):
super(SingleKeyManagerTestCase, self).setUp()
self.key_id = '00000000-0000-0000-0000-000000000000'
encoded = array.array('B', ('0' * 64).decode('hex')).tolist()
encoded = array.array('B', decode_hex('0' * 64)[0]).tolist()
self.key = key.SymmetricKey('AES', encoded)
def test___init__(self):

View File

@ -15,6 +15,7 @@
import array
import codecs
import mock
import six
@ -24,9 +25,11 @@ from nova.keymgr import key
from nova.tests.unit.volume.encryptors import test_base
from nova.volume.encryptors import cryptsetup
decode_hex = codecs.getdecoder("hex_codec")
def fake__get_key(context):
raw = array.array('B', ('0' * 64).decode('hex')).tolist()
raw = array.array('B', decode_hex('0' * 64)[0]).tolist()
symmetric_key = key.SymmetricKey('AES', raw)
return symmetric_key

View File

@ -83,11 +83,6 @@ nova.tests.unit.db.test_migrations.TestNovaMigrationsMySQL
nova.tests.unit.db.test_migrations.TestNovaMigrationsPostgreSQL
nova.tests.unit.db.test_migrations.TestNovaMigrationsSQLite
nova.tests.unit.image.test_fake.FakeImageServiceTestCase
nova.tests.unit.keymgr.test_barbican.BarbicanKeyManagerTestCase
nova.tests.unit.keymgr.test_conf_key_mgr.ConfKeyManagerTestCase
nova.tests.unit.keymgr.test_key.SymmetricKeyTestCase
nova.tests.unit.keymgr.test_mock_key_mgr.MockKeyManagerTestCase
nova.tests.unit.keymgr.test_single_key_mgr.SingleKeyManagerTestCase
nova.tests.unit.network.test_manager.LdapDNSTestCase
nova.tests.unit.pci.test_manager.PciDevTrackerTestCase
nova.tests.unit.pci.test_stats.PciDeviceStatsTestCase
@ -189,8 +184,6 @@ nova.tests.unit.virt.xenapi.test_xenapi.HypervisorPoolTestCase
nova.tests.unit.virt.xenapi.test_xenapi.XenAPIDiffieHellmanTestCase
nova.tests.unit.virt.xenapi.test_xenapi.XenAPIDom0IptablesFirewallTestCase
nova.tests.unit.virt.xenapi.test_xenapi.XenAPIVMTestCase
nova.tests.unit.volume.encryptors.test_cryptsetup.CryptsetupEncryptorTestCase
nova.tests.unit.volume.encryptors.test_luks.LuksEncryptorTestCase
nova.tests.unit.volume.test_cinder.CinderApiTestCase
##########################################################################