From c60f06db1b044659e91c769eb616a403f8ebfa53 Mon Sep 17 00:00:00 2001 From: Bob Long Date: Tue, 7 Jan 2025 16:56:33 +1100 Subject: [PATCH] CI: add cx_apj_tool_unittests.py --- .github/workflows/test_scripts.yml | 1 + Tools/scripts/build_ci.sh | 5 + Tools/scripts/cx_apj_tool_unittests.py | 241 +++++++++++++++++++++++++ 3 files changed, 247 insertions(+) create mode 100644 Tools/scripts/cx_apj_tool_unittests.py diff --git a/.github/workflows/test_scripts.yml b/.github/workflows/test_scripts.yml index ab1f10bb3e..840e6bca59 100644 --- a/.github/workflows/test_scripts.yml +++ b/.github/workflows/test_scripts.yml @@ -20,6 +20,7 @@ jobs: astyle-cleanliness, validate_board_list, param_check_unittests, + cx_apj_tool_unittests, ] steps: # git checkout the PR diff --git a/Tools/scripts/build_ci.sh b/Tools/scripts/build_ci.sh index ff75b41a44..8c24ca8a42 100755 --- a/Tools/scripts/build_ci.sh +++ b/Tools/scripts/build_ci.sh @@ -502,6 +502,11 @@ for t in $CI_BUILD_TARGET; do continue fi + if [ "$t" == "cx_apj_tool_unittests" ]; then + python Tools/scripts/cx_apj_tool_unittests.py + continue + fi + if [[ -z ${CI_CRON_JOB+1} ]]; then echo "Starting waf build for board ${t}..." $waf configure --board "$t" \ diff --git a/Tools/scripts/cx_apj_tool_unittests.py b/Tools/scripts/cx_apj_tool_unittests.py new file mode 100644 index 0000000000..7dffaa582a --- /dev/null +++ b/Tools/scripts/cx_apj_tool_unittests.py @@ -0,0 +1,241 @@ +import unittest +from unittest.mock import patch, MagicMock +import struct + +# Import the functions and the script itself +from cx_apj_tool import replace_board_name, fix_app_descriptor, to_unsigned, __main__ as cx_apj_tool_main + + +class TestReplaceBoardName(unittest.TestCase): + def test_replace_valid(self): + image = b"This is the old_board_name and nothing else" + old_name = "old_board_name" + new_name = "new_name" + updated_image = replace_board_name(image, old_name, new_name) + self.assertEqual(updated_image, b"This is the new_name\x00\x00\x00\x00\x00\x00 and nothing else") + + def test_old_name_not_found(self): + image = b"This is the old_board_name and nothing else" + old_name = "missing_name" + new_name = "new_name" + with self.assertRaises(LookupError): + replace_board_name(image, old_name, new_name) + + def test_old_name_appears_multiple_times(self): + image = b"This is the old_board_name and old_board_name again" + old_name = "old_board_name" + new_name = "new_name" + with self.assertRaises(AssertionError): + replace_board_name(image, old_name, new_name) + + def test_new_name_too_long(self): + image = b"This is the old_board_name and nothing else" + old_name = "old_board_name" + new_name = "new_board_name_too_long" + with self.assertRaises(ValueError): + replace_board_name(image, old_name, new_name) + + +class TestFixAppDescriptor(unittest.TestCase): + def test_fix_valid_descriptor(self): + # Mock an image with a valid descriptor + descriptor = b'\x40\xa2\xe4\xf1\x64\x68\x91\x06' + githash = 0x12345678 + img_len = 64 + crc1 = 0x11111111 + crc2 = 0x22222222 + app_descriptor = struct.pack('