Skip to contentSkip to conversation

sbrk01: Refactor the test using new LTP API

PATCH Superseded Avinesh Kumar 2 participants 4 messages Aug 8, 2023 – Aug 9, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 sbrk01: Refactor the test using new LTP API

---
 testcases/kernel/syscalls/sbrk/sbrk01.c | 121 +++++-------------------
 1 file changed, 26 insertions(+), 95 deletions(-)
testcases/kernel/syscalls/sbrk/sbrk01.c +26 -95
index ce26b1503..109b1d125 100644
@@ -1,111 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
- *  AUTHOR		: William Roske
- *  CO-PILOT		: Dave Fenner
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
- *
- */
-/*
- * DESCRIPTION
- *	1.) test sbrk(8192) should return successfully.
- *	2.) test sbrk(-8192) should return successfully.
+ *  AUTHOR : William Roske, CO-PILOT : Dave Fenner
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
  */
 
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/types.h>
-
-#include "test.h"
+/*\
+ * [Description]
+ *
+ * Verify that sbrk() successfully increments or decrements the program's
+ * data break.
+ */
 
-char *TCID = "sbrk01";
+#include "tst_test.h"
 
-static struct test_case_t {
+static struct tcase {
 	long increment;
-} test_cases[] = {
+} tcases[] = {
+	{0},
 	{8192},
-	{-8192},
+	{-8192}
 };
 
-static void setup(void);
-static void sbrk_verify(const struct test_case_t *);
-static void cleanup(void);
-
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-int main(int ac, char **av)
-{
-	int lc;
-	int i;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		for (i = 0; i < TST_TOTAL; i++)
-			sbrk_verify(&test_cases[i]);
-	}
-
-	cleanup();
-	tst_exit();
-
-}
-
-static void setup(void)
-{
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-}
-
-static void sbrk_verify(const struct test_case_t *test)
+static void run(unsigned int i)
 {
 	void *tret;
+	struct tcase *tc = &tcases[i];
 
-	tret = sbrk(test->increment);
-	TEST_ERRNO = errno;
+	tret = sbrk(tc->increment);
+	TST_ERR = errno;
 
-	if (tret == (void *)-1) {
-		tst_resm(TFAIL | TTERRNO, "sbrk - Increase by %ld bytes failed",
-			 test->increment);
-	} else {
-		tst_resm(TPASS, "sbrk - Increase by %ld bytes returned %p",
-			 test->increment, tret);
-	}
+	if (tret == (void *) -1)
+		tst_res(TFAIL | TTERRNO, "sbrk(%ld) failed", tc->increment);
+	else
+		tst_res(TPASS, "sbrk(%ld) returned %p", tc->increment, tret);
 }
 
-static void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.test = run,
+	.tcnt = ARRAY_SIZE(tcases)
+};
Signature · 5 lines
2.41.0


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

PATCH 2/2 sbrk02.c: Refactor the test using new LTP API

---
 testcases/kernel/syscalls/sbrk/sbrk02.c | 102 +++++++-----------------
 1 file changed, 28 insertions(+), 74 deletions(-)
testcases/kernel/syscalls/sbrk/sbrk02.c +28 -74
index 84744ef90..69a7ce35c 100644
@@ -1,101 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2014 Fujitsu Ltd.
  * Author: Zeng Linggang <[email protected]>
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- */
-/*
- * DESCRIPTION
- *	Check sbrk() with error condition that should produce ENOMEM.
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
  */
 
-#include <errno.h>
-#include <unistd.h>
-#include "test.h"
-
-#define INC 16*1024*1024
+/*\
+ * [Description]
+ *
+ * Verify that sbrk() on failure sets errno to ENOMEM.
+ */
 
-char *TCID = "sbrk02";
-int TST_TOTAL = 1;
 
-static void setup(void);
-static void sbrk_verify(void);
-static void cleanup(void);
+#include "tst_test.h"
 
+#define INC (16*1024*1024)
 static long increment = INC;
 
-int main(int argc, char *argv[])
+static void run(void)
 {
-	int lc;
-	int i;
-
-	tst_parse_opts(argc, argv, NULL, NULL);
+	void *tret;
 
-	setup();
+	tret = sbrk(increment);
+	TST_ERR = errno;
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
-		for (i = 0; i < TST_TOTAL; i++)
-			sbrk_verify();
+	if (tret != (void *)-1) {
+		tst_res(TFAIL, "sbrk(%ld) unexpectedly passed and returned %p, "
+						"expected (void *)-1 with errno=%d",
+						increment, tret, ENOMEM);
+		return;
 	}
 
-	cleanup();
-	tst_exit();
+	if (TST_ERR == ENOMEM)
+		tst_res(TPASS | TTERRNO, "sbrk(%ld) failed as expected", increment);
+	else
+		tst_res(TFAIL | TTERRNO, "sbrk(%ld) failed but unexpected errno, "
+								"expected errno=%d - %s",
+								increment, ENOMEM, strerror(ENOMEM));
 }
 
 static void setup(void)
 {
 	void *ret = NULL;
 
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	/* call sbrk until it fails or increment overflows */
 	while (ret != (void *)-1 && increment > 0) {
 		ret = sbrk(increment);
 		increment += INC;
 	}
-	tst_resm(TINFO | TERRNO, "setup() bailing inc: %ld, ret: %p, sbrk: %p",
-		increment, ret, sbrk(0));
-
-	errno = 0;
 }
 
-static void sbrk_verify(void)
-{
-	void *tret;
-
-	tret = sbrk(increment);
-	TEST_ERRNO = errno;
-
-	if (tret != (void *)-1) {
-		tst_resm(TFAIL,
-			 "sbrk(%ld) returned %p, expected (void *)-1, errno=%d",
-			 increment, tret, ENOMEM);
-		return;
-	}
-
-	if (TEST_ERRNO == ENOMEM) {
-		tst_resm(TPASS | TTERRNO, "sbrk(%ld) failed as expected",
-			 increment);
-	} else {
-		tst_resm(TFAIL | TTERRNO,
-			 "sbrk(%ld) failed unexpectedly; expected: %d - %s",
-			 increment, ENOMEM, strerror(ENOMEM));
-	}
-}
-
-static void cleanup(void)
-{
-}
+static struct tst_test test = {
+	.test_all = run,
+	.setup = setup
+};
Signature · 5 lines
2.41.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw
In reply to Avinesh Kumar

Hi Avinesh,

Quoted 117 lines · Avinesh Kumar
---
 testcases/kernel/syscalls/sbrk/sbrk01.c | 121 +++++-------------------
 1 file changed, 26 insertions(+), 95 deletions(-)
testcases/kernel/syscalls/sbrk/sbrk01.c +14 -82
index ce26b1503..109b1d125 100644
@@ -1,111 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0
 /*
  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
- *  AUTHOR             : William Roske
- *  CO-PILOT           : Dave Fenner
- *
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of version 2 of the GNU General Public License as
- * published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it would be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- * Further, this software is distributed without any warranty that it is
- * free of the rightful claim of any third person regarding infringement
- * or the like.  Any license provided herein, whether implied or
- * otherwise, applies only to this software file.  Patent licenses, if
- * any, provided herein do not apply to combinations of this program with
- * other software, or any other product whatsoever.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
- * Mountain View, CA  94043, or:
- *
- * http://www.sgi.com
- *
- * For further information regarding this notice, see:
- *
- * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
- *
- */
-/*
- * DESCRIPTION
- *     1.) test sbrk(8192) should return successfully.
- *     2.) test sbrk(-8192) should return successfully.
+ *  AUTHOR : William Roske, CO-PILOT : Dave Fenner
+ * Copyright (c) 2023 SUSE LLC Avinesh Kumar <[email protected]>
  */
 
-#include <unistd.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/types.h>
-
-#include "test.h"
+/*\
+ * [Description]
+ *
+ * Verify that sbrk() successfully increments or decrements the program's
+ * data break.
+ */
 
-char *TCID = "sbrk01";
+#include "tst_test.h"
 
-static struct test_case_t {
+static struct tcase {
        long increment;
-} test_cases[] = {
+} tcases[] = {
+       {0},
        {8192},
-       {-8192},
+       {-8192}
 };
 
-static void setup(void);
-static void sbrk_verify(const struct test_case_t *);
-static void cleanup(void);
-
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-int main(int ac, char **av)
-{
-       int lc;
-       int i;
-
-       tst_parse_opts(ac, av, NULL, NULL);
-
-       setup();
-
-       for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-               tst_count = 0;
-
-               for (i = 0; i < TST_TOTAL; i++)
-                       sbrk_verify(&test_cases[i]);
-       }
-
-       cleanup();
-       tst_exit();
-
-}
-
-static void setup(void)
-{
-       tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-       TEST_PAUSE;
-}
-

testcases/kernel/syscalls/sbrk/sbrk01.c line 31

Quoted 10 lines
testcases/kernel/syscalls/sbrk/sbrk01.c +4 -3
-static void sbrk_verify(const struct test_case_t *test)
+static void run(unsigned int i)
 {
        void *tret;
+       struct tcase *tc = &tcases[i];
 
-       tret = sbrk(test->increment);
-       TEST_ERRNO = errno;
+       tret = sbrk(tc->increment);
+       TST_ERR = errno;

Can we make use of the LTP test macro TESTPTR()? It has already given TST_RET_PRT and TST_ERR inside that.

See:
https://github.com/linux-test-project/ltp/blob/master/include/tst_test_macros.h#L30

Quoted 32 lines
testcases/kernel/syscalls/sbrk/sbrk01.c +8 -11
-       if (tret == (void *)-1) {
-               tst_resm(TFAIL | TTERRNO, "sbrk - Increase by %ld bytes
 failed",
-                        test->increment);
-       } else {
-               tst_resm(TPASS, "sbrk - Increase by %ld bytes returned %p",
-                        test->increment, tret);
-       }
+       if (tret == (void *) -1)
+               tst_res(TFAIL | TTERRNO, "sbrk(%ld) failed",
 tc->increment);
+       else
+               tst_res(TPASS, "sbrk(%ld) returned %p", tc->increment,
 tret);
 }
 
-static void cleanup(void)
-{
-}
+static struct tst_test test = {
+       .test = run,
+       .tcnt = ARRAY_SIZE(tcases)
+};
--

2.41.0

--
Mailing list info: https://lists.linux.it/listinfo/ltp

Signature · 5 lines
Regards,
Li Wang

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

Hi Li,

Quoted 11 lines

testcases/kernel/syscalls/sbrk/sbrk01.c line 31

Quoted 4 lines
testcases/kernel/syscalls/sbrk/sbrk01.c +2 -2
-       tret = sbrk(test->increment);
-       TEST_ERRNO = errno;
+       tret = sbrk(tc->increment);
+       TST_ERR = errno;

Can we make use of the LTP test macro TESTPTR()? It has already given TST_RET_PRT and TST_ERR inside that.

See:
https://github.com/linux-test-project/ltp/blob/master/include/tst_test_macro
s.h#L30

Thank you for reviewing and pointing this out, I have sent the updated patches with TESTPTR() macro.

--
Regards,
Avinesh

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