blob: 28da417686e497c7178bc033c2e8fe79bb684c66 [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
8#include <common.h>
Simon Glass400175b2020-01-27 08:49:56 -07009#include <console.h>
Simon Glass81098632019-12-29 21:19:23 -070010#include <malloc.h>
Simon Glassef7e2642020-11-08 21:08:43 -070011#ifdef CONFIG_SANDBOX
12#include <asm/state.h>
13#endif
Simon Glass401d1c42020-10-30 21:38:53 -060014#include <asm/global_data.h>
Joe Hershbergere721b882015-05-20 14:27:27 -050015#include <test/test.h>
16#include <test/ut.h>
17
Simon Glass9ce8b402015-11-08 23:47:50 -070018DECLARE_GLOBAL_DATA_PTR;
19
Joe Hershbergere721b882015-05-20 14:27:27 -050020void ut_fail(struct unit_test_state *uts, const char *fname, int line,
21 const char *func, const char *cond)
22{
Simon Glass9ce8b402015-11-08 23:47:50 -070023 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
Joe Hershbergere721b882015-05-20 14:27:27 -050024 printf("%s:%d, %s(): %s\n", fname, line, func, cond);
25 uts->fail_count++;
26}
27
28void ut_failf(struct unit_test_state *uts, const char *fname, int line,
29 const char *func, const char *cond, const char *fmt, ...)
30{
31 va_list args;
32
Simon Glass9ce8b402015-11-08 23:47:50 -070033 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
Joe Hershbergere721b882015-05-20 14:27:27 -050034 printf("%s:%d, %s(): %s: ", fname, line, func, cond);
35 va_start(args, fmt);
36 vprintf(fmt, args);
37 va_end(args);
38 putc('\n');
39 uts->fail_count++;
40}
Simon Glass81098632019-12-29 21:19:23 -070041
42ulong ut_check_free(void)
43{
44 struct mallinfo info = mallinfo();
45
46 return info.uordblks;
47}
48
49long ut_check_delta(ulong last)
50{
51 return ut_check_free() - last;
52}
53
Simon Glassc1a2bb42021-05-08 06:59:56 -060054static int readline_check(struct unit_test_state *uts)
55{
56 int ret;
57
58 ret = console_record_readline(uts->actual_str, sizeof(uts->actual_str));
59 if (ret == -ENOSPC) {
60 ut_fail(uts, __FILE__, __LINE__, __func__,
61 "Console record buffer too small - increase CONFIG_CONSOLE_RECORD_OUT_SIZE");
62 return ret;
63 }
64
65 return 0;
66}
67
Simon Glass400175b2020-01-27 08:49:56 -070068int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
69{
70 va_list args;
Simon Glass090d6642021-05-08 06:59:57 -060071 int len;
Simon Glassc1a2bb42021-05-08 06:59:56 -060072 int ret;
Simon Glass400175b2020-01-27 08:49:56 -070073
74 va_start(args, fmt);
Simon Glass090d6642021-05-08 06:59:57 -060075 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
Simon Glass400175b2020-01-27 08:49:56 -070076 va_end(args);
Simon Glass090d6642021-05-08 06:59:57 -060077 if (len >= sizeof(uts->expect_str)) {
78 ut_fail(uts, __FILE__, __LINE__, __func__,
79 "unit_test_state->expect_str too small");
80 return -EOVERFLOW;
81 }
Simon Glassc1a2bb42021-05-08 06:59:56 -060082 ret = readline_check(uts);
83 if (ret < 0)
84 return ret;
Simon Glass400175b2020-01-27 08:49:56 -070085
86 return strcmp(uts->expect_str, uts->actual_str);
87}
88
Simon Glass33d7edf2020-07-28 19:41:10 -060089int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
90{
91 va_list args;
Simon Glass090d6642021-05-08 06:59:57 -060092 int len;
Simon Glassc1a2bb42021-05-08 06:59:56 -060093 int ret;
Simon Glass33d7edf2020-07-28 19:41:10 -060094
95 va_start(args, fmt);
Simon Glass090d6642021-05-08 06:59:57 -060096 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
Simon Glass33d7edf2020-07-28 19:41:10 -060097 va_end(args);
Simon Glass090d6642021-05-08 06:59:57 -060098 if (len >= sizeof(uts->expect_str)) {
99 ut_fail(uts, __FILE__, __LINE__, __func__,
100 "unit_test_state->expect_str too small");
101 return -EOVERFLOW;
102 }
Simon Glassc1a2bb42021-05-08 06:59:56 -0600103 ret = readline_check(uts);
104 if (ret < 0)
105 return ret;
Simon Glass33d7edf2020-07-28 19:41:10 -0600106
107 return strncmp(uts->expect_str, uts->actual_str,
108 strlen(uts->expect_str));
109}
110
111int ut_check_skipline(struct unit_test_state *uts)
112{
Simon Glassc1a2bb42021-05-08 06:59:56 -0600113 int ret;
114
Simon Glass33d7edf2020-07-28 19:41:10 -0600115 if (!console_record_avail())
116 return -ENFILE;
Simon Glassc1a2bb42021-05-08 06:59:56 -0600117 ret = readline_check(uts);
118 if (ret < 0)
119 return ret;
Simon Glass33d7edf2020-07-28 19:41:10 -0600120
121 return 0;
122}
123
Simon Glass2e090082021-08-18 21:40:33 -0600124int ut_check_skip_to_line(struct unit_test_state *uts, const char *fmt, ...)
125{
126 va_list args;
127 int len;
128 int ret;
129
130 va_start(args, fmt);
131 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
132 va_end(args);
133 if (len >= sizeof(uts->expect_str)) {
134 ut_fail(uts, __FILE__, __LINE__, __func__,
135 "unit_test_state->expect_str too small");
136 return -EOVERFLOW;
137 }
138 while (1) {
139 if (!console_record_avail())
140 return -ENOENT;
141 ret = readline_check(uts);
142 if (ret < 0)
143 return ret;
144
145 if (!strcmp(uts->expect_str, uts->actual_str))
146 return 0;
147 }
148}
149
Simon Glass400175b2020-01-27 08:49:56 -0700150int ut_check_console_end(struct unit_test_state *uts)
151{
Simon Glassc1a2bb42021-05-08 06:59:56 -0600152 int ret;
153
Simon Glass400175b2020-01-27 08:49:56 -0700154 if (!console_record_avail())
155 return 0;
Simon Glassc1a2bb42021-05-08 06:59:56 -0600156 ret = readline_check(uts);
157 if (ret < 0)
158 return ret;
Simon Glass400175b2020-01-27 08:49:56 -0700159
160 return 1;
161}
162
163int ut_check_console_dump(struct unit_test_state *uts, int total_bytes)
164{
165 char *str = uts->actual_str;
166 int upto;
167
168 /* Handle empty dump */
169 if (!total_bytes)
170 return 0;
171
172 for (upto = 0; upto < total_bytes;) {
173 int len;
174 int bytes;
175
176 len = console_record_readline(str, sizeof(uts->actual_str));
177 if (str[8] != ':' || str[9] != ' ')
178 return 1;
179
Simon Glassc7b16d82021-05-08 07:00:00 -0600180 bytes = len - 8 - 2 - 3 * 16 - 2;
Simon Glass400175b2020-01-27 08:49:56 -0700181 upto += bytes;
182 }
183
184 return upto == total_bytes ? 0 : 1;
185}
Simon Glassef7e2642020-11-08 21:08:43 -0700186
187void ut_silence_console(struct unit_test_state *uts)
188{
189#ifdef CONFIG_SANDBOX
190 struct sandbox_state *state = state_get_current();
191
192 if (!state->show_test_output)
193 gd->flags |= GD_FLG_SILENT;
194#endif
195}
196
197void ut_unsilence_console(struct unit_test_state *uts)
198{
199 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
200}
Simon Glass47ec3ed2021-03-07 17:34:55 -0700201
202void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays)
203{
204#ifdef CONFIG_SANDBOX
205 state_set_skip_delays(skip_delays);
206#endif
207}