Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Simple unit test library |
| 4 | * |
| 5 | * Copyright (c) 2013 Google, Inc |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 8 | #include <console.h> |
Simon Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Glass | b8f0082 | 2020-11-08 21:08:43 -0700 | [diff] [blame] | 10 | #ifdef CONFIG_SANDBOX |
| 11 | #include <asm/state.h> |
| 12 | #endif |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 14 | #include <test/test.h> |
| 15 | #include <test/ut.h> |
| 16 | |
Simon Glass | fe6d12a | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 19 | void ut_fail(struct unit_test_state *uts, const char *fname, int line, |
| 20 | const char *func, const char *cond) |
| 21 | { |
Simon Glass | fe6d12a | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 22 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 23 | printf("%s:%d, %s(): %s\n", fname, line, func, cond); |
| 24 | uts->fail_count++; |
| 25 | } |
| 26 | |
| 27 | void 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 Glass | fe6d12a | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 32 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 33 | 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 Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 40 | |
| 41 | ulong ut_check_free(void) |
| 42 | { |
| 43 | struct mallinfo info = mallinfo(); |
| 44 | |
| 45 | return info.uordblks; |
| 46 | } |
| 47 | |
| 48 | long ut_check_delta(ulong last) |
| 49 | { |
| 50 | return ut_check_free() - last; |
| 51 | } |
| 52 | |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 53 | static 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 Glass | c2e59eb | 2024-08-22 07:57:47 -0600 | [diff] [blame] | 62 | } else if (ret == -ENOENT) { |
| 63 | strcpy(uts->actual_str, "<no-more-output>"); |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 64 | } |
| 65 | |
Simon Glass | c2e59eb | 2024-08-22 07:57:47 -0600 | [diff] [blame] | 66 | return ret; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 67 | } |
| 68 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 69 | int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...) |
| 70 | { |
| 71 | va_list args; |
Simon Glass | c638ae6 | 2021-05-08 06:59:57 -0600 | [diff] [blame] | 72 | int len; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 73 | int ret; |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 74 | |
| 75 | va_start(args, fmt); |
Simon Glass | c638ae6 | 2021-05-08 06:59:57 -0600 | [diff] [blame] | 76 | len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 77 | va_end(args); |
Simon Glass | c638ae6 | 2021-05-08 06:59:57 -0600 | [diff] [blame] | 78 | 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 Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 83 | ret = readline_check(uts); |
Simon Glass | c2e59eb | 2024-08-22 07:57:47 -0600 | [diff] [blame] | 84 | if (ret == -ENOENT) |
| 85 | return 1; |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 86 | |
| 87 | return strcmp(uts->expect_str, uts->actual_str); |
| 88 | } |
| 89 | |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 90 | int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...) |
| 91 | { |
| 92 | va_list args; |
Simon Glass | c638ae6 | 2021-05-08 06:59:57 -0600 | [diff] [blame] | 93 | int len; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 94 | int ret; |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 95 | |
| 96 | va_start(args, fmt); |
Simon Glass | c638ae6 | 2021-05-08 06:59:57 -0600 | [diff] [blame] | 97 | len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 98 | va_end(args); |
Simon Glass | c638ae6 | 2021-05-08 06:59:57 -0600 | [diff] [blame] | 99 | 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 Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 104 | ret = readline_check(uts); |
| 105 | if (ret < 0) |
| 106 | return ret; |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 107 | |
| 108 | return strncmp(uts->expect_str, uts->actual_str, |
| 109 | strlen(uts->expect_str)); |
| 110 | } |
| 111 | |
| 112 | int ut_check_skipline(struct unit_test_state *uts) |
| 113 | { |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 114 | int ret; |
| 115 | |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 116 | if (!console_record_avail()) |
| 117 | return -ENFILE; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 118 | ret = readline_check(uts); |
| 119 | if (ret < 0) |
| 120 | return ret; |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
Simon Glass | e85cd12 | 2023-10-01 19:15:13 -0600 | [diff] [blame] | 125 | int 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 Glass | 12892b0 | 2021-08-18 21:40:33 -0600 | [diff] [blame] | 152 | int 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 Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 178 | int ut_check_console_end(struct unit_test_state *uts) |
| 179 | { |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 180 | int ret; |
| 181 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 182 | if (!console_record_avail()) |
| 183 | return 0; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame] | 184 | ret = readline_check(uts); |
| 185 | if (ret < 0) |
| 186 | return ret; |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 187 | |
| 188 | return 1; |
| 189 | } |
| 190 | |
| 191 | int 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 Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 208 | bytes = len - 8 - 2 - 3 * 16 - 2; |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 209 | upto += bytes; |
| 210 | } |
| 211 | |
| 212 | return upto == total_bytes ? 0 : 1; |
| 213 | } |
Simon Glass | b8f0082 | 2020-11-08 21:08:43 -0700 | [diff] [blame] | 214 | |
| 215 | void 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 | |
| 225 | void ut_unsilence_console(struct unit_test_state *uts) |
| 226 | { |
| 227 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
| 228 | } |
Simon Glass | 59cad96 | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 229 | |
| 230 | void 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 | } |