Fix test_ipv6 and simplify to_global()

The tests test_to_global_with_bad_prefix and
test_to_global_with_bad_project are broken, they assert the TypeError
because they call to_global with an IPv6 address as MAC parameter and
do not check the functionality that they claim to check.

test_to_global_with_bad_project gets removed, because there is no
checking on the project_id, we simply hash whatever is there.

We also make sure that the correct error message is generated and add
a check to prevent regressing again.

While at it, simplify the code for to_global() in nova/ipv6/rfc2462.py.

Change-Id: I2a6f75beca1c37d135dcbb3a6eb0d739f2dd41e2
This commit is contained in:
Jens Rosenboom 2016-06-24 12:20:51 +02:00
parent 0871f4953d
commit 4647f418af
3 changed files with 28 additions and 35 deletions

View File

@ -38,15 +38,14 @@ def to_global(prefix, mac, project_id):
try:
mac_suffix = netaddr.EUI(mac).value & 0xffffff
mac_addr = netaddr.IPAddress(mac_suffix)
except netaddr.AddrFormatError:
raise TypeError(_('Bad mac for to_global_ipv6: %s') % mac)
try:
maskIP = netaddr.IPNetwork(prefix).ip
return (project_hash ^ static_num ^ mac_addr | maskIP).format()
except netaddr.AddrFormatError:
raise TypeError(_('Bad mac for to_global_ipv6: %s') % mac)
except TypeError:
raise TypeError(_('Bad prefix for to_global_ipv6: %s') % prefix)
except NameError:
raise TypeError(_('Bad project_id for to_global_ipv6: %s') %
project_id)
def to_mac(ipv6_address):

View File

@ -24,15 +24,15 @@ from nova.i18n import _
def to_global(prefix, mac, project_id):
try:
mac64 = netaddr.EUI(mac).eui64().words
int_addr = int(''.join(['%02x' % i for i in mac64]), 16)
mac64_addr = netaddr.IPAddress(int_addr)
maskIP = netaddr.IPNetwork(prefix).ip
return (mac64_addr ^ netaddr.IPAddress('::0200:0:0:0') |
maskIP).format()
mac64 = netaddr.EUI(mac).modified_eui64().value
mac64_addr = netaddr.IPAddress(mac64)
except netaddr.AddrFormatError:
raise TypeError(_('Bad mac for to_global_ipv6: %s') % mac)
except TypeError:
try:
maskIP = netaddr.IPNetwork(prefix).ip
return (mac64_addr | maskIP).format()
except netaddr.AddrFormatError:
raise TypeError(_('Bad prefix for to_global_ipv6: %s') % prefix)

View File

@ -35,22 +35,19 @@ class IPv6RFC2462TestCase(test.NoDBTestCase):
def test_to_global_with_bad_mac(self):
bad_mac = '02:16:3e:33:44:5Z'
self.assertRaises(TypeError, ipv6.to_global,
expected_msg = 'Bad mac for to_global_ipv6: %s' % bad_mac
err = self.assertRaises(TypeError, ipv6.to_global,
'2001:db8::', bad_mac, 'test')
self.assertEqual(expected_msg, str(err))
def test_to_global_with_bad_prefix(self):
bad_prefix = '82'
self.assertRaises(TypeError, ipv6.to_global,
bad_prefix = '2001::1::2'
expected_msg = 'Bad prefix for to_global_ipv6: %s' % bad_prefix
err = self.assertRaises(TypeError, ipv6.to_global,
bad_prefix,
'2001:db8::216:3eff:fe33:4455',
'02:16:3e:33:44:55',
'test')
def test_to_global_with_bad_project(self):
bad_project = 'non-existent-project-name'
self.assertRaises(TypeError, ipv6.to_global,
'2001:db8::',
'2001:db8::a94a:8fe5:ff33:4455',
bad_project)
self.assertEqual(expected_msg, str(err))
class IPv6AccountIdentiferTestCase(test.NoDBTestCase):
@ -69,20 +66,17 @@ class IPv6AccountIdentiferTestCase(test.NoDBTestCase):
self.assertEqual(mac, '02:16:3e:33:44:55')
def test_to_global_with_bad_mac(self):
bad_mac = '02:16:3e:33:44:5X'
self.assertRaises(TypeError, ipv6.to_global,
bad_mac = '02:16:3e:33:44:5Z'
expected_msg = 'Bad mac for to_global_ipv6: %s' % bad_mac
err = self.assertRaises(TypeError, ipv6.to_global,
'2001:db8::', bad_mac, 'test')
self.assertEqual(expected_msg, str(err))
def test_to_global_with_bad_prefix(self):
bad_prefix = '78'
self.assertRaises(TypeError, ipv6.to_global,
bad_prefix = '2001::1::2'
expected_msg = 'Bad prefix for to_global_ipv6: %s' % bad_prefix
err = self.assertRaises(TypeError, ipv6.to_global,
bad_prefix,
'2001:db8::a94a:8fe5:ff33:4455',
'02:16:3e:33:44:55',
'test')
def test_to_global_with_bad_project(self):
bad_project = 'non-existent-project-name'
self.assertRaises(TypeError, ipv6.to_global,
'2001:db8::',
'2001:db8::a94a:8fe5:ff33:4455',
bad_project)
self.assertEqual(expected_msg, str(err))