Skip to content

Commit e5a9d27

Browse files
committed
test: Add a test for snprintf() and the banner/version
Add a simple test to make sure that these functions obey the buffer size passed into them. Signed-off-by: Simon Glass <[email protected]> Reviewed-by: Bin Meng <[email protected]> Tested-by: Stephen Warren <[email protected]>
1 parent b089538 commit e5a9d27

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

test/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ obj-$(CONFIG_UNIT_TEST) += cmd_ut.o
88
obj-$(CONFIG_UNIT_TEST) += ut.o
99
obj-$(CONFIG_SANDBOX) += command_ut.o
1010
obj-$(CONFIG_SANDBOX) += compression.o
11+
obj-$(CONFIG_SANDBOX) += print_ut.o
1112
obj-$(CONFIG_UT_TIME) += time_ut.o

test/print_ut.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2012, The Chromium Authors
3+
*
4+
* SPDX-License-Identifier: GPL-2.0+
5+
*/
6+
7+
#define DEBUG
8+
9+
#include <common.h>
10+
#include <display_options.h>
11+
#include <version.h>
12+
13+
#define FAKE_BUILD_TAG "jenkins-u-boot-denx_uboot_dm-master-build-aarch64" \
14+
"and a lot more text to come"
15+
16+
static int do_ut_print(cmd_tbl_t *cmdtp, int flag, int argc,
17+
char *const argv[])
18+
{
19+
char big_str[400];
20+
int big_str_len;
21+
char str[10], *s;
22+
int len;
23+
24+
printf("%s: Testing print\n", __func__);
25+
26+
snprintf(str, sizeof(str), "testing");
27+
assert(!strcmp("testing", str));
28+
29+
snprintf(str, sizeof(str), "testing but too long");
30+
assert(!strcmp("testing b", str));
31+
32+
snprintf(str, 1, "testing none");
33+
assert(!strcmp("", str));
34+
35+
*str = 'x';
36+
snprintf(str, 0, "testing none");
37+
assert(*str == 'x');
38+
39+
/* Test the banner function */
40+
s = display_options_get_banner(true, str, sizeof(str));
41+
assert(s == str);
42+
assert(!strcmp("\n\nU-Boo\n\n", s));
43+
44+
s = display_options_get_banner(true, str, 1);
45+
assert(s == str);
46+
assert(!strcmp("", s));
47+
48+
s = display_options_get_banner(true, str, 2);
49+
assert(s == str);
50+
assert(!strcmp("\n", s));
51+
52+
s = display_options_get_banner(false, str, sizeof(str));
53+
assert(s == str);
54+
assert(!strcmp("U-Boot \n\n", s));
55+
56+
/* Give it enough space for some of the version */
57+
big_str_len = strlen(version_string) - 5;
58+
s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
59+
big_str_len);
60+
assert(s == big_str);
61+
assert(!strncmp(version_string, s, big_str_len - 3));
62+
assert(!strcmp("\n\n", s + big_str_len - 3));
63+
64+
/* Give it enough space for the version and some of the build tag */
65+
big_str_len = strlen(version_string) + 9 + 20;
66+
s = display_options_get_banner_priv(false, FAKE_BUILD_TAG, big_str,
67+
big_str_len);
68+
assert(s == big_str);
69+
len = strlen(version_string);
70+
assert(!strncmp(version_string, s, len));
71+
assert(!strncmp(", Build: ", s + len, 9));
72+
assert(!strncmp(FAKE_BUILD_TAG, s + 9 + len, 12));
73+
assert(!strcmp("\n\n", s + big_str_len - 3));
74+
75+
printf("%s: Everything went swimmingly\n", __func__);
76+
return 0;
77+
}
78+
79+
U_BOOT_CMD(
80+
ut_print, 1, 1, do_ut_print,
81+
"Very basic test of printf(), etc.",
82+
""
83+
);

0 commit comments

Comments
 (0)