public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/4] syscalls/readlink.h: Move common macros to readlink.h
@ 2018-02-12 10:21 Jinhui Huang
  2018-02-12 10:21 ` [LTP] [PATCH 2/4] syscalls/readlink01.c: Cleanup && Convert to new API Jinhui Huang
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Jinhui Huang @ 2018-02-12 10:21 UTC (permalink / raw)
  To: ltp

Signed-off-by: Jinhui Huang <huangjh.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/readlink/readlink.h | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
 create mode 100644 testcases/kernel/syscalls/readlink/readlink.h

diff --git a/testcases/kernel/syscalls/readlink/readlink.h b/testcases/kernel/syscalls/readlink/readlink.h
new file mode 100644
index 0000000..0361157
--- /dev/null
+++ b/testcases/kernel/syscalls/readlink/readlink.h
@@ -0,0 +1,26 @@
+/*
+* Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
+* Author: Jinhui huang <huangjh.jy@cn.fujitsu.com>
+*
+* 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
+* alone with this program.
+*/
+
+#ifndef	READLINK_H
+#define	READLINK_H
+
+#define TESTFILE	"test_file"
+#define SYMFILE	"slink_file"
+#define FILE_MODE	(S_IRUSR | S_IRGRP | S_IROTH)
+#define MODE_RWX	(S_IRWXU | S_IRWXG | S_IRWXO)
+#define MAX_SIZE	256
+
+#endif /* READLINK_H */
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH 2/4] syscalls/readlink01.c: Cleanup && Convert to new API
  2018-02-12 10:21 [LTP] [PATCH 1/4] syscalls/readlink.h: Move common macros to readlink.h Jinhui Huang
@ 2018-02-12 10:21 ` Jinhui Huang
  2018-02-12 10:21 ` [LTP] [PATCH 3/4] syscalls/readlink03.c: " Jinhui Huang
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Jinhui Huang @ 2018-02-12 10:21 UTC (permalink / raw)
  To: ltp

1) Take use of some safe macros
2) Merge readlink02 into readlink01

Signed-off-by: Jinhui Huang <huangjh.jy@cn.fujitsu.com>
---
 runtest/ltplite                                 |   1 -
 runtest/quickhit                                |   2 -
 runtest/stress.part3                            |   1 -
 runtest/syscalls                                |   1 -
 testcases/kernel/syscalls/.gitignore            |   1 -
 testcases/kernel/syscalls/readlink/readlink01.c | 216 +++++-------------------
 testcases/kernel/syscalls/readlink/readlink02.c | 210 -----------------------
 7 files changed, 44 insertions(+), 388 deletions(-)
 delete mode 100644 testcases/kernel/syscalls/readlink/readlink02.c

diff --git a/runtest/ltplite b/runtest/ltplite
index 5418495..cbc748b 100644
--- a/runtest/ltplite
+++ b/runtest/ltplite
@@ -623,7 +623,6 @@ readdir21 readdir21
 
 readlink01A symlink01 -T readlink01
 readlink01 readlink01
-readlink02 readlink02
 readlink03 readlink03
 readlink04 readlink04
 
diff --git a/runtest/quickhit b/runtest/quickhit
index 2a42769..6070ec0 100644
--- a/runtest/quickhit
+++ b/runtest/quickhit
@@ -195,8 +195,6 @@ readdir01 readdir01
 # write multiple files and try to find them with readdir
 #    TEST CASES
 # 	1.) Create n files and check that readdir() finds n files
-readlink02 readlink02
-# Basic test for readlink(2)
 rename02 rename02
 # Basic test for rename(2)
 rmdir04 rmdir04
diff --git a/runtest/stress.part3 b/runtest/stress.part3
index 242b696..ba83b65 100644
--- a/runtest/stress.part3
+++ b/runtest/stress.part3
@@ -528,7 +528,6 @@ readdir21 readdir21
 
 readlink01A symlink01 -T readlink01
 readlink01 readlink01
-readlink02 readlink02
 readlink03 readlink03
 readlink04 readlink04
 
diff --git a/runtest/syscalls b/runtest/syscalls
index 2a4fad0..72fbacf 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -882,7 +882,6 @@ readdir21 readdir21
 
 readlink01A symlink01 -T readlink01
 readlink01 readlink01
-readlink02 readlink02
 readlink03 readlink03
 readlink04 readlink04
 
diff --git a/testcases/kernel/syscalls/.gitignore b/testcases/kernel/syscalls/.gitignore
index 67211ca..8b23a85 100644
--- a/testcases/kernel/syscalls/.gitignore
+++ b/testcases/kernel/syscalls/.gitignore
@@ -738,7 +738,6 @@
 /readdir/readdir21
 /readlink/creat_slink
 /readlink/readlink01
-/readlink/readlink02
 /readlink/readlink03
 /readlink/readlink04
 /readlinkat/readlinkat01
diff --git a/testcases/kernel/syscalls/readlink/readlink01.c b/testcases/kernel/syscalls/readlink/readlink01.c
index 5f6448a..9f81876 100644
--- a/testcases/kernel/syscalls/readlink/readlink01.c
+++ b/testcases/kernel/syscalls/readlink/readlink01.c
@@ -1,195 +1,67 @@
 /*
+ * Copyright (c) International Business Machines  Corp., 2001
+ *   07/2001 Ported by Wayne Boyer
  *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * 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 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.
- *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software
- *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * 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.
  */
 
 /*
- * Test Name : readlink01
- *
  * Test Description :
- *  Verify that, readlink will succeed to read the contents of the symbolic
- *  link created the process.
- *
- * Expected Result:
- *  readlink() should return the contents of symbolic link path in the buffer
- *  on success.
- *
- * Algorithm:
- *  Setup:
- *   Setup signal handling.
- *   Create temporary directory.
- *   Pause for SIGUSR1 if option specified.
- *
- *  Test:
- *   Loop if the proper options are given.
- *   Execute system call
- *   Check return code, if system call failed (return=-1)
- *   	Issue a FAIL message.
- *   Otherwise,
- *   	Verify the Functionality of system call
- *      if successful,
- *      	Issue Functionality-Pass message.
- *      Otherwise,
- *		Issue Functionality-Fail message.
- *  Cleanup:
- *   Print errno log and/or timing stats if options given
- *   Delete the temporary directory created.
- *
- * Usage:  <for command-line>
- *  readlink01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
- *     where,  -c n : Run n copies concurrently.
- *             -f   : Turn off functionality Testing.
- *	       -i n : Execute test n times.
- *	       -I x : Execute test for x seconds.
- *	       -P x : Pause for x seconds between iterations.
- *	       -t   : Turn on syscall timing.
- *
- * HISTORY
- *	07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS:
- *  This test should be run by 'non-super-user' only.
- *
+ *   Testcase to check the basic functionality of the readlink(2),
+ *   readlink() will succeed to read the contents of the symbolic link.
  */
-#include <unistd.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <pwd.h>
 
-#include "test.h"
-#include "safe_macros.h"
-
-#define TESTFILE	"testfile"
-#define SYMFILE		"slink_file"
-#define FILE_MODE       S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
-#define MAX_SIZE	256
-
-char *TCID = "readlink01";
-int TST_TOTAL = 1;
-
-const int exp_val = sizeof(TESTFILE) - 1;	/* strlen of testfile */
-
-void setup();
-void cleanup();
+#include <pwd.h>
+#include <errno.h>
+#include <string.h>
 
-char nobody_uid[] = "nobody";
-struct passwd *ltpuser;
+#include "tst_test.h"
+#include "readlink.h"
 
-int main(int ac, char **av)
+static void verify_readlink(void)
 {
-	char buffer[MAX_SIZE];	/* temporary buffer to hold symlink contents */
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/*
-		 * Call readlink(2) to read the contents of
-		 * symlink into a buffer.
-		 */
-		TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
+	char buffer[MAX_SIZE];
+	int exp_val = strlen(TESTFILE);
 
-		if (TEST_RETURN == -1) {
-			tst_resm(TFAIL,
-				 "readlink() on %s failed, errno=%d : %s",
-				 SYMFILE, TEST_ERRNO, strerror(TEST_ERRNO));
-			continue;
-		}
+	TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
+	if (TEST_RETURN == -1) {
+		tst_res(TFAIL | TTERRNO, "readlink() on %s failed", SYMFILE);
+		return;
+	}
 
-		/*
-		 * Compare the return value of readlink()
-		 * with the expected value which is the
-		 * strlen() of testfile.
-		 */
-		if (TEST_RETURN == exp_val) {
-			/* Check for the contents of buffer */
-			if (memcmp(buffer, TESTFILE, exp_val) != 0) {
-				tst_resm(TFAIL, "Pathname %s and buffer"
-					 " contents %s differ",
-					 TESTFILE, buffer);
-			} else {
-				tst_resm(TPASS, "readlink() "
-					 "functionality on '%s' is "
-					 "correct", SYMFILE);
-			}
+	if (TEST_RETURN != exp_val) {
+		tst_res(TFAIL, "readlink() returned value %ld "
+			"did't match, Expected %d", TEST_RETURN, exp_val);
+	} else {
+		if (memcmp(buffer, TESTFILE, exp_val) != 0) {
+			tst_res(TFAIL, "Pathname %s and buffer "
+				"contents %s differ", TESTFILE, buffer);
 		} else {
-			tst_resm(TFAIL, "readlink() return value %ld "
-				 "does't match, Expected %d",
-				 TEST_RETURN, exp_val);
+			tst_res(TPASS, "readlink() functionality on '%s' was "
+				"correct", SYMFILE);
 		}
 	}
-
-	cleanup();
-	tst_exit();
 }
 
-/*
- * setup() - performs all ONE TIME setup for this test.
- *
- *  Create a temporary directory and change directory to it.
- *  Create a test file under temporary directory and close it
- *  Create a symbolic link of testfile.
- */
-void setup(void)
+static void setup(void)
 {
-	int fd;			/* file handle for testfile */
-
-	tst_require_root();
-
-	if ((ltpuser = getpwnam(nobody_uid)) == NULL) {
-		tst_brkm(TBROK, cleanup, "getpwname(nobody_uid) failed ");
-	}
-	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
+	int fd;
 
-	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
-		tst_brkm(TBROK | TERRNO, cleanup,
-			 "open(%s, O_RDWR|O_CREAT, %#o) failed",
-			 TESTFILE, FILE_MODE);
-	}
-
-	if (close(fd) == -1) {
-		tst_resm(TWARN | TERRNO, "close(%s) failed", TESTFILE);
-	}
-
-	/* Create a symlink of testfile under temporary directory */
-	SAFE_SYMLINK(cleanup, TESTFILE, SYMFILE);
+	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
+	SAFE_CLOSE(fd);
+	SAFE_SYMLINK(TESTFILE, SYMFILE);
 }
 
-/*
- * cleanup() - performs all ONE TIME cleanup for this test at
- *             completion or premature exit.
- *
- *  Remove the test directory and testfile created in the setup.
- */
-void cleanup(void)
-{
-
-	tst_rmdir();
-
-}
+static struct tst_test test = {
+	.test_all = verify_readlink,
+	.setup = setup,
+	.needs_tmpdir = 1,
+};
diff --git a/testcases/kernel/syscalls/readlink/readlink02.c b/testcases/kernel/syscalls/readlink/readlink02.c
deleted file mode 100644
index 218a6e3..0000000
--- a/testcases/kernel/syscalls/readlink/readlink02.c
+++ /dev/null
@@ -1,210 +0,0 @@
-/*
- * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
- *
- * 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/
- *
- */
-/* $Id: readlink02.c,v 1.6 2009/11/02 13:57:17 subrata_modak Exp $ */
-/**********************************************************
- *
- *    OS Test - Silicon Graphics, Inc.
- *
- *    TEST IDENTIFIER	: readlink02
- *
- *    EXECUTED BY	: anyone
- *
- *    TEST TITLE	: Basic test for readlink(2)
- *
- *    PARENT DOCUMENT	: usctpl01
- *
- *    TEST CASE TOTAL	: 1
- *
- *    WALL CLOCK TIME	: 1
- *
- *    CPU TYPES		: ALL
- *
- *    AUTHOR		: William Roske
- *
- *    CO-PILOT		: Dave Fenner
- *
- *    DATE STARTED	: 03/30/92
- *
- *    INITIAL RELEASE	: UNICOS 7.0
- *
- *    TEST CASES
- *
- * 	1.) readlink(2) returns...(See Description)
- *
- *    INPUT SPECIFICATIONS
- * 	The standard options for system call tests are accepted.
- *	(See the parse_opts(3) man page).
- *
- *    OUTPUT SPECIFICATIONS
- *$
- *    DURATION
- * 	Terminates - with frequency and infinite modes.
- *
- *    SIGNALS
- * 	Uses SIGUSR1 to pause before test if option set.
- * 	(See the parse_opts(3) man page).
- *
- *    RESOURCES
- * 	None
- *
- *    ENVIRONMENTAL NEEDS
- *      No run-time environmental needs.
- *
- *    SPECIAL PROCEDURAL REQUIREMENTS
- * 	None
- *
- *    INTERCASE DEPENDENCIES
- * 	None
- *
- *    DETAILED DESCRIPTION
- *	This is a Phase I test for the readlink(2) system call.  It is intended
- *	to provide a limited exposure of the system call, for now.  It
- *	should/will be extended when full functional tests are written for
- *	readlink(2).
- *
- * 	Setup:
- * 	  Setup signal handling.
- *	  Pause for SIGUSR1 if option specified.
- *
- * 	Test:
- *	 Loop if the proper options are given.
- * 	  Execute system call
- *	  Check return code, if system call failed (return=-1)
- *		Log the errno and Issue a FAIL message.
- *	  Otherwise, Issue a PASS message.
- *
- * 	Cleanup:
- * 	  Print errno log and/or timing stats if options given
- *
- *
- *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
-
-#include <sys/types.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <string.h>
-#include <signal.h>
-#include "test.h"
-
-void setup();
-void cleanup();
-
-char *TCID = "readlink02";
-int TST_TOTAL = 1;
-
-char fname[255], buf[255], symlnk[255];
-int fd;
-
-int main(int ac, char **av)
-{
-	int lc;
-
-    /***************************************************************
-     * parse standard options
-     ***************************************************************/
-	tst_parse_opts(ac, av, NULL, NULL);
-
-    /***************************************************************
-     * perform global setup for test
-     ***************************************************************/
-	setup();
-
-    /***************************************************************
-     * check looping state if -c option given
-     ***************************************************************/
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/*
-		 * Call readlink(2)
-		 */
-		TEST(readlink(symlnk, buf, 255));
-
-		/* check return code */
-		if (TEST_RETURN == -1) {
-			tst_resm(TFAIL,
-				 "readlink(%s, buf, 255) Failed, errno=%d : %s",
-				 symlnk, TEST_ERRNO, strerror(TEST_ERRNO));
-		} else {
-			tst_resm(TPASS,
-				 "readlink(%s, buf, 255) returned %ld",
-				 symlnk, TEST_RETURN);
-		}
-
-	}
-
-	cleanup();
-	tst_exit();
-}
-
-/***************************************************************
- * setup() - performs all ONE TIME setup for this test.
- ***************************************************************/
-void setup(void)
-{
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
-	sprintf(fname, "./tfile_%d", getpid());
-	if ((fd = open(fname, O_RDWR | O_CREAT, 0700)) == -1) {
-		tst_brkm(TBROK, cleanup,
-			 "open(%s, O_RDWR|O_CREAT,0700) Failed, errno=%d : %s",
-			 fname, errno, strerror(errno));
-	}
-	if (close(fd) == -1) {
-		tst_resm(TWARN, "close(%s) Failed, errno=%d : %s",
-			 fname, errno, strerror(errno));
-	}
-	sprintf(symlnk, "./sl_%d", getpid());
-	if (symlink(fname, symlnk) == -1) {
-		tst_resm(TWARN, "symlnk(%s, %s) Failed, errno=%d : %s",
-			 fname, symlnk, errno, strerror(errno));
-	}
-
-}
-
-/***************************************************************
- * cleanup() - performs all ONE TIME cleanup for this test at
- *		completion or premature exit.
- ***************************************************************/
-void cleanup(void)
-{
-
-	tst_rmdir();
-
-}
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH 3/4] syscalls/readlink03.c: Cleanup && Convert to new API
  2018-02-12 10:21 [LTP] [PATCH 1/4] syscalls/readlink.h: Move common macros to readlink.h Jinhui Huang
  2018-02-12 10:21 ` [LTP] [PATCH 2/4] syscalls/readlink01.c: Cleanup && Convert to new API Jinhui Huang
@ 2018-02-12 10:21 ` Jinhui Huang
  2018-02-12 10:21 ` [LTP] [PATCH 4/4] syscalls/readlink04.c: " Jinhui Huang
  2018-04-13 15:38 ` [LTP] [PATCH 1/4] syscalls/readlink.h: Move common macros to readlink.h Cyril Hrubis
  3 siblings, 0 replies; 5+ messages in thread
From: Jinhui Huang @ 2018-02-12 10:21 UTC (permalink / raw)
  To: ltp

1) Take use of some safe macros
2) Add a new test for EFAULT

Signed-off-by: Jinhui Huang <huangjh.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/readlink/readlink03.c | 217 ++++++++++--------------
 1 file changed, 88 insertions(+), 129 deletions(-)

diff --git a/testcases/kernel/syscalls/readlink/readlink03.c b/testcases/kernel/syscalls/readlink/readlink03.c
index 0106621..7de4b64 100644
--- a/testcases/kernel/syscalls/readlink/readlink03.c
+++ b/testcases/kernel/syscalls/readlink/readlink03.c
@@ -1,25 +1,20 @@
 /*
- *
- *   Copyright (c) International Business Machines  Corp., 2001
+ * Copyright (c) International Business Machines  Corp., 2001
  *   07/2001 Ported by Wayne Boyer
  *
- *   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.
+ * 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.
  *
- *   You should have received a copy of the GNU General Public License
- *   along with this program;  if not, write to the Free Software Foundation,
- *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ * 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.
  */
+
 /*
  * Test Description :
- *   Verify that,
  *   1) readlink(2) returns -1 and sets errno to EACCES if search/write
  *	permission is denied in the directory where the symbolic link
  *	resides.
@@ -35,155 +30,119 @@
  *	the path prefix is not a directory.
  *   7) readlink(2) returns -1 and sets errno to ELOOP if too many symbolic
  *	links were encountered in translating the pathname.
+ *   8) readlink(2) returns -1 and sets errno to EFAULT if buf outside the
+ *	process’s allocated address space.
  */
 
-#include <stdio.h>
-#include <sys/types.h>
-#include <fcntl.h>
+#include <pwd.h>
 #include <errno.h>
 #include <string.h>
-#include <signal.h>
-#include <sys/stat.h>
-#include <pwd.h>
 
-#include "test.h"
-#include "safe_macros.h"
-
-#define MODE_RWX	(S_IRWXU | S_IRWXG | S_IRWXO)
-#define FILE_MODE	(S_IRUSR | S_IRGRP | S_IROTH)
-#define DIR_TEMP	"testdir_1"
-#define TEST_FILE1	"testdir_1/tfile_1"
-#define SYM_FILE1	"testdir_1/sfile_1"
-#define TEST_FILE2	"tfile_2"
-#define SYM_FILE2	"sfile_2"
-#define TEST_FILE3	"tfile_3"
-#define SYM_FILE3	"tfile_3/sfile_3"
+#include "tst_test.h"
+#include "readlink.h"
+
+#define DIR_TEMP	"test_dir_1"
+#define TEST_FILE1	"test_dir_1/test_file_1"
+#define SYM_FILE1	"test_dir_1/slink_file_1"
+#define TEST_FILE2	"test_file_2"
+#define SYM_FILE2	"slink_file_2"
+#define TEST_FILE3	"test_file_3"
+#define SYM_FILE3	"test_file_3/slink_file_3"
 #define ELOOPFILE	"/test_eloop"
-#define MAX_SIZE	256
 
 static char longpathname[PATH_MAX + 2];
 static char elooppathname[sizeof(ELOOPFILE) * 43] = ".";
+static char buffer[MAX_SIZE];
 
-static struct test_case_t {
+static struct tcase {
 	char *link;
+	char *buf;
 	size_t buf_size;
 	int exp_errno;
-} test_cases[] = {
-	{SYM_FILE1, 1, EACCES},
-	    /* Don't test with bufsize -1, since this cause a fortify-check-fail when
-	       using glibc and -D_FORITY_SOURCE=2
-
-	       Discussion: http://lkml.org/lkml/2008/10/23/229
-	       Conclusion: Only test with 0 as non-positive bufsize.
-
-	       { SYM_FILE2, -1, EINVAL, NULL },
-	     */
-	{SYM_FILE2, 0, EINVAL},
-	{TEST_FILE2, 1, EINVAL},
-	{longpathname, 1, ENAMETOOLONG},
-	{"", 1, ENOENT},
-	{SYM_FILE3, 1, ENOTDIR},
-	{elooppathname, 1, ELOOP},
+} tcases[] = {
+	{SYM_FILE1, buffer, sizeof(buffer), EACCES},
+	/* Don't test with bufsize -1, since this cause a fortify-check-fail
+	   when using glibc and -D_FORITY_SOURCE=2
+
+	   Discussion: http://lkml.org/lkml/2008/10/23/229
+	   Conclusion: Only test with 0 as non-positive bufsize.
+
+	   { SYM_FILE2, -1, EINVAL, NULL },
+	*/
+	{SYM_FILE2, buffer, 0, EINVAL},
+	{TEST_FILE2, buffer, sizeof(buffer), EINVAL},
+	{longpathname, buffer, sizeof(buffer), ENAMETOOLONG},
+	{"", buffer, sizeof(buffer), ENOENT},
+	{SYM_FILE3, buffer, sizeof(buffer), ENOTDIR},
+	{elooppathname, buffer, sizeof(buffer), ELOOP},
+	{SYMFILE, (char *)-1, sizeof(buffer), EFAULT},
 };
 
-static void setup(void);
-static void readlink_verify(struct test_case_t *);
-static void cleanup(void);
-
-char *TCID = "readlink03";
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-int main(int ac, char **av)
+static void verify_readlink(unsigned int n)
 {
-	int i, lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
+	struct tcase *tc = &tcases[n];
 
-	setup();
+	TEST(readlink(tc->link, tc->buf, tc->buf_size));
+	if (TEST_RETURN != -1) {
+		tst_res(TFAIL, "readlink() sueeeeded unexpectedly, errno");
+		return;
+	}
 
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-		tst_count = 0;
+	if (TEST_ERRNO != tc->exp_errno) {
+		tst_res(TFAIL | TTERRNO,
+			"readlink() failed unexpectedly; expected: %d - %s, got",
+			tc->exp_errno, tst_strerrno(tc->exp_errno));
 
-		for (i = 0; i < TST_TOTAL; i++)
-			readlink_verify(&test_cases[i]);
+		if (tc->exp_errno == ENOENT && TEST_ERRNO == EINVAL) {
+			tst_res(TWARN | TTERRNO,
+				"It may be a Kernel Bug, see the patch:"
+				"http://git.kernel.org/linus/1fa1e7f6");
+		}
+	} else {
+		tst_res(TPASS | TTERRNO, "readlink() failed as expected");
 	}
-
-	cleanup();
-	tst_exit();
 }
 
-void setup(void)
+static void setup(void)
 {
-	struct passwd *ltpuser;
 	int i;
+	struct passwd *pwent;
 
-	tst_require_root();
-
-	tst_sig(NOFORK, DEF_HANDLER, cleanup);
-
-	ltpuser = SAFE_GETPWNAM(cleanup, "nobody");
-	SAFE_SETEUID(cleanup, ltpuser->pw_uid);
-
-	TEST_PAUSE;
+	pwent = SAFE_GETPWNAM("nobody");
+	SAFE_SETEUID(pwent->pw_uid);
 
-	tst_tmpdir();
+	SAFE_MKDIR(DIR_TEMP, MODE_RWX);
+	SAFE_TOUCH(TEST_FILE1, 0666, NULL);
+	SAFE_SYMLINK(TEST_FILE1, SYM_FILE1);
+	SAFE_CHMOD(DIR_TEMP, FILE_MODE);
 
-	SAFE_MKDIR(cleanup, DIR_TEMP, MODE_RWX);
-	SAFE_TOUCH(cleanup, TEST_FILE1, 0666, NULL);
-	SAFE_SYMLINK(cleanup, TEST_FILE1, SYM_FILE1);
-	SAFE_CHMOD(cleanup, DIR_TEMP, FILE_MODE);
-
-	SAFE_TOUCH(cleanup, TEST_FILE2, 0666, NULL);
-	SAFE_SYMLINK(cleanup, TEST_FILE2, SYM_FILE2);
+	SAFE_TOUCH(TEST_FILE2, 0666, NULL);
+	SAFE_SYMLINK(TEST_FILE2, SYM_FILE2);
 
 	memset(longpathname, 'a', PATH_MAX + 1);
 
-	SAFE_TOUCH(cleanup, TEST_FILE3, 0666, NULL);
+	SAFE_TOUCH(TEST_FILE3, 0666, NULL);
+
+	SAFE_MKDIR("test_eloop", MODE_RWX);
+	SAFE_SYMLINK("../test_eloop", "test_eloop/test_eloop");
 
-	/*
-	 * NOTE: the ELOOP test is written based on that the consecutive
-	 * symlinks limit in kernel is hardwired to 40.
-	 */
-	SAFE_MKDIR(cleanup, "test_eloop", MODE_RWX);
-	SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
 	for (i = 0; i < 43; i++)
 		strcat(elooppathname, ELOOPFILE);
-}
-
-void readlink_verify(struct test_case_t *tc)
-{
-	char buffer[MAX_SIZE];
-
-	if (tc->buf_size == 1)
-		tc->buf_size = sizeof(buffer);
-
-	TEST(readlink(tc->link, buffer, tc->buf_size));
-
-	if (TEST_RETURN != -1) {
-		tst_resm(TFAIL, "readlink() returned %ld, "
-			"expected -1, errno:%d", TEST_RETURN,
-			tc->exp_errno);
-		return;
-	}
 
-	if (TEST_ERRNO == tc->exp_errno) {
-		tst_resm(TPASS | TTERRNO, "readlink() failed as expected");
-	} else {
-		tst_resm(TFAIL | TTERRNO,
-			"readlink() failed unexpectedly; expected: %d - %s",
-			tc->exp_errno, strerror(tc->exp_errno));
-		if (tc->exp_errno == ENOENT && TEST_ERRNO == EINVAL) {
-			tst_resm(TWARN | TTERRNO,
-				"It may be a Kernel Bug, see the patch:"
-				"http://git.kernel.org/linus/1fa1e7f6");
-		}
-	}
+	SAFE_TOUCH(TESTFILE, 0666, NULL);
+	SAFE_SYMLINK(TESTFILE, SYMFILE);
 }
 
-void cleanup(void)
+static void cleanup(void)
 {
-	if (seteuid(0) == -1)
-		tst_resm(TWARN | TERRNO, "seteuid(0) failed");
-
-	tst_rmdir();
+	SAFE_SETEUID(0);
 }
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_readlink,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH 4/4] syscalls/readlink04.c: Cleanup && Convert to new API
  2018-02-12 10:21 [LTP] [PATCH 1/4] syscalls/readlink.h: Move common macros to readlink.h Jinhui Huang
  2018-02-12 10:21 ` [LTP] [PATCH 2/4] syscalls/readlink01.c: Cleanup && Convert to new API Jinhui Huang
  2018-02-12 10:21 ` [LTP] [PATCH 3/4] syscalls/readlink03.c: " Jinhui Huang
@ 2018-02-12 10:21 ` Jinhui Huang
  2018-04-13 15:38 ` [LTP] [PATCH 1/4] syscalls/readlink.h: Move common macros to readlink.h Cyril Hrubis
  3 siblings, 0 replies; 5+ messages in thread
From: Jinhui Huang @ 2018-02-12 10:21 UTC (permalink / raw)
  To: ltp

Signed-off-by: Jinhui Huang <huangjh.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/readlink/readlink04.c | 137 +++++++-----------------
 1 file changed, 36 insertions(+), 101 deletions(-)

diff --git a/testcases/kernel/syscalls/readlink/readlink04.c b/testcases/kernel/syscalls/readlink/readlink04.c
index 9c6c589..ff2736b 100644
--- a/testcases/kernel/syscalls/readlink/readlink04.c
+++ b/testcases/kernel/syscalls/readlink/readlink04.c
@@ -11,131 +11,62 @@
  * 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.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program;  if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
 /*
- * Test Name : readlink04
- *
  * Test Description :
- *  Verify that, readlink call will succeed to read the contents of the
- *  symbolic link if invoked by non-root user who is not the owner of the
- *  symbolic link.
- *
- * Expected Result:
- *  readlink() should return the contents of symbolic link path in the
- *  specified buffer on success.
+ *  readlink() will succeed to read the contents of the symbolic link if
+ *  invoked by non-root user who is not the owner of the symbolic link.
  */
 
-#include <stdlib.h>
 #include <pwd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <sys/wait.h>
 #include <errno.h>
 #include <string.h>
-#include "test.h"
-#include "safe_macros.h"
-
-char *TCID = "readlink04";
-int TST_TOTAL = 1;
 
-static char *TESTFILE = "./testfile";
-static char *SYMFILE = "slink_file";
-
-#define MAX_SIZE	256
+#include "tst_test.h"
+#include "readlink.h"
 
 static int exp_val;
 static char buffer[MAX_SIZE];
 
-static void setup(void);
-static void cleanup(void);
-
-int main(int ac, char **av)
+static void verify_readlink(void)
 {
-	int lc;
-
-	tst_parse_opts(ac, av, NULL, NULL);
-
-	setup();
-
-	for (lc = 0; TEST_LOOPING(lc); lc++) {
-
-		tst_count = 0;
-
-		/*
-		 * Call readlink(2) to read the contents of
-		 * symlink into a buffer.
-		 */
-		TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
-
-		if (TEST_RETURN == -1) {
-			tst_resm(TFAIL | TTERRNO, "readlink() on %s failed",
-			         SYMFILE);
-			continue;
-		}
-
-		if (TEST_RETURN == exp_val) {
-			/* Check for the contents of buffer */
-			if (memcmp(buffer, TESTFILE, exp_val) != 0) {
-				tst_brkm(TFAIL, cleanup, "TESTFILE %s "
-					 "and buffer contents %s "
-					 "differ", TESTFILE, buffer);
-			} else {
-				tst_resm(TPASS, "readlink() "
-					 "functionality on '%s' is "
-					 "correct", SYMFILE);
-			}
+	TEST(readlink(SYMFILE, buffer, sizeof(buffer)));
+	if (TEST_RETURN == -1)
+		tst_res(TFAIL | TTERRNO, "readlink() on %s failed", SYMFILE);
+
+	if (TEST_RETURN != exp_val) {
+		tst_res(TFAIL, "readlink() return value %ld "
+			"did match, Expected %d", TEST_RETURN, exp_val);
+	} else {
+		if (memcmp(buffer, TESTFILE, exp_val) != 0) {
+			tst_brk(TFAIL, "TESTFILE %s and buffer "
+				"contents %s differ", TESTFILE, buffer);
 		} else {
-			tst_resm(TFAIL, "readlink() return value %ld "
-				 "doesn't match, Expected %d",
-				 TEST_RETURN, exp_val);
+			tst_res(TPASS, "readlink() functionality on '%s' was "
+				"correct", SYMFILE);
 		}
 	}
-
-	cleanup();
-	tst_exit();
 }
 
-#define FILE_MODE  S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH
-
 static void setup(void)
 {
 	int fd;
 	char *tmp_dir = NULL;
 	struct passwd *pwent;
 
-	tst_require_root();
-
-	TEST_PAUSE;
-
-	tst_tmpdir();
-
 	/* get the name of the temporary directory */
-	if ((tmp_dir = getcwd(tmp_dir, 0)) == NULL)
-		tst_brkm(TBROK, NULL, "getcwd failed");
-
-	if ((pwent = getpwnam("bin")) == NULL)
-		tst_brkm(TBROK, cleanup, "getpwname() failed");
+	tmp_dir = SAFE_GETCWD(tmp_dir, 0);
+	pwent = SAFE_GETPWNAM("bin");
 
 	/* make the tmp directory belong to bin */
-	SAFE_CHOWN(cleanup, tmp_dir, pwent->pw_uid, pwent->pw_gid);
-
-	if (chmod(tmp_dir, 0711) != 0)
-		tst_brkm(TBROK|TERRNO, cleanup, "chmod(%s) failed", tmp_dir);
+	SAFE_CHOWN(tmp_dir, pwent->pw_uid, pwent->pw_gid);
+	SAFE_CHMOD(tmp_dir, 0711);
 
 	/* create test file and symlink */
-	if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1)
-		tst_brkm(TBROK|TERRNO, cleanup, "open(%s) failed", TESTFILE);
-
-	if (close(fd))
-		tst_brkm(TBROK|TERRNO, cleanup, "close(%s) failed", TESTFILE);
-
-	SAFE_SYMLINK(cleanup, TESTFILE, SYMFILE);
+	fd = SAFE_OPEN(TESTFILE, O_RDWR | O_CREAT, FILE_MODE);
+	SAFE_CLOSE(fd);
+	SAFE_SYMLINK(TESTFILE, SYMFILE);
 
 	/* set up the expected return value from the readlink() call */
 	exp_val = strlen(TESTFILE);
@@ -144,15 +75,19 @@ static void setup(void)
 	memset(buffer, 0, MAX_SIZE);
 
 	/* finally, change the id of the parent process to "nobody" */
-	if ((pwent = getpwnam("nobody")) == NULL)
-		tst_brkm(TBROK, cleanup, "getpwname() failed for nobody");
-
-	SAFE_SETEUID(cleanup, pwent->pw_uid);
+	pwent = SAFE_GETPWNAM("nobody");
+	SAFE_SETEUID(pwent->pw_uid);
 }
 
 static void cleanup(void)
 {
-	SAFE_SETEUID(NULL, 0);
-
-	tst_rmdir();
+	SAFE_SETEUID(0);
 }
+
+static struct tst_test test = {
+	.test_all = verify_readlink,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+	.needs_root = 1,
+};
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH 1/4] syscalls/readlink.h: Move common macros to readlink.h
  2018-02-12 10:21 [LTP] [PATCH 1/4] syscalls/readlink.h: Move common macros to readlink.h Jinhui Huang
                   ` (2 preceding siblings ...)
  2018-02-12 10:21 ` [LTP] [PATCH 4/4] syscalls/readlink04.c: " Jinhui Huang
@ 2018-04-13 15:38 ` Cyril Hrubis
  3 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2018-04-13 15:38 UTC (permalink / raw)
  To: ltp

Hi!
I've fixed a few typos and and also merged the readlink04 to readlink01,
since these thestcase were nearly the same and pushed, thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2018-04-13 15:38 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-02-12 10:21 [LTP] [PATCH 1/4] syscalls/readlink.h: Move common macros to readlink.h Jinhui Huang
2018-02-12 10:21 ` [LTP] [PATCH 2/4] syscalls/readlink01.c: Cleanup && Convert to new API Jinhui Huang
2018-02-12 10:21 ` [LTP] [PATCH 3/4] syscalls/readlink03.c: " Jinhui Huang
2018-02-12 10:21 ` [LTP] [PATCH 4/4] syscalls/readlink04.c: " Jinhui Huang
2018-04-13 15:38 ` [LTP] [PATCH 1/4] syscalls/readlink.h: Move common macros to readlink.h Cyril Hrubis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox