blob: a5a89a5c89c5d7399a36765b919bf5c799f77824 [file] [log] [blame]
Jianliang Shenbde10ed2021-08-25 13:48:00 +08001#################################
2Adding TF-M Regression Test Suite
3#################################
4
5.. contents:: Table of Contents
6
7*************************************
8Introduction of TF-M regression tests
9*************************************
10
11TF-M regression tests test whether changes to TF-M features work as expected.
12This documentation focus on three parts in TF-M regression tests:
13
141. ``test suite``: A series of tests of a certain function.
152. ``test case``: A specific test instance in test suites.
163. ``test service``: An extra module works like secure partitions to support
17 related test suites.
18
19Source structure
20================
21
22TF-M tests source code are located in
23`tf-m-tests <https://git.trustedfirmware.org/TF-M/tf-m-tests.git/>`__.
24
25+----------------+-------------------------------------------------------------+
26| Folder name | Description |
27+================+=============================================================+
28| test/config | The CMAKE test configurations files. |
29+----------------+-------------------------------------------------------------+
30| test/framework | Source code for test framework code, managing test suites. |
31+----------------+-------------------------------------------------------------+
32| test/suites | Test suites divided into subdirectories. |
33+----------------+-------------------------------------------------------------+
34| test/services | Test services divided into subdirectories. |
35+----------------+-------------------------------------------------------------+
36
37Test configuration
38==================
39
40A test configuration controls whether one or multiple test suites are enabled.
41The doc :doc:`TF-M Build Instructions </docs/technical_references/instructions/tfm_build_instruction>`
42shows some test configurations which are already supported in current TF-M.
43An example usage of test configuration shows below.
44
45.. code-block:: bash
46
47 cmake -S . -B cmake_build -DTFM_PLATFORM=arm/mps2/an521 \
48 -DTFM_TOOLCHAIN_FILE=toolchain_GNUARM.cmake \
49 -DCMAKE_BUILD_TYPE=Release \
50 -DTFM_PSA_API=ON \
51 -DTEST_NS_ATTESTATION=ON
52
53With this command, only non-secure initial attestation test suite will be built.
54
55***********************
56Adding a new test suite
57***********************
58
59This section introduces how to add a new test suite and control its compilation
60with a test configuration in ``tf-m-tests`` repository.
61
62Source code
63===========
64
65The test suite example subdirectory named ``<test_name>`` is located under the path
66``tf-m-tests/test/suites``. If the new test suite includes both non-secure and
67secure parts, the source code shall be divided shared code and specific code.
68An example test suite folder can be organized as the figure below.
69
70.. code-block:: bash
71
72 .
73 ├── CMakeLists.txt
74 ├── <test_name>_tests_common.c
75 ├── non_secure
76 ├── <test_name>_ns_interface_testsuite.c
77 └── <test_name>_ns_tests.h
78 └── secure
79 ├── <test_name>_s_interface_testsuite.c
80 └── <test_name>_s_tests.h
81
82Creating test configurations
83============================
84
85Developers shall assign corresponding test configurations to control the test
86suites.
87
88Naming test configurations
89--------------------------
90
91The test configurations of example test suites are ``TEST_NS_<TEST_NAME>``
92in non-secure and ``TEST_S_<TEST_NAME>`` in secure.
93
94.. Note::
95 The test configurations must be named with the prefixes ``TEST_S_`` and
96 ``TEST_NS_``, for secure regression tests and non-secure regression tests
97 respectively. Otherwise, tf-m-tests build system may not recognize it.
98
99Setting test configurations
100---------------------------
101
102When the test configurations have dependences, the default value need to be set.
103The setting is performed in following four steps.
104
105#. Command line input: The configuration can be enabled or disabled by the
106 command ``-DTEST_NS_<TEST_NAME>=ON/OFF -DTEST_S_<TEST_NAME>=ON/OFF``, and
107 the value cannot be changed throughout the whole compilation of TF-M tests.
108
109#. ``tf-m-tests/config/set_config.cmake``: The test configurations shall be
110 OFF if its dependences are not supported. The dependences are probably
111 from:
112
113 #. TF-M partitions configurations like ``TFM_PARTITION_CRYPTO``,
114 ``TFM_PARTITION_INITIAL_ATTESTATION``, etc.
115 #. TF-M build mode configuration like ``TFM_PSA_API``.
116 #. TF-M other configurations like ``TFM_PARTITION_FIRMWARE_UPDATE``,
117 ``FORWARD_PROT_MSG``, etc.
118
119#. ``tf-m-tests/config/default_ns_test_config.cmake`` or
120 ``tf-m-tests/config/default_s_test_config.cmake``: It is required to give
121 the default value of the new test configuration in these two files when
122 ``TEST_NS`` or ``TEST_S`` is ON. The recommended value is ON unless the
123 single test's code or data size is very large.
124
125#. ``tf-m-tests/config/default_test_config.cmake``: It is required to give the
126 default value of the new test configuration in the file when both
127 ``TEST_NS`` and ``TEST_S`` are OFF. The default value must be OFF.
128
129.. Note::
130 The test configurations must be set as CACHE value in CMAKE files. The CACHE
131 set cannot replace the value from command line, see
132 `Set Cache Entry <https://cmake.org/cmake/help/latest/command/set.html#set-cache-entry>`__.
133
134Checking test configurations
135----------------------------
136
137The new test configurations must be checked by function ``tfm_invalid_config()``
138if they have any dependence. The value comes from command line may be wrong when
139the dependences are conflicting. In addition to the dependences quoted in
140``tf-m-tests/config/set_config.cmake``, some other test configurations may be
141necessary.
142
143Applicating test configurations
144===============================
145
146The mission of test configurations is to control the build. They are applied
147in ``test/suites/<test_name>/CMakeLists.txt`` like the example below.
148
149.. code-block:: cmake
150
151 cmake_policy(SET CMP0079 NEW)
152
153 if (NOT TEST_NS_<TEST_NAME> AND NOT TEST_S_<TEST_NAME>)
154 return()
155 endif()
156
157 ####################### Non Secure #########################################
158
159 if (TEST_NS_<TEST_NAME>)
160 add_library(tfm_test_suite_<test_name>_ns STATIC EXCLUDE_FROM_ALL)
161 # target_sources()
162 # target_include_directories()
163 target_compile_definitions(tfm_test_suite_<test_name>_ns
164 INTERFACE
165 TEST_NS_<TEST_NAME>
166 )
167 # target_link_libraries()
168 endif()
169
170 ####################### Secure #############################################
171
172 if (TEST_S_<TEST_NAME>)
173 add_library(tfm_test_suite_<test_name>_s STATIC EXCLUDE_FROM_ALL)
174 # target_sources()
175 # target_include_directories()
176 target_compile_definitions(tfm_test_suite_<test_name>_s
177 INTERFACE
178 TEST_S_<TEST_NAME>
179 )
180 # target_link_libraries()
181 endif()
182
183The function ``target_compile_definitions`` will export the macros
184``TEST_NS_<TEST_NAME>`` or ``TEST_S_<TEST_NAME>`` into source code. and in the
185file ``tf-m-tests/framework/non_secure_suites.c`` or
186``tests/framework/secure_suites.c``, the definitions of these macros will be
187checked, and then the head file will be included and test cases will be
188registered if the macro is defined.
189
190.. code-block:: c
191
192 #ifdef TEST_NS_<TEST_NAME>
193 #include "<test_name>_ns_tests.h"
194 #endif
195
196 static struct test_suite_t test_suites[] = {
197 /* Non-secure example test cases */
198 // ......
199 #ifdef TEST_NS_<TEST_NAME>
200 {&register_testsuite_ns_<test_name>_interface, 0, 0, 0},
201 #endif
202 };
203
204.. code-block:: c
205
206 #ifdef TEST_S_<TEST_NAME>
207 #include "<test_name>_s_tests.h"
208 #endif
209
210 static struct test_suite_t test_suites[] = {
211 /* Secure example test cases */
212 // ......
213 #ifdef TEST_S_<TEST_NAME>
214 {&register_testsuite_s_<test_name>_interface, 0, 0, 0},
215 #endif
216 };
217
218.. Note::
219 On most platforms non-secure tests and secure tests run on the same CPU
220 core, but dual-core platform is an exception. So secure test library and
221 secure sevices shall be linked together in the file
222 ``tf-m-tests/test/test_services/CMakeLists.txt``. Thus they can be built on
223 secure CPU core and non-secure tests library and RTOS are built on
224 non-secure CPU core.
225
226.. code-block:: cmake
227
228 if (TEST_FRAMEWORK_S)
229 # ...
230 if (TEST_S_<TEST_NAME>)
231 add_library(tfm_test_suite_<test_name>_s STATIC EXCLUDE_FROM_ALL)
232 endif()
233 endif()
234
235************************************
236Adding a new test case in test suite
237************************************
238
239The test cases usually express as a function in source code. They will be added
240into an array with structure type called ``test_t`` defined in
241``tf-m-tests/test/framework/test_framework.h``.
242
243.. code-block:: c
244
245 struct test_t {
246 TEST_FUN * const test; /*!< Test function to call */
247 const char *name; /*!< Test name */
248 const char *desc; /*!< Test description */
249 struct test_result_t ret; /*!< Test result */
250 };
251
252For example, a new test case called ``TFM_NS_<TEST_NAME>_TEST_1001`` is created
253and the function ``tfm_<test_name>_test_1001`` needs to be defined in file
254``<test_name>_ns_interface_testsuite.c``. Then the function shall be appended
255into the array which will be quoted in function
256``register_testsuite_ns_<test_name>_interface``. See the reference code below.
257
258.. code-block:: c
259
260 /* List of test cases */
261 static void tfm_<test_name>_test_1001(struct test_result_t *ret);
262
263 /* Append test cases */
264 static struct test_t <test_name>_tests[] = {
265 {&tfm_<test_name>_test_1001, "TFM_NS_<TEST_NAME>_TEST_1001",
266 "Example test case", {TEST_PASSED}},
267 };
268
269 /* Register test case into test suites */
270 void register_testsuite_ns_<test_name>_interface(struct test_suite_t *p_test_suite)
271 {
272 uint32_t list_size;
273
274 list_size = (sizeof(<test_name>_tests) / sizeof(<test_name>_tests[0]));
275
276 set_testsuite("<TEST_NAME> non-secure interface test (TFM_NS_<TEST_NAME>_TEST_1XXX)",
277 <test_name>_tests, list_size, p_test_suite);
278 }
279
280 static void tfm_<test_name>_test_1001(struct test_result_t *ret)
281 {
282 /* test case code */
283 }
284
285********************
286Adding test services
287********************
288
289Some test group may need specific test services. These test services may support
290one or more groups thus developers shall determine the proper test scope.
291
292Steps
293=====
294
295Adding a test service is same as adding a secure partition, generally the
296process can be referenced from the document
297:doc:`Adding Secure Partition </docs/integration_guide/services/tfm_secure_partition_addition>`
298
299.. Note::
300 Each test service must have resource requirements declared in a manifest
301 file, the contents of test services are the same as secure partitions,but
302 their locations are different. Test service manifests shall be set in
303 ``tf-m-tests/test/test_services/tfm_test_manifest_list.yaml``.
304
305Configuration
306=============
307
308If the new test service names ``tfm_<test_name>_test_service`` only supports for the
309example test, the configuration in
310``tf-m-tests/test/test_services/CMakeLists.txt`` forms like below.
311
312.. code-block:: cmake
313
314 if (TEST_S_<TEST_NAME> OR TEST_NS_<TEST_NAME>)
315 add_subdirectory(tfm_<test_name>_test_service)
316 endif()
317
318--------------
319
320*Copyright (c) 2021, Arm Limited. All rights reserved.*