Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ccie18643 committed Sep 15, 2024
1 parent 266de8b commit 1b21a44
Show file tree
Hide file tree
Showing 34 changed files with 22 additions and 114 deletions.
2 changes: 1 addition & 1 deletion pytcp/stack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@

from pytcp.stack.arp_cache import ArpCache
from pytcp.stack.nd_cache import NdCache
from pytcp.stack.packet_handler import PacketHandler
from pytcp.stack.rx_ring import RxRing
from pytcp.stack.timer import Timer
from pytcp.stack.tx_ring import TxRing
from pytcp.subsystems.packet_handler import PacketHandler

if TYPE_CHECKING:
from net_addr import Ip4Address
Expand Down
File renamed without changes.
33 changes: 4 additions & 29 deletions tests__legacy/integration/packet_flows_rx.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,11 @@

from testslide import TestCase

from pytcp import config
from net_addr import Ip4Address, Ip4Host, Ip6Address, Ip6Host, MacAddress
from pytcp.lib.packet import PacketRx
from pytcp.lib.packet_stats import PacketStatsRx, PacketStatsTx
from pytcp.subsystems.packet_handler import PacketHandler

PACKET_HANDLER_MODULES = [
"pytcp.subsystems.packet_handler",
"protocols.ether.phrx",
"protocols.ether.phtx",
"protocols.arp.phrx",
"protocols.arp.phtx",
"protocols.ip4.phrx",
"protocols.ip4.phtx",
"protocols.ip6.phrx",
"protocols.ip6.phtx",
"protocols.icmp4.phrx",
"protocols.icmp4.phtx",
"protocols.icmp6.phrx",
"protocols.icmp6.phtx",
"protocols.udp.phrx",
"protocols.udp.phtx",
"protocols.tcp.phrx",
"protocols.tcp.phtx",
]
from pytcp.stack.packet_handler import PacketHandler


# Ensure critical configuration settings are set properly for the testing
Expand Down Expand Up @@ -115,14 +96,8 @@ def _patch_config(self) -> None:
"""
Patch critical config setting for all packet handler modules.
"""
for module in PACKET_HANDLER_MODULES:
for attribute, new_value in CONFIG_PATCHES.items():
try:
self.patch_attribute(
f"{module}.config", attribute, new_value
)
except ModuleNotFoundError:
continue
for attribute, new_value in CONFIG_PATCHES.items():
config.__dict__[attribute] = new_value

# Test name format:
# 'test_name__protocol_tested__test_description__optional_condition'
Expand Down
35 changes: 4 additions & 31 deletions tests__legacy/integration/packet_flows_rx_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,34 +37,15 @@

from testslide import StrictMock, TestCase

from pytcp import config
from net_addr import Ip4Address, Ip4Host, Ip6Address, Ip6Host, MacAddress
from pytcp.lib.packet import PacketRx
from pytcp.lib.packet_stats import PacketStatsRx, PacketStatsTx
from pytcp.stack.arp_cache import ArpCache
from pytcp.stack.nd_cache import NdCache
from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler
from pytcp.stack.tx_ring import TxRing

PACKET_HANDLER_MODULES = [
"pytcp.subsystems.packet_handler",
"protocols.ethernet.phrx",
"protocols.ethernet.phtx",
"protocols.arp.phrx",
"protocols.arp.phtx",
"protocols.ip4.phrx",
"protocols.ip4.phtx",
"protocols.ip6.phrx",
"protocols.ip6.phtx",
"protocols.icmp4.phrx",
"protocols.icmp4.phtx",
"protocols.icmp6.phrx",
"protocols.icmp6.phtx",
"protocols.udp.phrx",
"protocols.udp.phtx",
"protocols.tcp.phrx",
"protocols.tcp.phtx",
]


# Ensure critical configuration settings are set properly for
# the testing regardless of actual configuration
Expand Down Expand Up @@ -174,16 +155,8 @@ def _patch_config(self) -> None:
"""
Patch critical config setting for all packet handler modules.
"""
for module in PACKET_HANDLER_MODULES:
for attribute, new_value in CONFIG_PATCHES.items():
try:
self.patch_attribute(
target=f"{module}.config",
attribute=attribute,
new_value=new_value,
)
except ModuleNotFoundError:
continue
for attribute, new_value in CONFIG_PATCHES.items():
config.__dict__[attribute] = new_value

# Test name format:
# 'test_name__protocol_tested__test_description__optional_condition'
Expand Down
48 changes: 4 additions & 44 deletions tests__legacy/unit/mock_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@
from typing import no_type_check

from testslide import StrictMock, TestCase

from pytcp import config
from net_addr import Ip4Address, Ip4Host, Ip6Address, Ip6Host, MacAddress
from pytcp.stack.arp_cache import ArpCache
from pytcp.stack.nd_cache import NdCache
from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler
from pytcp.stack.tx_ring import TxRing

# # # IPv4
Expand Down Expand Up @@ -115,27 +115,6 @@ def __init__(self) -> None:
self.router_b_ip6_address = Ip6Address("fe80::2")


PACKET_HANDLER_MODULES = [
"pytcp.subsystems.packet_handler",
"pytcp.protocols.ether.phrx",
"pytcp.protocols.ether.phtx",
"pytcp.protocols.arp.phrx",
"pytcp.protocols.arp.phtx",
"pytcp.protocols.ip4.phrx",
"pytcp.protocols.ip4.phtx",
"pytcp.protocols.ip6.phrx",
"pytcp.protocols.ip6.phtx",
"pytcp.protocols.icmp4.phrx",
"pytcp.protocols.icmp4.phtx",
"pytcp.protocols.icmp6.phrx",
"pytcp.protocols.icmp6.phtx",
"pytcp.protocols.udp.phrx",
"pytcp.protocols.udp.phtx",
"pytcp.protocols.tcp.phrx",
"pytcp.protocols.tcp.phtx",
]


CONFIG_PATCHES = {
"LOG__CHANNEL": set(),
"IP6__SUPPORT_ENABLED": True,
Expand All @@ -151,27 +130,8 @@ def patch_config(self: TestCase, *, enable_log: bool = False) -> None:
"""
Patch critical config setting for all packet handler modules.
"""
for module in PACKET_HANDLER_MODULES:
for variable, value in CONFIG_PATCHES.items():
if enable_log and variable == "LOG__CHANNEL":
value = {
"stack",
"arp-c",
"nd-c",
"ether",
"arp",
"ip4",
"ip6",
"icmp4",
"icmp6",
"udp",
"tcp",
"socket",
}
try:
self.patch_attribute(f"{module}.config", variable, value)
except ModuleNotFoundError:
continue
for attribute, new_value in CONFIG_PATCHES.items():
config.__dict__[attribute] = new_value


# Had to disable type checking in this function because it modifies variables
Expand Down
2 changes: 1 addition & 1 deletion tests__legacy/unit/protocols__arp__phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.lib.tx_status import TxStatus
from pytcp.protocols.arp.arp__header import ArpOperation
from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler


class TestArpPhtx(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests__legacy/unit/protocols__ethernet__phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.lib.tx_status import TxStatus
from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler

TEST_FRAME_DIR = "tests__legacy/unit/test_frames/ethernet_phtx/"

Expand Down
2 changes: 1 addition & 1 deletion tests__legacy/unit/protocols__icmp4__phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
from pytcp.protocols.icmp4.message.icmp4_message__echo_request import (
Icmp4EchoRequestMessage,
)
from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler

TEST_FRAME_DIR = "tests__legacy/unit/test_frames/icmp4_phtx/"

Expand Down
2 changes: 1 addition & 1 deletion tests__legacy/unit/protocols__icmp6__phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
Icmp6NdOptions,
Icmp6NdOptionSlla,
)
from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler

TEST_FRAME_DIR = "tests__legacy/unit/test_frames/icmp6_phtx/"

Expand Down
2 changes: 1 addition & 1 deletion tests__legacy/unit/protocols__ip4__phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.lib.tx_status import TxStatus
from pytcp.protocols.raw.raw__assembler import RawAssembler
from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler

TEST_FRAME_DIR = "tests__legacy/unit/test_frames/ip4_phtx/"

Expand Down
2 changes: 1 addition & 1 deletion tests__legacy/unit/protocols__ip6__phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.lib.tx_status import TxStatus
from pytcp.protocols.raw.raw__assembler import RawAssembler
from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler

TEST_FRAME_DIR = "tests__legacy/unit/test_frames/ip6_phtx/"

Expand Down
2 changes: 1 addition & 1 deletion tests__legacy/unit/protocols__tcp__phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.lib.tx_status import TxStatus
from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler

TEST_FRAME_DIR = "tests__legacy/unit/test_frames/tcp_phtx/"

Expand Down
2 changes: 1 addition & 1 deletion tests__legacy/unit/protocols__udp__phtx.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

from pytcp.lib.packet_stats import PacketStatsTx
from pytcp.lib.tx_status import TxStatus
from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler

TEST_FRAME_DIR = "tests__legacy/unit/test_frames/udp_phtx/"

Expand Down
2 changes: 1 addition & 1 deletion tests__legacy/unreliable/subsystems__packet_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

from testslide import TestCase

from pytcp.subsystems.packet_handler import PacketHandler
from pytcp.stack.packet_handler import PacketHandler


class TestPacketHandler(TestCase):
Expand Down

0 comments on commit 1b21a44

Please sign in to comment.