diff mbox series

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

Message ID 20220324135443.1571-4-masahisa.kojima@linaro.org
State New
Headers show
Series enable menu-driven boot device selection | expand

Commit Message

Masahisa Kojima March 24, 2022, 1:54 p.m. UTC
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(+)

Comments

Heinrich Schuchardt April 2, 2022, 7:47 a.m. UTC | #1
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.

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);
Masahisa Kojima April 4, 2022, 2:54 p.m. UTC | #2
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);
>
diff mbox series

Patch

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];
+	u16 dest[] = u"U-Boot";
+	u16 src[] = u"test";
+	u16 expected[] = u"U-Boottest";
+	u16 null_src = u'\0';
+	size_t ret;
+
+	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));
+
+	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);