* [LTP] [PATCH] getrandom: Add negative tests for getrandom
@ 2024-04-16 8:04 Yang Xu via ltp
2024-04-16 18:45 ` Avinesh Kumar
0 siblings, 1 reply; 2+ messages in thread
From: Yang Xu via ltp @ 2024-04-16 8:04 UTC (permalink / raw)
To: ltp
Add negative cases for getrandom(), when errno is EFAULT or EINVAL
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
runtest/syscalls | 1 +
.../kernel/syscalls/getrandom/.gitignore | 1 +
.../kernel/syscalls/getrandom/getrandom05.c | 46 +++++++++++++++++++
3 files changed, 48 insertions(+)
create mode 100644 testcases/kernel/syscalls/getrandom/getrandom05.c
diff --git a/runtest/syscalls b/runtest/syscalls
index cc1e39c05..62eb4c1cd 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -504,6 +504,7 @@ getrandom01 getrandom01
getrandom02 getrandom02
getrandom03 getrandom03
getrandom04 getrandom04
+getrandom05 getrandom05
getresgid01 getresgid01
getresgid01_16 getresgid01_16
diff --git a/testcases/kernel/syscalls/getrandom/.gitignore b/testcases/kernel/syscalls/getrandom/.gitignore
index ef06ece9d..e47d8b3f4 100644
--- a/testcases/kernel/syscalls/getrandom/.gitignore
+++ b/testcases/kernel/syscalls/getrandom/.gitignore
@@ -2,3 +2,4 @@
/getrandom02
/getrandom03
/getrandom04
+/getrandom05
diff --git a/testcases/kernel/syscalls/getrandom/getrandom05.c b/testcases/kernel/syscalls/getrandom/getrandom05.c
new file mode 100644
index 000000000..a50228d3d
--- /dev/null
+++ b/testcases/kernel/syscalls/getrandom/getrandom05.c
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that getrandom(2) fails with
+ *
+ * - EFAULT when buf address is outside the accessible address space
+ * - EINVAL when flag is invalid
+ */
+
+#include <sys/random.h>
+#include "tst_test.h"
+
+static char buff_efault[64];
+static char buff_einval[64];
+
+static struct test_case_t {
+ char *buff;
+ size_t size;
+ unsigned int flag;
+ int expected_errno;
+ char *desc;
+} tcases[] = {
+ {NULL, sizeof(buff_efault), 0, EFAULT,
+ "buf address is outside the accessible address space"},
+ {buff_einval, sizeof(buff_einval), -1, EINVAL, "flag is invalid"},
+};
+
+static void verify_getrandom(unsigned int i)
+{
+ struct test_case_t *tc = &tcases[i];
+
+ TST_EXP_FAIL(getrandom(tc->buff, tc->size, tc->flag),
+ tc->expected_errno, "%s", tc->desc);
+}
+
+static struct tst_test test = {
+ .tcnt = ARRAY_SIZE(tcases),
+ .test = verify_getrandom,
+ .needs_root = 1,
+};
--
2.39.3
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [LTP] [PATCH] getrandom: Add negative tests for getrandom
2024-04-16 8:04 [LTP] [PATCH] getrandom: Add negative tests for getrandom Yang Xu via ltp
@ 2024-04-16 18:45 ` Avinesh Kumar
0 siblings, 0 replies; 2+ messages in thread
From: Avinesh Kumar @ 2024-04-16 18:45 UTC (permalink / raw)
To: Yang Xu; +Cc: ltp
Hi Yang Xu,
Please see comments below
On Tuesday, April 16, 2024 10:04:48 AM GMT+2 Yang Xu via ltp wrote:
> Add negative cases for getrandom(), when errno is EFAULT or EINVAL
>
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
> runtest/syscalls | 1 +
> .../kernel/syscalls/getrandom/.gitignore | 1 +
> .../kernel/syscalls/getrandom/getrandom05.c | 46 +++++++++++++++++++
> 3 files changed, 48 insertions(+)
> create mode 100644 testcases/kernel/syscalls/getrandom/getrandom05.c
>
> diff --git a/runtest/syscalls b/runtest/syscalls
> index cc1e39c05..62eb4c1cd 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -504,6 +504,7 @@ getrandom01 getrandom01
> getrandom02 getrandom02
> getrandom03 getrandom03
> getrandom04 getrandom04
> +getrandom05 getrandom05
>
> getresgid01 getresgid01
> getresgid01_16 getresgid01_16
> diff --git a/testcases/kernel/syscalls/getrandom/.gitignore
> b/testcases/kernel/syscalls/getrandom/.gitignore index ef06ece9d..e47d8b3f4
> 100644
> --- a/testcases/kernel/syscalls/getrandom/.gitignore
> +++ b/testcases/kernel/syscalls/getrandom/.gitignore
> @@ -2,3 +2,4 @@
> /getrandom02
> /getrandom03
> /getrandom04
> +/getrandom05
> diff --git a/testcases/kernel/syscalls/getrandom/getrandom05.c
> b/testcases/kernel/syscalls/getrandom/getrandom05.c new file mode 100644
> index 000000000..a50228d3d
> --- /dev/null
> +++ b/testcases/kernel/syscalls/getrandom/getrandom05.c
> @@ -0,0 +1,46 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
> + * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Verify that getrandom(2) fails with
> + *
> + * - EFAULT when buf address is outside the accessible address space
> + * - EINVAL when flag is invalid
> + */
> +
> +#include <sys/random.h>
> +#include "tst_test.h"
> +
> +static char buff_efault[64];
> +static char buff_einval[64];
> +
> +static struct test_case_t {
> + char *buff;
> + size_t size;
> + unsigned int flag;
> + int expected_errno;
> + char *desc;
> +} tcases[] = {
> + {NULL, sizeof(buff_efault), 0, EFAULT,
We usually use '(void *) -1' for inaccessible address.
> + "buf address is outside the accessible address space"},
> + {buff_einval, sizeof(buff_einval), -1, EINVAL, "flag is invalid"},
> +};
> +
> +static void verify_getrandom(unsigned int i)
> +{
> + struct test_case_t *tc = &tcases[i];
> +
> + TST_EXP_FAIL(getrandom(tc->buff, tc->size, tc->flag),
should be TST_EXP_FAIL2() here as getrandom() returns number of bytes copied
to buf (i.e. a non-negative integer) on success.
> + tc->expected_errno, "%s", tc->desc);
> +}
> +
> +static struct tst_test test = {
> + .tcnt = ARRAY_SIZE(tcases),
> + .test = verify_getrandom,
> + .needs_root = 1,
Do we really need root for this?
> +};
Regards,
Avinesh
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-04-16 18:45 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-16 8:04 [LTP] [PATCH] getrandom: Add negative tests for getrandom Yang Xu via ltp
2024-04-16 18:45 ` Avinesh Kumar
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox