TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
test_framework.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2019, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "test_framework.h"
9 
10 #include <assert.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 
15 static void test_failed(const struct test_result_t *ret)
16 {
18  if (ret->info_msg != 0) {
19  TEST_LOG(" %s", ret->info_msg);
20  if (ret->filename != 0) {
21  TEST_LOG(" (Failed at %s:%d)\r\n", ret->filename, ret->line);
22  }
23  } else {
24  if (ret->filename != 0) {
25  TEST_LOG(" Failed at %s:%d\r\n", ret->filename, ret->line);
26  }
27  }
28 
29  TEST_LOG(" TEST FAILED!\r\n");
30 }
31 
32 static void print_error(const char *err_msg)
33 {
35  TEST_LOG("Error ( %s )\r\n", err_msg);
36 }
37 
38 const char *test_err_to_str(enum test_suite_err_t err)
39 {
40  switch (err) {
42  return "TEST_SUITE_ERR_NO_ERROR";
44  return "TEST_SUITE_ERR_INVALID_DATA";
46  return "TEST_SUITE_ERR_INVALID_TEST_DATA";
48  return "TEST_SUITE_ERR_TEST_FAILED";
49  /* default: The default is not defined intentionally to force the
50  * compiler to check that all the enumeration values are
51  * covered in the switch.
52  */
53  }
54 }
55 
57  struct test_t *test_list, uint32_t size,
58  struct test_suite_t *p_ts)
59 {
60  if (p_ts == 0) {
61  print_error("TEST_SUITE_ERR_INVALID_DATA!");
63  }
64 
65  p_ts->name = name;
66  p_ts->test_list = test_list;
67  p_ts->list_size = size;
68 
70 }
71 
72 void set_test_failed(const char *info_msg, const char *filename, uint32_t line,
73  struct test_result_t *ret)
74 {
75  if (ret == 0) {
76  print_error("TEST_SUITE_ERR_INVALID_TEST_DATA!");
77  return;
78  }
79 
80  ret->val = TEST_FAILED;
81  ret->info_msg = info_msg;
82  ret->filename = filename;
83  ret->line = line;
84 }
85 
87 {
88  uint32_t failed_tests = 0;
89  uint32_t i;
90  struct test_t *p_test;
91 
92  if (test_suite == 0 || test_suite->freg == 0) {
93  print_error("TEST_SUITE_ERR_INVALID_DATA!");
95  }
96 
97  /* Sets test suite parameters */
98  test_suite->freg(test_suite);
99  if (test_suite->name == 0 || test_suite->list_size == 0) {
100  print_error("TEST_SUITE_ERR_INVALID_DATA!");
102  }
103 
105  TEST_LOG("Running Test Suite %s...\r\n", test_suite->name);
106 
107  /* Sets pointer to the first test */
108  p_test = test_suite->test_list;
109 
110  for (i = 0; i < test_suite->list_size; i++) {
111 
112  if (p_test->test == 0 || p_test->name == 0) {
113  print_error("TEST_SUITE_ERR_INVALID_TEST_DATA!");
115  }
116 
118  TEST_LOG("> Executing '%s' \r\n Description: '%s'\r\n",
119  p_test->name, p_test->desc);
120 
121  /* Sets the default value before the test */
122  p_test->ret.val = TEST_PASSED;
123 
124  /* Executes the test */
125  p_test->test(&p_test->ret);
126  if (p_test->ret.val == TEST_FAILED) {
127  test_failed(&p_test->ret);
128  failed_tests++;
129  } else {
131  TEST_LOG(" TEST PASSED!\r\n");
132  }
133 
134  /* Sets pointer to the next test */
135  p_test++;
136  }
137 
138 
139  if (failed_tests == 0) {
141  TEST_LOG("TESTSUITE PASSED!\r\n");
142  test_suite->val = TEST_PASSED;
143  } else {
145  TEST_LOG("TESTSUITE FAILED!\r\n");
147  TEST_LOG("Number of failed tests: %d of %d\r\n",
148  failed_tests, test_suite->list_size);
149  test_suite->val = TEST_FAILED;
150  }
151 
153 }
TEST_FUN *const test
enum test_suite_err_t run_testsuite(struct test_suite_t *test_suite)
Runs the given test suite.
uint32_t list_size
const char * info_msg
enum test_suite_err_t set_testsuite(const char *name, struct test_t *test_list, uint32_t size, struct test_suite_t *p_ts)
Sets test suite parameters.
void set_test_failed(const char *info_msg, const char *filename, uint32_t line, struct test_result_t *ret)
Sets test failure state and information in the test_result_t structure.
const char * filename
enum test_status_t val
const char * name
TESTSUITE_REG *const freg
struct test_t * test_list
const char * name
struct test_result_t ret
#define TEST_LOG(...)
enum test_status_t val
const char * test_err_to_str(enum test_suite_err_t err)
Translates the test suite error into a string.
void printf_set_color(enum serial_color_t color_id)
Sets the the text color in the serial port.
const char * desc
test_suite_err_t