xref: /aosp_15_r20/trusty/kernel/lib/unittest/include/lib/unittest/unittest.h (revision 344aa361028b423587d4ef3fa52a23d194628137)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #pragma once
25 
26 #include <inttypes.h>
27 #include <lib/trusty/ipc.h>
28 #include <lk/compiler.h>
29 #include <stdbool.h>
30 
31 __BEGIN_CDECLS
32 
33 /**
34  * struct unittest - struct representing a kernel unit-test.
35  * @port_name:  Port name.
36  * @run_test:   Function to call when a client connects to @port_name.
37  * @_href:      Private data used by library.
38  */
39 struct unittest {
40     const char* port_name;
41     bool (*run_test)(struct unittest* test);
42     struct handle_ref _href;
43 };
44 
45 int unittest_printf(const char* fmt, ...);
46 int unittest_add(struct unittest* test);
47 
48 #define trusty_unittest_printf(args...) \
49     do {                                \
50         unittest_printf(args);          \
51     } while (0)
52 
53 #include <lk/trusty_unittest.h>
54 
55 #include <lk/init.h>
56 #include <platform.h>
57 
58 #define PORT_TEST_COMMON(suite_name, port_name_string, suite_name_string) \
59     static bool run_##suite_name(struct unittest* test) {                 \
60         return RUN_ALL_SUITE_TESTS(suite_name_string);                    \
61     }                                                                     \
62                                                                           \
63     static void suite_name##_init(uint level) {                           \
64         static struct unittest test = {                                   \
65                 .port_name = port_name_string,                            \
66                 .run_test = run_##suite_name,                             \
67         };                                                                \
68         unittest_add(&test);                                              \
69     }                                                                     \
70                                                                           \
71     LK_INIT_HOOK(suite_name, suite_name##_init, LK_INIT_LEVEL_APPS);
72 
73 #define PORT_TEST(suite_name, port_name_string) \
74     PORT_TEST_COMMON(suite_name, port_name_string, NULL)
75 
76 #define PORT_TEST_SUITE(suite_name, port_name_string) \
77     PORT_TEST_COMMON(suite_name, port_name_string, #suite_name)
78 
79 __END_CDECLS
80