Skip to content

Commit

Permalink
Add valid netmask random generator
Browse files Browse the repository at this point in the history
  • Loading branch information
elyezer committed Oct 6, 2014
1 parent 75bef96 commit 97f86ce
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
16 changes: 15 additions & 1 deletion fauxfactory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'gen_iplum',
'gen_latin1',
'gen_mac',
'gen_netmask',
'gen_negative_integer',
'gen_numeric_string',
'gen_positive_integer',
Expand All @@ -38,7 +39,7 @@
from fauxfactory.constants import (
HTML_TAGS, LOREM_IPSUM_TEXT,
MAX_YEARS, MIN_YEARS,
SCHEMES, SUBDOMAINS, TLDS
SCHEMES, SUBDOMAINS, TLDS, VALID_NETMASKS
)
from functools import wraps

Expand Down Expand Up @@ -511,6 +512,19 @@ def gen_mac(delimiter=":"):
return _make_unicode(mac)


def gen_netmask():
"""Generates a random valid netmask.
For more info: http://www.iplocation.net/tools/netmask.php
:returns: The netmask is chosen from
:data:`fauxfactory.constants.VALID_NETMASKS`.
:rtype: str
"""
return random.choice(VALID_NETMASKS)


def gen_numeric_string(length=10):
"""Returns a random string made up of numbers.
Expand Down
30 changes: 30 additions & 0 deletions fauxfactory/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,36 @@
'org',
]

VALID_NETMASKS = (
u'240.0.0.0',
u'248.0.0.0',
u'252.0.0.0',
u'254.0.0.0',
u'255.0.0.0',
u'255.128.0.0',
u'255.192.0.0',
u'255.224.0.0',
u'255.240.0.0',
u'255.248.0.0',
u'255.252.0.0',
u'255.254.0.0',
u'255.255.0.0',
u'255.255.128.0',
u'255.255.192.0',
u'255.255.224.0',
u'255.255.240.0',
u'255.255.248.0',
u'255.255.252.0',
u'255.255.254.0',
u'255.255.255.0',
u'255.255.255.128',
u'255.255.255.192',
u'255.255.255.224',
u'255.255.255.240',
u'255.255.255.248',
u'255.255.255.252',
)

HTML_TAGS = [
'a', 'abbr', 'acronym', 'address', 'applet', 'area', 'b',
'base', 'basefont', 'bdo', 'big', 'blink', 'blockquote', 'body', 'br',
Expand Down
31 changes: 31 additions & 0 deletions tests/test_netmasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""Tests for the netmask generator"""

import re
import unittest

from fauxfactory import gen_netmask, VALID_NETMASKS


NETMASK_REGEX = re.compile(
'((255.){3}(0|128|192|224|240|248|252|254))|'
'((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})'
)


class NetmaskTestCase(unittest.TestCase):
"""Tests for ``gen_netmask`` generator."""

def test_gen_netmask(self):
"""Test if gen_netmask generates valid values"""
result = gen_netmask()
self.assertEqual(len(result.split('.')), 4)
self.assertIsNotNone(NETMASK_REGEX.match(result))

def test_valid_netmasks(self):
"""Test if VALID_NETMASKS constant have valid netmask values"""
for netmask in VALID_NETMASKS:
print netmask
self.assertIsNotNone(NETMASK_REGEX.match(netmask))

0 comments on commit 97f86ce

Please sign in to comment.