diff mbox series

[v3,8/8] test: rng: Add a UT testcase for the rng command

Message ID 20220304133429.1047752-9-sughosh.ganu@linaro.org
State Superseded
Delegated to: Ilias Apalodimas
Headers show
Series tpm: rng: Move TPM RNG functionality to driver model | expand

Commit Message

Sughosh Ganu March 4, 2022, 1:34 p.m. UTC
The 'rng' command dumps a number of random bytes on the console. Add a
set of tests for the 'rng' command. The test function performs basic
sanity testing of the command.

Since a unit test is being added for the command, enable it by default
in the sandbox platforms.

Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
---

Changes since V2: None

 cmd/Kconfig   |  1 +
 test/dm/rng.c | 29 +++++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

Comments

Simon Glass March 9, 2022, 2:35 a.m. UTC | #1
Hi Sughosh,

On Fri, 4 Mar 2022 at 06:35, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
>
> The 'rng' command dumps a number of random bytes on the console. Add a
> set of tests for the 'rng' command. The test function performs basic
> sanity testing of the command.
>
> Since a unit test is being added for the command, enable it by default
> in the sandbox platforms.
>
> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> ---
>
> Changes since V2: None
>
>  cmd/Kconfig   |  1 +
>  test/dm/rng.c | 29 +++++++++++++++++++++++++++++
>  2 files changed, 30 insertions(+)

Reviewed-by: Simon Glass <sjg@chromium.org>

>
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index 5e25e45fd2..47f1e23ef0 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -1810,6 +1810,7 @@ config CMD_GETTIME
>  config CMD_RNG
>         bool "rng command"
>         depends on DM_RNG
> +       default y if SANDBOX
>         select HEXDUMP
>         help
>           Print bytes from the hardware random number generator.
> diff --git a/test/dm/rng.c b/test/dm/rng.c
> index 5b34c93ed6..6d1f68848d 100644
> --- a/test/dm/rng.c
> +++ b/test/dm/rng.c
> @@ -25,3 +25,32 @@ static int dm_test_rng_read(struct unit_test_state *uts)
>         return 0;
>  }
>  DM_TEST(dm_test_rng_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
> +
> +/* Test the rng command */
> +static int dm_test_rng_cmd(struct unit_test_state *uts)
> +{
> +       struct udevice *dev;
> +
> +       ut_assertok(uclass_get_device(UCLASS_RNG, 0, &dev));
> +       ut_assertnonnull(dev);
> +
> +       ut_assertok(console_record_reset_enable());
> +
> +       run_command("rng", 0);
> +       ut_assert_nextlinen("00000000:");
> +       ut_assert_nextlinen("00000010:");
> +       ut_assert_nextlinen("00000020:");
> +       ut_assert_nextlinen("00000030:");

This is good enough for testing, except that here you don't have any
actual data. How come?

> +       ut_assert_console_end();
> +
> +       run_command("rng 0 10", 0);
> +       ut_assert_nextlinen("00000000:");
> +       ut_assert_console_end();
> +
> +       run_command("rng 20", 0);
> +       ut_assert_nextlinen("No RNG device");
> +       ut_assert_console_end();
> +
> +       return 0;
> +}
> +DM_TEST(dm_test_rng_cmd, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
> --
> 2.25.1
>

Regards,
SImon
Sughosh Ganu March 9, 2022, 6:18 a.m. UTC | #2
hi Simon,

On Wed, 9 Mar 2022 at 08:06, Simon Glass <sjg@chromium.org> wrote:
>
> Hi Sughosh,
>
> On Fri, 4 Mar 2022 at 06:35, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
> >
> > The 'rng' command dumps a number of random bytes on the console. Add a
> > set of tests for the 'rng' command. The test function performs basic
> > sanity testing of the command.
> >
> > Since a unit test is being added for the command, enable it by default
> > in the sandbox platforms.
> >
> > Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> > ---
> >
> > Changes since V2: None
> >
> >  cmd/Kconfig   |  1 +
> >  test/dm/rng.c | 29 +++++++++++++++++++++++++++++
> >  2 files changed, 30 insertions(+)
>
> Reviewed-by: Simon Glass <sjg@chromium.org>
>
> >
> > diff --git a/cmd/Kconfig b/cmd/Kconfig
> > index 5e25e45fd2..47f1e23ef0 100644
> > --- a/cmd/Kconfig
> > +++ b/cmd/Kconfig
> > @@ -1810,6 +1810,7 @@ config CMD_GETTIME
> >  config CMD_RNG
> >         bool "rng command"
> >         depends on DM_RNG
> > +       default y if SANDBOX
> >         select HEXDUMP
> >         help
> >           Print bytes from the hardware random number generator.
> > diff --git a/test/dm/rng.c b/test/dm/rng.c
> > index 5b34c93ed6..6d1f68848d 100644
> > --- a/test/dm/rng.c
> > +++ b/test/dm/rng.c
> > @@ -25,3 +25,32 @@ static int dm_test_rng_read(struct unit_test_state *uts)
> >         return 0;
> >  }
> >  DM_TEST(dm_test_rng_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
> > +
> > +/* Test the rng command */
> > +static int dm_test_rng_cmd(struct unit_test_state *uts)
> > +{
> > +       struct udevice *dev;
> > +
> > +       ut_assertok(uclass_get_device(UCLASS_RNG, 0, &dev));
> > +       ut_assertnonnull(dev);
> > +
> > +       ut_assertok(console_record_reset_enable());
> > +
> > +       run_command("rng", 0);
> > +       ut_assert_nextlinen("00000000:");
> > +       ut_assert_nextlinen("00000010:");
> > +       ut_assert_nextlinen("00000020:");
> > +       ut_assert_nextlinen("00000030:");
>
> This is good enough for testing, except that here you don't have any
> actual data. How come?

This being a test for a random number device, we cannot anticipate
what data we will be getting. The sandbox RNG driver does not return a
fixed set of values on each invocation of the command.

-sughosh

>
> > +       ut_assert_console_end();
> > +
> > +       run_command("rng 0 10", 0);
> > +       ut_assert_nextlinen("00000000:");
> > +       ut_assert_console_end();
> > +
> > +       run_command("rng 20", 0);
> > +       ut_assert_nextlinen("No RNG device");
> > +       ut_assert_console_end();
> > +
> > +       return 0;
> > +}
> > +DM_TEST(dm_test_rng_cmd, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
> > --
> > 2.25.1
> >
>
> Regards,
> SImon
Simon Glass March 12, 2022, 2:43 a.m. UTC | #3
Hi Sughosh,

On Tue, 8 Mar 2022 at 23:18, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
>
> hi Simon,
>
> On Wed, 9 Mar 2022 at 08:06, Simon Glass <sjg@chromium.org> wrote:
> >
> > Hi Sughosh,
> >
> > On Fri, 4 Mar 2022 at 06:35, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
> > >
> > > The 'rng' command dumps a number of random bytes on the console. Add a
> > > set of tests for the 'rng' command. The test function performs basic
> > > sanity testing of the command.
> > >
> > > Since a unit test is being added for the command, enable it by default
> > > in the sandbox platforms.
> > >
> > > Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> > > ---
> > >
> > > Changes since V2: None
> > >
> > >  cmd/Kconfig   |  1 +
> > >  test/dm/rng.c | 29 +++++++++++++++++++++++++++++
> > >  2 files changed, 30 insertions(+)
> >
> > Reviewed-by: Simon Glass <sjg@chromium.org>
> >
> > >
> > > diff --git a/cmd/Kconfig b/cmd/Kconfig
> > > index 5e25e45fd2..47f1e23ef0 100644
> > > --- a/cmd/Kconfig
> > > +++ b/cmd/Kconfig
> > > @@ -1810,6 +1810,7 @@ config CMD_GETTIME
> > >  config CMD_RNG
> > >         bool "rng command"
> > >         depends on DM_RNG
> > > +       default y if SANDBOX
> > >         select HEXDUMP
> > >         help
> > >           Print bytes from the hardware random number generator.
> > > diff --git a/test/dm/rng.c b/test/dm/rng.c
> > > index 5b34c93ed6..6d1f68848d 100644
> > > --- a/test/dm/rng.c
> > > +++ b/test/dm/rng.c
> > > @@ -25,3 +25,32 @@ static int dm_test_rng_read(struct unit_test_state *uts)
> > >         return 0;
> > >  }
> > >  DM_TEST(dm_test_rng_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
> > > +
> > > +/* Test the rng command */
> > > +static int dm_test_rng_cmd(struct unit_test_state *uts)
> > > +{
> > > +       struct udevice *dev;
> > > +
> > > +       ut_assertok(uclass_get_device(UCLASS_RNG, 0, &dev));
> > > +       ut_assertnonnull(dev);
> > > +
> > > +       ut_assertok(console_record_reset_enable());
> > > +
> > > +       run_command("rng", 0);
> > > +       ut_assert_nextlinen("00000000:");
> > > +       ut_assert_nextlinen("00000010:");
> > > +       ut_assert_nextlinen("00000020:");
> > > +       ut_assert_nextlinen("00000030:");
> >
> > This is good enough for testing, except that here you don't have any
> > actual data. How come?
>
> This being a test for a random number device, we cannot anticipate
> what data we will be getting. The sandbox RNG driver does not return a
> fixed set of values on each invocation of the command.

How come? Can you fix that by setting the random seed at the start of the test?

Regards,
Simon



>
> -sughosh
>
> >
> > > +       ut_assert_console_end();
> > > +
> > > +       run_command("rng 0 10", 0);
> > > +       ut_assert_nextlinen("00000000:");
> > > +       ut_assert_console_end();
> > > +
> > > +       run_command("rng 20", 0);
> > > +       ut_assert_nextlinen("No RNG device");
> > > +       ut_assert_console_end();
> > > +
> > > +       return 0;
> > > +}
> > > +DM_TEST(dm_test_rng_cmd, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
> > > --
> > > 2.25.1
> > >
> >
> > Regards,
> > SImon
Sughosh Ganu March 13, 2022, 11:07 a.m. UTC | #4
hi Simon,

On Sat, 12 Mar 2022 at 08:14, Simon Glass <sjg@chromium.org> wrote:
>
> Hi Sughosh,
>
> On Tue, 8 Mar 2022 at 23:18, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
> >
> > hi Simon,
> >
> > On Wed, 9 Mar 2022 at 08:06, Simon Glass <sjg@chromium.org> wrote:
> > >
> > > Hi Sughosh,
> > >
> > > On Fri, 4 Mar 2022 at 06:35, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
> > > >
> > > > The 'rng' command dumps a number of random bytes on the console. Add a
> > > > set of tests for the 'rng' command. The test function performs basic
> > > > sanity testing of the command.
> > > >
> > > > Since a unit test is being added for the command, enable it by default
> > > > in the sandbox platforms.
> > > >
> > > > Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> > > > ---
> > > >
> > > > Changes since V2: None
> > > >
> > > >  cmd/Kconfig   |  1 +
> > > >  test/dm/rng.c | 29 +++++++++++++++++++++++++++++
> > > >  2 files changed, 30 insertions(+)
> > >
> > > Reviewed-by: Simon Glass <sjg@chromium.org>
> > >
> > > >
> > > > diff --git a/cmd/Kconfig b/cmd/Kconfig
> > > > index 5e25e45fd2..47f1e23ef0 100644
> > > > --- a/cmd/Kconfig
> > > > +++ b/cmd/Kconfig
> > > > @@ -1810,6 +1810,7 @@ config CMD_GETTIME
> > > >  config CMD_RNG
> > > >         bool "rng command"
> > > >         depends on DM_RNG
> > > > +       default y if SANDBOX
> > > >         select HEXDUMP
> > > >         help
> > > >           Print bytes from the hardware random number generator.
> > > > diff --git a/test/dm/rng.c b/test/dm/rng.c
> > > > index 5b34c93ed6..6d1f68848d 100644
> > > > --- a/test/dm/rng.c
> > > > +++ b/test/dm/rng.c
> > > > @@ -25,3 +25,32 @@ static int dm_test_rng_read(struct unit_test_state *uts)
> > > >         return 0;
> > > >  }
> > > >  DM_TEST(dm_test_rng_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
> > > > +
> > > > +/* Test the rng command */
> > > > +static int dm_test_rng_cmd(struct unit_test_state *uts)
> > > > +{
> > > > +       struct udevice *dev;
> > > > +
> > > > +       ut_assertok(uclass_get_device(UCLASS_RNG, 0, &dev));
> > > > +       ut_assertnonnull(dev);
> > > > +
> > > > +       ut_assertok(console_record_reset_enable());
> > > > +
> > > > +       run_command("rng", 0);
> > > > +       ut_assert_nextlinen("00000000:");
> > > > +       ut_assert_nextlinen("00000010:");
> > > > +       ut_assert_nextlinen("00000020:");
> > > > +       ut_assert_nextlinen("00000030:");
> > >
> > > This is good enough for testing, except that here you don't have any
> > > actual data. How come?
> >
> > This being a test for a random number device, we cannot anticipate
> > what data we will be getting. The sandbox RNG driver does not return a
> > fixed set of values on each invocation of the command.
>
> How come? Can you fix that by setting the random seed at the start of the test?

Well I can, but do we really need to do that. I think the current test
that we have for the rng command and the uclass is fine.

-sughosh

>
> Regards,
> Simon
>
>
>
> >
> > -sughosh
> >
> > >
> > > > +       ut_assert_console_end();
> > > > +
> > > > +       run_command("rng 0 10", 0);
> > > > +       ut_assert_nextlinen("00000000:");
> > > > +       ut_assert_console_end();
> > > > +
> > > > +       run_command("rng 20", 0);
> > > > +       ut_assert_nextlinen("No RNG device");
> > > > +       ut_assert_console_end();
> > > > +
> > > > +       return 0;
> > > > +}
> > > > +DM_TEST(dm_test_rng_cmd, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
> > > > --
> > > > 2.25.1
> > > >
> > >
> > > Regards,
> > > SImon
Simon Glass March 13, 2022, 10:23 p.m. UTC | #5
Hi Sughosh,

On Sun, 13 Mar 2022 at 05:08, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
>
> hi Simon,
>
> On Sat, 12 Mar 2022 at 08:14, Simon Glass <sjg@chromium.org> wrote:
> >
> > Hi Sughosh,
> >
> > On Tue, 8 Mar 2022 at 23:18, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
> > >
> > > hi Simon,
> > >
> > > On Wed, 9 Mar 2022 at 08:06, Simon Glass <sjg@chromium.org> wrote:
> > > >
> > > > Hi Sughosh,
> > > >
> > > > On Fri, 4 Mar 2022 at 06:35, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
> > > > >
> > > > > The 'rng' command dumps a number of random bytes on the console. Add a
> > > > > set of tests for the 'rng' command. The test function performs basic
> > > > > sanity testing of the command.
> > > > >
> > > > > Since a unit test is being added for the command, enable it by default
> > > > > in the sandbox platforms.
> > > > >
> > > > > Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> > > > > ---
> > > > >
> > > > > Changes since V2: None
> > > > >
> > > > >  cmd/Kconfig   |  1 +
> > > > >  test/dm/rng.c | 29 +++++++++++++++++++++++++++++
> > > > >  2 files changed, 30 insertions(+)
> > > >
> > > > Reviewed-by: Simon Glass <sjg@chromium.org>
> > > >
> > > > >
> > > > > diff --git a/cmd/Kconfig b/cmd/Kconfig
> > > > > index 5e25e45fd2..47f1e23ef0 100644
> > > > > --- a/cmd/Kconfig
> > > > > +++ b/cmd/Kconfig
> > > > > @@ -1810,6 +1810,7 @@ config CMD_GETTIME
> > > > >  config CMD_RNG
> > > > >         bool "rng command"
> > > > >         depends on DM_RNG
> > > > > +       default y if SANDBOX
> > > > >         select HEXDUMP
> > > > >         help
> > > > >           Print bytes from the hardware random number generator.
> > > > > diff --git a/test/dm/rng.c b/test/dm/rng.c
> > > > > index 5b34c93ed6..6d1f68848d 100644
> > > > > --- a/test/dm/rng.c
> > > > > +++ b/test/dm/rng.c
> > > > > @@ -25,3 +25,32 @@ static int dm_test_rng_read(struct unit_test_state *uts)
> > > > >         return 0;
> > > > >  }
> > > > >  DM_TEST(dm_test_rng_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
> > > > > +
> > > > > +/* Test the rng command */
> > > > > +static int dm_test_rng_cmd(struct unit_test_state *uts)
> > > > > +{
> > > > > +       struct udevice *dev;
> > > > > +
> > > > > +       ut_assertok(uclass_get_device(UCLASS_RNG, 0, &dev));
> > > > > +       ut_assertnonnull(dev);
> > > > > +
> > > > > +       ut_assertok(console_record_reset_enable());
> > > > > +
> > > > > +       run_command("rng", 0);
> > > > > +       ut_assert_nextlinen("00000000:");
> > > > > +       ut_assert_nextlinen("00000010:");
> > > > > +       ut_assert_nextlinen("00000020:");
> > > > > +       ut_assert_nextlinen("00000030:");
> > > >
> > > > This is good enough for testing, except that here you don't have any
> > > > actual data. How come?
> > >
> > > This being a test for a random number device, we cannot anticipate
> > > what data we will be getting. The sandbox RNG driver does not return a
> > > fixed set of values on each invocation of the command.
> >
> > How come? Can you fix that by setting the random seed at the start of the test?
>
> Well I can, but do we really need to do that. I think the current test
> that we have for the rng command and the uclass is fine.

It is OK, but not great, since if there is a bug in the data output or
in the collection of the random data, then you will not find it with
this test.

Regards,
Simon
Sughosh Ganu March 24, 2022, 4:22 p.m. UTC | #6
hi Heinrich,

On Fri, 4 Mar 2022 at 19:05, Sughosh Ganu <sughosh.ganu@linaro.org> wrote:
>
> The 'rng' command dumps a number of random bytes on the console. Add a
> set of tests for the 'rng' command. The test function performs basic
> sanity testing of the command.
>
> Since a unit test is being added for the command, enable it by default
> in the sandbox platforms.
>
> Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
> ---
>
> Changes since V2: None
>
>  cmd/Kconfig   |  1 +
>  test/dm/rng.c | 29 +++++++++++++++++++++++++++++
>  2 files changed, 30 insertions(+)

Can you please pick this up, as this is unrelated to the TPM RNG
changes which are still under discussion. Thanks.

-sughosh

>
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index 5e25e45fd2..47f1e23ef0 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -1810,6 +1810,7 @@ config CMD_GETTIME
>  config CMD_RNG
>         bool "rng command"
>         depends on DM_RNG
> +       default y if SANDBOX
>         select HEXDUMP
>         help
>           Print bytes from the hardware random number generator.
> diff --git a/test/dm/rng.c b/test/dm/rng.c
> index 5b34c93ed6..6d1f68848d 100644
> --- a/test/dm/rng.c
> +++ b/test/dm/rng.c
> @@ -25,3 +25,32 @@ static int dm_test_rng_read(struct unit_test_state *uts)
>         return 0;
>  }
>  DM_TEST(dm_test_rng_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
> +
> +/* Test the rng command */
> +static int dm_test_rng_cmd(struct unit_test_state *uts)
> +{
> +       struct udevice *dev;
> +
> +       ut_assertok(uclass_get_device(UCLASS_RNG, 0, &dev));
> +       ut_assertnonnull(dev);
> +
> +       ut_assertok(console_record_reset_enable());
> +
> +       run_command("rng", 0);
> +       ut_assert_nextlinen("00000000:");
> +       ut_assert_nextlinen("00000010:");
> +       ut_assert_nextlinen("00000020:");
> +       ut_assert_nextlinen("00000030:");
> +       ut_assert_console_end();
> +
> +       run_command("rng 0 10", 0);
> +       ut_assert_nextlinen("00000000:");
> +       ut_assert_console_end();
> +
> +       run_command("rng 20", 0);
> +       ut_assert_nextlinen("No RNG device");
> +       ut_assert_console_end();
> +
> +       return 0;
> +}
> +DM_TEST(dm_test_rng_cmd, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);
> --
> 2.25.1
>
diff mbox series

Patch

diff --git a/cmd/Kconfig b/cmd/Kconfig
index 5e25e45fd2..47f1e23ef0 100644
--- a/cmd/Kconfig
+++ b/cmd/Kconfig
@@ -1810,6 +1810,7 @@  config CMD_GETTIME
 config CMD_RNG
 	bool "rng command"
 	depends on DM_RNG
+	default y if SANDBOX
 	select HEXDUMP
 	help
 	  Print bytes from the hardware random number generator.
diff --git a/test/dm/rng.c b/test/dm/rng.c
index 5b34c93ed6..6d1f68848d 100644
--- a/test/dm/rng.c
+++ b/test/dm/rng.c
@@ -25,3 +25,32 @@  static int dm_test_rng_read(struct unit_test_state *uts)
 	return 0;
 }
 DM_TEST(dm_test_rng_read, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
+
+/* Test the rng command */
+static int dm_test_rng_cmd(struct unit_test_state *uts)
+{
+	struct udevice *dev;
+
+	ut_assertok(uclass_get_device(UCLASS_RNG, 0, &dev));
+	ut_assertnonnull(dev);
+
+	ut_assertok(console_record_reset_enable());
+
+	run_command("rng", 0);
+	ut_assert_nextlinen("00000000:");
+	ut_assert_nextlinen("00000010:");
+	ut_assert_nextlinen("00000020:");
+	ut_assert_nextlinen("00000030:");
+	ut_assert_console_end();
+
+	run_command("rng 0 10", 0);
+	ut_assert_nextlinen("00000000:");
+	ut_assert_console_end();
+
+	run_command("rng 20", 0);
+	ut_assert_nextlinen("No RNG device");
+	ut_assert_console_end();
+
+	return 0;
+}
+DM_TEST(dm_test_rng_cmd, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT | UT_TESTF_CONSOLE_REC);