99from smpclient .exceptions import SMPClientException
1010from smpclient .requests .os_management import EchoWrite
1111from smpclient .transport ._udp_client import Addr , UDPClient
12- from smpclient .transport .udp import SMPUDPTransport
12+ from smpclient .transport .udp import IPV4_UDP_OVERHEAD , IPV6_UDP_OVERHEAD , SMPUDPTransport
1313
1414
1515def test_init () -> None :
@@ -27,6 +27,10 @@ async def test_connect(_: MagicMock) -> None:
2727 t = SMPUDPTransport ()
2828 t ._client = cast (MagicMock , t ._client ) # type: ignore
2929
30+ # Mock _transport for IPv4/IPv6 detection
31+ t ._client ._transport = MagicMock ()
32+ t ._client ._transport .get_extra_info .return_value = None
33+
3034 await t .connect ("192.168.0.1" , 0.001 )
3135 t ._client .connect .assert_awaited_once_with (Addr (host = "192.168.0.1" , port = 1337 ))
3236
@@ -110,3 +114,48 @@ async def test_send_and_receive() -> None:
110114 await t .send_and_receive (message )
111115 send_mock .assert_awaited_once_with (message )
112116 receive_mock .assert_awaited_once ()
117+
118+
119+ def test_max_unencoded_size_ipv4 () -> None :
120+ """Test MSS calculation for IPv4 (default)."""
121+ t = SMPUDPTransport (mtu = 1500 )
122+ # Before connection, defaults to IPv4
123+ assert t .max_unencoded_size == 1500 - IPV4_UDP_OVERHEAD
124+ assert t .max_unencoded_size == 1472
125+
126+
127+ def test_max_unencoded_size_custom_mtu () -> None :
128+ """Test MSS calculation with custom MTU."""
129+ t = SMPUDPTransport (mtu = 512 )
130+ assert t .max_unencoded_size == 512 - IPV4_UDP_OVERHEAD
131+ assert t .max_unencoded_size == 484
132+
133+
134+ @pytest .mark .asyncio
135+ async def test_ipv4_detection_real_socket () -> None :
136+ """Test IPv4 auto-detection with real socket connection."""
137+ t = SMPUDPTransport (mtu = 1500 )
138+
139+ # Create a real UDP connection to localhost IPv4
140+ await t .connect ("127.0.0.1" , 1.0 )
141+
142+ assert t ._is_ipv6 is False
143+ assert t .max_unencoded_size == 1500 - IPV4_UDP_OVERHEAD
144+ assert t .max_unencoded_size == 1472
145+
146+ await t .disconnect ()
147+
148+
149+ @pytest .mark .asyncio
150+ async def test_ipv6_detection_real_socket () -> None :
151+ """Test IPv6 auto-detection with real socket connection."""
152+ t = SMPUDPTransport (mtu = 1500 )
153+
154+ # Create a real UDP connection to localhost IPv6
155+ await t .connect ("::1" , 1.0 )
156+
157+ assert t ._is_ipv6 is True
158+ assert t .max_unencoded_size == 1500 - IPV6_UDP_OVERHEAD
159+ assert t .max_unencoded_size == 1452
160+
161+ await t .disconnect ()
0 commit comments