Skip to content

Commit

Permalink
Added unit tests for Ip6Host class
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 7, 2024
1 parent f48a762 commit 51b68bc
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 17 deletions.
6 changes: 4 additions & 2 deletions pytcp/lib/net_addr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
Ip4AddressFormatError,
Ip4HostFormatError,
Ip4HostGatewayError,
Ip4HostSanityError,
Ip4MaskFormatError,
Ip4NetworkFormatError,
Ip6AddressFormatError,
Ip6HostFormatError,
Ip6HostGatewayError,
Ip6HostSanityError,
Ip6MaskFormatError,
Ip6NetworkFormatError,
IpAddressFormatError,
Expand All @@ -52,11 +54,11 @@
MacAddressFormatError,
)
from .ip4_address import IP4__ADDRESS_LEN, Ip4Address
from .ip4_host import Ip4Host
from .ip4_host import Ip4Host, Ip4HostOrigin
from .ip4_mask import Ip4Mask
from .ip4_network import Ip4Network
from .ip6_address import IP6__ADDRESS_LEN, Ip6Address
from .ip6_host import Ip6Host
from .ip6_host import Ip6Host, Ip6HostOrigin
from .ip6_mask import Ip6Mask
from .ip6_network import Ip6Network
from .ip_address import IpAddress
Expand Down
2 changes: 1 addition & 1 deletion pytcp/lib/net_addr/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class Ip6HostSanityError(IpHostFormatError):

def __init__(self, message: Any, /):
super().__init__(
f"The IPv6 host doesn't belong to provided network: {message!r}"
f"The IPv6 address doesn't belong to the provided network: {message!r}"
)


Expand Down
7 changes: 4 additions & 3 deletions pytcp/lib/net_addr/ip6_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Ip6HostOrigin(IpHostOrigin):
"""

STATIC = auto()
ND = auto()
AUTOCONFIG = auto()
DHCP = auto()
UNKNOWN = auto()

Expand Down Expand Up @@ -97,7 +97,7 @@ def __init__(
self._origin = origin or Ip6HostOrigin.UNKNOWN
self._expiration_time = expiration_time or 0

if self._origin in {Ip6HostOrigin.ND, Ip6HostOrigin.DHCP}:
if self._origin in {Ip6HostOrigin.AUTOCONFIG, Ip6HostOrigin.DHCP}:
assert self._expiration_time >= int(time.time())
else:
assert self._expiration_time == 0
Expand Down Expand Up @@ -155,7 +155,8 @@ def _validate_gateway(self, address: Ip6Address | None, /) -> None:
"""

if address is not None and (
address not in Ip6Network("fe80::/10")
not address.is_global
and not address.is_link_local
or address == self._network.address
or address == self._address
):
Expand Down
17 changes: 9 additions & 8 deletions tests/unit/lib/net_addr/test__ip4_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@
from parameterized import parameterized_class # type: ignore
from testslide import TestCase

from pytcp.lib.net_addr.errors import Ip4HostSanityError
from pytcp.lib.net_addr.ip4_address import Ip4Address
from pytcp.lib.net_addr.ip4_host import Ip4Host, Ip4HostOrigin
from pytcp.lib.net_addr.ip4_mask import Ip4Mask
from pytcp.lib.net_addr.ip4_network import Ip4Network
from pytcp.lib.net_addr import (
Ip4Address,
Ip4Host,
Ip4HostOrigin,
Ip4HostSanityError,
Ip4Mask,
Ip4Network,
)

IP4_ADDRESS_EXPIRATION_TIME = int(time.time() + 3600)

Expand Down Expand Up @@ -96,7 +99,6 @@
"_kwargs": {
"gateway": Ip4Address("192.168.1.1"),
"origin": Ip4HostOrigin.STATIC,
"expiration_time": 0,
},
"_results": {
"__str__": "192.168.1.100/24",
Expand All @@ -120,7 +122,6 @@
"_kwargs": {
"gateway": Ip4Address("192.168.1.1"),
"origin": Ip4HostOrigin.STATIC,
"expiration_time": 0,
},
"_results": {
"__str__": "192.168.1.100/24",
Expand Down Expand Up @@ -267,7 +268,7 @@ def test__net_addr__ip4_host__expiration_time(self) -> None:
@parameterized_class(
[
{
"_description": "Test the IPv4 host: 192.168.1.100/24 (Ip4Address, Ip4Network)",
"_description": "Test the IPv4 host where address is not part of the network.",
"_args": [
(Ip4Address("192.168.1.100"), Ip4Network("192.168.2.0/24"))
],
Expand Down
Loading

0 comments on commit 51b68bc

Please sign in to comment.