Skip to content

Commit

Permalink
add IPv4 & IPv6 generator
Browse files Browse the repository at this point in the history
  • Loading branch information
fgonzalezaguilera committed Jul 12, 2021
1 parent 540f627 commit fb04a7c
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Currently supported generators are:
* Sequence: Create integers in sequence if called multiple times
* Name: Create a random name from a list `data/names/names.txt`
* Email: Create a random email, a combination of the random name generator and a domain from `data/domains/domains.txt`
* IPv4/IPv6: Create a random IPv4 or Ipv6 address
* String: String generation from a pattern

Feel free to roll your own generator by subclassing `Generator` and implement a `generate()` method that return the generated value.
Expand Down
34 changes: 34 additions & 0 deletions flask_seeder/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import uuid
import random
from ipaddress import IPv4Address, IPv6Address
import pkg_resources

from flask_seeder.parser import SGParser, Tokenizer
Expand Down Expand Up @@ -338,3 +339,36 @@ def generate(self):
result += func(node)

return result


class IPv4(Generator):
""" Random IPv4 generator """
IPV4LENGTH = 32
IPV4_MAX_PREFIX_LEN = (2**IPV4LENGTH) - 1

def __init__(self, rnd=None, **kwargs):
super().__init__(**kwargs)
self.rnd = rnd or random

def generate(self):
""" Generate a random IPv4 address """

return str(IPv4Address(
self.rnd.randint(0, self.IPV4_MAX_PREFIX_LEN)
))


class IPv6(Generator):
""" Random IPv6 generator """
IPV6LENGTH = 128
IPV6_MAX_PREFIX_LEN = (2**IPV6LENGTH) - 1

def __init__(self, rnd=None, **kwargs):
super().__init__(**kwargs)
self.rnd = rnd or random

def generate(self):
""" Generate a random IPv6 address """
return str(IPv6Address(
self.rnd.randint(0, self.IPV6_MAX_PREFIX_LEN)
))
27 changes: 27 additions & 0 deletions tests/generator/test_ipv4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from ipaddress import IPv4Address

from unittest import TestCase
from unittest.mock import MagicMock, patch

from flask_seeder.generator import IPv4


class TestIPv4Generator(TestCase):
def setUp(self):
self.rnd_mock = MagicMock()
# return value that will be used inside _string_from_ip_int function
self.rnd_mock.randint.side_effect = [IPv4Address._ALL_ONES]


def test_generate_mock_ipv4_with_default_values(self):
self.generator = IPv4(rnd=self.rnd_mock)
self.generator.generate()
self.rnd_mock.randint.assert_called_once_with(
0,
IPv4Address._ALL_ONES
)

def test_generate_mock_ipv4_returns_a_string(self):
self.generator = IPv4()
result = self.generator.generate()
self.assertEqual(type(result), str)
27 changes: 27 additions & 0 deletions tests/generator/test_ipv6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from ipaddress import IPv6Address

from unittest import TestCase
from unittest.mock import MagicMock, patch

from flask_seeder.generator import IPv6


class TestIPv6Generator(TestCase):
def setUp(self):
self.rnd_mock = MagicMock()
# return value that will be used inside _string_from_ip_int function
self.rnd_mock.randint.side_effect = [IPv6Address._ALL_ONES]


def test_generate_mock_ipv6_with_default_values(self):
self.generator = IPv6(rnd=self.rnd_mock)
self.generator.generate()
self.rnd_mock.randint.assert_called_once_with(
0,
IPv6Address._ALL_ONES
)

def test_generate_mock_ipv6_returns_a_string(self):
self.generator = IPv6()
result = self.generator.generate()
self.assertEqual(type(result), str)

0 comments on commit fb04a7c

Please sign in to comment.