Implemented Kernel Post-boot testing.
[akaros.git] / kern / include / test_infrastructure.h
1 #ifndef ROS_INC_TEST_INFRASTRUCTURE_H
2 #define ROS_INC_TEST_INFRASTRUCTURE_H
3
4 /*
5  * Header file with infrastructure needed for kernel unit tests:
6  *  - Assertion functions.
7  *  - Test structures.
8  */
9
10 #include <stdio.h>
11 #include <kmalloc.h>
12 #include <generated/autoconf.h>
13
14 /* Macros for assertions. 
15  * They depend on <stdbool.h> and printk() to be included in the source file. 
16  */
17 #define KT_ASSERT_M(message, test)                                               \
18         do {                                                                         \
19                 if (!(test)) {                                                           \
20                         extern char *kern_test_msg;                                          \
21                         char prefix[] = "Assertion failed: ";                                \
22                         int msg_size = sizeof(prefix) + sizeof(message) - 1;                 \
23                         kern_test_msg = (char*) kmalloc(msg_size, 0);                        \
24                         snprintf(kern_test_msg, msg_size, "%s%s", prefix, message);          \
25                         return false;                                                        \
26                 }                                                                        \
27         } while (0)
28
29 #define KT_ASSERT(test)                                                          \
30         do {                                                                         \
31                 if (!(test)) {                                                           \
32                         return false;                                                        \
33                 }                                                                        \
34         } while (0)
35
36
37 /* Postboot kernel tests: tests ran after boot in kernel mode. */
38
39 struct pb_kernel_test {
40         char name[256]; // Name of the test function.
41         bool (*func)(void); // Name of the test function, should be equal to 'name'.
42         bool enabled; // Whether to run or not the test.
43 };
44
45 /* A macro for testing if another macro has been #defined or not.  Can be used
46  * wherever you need a boolean defined.  Returns 0 or 1. */
47 #define is_defined(macro) is_defined_(macro)
48 #define is_defined_test_1 ,
49 #define is_defined_(value) is_defined__(is_defined_test_##value)
50 #define is_defined__(comma) is_defined___(comma 1, 0)
51 #define is_defined___(_, v, ...) v
52
53 /* Macro for registering a postboot kernel test. */
54 #define PB_K_TEST_REG(name) \
55         {"test_" #name, test_##name, is_defined(CONFIG_TEST_##name)}
56
57 extern struct pb_kernel_test pb_kernel_tests[];
58 extern int num_pb_kernel_tests;
59
60 #endif // ROS_INC_TEST_INFRASTRUCTURE_H