summaryrefslogtreecommitdiff
path: root/tests/common/tests.h
blob: 656e2d91c52d35d58f5510c05dcc373e3d0b4305 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef VBOOT_REFERENCE_COMMON_TESTS_H_
#define VBOOT_REFERENCE_COMMON_TESTS_H_

#include <setjmp.h>
#include <stdio.h>

/* Used to get a line number as a constant string. Need to stringify it twice */
#define STRINGIFY(x)	#x
#define TOSTRING(x)	STRINGIFY(x)

extern int gTestSuccess;
extern int gTestAbortArmed;
extern jmp_buf gTestJmpEnv;

/* Return 1 if result is equal to expected_result, else return 0.
 * Also update the global gTestSuccess flag if test fails. */
int test_eq(int result, int expected,
	    const char *preamble, const char *desc, const char *comment);

#define TEST_EQ(result, expected, comment) \
	test_eq(result, expected, \
		__FILE__ ":" TOSTRING(__LINE__), \
		#result " == " #expected, \
		comment)

#define TEST_EQ_S(result, expected) TEST_EQ(result, expected, NULL);

/* Return 0 if result is equal to not_expected_result, else return 1.
 * Also update the global gTestSuccess flag if test fails. */
int test_neq(int result, int not_expected,
	     const char *preamble, const char *desc, const char *comment);

#define TEST_NEQ(result, not_expected, comment) \
	test_neq(result, not_expected, \
		 __FILE__ ":" TOSTRING(__LINE__), \
		 #result " != " #not_expected, \
		 comment)

/* Return 1 if result pointer is equal to expected_result pointer,
 * else return 0.  Does not check pointer contents, only the pointer
 * itself.  Also update the global gTestSuccess flag if test fails. */
int test_ptr_eq(const void* result, const void* expected,
		const char *preamble, const char *desc, const char *comment);

#define TEST_PTR_EQ(result, expected, comment) \
	test_ptr_eq(result, expected, \
		    __FILE__ ":" TOSTRING(__LINE__), \
		    #result " == " #expected, \
		    comment)

/* Return 1 if result pointer is not equal to expected_result pointer,
 * else return 0.  Does not check pointer contents, only the pointer
 * itself.  Also update the global gTestSuccess flag if test fails. */
int test_ptr_neq(const void* result, const void* not_expected,
		 const char *preamble, const char *desc, const char *comment);

#define TEST_PTR_NEQ(result, not_expected, comment) \
	test_ptr_neq(result, not_expected, \
		     __FILE__ ":" TOSTRING(__LINE__), \
		     #result " != " #not_expected, \
		     comment)

/* Return 1 if result string is equal to expected_result string,
 * else return 0.  Also update the global gTestSuccess flag if test fails. */
int test_str_eq(const char* result, const char* expected,
		const char *preamble, const char *desc, const char *comment);

#define TEST_STR_EQ(result, expected, comment) \
	test_str_eq(result, expected, \
		    __FILE__ ":" TOSTRING(__LINE__), \
		    #result " == " #expected, \
		    comment)

/* Return 1 if result string is not equal to not_expected string,
 * else return 0.  Also update the global gTestSuccess flag if test fails. */
int test_str_neq(const char* result, const char* not_expected,
		 const char *preamble, const char *desc, const char *comment);

#define TEST_STR_NEQ(result, not_expected, comment) \
	test_str_neq(result, not_expected, \
		     __FILE__ ":" TOSTRING(__LINE__), \
		     #result " != " #not_expected, \
		     comment)

/* Return 1 if the result is true, else return 0.
 * Also update the global gTestSuccess flag if test fails. */
int test_true(int result,
	      const char *preamble, const char *desc, const char *comment);

#define TEST_TRUE(result, comment) \
	test_true(result, \
		  __FILE__ ":" TOSTRING(__LINE__), \
		  #result " == true", \
		  comment)

/* Return 1 if the result is false, else return 0.
 * Also update the global gTestSuccess flag if test fails. */
int test_false(int result,
	       const char *preamble, const char *desc, const char *comment);

#define TEST_FALSE(result, comment) \
	test_false(result, \
		   __FILE__ ":" TOSTRING(__LINE__), \
		   #result " == false", \
		   comment)

/* Return 1 if result is 0 (VB2_SUCCESS or other), else return 0.
 * Also update the global gTestSuccess flag if test fails. */
int test_succ(int result,
	      const char *preamble, const char *desc, const char *comment);

#define TEST_SUCC(result, comment) \
	test_succ(result, \
		  __FILE__ ":" TOSTRING(__LINE__), \
		  #result " == 0", \
		  comment)

/* Return 1 if result is not 0 (VB2_SUCCESS or other), else return 1.
 * Also update the global gTestSuccess flag if test fails. */
int test_fail(int result,
	      const char *preamble, const char *desc, const char *comment);

#define TEST_FAIL(result, comment) \
	test_fail(result, \
		  __FILE__ ":" TOSTRING(__LINE__), \
		  #result " != 0", \
		  comment)

/* Return 1 if vb2ex_abort() was called, else return 0.
 * Also update the global gTestSuccess flag if test fails. */
int test_abort(int aborted,
	       const char *preamble, const char *desc, const char *comment);

#define TEST_ABORT(call, comment) do { \
	gTestAbortArmed = 1; \
	int jumped = setjmp(gTestJmpEnv); \
	if (!jumped) \
		call; \
	gTestAbortArmed = 0; \
	test_abort(jumped, \
		   __FILE__ ":" TOSTRING(__LINE__), \
		   #call " causes abort", \
		   comment); \
} while (0)

/* ANSI Color coding sequences.
 *
 * Don't use \e as MSC does not recognize it as a valid escape sequence.
 */
#define COL_GREEN "\x1b[1;32m"
#define COL_YELLOW "\x1b[1;33m"
#define COL_RED "\x1b[0;31m"
#define COL_STOP "\x1b[m"

/* Abort if asprintf fails. */
#define xasprintf(...) \
	do { \
		if (asprintf(__VA_ARGS__) < 0) \
			abort(); \
	} while (0)

#endif  /* VBOOT_REFERENCE_COMMON_TESTS_H_ */