forked from theteladras/py.validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added is_mac_address validator
- Loading branch information
1 parent
3794e0c
commit 30c1ad1
Showing
5 changed files
with
269 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import TypedDict, Union, Literal | ||
|
||
from .utils.assert_string import assert_string | ||
from .utils.merge import merge | ||
|
||
class IsMacAddressOptions(TypedDict): | ||
no_separators: bool | ||
eui: Union[ | ||
Literal[48], | ||
Literal[64], | ||
Literal[None], | ||
] | ||
|
||
default_mac_address_options: IsMacAddressOptions = { | ||
"no_separators": False, | ||
"eui": None | ||
} | ||
|
||
def is_mac_address(input: str, options = {}) -> bool: | ||
input = assert_string(input) | ||
|
||
options = merge(options, default_mac_address_options) | ||
|
||
mac_address48 = r"^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){4}([0-9a-fA-F]{2})$" | ||
mac_address48_no_separators = r"^([0-9a-fA-F]){12}$" | ||
mac_address48_with_dots = r"^([0-9a-fA-F]{4}\.){2}([0-9a-fA-F]{4})$" | ||
mac_address64 = r"^(?:[0-9a-fA-F]{2}([-:\s]))([0-9a-fA-F]{2}\1){6}([0-9a-fA-F]{2})$" | ||
mac_address64_no_separators = r"^([0-9a-fA-F]){16}$" | ||
mac_address64_with_dots = r"^([0-9a-fA-F]{4}\.){3}([0-9a-fA-F]{4})$" | ||
|
||
if options["no_separators"]: | ||
if options["eui"] == 48: | ||
return input.match(mac_address48_no_separators) | ||
if options["eui"] == 64: | ||
return input.match(mac_address64_no_separators) | ||
return input.match(mac_address48_no_separators) or input.match(mac_address64_no_separators) | ||
|
||
if options["eui"] == 48: | ||
return input.match(mac_address48) or input.match(mac_address48_with_dots) | ||
if options["eui"] == 64: | ||
return input.match(mac_address64) or input.match(mac_address64_with_dots) | ||
|
||
return ( | ||
input.match(mac_address48) or | ||
input.match(mac_address48_with_dots) or | ||
input.match(mac_address64) or | ||
input.match(mac_address64_with_dots) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
import unittest | ||
from pyvalidator import * | ||
|
||
class TestIsMacAddress(unittest.TestCase): | ||
def valid_check(self, items, options = {}): | ||
for item in items: | ||
try: | ||
self.assertTrue(is_mac_address(item, options)) | ||
except Exception as e: | ||
print(f'failed for input: {item}') | ||
raise e | ||
|
||
def invalid_check(self, items, options = {}): | ||
for item in items: | ||
try: | ||
self.assertFalse(is_mac_address(item, options)) | ||
except Exception as e: | ||
print(f'failed for input: {item}') | ||
raise e | ||
|
||
def test_valid_mac_address(self): | ||
valid = [ | ||
'ab:ab:ab:ab:ab:ab', | ||
'FF:FF:FF:FF:FF:FF', | ||
'01:02:03:04:05:ab', | ||
'01:AB:03:04:05:06', | ||
'A9 C5 D4 9F EB D3', | ||
'01 02 03 04 05 ab', | ||
'01-02-03-04-05-ab', | ||
'0102.0304.05ab', | ||
'ab:ab:ab:ab:ab:ab:ab:ab', | ||
'FF:FF:FF:FF:FF:FF:FF:FF', | ||
'01:02:03:04:05:06:07:ab', | ||
'01:AB:03:04:05:06:07:08', | ||
'A9 C5 D4 9F EB D3 B6 65', | ||
'01 02 03 04 05 06 07 ab', | ||
'01-02-03-04-05-06-07-ab', | ||
'0102.0304.0506.07ab', | ||
] | ||
|
||
self.valid_check(valid) | ||
print('OK - test_valid_mac_address') | ||
|
||
def test_invalid_mac_address(self): | ||
invalid = [ | ||
' ', | ||
'0:0:0:0', | ||
' : ', | ||
'abc', | ||
'01:02:03:04:05', | ||
'01:02:03:04:05:z0', | ||
'01:02:03:04::ab', | ||
'1:2:3:4:5:6', | ||
'AB:CD:EF:GH:01:02', | ||
'A9C5 D4 9F EB D3', | ||
'01-02 03:04 05 ab', | ||
'0102.03:04.05ab', | ||
'900f/dffs/sdea', | ||
'01:02:03:04:05:06:07', | ||
'01:02:03:04:05:06:07:z0', | ||
'01:02:03:04:05:06::ab', | ||
'1:2:3:4:5:6:7:8', | ||
'AB:CD:EF:GH:01:02:03:04', | ||
'A9C5 D4 9F EB D3 B6 65', | ||
'01-02 03:04 05 06 07 ab', | ||
'0102.03:04.0506.07ab', | ||
'900f/dffs/sdea/54gh', | ||
] | ||
self.invalid_check(invalid) | ||
print('OK - test_invalid_mac_address') | ||
|
||
def test_valid_mac_address_eui48(self): | ||
valid = [ | ||
'ab:ab:ab:ab:ab:ab', | ||
'FF:FF:FF:FF:FF:FF', | ||
'01:02:03:04:05:ab', | ||
'01:AB:03:04:05:06', | ||
'A9 C5 D4 9F EB D3', | ||
'01 02 03 04 05 ab', | ||
'01-02-03-04-05-ab', | ||
'0102.0304.05ab', | ||
] | ||
|
||
self.valid_check(valid, { "eui": 48 }) | ||
print('OK - test_valid_mac_address_eui48') | ||
|
||
def test_invalid_mac_address_eui48(self): | ||
invalid = [ | ||
'ab:ab:ab:ab:ab:ab:ab:ab', | ||
'FF:FF:FF:FF:FF:FF:FF:FF', | ||
'01:02:03:04:05:06:07:ab', | ||
'01:AB:03:04:05:06:07:08', | ||
'A9 C5 D4 9F EB D3 B6 65', | ||
'01 02 03 04 05 06 07 ab', | ||
'01-02-03-04-05-06-07-ab', | ||
'0102.0304.0506.07ab', | ||
] | ||
self.invalid_check(invalid, { "eui": 48 }) | ||
print('OK - test_invalid_mac_address_eui48') | ||
|
||
def test_valid_mac_address_eui64(self): | ||
valid = [ | ||
'ab:ab:ab:ab:ab:ab:ab:ab', | ||
'FF:FF:FF:FF:FF:FF:FF:FF', | ||
'01:02:03:04:05:06:07:ab', | ||
'01:AB:03:04:05:06:07:08', | ||
'A9 C5 D4 9F EB D3 B6 65', | ||
'01 02 03 04 05 06 07 ab', | ||
'01-02-03-04-05-06-07-ab', | ||
'0102.0304.0506.07ab', | ||
] | ||
|
||
self.valid_check(valid, { "eui": 64 }) | ||
print('OK - test_valid_mac_address_eui64') | ||
|
||
def test_invalid_mac_address_eui64(self): | ||
invalid = [ | ||
'ab:ab:ab:ab:ab:ab', | ||
'FF:FF:FF:FF:FF:FF', | ||
'01:02:03:04:05:ab', | ||
'01:AB:03:04:05:06', | ||
'A9 C5 D4 9F EB D3', | ||
'01 02 03 04 05 ab', | ||
'01-02-03-04-05-ab', | ||
'0102.0304.05ab', | ||
] | ||
self.invalid_check(invalid, { "eui": 64 }) | ||
print('OK - test_invalid_mac_address_eui64') | ||
|
||
def test_valid_mac_address_no_separator(self): | ||
valid = [ | ||
'abababababab', | ||
'FFFFFFFFFFFF', | ||
'0102030405ab', | ||
'01AB03040506', | ||
'abababababababab', | ||
'FFFFFFFFFFFFFFFF', | ||
'01020304050607ab', | ||
'01AB030405060708', | ||
] | ||
|
||
self.valid_check(valid, { "no_separators": True }) | ||
print('OK - test_valid_mac_address_no_separator') | ||
|
||
def test_invalid_mac_address_no_separators(self): | ||
invalid = [ | ||
'abc', | ||
'01:02:03:04:05', | ||
'01:02:03:04::ab', | ||
'1:2:3:4:5:6', | ||
'AB:CD:EF:GH:01:02', | ||
'ab:ab:ab:ab:ab:ab', | ||
'FF:FF:FF:FF:FF:FF', | ||
'01:02:03:04:05:ab', | ||
'01:AB:03:04:05:06', | ||
'0102030405', | ||
'01020304ab', | ||
'123456', | ||
'ABCDEFGH0102', | ||
'01:02:03:04:05:06:07', | ||
'01:02:03:04:05:06::ab', | ||
'1:2:3:4:5:6:7:8', | ||
'AB:CD:EF:GH:01:02:03:04', | ||
'ab:ab:ab:ab:ab:ab:ab:ab', | ||
'FF:FF:FF:FF:FF:FF:FF:FF', | ||
'01:02:03:04:05:06:07:ab', | ||
'01:AB:03:04:05:06:07:08', | ||
'01020304050607', | ||
'010203040506ab', | ||
'12345678', | ||
'ABCDEFGH01020304', | ||
] | ||
self.invalid_check(invalid, { "no_separators": True }) | ||
print('OK - test_invalid_mac_address_no_separators') | ||
|
||
def test_valid_mac_address_no_separator_eui48(self): | ||
valid = [ | ||
'abababababab', | ||
'FFFFFFFFFFFF', | ||
'0102030405ab', | ||
'01AB03040506', | ||
] | ||
|
||
self.valid_check(valid, { "no_separators": True, "eui": 48 }) | ||
print('OK - test_valid_mac_address_no_separator_eui48') | ||
|
||
def test_invalid_mac_address_no_separators_eui48(self): | ||
invalid = [ | ||
'abababababababab', | ||
'FFFFFFFFFFFFFFFF', | ||
'01020304050607ab', | ||
'01AB030405060708', | ||
] | ||
self.invalid_check(invalid, { "no_separators": True, "eui": 48 }) | ||
print('OK - test_invalid_mac_address_no_separators_eui48') | ||
|
||
def test_valid_mac_address_no_separator_eui64(self): | ||
valid = [ | ||
'abababababababab', | ||
'FFFFFFFFFFFFFFFF', | ||
'01020304050607ab', | ||
'01AB030405060708', | ||
] | ||
|
||
self.valid_check(valid, { "no_separators": True, "eui": 64 }) | ||
print('OK - test_valid_mac_address_no_separator_eui64') | ||
|
||
def test_invalid_mac_address_no_separators_eui64(self): | ||
invalid = [ | ||
'abababababab', | ||
'FFFFFFFFFFFF', | ||
'0102030405ab', | ||
'01AB03040506', | ||
] | ||
self.invalid_check(invalid, { "no_separators": True, "eui": 64 }) | ||
print('OK - test_invalid_mac_address_no_separators_eui64') |