| 1 |
"""Unit tests for ssdp_listener.""" |
| 2 |
|
| 3 |
import asyncio |
| 4 |
from datetime import datetime, timedelta |
| 5 |
from typing import AsyncGenerator |
| 6 |
from unittest.mock import ANY, AsyncMock, Mock, patch |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from async_upnp_client.advertisement import SsdpAdvertisementListener |
| 11 |
from async_upnp_client.const import NotificationSubType, SsdpSource |
| 12 |
from async_upnp_client.search import SsdpSearchListener |
| 13 |
from async_upnp_client.ssdp_listener import ( |
| 14 |
SsdpDevice, |
| 15 |
SsdpListener, |
| 16 |
is_valid_location, |
| 17 |
same_headers_differ, |
| 18 |
) |
| 19 |
from async_upnp_client.utils import CaseInsensitiveDict |
| 20 |
|
| 21 |
from .common import ( |
| 22 |
ADVERTISEMENT_HEADERS_DEFAULT, |
| 23 |
ADVERTISEMENT_REQUEST_LINE, |
| 24 |
SEARCH_HEADERS_DEFAULT, |
| 25 |
SEARCH_REQUEST_LINE, |
| 26 |
) |
| 27 |
|
| 28 |
UDN = ADVERTISEMENT_HEADERS_DEFAULT["_udn"] |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture(autouse=True) |
| 32 |
async def mock_start_listeners() -> AsyncGenerator: |
| 33 |
"""Create listeners but don't call async_start().""" |
| 34 |
# pylint: disable=protected-access |
| 35 |
|
| 36 |
async def async_start(self: SsdpListener) -> None: |
| 37 |
self._advertisement_listener = SsdpAdvertisementListener( |
| 38 |
on_alive=self._on_alive, |
| 39 |
on_update=self._on_update, |
| 40 |
on_byebye=self._on_byebye, |
| 41 |
source=self.source, |
| 42 |
target=self.target, |
| 43 |
loop=self.loop, |
| 44 |
) |
| 45 |
# await self._advertisement_listener.async_start() |
| 46 |
|
| 47 |
self._search_listener = SsdpSearchListener( |
| 48 |
callback=self._on_search, |
| 49 |
loop=self.loop, |
| 50 |
source=self.source, |
| 51 |
target=self.target, |
| 52 |
timeout=self.search_timeout, |
| 53 |
) |
| 54 |
# await self._search_listener.async_start() |
| 55 |
|
| 56 |
with patch.object(SsdpListener, "async_start", new=async_start) as mock: |
| 57 |
yield mock |
| 58 |
|
| 59 |
|
| 60 |
async def see_advertisement(ssdp_listener: SsdpListener, request_line: str, headers: CaseInsensitiveDict) -> None: |
| 61 |
"""See advertisement.""" |
| 62 |
# pylint: disable=protected-access |
| 63 |
advertisement_listener = ssdp_listener._advertisement_listener |
| 64 |
assert advertisement_listener is not None |
| 65 |
advertisement_listener._on_data(request_line, headers) |
| 66 |
await asyncio.sleep(0) # Allow callback to run, if called. |
| 67 |
|
| 68 |
|
| 69 |
async def see_search(ssdp_listener: SsdpListener, request_line: str, headers: CaseInsensitiveDict) -> None: |
| 70 |
"""See search.""" |
| 71 |
# pylint: disable=protected-access |
| 72 |
search_listener = ssdp_listener._search_listener |
| 73 |
assert search_listener is not None |
| 74 |
search_listener._on_data(request_line, headers) |
| 75 |
await asyncio.sleep(0) # Allow callback to run, if called. |
| 76 |
|
| 77 |
|
| 78 |
@pytest.mark.parametrize( |
| 79 |
"location", |
| 80 |
[ |
| 81 |
"http://127.0.0.1/desc.xml", |
| 82 |
"http://127.0.0.2/desc.xml", # whole 127.0.0.0/8 is loopback, not just .1 |
| 83 |
"http://localhost/desc.xml", |
| 84 |
"http://LocalHost/desc.xml", |
| 85 |
"http://[::1]/desc.xml", |
| 86 |
"http://169.254.169.254/latest/", |
| 87 |
# Evasions of the old substring filter that still target loopback/link-local: |
| 88 |
"http://[::ffff:127.0.0.1]/desc.xml", # IPv4-mapped IPv6 loopback |
| 89 |
"http://[::ffff:7f00:1]/desc.xml", # IPv4-mapped IPv6 loopback (hex) |
| 90 |
"http://[email protected]/desc.xml", # credentials in authority |
| 91 |
"http://0.0.0.0/desc.xml", # unspecified address |
| 92 |
"ftp://192.168.1.1/desc.xml", # not http(s) |
| 93 |
# Obfuscated IPv4 encodings that resolve to loopback/link-local: |
| 94 |
"http://2130706433/desc.xml", # decimal 127.0.0.1 |
| 95 |
"http://0177.0.0.1/desc.xml", # octal 127.0.0.1 |
| 96 |
"http://0x7f.0.0.1/desc.xml", # hex 127.0.0.1 |
| 97 |
"http://127.1/desc.xml", # short form 127.0.0.1 |
| 98 |
"http://%31%32%37.0.0.1/desc.xml", # percent-encoded 127.0.0.1 |
| 99 |
"http://2852039166/desc.xml", # decimal 169.254.169.254 |
| 100 |
# Reserved/conventional loopback names and trailing-dot forms: |
| 101 |
"http://127.0.0.1./desc.xml", # trailing root-label dot |
| 102 |
"http://localhost./desc.xml", |
| 103 |
"http://localhost.localdomain/desc.xml", |
| 104 |
"http://localhost.localdomain./desc.xml", |
| 105 |
"http://evil.localhost/desc.xml", # RFC 6761 reserves *.localhost |
| 106 |
"http://foo.bar.localhost/desc.xml", |
| 107 |
# Malformed URLs must be rejected, not raise: |
| 108 |
"http://[::1/desc.xml", # unbalanced IPv6 bracket |
| 109 |
"http://[bad/desc.xml", |
| 110 |
], |
| 111 |
) |
| 112 |
def test_is_valid_location_rejects(location: str) -> None: |
| 113 |
"""Locations resolving to loopback/link-local/unspecified must be rejected.""" |
| 114 |
is_valid_location.cache_clear() |
| 115 |
assert is_valid_location(location) is False |
| 116 |
|
| 117 |
|
| 118 |
@pytest.mark.parametrize( |
| 119 |
"location", |
| 120 |
[ |
| 121 |
"http://192.168.1.1/desc.xml", # RFC1918 — normal LAN device |
| 122 |
"http://10.0.0.1/desc.xml", |
| 123 |
"http://172.16.0.1/desc.xml", |
| 124 |
"http://[fc00::1]/desc.xml", # IPv6 ULA — normal LAN device |
| 125 |
"http://[fd00::1]/desc.xml", |
| 126 |
"http://[fe80::1]/desc.xml", # IPv6 link-local — normal LAN device |
| 127 |
"http://dlna_dmr:1234/device.xml", # DNS name |
| 128 |
"http://my-device.local./desc.xml", # trailing-dot FQDN, not loopback |
| 129 |
"https://192.168.1.1:49152/desc.xml", |
| 130 |
], |
| 131 |
) |
| 132 |
def test_is_valid_location_accepts(location: str) -> None: |
| 133 |
"""Normal LAN locations (RFC1918, ULA, DNS names) must be accepted.""" |
| 134 |
is_valid_location.cache_clear() |
| 135 |
assert is_valid_location(location) is True |
| 136 |
|
| 137 |
|
| 138 |
@pytest.mark.asyncio |
| 139 |
async def test_see_advertisement_alive() -> None: |
| 140 |
"""Test seeing a device through an ssdp:alive-advertisement.""" |
| 141 |
# pylint: disable=protected-access |
| 142 |
async_callback = AsyncMock() |
| 143 |
listener = SsdpListener(async_callback=async_callback) |
| 144 |
await listener.async_start() |
| 145 |
|
| 146 |
# See device for the first time through alive-advertisement. |
| 147 |
async_callback.reset_mock() |
| 148 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 149 |
headers["NTS"] = "ssdp:alive" |
| 150 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 151 |
async_callback.assert_awaited_once_with( |
| 152 |
ANY, |
| 153 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 154 |
SsdpSource.ADVERTISEMENT_ALIVE, |
| 155 |
) |
| 156 |
assert UDN in listener.devices |
| 157 |
assert listener.devices[UDN].location is not None |
| 158 |
|
| 159 |
# See device for the second time through alive-advertisement, not triggering callback. |
| 160 |
async_callback.reset_mock() |
| 161 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 162 |
headers["NTS"] = "ssdp:alive" |
| 163 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 164 |
async_callback.assert_not_awaited() |
| 165 |
assert UDN in listener.devices |
| 166 |
assert listener.devices[UDN].location is not None |
| 167 |
|
| 168 |
await listener.async_stop() |
| 169 |
|
| 170 |
|
| 171 |
@pytest.mark.asyncio |
| 172 |
async def test_see_advertisement_byebye() -> None: |
| 173 |
"""Test seeing a device through an ssdp:byebye-advertisement.""" |
| 174 |
# pylint: disable=protected-access |
| 175 |
async_callback = AsyncMock() |
| 176 |
listener = SsdpListener(async_callback=async_callback) |
| 177 |
await listener.async_start() |
| 178 |
|
| 179 |
# See device for the first time through byebye-advertisement, not triggering callback. |
| 180 |
async_callback.reset_mock() |
| 181 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 182 |
headers["NTS"] = "ssdp:byebye" |
| 183 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 184 |
async_callback.assert_not_awaited() |
| 185 |
assert UDN not in listener.devices |
| 186 |
|
| 187 |
# See device for the first time through alive-advertisement, triggering callback. |
| 188 |
async_callback.reset_mock() |
| 189 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 190 |
headers["NTS"] = "ssdp:alive" |
| 191 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 192 |
async_callback.assert_awaited_once_with( |
| 193 |
ANY, |
| 194 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 195 |
SsdpSource.ADVERTISEMENT_ALIVE, |
| 196 |
) |
| 197 |
assert async_callback.await_args is not None |
| 198 |
device, dst, _ = async_callback.await_args.args |
| 199 |
assert device.combined_headers(dst)["NTS"] == NotificationSubType.SSDP_ALIVE |
| 200 |
assert UDN in listener.devices |
| 201 |
assert listener.devices[UDN].location is not None |
| 202 |
|
| 203 |
# See device for the second time through byebye-advertisement, triggering callback. |
| 204 |
async_callback.reset_mock() |
| 205 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 206 |
headers["NTS"] = "ssdp:byebye" |
| 207 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 208 |
async_callback.assert_awaited_once_with( |
| 209 |
ANY, |
| 210 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 211 |
SsdpSource.ADVERTISEMENT_BYEBYE, |
| 212 |
) |
| 213 |
assert async_callback.await_args is not None |
| 214 |
device, dst, _ = async_callback.await_args.args |
| 215 |
assert device.combined_headers(dst)["NTS"] == NotificationSubType.SSDP_BYEBYE |
| 216 |
assert UDN not in listener.devices |
| 217 |
|
| 218 |
await listener.async_stop() |
| 219 |
|
| 220 |
|
| 221 |
@pytest.mark.asyncio |
| 222 |
async def test_see_advertisement_update() -> None: |
| 223 |
"""Test seeing a device through a ssdp:update-advertisement.""" |
| 224 |
# pylint: disable=protected-access |
| 225 |
async_callback = AsyncMock() |
| 226 |
listener = SsdpListener(async_callback=async_callback) |
| 227 |
await listener.async_start() |
| 228 |
|
| 229 |
# See device for the first time through alive-advertisement, triggering callback. |
| 230 |
async_callback.reset_mock() |
| 231 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 232 |
headers["NTS"] = "ssdp:alive" |
| 233 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 234 |
async_callback.assert_awaited_once_with( |
| 235 |
ANY, |
| 236 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 237 |
SsdpSource.ADVERTISEMENT_ALIVE, |
| 238 |
) |
| 239 |
assert UDN in listener.devices |
| 240 |
assert listener.devices[UDN].location is not None |
| 241 |
|
| 242 |
# See device for the second time through update-advertisement, triggering callback. |
| 243 |
async_callback.reset_mock() |
| 244 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 245 |
headers["NTS"] = "ssdp:update" |
| 246 |
headers["BOOTID.UPNP.ORG"] = "2" |
| 247 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 248 |
async_callback.assert_awaited_once_with( |
| 249 |
ANY, |
| 250 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 251 |
SsdpSource.ADVERTISEMENT_UPDATE, |
| 252 |
) |
| 253 |
assert UDN in listener.devices |
| 254 |
assert listener.devices[UDN].location is not None |
| 255 |
|
| 256 |
await listener.async_stop() |
| 257 |
|
| 258 |
|
| 259 |
@pytest.mark.asyncio |
| 260 |
async def test_see_search() -> None: |
| 261 |
"""Test seeing a device through an search.""" |
| 262 |
# pylint: disable=protected-access |
| 263 |
async_callback = AsyncMock() |
| 264 |
listener = SsdpListener(async_callback=async_callback) |
| 265 |
await listener.async_start() |
| 266 |
|
| 267 |
# See device for the first time through search. |
| 268 |
async_callback.reset_mock() |
| 269 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 270 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 271 |
async_callback.assert_awaited_once_with( |
| 272 |
ANY, |
| 273 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 274 |
SsdpSource.SEARCH_CHANGED, |
| 275 |
) |
| 276 |
assert UDN in listener.devices |
| 277 |
assert listener.devices[UDN].location is not None |
| 278 |
|
| 279 |
# See same device again through search, not triggering a change. |
| 280 |
async_callback.reset_mock() |
| 281 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 282 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 283 |
async_callback.assert_awaited_once_with( |
| 284 |
ANY, |
| 285 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 286 |
SsdpSource.SEARCH_ALIVE, |
| 287 |
) |
| 288 |
assert UDN in listener.devices |
| 289 |
assert listener.devices[UDN].location is not None |
| 290 |
|
| 291 |
await listener.async_stop() |
| 292 |
|
| 293 |
|
| 294 |
@pytest.mark.asyncio |
| 295 |
async def test_see_search_sync() -> None: |
| 296 |
"""Test seeing a device through an search.""" |
| 297 |
# pylint: disable=protected-access |
| 298 |
callback = Mock() |
| 299 |
listener = SsdpListener(callback=callback) |
| 300 |
await listener.async_start() |
| 301 |
|
| 302 |
# See device for the first time through search. |
| 303 |
callback.reset_mock() |
| 304 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 305 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 306 |
callback.assert_called_with( |
| 307 |
ANY, |
| 308 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 309 |
SsdpSource.SEARCH_CHANGED, |
| 310 |
) |
| 311 |
assert UDN in listener.devices |
| 312 |
assert listener.devices[UDN].location is not None |
| 313 |
|
| 314 |
# See same device again through search, not triggering a change. |
| 315 |
callback.reset_mock() |
| 316 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 317 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 318 |
callback.assert_called_with( |
| 319 |
ANY, |
| 320 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 321 |
SsdpSource.SEARCH_ALIVE, |
| 322 |
) |
| 323 |
assert UDN in listener.devices |
| 324 |
assert listener.devices[UDN].location is not None |
| 325 |
|
| 326 |
await listener.async_stop() |
| 327 |
|
| 328 |
|
| 329 |
@pytest.mark.asyncio |
| 330 |
async def test_see_search_then_alive() -> None: |
| 331 |
"""Test seeing a device through a search, then a ssdp:alive-advertisement.""" |
| 332 |
# pylint: disable=protected-access |
| 333 |
async_callback = AsyncMock() |
| 334 |
listener = SsdpListener(async_callback=async_callback) |
| 335 |
await listener.async_start() |
| 336 |
|
| 337 |
# See device for the first time through search. |
| 338 |
async_callback.reset_mock() |
| 339 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 340 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 341 |
async_callback.assert_awaited_once_with( |
| 342 |
ANY, |
| 343 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 344 |
SsdpSource.SEARCH_CHANGED, |
| 345 |
) |
| 346 |
assert UDN in listener.devices |
| 347 |
assert listener.devices[UDN].location is not None |
| 348 |
|
| 349 |
# See device for the second time through alive-advertisement, not triggering callback. |
| 350 |
async_callback.reset_mock() |
| 351 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 352 |
headers["NTS"] = "ssdp:alive" |
| 353 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 354 |
async_callback.assert_not_awaited() |
| 355 |
assert UDN in listener.devices |
| 356 |
assert listener.devices[UDN].location is not None |
| 357 |
|
| 358 |
await listener.async_stop() |
| 359 |
|
| 360 |
|
| 361 |
@pytest.mark.asyncio |
| 362 |
async def test_see_search_then_update() -> None: |
| 363 |
"""Test seeing a device through a search, then a ssdp:update-advertisement.""" |
| 364 |
# pylint: disable=protected-access |
| 365 |
async_callback = AsyncMock() |
| 366 |
listener = SsdpListener(async_callback=async_callback) |
| 367 |
await listener.async_start() |
| 368 |
|
| 369 |
# See device for the first time through search. |
| 370 |
async_callback.reset_mock() |
| 371 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 372 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 373 |
async_callback.assert_awaited_once_with( |
| 374 |
ANY, |
| 375 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 376 |
SsdpSource.SEARCH_CHANGED, |
| 377 |
) |
| 378 |
assert UDN in listener.devices |
| 379 |
assert listener.devices[UDN].location is not None |
| 380 |
|
| 381 |
# See device for the second time through update-advertisement, triggering callback. |
| 382 |
async_callback.reset_mock() |
| 383 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 384 |
headers["NTS"] = "ssdp:update" |
| 385 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 386 |
async_callback.assert_awaited_once_with( |
| 387 |
ANY, |
| 388 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 389 |
SsdpSource.ADVERTISEMENT_UPDATE, |
| 390 |
) |
| 391 |
assert UDN in listener.devices |
| 392 |
assert listener.devices[UDN].location is not None |
| 393 |
|
| 394 |
await listener.async_stop() |
| 395 |
|
| 396 |
|
| 397 |
@pytest.mark.asyncio |
| 398 |
async def test_see_search_then_byebye() -> None: |
| 399 |
"""Test seeing a device through a search, then a ssdp:byebye-advertisement.""" |
| 400 |
# pylint: disable=protected-access |
| 401 |
async_callback = AsyncMock() |
| 402 |
listener = SsdpListener(async_callback=async_callback) |
| 403 |
await listener.async_start() |
| 404 |
|
| 405 |
# See device for the first time through search. |
| 406 |
async_callback.reset_mock() |
| 407 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 408 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 409 |
async_callback.assert_awaited_once_with( |
| 410 |
ANY, |
| 411 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 412 |
SsdpSource.SEARCH_CHANGED, |
| 413 |
) |
| 414 |
assert UDN in listener.devices |
| 415 |
assert listener.devices[UDN].location is not None |
| 416 |
|
| 417 |
# See device for the second time through byebye-advertisement, |
| 418 |
# triggering byebye-callback and device removed. |
| 419 |
async_callback.reset_mock() |
| 420 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 421 |
headers["NTS"] = "ssdp:byebye" |
| 422 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 423 |
async_callback.assert_awaited_once_with( |
| 424 |
ANY, |
| 425 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 426 |
SsdpSource.ADVERTISEMENT_BYEBYE, |
| 427 |
) |
| 428 |
assert UDN not in listener.devices |
| 429 |
|
| 430 |
await listener.async_stop() |
| 431 |
|
| 432 |
|
| 433 |
@pytest.mark.asyncio |
| 434 |
async def test_see_search_then_byebye_then_alive() -> None: |
| 435 |
"""Test seeing a device by search, then ssdp:byebye, then ssdp:alive.""" |
| 436 |
# pylint: disable=protected-access |
| 437 |
async_callback = AsyncMock() |
| 438 |
listener = SsdpListener(async_callback=async_callback) |
| 439 |
await listener.async_start() |
| 440 |
|
| 441 |
# See device for the first time through search. |
| 442 |
async_callback.reset_mock() |
| 443 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 444 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 445 |
async_callback.assert_awaited_once_with( |
| 446 |
ANY, |
| 447 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 448 |
SsdpSource.SEARCH_CHANGED, |
| 449 |
) |
| 450 |
assert UDN in listener.devices |
| 451 |
assert listener.devices[UDN].location is not None |
| 452 |
|
| 453 |
# See device for the second time through byebye-advertisement, |
| 454 |
# triggering byebye-callback and device removed. |
| 455 |
async_callback.reset_mock() |
| 456 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 457 |
headers["NTS"] = "ssdp:byebye" |
| 458 |
headers["LOCATION"] = "" |
| 459 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 460 |
async_callback.assert_awaited_once_with( |
| 461 |
ANY, |
| 462 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 463 |
SsdpSource.ADVERTISEMENT_BYEBYE, |
| 464 |
) |
| 465 |
assert UDN not in listener.devices |
| 466 |
|
| 467 |
# See device for the second time through alive-advertisement, not triggering callback. |
| 468 |
async_callback.reset_mock() |
| 469 |
headers = CaseInsensitiveDict(ADVERTISEMENT_HEADERS_DEFAULT) |
| 470 |
headers["NTS"] = "ssdp:alive" |
| 471 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 472 |
async_callback.assert_awaited_once_with( |
| 473 |
ANY, |
| 474 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 475 |
SsdpSource.ADVERTISEMENT_ALIVE, |
| 476 |
) |
| 477 |
assert UDN in listener.devices |
| 478 |
assert listener.devices[UDN].location is not None |
| 479 |
|
| 480 |
await listener.async_stop() |
| 481 |
|
| 482 |
|
| 483 |
@pytest.mark.asyncio |
| 484 |
async def test_purge_devices() -> None: |
| 485 |
"""Test if a device is purged when it times out given the value of the CACHE-CONTROL header.""" |
| 486 |
# pylint: disable=protected-access |
| 487 |
async_callback = AsyncMock() |
| 488 |
listener = SsdpListener(async_callback=async_callback) |
| 489 |
await listener.async_start() |
| 490 |
|
| 491 |
# See device through search. |
| 492 |
async_callback.reset_mock() |
| 493 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 494 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 495 |
async_callback.assert_awaited_once_with( |
| 496 |
ANY, |
| 497 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 498 |
SsdpSource.SEARCH_CHANGED, |
| 499 |
) |
| 500 |
assert UDN in listener.devices |
| 501 |
assert listener.devices[UDN].location is not None |
| 502 |
|
| 503 |
# "Wait" a bit... and purge devices. |
| 504 |
override_now = headers["_timestamp"] + timedelta(hours=1) |
| 505 |
listener._device_tracker.purge_devices(override_now) |
| 506 |
assert UDN not in listener.devices |
| 507 |
|
| 508 |
await listener.async_stop() |
| 509 |
|
| 510 |
|
| 511 |
@pytest.mark.asyncio |
| 512 |
async def test_purge_devices_2() -> None: |
| 513 |
"""Test if a device is purged when it times out, part 2.""" |
| 514 |
# pylint: disable=protected-access |
| 515 |
async_callback = AsyncMock() |
| 516 |
listener = SsdpListener(async_callback=async_callback) |
| 517 |
await listener.async_start() |
| 518 |
|
| 519 |
# See device through search. |
| 520 |
async_callback.reset_mock() |
| 521 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 522 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 523 |
async_callback.assert_awaited_once_with( |
| 524 |
ANY, |
| 525 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 526 |
SsdpSource.SEARCH_CHANGED, |
| 527 |
) |
| 528 |
assert UDN in listener.devices |
| 529 |
assert listener.devices[UDN].location is not None |
| 530 |
|
| 531 |
# See anotherdevice through search. |
| 532 |
async_callback.reset_mock() |
| 533 |
udn2 = "uuid:device_2" |
| 534 |
new_timestamp = SEARCH_HEADERS_DEFAULT["_timestamp"] + timedelta(hours=1) |
| 535 |
device_2_headers = CaseInsensitiveDict( |
| 536 |
{ |
| 537 |
**SEARCH_HEADERS_DEFAULT, |
| 538 |
"USN": udn2 + "::urn:schemas-upnp-org:service:WANCommonInterfaceConfig:2", |
| 539 |
"ST": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:2", |
| 540 |
"_udn": udn2, |
| 541 |
"_timestamp": new_timestamp, |
| 542 |
} |
| 543 |
) |
| 544 |
await see_search(listener, SEARCH_REQUEST_LINE, device_2_headers) |
| 545 |
async_callback.assert_awaited_once_with( |
| 546 |
ANY, |
| 547 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:2", |
| 548 |
SsdpSource.SEARCH_CHANGED, |
| 549 |
) |
| 550 |
assert UDN not in listener.devices |
| 551 |
assert udn2 in listener.devices |
| 552 |
assert listener.devices[udn2].location is not None |
| 553 |
|
| 554 |
await listener.async_stop() |
| 555 |
|
| 556 |
|
| 557 |
def test_same_headers_differ_profile() -> None: |
| 558 |
"""Test same_headers_differ.""" |
| 559 |
current_headers = CaseInsensitiveDict( |
| 560 |
{ |
| 561 |
"Cache-Control": "max-age=1900", |
| 562 |
"location": "http://192.168.1.1:80/RootDevice.xml", |
| 563 |
"Server": "UPnP/1.0 UPnP/1.0 UPnP-Device-Host/1.0", |
| 564 |
"ST": "urn:schemas-upnp-org:device:WANDevice:1", |
| 565 |
"USN": "uuid:upnp-WANDevice-1_0-123456789abc::urn:schemas-upnp-org:device:WANDevice:1", |
| 566 |
"EXT": "", |
| 567 |
"_location_original": "http://192.168.1.1:80/RootDevice.xml", |
| 568 |
"_timestamp": datetime.now(), |
| 569 |
"_host": "192.168.1.1", |
| 570 |
"_port": "1900", |
| 571 |
"_udn": "uuid:upnp-WANDevice-1_0-123456789abc", |
| 572 |
"_source": SsdpSource.SEARCH, |
| 573 |
} |
| 574 |
) |
| 575 |
new_headers = CaseInsensitiveDict( |
| 576 |
{ |
| 577 |
"Cache-Control": "max-age=1900", |
| 578 |
"location": "http://192.168.1.1:80/RootDevice.xml", |
| 579 |
"Server": "UPnP/1.0 UPnP/1.0 UPnP-Device-Host/1.0 abc", |
| 580 |
"Date": "Sat, 11 Sep 2021 12:00:00 GMT", |
| 581 |
"ST": "urn:schemas-upnp-org:device:WANDevice:1", |
| 582 |
"USN": "uuid:upnp-WANDevice-1_0-123456789abc::urn:schemas-upnp-org:device:WANDevice:1", |
| 583 |
"EXT": "", |
| 584 |
"_location_original": "http://192.168.1.1:80/RootDevice.xml", |
| 585 |
"_timestamp": datetime.now(), |
| 586 |
"_host": "192.168.1.1", |
| 587 |
"_port": "1900", |
| 588 |
"_udn": "uuid:upnp-WANDevice-1_0-123456789abc", |
| 589 |
"_source": SsdpSource.SEARCH, |
| 590 |
} |
| 591 |
) |
| 592 |
for _ in range(0, 10000): |
| 593 |
assert not same_headers_differ(current_headers, new_headers) |
| 594 |
|
| 595 |
|
| 596 |
@pytest.mark.asyncio |
| 597 |
async def test_see_search_invalid_usn() -> None: |
| 598 |
"""Test invalid USN is ignored.""" |
| 599 |
# pylint: disable=protected-access |
| 600 |
async_callback = AsyncMock() |
| 601 |
listener = SsdpListener(async_callback=async_callback) |
| 602 |
await listener.async_start() |
| 603 |
advertisement_listener = listener._advertisement_listener |
| 604 |
assert advertisement_listener is not None |
| 605 |
|
| 606 |
# See device for the first time through alive-advertisement. |
| 607 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 608 |
headers["ST"] = "urn:Microsoft Windows Peer Name Resolution Protocol: V4:IPV6:LinkLocal" |
| 609 |
headers["USN"] = "[fe80::aaaa:bbbb:cccc:dddd]:3540" |
| 610 |
del headers["_udn"] |
| 611 |
advertisement_listener._on_data(SEARCH_REQUEST_LINE, headers) |
| 612 |
async_callback.assert_not_awaited() |
| 613 |
|
| 614 |
await listener.async_stop() |
| 615 |
|
| 616 |
|
| 617 |
@pytest.mark.asyncio |
| 618 |
async def test_see_search_invalid_location() -> None: |
| 619 |
"""Test headers with invalid location is ignored.""" |
| 620 |
# pylint: disable=protected-access |
| 621 |
async_callback = AsyncMock() |
| 622 |
listener = SsdpListener(async_callback=async_callback) |
| 623 |
await listener.async_start() |
| 624 |
advertisement_listener = listener._advertisement_listener |
| 625 |
assert advertisement_listener is not None |
| 626 |
|
| 627 |
# See device for the first time through alive-advertisement. |
| 628 |
async_callback.reset_mock() |
| 629 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 630 |
headers["location"] = "192.168.1.1" |
| 631 |
advertisement_listener._on_data(SEARCH_REQUEST_LINE, headers) |
| 632 |
async_callback.assert_not_awaited() |
| 633 |
|
| 634 |
await listener.async_stop() |
| 635 |
|
| 636 |
|
| 637 |
@pytest.mark.asyncio |
| 638 |
@pytest.mark.parametrize( |
| 639 |
"location", |
| 640 |
[ |
| 641 |
"http://127.0.0.1:1234/device.xml", |
| 642 |
"http://127.0.0.20:1234/device.xml", |
| 643 |
"http://[::1]:1234/device.xml", |
| 644 |
"http://169.254.12.1:1234/device.xml", |
| 645 |
"http://localhost:1234/device.xml", |
| 646 |
], |
| 647 |
) |
| 648 |
async def test_see_search_localhost_location(location: str) -> None: |
| 649 |
"""Test localhost location (127.0.0.1/[::1]) is ignored.""" |
| 650 |
# pylint: disable=protected-access |
| 651 |
async_callback = AsyncMock() |
| 652 |
listener = SsdpListener(async_callback=async_callback) |
| 653 |
await listener.async_start() |
| 654 |
advertisement_listener = listener._advertisement_listener |
| 655 |
assert advertisement_listener is not None |
| 656 |
|
| 657 |
# See device for the first time through alive-advertisement. |
| 658 |
async_callback.reset_mock() |
| 659 |
headers = CaseInsensitiveDict(SEARCH_HEADERS_DEFAULT) |
| 660 |
headers["location"] = location |
| 661 |
advertisement_listener._on_data(SEARCH_REQUEST_LINE, headers) |
| 662 |
async_callback.assert_not_awaited() |
| 663 |
|
| 664 |
await listener.async_stop() |
| 665 |
|
| 666 |
|
| 667 |
@pytest.mark.asyncio |
| 668 |
async def test_combined_headers() -> None: |
| 669 |
"""Test combined headers.""" |
| 670 |
# pylint: disable=protected-access |
| 671 |
async_callback = AsyncMock() |
| 672 |
listener = SsdpListener(async_callback=async_callback) |
| 673 |
await listener.async_start() |
| 674 |
|
| 675 |
# See device for the first time through search. |
| 676 |
async_callback.reset_mock() |
| 677 |
headers = CaseInsensitiveDict({**SEARCH_HEADERS_DEFAULT, "booTID.UPNP.ORG": "0", "Original": "2"}) |
| 678 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 679 |
async_callback.assert_awaited_once_with( |
| 680 |
ANY, |
| 681 |
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 682 |
SsdpSource.SEARCH_CHANGED, |
| 683 |
) |
| 684 |
assert async_callback.await_args is not None |
| 685 |
device, dst, _ = async_callback.await_args.args |
| 686 |
assert UDN in listener.devices |
| 687 |
assert listener.devices[UDN].location is not None |
| 688 |
|
| 689 |
# See device for the second time through alive-advertisement, not triggering callback. |
| 690 |
async_callback.reset_mock() |
| 691 |
headers = CaseInsensitiveDict({**ADVERTISEMENT_HEADERS_DEFAULT, "BooTID.UPNP.ORG": "2"}) |
| 692 |
headers["NTS"] = "ssdp:alive" |
| 693 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 694 |
|
| 695 |
assert isinstance(device, SsdpDevice) |
| 696 |
combined = device.combined_headers(dst) |
| 697 |
assert isinstance(combined, CaseInsensitiveDict) |
| 698 |
result = {k.lower(): str(v) for k, v in combined.as_dict().items()} |
| 699 |
del result["_timestamp"] |
| 700 |
assert result == { |
| 701 |
"_host": "192.168.1.1", |
| 702 |
"_port": "1900", |
| 703 |
"_udn": "uuid:...", |
| 704 |
"bootid.upnp.org": "2", |
| 705 |
"cache-control": "max-age=1800", |
| 706 |
"date": "Fri, 1 Jan 2021 12:00:00 GMT", |
| 707 |
"location": "http://192.168.1.1:80/RootDevice.xml", |
| 708 |
"nt": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 709 |
"nts": NotificationSubType.SSDP_ALIVE, |
| 710 |
"original": "2", |
| 711 |
"server": "Linux/2.0 UPnP/1.0 async_upnp_client/0.1", |
| 712 |
"st": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 713 |
"usn": "uuid:...::urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 714 |
} |
| 715 |
assert combined["original"] == "2" |
| 716 |
assert combined["bootid.upnp.org"] == "2" |
| 717 |
assert "_source" not in combined |
| 718 |
|
| 719 |
headers = CaseInsensitiveDict( |
| 720 |
{ |
| 721 |
**ADVERTISEMENT_HEADERS_DEFAULT, |
| 722 |
"BooTID.UPNP.ORG": "2", |
| 723 |
"st": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:2", |
| 724 |
} |
| 725 |
) |
| 726 |
headers["NTS"] = "ssdp:alive" |
| 727 |
await see_advertisement(listener, ADVERTISEMENT_REQUEST_LINE, headers) |
| 728 |
combined = device.combined_headers(dst) |
| 729 |
assert combined["st"] == "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:2" |
| 730 |
await listener.async_stop() |
| 731 |
|
| 732 |
|
| 733 |
@pytest.mark.asyncio |
| 734 |
async def test_see_search_device_ipv4_and_ipv6() -> None: |
| 735 |
"""Test seeing the same device via IPv4, then via IPv6.""" |
| 736 |
# pylint: disable=protected-access |
| 737 |
async_callback = AsyncMock() |
| 738 |
listener = SsdpListener(async_callback=async_callback) |
| 739 |
await listener.async_start() |
| 740 |
|
| 741 |
# See device via IPv4, callback should be called. |
| 742 |
async_callback.reset_mock() |
| 743 |
location_ipv4 = "http://192.168.1.1:80/RootDevice.xml" |
| 744 |
headers = CaseInsensitiveDict( |
| 745 |
{ |
| 746 |
**SEARCH_HEADERS_DEFAULT, |
| 747 |
"LOCATION": location_ipv4, |
| 748 |
} |
| 749 |
) |
| 750 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 751 |
async_callback.assert_awaited_once_with(ANY, SEARCH_HEADERS_DEFAULT["ST"], SsdpSource.SEARCH_CHANGED) |
| 752 |
|
| 753 |
# See device via IPv6, callback should be called with SsdpSource.SEARCH_ALIVE, |
| 754 |
# not SEARCH_UPDATE. |
| 755 |
async_callback.reset_mock() |
| 756 |
location_ipv6 = "http://[fe80::1]:80/RootDevice.xml" |
| 757 |
headers = CaseInsensitiveDict( |
| 758 |
{ |
| 759 |
**SEARCH_HEADERS_DEFAULT, |
| 760 |
"LOCATION": location_ipv6, |
| 761 |
} |
| 762 |
) |
| 763 |
await see_search(listener, SEARCH_REQUEST_LINE, headers) |
| 764 |
async_callback.assert_awaited_once_with(ANY, SEARCH_HEADERS_DEFAULT["ST"], SsdpSource.SEARCH_ALIVE) |
| 765 |
|
| 766 |
assert listener.devices[SEARCH_HEADERS_DEFAULT["_udn"]].locations |