xref: /aosp_15_r20/external/vboot_reference/tests/common/tests.h (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2011 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 #ifndef VBOOT_REFERENCE_COMMON_TESTS_H_
7 #define VBOOT_REFERENCE_COMMON_TESTS_H_
8 
9 #include <setjmp.h>
10 #include <stdio.h>
11 
12 /* Used to get a line number as a constant string. Need to stringify it twice */
13 #define STRINGIFY(x)	#x
14 #define TOSTRING(x)	STRINGIFY(x)
15 
16 /* Create a temp dir under ${BUILD_RUN}. */
17 const char *create_test_tmp_dir(const char *name);
18 
19 extern int gTestSuccess;
20 extern int gTestAbortArmed;
21 extern jmp_buf gTestJmpEnv;
22 
23 /* Return 1 if result is equal to expected_result, else return 0.
24  * Also update the global gTestSuccess flag if test fails. */
25 int test_eq(int result, int expected,
26 	    const char *preamble, const char *desc, const char *comment);
27 
28 #define TEST_EQ(result, expected, comment) \
29 	test_eq(result, expected, \
30 		__FILE__ ":" TOSTRING(__LINE__), \
31 		#result " == " #expected, \
32 		comment)
33 
34 #define TEST_EQ_S(result, expected) TEST_EQ(result, expected, NULL);
35 
36 /* Return 0 if result is equal to not_expected_result, else return 1.
37  * Also update the global gTestSuccess flag if test fails. */
38 int test_neq(int result, int not_expected,
39 	     const char *preamble, const char *desc, const char *comment);
40 
41 #define TEST_NEQ(result, not_expected, comment) \
42 	test_neq(result, not_expected, \
43 		 __FILE__ ":" TOSTRING(__LINE__), \
44 		 #result " != " #not_expected, \
45 		 comment)
46 
47 /* Return 1 if result pointer is equal to expected_result pointer,
48  * else return 0.  Does not check pointer contents, only the pointer
49  * itself.  Also update the global gTestSuccess flag if test fails. */
50 int test_ptr_eq(const void* result, const void* expected,
51 		const char *preamble, const char *desc, const char *comment);
52 
53 #define TEST_PTR_EQ(result, expected, comment) \
54 	test_ptr_eq(result, expected, \
55 		    __FILE__ ":" TOSTRING(__LINE__), \
56 		    #result " == " #expected, \
57 		    comment)
58 
59 /* Return 1 if result pointer is not equal to expected_result pointer,
60  * else return 0.  Does not check pointer contents, only the pointer
61  * itself.  Also update the global gTestSuccess flag if test fails. */
62 int test_ptr_neq(const void* result, const void* not_expected,
63 		 const char *preamble, const char *desc, const char *comment);
64 
65 #define TEST_PTR_NEQ(result, not_expected, comment) \
66 	test_ptr_neq(result, not_expected, \
67 		     __FILE__ ":" TOSTRING(__LINE__), \
68 		     #result " != " #not_expected, \
69 		     comment)
70 
71 /* Return 1 if result string is equal to expected_result string,
72  * else return 0.  Also update the global gTestSuccess flag if test fails. */
73 int test_str_eq(const char* result, const char* expected,
74 		const char *preamble, const char *desc, const char *comment);
75 
76 #define TEST_STR_EQ(result, expected, comment) \
77 	test_str_eq(result, expected, \
78 		    __FILE__ ":" TOSTRING(__LINE__), \
79 		    #result " == " #expected, \
80 		    comment)
81 
82 /* Return 1 if result string is not equal to not_expected string,
83  * else return 0.  Also update the global gTestSuccess flag if test fails. */
84 int test_str_neq(const char* result, const char* not_expected,
85 		 const char *preamble, const char *desc, const char *comment);
86 
87 #define TEST_STR_NEQ(result, not_expected, comment) \
88 	test_str_neq(result, not_expected, \
89 		     __FILE__ ":" TOSTRING(__LINE__), \
90 		     #result " != " #not_expected, \
91 		     comment)
92 
93 /* Return 1 if the result is true, else return 0.
94  * Also update the global gTestSuccess flag if test fails. */
95 int test_true(int result,
96 	      const char *preamble, const char *desc, const char *comment);
97 
98 #define TEST_TRUE(result, comment) \
99 	test_true(result, \
100 		  __FILE__ ":" TOSTRING(__LINE__), \
101 		  #result " == true", \
102 		  comment)
103 
104 /* Return 1 if the result is false, else return 0.
105  * Also update the global gTestSuccess flag if test fails. */
106 int test_false(int result,
107 	       const char *preamble, const char *desc, const char *comment);
108 
109 #define TEST_FALSE(result, comment) \
110 	test_false(result, \
111 		   __FILE__ ":" TOSTRING(__LINE__), \
112 		   #result " == false", \
113 		   comment)
114 
115 /* Return 1 if result is 0 (VB2_SUCCESS or other), else return 0.
116  * Also update the global gTestSuccess flag if test fails. */
117 int test_succ(int result,
118 	      const char *preamble, const char *desc, const char *comment);
119 
120 #define TEST_SUCC(result, comment) \
121 	test_succ(result, \
122 		  __FILE__ ":" TOSTRING(__LINE__), \
123 		  #result " == 0", \
124 		  comment)
125 
126 /* Return 1 if result is not 0 (VB2_SUCCESS or other), else return 1.
127  * Also update the global gTestSuccess flag if test fails. */
128 int test_fail(int result,
129 	      const char *preamble, const char *desc, const char *comment);
130 
131 #define TEST_FAIL(result, comment) \
132 	test_fail(result, \
133 		  __FILE__ ":" TOSTRING(__LINE__), \
134 		  #result " != 0", \
135 		  comment)
136 
137 /* Return 1 if vb2ex_abort() was called, else return 0.
138  * Also update the global gTestSuccess flag if test fails. */
139 int test_abort(int aborted,
140 	       const char *preamble, const char *desc, const char *comment);
141 
142 #define TEST_ABORT(call, comment) do { \
143 	gTestAbortArmed = 1; \
144 	int jumped = setjmp(gTestJmpEnv); \
145 	if (!jumped) \
146 		call; \
147 	gTestAbortArmed = 0; \
148 	test_abort(jumped, \
149 		   __FILE__ ":" TOSTRING(__LINE__), \
150 		   #call " causes abort", \
151 		   comment); \
152 } while (0)
153 
154 /* ANSI Color coding sequences.
155  *
156  * Don't use \e as MSC does not recognize it as a valid escape sequence.
157  */
158 #define COL_GREEN "\x1b[1;32m"
159 #define COL_YELLOW "\x1b[1;33m"
160 #define COL_RED "\x1b[0;31m"
161 #define COL_STOP "\x1b[m"
162 
163 #define die(...) \
164 	do { \
165 		fprintf(stderr, __VA_ARGS__); \
166 		abort(); \
167 	} while (0)
168 
169 /* Abort if asprintf fails. */
170 #define xasprintf(...) \
171 	do { \
172 		if (asprintf(__VA_ARGS__) < 0) \
173 			abort(); \
174 	} while (0)
175 
176 #endif  /* VBOOT_REFERENCE_COMMON_TESTS_H_ */
177