-
-
Notifications
You must be signed in to change notification settings - Fork 683
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core/prodtest): Introduce unit test extension into prodtest [no …
…changelog]
- Loading branch information
kopecdav
committed
Mar 7, 2025
1 parent
486bbca
commit 244f169
Showing
9 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* This file is part of the Trezor project, https://trezor.io/ | ||
* | ||
* Copyright (c) SatoshiLabs | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <rust_ui_prodtest.h> | ||
#include <trezor_model.h> | ||
#include <trezor_rtl.h> | ||
|
||
#include <rtl/cli.h> | ||
#include <rtl/unit_test.h> | ||
|
||
static void prodtest_unit_test_list(cli_t* cli) { | ||
if (cli_arg_count(cli) > 0) { | ||
cli_error_arg_count(cli); | ||
return; | ||
} | ||
|
||
cli_trace(cli, "List of all registered unit tests:"); | ||
|
||
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, | ||
ut->unit_test_array[i].info); | ||
} | ||
|
||
cli_ok(cli, ""); | ||
} | ||
|
||
static void prodtest_unit_test_run(cli_t* cli) { | ||
if (cli_arg_count(cli) > 0) { | ||
cli_error_arg_count(cli); | ||
return; | ||
} | ||
|
||
bool ut_passed = true; | ||
|
||
cli_trace(cli, "Running all unit tests..."); | ||
|
||
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); | ||
|
||
cli_trace(cli, "%s: %s", ut->unit_test_array[i].name, | ||
test_result == UT_PASSED ? "PASSED" : "FAILED"); | ||
|
||
if (test_result == UT_FAILED) { | ||
ut_passed = false; | ||
} | ||
} | ||
|
||
if (ut_passed) { | ||
cli_ok(cli, ""); | ||
} else { | ||
cli_error(cli, CLI_ERROR, "Some of the unit test failed"); | ||
} | ||
} | ||
|
||
// clang-format off | ||
|
||
PRODTEST_CLI_CMD( | ||
.name = "unit-test-list", | ||
.func = prodtest_unit_test_list, | ||
.info = "Print list of all registered unit tests", | ||
.args = "" | ||
) | ||
|
||
PRODTEST_CLI_CMD( | ||
.name = "unit-test-run", | ||
.func = prodtest_unit_test_run, | ||
.info = "Run all registerd unit tests", | ||
.args = "" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* This file is part of the Trezor project, https://trezor.io/ | ||
* | ||
* Copyright (c) SatoshiLabs | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <trezor_types.h> | ||
|
||
#include <rtl/cli.h> | ||
|
||
typedef enum { | ||
UT_PASSED = 0, | ||
UT_FAILED, | ||
} ut_status_t; | ||
|
||
// unit test handler routine prototype | ||
typedef ut_status_t (*ut_handler_t)(cli_t* ut); | ||
|
||
// Structure describing the registration record for a CLI unit test handler | ||
typedef struct { | ||
// Unit test name | ||
const char* name; | ||
// Unit test handler | ||
ut_handler_t func; | ||
// Single line unit test description | ||
const char* info; | ||
} unit_test_record_t; | ||
|
||
#define CONCAT_UT_INDIRECT(x, y) x##y | ||
#define CONCAT_UT(x, y) CONCAT_INDIRECT(x, y) | ||
|
||
// Register a unit test by placing its registration record | ||
// into a specially designated linker script section | ||
#define REGISTER_UNIT_TEST(...) \ | ||
__attribute__((used, section(".unit_test"))) static const unit_test_record_t \ | ||
CONCAT_UT(_ut_handler, __COUNTER__) = {__VA_ARGS__}; | ||
|
||
typedef struct { | ||
// Registered unit test record handlers | ||
const unit_test_record_t* unit_test_array; | ||
size_t unit_test_count; | ||
} unit_test_t; | ||
|
||
// Returns the pointer to unit_test_t structure with all registered records | ||
unit_test_t* unit_test_get_records(void); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* This file is part of the Trezor project, https://trezor.io/ | ||
* | ||
* Copyright (c) SatoshiLabs | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include <trezor_types.h> | ||
|
||
#include <rtl/unit_test.h> | ||
|
||
extern unit_test_record_t _unit_test_section_start; | ||
extern unit_test_record_t _unit_test_section_end; | ||
|
||
unit_test_t g_ut = {0}; | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters