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
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' %