Skip to contentSkip to conversation

sched_getattr02: refactor with new LTP API

PATCH In review Ma Xinjian via ltp 2 participants 2 messages Sep 13, 2024 – Dec 16, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sched_getattr02: refactor with new LTP API

---
 .../syscalls/sched_getattr/sched_getattr02.c  | 95 +++++++------------
 1 file changed, 32 insertions(+), 63 deletions(-)
testcases/kernel/syscalls/sched_getattr/sched_getattr02.c +32 -63
index 5efec2ff5..ded541083 100644
@@ -1,43 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
 /*
  * Copyright (c) Huawei Technologies Co., Ltd., 2015
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
+ * Copyright (c) Linux Test Project, 2015-2024
  */
 
- /* Description:
- *   Verify that:
- *              1) sched_getattr fails with unused pid
- *              2) sched_getattr fails with invalid address
- *              3) sched_getattr fails with invalid value
- *              4) sched_getattr fails with invalid flag
+/*\
+ * [Description]
+ *
+ * Verify that, sched_getattr(2) returns -1 and sets errno to
+ *
+ * 1. ESRCH if pid is unused.
+ * 2. EINVAL if address is NULL.
+ * 3. EINVAL if size is invalid.
+ * 4. EINVAL if flag is not zero.
  */
 
 #define _GNU_SOURCE
 
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <linux/unistd.h>
-#include <linux/kernel.h>
-#include <linux/types.h>
-#include <sys/syscall.h>
-#include <pthread.h>
 #include <errno.h>
+#include <string.h>
 
-#include "test.h"
+#include "tst_test.h"
 #include "lapi/sched.h"
 
-char *TCID = "sched_getattr02";
-
 static pid_t pid;
 static pid_t unused_pid;
 struct sched_attr attr_copy;
@@ -48,58 +33,42 @@ static struct test_case {
 	unsigned int size;
 	unsigned int flags;
 	int exp_errno;
-} test_cases[] = {
+} tcase[] = {
 	{&unused_pid, &attr_copy, sizeof(struct sched_attr), 0, ESRCH},
 	{&pid, NULL, sizeof(struct sched_attr), 0, EINVAL},
 	{&pid, &attr_copy, sizeof(struct sched_attr) - 1, 0, EINVAL},
 	{&pid, &attr_copy, sizeof(struct sched_attr), 1000, EINVAL}
 };
 
-static void setup(void);
-static void sched_getattr_verify(const struct test_case *test);
-
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-static void sched_getattr_verify(const struct test_case *test)
+static void verify_sched_getattr(unsigned int n)
 {
-	TEST(sched_getattr(*(test->pid), test->a, test->size,
-			test->flags));
+	struct test_case *tc = tcase + n;
+
+	TEST(sched_getattr(*(tc->pid), tc->a, tc->size, tc->flags));
 
-	if (TEST_RETURN != -1) {
-		tst_resm(TFAIL, "sched_getattr() succeeded unexpectedly.");
+	if (TST_RET != -1) {
+		tst_res(TFAIL, "sched_getattr() succeeded unexpectedly.");
 		return;
 	}
 
-	if (TEST_ERRNO == test->exp_errno) {
-		tst_resm(TPASS | TTERRNO,
-			"sched_getattr() failed expectedly");
+	if (TST_ERR == tc->exp_errno) {
+		tst_res(TPASS | TTERRNO, "sched_getattr() failed expectedly");
 		return;
 	}
 
-	tst_resm(TFAIL | TTERRNO, "sched_getattr() failed unexpectedly "
+	tst_res(TFAIL | TTERRNO, "sched_getattr() failed unexpectedly "
 		": expected: %d - %s",
-		test->exp_errno, tst_strerrno(test->exp_errno));
+		tc->exp_errno, tst_strerrno(tc->exp_errno));
 }
 
-int main(int argc, char **argv)
+static void setup(void)
 {
-	int lc, i;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		for (i = 0; i < TST_TOTAL; i++)
-			sched_getattr_verify(&test_cases[i]);
-	}
-
-	tst_exit();
+	unused_pid = tst_get_unused_pid();
 }
 
-void setup(void)
-{
-	unused_pid = tst_get_unused_pid(setup);
-
-	TEST_PAUSE;
-}
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.test = verify_sched_getattr,
+	.tcnt = ARRAY_SIZE(tcase),
+	.setup = setup,
+};
Signature · 5 lines
2.42.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

Hi Ma Xinjian,

There was missing static:

$ make check-sched_getattr02
CHECK testcases/kernel/syscalls/sched_getattr/sched_getattr02.c
sched_getattr02.c:28:19: warning: Symbol 'attr_copy' has no prototype or library ('tst_') prefix. Should it be static?
Quoted 19 lines
testcases/kernel/syscalls/sched_getattr/sched_getattr02.c +7 -7
+static void verify_sched_getattr(unsigned int n)
 {
-	TEST(sched_getattr(*(test->pid), test->a, test->size,
-			test->flags));
+	struct test_case *tc = tcase + n;
+
+	TEST(sched_getattr(*(tc->pid), tc->a, tc->size, tc->flags));
 
-	if (TEST_RETURN != -1) {
-		tst_resm(TFAIL, "sched_getattr() succeeded unexpectedly.");
+	if (TST_RET != -1) {
+		tst_res(TFAIL, "sched_getattr() succeeded unexpectedly.");
 		return;
 	}
 
-	if (TEST_ERRNO == test->exp_errno) {
-		tst_resm(TPASS | TTERRNO,
-			"sched_getattr() failed expectedly");
+	if (TST_ERR == tc->exp_errno) {

testcases/kernel/syscalls/sched_getattr/sched_getattr02.c static struct test_case { · line 62

Quoted 10 lines
testcases/kernel/syscalls/sched_getattr/sched_getattr02.c +3 -2
+		tst_res(TPASS | TTERRNO, "sched_getattr() failed expectedly");
 		return;
 	}
 
-	tst_resm(TFAIL | TTERRNO, "sched_getattr() failed unexpectedly "
+	tst_res(TFAIL | TTERRNO, "sched_getattr() failed unexpectedly "
 		": expected: %d - %s",
-		test->exp_errno, tst_strerrno(test->exp_errno));
+		tc->exp_errno, tst_strerrno(tc->exp_errno));
 }

We have TST_EXP_FAIL() for it. Merged with following diff. Thanks!

Kind regards,
Petr

+++ testcases/kernel/syscalls/sched_getattr/sched_getattr02.c @@ -7,7 +7,7 @@
/*\
* [Description]
*
- * Verify that, sched_getattr(2) returns -1 and sets errno to
+ * Verify that, sched_getattr(2) returns -1 and sets errno to:
*
* 1. ESRCH if pid is unused.
* 2. EINVAL if address is NULL.
@@ -18,18 +18,16 @@
#define _GNU_SOURCE

#include <errno.h>
-#include <string.h>
-
#include "tst_test.h"
#include "lapi/sched.h"

static pid_t pid;
static pid_t unused_pid;
-struct sched_attr attr_copy;
+static struct sched_attr attr_copy;

static struct test_case {
pid_t *pid;
- struct sched_attr *a;
+ struct sched_attr *attr;
unsigned int size;
unsigned int flags;
int exp_errno;
@@ -42,23 +40,11 @@ static struct test_case {

static void verify_sched_getattr(unsigned int n)
{
- struct test_case *tc = tcase + n;
-
- TEST(sched_getattr(*(tc->pid), tc->a, tc->size, tc->flags));
-
- if (TST_RET != -1) {
- tst_res(TFAIL, "sched_getattr() succeeded unexpectedly.");
- return;
- }
-
- if (TST_ERR == tc->exp_errno) {
- tst_res(TPASS | TTERRNO, "sched_getattr() failed expectedly");
- return;
- }
+ struct test_case *tc = &tcase[n];

- tst_res(TFAIL | TTERRNO, "sched_getattr() failed unexpectedly "
- ": expected: %d - %s",
- tc->exp_errno, tst_strerrno(tc->exp_errno));
+ TST_EXP_FAIL(sched_getattr(*(tc->pid), tc->attr, tc->size, tc->flags),
+ tc->exp_errno, "sched_getattr(%d, ..., %d, %d)", *tc->pid,
+ tc->size, tc->flags);
}

static void setup(void)

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp