blob: ae99831ac8ff73854ff2239d47528a5b1f6948ea [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Joe Hershbergere721b882015-05-20 14:27:27 -05002/*
3 * Simple unit test library
4 *
5 * Copyright (c) 2013 Google, Inc
Joe Hershbergere721b882015-05-20 14:27:27 -05006 */
7
Simon Glass400175b2020-01-27 08:49:56 -07008#include <console.h>
Simon Glass81098632019-12-29 21:19:23 -07009#include <malloc.h>
Simon Glassef7e2642020-11-08 21:08:43 -070010#ifdef CONFIG_SANDBOX
11#include <asm/state.h>
12#endif
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050014#include <test/test.h>
15#include <test/ut.h>
16
Simon Glass9ce8b402015-11-08 23:47:50 -070017DECLARE_GLOBAL_DATA_PTR;
18
Joe Hershbergere721b882015-05-20 14:27:27 -050019void ut_fail(struct unit_test_state *uts, const char *fname, int line,
20 const char *func, const char *cond)
21{
Simon Glass9ce8b402015-11-08 23:47:50 -070022 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
Joe Hershbergere721b882015-05-20 14:27:27 -050023 printf("%s:%d, %s(): %s\n", fname, line, func, cond);
24 uts->fail_count++;
25}
26
27void ut_failf(struct unit_test_state *uts, const char *fname, int line,
28 const char *func, const char *cond, const char *fmt, ...)
29{
30 va_list args;
31
Simon Glass9ce8b402015-11-08 23:47:50 -070032 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
Joe Hershbergere721b882015-05-20 14:27:27 -050033 printf("%s:%d, %s(): %s: ", fname, line, func, cond);
34 va_start(args, fmt);
35 vprintf(fmt, args);
36 va_end(args);
37 putc('\n');
38 uts->fail_count++;
39}
Simon Glass81098632019-12-29 21:19:23 -070040
41ulong ut_check_free(void)
42{
43 struct mallinfo info = mallinfo();
44
45 return info.uordblks;
46}
47
48long ut_check_delta(ulong last)
49{
50 return ut_check_free() - last;
51}
52
Simon Glassc1a2bb42021-05-08 06:59:56 -060053static int readline_check(struct unit_test_state *uts)
54{
55 int ret;
56
57 ret = console_record_readline(uts->actual_str, sizeof(uts->actual_str));
58 if (ret == -ENOSPC) {
59 ut_fail(uts, __FILE__, __LINE__, __func__,
60 "Console record buffer too small - increase CONFIG_CONSOLE_RECORD_OUT_SIZE");
61 return ret;
62 }
63
64 return 0;
65}
66
Simon Glass400175b2020-01-27 08:49:56 -070067int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
68{
69 va_list args;
Simon Glass090d6642021-05-08 06:59:57 -060070 int len;
Simon Glassc1a2bb42021-05-08 06:59:56 -060071 int ret;
Simon Glass400175b2020-01-27 08:49:56 -070072
73 va_start(args, fmt);
Simon Glass090d6642021-05-08 06:59:57 -060074 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
Simon Glass400175b2020-01-27 08:49:56 -070075 va_end(args);
Simon Glass090d6642021-05-08 06:59:57 -060076 if (len >= sizeof(uts->expect_str)) {
77 ut_fail(uts, __FILE__, __LINE__, __func__,
78 "unit_test_state->expect_str too small");
79 return -EOVERFLOW;
80 }
Simon Glassc1a2bb42021-05-08 06:59:56 -060081 ret = readline_check(uts);
82 if (ret < 0)
83 return ret;
Simon Glass400175b2020-01-27 08:49:56 -070084
85 return strcmp(uts->expect_str, uts->actual_str);
86}
87
Simon Glass33d7edf2020-07-28 19:41:10 -060088int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
89{
90 va_list args;
Simon Glass090d6642021-05-08 06:59:57 -060091 int len;
Simon Glassc1a2bb42021-05-08 06:59:56 -060092 int ret;
Simon Glass33d7edf2020-07-28 19:41:10 -060093
94 va_start(args, fmt);
Simon Glass090d6642021-05-08 06:59:57 -060095 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
Simon Glass33d7edf2020-07-28 19:41:10 -060096 va_end(args);
Simon Glass090d6642021-05-08 06:59:57 -060097 if (len >= sizeof(uts->expect_str)) {
98 ut_fail(uts, __FILE__, __LINE__, __func__,
99 "unit_test_state->expect_str too small");
100 return -EOVERFLOW;
101 }
Simon Glassc1a2bb42021-05-08 06:59:56 -0600102 ret = readline_check(uts);
103 if (ret < 0)
104 return ret;
Simon Glass33d7edf2020-07-28 19:41:10 -0600105
106 return strncmp(uts->expect_str, uts->actual_str,
107 strlen(uts->expect_str));
108}
109
110int ut_check_skipline(struct unit_test_state *uts)
111{
Simon Glassc1a2bb42021-05-08 06:59:56 -0600112 int ret;
113
Simon Glass33d7edf2020-07-28 19:41:10 -0600114 if (!console_record_avail())
115 return -ENFILE;
Simon Glassc1a2bb42021-05-08 06:59:56 -0600116 ret = readline_check(uts);
117 if (ret < 0)
118 return ret;
Simon Glass33d7edf2020-07-28 19:41:10 -0600119
120 return 0;
121}
122
Simon Glass30a75e72023-10-01 19:15:13 -0600123int ut_check_skip_to_linen(struct unit_test_state *uts, const char *fmt, ...)
124{
125 va_list args;
126 int len;
127 int ret;
128
129 va_start(args, fmt);
130 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
131 va_end(args);
132 if (len >= sizeof(uts->expect_str)) {
133 ut_fail(uts, __FILE__, __LINE__, __func__,
134 "unit_test_state->expect_str too small");
135 return -EOVERFLOW;
136 }
137 while (1) {
138 if (!console_record_avail())
139 return -ENOENT;
140 ret = readline_check(uts);
141 if (ret < 0)
142 return ret;
143
144 if (!strncmp(uts->expect_str, uts->actual_str,
145 strlen(uts->expect_str)))
146 return 0;
147 }
148}
149
Simon Glass2e090082021-08-18 21:40:33 -0600150int ut_check_skip_to_line(struct unit_test_state *uts, const char *fmt, ...)
151{
152 va_list args;
153 int len;
154 int ret;
155
156 va_start(args, fmt);
157 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
158 va_end(args);
159 if (len >= sizeof(uts->expect_str)) {
160 ut_fail(uts, __FILE__, __LINE__, __func__,
161 "unit_test_state->expect_str too small");
162 return -EOVERFLOW;
163 }
164 while (1) {
165 if (!console_record_avail())
166 return -ENOENT;
167 ret = readline_check(uts);
168 if (ret < 0)
169 return ret;
170
171 if (!strcmp(uts->expect_str, uts->actual_str))
172 return 0;
173 }
174}
175
Simon Glass400175b2020-01-27 08:49:56 -0700176int ut_check_console_end(struct unit_test_state *uts)
177{
Simon Glassc1a2bb42021-05-08 06:59:56 -0600178 int ret;
179
Simon Glass400175b2020-01-27 08:49:56 -0700180 if (!console_record_avail())
181 return 0;
Simon Glassc1a2bb42021-05-08 06:59:56 -0600182 ret = readline_check(uts);
183 if (ret < 0)
184 return ret;
Simon Glass400175b2020-01-27 08:49:56 -0700185
186 return 1;
187}
188
189int ut_check_console_dump(struct unit_test_state *uts, int total_bytes)
190{
191 char *str = uts->actual_str;
192 int upto;
193
194 /* Handle empty dump */
195 if (!total_bytes)
196 return 0;
197
198 for (upto = 0; upto < total_bytes;) {
199 int len;
200 int bytes;
201
202 len = console_record_readline(str, sizeof(uts->actual_str));
203 if (str[8] != ':' || str[9] != ' ')
204 return 1;
205
Simon Glassc7b16d82021-05-08 07:00:00 -0600206 bytes = len - 8 - 2 - 3 * 16 - 2;
Simon Glass400175b2020-01-27 08:49:56 -0700207 upto += bytes;
208 }
209
210 return upto == total_bytes ? 0 : 1;
211}
Simon Glassef7e2642020-11-08 21:08:43 -0700212
213void ut_silence_console(struct unit_test_state *uts)
214{
215#ifdef CONFIG_SANDBOX
216 struct sandbox_state *state = state_get_current();
217
218 if (!state->show_test_output)
219 gd->flags |= GD_FLG_SILENT;
220#endif
221}
222
223void ut_unsilence_console(struct unit_test_state *uts)
224{
225 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
226}
Simon Glass47ec3ed2021-03-07 17:34:55 -0700227
228void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays)
229{
230#ifdef CONFIG_SANDBOX
231 state_set_skip_delays(skip_delays);
232#endif
233}