| 1 |
"""Unit tests for ssdp.""" |
| 2 |
|
| 3 |
import asyncio |
| 4 |
from unittest.mock import ANY, AsyncMock, MagicMock |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from async_upnp_client.ssdp import ( |
| 9 |
SSDP_PORT, |
| 10 |
SsdpProtocol, |
| 11 |
build_ssdp_search_packet, |
| 12 |
decode_ssdp_packet, |
| 13 |
fix_ipv6_address_scope_id, |
| 14 |
get_ssdp_socket, |
| 15 |
is_ipv4_address, |
| 16 |
is_ipv6_address, |
| 17 |
is_valid_ssdp_packet, |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
def test_ssdp_search_packet() -> None: |
| 22 |
"""Test SSDP search packet generation.""" |
| 23 |
msg = build_ssdp_search_packet(("239.255.255.250", 1900), 4, "ssdp:all") |
| 24 |
assert ( |
| 25 |
msg |
| 26 |
== "M-SEARCH * HTTP/1.1\r\n" |
| 27 |
"HOST:239.255.255.250:1900\r\n" |
| 28 |
'MAN:"ssdp:discover"\r\n' |
| 29 |
"MX:4\r\n" |
| 30 |
"ST:ssdp:all\r\n" |
| 31 |
"\r\n".encode() |
| 32 |
) |
| 33 |
|
| 34 |
|
| 35 |
def test_ssdp_search_packet_v6() -> None: |
| 36 |
"""Test SSDP search packet generation.""" |
| 37 |
msg = build_ssdp_search_packet(("FF02::C", 1900, 0, 2), 4, "ssdp:all") |
| 38 |
assert ( |
| 39 |
msg |
| 40 |
== "M-SEARCH * HTTP/1.1\r\n" |
| 41 |
"HOST:[FF02::C%2]:1900\r\n" |
| 42 |
'MAN:"ssdp:discover"\r\n' |
| 43 |
"MX:4\r\n" |
| 44 |
"ST:ssdp:all\r\n" |
| 45 |
"\r\n".encode() |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
def test_is_valid_ssdp_packet() -> None: |
| 50 |
"""Test SSDP response validation.""" |
| 51 |
assert not is_valid_ssdp_packet(b"") |
| 52 |
|
| 53 |
msg = ( |
| 54 |
b"HTTP/1.1 200 OK\r\n" |
| 55 |
b"Cache-Control: max-age=1900\r\n" |
| 56 |
b"Location: http://192.168.1.1:80/RootDevice.xml\r\n" |
| 57 |
b"Server: UPnP/1.0 UPnP/1.0 UPnP-Device-Host/1.0\r\n" |
| 58 |
b"ST:urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1\r\n" |
| 59 |
b"USN: uuid:...::WANCommonInterfaceConfig:1\r\n" |
| 60 |
b"EXT:\r\n" |
| 61 |
b"\r\n" |
| 62 |
) |
| 63 |
assert is_valid_ssdp_packet(msg) |
| 64 |
|
| 65 |
|
| 66 |
def test_decode_ssdp_packet() -> None: |
| 67 |
"""Test SSDP response decoding.""" |
| 68 |
msg = ( |
| 69 |
b"HTTP/1.1 200 OK\r\n" |
| 70 |
b"Cache-Control: max-age=1900\r\n" |
| 71 |
b"Location: http://192.168.1.1:80/RootDevice.xml\r\n" |
| 72 |
b"Server: UPnP/1.0 UPnP/1.0 UPnP-Device-Host/1.0\r\n" |
| 73 |
b"ST:urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1\r\n" |
| 74 |
b"USN: uuid:...::WANCommonInterfaceConfig:1\r\n" |
| 75 |
b"EXT:\r\n" |
| 76 |
b"\r\n" |
| 77 |
) |
| 78 |
request_line, headers = decode_ssdp_packet(msg, ("local_addr", 1900), ("remote_addr", 12345)) |
| 79 |
|
| 80 |
assert request_line == "HTTP/1.1 200 OK" |
| 81 |
|
| 82 |
assert headers == { |
| 83 |
"cache-control": "max-age=1900", |
| 84 |
"location": "http://192.168.1.1:80/RootDevice.xml", |
| 85 |
"server": "UPnP/1.0 UPnP/1.0 UPnP-Device-Host/1.0", |
| 86 |
"st": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 87 |
"usn": "uuid:...::WANCommonInterfaceConfig:1", |
| 88 |
"ext": "", |
| 89 |
"_location_original": "http://192.168.1.1:80/RootDevice.xml", |
| 90 |
"_host": "remote_addr", |
| 91 |
"_port": 12345, |
| 92 |
"_local_addr": ("local_addr", 1900), |
| 93 |
"_remote_addr": ("remote_addr", 12345), |
| 94 |
"_udn": "uuid:...", |
| 95 |
"_timestamp": ANY, |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
def test_decode_ssdp_packet_missing_ending() -> None: |
| 100 |
"""Test SSDP response decoding with a missing end line.""" |
| 101 |
msg = ( |
| 102 |
b"HTTP/1.1 200 OK\r\n" |
| 103 |
b"CACHE-CONTROL: max-age = 1800\r\n" |
| 104 |
b"DATE:Sun, 25 Apr 2021 16:08:06 GMT\r\n" |
| 105 |
b"EXT:\r\n" |
| 106 |
b"LOCATION: http://192.168.107.148:8088/description\r\n" |
| 107 |
b"SERVER: Ubuntu/10.04 UPnP/1.1 Harmony/16.3\r\n" |
| 108 |
b"ST: urn:myharmony-com:device:harmony:1\r\n" |
| 109 |
b"USN: uuid:...::urn:myharmony-com:device:harmony:1\r\n" |
| 110 |
b"BOOTID.UPNP.ORG:1619366886\r\n" |
| 111 |
) |
| 112 |
request_line, headers = decode_ssdp_packet(msg, ("local_addr", 1900), ("remote_addr", 12345)) |
| 113 |
|
| 114 |
assert request_line == "HTTP/1.1 200 OK" |
| 115 |
|
| 116 |
assert headers == { |
| 117 |
"cache-control": "max-age = 1800", |
| 118 |
"date": "Sun, 25 Apr 2021 16:08:06 GMT", |
| 119 |
"location": "http://192.168.107.148:8088/description", |
| 120 |
"server": "Ubuntu/10.04 UPnP/1.1 Harmony/16.3", |
| 121 |
"st": "urn:myharmony-com:device:harmony:1", |
| 122 |
"usn": "uuid:...::urn:myharmony-com:device:harmony:1", |
| 123 |
"bootid.upnp.org": "1619366886", |
| 124 |
"ext": "", |
| 125 |
"_location_original": "http://192.168.107.148:8088/description", |
| 126 |
"_host": "remote_addr", |
| 127 |
"_port": 12345, |
| 128 |
"_local_addr": ("local_addr", 1900), |
| 129 |
"_remote_addr": ("remote_addr", 12345), |
| 130 |
"_udn": "uuid:...", |
| 131 |
"_timestamp": ANY, |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
def test_decode_ssdp_packet_duplicate_header() -> None: |
| 136 |
"""Test SSDP response decoding with a duplicate header.""" |
| 137 |
msg = b"HTTP/1.1 200 OK\r\nCACHE-CONTROL: max-age = 1800\r\nCACHE-CONTROL: max-age = 1800\r\n\r\n" |
| 138 |
_, headers = decode_ssdp_packet(msg, ("local_addr", 1900), ("remote_addr", 12345)) |
| 139 |
|
| 140 |
assert headers == { |
| 141 |
"cache-control": "max-age = 1800", |
| 142 |
"_host": "remote_addr", |
| 143 |
"_port": 12345, |
| 144 |
"_local_addr": ("local_addr", 1900), |
| 145 |
"_remote_addr": ("remote_addr", 12345), |
| 146 |
"_timestamp": ANY, |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
def test_decode_ssdp_packet_empty_location() -> None: |
| 151 |
"""Test SSDP response decoding with an empty location.""" |
| 152 |
msg = b"HTTP/1.1 200 OK\r\nLOCATION: \r\nCACHE-CONTROL: max-age = 1800\r\n\r\n" |
| 153 |
_, headers = decode_ssdp_packet(msg, ("local_addr", 1900), ("remote_addr", 12345)) |
| 154 |
|
| 155 |
assert headers == { |
| 156 |
"cache-control": "max-age = 1800", |
| 157 |
"location": "", |
| 158 |
"_host": "remote_addr", |
| 159 |
"_port": 12345, |
| 160 |
"_local_addr": ("local_addr", 1900), |
| 161 |
"_remote_addr": ("remote_addr", 12345), |
| 162 |
"_timestamp": ANY, |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
@pytest.mark.asyncio |
| 167 |
async def test_ssdp_protocol_handles_broken_headers() -> None: |
| 168 |
"""Test SsdpProtocol is able to handle broken headers.""" |
| 169 |
msg = b"HTTP/1.1 200 OK\r\nDEFUNCT\r\nCACHE-CONTROL: max-age = 1800\r\n\r\n" |
| 170 |
addr = ("addr", 123) |
| 171 |
loop = asyncio.get_event_loop() |
| 172 |
|
| 173 |
async_on_data_mock = AsyncMock() |
| 174 |
protocol = SsdpProtocol(loop, async_on_data=async_on_data_mock) |
| 175 |
protocol.transport = MagicMock() |
| 176 |
protocol.datagram_received(msg, addr) |
| 177 |
async_on_data_mock.assert_not_awaited() |
| 178 |
|
| 179 |
|
| 180 |
def test_decode_ssdp_packet_v6() -> None: |
| 181 |
"""Test SSDP response decoding.""" |
| 182 |
msg = ( |
| 183 |
b"HTTP/1.1 200 OK\r\n" |
| 184 |
b"Cache-Control: max-age=1900\r\n" |
| 185 |
b"Location: http://[fe80::2]:80/RootDevice.xml\r\n" |
| 186 |
b"Server: UPnP/1.0 UPnP/1.0 UPnP-Device-Host/1.0\r\n" |
| 187 |
b"ST:urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1\r\n" |
| 188 |
b"USN: uuid:...::WANCommonInterfaceConfig:1\r\n" |
| 189 |
b"EXT:\r\n" |
| 190 |
b"\r\n" |
| 191 |
) |
| 192 |
|
| 193 |
request_line, headers = decode_ssdp_packet(msg, ("FF02::C", 1900, 0, 3), ("fe80::1", 123, 0, 3)) |
| 194 |
|
| 195 |
assert request_line == "HTTP/1.1 200 OK" |
| 196 |
|
| 197 |
assert headers == { |
| 198 |
"cache-control": "max-age=1900", |
| 199 |
"location": "http://[fe80::2%3]:80/RootDevice.xml", |
| 200 |
"server": "UPnP/1.0 UPnP/1.0 UPnP-Device-Host/1.0", |
| 201 |
"st": "urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1", |
| 202 |
"usn": "uuid:...::WANCommonInterfaceConfig:1", |
| 203 |
"ext": "", |
| 204 |
"_location_original": "http://[fe80::2]:80/RootDevice.xml", |
| 205 |
"_host": "fe80::1%3", |
| 206 |
"_port": 123, |
| 207 |
"_local_addr": ("FF02::C", 1900, 0, 3), |
| 208 |
"_remote_addr": ("fe80::1", 123, 0, 3), |
| 209 |
"_udn": "uuid:...", |
| 210 |
"_timestamp": ANY, |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
def test_get_ssdp_socket() -> None: |
| 215 |
"""Test get_ssdp_socket accepts a port.""" |
| 216 |
|
| 217 |
_, source, target = get_ssdp_socket(("127.0.0.1", 0), ("127.0.0.1", SSDP_PORT)) |
| 218 |
assert source == ("127.0.0.1", 0) |
| 219 |
assert target == ("127.0.0.1", SSDP_PORT) |
| 220 |
|
| 221 |
|
| 222 |
_, source, target = get_ssdp_socket( |
| 223 |
("127.0.0.1", 0), |
| 224 |
("127.0.0.1", 1234), |
| 225 |
) |
| 226 |
assert source == ("127.0.0.1", 0) |
| 227 |
assert target == ("127.0.0.1", 1234) |
| 228 |
|
| 229 |
|
| 230 |
def test_microsoft_butchers_ssdp() -> None: |
| 231 |
"""Test parsing a `Microsoft Windows Peer Name Resolution Protocol` packet.""" |
| 232 |
msg = ( |
| 233 |
b"HTTP/1.1 200 OK\r\n" |
| 234 |
b"ST:urn:Microsoft Windows Peer Name Resolution Protocol: V4:IPV6:LinkLocal\r\n" |
| 235 |
b"USN:[fe80::aaaa:bbbb:cccc:dddd]:3540\r\n" |
| 236 |
b"Location:192.168.1.1\r\n" |
| 237 |
b"AL:[fe80::aaaa:bbbb:cccc:dddd]:3540\r\n" |
| 238 |
b'OPT:"http://schemas.upnp.org/upnp/1/0/"; ns=01\r\n' |
| 239 |
b"01-NLS:abcdef0123456789abcdef012345678\r\n" |
| 240 |
b"Cache-Control:max-age=14400\r\n" |
| 241 |
b"Server:Microsoft-Windows-NT/5.1 UPnP/1.0 UPnP-Device-Host/1.0\r\n" |
| 242 |
b"Ext:\r\n" |
| 243 |
) |
| 244 |
|
| 245 |
request_line, headers = decode_ssdp_packet(msg, ("239.255.255.250", 1900), ("192.168.1.1", 12345)) |
| 246 |
|
| 247 |
assert request_line == "HTTP/1.1 200 OK" |
| 248 |
assert headers == { |
| 249 |
"st": "urn:Microsoft Windows Peer Name Resolution Protocol: V4:IPV6:LinkLocal", |
| 250 |
"usn": "[fe80::aaaa:bbbb:cccc:dddd]:3540", |
| 251 |
"location": "192.168.1.1", |
| 252 |
"al": "[fe80::aaaa:bbbb:cccc:dddd]:3540", |
| 253 |
"opt": '"http://schemas.upnp.org/upnp/1/0/"; ns=01', |
| 254 |
"01-nls": "abcdef0123456789abcdef012345678", |
| 255 |
"cache-control": "max-age=14400", |
| 256 |
"server": "Microsoft-Windows-NT/5.1 UPnP/1.0 UPnP-Device-Host/1.0", |
| 257 |
"ext": "", |
| 258 |
"_location_original": "192.168.1.1", |
| 259 |
"_host": "192.168.1.1", |
| 260 |
"_port": 12345, |
| 261 |
"_local_addr": ("239.255.255.250", 1900), |
| 262 |
"_remote_addr": ("192.168.1.1", 12345), |
| 263 |
"_timestamp": ANY, |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
def test_is_ipv4_address() -> None: |
| 268 |
"""Test is_ipv4_address().""" |
| 269 |
assert is_ipv4_address(("192.168.1.1", 12345)) |
| 270 |
assert not is_ipv4_address(("fe80::1", 12345, 0, 6)) |
| 271 |
|
| 272 |
|
| 273 |
def test_is_ipv6_address() -> None: |
| 274 |
"""Test is_ipv6_address().""" |
| 275 |
assert is_ipv6_address(("fe80::1", 12345, 0, 6)) |
| 276 |
assert not is_ipv6_address(("192.168.1.1", 12345)) |
| 277 |
|
| 278 |
|
| 279 |
def test_fix_ipv6_address_scope_id() -> None: |
| 280 |
"""Test fix_ipv6_address_scope_id.""" |
| 281 |
assert fix_ipv6_address_scope_id(("fe80::1", 0, 0, 4)) == ("fe80::1", 0, 0, 4) |
| 282 |
assert fix_ipv6_address_scope_id(("fe80::1%4", 0, 0, 4)) == ("fe80::1", 0, 0, 4) |
| 283 |
assert fix_ipv6_address_scope_id(("fe80::1%4", 0, 0, 0)) == ("fe80::1", 0, 0, 4) |
| 284 |
assert fix_ipv6_address_scope_id(None) is None |
| 285 |
assert fix_ipv6_address_scope_id(("192.168.1.1", 0)) == ("192.168.1.1", 0) |