Skip to content

Commit

Permalink
Fixed TLV decoder issue (#22948)
Browse files Browse the repository at this point in the history
* Fixed TLV decoder issue

Fixed issue of the TLV decoder:

When giving tlv_bytes = b"\0256\001\0255\001&\000\031\346x\2077\001$\002\001$\003\006$\004\000\030(\002\030\030\030\030", the following errors occur:

Traceback (most recent call last):
  File "/home/chingtzuchen/exp/tlv2.py", line 743, in <module>
    out = reader.get()
  File "/home/chingtzuchen/exp/tlv2.py", line 459, in get
    self._get(self._tlv, self._decodings, out)
  File "/home/chingtzuchen/exp/tlv2.py", line 674, in _get
    self._decodeVal(tlv, decoding)
  File "/home/chingtzuchen/exp/tlv2.py", line 574, in _decodeVal
    self._get(tlv, decoding["Structure"], decoding["value"])
  File "/home/chingtzuchen/exp/tlv2.py", line 674, in _get
    self._decodeVal(tlv, decoding)
  File "/home/chingtzuchen/exp/tlv2.py", line 578, in _decodeVal
    self._get(tlv, decoding["Array"], decoding["value"])
  File "/home/chingtzuchen/exp/tlv2.py", line 674, in _get
    self._decodeVal(tlv, decoding)
  File "/home/chingtzuchen/exp/tlv2.py", line 574, in _decodeVal
    self._get(tlv, decoding["Structure"], decoding["value"])
  File "/home/chingtzuchen/exp/tlv2.py", line 674, in _get
    self._decodeVal(tlv, decoding)
  File "/home/chingtzuchen/exp/tlv2.py", line 574, in _decodeVal
    self._get(tlv, decoding["Structure"], decoding["value"])
  File "/home/chingtzuchen/exp/tlv2.py", line 674, in _get
    self._decodeVal(tlv, decoding)
  File "/home/chingtzuchen/exp/tlv2.py", line 582, in _decodeVal
    self._get(tlv, decoding["Path"], decoding["value"])
  File "/home/chingtzuchen/exp/tlv2.py", line 685, in _get
    out[decoding["tag"]] = decoding["value"]
IndexError: list assignment index out of range

Added condition and made it more clean to fix the issue, after this fix the bytes can be decoded:

{'Any': {1: [{1: {'Any': 2272847385, 1: [1, 6, 0], 2: False}}]}}

* Update __init__.py

* Add unit test case

Add unit test case for structure TLV bytes input

Test result:

```
$ python test_tlv.py 
.....
----------------------------------------------------------------------
Ran 5 tests in 0.000s

OK
```

* Update test_tlv.py

Update style
  • Loading branch information
falay authored and pull[bot] committed Mar 10, 2023
1 parent fbb7639 commit 602eb87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/controller/python/chip/tlv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,13 +680,11 @@ def _get(self, tlv, decodings, out):
if "profileTag" in list(decoding.keys()):
out[decoding["profileTag"]] = decoding["value"]
elif "tag" in list(decoding.keys()):
if decoding["tag"] is not None:
out[decoding["tag"]] = decoding["value"]
if isinstance(out, Mapping):
tag = decoding["tag"] if decoding["tag"] is not None else "Any"
out[tag] = decoding["value"]
else:
if isinstance(out, Mapping):
out["Any"] = decoding["value"]
elif isinstance(out, Sequence):
out.append(decoding["value"])
out.append(decoding["value"])
else:
raise ValueError("Attempt to decode unsupported TLV tag")

Expand Down
12 changes: 12 additions & 0 deletions src/controller/python/test/unit_tests/test_tlv.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,18 @@ def test_uint(self):
self._read_case([0b00000101, 0xab, 0xaa], tlvUint(0xaaab))
self._read_case([0b00000100, 0xab], tlvUint(0xab))

def test_structure(self):
test_cases = [
(b'\x15\x36\x01\x15\x35\x01\x26\x00\xBF\xA2\x55\x16\x37\x01\x24\x02\x00\x24\x03\x28\x24\x04\x00\x18\x24\x02\x01\x18\x18\x18\x18',
{1: [{1: {0: 374710975, 1: [0, 40, 0], 2: 1}}]}),
(b'\x156\x01\x155\x01&\x00\xBF\xA2U\x167\x01$\x02\x00$\x03($\x04\x01\x18,\x02\x18Nordic Semiconductor ASA\x18\x18\x18\x18',
{1: [{1: {0: 374710975, 1: [0, 40, 1], 2: 'Nordic Semiconductor ASA'}}]}),
(b"\0256\001\0255\001&\000\031\346x\2077\001$\002\001$\003\006$\004\000\030(\002\030\030\030\030",
{1: [{1: {0: 2272847385, 1: [1, 6, 0], 2: False}}]})
]
for tlv_bytes, answer in test_cases:
self._read_case(tlv_bytes, answer)


if __name__ == '__main__':
unittest.main()

0 comments on commit 602eb87

Please sign in to comment.