1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-21 23:18:13 +00:00

python/protobuf: do not warn for unknown enum values (fixes #832)

This commit is contained in:
matejcik 2020-02-13 15:08:22 +01:00
parent e9003d0742
commit 3959600760
3 changed files with 34 additions and 6 deletions

View File

@ -161,7 +161,7 @@ class EnumType:
def validate(self, fvalue: int) -> int:
if fvalue not in self.enum_values:
# raise TypeError("Invalid enum value")
LOG.warning("Value {} unknown for type {}".format(fvalue, self.enum_name))
LOG.info("Value {} unknown for type {}".format(fvalue, self.enum_name))
return fvalue
def to_str(self, fvalue: int) -> str:
@ -485,7 +485,10 @@ def format_message(
return "{} bytes {}{}".format(length, output, suffix)
if isinstance(value, int) and isinstance(ftype, EnumType):
return "{} ({})".format(ftype.to_str(value), value)
try:
return "{} ({})".format(ftype.to_str(value), value)
except TypeError:
return str(value)
return repr(value)
@ -558,7 +561,10 @@ def to_dict(msg: MessageType, hexlify_bytes: bool = True) -> Dict[str, Any]:
elif isinstance(value, list):
return [convert_value(ftype, v) for v in value]
elif isinstance(value, int) and isinstance(ftype, EnumType):
return ftype.to_str(value)
try:
return ftype.to_str(value)
except TypeError:
return value
else:
return value

View File

@ -15,6 +15,7 @@
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
from io import BytesIO
import logging
import pytest
@ -137,6 +138,7 @@ def test_simple_message():
def test_validate_enum(caplog):
caplog.set_level(logging.INFO)
# round-trip of a valid value
msg = EnumMessageMoreValues(enum=0)
buf = BytesIO()
@ -154,7 +156,7 @@ def test_validate_enum(caplog):
assert len(caplog.records) == 1
record = caplog.records.pop(0)
assert record.levelname == "WARNING"
assert record.levelname == "INFO"
assert record.getMessage() == "Value 19 unknown for type t"
msg.enum = 3
@ -165,7 +167,7 @@ def test_validate_enum(caplog):
assert len(caplog.records) == 1
record = caplog.records.pop(0)
assert record.levelname == "WARNING"
assert record.levelname == "INFO"
assert record.getMessage() == "Value 3 unknown for type t"
@ -182,12 +184,14 @@ def test_repeated():
def test_enum_in_repeated(caplog):
caplog.set_level(logging.INFO)
msg = RepeatedFields(enumlist=[0, 1, 2, 3])
buf = BytesIO()
protobuf.dump_message(buf, msg)
assert len(caplog.records) == 2
for record in caplog.records:
assert record.levelname == "WARNING"
assert record.levelname == "INFO"
assert "unknown for type t" in record.getMessage()

View File

@ -216,3 +216,21 @@ def test_nested_recover():
dictdata = {"nested": {}}
recovered = protobuf.dict_to_proto(NestedMessage, dictdata)
assert isinstance(recovered.nested, SimpleMessage)
@with_simple_enum
def test_unknown_enum_to_str():
simple = SimpleMessage(enum=SimpleEnum.QUUX)
string = protobuf.format_message(simple)
assert "enum: QUUX (13)" in string
simple = SimpleMessage(enum=6000)
string = protobuf.format_message(simple)
assert "enum: 6000" in string
@with_simple_enum
def test_unknown_enum_to_dict():
simple = SimpleMessage(enum=6000)
converted = protobuf.to_dict(simple)
assert converted["enum"] == 6000