Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Simple unit test library |
| 4 | * |
| 5 | * Copyright (c) 2013 Google, Inc |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 400175b | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 9 | #include <console.h> |
Simon Glass | 8109863 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Simon Glass | ef7e264 | 2020-11-08 21:08:43 -0700 | [diff] [blame] | 11 | #ifdef CONFIG_SANDBOX |
| 12 | #include <asm/state.h> |
| 13 | #endif |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 14 | #include <test/test.h> |
| 15 | #include <test/ut.h> |
| 16 | |
Simon Glass | 9ce8b40 | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
Joe Hershberger | e721b88 | 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 | 9ce8b40 | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 22 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
Joe Hershberger | e721b88 | 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 | 9ce8b40 | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 32 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
Joe Hershberger | e721b88 | 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 | 8109863 | 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 | 400175b | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 53 | int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...) |
| 54 | { |
| 55 | va_list args; |
| 56 | |
| 57 | va_start(args, fmt); |
| 58 | vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); |
| 59 | va_end(args); |
| 60 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); |
| 61 | |
| 62 | return strcmp(uts->expect_str, uts->actual_str); |
| 63 | } |
| 64 | |
Simon Glass | 33d7edf | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 65 | int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...) |
| 66 | { |
| 67 | va_list args; |
| 68 | |
| 69 | va_start(args, fmt); |
| 70 | vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); |
| 71 | va_end(args); |
| 72 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); |
| 73 | |
| 74 | return strncmp(uts->expect_str, uts->actual_str, |
| 75 | strlen(uts->expect_str)); |
| 76 | } |
| 77 | |
| 78 | int ut_check_skipline(struct unit_test_state *uts) |
| 79 | { |
| 80 | if (!console_record_avail()) |
| 81 | return -ENFILE; |
| 82 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); |
| 83 | |
| 84 | return 0; |
| 85 | } |
| 86 | |
Simon Glass | 400175b | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 87 | int ut_check_console_end(struct unit_test_state *uts) |
| 88 | { |
| 89 | if (!console_record_avail()) |
| 90 | return 0; |
| 91 | |
| 92 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); |
| 93 | |
| 94 | return 1; |
| 95 | } |
| 96 | |
| 97 | int ut_check_console_dump(struct unit_test_state *uts, int total_bytes) |
| 98 | { |
| 99 | char *str = uts->actual_str; |
| 100 | int upto; |
| 101 | |
| 102 | /* Handle empty dump */ |
| 103 | if (!total_bytes) |
| 104 | return 0; |
| 105 | |
| 106 | for (upto = 0; upto < total_bytes;) { |
| 107 | int len; |
| 108 | int bytes; |
| 109 | |
| 110 | len = console_record_readline(str, sizeof(uts->actual_str)); |
| 111 | if (str[8] != ':' || str[9] != ' ') |
| 112 | return 1; |
| 113 | |
| 114 | bytes = len - 8 - 2 - 3 * 16 - 4; |
| 115 | upto += bytes; |
| 116 | } |
| 117 | |
| 118 | return upto == total_bytes ? 0 : 1; |
| 119 | } |
Simon Glass | ef7e264 | 2020-11-08 21:08:43 -0700 | [diff] [blame] | 120 | |
| 121 | void ut_silence_console(struct unit_test_state *uts) |
| 122 | { |
| 123 | #ifdef CONFIG_SANDBOX |
| 124 | struct sandbox_state *state = state_get_current(); |
| 125 | |
| 126 | if (!state->show_test_output) |
| 127 | gd->flags |= GD_FLG_SILENT; |
| 128 | #endif |
| 129 | } |
| 130 | |
| 131 | void ut_unsilence_console(struct unit_test_state *uts) |
| 132 | { |
| 133 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
| 134 | } |