laforge submitted this change.

View Change


Approvals: fixeria: Looks good to me, but someone else must approve Jenkins Builder: Verified laforge: Looks good to me, approved
tlv: Fix IE.from_dict() method

The existing IE.from_dict() method *supposedly* accepts a dict as
input value, but it actually expects the raw decoded value, unless it is
a nested IE. This is inconsistent in various ways, and results in a bug
visible at a higher layer, such as files like EF.{DOMAIN,IMPI,IMPU},
which are transparent files containing a single BER-TLV IE.

Decoding such files worked, but re-encoding them did not, due to the
fact that we'd pass a dict to the from_dict method, which then gets
assigned to self.decoded and further passed along to any later actual
encoder function like to_bytes or to_tlv. In that instance, the dict
might be handed to a self._construct which has no idea how to process
the dict, as it expects the raw decoded value.

Change-Id: I3dd5204510e5c32ef1c4a999258d87cb3f1df8c8
Closes: OS#6073
Related: OS#6072
---
M pySim/tlv.py
1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/pySim/tlv.py b/pySim/tlv.py
index a5baa23..bd95505 100644
--- a/pySim/tlv.py
+++ b/pySim/tlv.py
@@ -161,7 +161,10 @@
self.children = self.nested_collection.from_dict(decoded)
else:
self.children = []
- self.decoded = decoded
+ expected_key_name = camel_to_snake(type(self).__name__)
+ if not expected_key_name in decoded:
+ raise ValueError("Dict %s doesn't contain expected key %s" % (decoded, expected_key_name))
+ self.decoded = decoded[expected_key_name]

def is_constructed(self):
"""Is this IE constructed by further nested IEs?"""
@@ -388,7 +391,7 @@
if k in self.members_by_name:
cls = self.members_by_name[k]
inst = cls()
- inst.from_dict(i[k])
+ inst.from_dict({k: i[k]})
res.append(inst)
else:
raise ValueError('%s: Unknown TLV Class %s in %s; expected %s' %

To view, visit change 33451. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pysim
Gerrit-Branch: master
Gerrit-Change-Id: I3dd5204510e5c32ef1c4a999258d87cb3f1df8c8
Gerrit-Change-Number: 33451
Gerrit-PatchSet: 2
Gerrit-Owner: laforge <laforge@osmocom.org>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <vyanitskiy@sysmocom.de>
Gerrit-Reviewer: laforge <laforge@osmocom.org>
Gerrit-MessageType: merged