Add accessor helpers for mbedtls_test_info

Step one of being able to control access to mbedtls_test_info with
a mutex.

Signed-off-by: Paul Elliott <paul.elliott@arm.com>
This commit is contained in:
Paul Elliott 2023-10-27 18:41:02 +01:00
parent a021d63bf7
commit 4580d4d829
7 changed files with 159 additions and 33 deletions

View file

@ -427,7 +427,7 @@ int test_hooks_failure_detected(void)
mbedtls_test_mutex_usage_check();
#endif
if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS) {
if (mbedtls_test_get_result() != MBEDTLS_TEST_RESULT_SUCCESS) {
return 1;
}
return 0;

View file

@ -343,9 +343,11 @@ int main(int argc, char *argv[])
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
mbedtls_test_mutex_usage_check();
#endif
int result = (int) mbedtls_test_get_result();
mbedtls_printf("Running metatest %s... done, result=%d\n",
argv[1], (int) mbedtls_test_info.result);
mbedtls_exit(mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS ?
argv[1], result);
mbedtls_exit(result == MBEDTLS_TEST_RESULT_SUCCESS ?
MBEDTLS_EXIT_SUCCESS :
MBEDTLS_EXIT_FAILURE);
}

View file

@ -74,7 +74,81 @@ typedef struct {
#endif
}
mbedtls_test_info_t;
extern mbedtls_test_info_t mbedtls_test_info;
/**
* \brief Get the current test result status
*
* \return The current test result status
*/
mbedtls_test_result_t mbedtls_test_get_result(void);
/**
* \brief Get the current test name/description
*
* \return The current test name/description
*/
const char *mbedtls_test_get_test(void);
/**
* \brief Get the current test filename
*
* \return The current test filename
*/
const char *mbedtls_get_test_filename(void);
/**
* \brief Get the current test file line number (for failure / skip)
*
* \return The current test file line number (for failure / skip)
*/
int mbedtls_test_get_line_no(void);
/**
* \brief Increment the current test step.
*/
void mbedtls_test_increment_step(void);
/**
* \brief Get the current test step
*
* \return The current test step
*/
unsigned long mbedtls_test_get_step(void);
/**
* \brief Get the current test line buffer 1
*
* \return The current test line buffer 1
*/
const char *mbedtls_test_get_line1(void);
/**
* \brief Get the current test line buffer 2
*
* \return The current test line buffer 2
*/
const char *mbedtls_test_get_line2(void);
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
/**
* \brief Get the current mutex usage error message
*
* \return The current mutex error message (may be NULL if no error)
*/
const char *mbedtls_test_get_mutex_usage_error(void);
/**
* \brief Set the current mutex usage error message
*
* \note This will only set the mutex error message if one has not
* already been set, or if we are clearing the message (msg is
* NULL)
*
* \param msg Error message to set (can be NULL to clear)
*/
void mbedtls_test_set_mutex_usage_error(const char *msg);
#endif
int mbedtls_test_platform_setup(void);
void mbedtls_test_platform_teardown(void);

View file

@ -22,6 +22,61 @@ static mbedtls_platform_context platform_ctx;
mbedtls_test_info_t mbedtls_test_info;
/*----------------------------------------------------------------------------*/
/* Mbedtls Test Info accessors */
mbedtls_test_result_t mbedtls_test_get_result(void)
{
return mbedtls_test_info.result;
}
const char *mbedtls_test_get_test(void)
{
return mbedtls_test_info.test;
}
const char *mbedtls_get_test_filename(void)
{
return mbedtls_test_info.filename;
}
int mbedtls_test_get_line_no(void)
{
return mbedtls_test_info.line_no;
}
void mbedtls_test_increment_step(void)
{
++mbedtls_test_info.step;
}
unsigned long mbedtls_test_get_step(void)
{
return mbedtls_test_info.step;
}
const char *mbedtls_test_get_line1(void)
{
return mbedtls_test_info.line1;
}
const char *mbedtls_test_get_line2(void)
{
return mbedtls_test_info.line2;
}
#if defined(MBEDTLS_TEST_MUTEX_USAGE)
const char *mbedtls_test_get_mutex_usage_error(void)
{
return mbedtls_test_info.mutex_usage_error;
}
void mbedtls_test_set_mutex_usage_error(const char *msg)
{
if (mbedtls_test_info.mutex_usage_error == NULL || msg == NULL) {
mbedtls_test_info.mutex_usage_error = msg;
}
}
#endif // #if defined(MBEDTLS_TEST_MUTEX_USAGE)
/*----------------------------------------------------------------------------*/
/* Helper Functions */

View file

@ -109,9 +109,7 @@ static void mbedtls_test_mutex_usage_error(mbedtls_threading_mutex_t *mutex,
{
(void) mutex;
if (mbedtls_test_info.mutex_usage_error == NULL) {
mbedtls_test_info.mutex_usage_error = msg;
}
mbedtls_test_set_mutex_usage_error(msg);
mbedtls_fprintf(stdout, "[mutex: %s] ", msg);
/* Don't mark the test as failed yet. This way, if the test fails later
* for a functional reason, the test framework will report the message
@ -233,17 +231,15 @@ void mbedtls_test_mutex_usage_check(void)
* negative number means a missing init somewhere. */
mbedtls_fprintf(stdout, "[mutex: %d leaked] ", live_mutexes);
live_mutexes = 0;
if (mbedtls_test_info.mutex_usage_error == NULL) {
mbedtls_test_info.mutex_usage_error = "missing free";
}
mbedtls_test_set_mutex_usage_error("missing free");
}
if (mbedtls_test_info.mutex_usage_error != NULL &&
mbedtls_test_info.result != MBEDTLS_TEST_RESULT_FAILED) {
if (mbedtls_test_get_mutex_usage_error() != NULL &&
mbedtls_test_get_result() != MBEDTLS_TEST_RESULT_FAILED) {
/* Functionally, the test passed. But there was a mutex usage error,
* so mark the test as failed after all. */
mbedtls_test_fail("Mutex usage error", __LINE__, __FILE__);
}
mbedtls_test_info.mutex_usage_error = NULL;
mbedtls_test_set_mutex_usage_error(NULL);
}
void mbedtls_test_mutex_usage_end(void)

View file

@ -371,14 +371,12 @@ static void write_outcome_entry(FILE *outcome_file,
* \param missing_unmet_dependencies Non-zero if there was a problem tracking
* all unmet dependencies, 0 otherwise.
* \param ret The test dispatch status (DISPATCH_xxx).
* \param info A pointer to the test info structure.
*/
static void write_outcome_result(FILE *outcome_file,
size_t unmet_dep_count,
int unmet_dependencies[],
int missing_unmet_dependencies,
int ret,
const mbedtls_test_info_t *info)
int ret)
{
if (outcome_file == NULL) {
return;
@ -401,7 +399,7 @@ static void write_outcome_result(FILE *outcome_file,
}
break;
}
switch (info->result) {
switch (mbedtls_test_get_result()) {
case MBEDTLS_TEST_RESULT_SUCCESS:
mbedtls_fprintf(outcome_file, "PASS;");
break;
@ -410,8 +408,9 @@ static void write_outcome_result(FILE *outcome_file,
break;
default:
mbedtls_fprintf(outcome_file, "FAIL;%s:%d:%s",
info->filename, info->line_no,
info->test);
mbedtls_get_test_filename(),
mbedtls_test_get_line_no(),
mbedtls_test_get_test());
break;
}
break;
@ -614,7 +613,7 @@ int execute_tests(int argc, const char **argv)
break;
}
mbedtls_fprintf(stdout, "%s%.66s",
mbedtls_test_info.result == MBEDTLS_TEST_RESULT_FAILED ?
mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_FAILED ?
"\n" : "", buf);
mbedtls_fprintf(stdout, " ");
for (i = strlen(buf) + 1; i < 67; i++) {
@ -690,7 +689,7 @@ int execute_tests(int argc, const char **argv)
write_outcome_result(outcome_file,
unmet_dep_count, unmet_dependencies,
missing_unmet_dependencies,
ret, &mbedtls_test_info);
ret);
if (unmet_dep_count > 0 || ret == DISPATCH_UNSUPPORTED_SUITE) {
total_skipped++;
mbedtls_fprintf(stdout, "----");
@ -715,30 +714,30 @@ int execute_tests(int argc, const char **argv)
unmet_dep_count = 0;
missing_unmet_dependencies = 0;
} else if (ret == DISPATCH_TEST_SUCCESS) {
if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SUCCESS) {
if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_SUCCESS) {
mbedtls_fprintf(stdout, "PASS\n");
} else if (mbedtls_test_info.result == MBEDTLS_TEST_RESULT_SKIPPED) {
} else if (mbedtls_test_get_result() == MBEDTLS_TEST_RESULT_SKIPPED) {
mbedtls_fprintf(stdout, "----\n");
total_skipped++;
} else {
total_errors++;
mbedtls_fprintf(stdout, "FAILED\n");
mbedtls_fprintf(stdout, " %s\n at ",
mbedtls_test_info.test);
if (mbedtls_test_info.step != (unsigned long) (-1)) {
mbedtls_test_get_test());
if (mbedtls_test_get_step() != (unsigned long) (-1)) {
mbedtls_fprintf(stdout, "step %lu, ",
mbedtls_test_info.step);
mbedtls_test_get_step());
}
mbedtls_fprintf(stdout, "line %d, %s",
mbedtls_test_info.line_no,
mbedtls_test_info.filename);
if (mbedtls_test_info.line1[0] != 0) {
mbedtls_test_get_line_no(),
mbedtls_get_test_filename());
if (mbedtls_test_get_line1()[0] != 0) {
mbedtls_fprintf(stdout, "\n %s",
mbedtls_test_info.line1);
mbedtls_test_get_line1());
}
if (mbedtls_test_info.line2[0] != 0) {
if (mbedtls_test_get_line2()[0] != 0) {
mbedtls_fprintf(stdout, "\n %s",
mbedtls_test_info.line2);
mbedtls_test_get_line2());
}
}
fflush(stdout);

View file

@ -31,7 +31,7 @@ static int check_dhm_param_output(const mbedtls_mpi *expected,
int ok = 0;
mbedtls_mpi_init(&actual);
++mbedtls_test_info.step;
mbedtls_test_increment_step();
TEST_ASSERT(size >= *offset + 2);
n = (buffer[*offset] << 8) | buffer[*offset + 1];