1 /*
2 * Copyright (c) 2013, Google, Inc. All rights reserved
3 *
4 * Use of this source code is governed by a MIT-style
5 * license that can be found in the LICENSE file or at
6 * https://opensource.org/licenses/MIT
7 */
8 /*
9 * Functions for unit tests. See lib/unittest/include/unittest.h for usage.
10 */
11 #include <lib/unittest.h>
12
13 #include <lk/debug.h>
14 #include <stdbool.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <stddef.h>
18 #include <stdint.h>
19
20 #if LK_DEBUGLEVEL > 2
21 #define TEST_FAILURE_CASES 1
22 #else
23 #define TEST_FAILURE_CASES 0
24 #endif
25
26 // Default output function is the printf
27 static _printf_engine_output_func out_func = _fprintf_output_func;
28 // Buffer the argument to be sent to the output function
29 static void *out_func_arg = stdout;
30
31 /**
32 * \brief Function called to dump results
33 *
34 * This function will call the out_func callback
35 */
unittest_printf(const char * format,...)36 void unittest_printf (const char *format, ...) {
37 va_list ap;
38 va_start (ap, format);
39
40 _printf_engine(out_func, out_func_arg, format, ap);
41
42 va_end(ap);
43 }
44
expect_bytes_eq(const uint8_t * expected,const uint8_t * actual,size_t len,const char * msg)45 bool expect_bytes_eq(const uint8_t *expected, const uint8_t *actual, size_t len,
46 const char *msg) {
47 if (memcmp(expected, actual, len)) {
48 printf("%s. expected\n", msg);
49 hexdump8(expected, len);
50 printf("actual\n");
51 hexdump8(actual, len);
52 return false;
53 }
54 return true;
55 }
56
unittest_set_output_function(_printf_engine_output_func fun,void * arg)57 void unittest_set_output_function (_printf_engine_output_func fun, void *arg) {
58 out_func = fun;
59 out_func_arg = arg;
60 }
61
62 /* test case for unittests itself */
unittest_printf_tests(void)63 static bool unittest_printf_tests(void) {
64 BEGIN_TEST;
65
66 unittest_printf("\n");
67 unittest_printf("test printf\n");
68 unittest_printf("test printf with args %d\n", 1);
69
70 END_TEST;
71 }
72
unittest_simple_pass(void)73 static bool unittest_simple_pass(void) {
74 BEGIN_TEST;
75
76 // simple true conditions
77 // note: most of these will probably compile out
78 EXPECT_EQ(0, 0, "0=0");
79 EXPECT_EQ(1, 1, "1=1");
80 EXPECT_NE(0, 1, "0=1");
81 EXPECT_NE(1, 0, "1=0");
82 EXPECT_LE(0, 1, "0<=1");
83 EXPECT_LE(1, 1, "1<=1");
84 EXPECT_LT(0, 1, "0<1");
85 EXPECT_GE(1, 0, "1>=0");
86 EXPECT_GE(1, 1, "1>=1");
87 EXPECT_GT(1, 0, "1>0");
88 EXPECT_TRUE(true, "true");
89 EXPECT_FALSE(false, "false");
90 EXPECT_NULL((void *)0, "null");
91 EXPECT_NONNULL((void *)1, "nonnull");
92
93 END_TEST;
94 }
95
unittest_simple_fail(void)96 static bool unittest_simple_fail(void) {
97 BEGIN_TEST;
98
99 // failure conditions
100 EXPECT_EQ(0, 1, "0=1");
101 EXPECT_EQ(1, 0, "1=0");
102 EXPECT_NE(1, 1, "1=1");
103 EXPECT_LE(2, 1, "2<=1");
104 EXPECT_LE(2, 1, "2<=1");
105 EXPECT_LT(2, 1, "2<1");
106 EXPECT_GE(1, 2, "1>=2");
107 EXPECT_GE(1, 2, "1>=2");
108 EXPECT_GT(1, 2, "1>2");
109 EXPECT_TRUE(false, "false");
110 EXPECT_FALSE(true, "true");
111 EXPECT_NULL((void *)1, "null");
112 EXPECT_NONNULL((void *)0, "nonnull");
113
114 END_TEST;
115 }
116
unittest_assert_pass(void)117 static bool unittest_assert_pass(void) {
118 BEGIN_TEST;
119
120 // simple true conditions
121 // note: most of these will probably compile out
122 ASSERT_EQ(0, 0, "0=0");
123 ASSERT_EQ(1, 1, "1=1");
124 ASSERT_NE(0, 1, "0=1");
125 ASSERT_NE(1, 0, "1=0");
126 ASSERT_LE(0, 1, "0<=1");
127 ASSERT_LE(1, 1, "1<=1");
128 ASSERT_LT(0, 1, "0<1");
129 ASSERT_GE(1, 0, "1>=0");
130 ASSERT_GE(1, 1, "1>=1");
131 ASSERT_GT(1, 0, "1>0");
132 ASSERT_TRUE(true, "true");
133 ASSERT_FALSE(false, "false");
134 EXPECT_NULL((void *)0, "null");
135 EXPECT_NONNULL((void *)1, "nonnull");
136
137 END_TEST;
138 }
139
unittest_assert_fail_eq(void)140 static bool unittest_assert_fail_eq(void) {
141 BEGIN_TEST;
142 ASSERT_EQ(0, 1, "0=1");
143 unittest_printf("should not see this\n");
144 END_TEST;
145 }
146
unittest_assert_fail_ne(void)147 static bool unittest_assert_fail_ne(void) {
148 BEGIN_TEST;
149 ASSERT_NE(1, 1, "1=1");
150 unittest_printf("should not see this\n");
151 END_TEST;
152 }
153
unittest_assert_fail_le(void)154 static bool unittest_assert_fail_le(void) {
155 BEGIN_TEST;
156 ASSERT_LE(2, 1, "2<=1");
157 unittest_printf("should not see this\n");
158 END_TEST;
159 }
160
unittest_assert_fail_lt(void)161 static bool unittest_assert_fail_lt(void) {
162 BEGIN_TEST;
163 ASSERT_LT(2, 1, "2<1");
164 unittest_printf("should not see this\n");
165 END_TEST;
166 }
167
unittest_assert_fail_ge(void)168 static bool unittest_assert_fail_ge(void) {
169 BEGIN_TEST;
170 ASSERT_GE(1, 2, "1>=2");
171 unittest_printf("should not see this\n");
172 END_TEST;
173 }
174
unittest_assert_fail_gt(void)175 static bool unittest_assert_fail_gt(void) {
176 BEGIN_TEST;
177 ASSERT_GT(1, 2, "1>2");
178 unittest_printf("should not see this\n");
179 END_TEST;
180 }
181
unittest_assert_fail_true(void)182 static bool unittest_assert_fail_true(void) {
183 BEGIN_TEST;
184 ASSERT_GT(1, 2, "1>2");
185 unittest_printf("should not see this\n");
186 END_TEST;
187 }
188
unittest_assert_fail_false(void)189 static bool unittest_assert_fail_false(void) {
190 BEGIN_TEST;
191 ASSERT_GT(1, 2, "1>2");
192 unittest_printf("should not see this\n");
193 END_TEST;
194 }
195
unittest_assert_fail_null(void)196 static bool unittest_assert_fail_null(void) {
197 BEGIN_TEST;
198 ASSERT_NULL((void *)1, "null");
199 unittest_printf("should not see this\n");
200 END_TEST;
201 }
202
unittest_assert_fail_nonnull(void)203 static bool unittest_assert_fail_nonnull(void) {
204 BEGIN_TEST;
205 ASSERT_NONNULL((void *)0, "nonnull");
206 unittest_printf("should not see this\n");
207 END_TEST;
208 }
209
210 BEGIN_TEST_CASE(unittest)
211 RUN_TEST(unittest_printf_tests);
212 RUN_TEST(unittest_simple_pass);
213 RUN_TEST(unittest_assert_pass);
214 #if TEST_FAILURE_CASES
215 RUN_TEST(unittest_simple_fail);
216 RUN_TEST(unittest_assert_fail_eq);
217 RUN_TEST(unittest_assert_fail_ne);
218 RUN_TEST(unittest_assert_fail_le);
219 RUN_TEST(unittest_assert_fail_lt);
220 RUN_TEST(unittest_assert_fail_ge);
221 RUN_TEST(unittest_assert_fail_gt);
222 RUN_TEST(unittest_assert_fail_true);
223 RUN_TEST(unittest_assert_fail_false);
224 RUN_TEST(unittest_assert_fail_null);
225 RUN_TEST(unittest_assert_fail_nonnull);
226 #endif
227 END_TEST_CASE(unittest)
228