clean up the huge import list in these tests

Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
Brian S. Stephan 2024-04-12 18:53:39 -05:00
parent eb95c80815
commit 1f65f23a4f
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
1 changed files with 37 additions and 40 deletions

View File

@ -10,11 +10,8 @@ import unittest.mock as mock
import pytest
from decorator import decorator
import gp2040ce_bintools.builder as builder
from gp2040ce_bintools import get_config_pb2
from gp2040ce_bintools.builder import (FirmwareLengthError, combine_firmware_and_config,
concatenate_firmware_and_storage_files, get_gp2040ce_from_usb,
pad_binary_up_to_board_config, pad_binary_up_to_user_config,
replace_config_in_binary, write_new_config_to_filename, write_new_config_to_usb)
from gp2040ce_bintools.storage import (STORAGE_SIZE, get_board_storage_section, get_config, get_config_footer,
get_user_storage_section, serialize_config_with_footer)
@ -38,8 +35,8 @@ def test_concatenate_to_file(tmp_path):
tmp_file = os.path.join(tmp_path, 'concat.bin')
firmware_file = os.path.join(HERE, 'test-files', 'test-firmware.bin')
config_file = os.path.join(HERE, 'test-files', 'test-config.bin')
concatenate_firmware_and_storage_files(firmware_file, binary_user_config_filename=config_file,
combined_filename=tmp_file)
builder.concatenate_firmware_and_storage_files(firmware_file, binary_user_config_filename=config_file,
combined_filename=tmp_file)
with open(tmp_file, 'rb') as file:
content = file.read()
assert len(content) == 2 * 1024 * 1024
@ -50,8 +47,8 @@ def test_concatenate_board_config_to_file(tmp_path):
tmp_file = os.path.join(tmp_path, 'concat.bin')
firmware_file = os.path.join(HERE, 'test-files', 'test-firmware.bin')
config_file = os.path.join(HERE, 'test-files', 'test-config.bin')
concatenate_firmware_and_storage_files(firmware_file, binary_board_config_filename=config_file,
combined_filename=tmp_file)
builder.concatenate_firmware_and_storage_files(firmware_file, binary_board_config_filename=config_file,
combined_filename=tmp_file)
with open(tmp_file, 'rb') as file:
content = file.read()
assert len(content) == (2 * 1024 * 1024) - (16 * 1024)
@ -62,8 +59,8 @@ def test_concatenate_both_configs_to_file(tmp_path):
tmp_file = os.path.join(tmp_path, 'concat.bin')
firmware_file = os.path.join(HERE, 'test-files', 'test-firmware.bin')
config_file = os.path.join(HERE, 'test-files', 'test-config.bin')
concatenate_firmware_and_storage_files(firmware_file, binary_board_config_filename=config_file,
binary_user_config_filename=config_file, combined_filename=tmp_file)
builder.concatenate_firmware_and_storage_files(firmware_file, binary_board_config_filename=config_file,
binary_user_config_filename=config_file, combined_filename=tmp_file)
with open(tmp_file, 'rb') as file:
content = file.read()
assert len(content) == 2 * 1024 * 1024
@ -81,8 +78,8 @@ def test_concatenate_user_json_to_file(tmp_path):
tmp_file = os.path.join(tmp_path, 'concat.bin')
firmware_file = os.path.join(HERE, 'test-files', 'test-firmware.bin')
config_file = os.path.join(HERE, 'test-files', 'test-config.json')
concatenate_firmware_and_storage_files(firmware_file, json_user_config_filename=config_file,
combined_filename=tmp_file)
builder.concatenate_firmware_and_storage_files(firmware_file, json_user_config_filename=config_file,
combined_filename=tmp_file)
with open(tmp_file, 'rb') as file:
content = file.read()
assert len(content) == 2 * 1024 * 1024
@ -93,7 +90,7 @@ def test_concatenate_to_file_incomplete_args_is_error(tmp_path):
tmp_file = os.path.join(tmp_path, 'concat.bin')
firmware_file = os.path.join(HERE, 'test-files', 'test-firmware.bin')
with pytest.raises(ValueError):
concatenate_firmware_and_storage_files(firmware_file, combined_filename=tmp_file)
builder.concatenate_firmware_and_storage_files(firmware_file, combined_filename=tmp_file)
def test_concatenate_to_usb(tmp_path):
@ -103,8 +100,8 @@ def test_concatenate_to_usb(tmp_path):
end_out, end_in = mock.MagicMock(), mock.MagicMock()
with mock.patch('gp2040ce_bintools.builder.get_bootsel_endpoints', return_value=(end_out, end_in)):
with mock.patch('gp2040ce_bintools.builder.write') as mock_write:
concatenate_firmware_and_storage_files(firmware_file, binary_user_config_filename=config_file,
usb=True)
builder.concatenate_firmware_and_storage_files(firmware_file, binary_user_config_filename=config_file,
usb=True)
assert mock_write.call_args.args[2] == 0x10000000
assert len(mock_write.call_args.args[3]) == 2 * 1024 * 1024
@ -115,9 +112,9 @@ def test_concatenate_to_uf2(tmp_path, firmware_binary, config_binary):
tmp_file = os.path.join(tmp_path, 'concat.uf2')
firmware_file = os.path.join(HERE, 'test-files', 'test-firmware.bin')
config_file = os.path.join(HERE, 'test-files', 'test-config.bin')
concatenate_firmware_and_storage_files(firmware_file, binary_board_config_filename=config_file,
binary_user_config_filename=config_file,
combined_filename=tmp_file)
builder.concatenate_firmware_and_storage_files(firmware_file, binary_board_config_filename=config_file,
binary_user_config_filename=config_file,
combined_filename=tmp_file)
with open(tmp_file, 'rb') as file:
content = file.read()
# size of the file should be 2x the binary version, and the binary is 2 MB
@ -129,8 +126,8 @@ def test_concatenate_to_uf2_board_only(tmp_path, firmware_binary, config_binary)
tmp_file = os.path.join(tmp_path, 'concat.uf2')
firmware_file = os.path.join(HERE, 'test-files', 'test-firmware.bin')
config_file = os.path.join(HERE, 'test-files', 'test-config.bin')
concatenate_firmware_and_storage_files(firmware_file, binary_board_config_filename=config_file,
combined_filename=tmp_file)
builder.concatenate_firmware_and_storage_files(firmware_file, binary_board_config_filename=config_file,
combined_filename=tmp_file)
with open(tmp_file, 'rb') as file:
content = file.read()
# size of the file should be 2x the binary version (minus user config space), and the binary is 2 MB - 16KB
@ -139,25 +136,25 @@ def test_concatenate_to_uf2_board_only(tmp_path, firmware_binary, config_binary)
def test_padding_firmware(firmware_binary):
"""Test that firmware is padded to the expected size."""
padded = pad_binary_up_to_user_config(firmware_binary)
padded = builder.pad_binary_up_to_user_config(firmware_binary)
assert len(padded) == 2080768
def test_padding_firmware_can_truncate():
"""Test that firmware is padded to the expected size."""
padded = pad_binary_up_to_user_config(bytearray(b'\x00' * 4 * 1024 * 1024), or_truncate=True)
padded = builder.pad_binary_up_to_user_config(bytearray(b'\x00' * 4 * 1024 * 1024), or_truncate=True)
assert len(padded) == 2080768
def test_padding_firmware_to_board(firmware_binary):
"""Test that firmware is padded to the expected size."""
padded = pad_binary_up_to_board_config(firmware_binary)
padded = builder.pad_binary_up_to_board_config(firmware_binary)
assert len(padded) == 2080768 - (16 * 1024)
def test_firmware_plus_storage_section(firmware_binary, storage_dump):
"""Test that combining firmware and storage produces a valid combined binary."""
whole_board = combine_firmware_and_config(firmware_binary, None, storage_dump)
whole_board = builder.combine_firmware_and_config(firmware_binary, None, storage_dump)
# if this is valid, we should be able to find the storage and footer again
storage = get_user_storage_section(whole_board)
footer_size, _, _ = get_config_footer(storage)
@ -166,7 +163,7 @@ def test_firmware_plus_storage_section(firmware_binary, storage_dump):
def test_firmware_plus_user_config_binary(firmware_binary, config_binary):
"""Test that combining firmware and user config produces a valid combined binary."""
whole_board = combine_firmware_and_config(firmware_binary, None, config_binary)
whole_board = builder.combine_firmware_and_config(firmware_binary, None, config_binary)
# if this is valid, we should be able to find the storage and footer again
storage = get_user_storage_section(whole_board)
footer_size, _, _ = get_config_footer(storage)
@ -175,8 +172,8 @@ def test_firmware_plus_user_config_binary(firmware_binary, config_binary):
def test_chunky_firmware_plus_user_config_binary(config_binary):
"""Test that combining giant firmware and storage produces a valid combined binary."""
whole_board = combine_firmware_and_config(bytearray(b'\x00' * 4 * 1024 * 1024), None, config_binary,
replace_extra=True)
whole_board = builder.combine_firmware_and_config(bytearray(b'\x00' * 4 * 1024 * 1024), None, config_binary,
replace_extra=True)
# if this is valid, we should be able to find the storage and footer again
storage = get_user_storage_section(whole_board)
footer_size, _, _ = get_config_footer(storage)
@ -185,7 +182,7 @@ def test_chunky_firmware_plus_user_config_binary(config_binary):
def test_firmware_plus_board_config_binary(firmware_binary, config_binary):
"""Test that combining firmware and board config produces a valid combined binary."""
almost_whole_board = combine_firmware_and_config(firmware_binary, config_binary, None)
almost_whole_board = builder.combine_firmware_and_config(firmware_binary, config_binary, None)
assert len(almost_whole_board) == (2 * 1024 * 1024) - (16 * 1024)
# if this is valid, we should be able to find the storage and footer again
storage = get_board_storage_section(almost_whole_board)
@ -195,7 +192,7 @@ def test_firmware_plus_board_config_binary(firmware_binary, config_binary):
def test_firmware_plus_board_and_user_config_binary(firmware_binary, config_binary):
"""Test that combining firmware and both board and user configs produces a valid combined binary."""
whole_board = combine_firmware_and_config(firmware_binary, config_binary, config_binary)
whole_board = builder.combine_firmware_and_config(firmware_binary, config_binary, config_binary)
assert len(whole_board) == 2 * 1024 * 1024
# if this is valid, we should be able to find the storage and footer again
storage = get_board_storage_section(whole_board)
@ -209,12 +206,12 @@ def test_firmware_plus_board_and_user_config_binary(firmware_binary, config_bina
def test_combine_must_get_at_least_one_config(firmware_binary):
"""Test that we error if we are asked to combine with nothing to combine."""
with pytest.raises(ValueError):
combine_firmware_and_config(firmware_binary, None, None)
builder.combine_firmware_and_config(firmware_binary, None, None)
def test_replace_config_in_binary(config_binary):
"""Test that a config binary is placed in the storage location of a source binary to overwrite."""
whole_board = replace_config_in_binary(bytearray(b'\x00' * 3 * 1024 * 1024), config_binary)
whole_board = builder.replace_config_in_binary(bytearray(b'\x00' * 3 * 1024 * 1024), config_binary)
assert len(whole_board) == 3 * 1024 * 1024
# if this is valid, we should be able to find the storage and footer again
storage = get_user_storage_section(whole_board)
@ -224,7 +221,7 @@ def test_replace_config_in_binary(config_binary):
def test_replace_config_in_binary_not_big_enough(config_binary):
"""Test that a config binary is placed in the storage location of a source binary to pad."""
whole_board = replace_config_in_binary(bytearray(b'\x00' * 1 * 1024 * 1024), config_binary)
whole_board = builder.replace_config_in_binary(bytearray(b'\x00' * 1 * 1024 * 1024), config_binary)
assert len(whole_board) == 2 * 1024 * 1024
# if this is valid, we should be able to find the storage and footer again
storage = get_user_storage_section(whole_board)
@ -234,8 +231,8 @@ def test_replace_config_in_binary_not_big_enough(config_binary):
def test_padding_firmware_too_big(firmware_binary):
"""Test that firmware is padded to the expected size."""
with pytest.raises(FirmwareLengthError):
_ = pad_binary_up_to_user_config(firmware_binary + firmware_binary + firmware_binary)
with pytest.raises(builder.FirmwareLengthError):
_ = builder.pad_binary_up_to_user_config(firmware_binary + firmware_binary + firmware_binary)
@with_pb2s
@ -251,7 +248,7 @@ def test_write_new_config_to_whole_board(whole_board_dump, tmp_path):
config = get_config(get_user_storage_section(board_dump))
assert config.boardVersion == 'v0.7.5'
config.boardVersion = 'v0.7.5-COOL'
write_new_config_to_filename(config, tmp_file, inject=True)
builder.write_new_config_to_filename(config, tmp_file, inject=True)
# read new file
with open(tmp_file, 'rb') as file:
@ -271,7 +268,7 @@ def test_write_new_config_to_firmware(firmware_binary, tmp_path):
config_pb2 = get_config_pb2()
config = config_pb2.Config()
config.boardVersion = 'v0.7.5-COOL'
write_new_config_to_filename(config, tmp_file, inject=True)
builder.write_new_config_to_filename(config, tmp_file, inject=True)
# read new file
with open(tmp_file, 'rb') as file:
@ -288,7 +285,7 @@ def test_write_new_config_to_config_bin(firmware_binary, tmp_path):
config_pb2 = get_config_pb2()
config = config_pb2.Config()
config.boardVersion = 'v0.7.5-COOL'
write_new_config_to_filename(config, tmp_file)
builder.write_new_config_to_filename(config, tmp_file)
# read new file
with open(tmp_file, 'rb') as file:
@ -306,7 +303,7 @@ def test_write_new_config_to_config_uf2(firmware_binary, tmp_path):
config_pb2 = get_config_pb2()
config = config_pb2.Config()
config.boardVersion = 'v0.7.5-COOL'
write_new_config_to_filename(config, tmp_file)
builder.write_new_config_to_filename(config, tmp_file)
# read new file
with open(tmp_file, 'rb') as file:
@ -323,7 +320,7 @@ def test_write_new_config_to_usb(config_binary):
serialized = serialize_config_with_footer(config)
end_out, end_in = mock.MagicMock(), mock.MagicMock()
with mock.patch('gp2040ce_bintools.builder.write') as mock_write:
write_new_config_to_usb(config, end_out, end_in)
builder.write_new_config_to_usb(config, end_out, end_in)
# check that it got padded
assert len(serialized) == 3321
@ -342,7 +339,7 @@ def test_get_gp2040ce_from_usb():
mock_in = mock.MagicMock()
with mock.patch('gp2040ce_bintools.builder.get_bootsel_endpoints', return_value=(mock_out, mock_in)) as mock_get:
with mock.patch('gp2040ce_bintools.builder.read') as mock_read:
config, _, _ = get_gp2040ce_from_usb()
config, _, _ = builder.get_gp2040ce_from_usb()
mock_get.assert_called_once()
mock_read.assert_called_with(mock_out, mock_in, 0x10000000, 2 * 1024 * 1024)