add some more UF2 tests and sanity checks

Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
Brian S. Stephan 2024-04-12 16:58:53 -05:00
parent 4a7203d969
commit e35d8dbf3d
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
3 changed files with 25 additions and 1 deletions

View File

@ -117,6 +117,10 @@ def convert_uf2_to_binary(uf2: bytearray) -> bytearray:
binary += content[0:bytes_]
old_uf2_addr = uf2_addr
# when this is all done we should have counted the expected number of blocks
if block_count != block_num + 1:
raise ValueError(f"not all expected blocks ({block_count}) were found, only got {block_num + 1}!")
return binary

View File

@ -3,7 +3,6 @@
SPDX-FileCopyrightText: © 2023 Brian S. Stephan <bss@incorporeal.org>
SPDX-License-Identifier: GPL-3.0-or-later
"""
import math
import os
import sys
import unittest.mock as mock

View File

@ -166,6 +166,27 @@ def test_convert_binary_to_uf2_to_binary(whole_board_with_board_config_dump):
assert whole_board_with_board_config_dump == binary
def test_malformed_uf2(whole_board_with_board_config_dump):
"""Check that we expect a properly-formed UF2."""
uf2 = storage.convert_binary_to_uf2(whole_board_with_board_config_dump)
# truncated UF2 --- byte mismatch
with pytest.raises(ValueError):
storage.convert_uf2_to_binary(uf2[:-4])
# truncated uf2 --- counter is wrong
with pytest.raises(ValueError):
storage.convert_uf2_to_binary(uf2[512:])
# truncated uf2 --- total count is wrong
with pytest.raises(ValueError):
storage.convert_uf2_to_binary(uf2[:-512])
# malformed UF2 --- counter jumps in the middle, suggests total blocks is wrong
with pytest.raises(ValueError):
storage.convert_uf2_to_binary(uf2 + uf2)
@with_pb2s
def test_serialize_config_with_footer(storage_dump, config_binary):
"""Test that reserializing a read in config matches the original.