Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012, The Chromium Authors |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 4 | */ |
| 5 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 6 | #include <common.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 7 | #include <command.h> |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 8 | #include <efi_api.h> |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 9 | #include <display_options.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 11 | #include <version.h> |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 12 | #include <test/suites.h> |
| 13 | #include <test/test.h> |
| 14 | #include <test/ut.h> |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 15 | |
| 16 | #define FAKE_BUILD_TAG "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \ |
| 17 | "and a lot more text to come" |
| 18 | |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 19 | /* Declare a new print test */ |
| 20 | #define PRINT_TEST(_name, _flags) UNIT_TEST(_name, _flags, print_test) |
| 21 | |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 22 | #if CONFIG_IS_ENABLED(LIB_UUID) |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 23 | /* Test printing GUIDs */ |
| 24 | static int print_guid(struct unit_test_state *uts) |
| 25 | { |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 26 | unsigned char guid[16] = { |
| 27 | 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 |
| 28 | }; |
| 29 | char str[40]; |
| 30 | |
| 31 | sprintf(str, "%pUb", guid); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 32 | ut_assertok(strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str)); |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 33 | sprintf(str, "%pUB", guid); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 34 | ut_assertok(strcmp("01020304-0506-0708-090A-0B0C0D0E0F10", str)); |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 35 | sprintf(str, "%pUl", guid); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 36 | ut_assertok(strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str)); |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 37 | sprintf(str, "%pUL", guid); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 38 | ut_assertok(strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str)); |
Heinrich Schuchardt | 3bad256 | 2019-04-29 08:08:43 +0200 | [diff] [blame] | 39 | |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 40 | return 0; |
| 41 | } |
| 42 | PRINT_TEST(print_guid, 0); |
| 43 | #endif |
| 44 | |
Heinrich Schuchardt | 7a9e6ee | 2018-08-31 21:31:24 +0200 | [diff] [blame] | 45 | #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD) |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 46 | /* Test efi_loader specific printing */ |
| 47 | static int print_efi_ut(struct unit_test_state *uts) |
| 48 | { |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 49 | char str[10]; |
| 50 | u8 buf[sizeof(struct efi_device_path_sd_mmc_path) + |
| 51 | sizeof(struct efi_device_path)]; |
| 52 | u8 *pos = buf; |
| 53 | struct efi_device_path *dp_end; |
| 54 | struct efi_device_path_sd_mmc_path *dp_sd = |
| 55 | (struct efi_device_path_sd_mmc_path *)pos; |
| 56 | |
| 57 | /* Create a device path for an SD card */ |
| 58 | dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE; |
| 59 | dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD; |
| 60 | dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path); |
| 61 | dp_sd->slot_number = 3; |
| 62 | pos += sizeof(struct efi_device_path_sd_mmc_path); |
| 63 | /* Append end node */ |
| 64 | dp_end = (struct efi_device_path *)pos; |
| 65 | dp_end->type = DEVICE_PATH_TYPE_END; |
| 66 | dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 67 | dp_end->length = sizeof(struct efi_device_path); |
| 68 | |
| 69 | snprintf(str, sizeof(str), "_%pD_", buf); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 70 | ut_assertok(strcmp("_/SD(3)_", str)); |
Heinrich Schuchardt | 5f1ce1d | 2018-01-26 06:30:30 +0100 | [diff] [blame] | 71 | |
| 72 | /* NULL device path */ |
| 73 | snprintf(str, sizeof(str), "_%pD_", NULL); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 74 | ut_assertok(strcmp("_<NULL>_", str)); |
Heinrich Schuchardt | 256060e | 2018-01-10 18:06:08 +0100 | [diff] [blame] | 75 | |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 76 | return 0; |
| 77 | } |
| 78 | PRINT_TEST(print_efi_ut, 0); |
| 79 | #endif |
| 80 | |
| 81 | static int print_printf(struct unit_test_state *uts) |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 82 | { |
| 83 | char big_str[400]; |
| 84 | int big_str_len; |
| 85 | char str[10], *s; |
| 86 | int len; |
| 87 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 88 | snprintf(str, sizeof(str), "testing"); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 89 | ut_assertok(strcmp("testing", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 90 | |
| 91 | snprintf(str, sizeof(str), "testing but too long"); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 92 | ut_assertok(strcmp("testing b", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 93 | |
| 94 | snprintf(str, 1, "testing none"); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 95 | ut_assertok(strcmp("", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 96 | |
| 97 | *str = 'x'; |
| 98 | snprintf(str, 0, "testing none"); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 99 | ut_asserteq('x', *str); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 100 | |
Rob Clark | 085391b | 2017-09-13 18:46:54 -0400 | [diff] [blame] | 101 | sprintf(big_str, "_%ls_", L"foo"); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 102 | ut_assertok(strcmp("_foo_", big_str)); |
Rob Clark | 085391b | 2017-09-13 18:46:54 -0400 | [diff] [blame] | 103 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 104 | /* Test the banner function */ |
| 105 | s = display_options_get_banner(true, str, sizeof(str)); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 106 | ut_asserteq_ptr(str, s); |
| 107 | ut_assertok(strcmp("\n\nU-Boo\n\n", s)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 108 | |
Heinrich Schuchardt | 6c74e94 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 109 | /* Assert that we do not overwrite memory before the buffer */ |
| 110 | str[0] = '`'; |
| 111 | s = display_options_get_banner(true, str + 1, 1); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 112 | ut_asserteq_ptr(str + 1, s); |
| 113 | ut_assertok(strcmp("`", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 114 | |
Heinrich Schuchardt | 6c74e94 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 115 | str[0] = '~'; |
| 116 | s = display_options_get_banner(true, str + 1, 2); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 117 | ut_asserteq_ptr(str + 1, s); |
| 118 | ut_assertok(strcmp("~\n", str)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 119 | |
Heinrich Schuchardt | 6c74e94 | 2019-04-26 18:39:00 +0200 | [diff] [blame] | 120 | /* The last two characters are set to \n\n for all buffer sizes > 2 */ |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 121 | s = display_options_get_banner(false, str, sizeof(str)); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 122 | ut_asserteq_ptr(str, s); |
| 123 | ut_assertok(strcmp("U-Boot \n\n", s)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 124 | |
| 125 | /* Give it enough space for some of the version */ |
| 126 | big_str_len = strlen(version_string) - 5; |
| 127 | s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str, |
| 128 | big_str_len); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 129 | ut_asserteq_ptr(big_str, s); |
| 130 | ut_assertok(strncmp(version_string, s, big_str_len - 3)); |
| 131 | ut_assertok(strcmp("\n\n", s + big_str_len - 3)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 132 | |
| 133 | /* Give it enough space for the version and some of the build tag */ |
| 134 | big_str_len = strlen(version_string) + 9 + 20; |
| 135 | s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str, |
| 136 | big_str_len); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 137 | ut_asserteq_ptr(big_str, s); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 138 | len = strlen(version_string); |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 139 | ut_assertok(strncmp(version_string, s, len)); |
| 140 | ut_assertok(strncmp(", Build: ", s + len, 9)); |
| 141 | ut_assertok(strncmp(FAKE_BUILD_TAG, s + 9 + len, 12)); |
| 142 | ut_assertok(strcmp("\n\n", s + big_str_len - 3)); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 143 | |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 144 | return 0; |
| 145 | } |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 146 | PRINT_TEST(print_printf, 0); |
Simon Glass | e5a9d27 | 2017-06-15 21:37:51 -0600 | [diff] [blame] | 147 | |
Simon Glass | fbb99dc | 2021-05-08 06:59:58 -0600 | [diff] [blame^] | 148 | int do_ut_print(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
| 149 | { |
| 150 | struct unit_test *tests = UNIT_TEST_SUITE_START(print_test); |
| 151 | const int n_ents = UNIT_TEST_SUITE_COUNT(print_test); |
| 152 | |
| 153 | return cmd_ut_category("print", "print_", tests, n_ents, argc, argv); |
| 154 | } |