blob: 5fabdf842899f8379342a2885f720054d4343397 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse5a9d272017-06-15 21:37:51 -06002/*
3 * Copyright (c) 2012, The Chromium Authors
Simon Glasse5a9d272017-06-15 21:37:51 -06004 */
5
Simon Glasse5a9d272017-06-15 21:37:51 -06006#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -06007#include <command.h>
Heinrich Schuchardt256060e2018-01-10 18:06:08 +01008#include <efi_api.h>
Simon Glasse5a9d272017-06-15 21:37:51 -06009#include <display_options.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060010#include <log.h>
Simon Glasse5a9d272017-06-15 21:37:51 -060011#include <version.h>
Simon Glassfbb99dc2021-05-08 06:59:58 -060012#include <test/suites.h>
13#include <test/test.h>
14#include <test/ut.h>
Simon Glasse5a9d272017-06-15 21:37:51 -060015
16#define FAKE_BUILD_TAG "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \
17 "and a lot more text to come"
18
Simon Glassfbb99dc2021-05-08 06:59:58 -060019/* Declare a new print test */
20#define PRINT_TEST(_name, _flags) UNIT_TEST(_name, _flags, print_test)
21
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020022#if CONFIG_IS_ENABLED(LIB_UUID)
Simon Glassfbb99dc2021-05-08 06:59:58 -060023/* Test printing GUIDs */
24static int print_guid(struct unit_test_state *uts)
25{
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020026 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 Glassfbb99dc2021-05-08 06:59:58 -060032 ut_assertok(strcmp("01020304-0506-0708-090a-0b0c0d0e0f10", str));
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020033 sprintf(str, "%pUB", guid);
Simon Glassfbb99dc2021-05-08 06:59:58 -060034 ut_assertok(strcmp("01020304-0506-0708-090A-0B0C0D0E0F10", str));
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020035 sprintf(str, "%pUl", guid);
Simon Glassfbb99dc2021-05-08 06:59:58 -060036 ut_assertok(strcmp("04030201-0605-0807-090a-0b0c0d0e0f10", str));
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020037 sprintf(str, "%pUL", guid);
Simon Glassfbb99dc2021-05-08 06:59:58 -060038 ut_assertok(strcmp("04030201-0605-0807-090A-0B0C0D0E0F10", str));
Heinrich Schuchardt3bad2562019-04-29 08:08:43 +020039
Simon Glassfbb99dc2021-05-08 06:59:58 -060040 return 0;
41}
42PRINT_TEST(print_guid, 0);
43#endif
44
Heinrich Schuchardt7a9e6ee2018-08-31 21:31:24 +020045#if CONFIG_IS_ENABLED(EFI_LOADER) && !defined(API_BUILD)
Simon Glassfbb99dc2021-05-08 06:59:58 -060046/* Test efi_loader specific printing */
47static int print_efi_ut(struct unit_test_state *uts)
48{
Heinrich Schuchardt256060e2018-01-10 18:06:08 +010049 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 Glassfbb99dc2021-05-08 06:59:58 -060070 ut_assertok(strcmp("_/SD(3)_", str));
Heinrich Schuchardt5f1ce1d2018-01-26 06:30:30 +010071
72 /* NULL device path */
73 snprintf(str, sizeof(str), "_%pD_", NULL);
Simon Glassfbb99dc2021-05-08 06:59:58 -060074 ut_assertok(strcmp("_<NULL>_", str));
Heinrich Schuchardt256060e2018-01-10 18:06:08 +010075
Simon Glassfbb99dc2021-05-08 06:59:58 -060076 return 0;
77}
78PRINT_TEST(print_efi_ut, 0);
79#endif
80
81static int print_printf(struct unit_test_state *uts)
Simon Glasse5a9d272017-06-15 21:37:51 -060082{
83 char big_str[400];
84 int big_str_len;
85 char str[10], *s;
86 int len;
87
Simon Glasse5a9d272017-06-15 21:37:51 -060088 snprintf(str, sizeof(str), "testing");
Simon Glassfbb99dc2021-05-08 06:59:58 -060089 ut_assertok(strcmp("testing", str));
Simon Glasse5a9d272017-06-15 21:37:51 -060090
91 snprintf(str, sizeof(str), "testing but too long");
Simon Glassfbb99dc2021-05-08 06:59:58 -060092 ut_assertok(strcmp("testing b", str));
Simon Glasse5a9d272017-06-15 21:37:51 -060093
94 snprintf(str, 1, "testing none");
Simon Glassfbb99dc2021-05-08 06:59:58 -060095 ut_assertok(strcmp("", str));
Simon Glasse5a9d272017-06-15 21:37:51 -060096
97 *str = 'x';
98 snprintf(str, 0, "testing none");
Simon Glassfbb99dc2021-05-08 06:59:58 -060099 ut_asserteq('x', *str);
Simon Glasse5a9d272017-06-15 21:37:51 -0600100
Rob Clark085391b2017-09-13 18:46:54 -0400101 sprintf(big_str, "_%ls_", L"foo");
Simon Glassfbb99dc2021-05-08 06:59:58 -0600102 ut_assertok(strcmp("_foo_", big_str));
Rob Clark085391b2017-09-13 18:46:54 -0400103
Simon Glasse5a9d272017-06-15 21:37:51 -0600104 /* Test the banner function */
105 s = display_options_get_banner(true, str, sizeof(str));
Simon Glassfbb99dc2021-05-08 06:59:58 -0600106 ut_asserteq_ptr(str, s);
107 ut_assertok(strcmp("\n\nU-Boo\n\n", s));
Simon Glasse5a9d272017-06-15 21:37:51 -0600108
Heinrich Schuchardt6c74e942019-04-26 18:39:00 +0200109 /* Assert that we do not overwrite memory before the buffer */
110 str[0] = '`';
111 s = display_options_get_banner(true, str + 1, 1);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600112 ut_asserteq_ptr(str + 1, s);
113 ut_assertok(strcmp("`", str));
Simon Glasse5a9d272017-06-15 21:37:51 -0600114
Heinrich Schuchardt6c74e942019-04-26 18:39:00 +0200115 str[0] = '~';
116 s = display_options_get_banner(true, str + 1, 2);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600117 ut_asserteq_ptr(str + 1, s);
118 ut_assertok(strcmp("~\n", str));
Simon Glasse5a9d272017-06-15 21:37:51 -0600119
Heinrich Schuchardt6c74e942019-04-26 18:39:00 +0200120 /* The last two characters are set to \n\n for all buffer sizes > 2 */
Simon Glasse5a9d272017-06-15 21:37:51 -0600121 s = display_options_get_banner(false, str, sizeof(str));
Simon Glassfbb99dc2021-05-08 06:59:58 -0600122 ut_asserteq_ptr(str, s);
123 ut_assertok(strcmp("U-Boot \n\n", s));
Simon Glasse5a9d272017-06-15 21:37:51 -0600124
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 Glassfbb99dc2021-05-08 06:59:58 -0600129 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 Glasse5a9d272017-06-15 21:37:51 -0600132
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 Glassfbb99dc2021-05-08 06:59:58 -0600137 ut_asserteq_ptr(big_str, s);
Simon Glasse5a9d272017-06-15 21:37:51 -0600138 len = strlen(version_string);
Simon Glassfbb99dc2021-05-08 06:59:58 -0600139 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 Glasse5a9d272017-06-15 21:37:51 -0600143
Simon Glasse5a9d272017-06-15 21:37:51 -0600144 return 0;
145}
Simon Glassfbb99dc2021-05-08 06:59:58 -0600146PRINT_TEST(print_printf, 0);
Simon Glasse5a9d272017-06-15 21:37:51 -0600147
Simon Glassfbb99dc2021-05-08 06:59:58 -0600148int 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}