Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 8, 2024
1 parent 5491860 commit 176cd05
Show file tree
Hide file tree
Showing 13 changed files with 231 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"""


from typing import Any

from testslide import TestCase

from pytcp.lib.int_checks import UINT_16__MAX, UINT_16__MIN
Expand All @@ -57,7 +59,8 @@ def setUp(self) -> None:
message constructor.
"""

self._message_kwargs = {
self._args: list[Any] = []
self._kwargs: dict[str, Any] = {
"code": Icmp4DestinationUnreachableCode.NETWORK,
"mtu": None,
"cksum": 0,
Expand All @@ -73,10 +76,10 @@ def test__icmp4__destination_unreachable__code__not_Icmp4DestinationUnreachableC
an Icmp4DestinationUnreachableCode.
"""

self._message_kwargs["code"] = value = "not an Icmp4DestinationUnreachableCode" # type: ignore
self._kwargs["code"] = value = "not an Icmp4DestinationUnreachableCode"

with self.assertRaises(AssertionError) as error:
Icmp4DestinationUnreachableMessage(**self._message_kwargs) # type: ignore
Icmp4DestinationUnreachableMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -94,15 +97,13 @@ def test__icmp4__destination_unreachable__frag_no_mtu(
provided.
"""

self._message_kwargs["code"] = (
self._kwargs["code"] = (
Icmp4DestinationUnreachableCode.FRAGMENTATION_NEEDED
)
self._message_kwargs["mtu"] = value = None
self._kwargs["mtu"] = value = None

with self.assertRaises(AssertionError) as error:
Icmp4DestinationUnreachableMessage(
**self._message_kwargs, # type: ignore
)
Icmp4DestinationUnreachableMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -120,13 +121,13 @@ def test__icmp4__destination_unreachable__no_frag_mtu(
provided.
"""

self._message_kwargs["mtu"] = value = 1500
self._kwargs["mtu"] = value = 1500

for code in Icmp4DestinationUnreachableCode:
if code != Icmp4DestinationUnreachableCode.FRAGMENTATION_NEEDED:
with self.assertRaises(AssertionError) as error:
Icmp4DestinationUnreachableMessage(
**self._message_kwargs, # type: ignore
*self._args, **self._kwargs
)

self.assertEqual(
Expand All @@ -143,12 +144,10 @@ def test__icmp4__destination_unreachable__cksum__under_min(
is lower than the minimum supported value.
"""

self._message_kwargs["cksum"] = value = UINT_16__MIN - 1
self._kwargs["cksum"] = value = UINT_16__MIN - 1

with self.assertRaises(AssertionError) as error:
Icmp4DestinationUnreachableMessage(
**self._message_kwargs, # type: ignore
)
Icmp4DestinationUnreachableMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -165,12 +164,10 @@ def test__icmp4__destination_unreachable__cksum__over_max(
is higher than the maximum supported value.
"""

self._message_kwargs["cksum"] = value = UINT_16__MAX + 1
self._kwargs["cksum"] = value = UINT_16__MAX + 1

with self.assertRaises(AssertionError) as error:
Icmp4DestinationUnreachableMessage(
**self._message_kwargs, # type: ignore
)
Icmp4DestinationUnreachableMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -187,15 +184,13 @@ def test__icmp4__destination_unreachable__mtu__under_min(
is lower than the minimum supported value.
"""

self._message_kwargs["code"] = (
self._kwargs["code"] = (
Icmp4DestinationUnreachableCode.FRAGMENTATION_NEEDED
)
self._message_kwargs["mtu"] = value = UINT_16__MIN - 1
self._kwargs["mtu"] = value = UINT_16__MIN - 1

with self.assertRaises(AssertionError) as error:
Icmp4DestinationUnreachableMessage(
**self._message_kwargs, # type: ignore
)
Icmp4DestinationUnreachableMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -212,15 +207,13 @@ def test__icmp4__destination_unreachable__mtu__over_max(
is higher than the maximum supported value.
"""

self._message_kwargs["code"] = (
self._kwargs["code"] = (
Icmp4DestinationUnreachableCode.FRAGMENTATION_NEEDED
)
self._message_kwargs["mtu"] = value = UINT_16__MAX + 1
self._kwargs["mtu"] = value = UINT_16__MAX + 1

with self.assertRaises(AssertionError) as error:
Icmp4DestinationUnreachableMessage(
**self._message_kwargs, # type: ignore
)
Icmp4DestinationUnreachableMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -238,12 +231,10 @@ def test__icmp4__destination_unreachable__data_len__over_max(
"""

value = IP4__PAYLOAD__MAX_LEN - ICMP4__DESTINATION_UNREACHABLE__LEN + 1
self._message_kwargs["data"] = b"X" * value
self._kwargs["data"] = b"X" * value

with self.assertRaises(AssertionError) as error:
Icmp4DestinationUnreachableMessage(
**self._message_kwargs, # type: ignore
)
Icmp4DestinationUnreachableMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand Down
39 changes: 20 additions & 19 deletions tests/unit/protocols/icmp4/test__icmp4__echo_reply__asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"""


from typing import Any

from testslide import TestCase

from pytcp.lib.int_checks import UINT_16__MAX, UINT_16__MIN
Expand All @@ -57,7 +59,8 @@ def setUp(self) -> None:
constructor.
"""

self._message_kwargs = {
self._args: list[Any] = []
self._kwargs: dict[str, Any] = {
"code": Icmp4EchoReplyCode.DEFAULT,
"cksum": 0,
"id": 0,
Expand All @@ -73,10 +76,10 @@ def test__icmp4__echo_reply__code__not_Icmp4EchoReplyCode(
when the provided 'code' argument is not an Icmp4EchoReplyCode.
"""

self._message_kwargs["code"] = value = "not an Icmp4EchoReplyCode"
self._kwargs["code"] = value = "not an Icmp4EchoReplyCode"

with self.assertRaises(AssertionError) as error:
Icmp4EchoReplyMessage(**self._message_kwargs) # type: ignore
Icmp4EchoReplyMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -91,10 +94,10 @@ def test__icmp4__echo_reply__cksum__under_min(self) -> None:
minimum supported value.
"""

self._message_kwargs["cksum"] = value = UINT_16__MIN - 1
self._kwargs["cksum"] = value = UINT_16__MIN - 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoReplyMessage(**self._message_kwargs) # type: ignore
Icmp4EchoReplyMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -109,10 +112,10 @@ def test__icmp4__echo_reply__cksum__over_max(self) -> None:
maximum supported value.
"""

self._message_kwargs["cksum"] = value = UINT_16__MAX + 1
self._kwargs["cksum"] = value = UINT_16__MAX + 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoReplyMessage(**self._message_kwargs) # type: ignore
Icmp4EchoReplyMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -127,10 +130,10 @@ def test__icmp4__echo_reply__id__under_min(self) -> None:
minimum supported value.
"""

self._message_kwargs["id"] = value = UINT_16__MIN - 1
self._kwargs["id"] = value = UINT_16__MIN - 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoReplyMessage(**self._message_kwargs) # type: ignore
Icmp4EchoReplyMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -145,10 +148,10 @@ def test__icmp4__echo_reply__id__over_max(self) -> None:
maximum supported value.
"""

self._message_kwargs["id"] = value = UINT_16__MAX + 1
self._kwargs["id"] = value = UINT_16__MAX + 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoReplyMessage(**self._message_kwargs) # type: ignore
Icmp4EchoReplyMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -163,10 +166,10 @@ def test__icmp4__echo_reply__seq__under_min(self) -> None:
minimum supported value.
"""

self._message_kwargs["seq"] = value = UINT_16__MIN - 1
self._kwargs["seq"] = value = UINT_16__MIN - 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoReplyMessage(**self._message_kwargs) # type: ignore
Icmp4EchoReplyMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -181,10 +184,10 @@ def test__icmp4__echo_reply__seq__over_max(self) -> None:
maximum supported value.
"""

self._message_kwargs["seq"] = value = UINT_16__MAX + 1
self._kwargs["seq"] = value = UINT_16__MAX + 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoReplyMessage(**self._message_kwargs) # type: ignore
Icmp4EchoReplyMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -200,12 +203,10 @@ def test__icmp4__echo_reply__data_len__over_max(self) -> None:
"""

value = IP4__PAYLOAD__MAX_LEN - ICMP4__ECHO_REPLY__LEN + 1
self._message_kwargs["data"] = b"X" * value
self._kwargs["data"] = b"X" * value

with self.assertRaises(AssertionError) as error:
Icmp4EchoReplyMessage(
**self._message_kwargs, # type: ignore
)
Icmp4EchoReplyMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand Down
39 changes: 20 additions & 19 deletions tests/unit/protocols/icmp4/test__icmp4__echo_request__asserts.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"""


from typing import Any

from testslide import TestCase

from pytcp.lib.int_checks import UINT_16__MAX, UINT_16__MIN
Expand All @@ -57,7 +59,8 @@ def setUp(self) -> None:
constructor.
"""

self._message_kwargs = {
self._args: list[Any] = []
self._kwargs: dict[str, Any] = {
"code": Icmp4EchoRequestCode.DEFAULT,
"cksum": 0,
"id": 0,
Expand All @@ -73,10 +76,10 @@ def test__icmp4__echo_request__code__not_Icmp4EchoRequestCode(
when the provided 'code' argument is not an Icmp4EchoRequestCode.
"""

self._message_kwargs["code"] = value = "not an Icmp4EchoRequestCode"
self._kwargs["code"] = value = "not an Icmp4EchoRequestCode"

with self.assertRaises(AssertionError) as error:
Icmp4EchoRequestMessage(**self._message_kwargs) # type: ignore
Icmp4EchoRequestMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -91,10 +94,10 @@ def test__icmp4__echo_request__cksum__under_min(self) -> None:
minimum supported value.
"""

self._message_kwargs["cksum"] = value = UINT_16__MIN - 1
self._kwargs["cksum"] = value = UINT_16__MIN - 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoRequestMessage(**self._message_kwargs) # type: ignore
Icmp4EchoRequestMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -109,10 +112,10 @@ def test__icmp4__echo_request__cksum__over_max(self) -> None:
maximum supported value.
"""

self._message_kwargs["cksum"] = value = UINT_16__MAX + 1
self._kwargs["cksum"] = value = UINT_16__MAX + 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoRequestMessage(**self._message_kwargs) # type: ignore
Icmp4EchoRequestMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -127,10 +130,10 @@ def test__icmp4__echo_request__id__under_min(self) -> None:
minimum supported value.
"""

self._message_kwargs["id"] = value = UINT_16__MIN - 1
self._kwargs["id"] = value = UINT_16__MIN - 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoRequestMessage(**self._message_kwargs) # type: ignore
Icmp4EchoRequestMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -145,10 +148,10 @@ def test__icmp4__echo_request__id__over_max(self) -> None:
maximum supported value.
"""

self._message_kwargs["id"] = value = UINT_16__MAX + 1
self._kwargs["id"] = value = UINT_16__MAX + 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoRequestMessage(**self._message_kwargs) # type: ignore
Icmp4EchoRequestMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -163,10 +166,10 @@ def test__icmp4__echo_request__seq__under_min(self) -> None:
minimum supported value.
"""

self._message_kwargs["seq"] = value = UINT_16__MIN - 1
self._kwargs["seq"] = value = UINT_16__MIN - 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoRequestMessage(**self._message_kwargs) # type: ignore
Icmp4EchoRequestMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -181,10 +184,10 @@ def test__icmp4__echo_request__seq__over_max(self) -> None:
maximum supported value.
"""

self._message_kwargs["seq"] = value = UINT_16__MAX + 1
self._kwargs["seq"] = value = UINT_16__MAX + 1

with self.assertRaises(AssertionError) as error:
Icmp4EchoRequestMessage(**self._message_kwargs) # type: ignore
Icmp4EchoRequestMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand All @@ -200,12 +203,10 @@ def test__icmp4__echo_request__data_len__over_max(self) -> None:
"""

value = IP4__PAYLOAD__MAX_LEN - ICMP4__ECHO_REQUEST__LEN + 1
self._message_kwargs["data"] = b"X" * value
self._kwargs["data"] = b"X" * value

with self.assertRaises(AssertionError) as error:
Icmp4EchoRequestMessage(
**self._message_kwargs, # type: ignore
)
Icmp4EchoRequestMessage(*self._args, **self._kwargs)

self.assertEqual(
str(error.exception),
Expand Down
Loading

0 comments on commit 176cd05

Please sign in to comment.