blob: 7454da3e001f5953989bd702cec0c14c996fea7d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Joe Hershberger3a77be52015-05-20 14:27:27 -05002/*
3 * Simple unit test library
4 *
5 * Copyright (c) 2013 Google, Inc
Joe Hershberger3a77be52015-05-20 14:27:27 -05006 */
7
Simon Glassd856e912020-01-27 08:49:56 -07008#include <console.h>
Simon Glass19920d72019-12-29 21:19:23 -07009#include <malloc.h>
Simon Glassb8f00822020-11-08 21:08:43 -070010#ifdef CONFIG_SANDBOX
11#include <asm/state.h>
12#endif
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Joe Hershberger3a77be52015-05-20 14:27:27 -050014#include <test/test.h>
15#include <test/ut.h>
16
Simon Glassfe6d12a2015-11-08 23:47:50 -070017DECLARE_GLOBAL_DATA_PTR;
18
Joe Hershberger3a77be52015-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 Glassfe6d12a2015-11-08 23:47:50 -070022 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
Joe Hershberger3a77be52015-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 Glassfe6d12a2015-11-08 23:47:50 -070032 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
Joe Hershberger3a77be52015-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 Glass19920d72019-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 Glassd6ed4af2021-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;
Simon Glassc2e59eb2024-08-22 07:57:47 -060062 } else if (ret == -ENOENT) {
63 strcpy(uts->actual_str, "<no-more-output>");
Simon Glassd6ed4af2021-05-08 06:59:56 -060064 }
65
Simon Glassc2e59eb2024-08-22 07:57:47 -060066 return ret;
Simon Glassd6ed4af2021-05-08 06:59:56 -060067}
68
Simon Glassd856e912020-01-27 08:49:56 -070069int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
70{
71 va_list args;
Simon Glassc638ae62021-05-08 06:59:57 -060072 int len;
Simon Glassd6ed4af2021-05-08 06:59:56 -060073 int ret;
Simon Glassd856e912020-01-27 08:49:56 -070074
75 va_start(args, fmt);
Simon Glassc638ae62021-05-08 06:59:57 -060076 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
Simon Glassd856e912020-01-27 08:49:56 -070077 va_end(args);
Simon Glassc638ae62021-05-08 06:59:57 -060078 if (len >= sizeof(uts->expect_str)) {
79 ut_fail(uts, __FILE__, __LINE__, __func__,
80 "unit_test_state->expect_str too small");
81 return -EOVERFLOW;
82 }
Simon Glassd6ed4af2021-05-08 06:59:56 -060083 ret = readline_check(uts);
Simon Glassc2e59eb2024-08-22 07:57:47 -060084 if (ret == -ENOENT)
85 return 1;
Simon Glassd856e912020-01-27 08:49:56 -070086
87 return strcmp(uts->expect_str, uts->actual_str);
88}
89
Simon Glassc963df92020-07-28 19:41:10 -060090int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
91{
92 va_list args;
Simon Glassc638ae62021-05-08 06:59:57 -060093 int len;
Simon Glassd6ed4af2021-05-08 06:59:56 -060094 int ret;
Simon Glassc963df92020-07-28 19:41:10 -060095
96 va_start(args, fmt);
Simon Glassc638ae62021-05-08 06:59:57 -060097 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
Simon Glassc963df92020-07-28 19:41:10 -060098 va_end(args);
Simon Glassc638ae62021-05-08 06:59:57 -060099 if (len >= sizeof(uts->expect_str)) {
100 ut_fail(uts, __FILE__, __LINE__, __func__,
101 "unit_test_state->expect_str too small");
102 return -EOVERFLOW;
103 }
Simon Glassd6ed4af2021-05-08 06:59:56 -0600104 ret = readline_check(uts);
105 if (ret < 0)
106 return ret;
Simon Glassc963df92020-07-28 19:41:10 -0600107
108 return strncmp(uts->expect_str, uts->actual_str,
109 strlen(uts->expect_str));
110}
111
112int ut_check_skipline(struct unit_test_state *uts)
113{
Simon Glassd6ed4af2021-05-08 06:59:56 -0600114 int ret;
115
Simon Glassc963df92020-07-28 19:41:10 -0600116 if (!console_record_avail())
117 return -ENFILE;
Simon Glassd6ed4af2021-05-08 06:59:56 -0600118 ret = readline_check(uts);
119 if (ret < 0)
120 return ret;
Simon Glassc963df92020-07-28 19:41:10 -0600121
122 return 0;
123}
124
Simon Glasse85cd122023-10-01 19:15:13 -0600125int ut_check_skip_to_linen(struct unit_test_state *uts, const char *fmt, ...)
126{
127 va_list args;
128 int len;
129 int ret;
130
131 va_start(args, fmt);
132 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
133 va_end(args);
134 if (len >= sizeof(uts->expect_str)) {
135 ut_fail(uts, __FILE__, __LINE__, __func__,
136 "unit_test_state->expect_str too small");
137 return -EOVERFLOW;
138 }
139 while (1) {
140 if (!console_record_avail())
141 return -ENOENT;
142 ret = readline_check(uts);
143 if (ret < 0)
144 return ret;
145
146 if (!strncmp(uts->expect_str, uts->actual_str,
147 strlen(uts->expect_str)))
148 return 0;
149 }
150}
151
Simon Glass12892b02021-08-18 21:40:33 -0600152int ut_check_skip_to_line(struct unit_test_state *uts, const char *fmt, ...)
153{
154 va_list args;
155 int len;
156 int ret;
157
158 va_start(args, fmt);
159 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
160 va_end(args);
161 if (len >= sizeof(uts->expect_str)) {
162 ut_fail(uts, __FILE__, __LINE__, __func__,
163 "unit_test_state->expect_str too small");
164 return -EOVERFLOW;
165 }
166 while (1) {
167 if (!console_record_avail())
168 return -ENOENT;
169 ret = readline_check(uts);
170 if (ret < 0)
171 return ret;
172
173 if (!strcmp(uts->expect_str, uts->actual_str))
174 return 0;
175 }
176}
177
Simon Glassd856e912020-01-27 08:49:56 -0700178int ut_check_console_end(struct unit_test_state *uts)
179{
Simon Glassd6ed4af2021-05-08 06:59:56 -0600180 int ret;
181
Simon Glassd856e912020-01-27 08:49:56 -0700182 if (!console_record_avail())
183 return 0;
Simon Glassd6ed4af2021-05-08 06:59:56 -0600184 ret = readline_check(uts);
185 if (ret < 0)
186 return ret;
Simon Glassd856e912020-01-27 08:49:56 -0700187
188 return 1;
189}
190
191int ut_check_console_dump(struct unit_test_state *uts, int total_bytes)
192{
193 char *str = uts->actual_str;
194 int upto;
195
196 /* Handle empty dump */
197 if (!total_bytes)
198 return 0;
199
200 for (upto = 0; upto < total_bytes;) {
201 int len;
202 int bytes;
203
204 len = console_record_readline(str, sizeof(uts->actual_str));
205 if (str[8] != ':' || str[9] != ' ')
206 return 1;
207
Simon Glass85c8fc52021-05-08 07:00:00 -0600208 bytes = len - 8 - 2 - 3 * 16 - 2;
Simon Glassd856e912020-01-27 08:49:56 -0700209 upto += bytes;
210 }
211
212 return upto == total_bytes ? 0 : 1;
213}
Simon Glassb8f00822020-11-08 21:08:43 -0700214
215void ut_silence_console(struct unit_test_state *uts)
216{
217#ifdef CONFIG_SANDBOX
218 struct sandbox_state *state = state_get_current();
219
220 if (!state->show_test_output)
221 gd->flags |= GD_FLG_SILENT;
222#endif
223}
224
225void ut_unsilence_console(struct unit_test_state *uts)
226{
227 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
228}
Simon Glass59cad962021-03-07 17:34:55 -0700229
230void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays)
231{
232#ifdef CONFIG_SANDBOX
233 state_set_skip_delays(skip_delays);
234#endif
235}