Skip to content

Commit

Permalink
Merge pull request #56 from elyezer/improve-gen-netmask
Browse files Browse the repository at this point in the history
Improve gen_netmask
  • Loading branch information
omaciel committed Oct 6, 2014
2 parents 713949c + edcb4d7 commit 5c0d583
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
8 changes: 5 additions & 3 deletions fauxfactory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,17 +512,19 @@ def gen_mac(delimiter=":"):
return _make_unicode(mac)


def gen_netmask():
def gen_netmask(min_cidr=1, max_cidr=31):
"""Generates a random valid netmask.
For more info: http://www.iplocation.net/tools/netmask.php
:param int min_cidr: Inferior CIDR limit
:param int max_cidr: Superior CIDR limit
:returns: The netmask is chosen from
:data:`fauxfactory.constants.VALID_NETMASKS`.
:data:`fauxfactory.constants.VALID_NETMASKS` respecting the CIDR range
:rtype: str
"""
return random.choice(VALID_NETMASKS)
return VALID_NETMASKS[random.randint(min_cidr, max_cidr)]


def gen_numeric_string(length=10):
Expand Down
4 changes: 4 additions & 0 deletions fauxfactory/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
'org',
]

# The tuple index corresponds to a CIDR number
# E.g. CIDR 1 == VALID_NETMASKS[1]
VALID_NETMASKS = (
u'0.0.0.0',
u'128.0.0.0',
u'192.0.0.0',
u'224.0.0.0',
Expand Down Expand Up @@ -74,6 +77,7 @@
u'255.255.255.248',
u'255.255.255.252',
u'255.255.255.254',
u'255.255.255.255',
)

HTML_TAGS = [
Expand Down
9 changes: 7 additions & 2 deletions tests/test_netmasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@


NETMASK_REGEX = re.compile(
'((255.){3}(0|128|192|224|240|248|252|254))|'
'((255.){3}(0|128|192|224|240|248|252|254|255))|'
'((255.){2}(0|128|192|224|240|248|252|254).0)|'
'(255.(0|128|192|224|240|248|252|254)(.0){2})|'
'((128|192|224|240|248|252|254)(.0){3})'
'((0|128|192|224|240|248|252|254)(.0){3})'
)


Expand All @@ -24,6 +24,11 @@ def test_gen_netmask(self):
self.assertEqual(len(result.split('.')), 4)
self.assertIsNotNone(NETMASK_REGEX.match(result))

def test_gen_netmask_boundary(self):
"""Test gen_netmask boundary cases"""
self.assertEqual(u'0.0.0.0', gen_netmask(0, 0))
self.assertEqual(u'255.255.255.255', gen_netmask(32, 32))

def test_valid_netmasks(self):
"""Test if VALID_NETMASKS constant have valid netmask values"""
for netmask in VALID_NETMASKS:
Expand Down

0 comments on commit 5c0d583

Please sign in to comment.