Skip to content

Commit 521a86d

Browse files
committed
Add tests for Udp6Transport and sockfix module functionality
1 parent e9e7411 commit 521a86d

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

tests/carrier/asyncio/test_udp6.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import asyncio
2+
import importlib
3+
import socket
4+
import sys
5+
from unittest import mock
6+
7+
import pytest
8+
9+
from pysnmp.carrier.asyncio.dgram import udp6
10+
11+
12+
def test_udp6_normalize_address_strips_zone_id():
13+
transport = udp6.Udp6Transport(loop=asyncio.new_event_loop())
14+
15+
normalized = transport.normalize_address(("fe80::1%eth0", 161))
16+
17+
assert normalized[0] == "fe80::1"
18+
assert normalized[1] == 161
19+
assert normalized[2] == 0
20+
assert normalized[3] == 0
21+
22+
23+
def test_udp6_transport_sets_ipv6_transparent_constant(monkeypatch):
24+
monkeypatch.delattr(socket, "IPV6_TRANSPARENT", raising=False)
25+
26+
module_name = "pysnmp.carrier.sockfix"
27+
if module_name in sys.modules:
28+
del sys.modules[module_name]
29+
30+
sockfix = importlib.import_module(module_name)
31+
32+
assert hasattr(socket, "IPV6_TRANSPARENT")
33+
assert socket.IPV6_TRANSPARENT == 75
34+
assert sockfix.SYMBOLS["IPV6_TRANSPARENT"] == 75
35+
36+
37+
@pytest.mark.asyncio
38+
async def test_udp6_transport_send_receive_loopback():
39+
if not socket.has_ipv6:
40+
pytest.skip("IPv6 is not available on this platform")
41+
42+
loop = asyncio.get_event_loop()
43+
received = []
44+
45+
def on_receive(transport, addr, msg):
46+
received.append(msg)
47+
48+
server = udp6.Udp6Transport(loop=loop)
49+
server._callback_function = on_receive
50+
server.open_server_mode(iface=("::1", 0))
51+
await asyncio.sleep(0.05)
52+
53+
server_future = server._lport
54+
server_transport, _ = await server_future
55+
port = server_transport.get_extra_info("sockname")[1]
56+
57+
client = udp6.Udp6Transport(loop=loop)
58+
client._callback_function = mock.Mock()
59+
client.open_client_mode(iface=("::1", 0))
60+
await asyncio.sleep(0.05)
61+
62+
payload = b"snmp-ipv6-test"
63+
client.send_message(payload, ("::1", port))
64+
65+
await asyncio.sleep(0.05)
66+
67+
assert received == [payload]
68+
69+
client.close_transport()
70+
server.close_transport()

tests/carrier/test_sockfix.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import importlib
2+
import socket
3+
import sys
4+
5+
6+
def test_sockfix_defines_ipv6_transparent_when_missing(monkeypatch):
7+
"""Ensure the carrier sockfix adds IPV6_TRANSPARENT when the socket module lacks it."""
8+
monkeypatch.delattr(socket, "IPV6_TRANSPARENT", raising=False)
9+
10+
module_name = "pysnmp.carrier.sockfix"
11+
if module_name in sys.modules:
12+
del sys.modules[module_name]
13+
14+
sockfix = importlib.import_module(module_name)
15+
16+
assert hasattr(socket, "IPV6_TRANSPARENT")
17+
assert socket.IPV6_TRANSPARENT == 75
18+
assert sockfix.SYMBOLS["IPV6_TRANSPARENT"] == 75
19+
20+
21+
def test_sockfix_does_not_override_existing_ipv6_transparent(monkeypatch):
22+
"""Ensure existing IPV6_TRANSPARENT values are preserved when loading sockfix."""
23+
monkeypatch.setattr(socket, "IPV6_TRANSPARENT", 0xBEEF, raising=False)
24+
25+
module_name = "pysnmp.carrier.sockfix"
26+
if module_name in sys.modules:
27+
del sys.modules[module_name]
28+
29+
importlib.import_module(module_name)
30+
31+
assert socket.IPV6_TRANSPARENT == 0xBEEF

0 commit comments

Comments
 (0)