[PATCH v4 03/11] test: unit test for u16_strlcat()

Masahisa Kojima masahisa.kojima at linaro.org
Mon Apr 4 16:54:02 CEST 2022


On Sat, 2 Apr 2022 at 16:47, Heinrich Schuchardt <xypron.glpk at 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 at linaro.org>
> > Reviewed-by: Simon Glass <sjg at 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);
>


More information about the U-Boot mailing list