Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, The Chromium Authors |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 7 | #include <command.h> |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 8 | #include <efi_api.h> |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 9 | #include <display_options.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 11 | #include <mapmem.h> |
Pali Rohár | ba87ddf | 2021-08-02 15:18:31 +0200 | [diff] [blame] | 12 | #include <version_string.h> |
Simon Glass | 978739d | 2021-10-14 12:48:06 -0600 | [diff] [blame] | 13 | #include <vsprintf.h> |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 14 | #include <test/suites.h> |
| 15 | #include <test/test.h> |
| 16 | #include <test/ut.h> |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 17 | |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 18 | #define BUF_SIZE 0x100 |
| 19 | |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 20 | #define FAKE_BUILD_TAG "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \ |
| 21 | "and a lot more text to come" |
| 22 | |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 23 | /* Declare a new print test */ |
| 24 | #define PRINT_TEST(_name, _flags) UNIT_TEST(_name, _flags, print_test) |
| 25 | |
| 26 | #if CONFIG_IS_ENABLED(LIB_UUID) |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 27 | /* Test printing GUIDs */ |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 28 | static int print_guid(struct unit_test_state *uts) |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 29 | { |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 30 | unsigned char guid[16] = { |
| 31 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 |
| 32 | }; |
| 33 | char str[40]; |
Heinrich Schuchardt | 7eaa37b | 2021-11-15 19:06:55 +0100 | [diff] [blame] | 34 | int ret; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 35 | |
| 36 | sprintf(str, "%pUb", guid); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 37 | ut_assertok(strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str)); |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 38 | sprintf(str, "%pUB", guid); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 39 | ut_assertok(strcmp("01020304-0506-0708-090A-0B0C0D0E0F10", str)); |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 40 | sprintf(str, "%pUl", guid); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 41 | ut_assertok(strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str)); |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 42 | sprintf(str, "%pUL", guid); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 43 | ut_assertok(strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str)); |
Heinrich Schuchardt | 7eaa37b | 2021-11-15 19:06:55 +0100 | [diff] [blame] | 44 | ret = snprintf(str, 4, "%pUL", guid); |
| 45 | ut_asserteq(0, str[3]); |
| 46 | ut_asserteq(36, ret); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 47 | |
| 48 | return 0; |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 49 | } |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 50 | PRINT_TEST(print_guid, 0); |
| 51 | #endif |
Heinrich Schuchardt | 6f03566 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 52 | |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 53 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 54 | /* Test efi_loader specific printing */ |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 55 | static int print_efi_ut(struct unit_test_state *uts) |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 56 | { |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 57 | char str[10]; |
| 58 | u8 buf[sizeof(struct efi_device_path_sd_mmc_path) + |
| 59 | sizeof(struct efi_device_path)]; |
| 60 | u8 *pos = buf; |
| 61 | struct efi_device_path *dp_end; |
| 62 | struct efi_device_path_sd_mmc_path *dp_sd = |
| 63 | (struct efi_device_path_sd_mmc_path *)pos; |
| 64 | |
| 65 | /* Create a device path for an SD card */ |
| 66 | dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 67 | dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD; |
| 68 | dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path); |
| 69 | dp_sd->slot_number = 3; |
| 70 | pos += sizeof(struct efi_device_path_sd_mmc_path); |
| 71 | /* Append end node */ |
| 72 | dp_end = (struct efi_device_path *)pos; |
| 73 | dp_end->type = DEVICE_PATH_TYPE_END; |
| 74 | dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 75 | dp_end->length = sizeof(struct efi_device_path); |
| 76 | |
| 77 | snprintf(str, sizeof(str), "_%pD_", buf); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 78 | ut_assertok(strcmp("_/SD(3)_", str)); |
Heinrich Schuchardt | 3b47f33 | 2018-01-26 06:30:30 +0100 | [diff] [blame] | 79 | |
| 80 | /* NULL device path */ |
| 81 | snprintf(str, sizeof(str), "_%pD_", NULL); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 82 | ut_assertok(strcmp("_<NULL>_", str)); |
| 83 | |
| 84 | return 0; |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 85 | } |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 86 | PRINT_TEST(print_efi_ut, 0); |
| 87 | #endif |
Heinrich Schuchardt | 218d389 | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 88 | |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 89 | static int print_printf(struct unit_test_state *uts) |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 90 | { |
| 91 | char big_str[400]; |
| 92 | int big_str_len; |
| 93 | char str[10], *s; |
| 94 | int len; |
| 95 | |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 96 | snprintf(str, sizeof(str), "testing"); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 97 | ut_assertok(strcmp("testing", str)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 98 | |
| 99 | snprintf(str, sizeof(str), "testing but too long"); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 100 | ut_assertok(strcmp("testing b", str)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 101 | |
| 102 | snprintf(str, 1, "testing none"); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 103 | ut_assertok(strcmp("", str)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 104 | |
| 105 | *str = 'x'; |
| 106 | snprintf(str, 0, "testing none"); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 107 | ut_asserteq('x', *str); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 108 | |
Rob Clark | 22ae0a7 | 2017-09-13 18:46:54 -0400 | [diff] [blame] | 109 | sprintf(big_str, "_%ls_", L"foo"); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 110 | ut_assertok(strcmp("_foo_", big_str)); |
Rob Clark | 22ae0a7 | 2017-09-13 18:46:54 -0400 | [diff] [blame] | 111 | |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 112 | /* Test the banner function */ |
| 113 | s = display_options_get_banner(true, str, sizeof(str)); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 114 | ut_asserteq_ptr(str, s); |
| 115 | ut_assertok(strcmp("\n\nU-Boo\n\n", s)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 116 | |
Heinrich Schuchardt | b144ab1 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 117 | /* Assert that we do not overwrite memory before the buffer */ |
| 118 | str[0] = '`'; |
| 119 | s = display_options_get_banner(true, str + 1, 1); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 120 | ut_asserteq_ptr(str + 1, s); |
| 121 | ut_assertok(strcmp("`", str)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 122 | |
Heinrich Schuchardt | b144ab1 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 123 | str[0] = '~'; |
| 124 | s = display_options_get_banner(true, str + 1, 2); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 125 | ut_asserteq_ptr(str + 1, s); |
| 126 | ut_assertok(strcmp("~\n", str)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 127 | |
Heinrich Schuchardt | b144ab1 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 128 | /* The last two characters are set to \n\n for all buffer sizes > 2 */ |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 129 | s = display_options_get_banner(false, str, sizeof(str)); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 130 | ut_asserteq_ptr(str, s); |
| 131 | ut_assertok(strcmp("U-Boot \n\n", s)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 132 | |
| 133 | /* Give it enough space for some of the version */ |
| 134 | big_str_len = strlen(version_string) - 5; |
| 135 | s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str, |
| 136 | big_str_len); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 137 | ut_asserteq_ptr(big_str, s); |
| 138 | ut_assertok(strncmp(version_string, s, big_str_len - 3)); |
| 139 | ut_assertok(strcmp("\n\n", s + big_str_len - 3)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 140 | |
| 141 | /* Give it enough space for the version and some of the build tag */ |
| 142 | big_str_len = strlen(version_string) + 9 + 20; |
| 143 | s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str, |
| 144 | big_str_len); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 145 | ut_asserteq_ptr(big_str, s); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 146 | len = strlen(version_string); |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 147 | ut_assertok(strncmp(version_string, s, len)); |
| 148 | ut_assertok(strncmp(", Build: ", s + len, 9)); |
| 149 | ut_assertok(strncmp(FAKE_BUILD_TAG, s + 9 + len, 12)); |
| 150 | ut_assertok(strcmp("\n\n", s + big_str_len - 3)); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 151 | |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 152 | return 0; |
| 153 | } |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 154 | PRINT_TEST(print_printf, 0); |
Simon Glass | 75ee32f | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 155 | |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 156 | static int print_display_buffer(struct unit_test_state *uts) |
| 157 | { |
| 158 | u8 *buf; |
| 159 | int i; |
| 160 | |
| 161 | buf = map_sysmem(0, BUF_SIZE); |
| 162 | memset(buf, '\0', BUF_SIZE); |
| 163 | for (i = 0; i < 0x11; i++) |
| 164 | buf[i] = i * 0x11; |
| 165 | |
| 166 | /* bytes */ |
| 167 | console_record_reset(); |
| 168 | print_buffer(0, buf, 1, 0x12, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 169 | ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........"); |
| 170 | ut_assert_nextline("00000010: 10 00 .."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 171 | ut_assert_console_end(); |
| 172 | |
| 173 | /* line length */ |
| 174 | console_record_reset(); |
| 175 | print_buffer(0, buf, 1, 0x12, 8); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 176 | ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw"); |
| 177 | ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........"); |
| 178 | ut_assert_nextline("00000010: 10 00 .."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 179 | ut_assert_console_end(); |
| 180 | |
| 181 | /* long line */ |
| 182 | console_record_reset(); |
| 183 | buf[0x41] = 0x41; |
| 184 | print_buffer(0, buf, 1, 0x42, 0x40); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 185 | ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ..\"3DUfw........................................................"); |
| 186 | ut_assert_nextline("00000040: 00 41 .A"); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 187 | ut_assert_console_end(); |
| 188 | |
| 189 | /* address */ |
| 190 | console_record_reset(); |
| 191 | print_buffer(0x12345678, buf, 1, 0x12, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 192 | ut_assert_nextline("12345678: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........"); |
| 193 | ut_assert_nextline("12345688: 10 00 .."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 194 | ut_assert_console_end(); |
| 195 | |
| 196 | /* 16-bit */ |
| 197 | console_record_reset(); |
| 198 | print_buffer(0, buf, 2, 9, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 199 | ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........"); |
| 200 | ut_assert_nextline("00000010: 0010 .."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 201 | ut_assert_console_end(); |
| 202 | |
| 203 | /* 32-bit */ |
| 204 | console_record_reset(); |
| 205 | print_buffer(0, buf, 4, 5, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 206 | ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........"); |
| 207 | ut_assert_nextline("00000010: 00000010 ...."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 208 | ut_assert_console_end(); |
| 209 | |
| 210 | /* 64-bit */ |
| 211 | console_record_reset(); |
| 212 | print_buffer(0, buf, 8, 3, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 213 | ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........"); |
| 214 | ut_assert_nextline("00000010: 0000000000000010 ........"); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 215 | ut_assert_console_end(); |
| 216 | |
| 217 | /* ASCII */ |
| 218 | console_record_reset(); |
| 219 | buf[1] = 31; |
| 220 | buf[2] = 32; |
| 221 | buf[3] = 33; |
| 222 | for (i = 0; i < 4; i++) |
| 223 | buf[4 + i] = 126 + i; |
| 224 | buf[8] = 255; |
| 225 | print_buffer(0, buf, 1, 10, 0); |
Simon Glass | 85c8fc5 | 2021-05-08 07:00:00 -0600 | [diff] [blame] | 226 | ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~....."); |
Simon Glass | 4a249ba | 2021-05-08 06:59:59 -0600 | [diff] [blame] | 227 | ut_assert_console_end(); |
| 228 | |
| 229 | unmap_sysmem(buf); |
| 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | PRINT_TEST(print_display_buffer, UT_TESTF_CONSOLE_REC); |
| 234 | |
Simon Glass | 02b8f1a | 2021-05-08 07:00:05 -0600 | [diff] [blame] | 235 | static int print_hexdump_line(struct unit_test_state *uts) |
| 236 | { |
| 237 | char *linebuf; |
| 238 | u8 *buf; |
| 239 | int i; |
| 240 | |
| 241 | buf = map_sysmem(0, BUF_SIZE); |
| 242 | memset(buf, '\0', BUF_SIZE); |
| 243 | for (i = 0; i < 0x11; i++) |
| 244 | buf[i] = i * 0x11; |
| 245 | |
| 246 | /* Check buffer size calculations */ |
| 247 | linebuf = map_sysmem(0x400, BUF_SIZE); |
| 248 | memset(linebuf, '\xff', BUF_SIZE); |
| 249 | ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75)); |
| 250 | ut_asserteq(-1, linebuf[0]); |
| 251 | ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76)); |
| 252 | ut_asserteq(0, linebuf[75]); |
| 253 | ut_asserteq(-1, linebuf[76]); |
| 254 | |
| 255 | unmap_sysmem(buf); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | PRINT_TEST(print_hexdump_line, UT_TESTF_CONSOLE_REC); |
| 260 | |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 261 | static int print_do_hex_dump(struct unit_test_state *uts) |
| 262 | { |
| 263 | u8 *buf; |
| 264 | int i; |
| 265 | |
| 266 | buf = map_sysmem(0, BUF_SIZE); |
| 267 | memset(buf, '\0', BUF_SIZE); |
| 268 | for (i = 0; i < 0x11; i++) |
| 269 | buf[i] = i * 0x11; |
| 270 | |
| 271 | /* bytes */ |
| 272 | console_record_reset(); |
| 273 | print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12); |
| 274 | ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........"); |
| 275 | ut_assert_nextline("00000010: 10 00 .."); |
| 276 | ut_assert_console_end(); |
| 277 | |
Simon Glass | f1d0647 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 278 | /* line length */ |
| 279 | console_record_reset(); |
| 280 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true); |
| 281 | ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw"); |
| 282 | ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........"); |
| 283 | ut_assert_nextline("00000010: 10 00 .."); |
| 284 | ut_assert_console_end(); |
| 285 | unmap_sysmem(buf); |
| 286 | |
| 287 | /* long line */ |
| 288 | console_record_reset(); |
| 289 | buf[0x41] = 0x41; |
| 290 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true); |
| 291 | ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ..\"3DUfw........................................................"); |
| 292 | ut_assert_nextline("00000040: 00 41 .A"); |
| 293 | ut_assert_console_end(); |
| 294 | |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 295 | /* 16-bit */ |
| 296 | console_record_reset(); |
Simon Glass | f1d0647 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 297 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true); |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 298 | ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........"); |
| 299 | ut_assert_nextline("00000010: 0010 .."); |
| 300 | ut_assert_console_end(); |
| 301 | unmap_sysmem(buf); |
| 302 | |
| 303 | /* 32-bit */ |
| 304 | console_record_reset(); |
Simon Glass | f1d0647 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 305 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true); |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 306 | ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........"); |
| 307 | ut_assert_nextline("00000010: 00000010 ...."); |
| 308 | ut_assert_console_end(); |
| 309 | unmap_sysmem(buf); |
| 310 | |
| 311 | /* 64-bit */ |
| 312 | console_record_reset(); |
| 313 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true); |
| 314 | ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........"); |
| 315 | ut_assert_nextline("00000010: 0000000000000010 ........"); |
| 316 | ut_assert_console_end(); |
| 317 | unmap_sysmem(buf); |
| 318 | |
| 319 | /* ASCII */ |
| 320 | console_record_reset(); |
| 321 | buf[1] = 31; |
| 322 | buf[2] = 32; |
| 323 | buf[3] = 33; |
| 324 | for (i = 0; i < 4; i++) |
| 325 | buf[4 + i] = 126 + i; |
| 326 | buf[8] = 255; |
Simon Glass | f1d0647 | 2021-05-08 07:00:03 -0600 | [diff] [blame] | 327 | print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true); |
Simon Glass | 2650715 | 2021-05-08 07:00:02 -0600 | [diff] [blame] | 328 | ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~....."); |
| 329 | ut_assert_console_end(); |
| 330 | unmap_sysmem(buf); |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | PRINT_TEST(print_do_hex_dump, UT_TESTF_CONSOLE_REC); |
| 335 | |
Simon Glass | 978739d | 2021-10-14 12:48:06 -0600 | [diff] [blame] | 336 | static int print_itoa(struct unit_test_state *uts) |
| 337 | { |
| 338 | ut_asserteq_str("123", simple_itoa(123)); |
| 339 | ut_asserteq_str("0", simple_itoa(0)); |
| 340 | ut_asserteq_str("2147483647", simple_itoa(0x7fffffff)); |
| 341 | ut_asserteq_str("4294967295", simple_itoa(0xffffffff)); |
Simon Glass | 1f46718 | 2021-10-14 12:48:07 -0600 | [diff] [blame] | 342 | |
| 343 | /* Use #ifdef here to avoid a compiler warning on 32-bit machines */ |
| 344 | #ifdef CONFIG_PHYS_64BIT |
Simon Glass | 978739d | 2021-10-14 12:48:06 -0600 | [diff] [blame] | 345 | if (sizeof(ulong) == 8) { |
| 346 | ut_asserteq_str("9223372036854775807", |
| 347 | simple_itoa((1UL << 63) - 1)); |
| 348 | ut_asserteq_str("18446744073709551615", simple_itoa(-1)); |
| 349 | } |
Simon Glass | 1f46718 | 2021-10-14 12:48:07 -0600 | [diff] [blame] | 350 | #endif /* CONFIG_PHYS_64BIT */ |
Simon Glass | 978739d | 2021-10-14 12:48:06 -0600 | [diff] [blame] | 351 | |
| 352 | return 0; |
| 353 | } |
| 354 | PRINT_TEST(print_itoa, 0); |
| 355 | |
Heinrich Schuchardt | 7eaa37b | 2021-11-15 19:06:55 +0100 | [diff] [blame] | 356 | static int snprint(struct unit_test_state *uts) |
| 357 | { |
| 358 | char buf[10] = "xxxxxxxxx"; |
| 359 | int ret; |
| 360 | |
| 361 | ret = snprintf(buf, 4, "%s:%s", "abc", "def"); |
| 362 | ut_asserteq(0, buf[3]); |
| 363 | ut_asserteq(7, ret); |
| 364 | ret = snprintf(buf, 4, "%s:%d", "abc", 9999); |
| 365 | ut_asserteq(8, ret); |
| 366 | return 0; |
| 367 | } |
| 368 | PRINT_TEST(snprint, 0); |
| 369 | |
Simon Glass | 1f46718 | 2021-10-14 12:48:07 -0600 | [diff] [blame] | 370 | static int print_xtoa(struct unit_test_state *uts) |
| 371 | { |
| 372 | ut_asserteq_str("7f", simple_xtoa(127)); |
| 373 | ut_asserteq_str("00", simple_xtoa(0)); |
| 374 | ut_asserteq_str("7fffffff", simple_xtoa(0x7fffffff)); |
| 375 | ut_asserteq_str("ffffffff", simple_xtoa(0xffffffff)); |
| 376 | |
| 377 | /* Use #ifdef here to avoid a compiler warning on 32-bit machines */ |
| 378 | #ifdef CONFIG_PHYS_64BIT |
| 379 | if (sizeof(ulong) == 8) { |
| 380 | ut_asserteq_str("7fffffffffffffff", |
| 381 | simple_xtoa((1UL << 63) - 1)); |
| 382 | ut_asserteq_str("ffffffffffffffff", simple_xtoa(-1)); |
| 383 | } |
| 384 | #endif /* CONFIG_PHYS_64BIT */ |
| 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | PRINT_TEST(print_xtoa, 0); |
| 389 | |
Simon Glass | d242368 | 2021-05-08 06:59:58 -0600 | [diff] [blame] | 390 | int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 391 | { |
| 392 | struct unit_test *tests = UNIT_TEST_SUITE_START(print_test); |
| 393 | const int n_ents = UNIT_TEST_SUITE_COUNT(print_test); |
| 394 | |
| 395 | return cmd_ut_category("print", "print_", tests, n_ents, argc, argv); |
| 396 | } |