1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2012, The Chromium Authors
9 #include <display_options.h>
12 #include <version_string.h>
14 #include <test/suites.h>
15 #include <test/test.h>
18 #define BUF_SIZE 0x100
20 #define FAKE_BUILD_TAG "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \
21 "and a lot more text to come"
23 /* Declare a new print test */
24 #define PRINT_TEST(_name, _flags) UNIT_TEST(_name, _flags, print_test)
26 #if CONFIG_IS_ENABLED(LIB_UUID)
27 /* Test printing GUIDs */
28 static int print_guid(struct unit_test_state *uts)
30 unsigned char guid[16] = {
31 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16
35 sprintf(str, "%pUb", guid);
36 ut_assertok(strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str));
37 sprintf(str, "%pUB", guid);
38 ut_assertok(strcmp("01020304-0506-0708-090A-0B0C0D0E0F10", str));
39 sprintf(str, "%pUl", guid);
40 ut_assertok(strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str));
41 sprintf(str, "%pUL", guid);
42 ut_assertok(strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str));
46 PRINT_TEST(print_guid, 0);
49 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
50 /* Test efi_loader specific printing */
51 static int print_efi_ut(struct unit_test_state *uts)
54 u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
55 sizeof(struct efi_device_path)];
57 struct efi_device_path *dp_end;
58 struct efi_device_path_sd_mmc_path *dp_sd =
59 (struct efi_device_path_sd_mmc_path *)pos;
61 /* Create a device path for an SD card */
62 dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
63 dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
64 dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
65 dp_sd->slot_number = 3;
66 pos += sizeof(struct efi_device_path_sd_mmc_path);
68 dp_end = (struct efi_device_path *)pos;
69 dp_end->type = DEVICE_PATH_TYPE_END;
70 dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
71 dp_end->length = sizeof(struct efi_device_path);
73 snprintf(str, sizeof(str), "_%pD_", buf);
74 ut_assertok(strcmp("_/SD(3)_", str));
76 /* NULL device path */
77 snprintf(str, sizeof(str), "_%pD_", NULL);
78 ut_assertok(strcmp("_<NULL>_", str));
82 PRINT_TEST(print_efi_ut, 0);
85 static int print_printf(struct unit_test_state *uts)
92 snprintf(str, sizeof(str), "testing");
93 ut_assertok(strcmp("testing", str));
95 snprintf(str, sizeof(str), "testing but too long");
96 ut_assertok(strcmp("testing b", str));
98 snprintf(str, 1, "testing none");
99 ut_assertok(strcmp("", str));
102 snprintf(str, 0, "testing none");
103 ut_asserteq('x', *str);
105 sprintf(big_str, "_%ls_", L"foo");
106 ut_assertok(strcmp("_foo_", big_str));
108 /* Test the banner function */
109 s = display_options_get_banner(true, str, sizeof(str));
110 ut_asserteq_ptr(str, s);
111 ut_assertok(strcmp("\n\nU-Boo\n\n", s));
113 /* Assert that we do not overwrite memory before the buffer */
115 s = display_options_get_banner(true, str + 1, 1);
116 ut_asserteq_ptr(str + 1, s);
117 ut_assertok(strcmp("`", str));
120 s = display_options_get_banner(true, str + 1, 2);
121 ut_asserteq_ptr(str + 1, s);
122 ut_assertok(strcmp("~\n", str));
124 /* The last two characters are set to \n\n for all buffer sizes > 2 */
125 s = display_options_get_banner(false, str, sizeof(str));
126 ut_asserteq_ptr(str, s);
127 ut_assertok(strcmp("U-Boot \n\n", s));
129 /* Give it enough space for some of the version */
130 big_str_len = strlen(version_string) - 5;
131 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
133 ut_asserteq_ptr(big_str, s);
134 ut_assertok(strncmp(version_string, s, big_str_len - 3));
135 ut_assertok(strcmp("\n\n", s + big_str_len - 3));
137 /* Give it enough space for the version and some of the build tag */
138 big_str_len = strlen(version_string) + 9 + 20;
139 s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
141 ut_asserteq_ptr(big_str, s);
142 len = strlen(version_string);
143 ut_assertok(strncmp(version_string, s, len));
144 ut_assertok(strncmp(", Build: ", s + len, 9));
145 ut_assertok(strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
146 ut_assertok(strcmp("\n\n", s + big_str_len - 3));
150 PRINT_TEST(print_printf, 0);
152 static int print_display_buffer(struct unit_test_state *uts)
157 buf = map_sysmem(0, BUF_SIZE);
158 memset(buf, '\0', BUF_SIZE);
159 for (i = 0; i < 0x11; i++)
163 console_record_reset();
164 print_buffer(0, buf, 1, 0x12, 0);
165 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
166 ut_assert_nextline("00000010: 10 00 ..");
167 ut_assert_console_end();
170 console_record_reset();
171 print_buffer(0, buf, 1, 0x12, 8);
172 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw");
173 ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........");
174 ut_assert_nextline("00000010: 10 00 ..");
175 ut_assert_console_end();
178 console_record_reset();
180 print_buffer(0, buf, 1, 0x42, 0x40);
181 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........................................................");
182 ut_assert_nextline("00000040: 00 41 .A");
183 ut_assert_console_end();
186 console_record_reset();
187 print_buffer(0x12345678, buf, 1, 0x12, 0);
188 ut_assert_nextline("12345678: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
189 ut_assert_nextline("12345688: 10 00 ..");
190 ut_assert_console_end();
193 console_record_reset();
194 print_buffer(0, buf, 2, 9, 0);
195 ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........");
196 ut_assert_nextline("00000010: 0010 ..");
197 ut_assert_console_end();
200 console_record_reset();
201 print_buffer(0, buf, 4, 5, 0);
202 ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........");
203 ut_assert_nextline("00000010: 00000010 ....");
204 ut_assert_console_end();
207 console_record_reset();
208 print_buffer(0, buf, 8, 3, 0);
209 ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........");
210 ut_assert_nextline("00000010: 0000000000000010 ........");
211 ut_assert_console_end();
214 console_record_reset();
218 for (i = 0; i < 4; i++)
219 buf[4 + i] = 126 + i;
221 print_buffer(0, buf, 1, 10, 0);
222 ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....");
223 ut_assert_console_end();
229 PRINT_TEST(print_display_buffer, UT_TESTF_CONSOLE_REC);
231 static int print_hexdump_line(struct unit_test_state *uts)
237 buf = map_sysmem(0, BUF_SIZE);
238 memset(buf, '\0', BUF_SIZE);
239 for (i = 0; i < 0x11; i++)
242 /* Check buffer size calculations */
243 linebuf = map_sysmem(0x400, BUF_SIZE);
244 memset(linebuf, '\xff', BUF_SIZE);
245 ut_asserteq(-ENOSPC, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 75));
246 ut_asserteq(-1, linebuf[0]);
247 ut_asserteq(0x10, hexdump_line(0, buf, 1, 0x10, 0, linebuf, 76));
248 ut_asserteq(0, linebuf[75]);
249 ut_asserteq(-1, linebuf[76]);
255 PRINT_TEST(print_hexdump_line, UT_TESTF_CONSOLE_REC);
257 static int print_do_hex_dump(struct unit_test_state *uts)
262 buf = map_sysmem(0, BUF_SIZE);
263 memset(buf, '\0', BUF_SIZE);
264 for (i = 0; i < 0x11; i++)
268 console_record_reset();
269 print_hex_dump_bytes("", DUMP_PREFIX_ADDRESS, buf, 0x12);
270 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 88 99 aa bb cc dd ee ff ..\"3DUfw........");
271 ut_assert_nextline("00000010: 10 00 ..");
272 ut_assert_console_end();
275 console_record_reset();
276 print_hex_dump("", DUMP_PREFIX_ADDRESS, 8, 1, buf, 0x12, true);
277 ut_assert_nextline("00000000: 00 11 22 33 44 55 66 77 ..\"3DUfw");
278 ut_assert_nextline("00000008: 88 99 aa bb cc dd ee ff ........");
279 ut_assert_nextline("00000010: 10 00 ..");
280 ut_assert_console_end();
284 console_record_reset();
286 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0x40, 1, buf, 0x42, true);
287 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........................................................");
288 ut_assert_nextline("00000040: 00 41 .A");
289 ut_assert_console_end();
292 console_record_reset();
293 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 2, buf, 0x12, true);
294 ut_assert_nextline("00000000: 1100 3322 5544 7766 9988 bbaa ddcc ffee ..\"3DUfw........");
295 ut_assert_nextline("00000010: 0010 ..");
296 ut_assert_console_end();
300 console_record_reset();
301 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 4, buf, 0x14, true);
302 ut_assert_nextline("00000000: 33221100 77665544 bbaa9988 ffeeddcc ..\"3DUfw........");
303 ut_assert_nextline("00000010: 00000010 ....");
304 ut_assert_console_end();
308 console_record_reset();
309 print_hex_dump("", DUMP_PREFIX_ADDRESS, 16, 8, buf, 0x18, true);
310 ut_assert_nextline("00000000: 7766554433221100 ffeeddccbbaa9988 ..\"3DUfw........");
311 ut_assert_nextline("00000010: 0000000000000010 ........");
312 ut_assert_console_end();
316 console_record_reset();
320 for (i = 0; i < 4; i++)
321 buf[4 + i] = 126 + i;
323 print_hex_dump("", DUMP_PREFIX_ADDRESS, 0, 1, buf, 10, true);
324 ut_assert_nextline("00000000: 00 1f 20 21 7e 7f 80 81 ff 99 .. !~.....");
325 ut_assert_console_end();
330 PRINT_TEST(print_do_hex_dump, UT_TESTF_CONSOLE_REC);
332 static int print_itoa(struct unit_test_state *uts)
334 ut_asserteq_str("123", simple_itoa(123));
335 ut_asserteq_str("0", simple_itoa(0));
336 ut_asserteq_str("2147483647", simple_itoa(0x7fffffff));
337 ut_asserteq_str("4294967295", simple_itoa(0xffffffff));
338 if (sizeof(ulong) == 8) {
339 ut_asserteq_str("9223372036854775807",
340 simple_itoa((1UL << 63) - 1));
341 ut_asserteq_str("18446744073709551615", simple_itoa(-1));
346 PRINT_TEST(print_itoa, 0);
348 int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
350 struct unit_test *tests = UNIT_TEST_SUITE_START(print_test);
351 const int n_ents = UNIT_TEST_SUITE_COUNT(print_test);
353 return cmd_ut_category("print", "print_", tests, n_ents, argc, argv);