Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate MAC EUI versions #127

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions netfields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def to_python(self, value):
return value

try:
return EUI(value, dialect=mac_unix_common)
return EUI(value, version=48, dialect=mac_unix_common)
except (AddrFormatError, IndexError, TypeError) as e:
raise ValidationError(e)

Expand Down Expand Up @@ -240,7 +240,12 @@ def to_python(self, value):
return value

try:
return EUI(value, dialect=mac_eui64)
mac = EUI(value, dialect=mac_eui64)
if mac.version == 64:
return mac
mac = mac.eui64()
mac.dialect = mac_eui64
return mac
except (AddrFormatError, IndexError, TypeError) as e:
raise ValidationError(e)

Expand Down
9 changes: 7 additions & 2 deletions netfields/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def to_python(self, value):
value = value.strip()

try:
return EUI(value, dialect=mac_unix_common)
return EUI(value, version=48, dialect=mac_unix_common)
except (AddrFormatError, IndexError, TypeError):
raise ValidationError(self.error_messages['invalid'])

Expand Down Expand Up @@ -136,7 +136,12 @@ def to_python(self, value):
value = value.strip()

try:
return EUI(value, dialect=mac_eui64)
mac = EUI(value, dialect=mac_eui64)
if mac.version == 64:
return mac
mac = mac.eui64()
mac.dialect = mac_eui64
return mac
except (AddrFormatError, IndexError, TypeError):
raise ValidationError(self.error_messages['invalid'])

Expand Down
9 changes: 7 additions & 2 deletions netfields/rest_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def to_internal_value(self, data):
if data is None:
return data
try:
return EUI(data, dialect=mac_unix_common)
return EUI(data, version=48, dialect=mac_unix_common)
except (AddrFormatError, IndexError, TypeError):
self.fail('invalid')

Expand All @@ -92,7 +92,12 @@ def to_internal_value(self, data):
if data is None:
return data
try:
return EUI(data, dialect=mac_eui64)
mac = EUI(data, dialect=mac_eui64)
if mac.version == 64:
return mac
mac = mac.eui64()
mac.dialect = mac_eui64
return mac
except (AddrFormatError, IndexError, TypeError):
self.fail('invalid')

Expand Down
9 changes: 9 additions & 0 deletions test/tests/test_form_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ def test_strip(self):
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data['field'], self.mac)

def test_eui64(self):
form = MacAddressTestModelForm({'field': '00-AA-2B-C3-DD-44-EE-55'})
self.assertFalse(form.is_valid())

def test_invalid(self):
form = MacAddressTestModelForm({'field': 'notvalid'})
self.assertFalse(form.is_valid())
Expand Down Expand Up @@ -275,6 +279,11 @@ def test_strip(self):
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data['field'], self.mac)

def test_eui48(self):
form = MacAddress8TestModelForm({'field': '00:aa:2b:c3:dd:44'})
self.assertTrue(form.is_valid())
self.assertEqual(form.cleaned_data['field'], EUI('00:aa:2b:ff:fe:c3:dd:44', dialect=mac_eui64))

def test_invalid(self):
form = MacAddress8TestModelForm({'field': 'notvalid'})
self.assertFalse(form.is_valid())
6 changes: 3 additions & 3 deletions test/tests/test_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ def test_read_me_example(self):

class TestMacFieldFunctions(TestCase):
def setUp(self):
MACTestModel.objects.create(field='aa:bb:cc:dd:ee:ff')
MACTestModel.objects.create(field='88:bb:cc:dd:ee:ff')

def test_trunc(self):
qs = MACTestModel.objects.annotate(trunc=Trunc(F('field')))
self.assertEqual(qs[0].trunc, EUI('aa:bb:cc:00:00:00'))
self.assertEqual(qs[0].trunc, EUI('88:bb:cc:00:00:00'))

def test_macaddr8_to7bit(self):
qs = MACTestModel.objects.annotate(eui64=Macaddr8Set7bit(F('field')))
self.assertEqual(qs[0].eui64, EUI('aa:bb:cc:ff:fe:dd:ee:ff'))
self.assertEqual(qs[0].eui64, EUI('8a:bb:cc:ff:fe:dd:ee:ff'))


class TestMac8FieldFunctions(TestCase):
Expand Down