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 | |
| 8 | #include <common.h> |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 9 | #include <console.h> |
Simon Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | b8f0082 | 2020-11-08 21:08:43 -0700 | [diff] [blame] | 11 | #ifdef CONFIG_SANDBOX |
| 12 | #include <asm/state.h> |
| 13 | #endif |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 15 | #include <test/test.h> |
| 16 | #include <test/ut.h> |
| 17 | |
Simon Glass | fe6d12a | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | void ut_fail(struct unit_test_state *uts, const char *fname, int line, |
| 21 | const char *func, const char *cond) |
| 22 | { |
Simon Glass | fe6d12a | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 23 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 24 | printf("%s:%d, %s(): %s\n", fname, line, func, cond); |
| 25 | uts->fail_count++; |
| 26 | } |
| 27 | |
| 28 | void 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 Glass | fe6d12a | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 33 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
Joe Hershberger | 3a77be5 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 34 | 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 Glass | 19920d7 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 41 | |
| 42 | ulong ut_check_free(void) |
| 43 | { |
| 44 | struct mallinfo info = mallinfo(); |
| 45 | |
| 46 | return info.uordblks; |
| 47 | } |
| 48 | |
| 49 | long ut_check_delta(ulong last) |
| 50 | { |
| 51 | return ut_check_free() - last; |
| 52 | } |
| 53 | |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame^] | 54 | static 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 Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 68 | int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...) |
| 69 | { |
| 70 | va_list args; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame^] | 71 | int ret; |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 72 | |
| 73 | va_start(args, fmt); |
| 74 | vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); |
| 75 | va_end(args); |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame^] | 76 | ret = readline_check(uts); |
| 77 | if (ret < 0) |
| 78 | return ret; |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 79 | |
| 80 | return strcmp(uts->expect_str, uts->actual_str); |
| 81 | } |
| 82 | |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 83 | int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...) |
| 84 | { |
| 85 | va_list args; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame^] | 86 | int ret; |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 87 | |
| 88 | va_start(args, fmt); |
| 89 | vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); |
| 90 | va_end(args); |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame^] | 91 | ret = readline_check(uts); |
| 92 | if (ret < 0) |
| 93 | return ret; |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 94 | |
| 95 | return strncmp(uts->expect_str, uts->actual_str, |
| 96 | strlen(uts->expect_str)); |
| 97 | } |
| 98 | |
| 99 | int ut_check_skipline(struct unit_test_state *uts) |
| 100 | { |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame^] | 101 | int ret; |
| 102 | |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 103 | if (!console_record_avail()) |
| 104 | return -ENFILE; |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame^] | 105 | ret = readline_check(uts); |
| 106 | if (ret < 0) |
| 107 | return ret; |
Simon Glass | c963df9 | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 112 | int ut_check_console_end(struct unit_test_state *uts) |
| 113 | { |
Simon Glass | d6ed4af | 2021-05-08 06:59:56 -0600 | [diff] [blame^] | 114 | int ret; |
| 115 | |
Simon Glass | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 116 | if (!console_record_avail()) |
| 117 | return 0; |
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 | d856e91 | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 121 | |
| 122 | return 1; |
| 123 | } |
| 124 | |
| 125 | int ut_check_console_dump(struct unit_test_state *uts, int total_bytes) |
| 126 | { |
| 127 | char *str = uts->actual_str; |
| 128 | int upto; |
| 129 | |
| 130 | /* Handle empty dump */ |
| 131 | if (!total_bytes) |
| 132 | return 0; |
| 133 | |
| 134 | for (upto = 0; upto < total_bytes;) { |
| 135 | int len; |
| 136 | int bytes; |
| 137 | |
| 138 | len = console_record_readline(str, sizeof(uts->actual_str)); |
| 139 | if (str[8] != ':' || str[9] != ' ') |
| 140 | return 1; |
| 141 | |
| 142 | bytes = len - 8 - 2 - 3 * 16 - 4; |
| 143 | upto += bytes; |
| 144 | } |
| 145 | |
| 146 | return upto == total_bytes ? 0 : 1; |
| 147 | } |
Simon Glass | b8f0082 | 2020-11-08 21:08:43 -0700 | [diff] [blame] | 148 | |
| 149 | void ut_silence_console(struct unit_test_state *uts) |
| 150 | { |
| 151 | #ifdef CONFIG_SANDBOX |
| 152 | struct sandbox_state *state = state_get_current(); |
| 153 | |
| 154 | if (!state->show_test_output) |
| 155 | gd->flags |= GD_FLG_SILENT; |
| 156 | #endif |
| 157 | } |
| 158 | |
| 159 | void ut_unsilence_console(struct unit_test_state *uts) |
| 160 | { |
| 161 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
| 162 | } |
Simon Glass | 59cad96 | 2021-03-07 17:34:55 -0700 | [diff] [blame] | 163 | |
| 164 | void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays) |
| 165 | { |
| 166 | #ifdef CONFIG_SANDBOX |
| 167 | state_set_skip_delays(skip_delays); |
| 168 | #endif |
| 169 | } |