From: Masahisa Kojima <masahisa.kojima@linaro.org>
To: Heinrich Schuchardt <xypron.glpk@gmx.de>
Cc: Ilias Apalodimas <ilias.apalodimas@linaro.org>,
Simon Glass <sjg@chromium.org>,
Takahiro Akashi <takahiro.akashi@linaro.org>,
Francois Ozog <francois.ozog@linaro.org>,
Mark Kettenis <mark.kettenis@xs4all.nl>,
u-boot@lists.denx.de
Subject: Re: [PATCH v4 03/11] test: unit test for u16_strlcat()
Date: Mon, 4 Apr 2022 23:54:02 +0900 [thread overview]
Message-ID: <CADQ0-X9siDA=6KARJp8f-qnLAoCeG5boq+WJAtWXC714jb9ddQ@mail.gmail.com> (raw)
In-Reply-To: <73867c54-96c1-6e01-1485-f09c291d527f@gmx.de>
On Sat, 2 Apr 2022 at 16:47, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> On 3/24/22 14:54, Masahisa Kojima wrote:
> > Provide a unit test for function u16_strlcat().
> >
> > Signed-off-by: Masahisa Kojima <masahisa.kojima@linaro.org>
> > Reviewed-by: Simon Glass <sjg@chromium.org>
> > ---
> > No change since v2
> >
> > Newly created in v2
> >
> > test/unicode_ut.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
> > 1 file changed, 45 insertions(+)
> >
> > diff --git a/test/unicode_ut.c b/test/unicode_ut.c
> > index f2f63d5367..f79b0439bc 100644
> > --- a/test/unicode_ut.c
> > +++ b/test/unicode_ut.c
> > @@ -758,6 +758,51 @@ static int unicode_test_efi_create_indexed_name(struct unit_test_state *uts)
> > UNICODE_TEST(unicode_test_efi_create_indexed_name);
> > #endif
> >
> > +static int unicode_test_u16_strlcat(struct unit_test_state *uts)
> > +{
> > + u16 buf[11];
>
> buf should be longer then the expected test result, e.g. buf[40].
>
> > + u16 dest[] = u"U-Boot";
> > + u16 src[] = u"test";
>
> The letters in the test should be different to each other and should not
> have zero bytes. You could use:
>
> dest = {0x043b, 0x043e, 0x0434, 0x043a, 0x0430, 0} /* u"лодка" */
> src = {0x6f5c, 0x8247, 0} /* u"潜艇" */
>
> > + u16 expected[] = u"U-Boottest";
> > + u16 null_src = u'\0';
> > + size_t ret;
> > +
>
> If you want to detect failures, you have to set the unused buffer bytes
> to non-zero values except for the last two bytes which should be set to
> zero.
>
> > + memcpy(buf, dest, sizeof(dest));
> > + ret = u16_strlcat(buf, src, sizeof(buf));
> > + ut_asserteq(20, ret);
> > + ut_assert(!unicode_test_u16_strcmp(buf, expected, 11));
> > +
> > + /* dest is empty string */
> > + memset(buf, 0, sizeof(buf));
> > + ret = u16_strlcat(buf, src, sizeof(buf));
> > + ut_asserteq(8, ret);
> > + ut_assert(!unicode_test_u16_strcmp(buf, src, 11));
> > +
> > + /* src is empty string */
> > + memset(buf, 0, sizeof(buf));
> > + memcpy(buf, dest, sizeof(dest));
> > + ret = u16_strlcat(buf, &null_src, sizeof(buf));
> > + ut_asserteq(12, ret);
> > + ut_assert(!unicode_test_u16_strcmp(buf, dest, 11));
> > +
> > + /* size is smaller than dest size */
> > + memset(buf, 0, sizeof(buf));
> > + memcpy(buf, dest, sizeof(dest));
> > + ret = u16_strlcat(buf, src, 6);
> > + ut_asserteq(14, ret);
> > + ut_assert(!unicode_test_u16_strcmp(buf, dest, 11));
> > +
> > + /* size is insufficient to append src string */
> > + memset(buf, 0, sizeof(buf));
> > + memcpy(buf, dest, sizeof(dest));
> > + ret = u16_strlcat(buf, src, 20);
> > + ut_asserteq(20, ret);
> > + ut_assert(!unicode_test_u16_strcmp(buf, u"U-Boottes", 11));
>
> This test does not catch all corner cases. You never use an odd value
> for size. E.g. size = 13 lead to a buffer overrun which you did not catch.
>
> Please, enclose your tests in a loop for size from 0 to sizeof(buf) - 2
> where sizeof(buf) is larger then any expected string size.
>
> Test with dest and/or src being { 0 } too.
I will add these test cases in the next version.
Thanks,
Masahisa Kojima
>
> Best regards
>
> Heinrich
>
> > +
> > + return 0;
> > +}
> > +UNICODE_TEST(unicode_test_u16_strlcat);
> > +
> > int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
> > {
> > struct unit_test *tests = UNIT_TEST_SUITE_START(unicode_test);
>
next prev parent reply other threads:[~2022-04-04 14:54 UTC|newest]
Thread overview: 43+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-24 13:54 [PATCH v4 00/11] enable menu-driven boot device selection Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 01/11] bootmenu: fix menu API error handling Masahisa Kojima
2022-03-30 8:55 ` Ilias Apalodimas
2022-03-24 13:54 ` [PATCH v4 02/11] lib/charset: add u16_strlcat() function Masahisa Kojima
2022-04-02 7:14 ` Heinrich Schuchardt
2022-04-04 14:50 ` Masahisa Kojima
2022-04-16 7:32 ` Heinrich Schuchardt
2022-04-18 7:47 ` Masahisa Kojima
2022-04-28 7:45 ` Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 03/11] test: unit test for u16_strlcat() Masahisa Kojima
2022-04-02 7:47 ` Heinrich Schuchardt
2022-04-04 14:54 ` Masahisa Kojima [this message]
2022-03-24 13:54 ` [PATCH v4 04/11] menu: always show the menu regardless of the number of entry Masahisa Kojima
2022-04-02 7:56 ` Heinrich Schuchardt
2022-03-24 13:54 ` [PATCH v4 05/11] efi_loader: export efi_locate_device_handle() Masahisa Kojima
2022-04-01 5:43 ` Ilias Apalodimas
2022-03-24 13:54 ` [PATCH v4 06/11] efi_loader: bootmgr: add booting from removable media Masahisa Kojima
2022-03-30 19:13 ` Ilias Apalodimas
2022-03-31 0:51 ` Masahisa Kojima
2022-03-31 6:25 ` Ilias Apalodimas
2022-04-02 6:12 ` Heinrich Schuchardt
2022-04-04 6:48 ` Masahisa Kojima
2022-04-04 21:54 ` Heinrich Schuchardt
2022-03-24 13:54 ` [PATCH v4 07/11] bootmenu: add UEFI and disto_boot entries Masahisa Kojima
2022-04-01 6:08 ` Ilias Apalodimas
2022-04-02 6:33 ` Heinrich Schuchardt
2022-04-04 8:10 ` Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 08/11] bootmenu: factor out the user input handling Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 09/11] efi_loader: add menu-driven UEFI Boot Variable maintenance Masahisa Kojima
2022-03-31 8:31 ` Ilias Apalodimas
2022-04-14 9:25 ` Masahisa Kojima
2022-03-24 13:54 ` [PATCH v4 10/11] bootmenu: add removable media entries Masahisa Kojima
2022-03-31 8:48 ` Ilias Apalodimas
2022-03-31 10:18 ` Masahisa Kojima
2022-04-04 22:00 ` Heinrich Schuchardt
2022-03-24 13:54 ` [PATCH v4 11/11] doc:bootmenu: add UEFI boot variable and distro boot support Masahisa Kojima
2022-04-02 5:51 ` Heinrich Schuchardt
2022-03-25 1:20 ` [PATCH v4 00/11] enable menu-driven boot device selection Takahiro Akashi
2022-03-25 6:57 ` Masahisa Kojima
2022-04-02 5:48 ` Heinrich Schuchardt
2022-04-04 6:10 ` Masahisa Kojima
2022-04-16 6:46 ` Heinrich Schuchardt
2022-04-28 7:35 ` Masahisa Kojima
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CADQ0-X9siDA=6KARJp8f-qnLAoCeG5boq+WJAtWXC714jb9ddQ@mail.gmail.com' \
--to=masahisa.kojima@linaro.org \
--cc=francois.ozog@linaro.org \
--cc=ilias.apalodimas@linaro.org \
--cc=mark.kettenis@xs4all.nl \
--cc=sjg@chromium.org \
--cc=takahiro.akashi@linaro.org \
--cc=u-boot@lists.denx.de \
--cc=xypron.glpk@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).