This is merely a historical archive of years 2008-2021, before the migration to mailman3.
A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.
fixeria gerrit-no-reply at lists.osmocom.orgfixeria has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmocom-bb/+/23136 ) Change subject: trx_toolkit/codec.py: implement check() API for Envelope ...................................................................... trx_toolkit/codec.py: implement check() API for Envelope Change-Id: I8258504493cf85c401677866fe8474de2e49786b Related: OS#4006, SYS#4895 --- M src/target/trx_toolkit/codec.py M src/target/trx_toolkit/test_codec.py 2 files changed, 28 insertions(+), 0 deletions(-) git pull ssh://gerrit.osmocom.org:29418/osmocom-bb refs/changes/36/23136/1 diff --git a/src/target/trx_toolkit/codec.py b/src/target/trx_toolkit/codec.py index e3cd600..cc3b5ed 100644 --- a/src/target/trx_toolkit/codec.py +++ b/src/target/trx_toolkit/codec.py @@ -271,6 +271,15 @@ def __delitem__(self, key: str) -> None: del self.content[key] + def check(self) -> None: + ''' Check the content before encoding and after decoding. + Raise exceptions (e.g. ValueError) if something is wrong. + + Do not assert for every possible error (e.g. a negative value + for a Uint field) if an exception will be thrown by the field's + to_bytes() method anyway. Only additional constraints here. + ''' + def from_bytes(self, data: bytes, offset: int = 0) -> int: self.content = { } # forget the old content try: # Fields throw exceptions @@ -282,6 +291,7 @@ if self.check_len and len(data) != offset: raise DecodeError(self, 'Unhandled tail octets: %s' % data[offset:].hex()) + self.check() # Check the content after decoding (raises exceptions) return offset def to_bytes(self) -> bytes: @@ -291,4 +301,5 @@ except Exception as e: # Add contextual info raise EncodeError(self, f) from e + self.check() # Check the content before encoding (raises exceptions) return b''.join([proc(f) for f in self.STRUCT]) diff --git a/src/target/trx_toolkit/test_codec.py b/src/target/trx_toolkit/test_codec.py index 0499f3a..287be92 100644 --- a/src/target/trx_toolkit/test_codec.py +++ b/src/target/trx_toolkit/test_codec.py @@ -419,6 +419,10 @@ self.STRUCT[-1].get_pres_dec = lambda v, _: bool(v['flag']) self.STRUCT[-1].get_pres_enc = lambda v: bool(v['flag']) + def check(self) -> None: + if not self['ver'] in (0, 1, 2): + raise ValueError('Unknown version %d' % self['ver']) + class Envelope(unittest.TestCase): def test_rest_octets(self): pdu = TestPDU(check_len=False) @@ -467,5 +471,18 @@ pdu.from_bytes(b'\x28\x00\x00\x01\xf0\xbe\xed') self.assertEqual(pdu.content, content) + def test_to_bytes_check(self): + pdu = TestPDU() + + pdu.content = { 'ver' : 8, 'flag' : 1, 'data' : b'', 'tail' : b'\xde\xbe' } + with self.assertRaises(ValueError): + pdu.to_bytes() + + def test_from_bytes_check(self): + pdu = TestPDU() + + with self.assertRaises(ValueError): + pdu.from_bytes(b'\xf0\x00\x00\x00') + if __name__ == '__main__': unittest.main() -- To view, visit https://gerrit.osmocom.org/c/osmocom-bb/+/23136 To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings Gerrit-Project: osmocom-bb Gerrit-Branch: master Gerrit-Change-Id: I8258504493cf85c401677866fe8474de2e49786b Gerrit-Change-Number: 23136 Gerrit-PatchSet: 1 Gerrit-Owner: fixeria <vyanitskiy at sysmocom.de> Gerrit-MessageType: newchange -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20210226/23d3215b/attachment.htm>