2020-05-20 19:17:33 +00:00
|
|
|
# type: ignore
|
2024-06-03 18:06:54 +00:00
|
|
|
# ruff: noqa: S106
|
|
|
|
|
2024-01-22 17:25:23 +00:00
|
|
|
import asyncio
|
2024-10-16 14:28:27 +00:00
|
|
|
import base64
|
|
|
|
import json
|
2024-07-17 17:57:09 +00:00
|
|
|
import logging
|
2023-08-29 13:04:28 +00:00
|
|
|
import re
|
2023-11-07 01:15:41 +00:00
|
|
|
import socket
|
2024-11-18 18:46:36 +00:00
|
|
|
from asyncio import timeout as asyncio_timeout
|
2024-01-22 17:25:23 +00:00
|
|
|
from unittest.mock import MagicMock
|
2021-09-24 15:18:11 +00:00
|
|
|
|
2024-01-18 17:32:26 +00:00
|
|
|
import aiohttp
|
2020-05-20 19:17:33 +00:00
|
|
|
import pytest # type: ignore # https://github.com/pytest-dev/pytest/issues/3342
|
2024-10-16 14:28:27 +00:00
|
|
|
from cryptography.hazmat.primitives import hashes
|
|
|
|
from cryptography.hazmat.primitives.asymmetric import padding as asymmetric_padding
|
2020-05-20 19:17:33 +00:00
|
|
|
|
2023-11-21 20:58:41 +00:00
|
|
|
from kasa import (
|
2023-12-29 19:17:15 +00:00
|
|
|
Credentials,
|
2024-02-04 15:20:08 +00:00
|
|
|
Device,
|
2023-11-21 20:58:41 +00:00
|
|
|
DeviceType,
|
|
|
|
Discover,
|
2024-10-22 13:33:46 +00:00
|
|
|
IotProtocol,
|
2024-02-21 15:52:55 +00:00
|
|
|
KasaException,
|
2023-11-21 20:58:41 +00:00
|
|
|
)
|
2024-10-22 13:33:46 +00:00
|
|
|
from kasa.device_factory import (
|
|
|
|
get_device_class_from_family,
|
|
|
|
get_device_class_from_sys_info,
|
|
|
|
get_protocol,
|
|
|
|
)
|
2023-12-29 19:17:15 +00:00
|
|
|
from kasa.deviceconfig import (
|
|
|
|
DeviceConfig,
|
2024-06-03 18:06:54 +00:00
|
|
|
DeviceConnectionParameters,
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
2024-10-16 14:28:27 +00:00
|
|
|
from kasa.discover import (
|
|
|
|
DiscoveryResult,
|
|
|
|
_AesDiscoveryQuery,
|
|
|
|
_DiscoverProtocol,
|
|
|
|
json_dumps,
|
|
|
|
)
|
2024-02-21 15:52:55 +00:00
|
|
|
from kasa.exceptions import AuthenticationError, UnsupportedDeviceError
|
2024-11-18 14:53:11 +00:00
|
|
|
from kasa.iot import IotDevice, IotPlug
|
2024-11-12 13:40:44 +00:00
|
|
|
from kasa.transports.aestransport import AesEncyptionSession
|
|
|
|
from kasa.transports.xortransport import XorEncryption, XorTransport
|
2020-05-20 19:17:33 +00:00
|
|
|
|
2024-01-29 17:14:30 +00:00
|
|
|
from .conftest import (
|
|
|
|
bulb_iot,
|
2024-05-13 16:34:44 +00:00
|
|
|
dimmer_iot,
|
|
|
|
lightstrip_iot,
|
2024-01-29 17:14:30 +00:00
|
|
|
new_discovery,
|
2024-03-01 18:32:45 +00:00
|
|
|
plug_iot,
|
2024-01-29 17:14:30 +00:00
|
|
|
strip_iot,
|
2024-03-01 18:32:45 +00:00
|
|
|
wallswitch_iot,
|
2024-01-29 17:14:30 +00:00
|
|
|
)
|
2020-05-20 19:17:33 +00:00
|
|
|
|
2024-11-11 17:41:31 +00:00
|
|
|
# A physical device has to respond to discovery for the tests to work.
|
|
|
|
pytestmark = [pytest.mark.requires_dummy]
|
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
UNSUPPORTED = {
|
|
|
|
"result": {
|
|
|
|
"device_id": "xx",
|
|
|
|
"owner": "xx",
|
|
|
|
"device_type": "SMART.TAPOXMASTREE",
|
|
|
|
"device_model": "P110(EU)",
|
|
|
|
"ip": "127.0.0.1",
|
|
|
|
"mac": "48-22xxx",
|
|
|
|
"is_support_iot_cloud": True,
|
|
|
|
"obd_src": "tplink",
|
|
|
|
"factory_default": False,
|
|
|
|
"mgt_encrypt_schm": {
|
|
|
|
"is_support_https": False,
|
|
|
|
"encrypt_type": "AES",
|
|
|
|
"http_port": 80,
|
|
|
|
"lv": 2,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"error_code": 0,
|
|
|
|
}
|
|
|
|
|
2020-05-20 19:17:33 +00:00
|
|
|
|
2024-03-01 18:32:45 +00:00
|
|
|
@wallswitch_iot
|
|
|
|
async def test_type_detection_switch(dev: Device):
|
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2024-11-13 16:10:06 +00:00
|
|
|
with pytest.deprecated_call(match="use device_type property instead"):
|
|
|
|
assert d.is_wallswitch
|
|
|
|
assert d.device_type is DeviceType.WallSwitch
|
2024-03-01 18:32:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
@plug_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_type_detection_plug(dev: Device):
|
2021-09-19 21:45:48 +00:00
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2020-05-20 19:17:33 +00:00
|
|
|
assert d.device_type == DeviceType.Plug
|
|
|
|
|
|
|
|
|
2023-12-05 19:07:10 +00:00
|
|
|
@bulb_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_type_detection_bulb(dev: Device):
|
2021-09-19 21:45:48 +00:00
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2020-07-19 20:32:17 +00:00
|
|
|
# TODO: light_strip is a special case for now to force bulb tests on it
|
2024-11-13 16:10:06 +00:00
|
|
|
|
|
|
|
if d.device_type is not DeviceType.LightStrip:
|
2020-07-19 20:32:17 +00:00
|
|
|
assert d.device_type == DeviceType.Bulb
|
2020-05-20 19:17:33 +00:00
|
|
|
|
|
|
|
|
2024-01-29 17:14:30 +00:00
|
|
|
@strip_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_type_detection_strip(dev: Device):
|
2021-09-19 21:45:48 +00:00
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2020-05-20 19:17:33 +00:00
|
|
|
assert d.device_type == DeviceType.Strip
|
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@dimmer_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_type_detection_dimmer(dev: Device):
|
2021-09-19 21:45:48 +00:00
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2020-05-20 19:17:33 +00:00
|
|
|
assert d.device_type == DeviceType.Dimmer
|
|
|
|
|
|
|
|
|
2024-05-13 16:34:44 +00:00
|
|
|
@lightstrip_iot
|
2024-02-04 15:20:08 +00:00
|
|
|
async def test_type_detection_lightstrip(dev: Device):
|
2021-09-19 21:45:48 +00:00
|
|
|
d = Discover._get_device_class(dev._last_update)("localhost")
|
2020-07-19 20:32:17 +00:00
|
|
|
assert d.device_type == DeviceType.LightStrip
|
|
|
|
|
|
|
|
|
2024-11-23 12:20:51 +00:00
|
|
|
@pytest.mark.xdist_group(name="caplog")
|
2024-11-18 14:53:11 +00:00
|
|
|
async def test_type_unknown(caplog):
|
2020-05-20 19:17:33 +00:00
|
|
|
invalid_info = {"system": {"get_sysinfo": {"type": "nosuchtype"}}}
|
2024-11-18 14:53:11 +00:00
|
|
|
assert Discover._get_device_class(invalid_info) is IotPlug
|
|
|
|
msg = "Unknown device type nosuchtype, falling back to plug"
|
|
|
|
assert msg in caplog.text
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
|
2023-07-09 23:55:27 +00:00
|
|
|
@pytest.mark.parametrize("custom_port", [123, None])
|
2023-12-04 18:50:05 +00:00
|
|
|
async def test_discover_single(discovery_mock, custom_port, mocker):
|
2021-09-24 15:18:11 +00:00
|
|
|
"""Make sure that discover_single returns an initialized SmartDevice instance."""
|
2023-08-29 13:04:28 +00:00
|
|
|
host = "127.0.0.1"
|
2023-12-04 18:50:05 +00:00
|
|
|
discovery_mock.ip = host
|
|
|
|
discovery_mock.port_override = custom_port
|
2023-08-29 13:04:28 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
device_class = Discover._get_device_class(discovery_mock.discovery_data)
|
2024-06-03 09:14:10 +00:00
|
|
|
# discovery_mock patches protocol query methods so use spy here.
|
|
|
|
update_mock = mocker.spy(device_class, "update")
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
x = await Discover.discover_single(
|
|
|
|
host, port=custom_port, credentials=Credentials()
|
|
|
|
)
|
2024-02-04 15:20:08 +00:00
|
|
|
assert issubclass(x.__class__, Device)
|
2023-12-04 18:50:05 +00:00
|
|
|
assert x._discovery_info is not None
|
|
|
|
assert x.port == custom_port or x.port == discovery_mock.default_port
|
2024-06-03 09:14:10 +00:00
|
|
|
# Make sure discovery does not call update()
|
2023-12-29 19:17:15 +00:00
|
|
|
assert update_mock.call_count == 0
|
2024-01-11 15:12:02 +00:00
|
|
|
if discovery_mock.default_port == 80:
|
|
|
|
assert x.alias is None
|
2023-12-29 19:17:15 +00:00
|
|
|
|
2024-06-03 18:06:54 +00:00
|
|
|
ct = DeviceConnectionParameters.from_values(
|
2024-01-03 21:46:08 +00:00
|
|
|
discovery_mock.device_type,
|
|
|
|
discovery_mock.encrypt_type,
|
|
|
|
discovery_mock.login_version,
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
|
|
|
uses_http = discovery_mock.default_port == 80
|
|
|
|
config = DeviceConfig(
|
2024-01-03 21:46:08 +00:00
|
|
|
host=host,
|
|
|
|
port_override=custom_port,
|
|
|
|
connection_type=ct,
|
|
|
|
uses_http=uses_http,
|
|
|
|
credentials=Credentials(),
|
2023-12-29 19:17:15 +00:00
|
|
|
)
|
|
|
|
assert x.config == config
|
2023-08-29 13:04:28 +00:00
|
|
|
|
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
async def test_discover_single_hostname(discovery_mock, mocker):
|
2023-11-07 01:15:41 +00:00
|
|
|
"""Make sure that discover_single returns an initialized SmartDevice instance."""
|
|
|
|
host = "foobar"
|
|
|
|
ip = "127.0.0.1"
|
|
|
|
|
2023-12-04 18:50:05 +00:00
|
|
|
discovery_mock.ip = ip
|
2023-12-29 19:17:15 +00:00
|
|
|
device_class = Discover._get_device_class(discovery_mock.discovery_data)
|
|
|
|
update_mock = mocker.patch.object(device_class, "update")
|
2023-11-07 01:15:41 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
x = await Discover.discover_single(host, credentials=Credentials())
|
2024-02-04 15:20:08 +00:00
|
|
|
assert issubclass(x.__class__, Device)
|
2023-12-04 18:50:05 +00:00
|
|
|
assert x._discovery_info is not None
|
2023-11-07 01:15:41 +00:00
|
|
|
assert x.host == host
|
2023-12-29 19:17:15 +00:00
|
|
|
assert update_mock.call_count == 0
|
2023-11-07 01:15:41 +00:00
|
|
|
|
|
|
|
mocker.patch("socket.getaddrinfo", side_effect=socket.gaierror())
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2023-12-29 19:17:15 +00:00
|
|
|
x = await Discover.discover_single(host, credentials=Credentials())
|
2023-11-07 01:15:41 +00:00
|
|
|
|
|
|
|
|
2024-06-03 18:06:54 +00:00
|
|
|
async def test_discover_credentials(mocker):
|
|
|
|
"""Make sure that discover gives credentials precedence over un and pw."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
|
2024-09-27 16:20:25 +00:00
|
|
|
async def mock_discover(self, *_, **__):
|
2024-06-03 18:06:54 +00:00
|
|
|
self.discovered_devices = {host: MagicMock()}
|
2024-09-27 16:20:25 +00:00
|
|
|
self.seen_hosts.add(host)
|
|
|
|
self._handle_discovered_event()
|
2024-06-03 18:06:54 +00:00
|
|
|
|
2024-09-27 16:20:25 +00:00
|
|
|
mocker.patch.object(_DiscoverProtocol, "do_discover", new=mock_discover)
|
2024-06-03 18:06:54 +00:00
|
|
|
dp = mocker.spy(_DiscoverProtocol, "__init__")
|
|
|
|
|
|
|
|
# Only credentials passed
|
|
|
|
await Discover.discover(credentials=Credentials(), timeout=0)
|
|
|
|
assert dp.mock_calls[0].kwargs["credentials"] == Credentials()
|
|
|
|
# Credentials and un/pw passed
|
|
|
|
await Discover.discover(
|
|
|
|
credentials=Credentials(), username="Foo", password="Bar", timeout=0
|
|
|
|
)
|
|
|
|
assert dp.mock_calls[1].kwargs["credentials"] == Credentials()
|
|
|
|
# Only un/pw passed
|
|
|
|
await Discover.discover(username="Foo", password="Bar", timeout=0)
|
|
|
|
assert dp.mock_calls[2].kwargs["credentials"] == Credentials("Foo", "Bar")
|
|
|
|
# Only un passed, credentials should be None
|
|
|
|
await Discover.discover(username="Foo", timeout=0)
|
|
|
|
assert dp.mock_calls[3].kwargs["credentials"] is None
|
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_single_credentials(mocker):
|
|
|
|
"""Make sure that discover_single gives credentials precedence over un and pw."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
|
2024-09-27 16:20:25 +00:00
|
|
|
async def mock_discover(self, *_, **__):
|
2024-06-03 18:06:54 +00:00
|
|
|
self.discovered_devices = {host: MagicMock()}
|
2024-09-27 16:20:25 +00:00
|
|
|
self.seen_hosts.add(host)
|
|
|
|
self._handle_discovered_event()
|
2024-06-03 18:06:54 +00:00
|
|
|
|
2024-09-27 16:20:25 +00:00
|
|
|
mocker.patch.object(_DiscoverProtocol, "do_discover", new=mock_discover)
|
2024-06-03 18:06:54 +00:00
|
|
|
dp = mocker.spy(_DiscoverProtocol, "__init__")
|
|
|
|
|
|
|
|
# Only credentials passed
|
|
|
|
await Discover.discover_single(host, credentials=Credentials(), timeout=0)
|
|
|
|
assert dp.mock_calls[0].kwargs["credentials"] == Credentials()
|
|
|
|
# Credentials and un/pw passed
|
|
|
|
await Discover.discover_single(
|
|
|
|
host, credentials=Credentials(), username="Foo", password="Bar", timeout=0
|
|
|
|
)
|
|
|
|
assert dp.mock_calls[1].kwargs["credentials"] == Credentials()
|
|
|
|
# Only un/pw passed
|
|
|
|
await Discover.discover_single(host, username="Foo", password="Bar", timeout=0)
|
|
|
|
assert dp.mock_calls[2].kwargs["credentials"] == Credentials("Foo", "Bar")
|
|
|
|
# Only un passed, credentials should be None
|
|
|
|
await Discover.discover_single(host, username="Foo", timeout=0)
|
|
|
|
assert dp.mock_calls[3].kwargs["credentials"] is None
|
|
|
|
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
async def test_discover_single_unsupported(unsupported_device_info, mocker):
|
2023-08-29 13:04:28 +00:00
|
|
|
"""Make sure that discover_single handles unsupported devices correctly."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
|
|
|
|
# Test with a valid unsupported response
|
|
|
|
with pytest.raises(
|
2024-02-21 15:52:55 +00:00
|
|
|
UnsupportedDeviceError,
|
2023-08-29 13:04:28 +00:00
|
|
|
):
|
|
|
|
await Discover.discover_single(host)
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
async def test_discover_single_no_response(mocker):
|
|
|
|
"""Make sure that discover_single handles no response correctly."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
mocker.patch.object(_DiscoverProtocol, "do_discover")
|
2023-08-29 13:04:28 +00:00
|
|
|
with pytest.raises(
|
2024-02-21 15:52:55 +00:00
|
|
|
KasaException, match=f"Timed out getting discovery response for {host}"
|
2023-08-29 13:04:28 +00:00
|
|
|
):
|
2023-12-29 19:17:15 +00:00
|
|
|
await Discover.discover_single(host, discovery_timeout=0)
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
INVALIDS = [
|
|
|
|
("No 'system' or 'get_sysinfo' in response", {"no": "data"}),
|
|
|
|
(
|
|
|
|
"Unable to find the device type field",
|
|
|
|
{"system": {"get_sysinfo": {"missing_type": 1}}},
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2024-08-30 15:30:07 +00:00
|
|
|
@pytest.mark.parametrize(("msg", "data"), INVALIDS)
|
2021-09-24 15:18:11 +00:00
|
|
|
async def test_discover_invalid_info(msg, data, mocker):
|
|
|
|
"""Make sure that invalid discovery information raises an exception."""
|
2023-08-29 13:04:28 +00:00
|
|
|
host = "127.0.0.1"
|
|
|
|
|
2024-02-05 17:53:09 +00:00
|
|
|
async def mock_discover(self):
|
2023-08-29 13:04:28 +00:00
|
|
|
self.datagram_received(
|
2024-01-26 09:11:31 +00:00
|
|
|
XorEncryption.encrypt(json_dumps(data))[4:], (host, 9999)
|
2023-08-29 13:04:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
mocker.patch.object(_DiscoverProtocol, "do_discover", mock_discover)
|
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException, match=msg):
|
2023-08-29 13:04:28 +00:00
|
|
|
await Discover.discover_single(host)
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_send(mocker):
|
|
|
|
"""Test discovery parameters."""
|
2024-02-05 17:53:09 +00:00
|
|
|
discovery_timeout = 0
|
|
|
|
proto = _DiscoverProtocol(discovery_timeout=discovery_timeout)
|
2021-09-24 15:18:11 +00:00
|
|
|
assert proto.discovery_packets == 3
|
2024-01-22 17:25:23 +00:00
|
|
|
assert proto.target_1 == ("255.255.255.255", 9999)
|
2021-10-29 00:44:51 +00:00
|
|
|
transport = mocker.patch.object(proto, "transport")
|
2024-01-22 17:25:23 +00:00
|
|
|
await proto.do_discover()
|
2024-10-29 16:21:24 +00:00
|
|
|
assert transport.sendto.call_count == proto.discovery_packets * 2
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_datagram_received(mocker, discovery_data):
|
|
|
|
"""Verify that datagram received fills discovered_devices."""
|
|
|
|
proto = _DiscoverProtocol()
|
2023-12-04 18:50:05 +00:00
|
|
|
|
2024-01-26 09:11:31 +00:00
|
|
|
mocker.patch.object(XorEncryption, "decrypt")
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
addr = "127.0.0.1"
|
2023-12-04 18:50:05 +00:00
|
|
|
port = 20002 if "result" in discovery_data else 9999
|
|
|
|
|
|
|
|
mocker.patch("kasa.discover.json_loads", return_value=discovery_data)
|
|
|
|
proto.datagram_received("<placeholder data>", (addr, port))
|
|
|
|
|
2023-08-29 13:04:28 +00:00
|
|
|
addr2 = "127.0.0.2"
|
2023-12-04 18:50:05 +00:00
|
|
|
mocker.patch("kasa.discover.json_loads", return_value=UNSUPPORTED)
|
2023-08-29 13:04:28 +00:00
|
|
|
proto.datagram_received("<placeholder data>", (addr2, 20002))
|
2021-09-24 15:18:11 +00:00
|
|
|
|
|
|
|
# Check that device in discovered_devices is initialized correctly
|
|
|
|
assert len(proto.discovered_devices) == 1
|
2023-08-29 13:04:28 +00:00
|
|
|
# Check that unsupported device is 1
|
2023-12-19 12:50:33 +00:00
|
|
|
assert len(proto.unsupported_device_exceptions) == 1
|
2021-09-24 15:18:11 +00:00
|
|
|
dev = proto.discovered_devices[addr]
|
2024-02-04 15:20:08 +00:00
|
|
|
assert issubclass(dev.__class__, Device)
|
2021-09-24 15:18:11 +00:00
|
|
|
assert dev.host == addr
|
2021-10-29 00:44:51 +00:00
|
|
|
|
|
|
|
|
2024-08-30 15:30:07 +00:00
|
|
|
@pytest.mark.parametrize(("msg", "data"), INVALIDS)
|
2021-10-29 00:44:51 +00:00
|
|
|
async def test_discover_invalid_responses(msg, data, mocker):
|
|
|
|
"""Verify that we don't crash whole discovery if some devices in the network are sending unexpected data."""
|
|
|
|
proto = _DiscoverProtocol()
|
2023-06-17 23:03:04 +00:00
|
|
|
mocker.patch("kasa.discover.json_loads", return_value=data)
|
2024-01-26 09:11:31 +00:00
|
|
|
mocker.patch.object(XorEncryption, "encrypt")
|
|
|
|
mocker.patch.object(XorEncryption, "decrypt")
|
2021-10-29 00:44:51 +00:00
|
|
|
|
2023-08-29 13:04:28 +00:00
|
|
|
proto.datagram_received(data, ("127.0.0.1", 9999))
|
2021-10-29 00:44:51 +00:00
|
|
|
assert len(proto.discovered_devices) == 0
|
2023-11-20 13:17:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
AUTHENTICATION_DATA_KLAP = {
|
|
|
|
"result": {
|
|
|
|
"device_id": "xx",
|
|
|
|
"owner": "xx",
|
|
|
|
"device_type": "IOT.SMARTPLUGSWITCH",
|
|
|
|
"device_model": "HS100(UK)",
|
|
|
|
"ip": "127.0.0.1",
|
|
|
|
"mac": "12-34-56-78-90-AB",
|
|
|
|
"is_support_iot_cloud": True,
|
|
|
|
"obd_src": "tplink",
|
|
|
|
"factory_default": False,
|
|
|
|
"mgt_encrypt_schm": {
|
|
|
|
"is_support_https": False,
|
|
|
|
"encrypt_type": "KLAP",
|
|
|
|
"http_port": 80,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"error_code": 0,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
@new_discovery
|
|
|
|
async def test_discover_single_authentication(discovery_mock, mocker):
|
2023-11-20 13:17:10 +00:00
|
|
|
"""Make sure that discover_single handles authenticating devices correctly."""
|
|
|
|
host = "127.0.0.1"
|
2023-12-29 19:17:15 +00:00
|
|
|
discovery_mock.ip = host
|
|
|
|
device_class = Discover._get_device_class(discovery_mock.discovery_data)
|
2023-11-20 13:17:10 +00:00
|
|
|
mocker.patch.object(
|
2023-12-29 19:17:15 +00:00
|
|
|
device_class,
|
2023-11-20 13:17:10 +00:00
|
|
|
"update",
|
2024-02-21 15:52:55 +00:00
|
|
|
side_effect=AuthenticationError("Failed to authenticate"),
|
2023-11-20 13:17:10 +00:00
|
|
|
)
|
|
|
|
|
2024-08-30 15:30:07 +00:00
|
|
|
with pytest.raises( # noqa: PT012
|
2024-02-21 15:52:55 +00:00
|
|
|
AuthenticationError,
|
2023-11-20 13:17:10 +00:00
|
|
|
match="Failed to authenticate",
|
|
|
|
):
|
2023-12-29 19:17:15 +00:00
|
|
|
device = await Discover.discover_single(
|
|
|
|
host, credentials=Credentials("foo", "bar")
|
|
|
|
)
|
2023-11-21 20:58:41 +00:00
|
|
|
await device.update()
|
2023-11-20 13:17:10 +00:00
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
mocker.patch.object(device_class, "update")
|
|
|
|
device = await Discover.discover_single(host, credentials=Credentials("foo", "bar"))
|
2023-11-21 20:58:41 +00:00
|
|
|
await device.update()
|
2023-12-29 19:17:15 +00:00
|
|
|
assert isinstance(device, device_class)
|
2023-11-20 13:17:10 +00:00
|
|
|
|
|
|
|
|
2023-12-29 19:17:15 +00:00
|
|
|
@new_discovery
|
2024-02-27 17:39:04 +00:00
|
|
|
async def test_device_update_from_new_discovery_info(discovery_mock):
|
2024-02-05 17:53:09 +00:00
|
|
|
"""Make sure that new discovery devices update from discovery info correctly."""
|
2024-02-27 17:39:04 +00:00
|
|
|
discovery_data = discovery_mock.discovery_data
|
2024-02-05 17:53:09 +00:00
|
|
|
device_class = Discover._get_device_class(discovery_data)
|
|
|
|
device = device_class("127.0.0.1")
|
2024-11-12 21:00:04 +00:00
|
|
|
discover_info = DiscoveryResult.from_dict(discovery_data["result"])
|
|
|
|
discover_dump = discover_info.to_dict()
|
2024-02-05 17:53:09 +00:00
|
|
|
model, _, _ = discover_dump["device_model"].partition("(")
|
|
|
|
discover_dump["model"] = model
|
2023-11-20 13:17:10 +00:00
|
|
|
device.update_from_discover_info(discover_dump)
|
|
|
|
|
|
|
|
assert device.mac == discover_dump["mac"].replace("-", ":")
|
2024-02-05 17:53:09 +00:00
|
|
|
assert device.model == model
|
2023-11-20 13:17:10 +00:00
|
|
|
|
2024-02-05 17:53:09 +00:00
|
|
|
# TODO implement requires_update for SmartDevice
|
|
|
|
if isinstance(device, IotDevice):
|
|
|
|
with pytest.raises(
|
2024-02-21 15:52:55 +00:00
|
|
|
KasaException,
|
2024-02-05 17:53:09 +00:00
|
|
|
match=re.escape("You need to await update() to access the data"),
|
|
|
|
):
|
|
|
|
assert device.supported_modules
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_single_http_client(discovery_mock, mocker):
|
|
|
|
"""Make sure that discover_single returns an initialized SmartDevice instance."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
discovery_mock.ip = host
|
|
|
|
|
2024-01-18 17:32:26 +00:00
|
|
|
http_client = aiohttp.ClientSession()
|
2023-12-29 19:17:15 +00:00
|
|
|
|
2024-02-04 15:20:08 +00:00
|
|
|
x: Device = await Discover.discover_single(host)
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
assert x.config.uses_http == (discovery_mock.default_port == 80)
|
|
|
|
|
|
|
|
if discovery_mock.default_port == 80:
|
2024-01-18 09:57:33 +00:00
|
|
|
assert x.protocol._transport._http_client.client != http_client
|
2023-12-29 19:17:15 +00:00
|
|
|
x.config.http_client = http_client
|
2024-01-18 09:57:33 +00:00
|
|
|
assert x.protocol._transport._http_client.client == http_client
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_http_client(discovery_mock, mocker):
|
2024-02-05 17:53:09 +00:00
|
|
|
"""Make sure that discover returns an initialized SmartDevice instance."""
|
2023-12-29 19:17:15 +00:00
|
|
|
host = "127.0.0.1"
|
|
|
|
discovery_mock.ip = host
|
|
|
|
|
2024-01-18 17:32:26 +00:00
|
|
|
http_client = aiohttp.ClientSession()
|
2023-12-29 19:17:15 +00:00
|
|
|
|
|
|
|
devices = await Discover.discover(discovery_timeout=0)
|
2024-02-04 15:20:08 +00:00
|
|
|
x: Device = devices[host]
|
2023-12-29 19:17:15 +00:00
|
|
|
assert x.config.uses_http == (discovery_mock.default_port == 80)
|
|
|
|
|
|
|
|
if discovery_mock.default_port == 80:
|
2024-01-18 09:57:33 +00:00
|
|
|
assert x.protocol._transport._http_client.client != http_client
|
2023-12-29 19:17:15 +00:00
|
|
|
x.config.http_client = http_client
|
2024-01-18 09:57:33 +00:00
|
|
|
assert x.protocol._transport._http_client.client == http_client
|
2024-01-22 17:25:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
LEGACY_DISCOVER_DATA = {
|
|
|
|
"system": {
|
|
|
|
"get_sysinfo": {
|
|
|
|
"alias": "#MASKED_NAME#",
|
|
|
|
"dev_name": "Smart Wi-Fi Plug",
|
|
|
|
"deviceId": "0000000000000000000000000000000000000000",
|
|
|
|
"err_code": 0,
|
|
|
|
"hwId": "00000000000000000000000000000000",
|
|
|
|
"hw_ver": "0.0",
|
|
|
|
"mac": "00:00:00:00:00:00",
|
|
|
|
"mic_type": "IOT.SMARTPLUGSWITCH",
|
|
|
|
"model": "HS100(UK)",
|
|
|
|
"sw_ver": "1.1.0 Build 201016 Rel.175121",
|
|
|
|
"updating": 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class FakeDatagramTransport(asyncio.DatagramTransport):
|
|
|
|
GHOST_PORT = 8888
|
|
|
|
|
|
|
|
def __init__(self, dp, port, do_not_reply_count, unsupported=False):
|
|
|
|
self.dp = dp
|
|
|
|
self.port = port
|
|
|
|
self.do_not_reply_count = do_not_reply_count
|
|
|
|
self.send_count = 0
|
|
|
|
if port == 9999:
|
2024-01-26 09:11:31 +00:00
|
|
|
self.datagram = XorEncryption.encrypt(json_dumps(LEGACY_DISCOVER_DATA))[4:]
|
2024-01-22 17:25:23 +00:00
|
|
|
elif port == 20002:
|
|
|
|
discovery_data = UNSUPPORTED if unsupported else AUTHENTICATION_DATA_KLAP
|
|
|
|
self.datagram = (
|
|
|
|
b"\x02\x00\x00\x01\x01[\x00\x00\x00\x00\x00\x00W\xcev\xf8"
|
|
|
|
+ json_dumps(discovery_data).encode()
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self.datagram = {"foo": "bar"}
|
|
|
|
|
|
|
|
def get_extra_info(self, name, default=None):
|
|
|
|
return MagicMock()
|
|
|
|
|
|
|
|
def sendto(self, data, addr=None):
|
|
|
|
ip, port = addr
|
|
|
|
if port == self.port or self.port == self.GHOST_PORT:
|
|
|
|
self.send_count += 1
|
|
|
|
if self.send_count > self.do_not_reply_count:
|
|
|
|
self.dp.datagram_received(self.datagram, (ip, self.port))
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("port", [9999, 20002])
|
|
|
|
@pytest.mark.parametrize("do_not_reply_count", [0, 1, 2, 3, 4])
|
|
|
|
async def test_do_discover_drop_packets(mocker, port, do_not_reply_count):
|
2024-02-05 17:53:09 +00:00
|
|
|
"""Make sure that _DiscoverProtocol handles authenticating devices correctly."""
|
2024-01-22 17:25:23 +00:00
|
|
|
host = "127.0.0.1"
|
2024-02-05 17:53:09 +00:00
|
|
|
discovery_timeout = 0
|
2024-01-22 17:25:23 +00:00
|
|
|
|
|
|
|
dp = _DiscoverProtocol(
|
|
|
|
target=host,
|
|
|
|
discovery_timeout=discovery_timeout,
|
|
|
|
discovery_packets=5,
|
|
|
|
)
|
2024-10-29 16:21:24 +00:00
|
|
|
ft = FakeDatagramTransport(dp, port, do_not_reply_count)
|
2024-01-22 17:25:23 +00:00
|
|
|
dp.connection_made(ft)
|
|
|
|
|
2024-02-05 17:53:09 +00:00
|
|
|
await dp.wait_for_discovery_to_complete()
|
2024-01-22 17:25:23 +00:00
|
|
|
|
|
|
|
await asyncio.sleep(0)
|
2024-10-29 16:21:24 +00:00
|
|
|
assert ft.send_count == do_not_reply_count + 1
|
2024-01-22 17:25:23 +00:00
|
|
|
assert dp.discover_task.done()
|
2024-02-05 17:53:09 +00:00
|
|
|
assert dp.discover_task.cancelled()
|
2024-01-22 17:25:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2024-08-30 15:30:07 +00:00
|
|
|
("port", "will_timeout"),
|
2024-01-22 17:25:23 +00:00
|
|
|
[(FakeDatagramTransport.GHOST_PORT, True), (20002, False)],
|
|
|
|
ids=["unknownport", "unsupporteddevice"],
|
|
|
|
)
|
|
|
|
async def test_do_discover_invalid(mocker, port, will_timeout):
|
2024-02-05 17:53:09 +00:00
|
|
|
"""Make sure that _DiscoverProtocol handles invalid devices correctly."""
|
2024-01-22 17:25:23 +00:00
|
|
|
host = "127.0.0.1"
|
2024-02-05 17:53:09 +00:00
|
|
|
discovery_timeout = 0
|
2024-01-22 17:25:23 +00:00
|
|
|
|
|
|
|
dp = _DiscoverProtocol(
|
|
|
|
target=host,
|
|
|
|
discovery_timeout=discovery_timeout,
|
|
|
|
discovery_packets=5,
|
|
|
|
)
|
|
|
|
ft = FakeDatagramTransport(dp, port, 0, unsupported=True)
|
|
|
|
dp.connection_made(ft)
|
|
|
|
|
2024-02-05 17:53:09 +00:00
|
|
|
await dp.wait_for_discovery_to_complete()
|
2024-01-22 17:25:23 +00:00
|
|
|
await asyncio.sleep(0)
|
|
|
|
assert dp.discover_task.done()
|
2024-02-05 17:53:09 +00:00
|
|
|
assert dp.discover_task.cancelled() != will_timeout
|
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_propogates_task_exceptions(discovery_mock):
|
|
|
|
"""Make sure that discover propogates callback exceptions."""
|
|
|
|
discovery_timeout = 0
|
|
|
|
|
|
|
|
async def on_discovered(dev):
|
2024-02-21 15:52:55 +00:00
|
|
|
raise KasaException("Dummy exception")
|
2024-02-05 17:53:09 +00:00
|
|
|
|
2024-02-21 15:52:55 +00:00
|
|
|
with pytest.raises(KasaException):
|
2024-02-05 17:53:09 +00:00
|
|
|
await Discover.discover(
|
|
|
|
discovery_timeout=discovery_timeout, on_discovered=on_discovered
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def test_do_discover_no_connection(mocker):
|
|
|
|
"""Make sure that if the datagram connection doesnt start a TimeoutError is raised."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
discovery_timeout = 0
|
|
|
|
mocker.patch.object(_DiscoverProtocol, "DISCOVERY_START_TIMEOUT", 0)
|
|
|
|
dp = _DiscoverProtocol(
|
|
|
|
target=host,
|
|
|
|
discovery_timeout=discovery_timeout,
|
|
|
|
discovery_packets=5,
|
|
|
|
)
|
|
|
|
# Normally tests would simulate connection as per below
|
|
|
|
# ft = FakeDatagramTransport(dp, port, 0, unsupported=True)
|
|
|
|
# dp.connection_made(ft)
|
|
|
|
|
|
|
|
with pytest.raises(asyncio.TimeoutError):
|
|
|
|
await dp.wait_for_discovery_to_complete()
|
|
|
|
|
|
|
|
|
|
|
|
async def test_do_discover_external_cancel(mocker):
|
|
|
|
"""Make sure that a cancel other than when target is discovered propogates."""
|
|
|
|
host = "127.0.0.1"
|
|
|
|
discovery_timeout = 1
|
|
|
|
|
|
|
|
dp = _DiscoverProtocol(
|
|
|
|
target=host,
|
|
|
|
discovery_timeout=discovery_timeout,
|
|
|
|
discovery_packets=1,
|
|
|
|
)
|
|
|
|
# Normally tests would simulate connection as per below
|
|
|
|
ft = FakeDatagramTransport(dp, 9999, 1, unsupported=True)
|
|
|
|
dp.connection_made(ft)
|
|
|
|
|
|
|
|
with pytest.raises(asyncio.TimeoutError):
|
|
|
|
async with asyncio_timeout(0):
|
|
|
|
await dp.wait_for_discovery_to_complete()
|
2024-07-17 17:57:09 +00:00
|
|
|
|
|
|
|
|
2024-11-23 12:20:51 +00:00
|
|
|
@pytest.mark.xdist_group(name="caplog")
|
2024-07-17 17:57:09 +00:00
|
|
|
async def test_discovery_redaction(discovery_mock, caplog: pytest.LogCaptureFixture):
|
|
|
|
"""Test query sensitive info redaction."""
|
|
|
|
mac = "12:34:56:78:9A:BC"
|
|
|
|
|
|
|
|
if discovery_mock.default_port == 9999:
|
|
|
|
sysinfo = discovery_mock.discovery_data["system"]["get_sysinfo"]
|
|
|
|
if "mac" in sysinfo:
|
|
|
|
sysinfo["mac"] = mac
|
|
|
|
elif "mic_mac" in sysinfo:
|
|
|
|
sysinfo["mic_mac"] = mac
|
|
|
|
else:
|
|
|
|
discovery_mock.discovery_data["result"]["mac"] = mac
|
|
|
|
|
|
|
|
# Info no message logging
|
|
|
|
caplog.set_level(logging.INFO)
|
|
|
|
await Discover.discover()
|
|
|
|
|
|
|
|
assert mac not in caplog.text
|
|
|
|
|
|
|
|
caplog.set_level(logging.DEBUG)
|
|
|
|
|
|
|
|
# Debug no redaction
|
|
|
|
caplog.clear()
|
|
|
|
Discover._redact_data = False
|
|
|
|
await Discover.discover()
|
|
|
|
assert mac in caplog.text
|
|
|
|
|
|
|
|
# Debug redaction
|
|
|
|
caplog.clear()
|
|
|
|
Discover._redact_data = True
|
|
|
|
await Discover.discover()
|
|
|
|
assert mac not in caplog.text
|
|
|
|
assert "12:34:56:00:00:00" in caplog.text
|
2024-10-16 14:28:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_decryption():
|
|
|
|
"""Test discovery decryption."""
|
|
|
|
key = b"8\x89\x02\xfa\xf5Xs\x1c\xa1 H\x9a\x82\xc7\xd9\t"
|
|
|
|
iv = b"9=\xf8\x1bS\xcd0\xb5\x89i\xba\xfd^9\x9f\xfa"
|
|
|
|
key_iv = key + iv
|
|
|
|
|
|
|
|
_AesDiscoveryQuery.generate_query()
|
|
|
|
keypair = _AesDiscoveryQuery.keypair
|
|
|
|
|
|
|
|
padding = asymmetric_padding.OAEP(
|
|
|
|
mgf=asymmetric_padding.MGF1(algorithm=hashes.SHA1()), # noqa: S303
|
|
|
|
algorithm=hashes.SHA1(), # noqa: S303
|
|
|
|
label=None,
|
|
|
|
)
|
|
|
|
encrypted_key_iv = keypair.public_key.encrypt(key_iv, padding)
|
|
|
|
encrypted_key_iv_b4 = base64.b64encode(encrypted_key_iv)
|
|
|
|
encryption_session = AesEncyptionSession(key_iv[:16], key_iv[16:])
|
|
|
|
|
|
|
|
data_dict = {"foo": 1, "bar": 2}
|
|
|
|
data = json.dumps(data_dict)
|
|
|
|
encypted_data = encryption_session.encrypt(data.encode())
|
|
|
|
|
|
|
|
encrypt_info = {
|
|
|
|
"data": encypted_data.decode(),
|
|
|
|
"key": encrypted_key_iv_b4.decode(),
|
|
|
|
"sym_schm": "AES",
|
|
|
|
}
|
|
|
|
info = {**UNSUPPORTED["result"], "encrypt_info": encrypt_info}
|
2024-11-12 21:00:04 +00:00
|
|
|
dr = DiscoveryResult.from_dict(info)
|
2024-10-16 14:28:27 +00:00
|
|
|
Discover._decrypt_discovery_data(dr)
|
|
|
|
assert dr.decrypted_data == data_dict
|
2024-10-22 13:33:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discover_try_connect_all(discovery_mock, mocker):
|
|
|
|
"""Test that device update is called on main."""
|
|
|
|
if "result" in discovery_mock.discovery_data:
|
2024-10-22 17:09:35 +00:00
|
|
|
dev_class = get_device_class_from_family(
|
|
|
|
discovery_mock.device_type, https=discovery_mock.https
|
|
|
|
)
|
2024-10-22 13:33:46 +00:00
|
|
|
cparams = DeviceConnectionParameters.from_values(
|
|
|
|
discovery_mock.device_type,
|
|
|
|
discovery_mock.encrypt_type,
|
|
|
|
discovery_mock.login_version,
|
2024-10-22 17:09:35 +00:00
|
|
|
discovery_mock.https,
|
2024-10-22 13:33:46 +00:00
|
|
|
)
|
|
|
|
protocol = get_protocol(
|
|
|
|
DeviceConfig(discovery_mock.ip, connection_type=cparams)
|
|
|
|
)
|
|
|
|
protocol_class = protocol.__class__
|
|
|
|
transport_class = protocol._transport.__class__
|
|
|
|
else:
|
|
|
|
dev_class = get_device_class_from_sys_info(discovery_mock.discovery_data)
|
|
|
|
protocol_class = IotProtocol
|
|
|
|
transport_class = XorTransport
|
|
|
|
|
|
|
|
async def _query(self, *args, **kwargs):
|
|
|
|
if (
|
|
|
|
self.__class__ is protocol_class
|
|
|
|
and self._transport.__class__ is transport_class
|
|
|
|
):
|
|
|
|
return discovery_mock.query_data
|
|
|
|
raise KasaException()
|
|
|
|
|
|
|
|
async def _update(self, *args, **kwargs):
|
|
|
|
if (
|
|
|
|
self.protocol.__class__ is protocol_class
|
|
|
|
and self.protocol._transport.__class__ is transport_class
|
|
|
|
):
|
|
|
|
return
|
|
|
|
raise KasaException()
|
|
|
|
|
|
|
|
mocker.patch("kasa.IotProtocol.query", new=_query)
|
|
|
|
mocker.patch("kasa.SmartProtocol.query", new=_query)
|
|
|
|
mocker.patch.object(dev_class, "update", new=_update)
|
|
|
|
|
2024-10-25 18:43:37 +00:00
|
|
|
session = aiohttp.ClientSession()
|
|
|
|
dev = await Discover.try_connect_all(discovery_mock.ip, http_client=session)
|
2024-10-22 13:33:46 +00:00
|
|
|
|
|
|
|
assert dev
|
|
|
|
assert isinstance(dev, dev_class)
|
|
|
|
assert isinstance(dev.protocol, protocol_class)
|
|
|
|
assert isinstance(dev.protocol._transport, transport_class)
|
2024-10-25 18:43:37 +00:00
|
|
|
assert dev.config.uses_http is (transport_class != XorTransport)
|
|
|
|
if transport_class != XorTransport:
|
|
|
|
assert dev.protocol._transport._http_client.client == session
|
2024-11-07 19:09:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_discovery_device_repr(discovery_mock, mocker):
|
|
|
|
"""Test that repr works when only discovery data is available."""
|
|
|
|
host = "foobar"
|
|
|
|
ip = "127.0.0.1"
|
|
|
|
|
|
|
|
discovery_mock.ip = ip
|
|
|
|
device_class = Discover._get_device_class(discovery_mock.discovery_data)
|
|
|
|
update_mock = mocker.patch.object(device_class, "update")
|
|
|
|
|
|
|
|
dev = await Discover.discover_single(host, credentials=Credentials())
|
|
|
|
assert update_mock.call_count == 0
|
|
|
|
|
|
|
|
repr_ = repr(dev)
|
|
|
|
assert dev.host in repr_
|
|
|
|
assert str(dev.device_type) in repr_
|
|
|
|
assert dev.model in repr_
|
|
|
|
|
|
|
|
# For IOT devices, _last_update is filled from the discovery data
|
|
|
|
if dev._last_update:
|
|
|
|
assert "update() needed" not in repr_
|
|
|
|
else:
|
|
|
|
assert "update() needed" in repr_
|