Skip to content

Commit

Permalink
Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
puddly committed Nov 8, 2024
1 parent 1dcd392 commit a91399d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
15 changes: 7 additions & 8 deletions bellows/ezsp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,14 +351,13 @@ async def get_board_info(
special, ver_info_bytes = t.uint8_t.deserialize(ver_info_bytes)
version = f"{major}.{minor}.{patch}.{special} build {build}"

if xncp.FirmwareFeatures.BUILD_STRING in self._xncp_features:
try:
build_string = await self.xncp_get_build_string()
except InvalidCommandError:
build_string = None
try:
build_string = await self.xncp_get_build_string()
except InvalidCommandError:
build_string = None

if build_string:
version = f"{version} ({build_string})"
if build_string:
version = f"{version} ({build_string})"

return (
tokens[t.EzspMfgTokenId.MFG_STRING],
Expand Down Expand Up @@ -689,7 +688,7 @@ async def xncp_get_mfg_token_override(self, token: t.EzspMfgTokenId) -> bytes:
rsp = await self.send_xncp_frame(xncp.GetMfgTokenOverrideReq(token=token))
return rsp.value

async def xncp_get_build_string(self) -> bytes:
async def xncp_get_build_string(self) -> str:
"""Get build string."""
rsp = await self.send_xncp_frame(xncp.GetBuildStringReq())
return rsp.build_string.decode("utf-8")
Expand Down
39 changes: 38 additions & 1 deletion tests/test_ezsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from bellows.ash import NcpFailure
from bellows.exception import EzspError, InvalidCommandError
from bellows.ezsp import EZSP, EZSP_LATEST
from bellows.ezsp.xncp import FirmwareFeatures
import bellows.types as t

if sys.version_info[:2] < (3, 11):
Expand Down Expand Up @@ -323,46 +324,60 @@ async def test_ezsp_newer_version(ezsp_f):
(
"mfg_board_name",
"mfg_string",
"xncp_build_string",
"value_version_info",
"expected",
),
[
(
(b"\xfe\xff\xff\xff",),
(b"Manufacturer\xff\xff\xff",),
(InvalidCommandError("XNCP is not supported"),),
(t.EmberStatus.SUCCESS, b"\x01\x02\x03\x04\x05\x06"),
("Manufacturer", "0xFE", "3.4.5.6 build 513"),
),
(
(b"\xfe\xff\xff\xff",),
(b"Manufacturer\xff\xff\xff",),
(InvalidCommandError("XNCP is not supported"),),
(t.EmberStatus.ERR_FATAL, b"\x01\x02\x03\x04\x05\x06"),
("Manufacturer", "0xFE", None),
),
(
(b"SkyBlue v0.1\x00\xff\xff\xff",),
(b"Nabu Casa\x00\xff\xff\xff\xff\xff\xff",),
(InvalidCommandError("XNCP is not supported"),),
(t.EmberStatus.SUCCESS, b"\xbf\x00\x07\x01\x00\x00\xaa"),
("Nabu Casa", "SkyBlue v0.1", "7.1.0.0 build 191"),
),
(
(b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",),
(b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",),
(InvalidCommandError("XNCP is not supported"),),
(t.EmberStatus.SUCCESS, b"\xbf\x00\x07\x01\x00\x00\xaa"),
(None, None, "7.1.0.0 build 191"),
),
(
(b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",),
(b"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00",),
(InvalidCommandError("XNCP is not supported"),),
(t.EmberStatus.SUCCESS, b")\x01\x06\n\x03\x00\xaa"),
(None, None, "6.10.3.0 build 297"),
),
(
(b"SkyBlue v0.1\x00\xff\xff\xff",),
(b"Nabu Casa\x00\xff\xff\xff\xff\xff\xff",),
("special build",),
(t.EmberStatus.SUCCESS, b"\xbf\x00\x07\x01\x00\x00\xaa"),
("Nabu Casa", "SkyBlue v0.1", "7.1.0.0 build 191 (special build)"),
),
],
)
async def test_board_info(
ezsp_f,
mfg_board_name: bytes,
mfg_string: bytes,
xncp_build_string: str | Exception,
value_version_info: tuple[t.EmberStatus, bytes],
expected: tuple[str | None, str | None, str],
):
Expand All @@ -384,7 +399,7 @@ async def replacement(command_name, tokenId=None, valueId=None):
("getValue", t.EzspValueId.VALUE_VERSION_INFO): value_version_info,
}
),
):
), patch.object(ezsp_f, "xncp_get_build_string", side_effect=xncp_build_string):
mfg, brd, ver = await ezsp_f.get_board_info()

assert (mfg, brd, ver) == expected
Expand Down Expand Up @@ -432,6 +447,28 @@ async def _mock_cmd(*args, **kwargs):
await ezsp_f.leaveNetwork(timeout=0.01)


async def test_xncp_token_override(ezsp_f):
ezsp_f.getMfgToken = AsyncMock(return_value=[b"firmware value"])
ezsp_f.xncp_get_mfg_token_override = AsyncMock(return_value=b"xncp value")

# Without firmware support, the XNCP command isn't sent
assert (
await ezsp_f.get_mfg_token(t.EzspMfgTokenId.MFG_CUSTOM_EUI_64)
) == b"firmware value"

# With firmware support, it is
ezsp_f._xncp_features |= FirmwareFeatures.MFG_TOKEN_OVERRIDES
assert (
await ezsp_f.get_mfg_token(t.EzspMfgTokenId.MFG_CUSTOM_EUI_64)
) == b"xncp value"

# Tokens without overrides are still read normally
ezsp_f.xncp_get_mfg_token_override.side_effect = InvalidCommandError
assert (
await ezsp_f.get_mfg_token(t.EzspMfgTokenId.MFG_CUSTOM_EUI_64)
) == b"firmware value"


@pytest.mark.parametrize(
"value, expected_result",
[
Expand Down

0 comments on commit a91399d

Please sign in to comment.