def test_parse_silabs_gbl(): list(validators.parse_silabs_gbl(VALID_GBL_IMAGE)) image = create_gbl_image([(b"AAAA", b"test"), (b"BBBB", b"foo" * 20)]) header, tag1, tag2, checksum = validators.parse_silabs_gbl(image) assert header[0] == b"\xEB\x17\xA6\x03" and len(header[1]) == 8 assert tag1 == (b"AAAA", b"test") assert tag2 == (b"BBBB", b"foo" * 20) assert checksum[0] == b"\xFC\x04\x04\xFC" and len(checksum[1]) == 4 # Arbitrary padding is allowed parsed_image = [header, tag1, tag2, checksum] assert list(validators.parse_silabs_gbl(image + b"\x00")) == parsed_image assert list(validators.parse_silabs_gbl(image + b"\xAB\xCD\xEF")) == parsed_image # Normal truncated images are detected with pytest.raises(ValidationError): list(validators.parse_silabs_gbl(image[-10:])) # Structurally sound but truncated images are detected with pytest.raises(ValidationError): offset = image.index(b"test") bad_image = image[:offset - 8] list(validators.parse_silabs_gbl(bad_image)) # Corrupted images are detected with pytest.raises(ValidationError): corrupted_image = image.replace(b"foo", b"goo", 1) assert image != corrupted_image list(validators.parse_silabs_gbl(corrupted_image))
def create_gbl_image(tags): # All images start with an 8-byte header tags = [(b"\xEB\x17\xA6\x03", b"\x00\x00\x00\x03\x01\x01\x00\x00")] + tags assert all([len(tag) == 4 for tag, value in tags]) image = b"".join(tag + len(value).to_bytes(4, "little") + value for tag, value in tags) # And end with a checksum image += (b"\xFC\x04\x04\xFC" + b"\x04\x00\x00\x00" + zlib.crc32(image + b"\xFC\x04\x04\xFC" + b"\x04\x00\x00\x00").to_bytes(4, "little")) assert list(validators.parse_silabs_gbl(image)) return image