Skip to content

Commit

Permalink
feat: added is_mac_address validator
Browse files Browse the repository at this point in the history
  • Loading branch information
theteladras committed Apr 17, 2022
1 parent 3794e0c commit 30c1ad1
Show file tree
Hide file tree
Showing 5 changed files with 269 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ Validator | Description
**is_jwt(str)** | check if the string is valid JWT token.
**is_lat_long(str [, options])** | check if the string is a valid latitude-longitude coordinate in the format `lat,long` or `lat, long`.<br/><br/>`options` is an object that defaults to `{ "check_dms": False }`. Pass `check_dms` as `True` to validate DMS(degrees, minutes, and seconds) latitude-longitude format.
**isLowercase(str)** | check if the string is lowercase.
**is_mac_address(str, options)** | check if the string is a MAC address.<br/><br/>`options` is an dictionary which defaults to `{ "no_separators": False, "eui": None }`. If `no_separators` is *True*, the validator will allow MAC addresses without separators. Also, it allows the use of hyphens, spaces or dots e.g '01 02 03 04 05 ab', '01-02-03-04-05-ab' or '0102.0304.05ab'. The `eui` property helps specify if it needs to be validated against EUI-48 or EUI-64. The accepted values of `eui` are: [int] 48, 64, if not set it will check for either of them to be valid.
**is_md5(str)** | check if the string is a MD5 hash.<br/><br/>Please note that you can also use the `isHash(str, 'md5')` function. Keep in mind that MD5 has some collision weaknesses compared to other algorithms (e.g., SHA).
**is_mobile_number(str [, locale [, options]])** | check if the string is a mobile phone number,<br/><br/>locale can be an array of locales (e.g `['sr-RS', 'bs-BA']`) or a single locale from `['am-Am', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'az-AZ', 'az-LY', 'az-LB', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'ca-AD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'de-LU', 'el-GR', 'en-AU', 'en-BM', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-HK', 'en-MO', 'en-IE', 'en-IN', 'en-KE', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PK', 'en-PH', 'en-RW', 'en-SG', 'en-SL', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-HN', 'es-PE', 'es-EC', 'es-ES', 'es-MX', 'es-PA', 'es-PY', 'es-UY', 'es-VE', 'et-EE', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-RE', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'lt-LT', 'ms-MY', ''mz-MZ', nb-NO', 'ne-NP', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'pt-AO', 'ro-RO', 'ru-RU', 'si-LK' 'sl-SI', 'sk-SK', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tr-TR', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-MO', 'zh-TW', 'dz-BT']`, or can be left out which would implicitly set it to 'any' and it would try to find a signle valid value from the previouse list of locales (can be explicitly set as well).<br/><br/>`options` is an optional dictionary that can be supplied with the following keys: `strictMode`, if this is set to `True`, the mobile number must be supplied with the country code and therefore must start with `+`. If an invalid locale is provided, it will raise an exception.
**is_mobile_number(str, locale, options)** | check if the string is a mobile phone number,<br/><br/>locale can be an array of locales (e.g `['sr-RS', 'bs-BA']`) or a single locale from `['am-Am', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', ar-JO', 'ar-KW', 'ar-SA', 'ar-SY', 'ar-TN', 'az-AZ', 'az-LY', 'az-LB', 'bs-BA', 'be-BY', 'bg-BG', 'bn-BD', 'ca-AD', 'cs-CZ', 'da-DK', 'de-DE', 'de-AT', 'de-CH', 'de-LU', 'el-GR', 'en-AU', 'en-BM', 'en-CA', 'en-GB', 'en-GG', 'en-GH', 'en-HK', 'en-MO', 'en-IE', 'en-IN', 'en-KE', 'en-MT', 'en-MU', 'en-NG', 'en-NZ', 'en-PK', 'en-PH', 'en-RW', 'en-SG', 'en-SL', 'en-UG', 'en-US', 'en-TZ', 'en-ZA', 'en-ZM', 'en-ZW', 'es-AR', 'es-BO', 'es-CL', 'es-CO', 'es-CR', 'es-CU', 'es-DO', 'es-HN', 'es-PE', 'es-EC', 'es-ES', 'es-MX', 'es-PA', 'es-PY', 'es-UY', 'es-VE', 'et-EE', 'fa-IR', 'fi-FI', 'fj-FJ', 'fo-FO', 'fr-BE', 'fr-FR', 'fr-GF', 'fr-GP', 'fr-MQ', 'fr-RE', 'ga-IE', 'he-IL', 'hu-HU', 'id-ID', 'it-IT', 'it-SM', 'ja-JP', 'ka-GE', 'kk-KZ', 'kl-GL', 'ko-KR', 'lt-LT', 'ms-MY', ''mz-MZ', nb-NO', 'ne-NP', 'nl-BE', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'pt-AO', 'ro-RO', 'ru-RU', 'si-LK' 'sl-SI', 'sk-SK', 'sq-AL', 'sr-RS', 'sv-SE', 'tg-TJ', 'th-TH', 'tr-TR', 'uk-UA', 'uz-UZ', 'vi-VN', 'zh-CN', 'zh-HK', 'zh-MO', 'zh-TW', 'dz-BT']`, or can be left out which would implicitly set it to 'any' and it would try to find a signle valid value from the previouse list of locales (can be explicitly set as well).<br/><br/>`options` is an optional dictionary that can be supplied with the following keys: `strictMode`, if this is set to `True`, the mobile number must be supplied with the country code and therefore must start with `+`. If an invalid locale is provided, it will raise an exception.
**is_mongo_id(str)** | check if the string is a valid hex-encoded representation of a [MongoDB ObjectId][mongoid].
**is_multibyte(str)** | check if the string contains one or more multibyte chars.
**is_number(str [, options])** | check if the string is a number (int or float)
Expand Down
1 change: 1 addition & 0 deletions pyvalidator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,4 @@
from pyvalidator.is_regex import is_regex
from pyvalidator.is_css_unit import is_css_unit
from pyvalidator.is_url import is_url
from pyvalidator.is_mac_address import is_mac_address
48 changes: 48 additions & 0 deletions pyvalidator/is_mac_address.py
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)
)
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
readme = f.read()

MAJOR = 0
MINOR = 8
PATCH = 5
MINOR = 9
PATCH = 0

VERSION = '{}.{}.{}'.format(MAJOR, MINOR, PATCH)
DESCRIPTION = 'String validation and sanitization'
Expand Down
216 changes: 216 additions & 0 deletions test/test_is_mac_address.py
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')

0 comments on commit 30c1ad1

Please sign in to comment.