Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_security_iaswd_warning_mode(raw, mode, name):
"""Test warning command class of IasWD cluster."""
def _test(warning, data):
assert warning.serialize() == data
assert warning == raw
assert warning.mode == mode
assert warning.mode.name == name
warning.mode = mode
assert warning.serialize() == data
assert warning.mode == mode
data = types.uint8_t(raw).serialize()
_test(sec.IasWd.Warning(raw), data)
extra = b"The rest of the owl\xaa\x55"
warn, rest = sec.IasWd.Warning.deserialize(data + extra)
assert rest == extra
_test(warn, data)
repr(warn)
def test_struct_init():
class TestStruct(t.Struct):
a: t.uint8_t
b: t.uint16_t
c: t.CharacterString
ts = TestStruct(a=1, b=0x0100, c="TestStruct")
assert repr(ts)
assert isinstance(ts.a, t.uint8_t)
assert isinstance(ts.b, t.uint16_t)
assert isinstance(ts.c, t.CharacterString)
assert ts.a == 1
assert ts.b == 0x100
assert ts.c == "TestStruct"
ts2, remaining = TestStruct.deserialize(b"\x01\x00\x01\x0aTestStruct")
assert not remaining
assert ts == ts2
assert ts.serialize() == ts2.serialize()
ts3 = ts2.replace(b=0x0100)
assert ts3 == ts2
assert ts3.serialize() == ts2.serialize()
ts4 = ts2.replace(b=0x0101)
async def test_groups(tmpdir, monkeypatch):
monkeypatch.setattr(
Device, "schedule_initialize", mock_dev_init(Status.ENDPOINTS_INIT)
)
group_id, group_name = 0x1221, "app db Test Group 0x1221"
async def mock_request(*args, **kwargs):
return [ZCLStatus.SUCCESS, group_id]
monkeypatch.setattr(zigpy.zcl.Cluster, "request", mock_request)
db = os.path.join(str(tmpdir), "test.db")
app = await make_app(db)
ieee = make_ieee()
app.handle_join(99, ieee, 0)
dev = app.get_device(ieee)
ep = dev.add_endpoint(1)
ep.profile_id = 260
ep.device_type = profiles.zha.DeviceType.PUMP
ep.add_input_cluster(4)
app.device_initialized(dev)
ieee_b = make_ieee(2)
app.handle_join(100, ieee_b, 0)
dev_b = app.get_device(ieee_b)
def test_struct_init():
class TestStruct(t.Struct):
a: t.uint8_t
b: t.uint16_t
c: t.CharacterString
ts = TestStruct(a=1, b=0x0100, c="TestStruct")
assert repr(ts)
assert isinstance(ts.a, t.uint8_t)
assert isinstance(ts.b, t.uint16_t)
assert isinstance(ts.c, t.CharacterString)
assert ts.a == 1
assert ts.b == 0x100
assert ts.c == "TestStruct"
ts2, remaining = TestStruct.deserialize(b"\x01\x00\x01\x0aTestStruct")
assert not remaining
assert ts == ts2
assert ts.serialize() == ts2.serialize()
ts3 = ts2.replace(b=0x0100)
assert ts3 == ts2
assert ts3.serialize() == ts2.serialize()
ts4 = ts2.replace(b=0x0101)
assert ts4 != ts2
async def test_permit(app, ieee):
ncp_ieee = t.EUI64(map(t.uint8_t, range(8, 16)))
app._ieee = ncp_ieee
app.devices[ieee] = mock.MagicMock()
app.devices[ieee].zdo.permit = mock.MagicMock(
side_effect=asyncio.coroutine(mock.MagicMock())
)
app.permit_ncp = mock.MagicMock(side_effect=asyncio.coroutine(mock.MagicMock()))
await app.permit(node=(1, 1, 1, 1, 1, 1, 1, 1))
assert app.devices[ieee].zdo.permit.call_count == 0
assert app.permit_ncp.call_count == 0
await app.permit(node=ieee)
assert app.devices[ieee].zdo.permit.call_count == 1
assert app.permit_ncp.call_count == 0
await app.permit(node=ncp_ieee)
assert app.devices[ieee].zdo.permit.call_count == 1
assert app.permit_ncp.call_count == 1
def make_ieee(init=0):
return t.EUI64(map(t.uint8_t, range(init, init + 8)))
def test_ota_image_destination():
extra = b"abcdefghklmnpqr"
dst = t.EUI64.deserialize(b"12345678")[0]
hdr, rest = _test_ota_img_header(b"\x02\x00", dst.serialize(), extra)
assert rest == extra
assert hdr.security_credential_version_present is False
assert hdr.device_specific_file is True
assert hdr.upgrade_file_destination == dst
assert hdr.hardware_versions_present is False
data = tsn.serialize()
extra = b"abcdefExtraDataHere"
hdr, rest = types.ZDOHeader.deserialize(cmd_id, data + extra)
assert rest == extra
assert hdr.tsn == tsn
assert hdr.command_id == cmd_id
assert hdr.is_reply is False
hdr.command_id = types.ZDOCmd.Bind_rsp
assert hdr.is_reply is True
assert hdr.serialize() == data
new_tsn = 0xBB
hdr.tsn = new_tsn
assert isinstance(hdr.tsn, t.uint8_t)
assert hdr.tsn == new_tsn
async def test_broadcast(app):
app.ieee = t.EUI64(map(t.uint8_t, [8, 9, 10, 11, 12, 13, 14, 15]))
(profile, cluster, src_ep, dst_ep, data) = (
zha.PROFILE_ID,
1,
2,
3,
b"\x02\x01\x00",
)
await device.broadcast(app, profile, cluster, src_ep, dst_ep, 0, 0, 123, data)
assert app.broadcast.call_count == 1
assert app.broadcast.call_args[0][0] == profile
assert app.broadcast.call_args[0][1] == cluster
assert app.broadcast.call_args[0][2] == src_ep
assert app.broadcast.call_args[0][3] == dst_ep
assert app.broadcast.call_args[0][7] == data
assert (epid in signature) or (
"profile" in epdata and "device_type" in epdata
)
if "profile" in epdata:
profile = epdata["profile"]
assert isinstance(profile, int) and 0 <= profile <= 0xFFFF
if "device_type" in epdata:
device_type = epdata["device_type"]
assert isinstance(device_type, int) and 0 <= device_type <= 0xFFFF
all_clusters = epdata.get("input_clusters", []) + epdata.get(
"output_clusters", []
)
for cluster in all_clusters:
assert (
(isinstance(cluster, int) and cluster in Cluster._registry)
or (isinstance(cluster, int) and _check_range(cluster))
or issubclass(cluster, Cluster)
)