Skip to content

Commit

Permalink
Add MAC Address Multicast (#1918)
Browse files Browse the repository at this point in the history
  • Loading branch information
d3vyce authored Oct 9, 2023
1 parent f7c0ed5 commit ffe7277
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
14 changes: 12 additions & 2 deletions faker/providers/internet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,18 @@ def ipv6(self, network: bool = False) -> str:
address = str(IPv6Network(address, strict=False))
return address

def mac_address(self) -> str:
mac = [self.generator.random.randint(0x00, 0xFF) for _ in range(0, 6)]
def mac_address(self, multicast: bool = False) -> str:
"""
Returns a random MAC address.
:param multicast: Multicast address
:returns: MAC Address
"""
mac = [self.generator.random.randint(0x00, 0xFF) for _ in range(0, 5)]
if multicast is True:
mac.insert(0, self.generator.random.randrange(0x01, 0xFF, 2))
else:
mac.insert(0, self.generator.random.randrange(0x00, 0xFE, 2))
return ":".join("%02x" % x for x in mac)

def port_number(self, is_system: bool = False, is_user: bool = False, is_dynamic: bool = False) -> int:
Expand Down
9 changes: 9 additions & 0 deletions tests/providers/test_internet.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,15 @@ def test_ipv6(self, faker, num_samples):
assert len(address) <= 39 + 4
assert re.compile(r"^([0-9a-f]{0,4}:){2,7}[0-9a-f]{0,4}/\d{1,3}$").search(address)

def test_mac_address(self, faker):
provider = InternetProvider(faker)

unicast_address = provider.mac_address()
assert int(unicast_address[0:2], base=16) % 2 == 0

multicast_address = provider.mac_address(multicast=True)
assert int(multicast_address[0:2], base=16) % 2 == 1

def test_port_number(self, faker, num_samples):
for _ in range(num_samples):
assert 0 <= faker.port_number() <= 65535
Expand Down

0 comments on commit ffe7277

Please sign in to comment.