Debian Codegraph @

tests/profiles/test_igd.py

raw symbols SCIP
1 """Unit tests for the IGD profile."""
2
3 from datetime import datetime, timedelta
4
5 import pytest
6
7 from async_upnp_client.client import UpnpDevice
8 from async_upnp_client.client_factory import UpnpFactory
9 from async_upnp_client.const import HttpResponse
10 from async_upnp_client.event_handler import UpnpEventHandler
11 from async_upnp_client.profiles.igd import (
12 IgdDevice,
13 IgdStateItem,
14 TrafficCounterState,
15 )
16
17 from ..conftest import RESPONSE_MAP, UpnpTestNotifyServer, UpnpTestRequester, read_file
18
19
20 class FixedCounterIgdDevice(IgdDevice):
21 """IGD device with deterministic counters for overflow tests."""
22
23 def __init__(
24 self,
25 device: UpnpDevice,
26 event_handler: UpnpEventHandler | None,
27 initial_traffic_state: TrafficCounterState,
28 current_traffic_state: TrafficCounterState,
29 ) -> None:
30 """Initialize test device with fixed traffic counters."""
31 super().__init__(device, event_handler)
32 self._last_traffic_state = initial_traffic_state
33 self._current_traffic_state = current_traffic_state
34
35 async def async_poll_traffic_data(
36 self,
37 items: set[IgdStateItem],
38 ) -> TrafficCounterState:
39 """Return a fixed traffic state for tests."""
40 return self._current_traffic_state
41
42
43 @pytest.mark.asyncio
44 async def test_init_igd_profile() -> None:
45 """Test if a IGD device can be initialized."""
46 requester = UpnpTestRequester(RESPONSE_MAP)
47 factory = UpnpFactory(requester)
48 device = await factory.async_create_device("http://igd:1234/device.xml")
49 notify_server = UpnpTestNotifyServer(
50 requester=requester,
51 source=("192.168.1.2", 8090),
52 )
53 event_handler = notify_server.event_handler
54 profile = IgdDevice(device, event_handler=event_handler)
55 assert profile
56
57
58 @pytest.mark.asyncio
59 async def test_get_total_bytes_received() -> None:
60 """Test getting total bytes received."""
61 responses = dict(RESPONSE_MAP)
62 responses[("POST", "http://igd:1234/WANCommonInterfaceConfig")] = HttpResponse(
63 200,
64 {},
65 read_file("igd/action_WANCIC_GetTotalBytesReceived.xml"),
66 )
67 requester = UpnpTestRequester(responses)
68 factory = UpnpFactory(requester)
69 device = await factory.async_create_device("http://igd:1234/device.xml")
70 notify_server = UpnpTestNotifyServer(
71 requester=requester,
72 source=("192.168.1.2", 8090),
73 )
74 event_handler = notify_server.event_handler
75 profile = IgdDevice(device, event_handler=event_handler)
76 total_bytes_received = await profile.async_get_total_bytes_received()
77 assert total_bytes_received == 1337
78
79
80 @pytest.mark.asyncio
81 async def test_get_total_packets_received_empty_response() -> None:
82 """Test getting total packets received with empty response, for broken (Draytek) device."""
83 responses = dict(RESPONSE_MAP)
84 responses[("POST", "http://igd:1234/WANCommonInterfaceConfig")] = HttpResponse(
85 200,
86 {},
87 read_file("igd/action_WANCIC_GetTotalPacketsReceived.xml"),
88 )
89 requester = UpnpTestRequester(responses)
90 factory = UpnpFactory(requester)
91 device = await factory.async_create_device("http://igd:1234/device.xml")
92 notify_server = UpnpTestNotifyServer(
93 requester=requester,
94 source=("192.168.1.2", 8090),
95 )
96 event_handler = notify_server.event_handler
97 profile = IgdDevice(device, event_handler=event_handler)
98 total_bytes_received = await profile.async_get_total_packets_received()
99 assert total_bytes_received is None
100
101
102 @pytest.mark.asyncio
103 async def test_get_status_info_invalid_uptime() -> None:
104 """Test getting status info with an invalid uptime response."""
105 responses = dict(RESPONSE_MAP)
106 responses[("POST", "http://igd:1234/WANIPConnection")] = HttpResponse(
107 200,
108 {},
109 read_file("igd/action_WANIPConnection_GetStatusInfoInvalidUptime.xml"),
110 )
111 requester = UpnpTestRequester(responses)
112 factory = UpnpFactory(requester)
113 device = await factory.async_create_device("http://igd:1234/device.xml")
114 notify_server = UpnpTestNotifyServer(
115 requester=requester,
116 source=("192.168.1.2", 8090),
117 )
118 event_handler = notify_server.event_handler
119 profile = IgdDevice(device, event_handler=event_handler)
120 status_info = await profile.async_get_status_info()
121 assert status_info is None
122
123
124 @pytest.mark.asyncio
125 async def test_negative_bytes_received_counter() -> None:
126 """
127 Test getting a negative total bytes received counter.
128
129 Some devices implement the counter as a signed integer (i4),
130 which can result in negative values.
131 """
132 responses = dict(RESPONSE_MAP)
133 responses[("POST", "http://igd:1234/WANCommonInterfaceConfig")] = HttpResponse(
134 200,
135 {},
136 read_file("igd/action_WANCIC_GetTotalBytesReceived_i4.xml"),
137 )
138 requester = UpnpTestRequester(responses)
139 factory = UpnpFactory(requester)
140 device = await factory.async_create_device("http://igd:1234/device.xml")
141 notify_server = UpnpTestNotifyServer(
142 requester=requester,
143 source=("192.168.1.2", 8090),
144 )
145 event_handler = notify_server.event_handler
146 profile = IgdDevice(device, event_handler=event_handler)
147 total_bytes_received = await profile.async_get_total_bytes_received()
148 assert total_bytes_received == 1615498126 # 2**31 + -531985522
149
150
151 @pytest.mark.asyncio
152 async def test_derived_rates_with_uint32_overflow() -> None:
153 """Test derived rates with and without uint32 overflow handling."""
154 requester = UpnpTestRequester(RESPONSE_MAP)
155 factory = UpnpFactory(requester)
156 device = await factory.async_create_device("http://igd:1234/device.xml")
157 notify_server = UpnpTestNotifyServer(
158 requester=requester,
159 source=("192.168.1.2", 8090),
160 )
161 event_handler = notify_server.event_handler
162
163 previous_timestamp = datetime(2000, 1, 1, 0, 0, 0)
164 initial_traffic_state = TrafficCounterState(
165 timestamp=previous_timestamp,
166 bytes_received=2**32 - 100,
167 bytes_sent=2**32 - 200,
168 packets_received=2**32 - 50,
169 packets_sent=2**32 - 1,
170 bytes_received_original=2**32 - 100,
171 bytes_sent_original=2**32 - 200,
172 packets_received_original=2**32 - 50,
173 packets_sent_original=2**32 - 1,
174 )
175 current_traffic_state = TrafficCounterState(
176 timestamp=previous_timestamp + timedelta(seconds=10),
177 bytes_received=50,
178 bytes_sent=25,
179 packets_received=10,
180 packets_sent=4,
181 bytes_received_original=50,
182 bytes_sent_original=25,
183 packets_received_original=10,
184 packets_sent_original=4,
185 )
186 profile = FixedCounterIgdDevice(
187 device,
188 event_handler,
189 initial_traffic_state=initial_traffic_state,
190 current_traffic_state=current_traffic_state,
191 )
192
193 state = await profile.async_get_traffic_and_status_data(
194 items={
195 IgdStateItem.KIBIBYTES_PER_SEC_RECEIVED,
196 IgdStateItem.KIBIBYTES_PER_SEC_SENT,
197 IgdStateItem.PACKETS_PER_SEC_RECEIVED,
198 IgdStateItem.PACKETS_PER_SEC_SENT,
199 IgdStateItem.KIBIBYTES_PER_SEC_RECEIVED_NO_ROLLOVER,
200 IgdStateItem.KIBIBYTES_PER_SEC_SENT_NO_ROLLOVER,
201 IgdStateItem.PACKETS_PER_SEC_RECEIVED_NO_ROLLOVER,
202 IgdStateItem.PACKETS_PER_SEC_SENT_NO_ROLLOVER,
203 },
204 force_poll=True,
205 )
206
207 assert state.kibibytes_per_sec_received_no_rollover is None
208 assert state.kibibytes_per_sec_sent_no_rollover is None
209 assert state.packets_per_sec_received_no_rollover is None
210 assert state.packets_per_sec_sent_no_rollover is None
211
212 elapsed_seconds = (current_traffic_state.timestamp - initial_traffic_state.timestamp).total_seconds()
213
214 assert state.kibibytes_per_sec_received == pytest.approx((150 / 1024) / elapsed_seconds)
215 assert state.kibibytes_per_sec_sent == pytest.approx((225 / 1024) / elapsed_seconds)
216 assert state.packets_per_sec_received == pytest.approx(60 / elapsed_seconds)
217 assert state.packets_per_sec_sent == pytest.approx(5 / elapsed_seconds)