Skip to content

Commit

Permalink
fixup! refactor(core/prodtest): Refactor unit tests into separate rtl…
Browse files Browse the repository at this point in the history
… library [no changelog]
  • Loading branch information
kopecdav committed Mar 5, 2025
1 parent 07f80e6 commit 3722dc1
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 40 deletions.
4 changes: 2 additions & 2 deletions core/embed/projects/prodtest/cmd/prodtest_pmic.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,13 @@ PRODTEST_CLI_CMD(
.args = "[<count>] [<period>]"
);

PRODTEST_UNIT_TEST(
REGISTER_UNIT_TEST(
.name = "ut-pmic-init-deinit",
.func = ut_pmic_init_deinit,
.info = "Test PMIC driver initialization and deinitialization",
)

PRODTEST_UNIT_TEST(
REGISTER_UNIT_TEST(
.name = "ut-pmic-battery",
.func = ut_pmic_battery,
.info = "Test PMIC battery connection",
Expand Down
6 changes: 3 additions & 3 deletions core/embed/projects/prodtest/cmd/prodtest_unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static void prodtest_unit_test_list(cli_t* cli) {

cli_trace(cli, "List of all registered unit tests:");

ut_t* ut = ut_get_records_ptr();
unit_test_t* ut = unit_test_get_records();

for (size_t i = 0; i < ut->unit_test_count; i++) {
cli_trace(cli, " %s - %s ", ut->unit_test_array[i].name,
Expand All @@ -52,7 +52,7 @@ static void prodtest_unit_test_run(cli_t* cli) {

cli_trace(cli, "Running all unit tests...");

ut_t* ut = ut_get_records_ptr();
unit_test_t* ut = unit_test_get_records();

for (size_t i = 0; i < ut->unit_test_count; i++) {
ut_status_t test_result = ut->unit_test_array[i].func(cli);
Expand All @@ -68,7 +68,7 @@ static void prodtest_unit_test_run(cli_t* cli) {
if (ut_passed) {
cli_ok(cli, "");
} else {
cli_error(cli, CLI_ERROR, "Some of the unit tests Failed");
cli_error(cli, CLI_ERROR, "Some of the unit test failed");
}
}

Expand Down
6 changes: 0 additions & 6 deletions core/embed/projects/prodtest/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,11 @@ int main(void) {

extern cli_command_t _prodtest_cli_cmd_section_start;
extern cli_command_t _prodtest_cli_cmd_section_end;
extern unit_test_record_t _prodtest_unit_test_section_start;
extern unit_test_record_t _prodtest_unit_test_section_end;

cli_set_commands(
&g_cli, &_prodtest_cli_cmd_section_start,
&_prodtest_cli_cmd_section_end - &_prodtest_cli_cmd_section_start);

ut_set_records(
&_prodtest_unit_test_section_start,
&_prodtest_unit_test_section_end - &_prodtest_unit_test_section_start);

#ifdef USE_OPTIGA
optiga_init();
optiga_open_application();
Expand Down
19 changes: 6 additions & 13 deletions core/embed/rtl/inc/rtl/unit_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

#include <rtl/cli.h>

typedef struct ut ut_t;

typedef enum {
UT_PASSED = 0,
UT_FAILED,
Expand All @@ -48,20 +46,15 @@ typedef struct {

// Register a unit test by placing its registration record
// into a specially designated linker script section
#define PRODTEST_UNIT_TEST(...) \
__attribute__((used, \
section(".prodtest_ut"))) static const unit_test_record_t \
#define REGISTER_UNIT_TEST(...) \
__attribute__((used, section(".unit_test"))) static const unit_test_record_t \
CONCAT_UT(_ut_handler, __COUNTER__) = {__VA_ARGS__};

struct ut {
typedef struct {
// Registered unit test record handlers
const unit_test_record_t* unit_test_array;
size_t unit_test_count;
};

// Register unit test records to ut structure
void ut_set_records(const unit_test_record_t* unit_test_array,
size_t unit_test_count);
} unit_test_t;

// Returns the pointer to ut structure with all registered records
ut_t* ut_get_records_ptr();
// Returns the pointer to unit_test_t structure with all registered records
unit_test_t* unit_test_get_records(void);
15 changes: 8 additions & 7 deletions core/embed/rtl/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@

#include <rtl/unit_test.h>

ut_t g_ut = {0};
extern unit_test_record_t _unit_test_section_start;
extern unit_test_record_t _unit_test_section_end;

void ut_set_records(const unit_test_record_t* unit_test_array,
size_t unit_test_count) {
g_ut.unit_test_array = unit_test_array;
g_ut.unit_test_count = unit_test_count;
}
unit_test_t g_ut = {0};

ut_t* ut_get_records_ptr() { return &g_ut; }
unit_test_t* unit_test_get_records(void) {
g_ut.unit_test_array = &_unit_test_section_start;
g_ut.unit_test_count = &_unit_test_section_end - &_unit_test_section_start;
return &g_ut;
}
6 changes: 3 additions & 3 deletions core/embed/sys/linker/stm32f4/prodtest.ld
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ SECTIONS {
_prodtest_cli_cmd_section_end = .;
. = ALIGN(4);

_prodtest_unit_test_section_start = .;
KEEP(*(.prodtest_ut))
_prodtest_unit_test_section_end = .;
_unit_test_section_start = .;
KEEP(*(.unit_test))
_unit_test_section_end = .;

. = ALIGN(128K);
} >FLASH
Expand Down
6 changes: 3 additions & 3 deletions core/embed/sys/linker/stm32u58/prodtest.ld
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ SECTIONS {
_prodtest_cli_cmd_section_end = .;
. = ALIGN(4);

_prodtest_unit_test_section_start = .;
KEEP(*(.prodtest_ut))
_prodtest_unit_test_section_end = .;
_unit_test_section_start = .;
KEEP(*(.unit_test))
_unit_test_section_end = .;

. = ALIGN(512);
} >FLASH AT>FLASH
Expand Down
6 changes: 3 additions & 3 deletions core/embed/sys/linker/stm32u5g/prodtest.ld
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ SECTIONS {
_prodtest_cli_cmd_section_end = .;
. = ALIGN(4);

_prodtest_unit_test_section_start = .;
KEEP(*(.prodtest_ut))
_prodtest_unit_test_section_end = .;
_unit_test_section_start = .;
KEEP(*(.unit_test))
_unit_test_section_end = .;

. = ALIGN(512);
} >FLASH AT>FLASH
Expand Down

0 comments on commit 3722dc1

Please sign in to comment.