xref: /aosp_15_r20/external/vboot_reference/tests/common/tests.c (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  * Common functions used by tests.
6  */
7 
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <sys/stat.h>
12 #include <unistd.h>
13 
14 #include "2common.h"
15 #include "common/tests.h"
16 
17 #define ENV_BUILD_RUN "BUILD_RUN"
18 
create_test_tmp_dir(const char * name)19 const char *create_test_tmp_dir(const char *name)
20 {
21 	const char *build_run = getenv(ENV_BUILD_RUN);
22 	if (!build_run)
23 		die("Failed to get env %s\n", ENV_BUILD_RUN);
24 
25 	char *dir = NULL;
26 	xasprintf(&dir, "%s/tests/%s.tmp", build_run, name);
27 
28 	struct stat st = {0};
29 	if (stat(dir, &st) == -1 && mkdir(dir, 0700))
30 		die("Failed to create dir %s\n", dir);
31 	return dir;
32 }
33 
34 /* Global test success flag. */
35 int gTestSuccess = 1;
36 int gTestAbortArmed = 0;
37 jmp_buf gTestJmpEnv;
38 
print_passed(const char * preamble,const char * desc,const char * comment)39 static void print_passed(const char *preamble, const char *desc,
40 			 const char *comment)
41 {
42 	fprintf(stderr, "%s: %s ... " COL_GREEN "PASSED\n" COL_STOP,
43 		preamble, comment ? comment : desc);
44 }
45 
print_failed(const char * preamble,const char * desc,const char * comment)46 static void print_failed(const char *preamble, const char *desc,
47 			 const char *comment)
48 {
49 	fprintf(stderr, "%s: %s ... " COL_RED "FAILED\n" COL_STOP,
50 		preamble, comment ? comment : desc);
51 }
52 
test_eq(int result,int expected,const char * preamble,const char * desc,const char * comment)53 int test_eq(int result, int expected,
54 	    const char *preamble, const char *desc, const char *comment)
55 {
56 	if (result == expected) {
57 		print_passed(preamble, desc, comment);
58 		return 1;
59 	} else {
60 		print_failed(preamble, desc, comment);
61 		fprintf(stderr, "	Expected: %#x (%d), got: %#x (%d)\n",
62 			expected, expected, result, result);
63 		gTestSuccess = 0;
64 		return 0;
65 	}
66 }
67 
test_neq(int result,int not_expected,const char * preamble,const char * desc,const char * comment)68 int test_neq(int result, int not_expected,
69 	     const char *preamble, const char *desc, const char *comment)
70 {
71 	if (result != not_expected) {
72 		print_passed(preamble, desc, comment);
73 		return 1;
74 	} else {
75 		print_failed(preamble, desc, comment);
76 		fprintf(stderr, "	Didn't expect %#x (%d), but got it.\n",
77 			not_expected, not_expected);
78 		gTestSuccess = 0;
79 		return 0;
80 	}
81 }
82 
test_ptr_eq(const void * result,const void * expected,const char * preamble,const char * desc,const char * comment)83 int test_ptr_eq(const void* result, const void* expected,
84 		const char *preamble, const char *desc, const char *comment)
85 {
86 	if (result == expected) {
87 		print_passed(preamble, desc, comment);
88 		return 1;
89 	} else {
90 		print_failed(preamble, desc, comment);
91 		fprintf(stderr, "	Expected: %#lx, got: %#lx\n",
92 			(long)expected, (long)result);
93 		gTestSuccess = 0;
94 		return 0;
95 	}
96 }
97 
test_ptr_neq(const void * result,const void * not_expected,const char * preamble,const char * desc,const char * comment)98 int test_ptr_neq(const void* result, const void* not_expected,
99 		 const char *preamble, const char *desc, const char *comment)
100 {
101 	if (result != not_expected) {
102 		print_passed(preamble, desc, comment);
103 		return 1;
104 	} else {
105 		print_failed(preamble, desc, comment);
106 		fprintf(stderr, "	Didn't expect %#lx, but got it\n",
107 			(long)not_expected);
108 		gTestSuccess = 0;
109 		return 0;
110 	}
111 }
112 
test_str_eq(const char * result,const char * expected,const char * preamble,const char * desc,const char * comment)113 int test_str_eq(const char* result, const char* expected,
114 		const char *preamble, const char *desc, const char *comment)
115 {
116 	if (!result || !expected) {
117 		print_failed(preamble, desc, comment);
118 		fprintf(stderr, "	String compare with NULL\n");
119 		gTestSuccess = 0;
120 		return 0;
121 	} else if (!strcmp(result, expected)) {
122 		print_passed(preamble, desc, comment);
123 		return 1;
124 	} else {
125 		print_failed(preamble, desc, comment);
126 		fprintf(stderr, "	Expected: \"%s\", got: \"%s\"\n",
127 			expected, result);
128 		gTestSuccess = 0;
129 		return 0;
130 	}
131 }
132 
test_str_neq(const char * result,const char * not_expected,const char * preamble,const char * desc,const char * comment)133 int test_str_neq(const char* result, const char* not_expected,
134 		 const char *preamble, const char *desc, const char *comment)
135 {
136 	if (!result || !not_expected) {
137 		print_failed(preamble, desc, comment);
138 		fprintf(stderr, "	String compare with NULL\n");
139 		gTestSuccess = 0;
140 		return 0;
141 	} else if (strcmp(result, not_expected)) {
142 		print_passed(preamble, desc, comment);
143 		fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
144 			preamble, desc, comment);
145 		return 1;
146 	} else {
147 		print_failed(preamble, desc, comment);
148 		fprintf(stderr, "	Didn't expect: \"%s\", but got it\n",
149 			not_expected);
150 		gTestSuccess = 0;
151 		return 0;
152 	}
153 }
154 
test_succ(int result,const char * preamble,const char * desc,const char * comment)155 int test_succ(int result,
156 	      const char *preamble, const char *desc, const char *comment)
157 {
158 	if (result == 0) {
159 		print_passed(preamble, desc, comment);
160 	} else {
161 		print_failed(preamble, desc, comment);
162 		fprintf(stderr, "	Expected SUCCESS, got: %#x (%d)\n",
163 			result, result);
164 		gTestSuccess = 0;
165 	}
166 	return !result;
167 }
168 
test_fail(int result,const char * preamble,const char * desc,const char * comment)169 int test_fail(int result,
170 	      const char *preamble, const char *desc, const char *comment)
171 {
172 	if (result != 0) {
173 		print_passed(preamble, desc, comment);
174 	} else {
175 		print_failed(preamble, desc, comment);
176 		fprintf(stderr,
177 			"	Didn't expect SUCCESS (0), but got it\n");
178 		gTestSuccess = 0;
179 	}
180 	return result;
181 }
182 
test_true(int result,const char * preamble,const char * desc,const char * comment)183 int test_true(int result,
184 	      const char *preamble, const char *desc, const char *comment)
185 {
186 	if (result) {
187 		print_passed(preamble, desc, comment);
188 	} else {
189 		print_failed(preamble, desc, comment);
190 		fprintf(stderr, "	Expected TRUE, got 0\n");
191 		gTestSuccess = 0;
192 	}
193 	return result;
194 }
195 
test_false(int result,const char * preamble,const char * desc,const char * comment)196 int test_false(int result,
197 	       const char *preamble, const char *desc, const char *comment)
198 {
199 	if (!result) {
200 		print_passed(preamble, desc, comment);
201 	} else {
202 		print_failed(preamble, desc, comment);
203 		fprintf(stderr, "	Expected FALSE, got: %#lx\n",
204 			(long)result);
205 		gTestSuccess = 0;
206 	}
207 	return !result;
208 }
209 
test_abort(int aborted,const char * preamble,const char * desc,const char * comment)210 int test_abort(int aborted,
211 	       const char *preamble, const char *desc, const char *comment)
212 {
213 	if (aborted) {
214 		print_passed(preamble, desc, comment);
215 	} else {
216 		print_failed(preamble, desc, comment);
217 		fprintf(stderr, "	Expected ABORT, but did not get it\n");
218 		gTestSuccess = 0;
219 	}
220 	return aborted;
221 }
222 
vb2ex_abort(void)223 void vb2ex_abort(void)
224 {
225 	/*
226 	 * If expecting an abort call, jump back to TEST_ABORT macro.
227 	 * Otherwise, force exit to ensure the test fails.
228 	 */
229 	if (gTestAbortArmed) {
230 		longjmp(gTestJmpEnv, 1);
231 	} else {
232 		fprintf(stderr, COL_RED "Unexpected ABORT encountered, "
233 			"exiting\n" COL_STOP);
234 		exit(1);
235 	}
236 }
237