Merge branch 'master' of git://git.denx.de/u-boot-sh
[oweals/u-boot.git] / test / print_ut.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2012, The Chromium Authors
4  */
5
6 #define DEBUG
7
8 #include <common.h>
9 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
10 #include <efi_api.h>
11 #endif
12 #include <display_options.h>
13 #include <version.h>
14
15 #define FAKE_BUILD_TAG  "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \
16                         "and a lot more text to come"
17
18 /* Test efi_loader specific printing */
19 static void efi_ut_print(void)
20 {
21 #if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
22         char str[10];
23         u8 buf[sizeof(struct efi_device_path_sd_mmc_path) +
24                sizeof(struct efi_device_path)];
25         u8 *pos = buf;
26         struct efi_device_path *dp_end;
27         struct efi_device_path_sd_mmc_path *dp_sd =
28                         (struct efi_device_path_sd_mmc_path *)pos;
29
30         /* Create a device path for an SD card */
31         dp_sd->dp.type = DEVICE_PATH_TYPE_MESSAGING_DEVICE;
32         dp_sd->dp.sub_type = DEVICE_PATH_SUB_TYPE_MSG_SD;
33         dp_sd->dp.length = sizeof(struct efi_device_path_sd_mmc_path);
34         dp_sd->slot_number = 3;
35         pos += sizeof(struct efi_device_path_sd_mmc_path);
36         /* Append end node */
37         dp_end = (struct efi_device_path *)pos;
38         dp_end->type = DEVICE_PATH_TYPE_END;
39         dp_end->sub_type = DEVICE_PATH_SUB_TYPE_END;
40         dp_end->length = sizeof(struct efi_device_path);
41
42         snprintf(str, sizeof(str), "_%pD_", buf);
43         assert(!strcmp("_/SD(3)_", str));
44
45         /* NULL device path */
46         snprintf(str, sizeof(str), "_%pD_", NULL);
47         assert(!strcmp("_<NULL>_", str));
48 #endif
49 }
50
51 static int do_ut_print(cmd_tbl_t *cmdtp, int flag, int argc,
52                        char *const argv[])
53 {
54         char big_str[400];
55         int big_str_len;
56         char str[10], *s;
57         int len;
58
59         printf("%s: Testing print\n", __func__);
60
61         snprintf(str, sizeof(str), "testing");
62         assert(!strcmp("testing", str));
63
64         snprintf(str, sizeof(str), "testing but too long");
65         assert(!strcmp("testing b", str));
66
67         snprintf(str, 1, "testing none");
68         assert(!strcmp("", str));
69
70         *str = 'x';
71         snprintf(str, 0, "testing none");
72         assert(*str == 'x');
73
74         sprintf(big_str, "_%ls_", L"foo");
75         assert(!strcmp("_foo_", big_str));
76
77         /* Test the banner function */
78         s = display_options_get_banner(true, str, sizeof(str));
79         assert(s == str);
80         assert(!strcmp("\n\nU-Boo\n\n", s));
81
82         /* Assert that we do not overwrite memory before the buffer */
83         str[0] = '`';
84         s = display_options_get_banner(true, str + 1, 1);
85         assert(s == str + 1);
86         assert(!strcmp("`", str));
87
88         str[0] = '~';
89         s = display_options_get_banner(true, str + 1, 2);
90         assert(s == str + 1);
91         assert(!strcmp("~\n", str));
92
93         /* The last two characters are set to \n\n for all buffer sizes > 2 */
94         s = display_options_get_banner(false, str, sizeof(str));
95         assert(s == str);
96         assert(!strcmp("U-Boot \n\n", s));
97
98         /* Give it enough space for some of the version */
99         big_str_len = strlen(version_string) - 5;
100         s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
101                                             big_str_len);
102         assert(s == big_str);
103         assert(!strncmp(version_string, s, big_str_len - 3));
104         assert(!strcmp("\n\n", s + big_str_len - 3));
105
106         /* Give it enough space for the version and some of the build tag */
107         big_str_len = strlen(version_string) + 9 + 20;
108         s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
109                                             big_str_len);
110         assert(s == big_str);
111         len = strlen(version_string);
112         assert(!strncmp(version_string, s, len));
113         assert(!strncmp(", Build: ", s + len, 9));
114         assert(!strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
115         assert(!strcmp("\n\n", s + big_str_len - 3));
116
117         /* Test efi_loader specific printing */
118         efi_ut_print();
119
120         printf("%s: Everything went swimmingly\n", __func__);
121         return 0;
122 }
123
124 U_BOOT_CMD(
125         ut_print,       1,      1,      do_ut_print,
126         "Very basic test of printf(), etc.",
127         ""
128 );