Skip to contentSkip to conversation

add open/openat + O_TMPFILE tests

PATCH Superseded Alexey Kodanev 2 participants 10 messages Jan 28, 2016 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover add open/openat + O_TMPFILE tests

v2:
* move dir_not_empty() -> tst_dir_is_empty() to a library,
* add SAFE_LINKAT, SAFE_READLINK macros,
* move O_TMPFILE definition to lapi/fcntl.h
* use relative path for test_dir,
* enable len_strict in SAFE_WRITE,
* set constant buffer size.

Alexey Kodanev (6):
lib/tst_dir_is_empty: add a library function include/lapi/fcntl.h: add O_TMPFILE definition lib/safe_macros: add linkat()
lib/safe_macros: add readlink() kernel/syscalls: add new test with 'open() + O_TMPFILE' kernel/syscalls/open14: openat03: add new test-cases

 include/lapi/fcntl.h                        |    4 +
 include/safe_macros.h                       |   13 ++
 include/test.h                              |    8 +
 lib/safe_macros.c                           |   35 ++++
 lib/tst_dir_is_empty.c                      |   49 +++++
 runtest/syscalls                            |    2 +
 testcases/kernel/syscalls/.gitignore        |    2 +
 testcases/kernel/syscalls/open/open14.c     |  253 +++++++++++++++++++++++++++
 testcases/kernel/syscalls/openat/openat03.c |  252 ++++++++++++++++++++++++++
 9 files changed, 618 insertions(+), 0 deletions(-)
 create mode 100644 lib/tst_dir_is_empty.c
 create mode 100644 testcases/kernel/syscalls/open/open14.c
 create mode 100644 testcases/kernel/syscalls/openat/openat03.c
#1 lore raw

PATCH v2 1/6 lib/tst_dir_is_empty: add a library function

Checks if a given directory is empty.

---
 include/test.h         |    8 +++++++
 lib/tst_dir_is_empty.c |   49 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 57 insertions(+), 0 deletions(-)
 create mode 100644 lib/tst_dir_is_empty.c
include/test.h +8 -0
index fc07dae..c364b75 100644
@@ -441,6 +441,14 @@ int tst_fs_fill_hardlinks(void (*cleanup) (void), const char *dir);
 int tst_fs_fill_subdirs(void (*cleanup) (void), const char *dir);
 
 /*
+ * lib/tst_dir_is_empty.c
+ *
+ * Checks if a given directory contains any entities,
+ * returns 1 if directory is empty, 0 otherwise
+ */
+int tst_dir_is_empty(void (*cleanup) (void), const char *name, int verbose);
+
+/*
  * lib/tst_pid.c
  *
  * Get a pid value not used by the OS
lib/tst_dir_is_empty.c new file +49 -0

49 lines not on this page · raw message

Signature · 1 line
1.7.1
#2 lore raw

PATCH v2 2/6 include/lapi/fcntl.h: add O_TMPFILE definition

In reply to Alexey Kodanev
---
 include/lapi/fcntl.h |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
include/lapi/fcntl.h +4 -0
index d0bb713..1ea3fb4 100644
@@ -23,6 +23,10 @@
 # define O_CLOEXEC 02000000
 #endif
 
+#ifndef O_TMPFILE
+# define O_TMPFILE (020000000 | O_DIRECTORY)
+#endif
+
 #ifndef F_DUPFD_CLOEXEC
 # define F_DUPFD_CLOEXEC 1030
 #endif
Signature · 1 line
1.7.1
#3 lore raw

PATCH v2 3/6 lib/safe_macros: add linkat()

In reply to Alexey Kodanev
---
 include/safe_macros.h |    7 +++++++
 lib/safe_macros.c     |   18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)
include/safe_macros.h +7 -0
index ab09699..e4041d4 100644
@@ -157,6 +157,13 @@ int	safe_link(const char *file, const int lineno,
 #define SAFE_LINK(cleanup_fn, oldpath, newpath) \
         safe_link(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath))
 
+int	safe_linkat(const char *file, const int lineno,
+		    void (cleanup_fn)(void), int olddirfd, const char *oldpath,
+		    int newdirfd, const char *newpath, int flags);
+#define SAFE_LINKAT(cleanup_fn, olddirfd, oldpath, newdirfd, newpath, flags) \
+	safe_linkat(__FILE__, __LINE__, cleanup_fn, (olddirfd), (oldpath), \
+		    (newdirfd), (newpath), (flags))
+
 int	safe_symlink(const char *file, const int lineno,
                      void (cleanup_fn)(void), const char *oldpath,
                      const char *newpath);
lib/safe_macros.c +18 -0
index 173d72f..9fa6ac4 100644
@@ -391,6 +391,24 @@ int safe_link(const char *file, const int lineno,
 	return rval;
 }
 
+int safe_linkat(const char *file, const int lineno,
+		void (cleanup_fn)(void), int olddirfd, const char *oldpath,
+		int newdirfd, const char *newpath, int flags)
+{
+	int rval;
+
+	rval = linkat(olddirfd, oldpath, newdirfd, newpath, flags);
+
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "%s:%d: linkat(%d,%s,%d,%s,%d) failed",
+			 file, lineno, olddirfd, oldpath, newdirfd,
+			 newpath, flags);
+	}
+
+	return rval;
+}
+
 int safe_symlink(const char *file, const int lineno,
                  void (cleanup_fn)(void), const char *oldpath,
                  const char *newpath)
Signature · 1 line
1.7.1
#4 lore raw

PATCH v2 4/6 lib/safe_macros: add readlink()

In reply to Alexey Kodanev
---
 include/safe_macros.h |    6 ++++++
 lib/safe_macros.c     |   17 +++++++++++++++++
 2 files changed, 23 insertions(+), 0 deletions(-)
include/safe_macros.h +6 -0
index e4041d4..8545c97 100644
@@ -164,6 +164,12 @@ int	safe_linkat(const char *file, const int lineno,
 	safe_linkat(__FILE__, __LINE__, cleanup_fn, (olddirfd), (oldpath), \
 		    (newdirfd), (newpath), (flags))
 
+ssize_t	safe_readlink(const char *file, const int lineno,
+		      void (cleanup_fn)(void), const char *path,
+		      char *buf, size_t bufsize);
+#define SAFE_READLINK(cleanup_fn, path, buf, bufsize) \
+	safe_readlink(__FILE__, __LINE__, cleanup_fn, (path), (buf), (bufsize))
+
 int	safe_symlink(const char *file, const int lineno,
                      void (cleanup_fn)(void), const char *oldpath,
                      const char *newpath);
lib/safe_macros.c +17 -0
index 9fa6ac4..5a05c84 100644
@@ -409,6 +409,23 @@ int safe_linkat(const char *file, const int lineno,
 	return rval;
 }
 
+ssize_t safe_readlink(const char *file, const int lineno,
+		  void (cleanup_fn)(void), const char *path,
+		  char *buf, size_t bufsize)
+{
+	ssize_t rval;
+
+	rval = readlink(path, buf, bufsize);
+
+	if (rval == -1) {
+		tst_brkm(TBROK | TERRNO, cleanup_fn,
+			 "%s:%d: readlink(%s,%p,%zu) failed",
+			 file, lineno, path, buf, bufsize);
+	}
+
+	return rval;
+}
+
 int safe_symlink(const char *file, const int lineno,
                  void (cleanup_fn)(void), const char *oldpath,
                  const char *newpath)
Signature · 1 line
1.7.1
#5 lore raw

PATCH v2 5/6 kernel/syscalls: add new test with 'open() + O_TMPFILE'

In reply to Alexey Kodanev

Test does the following steps:
* create an unnamed temporary file in TMP directory,
* write data into it,
* check that file not visible in the filesystem,
* name the file and check that it becomes visible in FS.

'openat() + O_TMPFILE' test repeats the same steps.

---
 runtest/syscalls                            |    2 +
 testcases/kernel/syscalls/.gitignore        |    2 +
 testcases/kernel/syscalls/open/open14.c     |  120 +++++++++++++++++++++++++++
 testcases/kernel/syscalls/openat/openat03.c |  120 +++++++++++++++++++++++++++
 4 files changed, 244 insertions(+), 0 deletions(-)
 create mode 100644 testcases/kernel/syscalls/open/open14.c
 create mode 100644 testcases/kernel/syscalls/openat/openat03.c
runtest/syscalls +2 -0
index 7173f22..3439948 100644
@@ -721,10 +721,12 @@ open10 open10
 open11 open11
 open12 open12
 open13 open13
+open14 open14
 
 #openat test cases
 openat01 openat01
 openat02 openat02
+openat03 openat03
 
 mincore01 mincore01
 mincore02 mincore02
testcases/kernel/syscalls/.gitignore +2 -0
index d5f21ef..6451dc4 100644
@@ -624,9 +624,11 @@
 /open/open12
 /open/open12_child
 /open/open13
+/open/open14
 /openat/openat01
 /openat/openat02
 /openat/openat02_child
+/openat/openat03
 /pathconf/pathconf01
 /pause/pause01
 /pause/pause02
testcases/kernel/syscalls/open/open14.c new file +120 -0

120 lines not on this page · raw message

testcases/kernel/syscalls/openat/openat03.c new file +120 -0

120 lines not on this page · raw message

Signature · 1 line
1.7.1
#6 lore raw

PATCH v2 5/6 kernel/syscalls: add new test with 'open() + O_TMPFILE'

Hi!

Quoted 18 lines
testcases/kernel/syscalls/open/open14.c new file +18 -0
+#define _GNU_SOURCE
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "test.h"
+#include "safe_macros.h"
+#include "lapi/fcntl.h"
+
+char *TCID = "open14";
+int TST_TOTAL = 1;
+static const char *test_dir = ".";
+static const ssize_t size = 1024;
+static char buf[1024];
+static const ssize_t blocks_num = 4;
+static struct stat st;
+

testcases/kernel/syscalls/open/open14.c line 48

Quoted 10 lines
testcases/kernel/syscalls/open/open14.c new file +10 -0
+static void cleanup(void)
+{
+	tst_rmdir();
+}
+
+static void setup(void)
+{
+	tst_tmpdir();
+
+	memset(buf, 1, size);
                       ^
		       sizeof(buf)
Quoted 27 lines
testcases/kernel/syscalls/open/open14.c new file +27 -0
+}
+
+void test01(void)
+{
+	int fd, i;
+
+	char path[PATH_MAX], tmp[PATH_MAX];
+
+	tst_resm(TINFO, "creating a file with O_TMPFILE flag");
+
+	fd = open(test_dir, O_TMPFILE | O_WRONLY | O_SYNC);
+	if (fd == -1) {
+		if (errno == EISDIR) {
+			tst_brkm(TCONF, cleanup,
+				"O_TMPFILE not supported");
+		}
+		tst_resm(TFAIL | TERRNO, "open() failed");
+		return;
+	}
+
+	tst_resm(TINFO, "writing data to the file");
+	for (i = 0; i < blocks_num; ++i)
+		SAFE_WRITE(cleanup, 1, fd, buf, size);
+
+	SAFE_FSTAT(cleanup, fd, &st);
+	tst_resm(TINFO, "file size is '%zu'", st.st_size);
+

testcases/kernel/syscalls/open/open14.c line 85

Quoted 10 lines
testcases/kernel/syscalls/open/open14.c new file +10 -0
+	if (st.st_size != blocks_num * size) {
+		tst_resm(TFAIL, "not expected size: '%zu' != '%zu'",
+			 st.st_size, blocks_num * size);
+		SAFE_CLOSE(cleanup, fd);
+		return;
+	}
+
+	tst_resm(TINFO, "looking for the file in '%s'", test_dir);
+	if (!tst_dir_is_empty(cleanup, test_dir, 1))
+		tst_brkm(TBROK, cleanup, "found a file, this is not expected");

^
Isn't this TFAIL rather than TBROK?

I see the difference as:

If some of the preparation steps has failed -> TBROK If some test assertion fails -> TFAIL

testcases/kernel/syscalls/open/open14.c line 97

Quoted 12 lines
testcases/kernel/syscalls/open/open14.c new file +12 -0
+	tst_resm(TINFO, "file not found, OK");
+
+	snprintf(path, PATH_MAX,  "/proc/self/fd/%d", fd);
+	SAFE_READLINK(cleanup, path, tmp, PATH_MAX);
+
+	tst_resm(TINFO, "renaming '%s' -> '%s/tmpfile'",
+		 tmp, test_dir);
+	SAFE_LINKAT(cleanup, AT_FDCWD, path, AT_FDCWD, "tmpfile",
+		    AT_SYMLINK_FOLLOW);
+
+	if (tst_dir_is_empty(cleanup, test_dir, 1))
+		tst_brkm(TBROK, cleanup, "file not found");

Here as well.

Quoted 66 lines
testcases/kernel/syscalls/open/open14.c new file +22 -0
+	SAFE_UNLINK(cleanup, "tmpfile");
+	SAFE_CLOSE(cleanup, fd);
+
+	tst_resm(TPASS, "test succeeded");
+}
+
+int main(int ac, char *av[])
+{
+	int lc;
+
+	tst_parse_opts(ac, av, NULL, NULL);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); ++lc) {
+		tst_count = 0;
+		test01();
+	}
+
+	cleanup();
+	tst_exit();
+}
testcases/kernel/syscalls/openat/openat03.c new file +38 -0
new file mode 100644
index 0000000..3a0c2f1
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2015-2016 Oracle and/or its affiliates. All Rights Reserved.
+ *
+ * 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 would 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, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Alexey Kodanev <[email protected]>
+ *
+ */
+
+#define _GNU_SOURCE
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+
+#include "test.h"
+#include "safe_macros.h"
+#include "lapi/fcntl.h"
+
+char *TCID = "openat03";
+int TST_TOTAL = 1;
+static const char *test_dir = ".";
+static const ssize_t size = 1024;
+static char buf[1024];
+static const ssize_t blocks_num = 4;
+static struct stat st;
+

testcases/kernel/syscalls/openat/openat03.c line 48

Quoted 10 lines
testcases/kernel/syscalls/openat/openat03.c new file +10 -0
+static void cleanup(void)
+{
+	tst_rmdir();
+}
+
+static void setup(void)
+{
+	tst_tmpdir();
+
+	memset(buf, 1, size);

Here as well.

Quoted 27 lines
testcases/kernel/syscalls/openat/openat03.c new file +27 -0
+}
+
+void test01(void)
+{
+	int fd, i;
+
+	char path[PATH_MAX], tmp[PATH_MAX];
+
+	tst_resm(TINFO, "creating a file with O_TMPFILE flag");
+
+	fd = openat(AT_FDCWD, test_dir, O_TMPFILE | O_WRONLY | O_SYNC);
+	if (fd == -1) {
+		if (errno == EISDIR) {
+			tst_brkm(TCONF, cleanup,
+				"O_TMPFILE not supported");
+		}
+		tst_resm(TFAIL | TERRNO, "openat() failed");
+		return;
+	}
+
+	tst_resm(TINFO, "writing data to the file");
+	for (i = 0; i < blocks_num; ++i)
+		SAFE_WRITE(cleanup, 1, fd, buf, size);
+
+	SAFE_FSTAT(cleanup, fd, &st);
+	tst_resm(TINFO, "file size is '%zu'", st.st_size);
+

testcases/kernel/syscalls/openat/openat03.c line 85

Quoted 10 lines
testcases/kernel/syscalls/openat/openat03.c new file +10 -0
+	if (st.st_size != blocks_num * size) {
+		tst_resm(TFAIL, "not expected size: '%zu' != '%zu'",
+			 st.st_size, blocks_num * size);
+		SAFE_CLOSE(cleanup, fd);
+		return;
+	}
+
+	tst_resm(TINFO, "looking for the file in '%s'", test_dir);
+	if (!tst_dir_is_empty(cleanup, test_dir, 1))
+		tst_brkm(TBROK, cleanup, "found a file, this is not expected");

And here.

testcases/kernel/syscalls/openat/openat03.c line 97

Quoted 12 lines
testcases/kernel/syscalls/openat/openat03.c new file +12 -0
+	tst_resm(TINFO, "file not found, OK");
+
+	snprintf(path, PATH_MAX,  "/proc/self/fd/%d", fd);
+	SAFE_READLINK(cleanup, path, tmp, PATH_MAX);
+
+	tst_resm(TINFO, "renaming '%s' -> '%s/tmpfile'",
+		 tmp, test_dir);
+	SAFE_LINKAT(cleanup, AT_FDCWD, path, AT_FDCWD, "tmpfile",
+		    AT_SYMLINK_FOLLOW);
+
+	if (tst_dir_is_empty(cleanup, test_dir, 1))
+		tst_brkm(TBROK, cleanup, "file not found");

And here.

Quoted 28 lines
testcases/kernel/syscalls/openat/openat03.c new file +22 -0
+	SAFE_UNLINK(cleanup, "tmpfile");
+	SAFE_CLOSE(cleanup, fd);
+
+	tst_resm(TPASS, "test succeeded");
+}
+
+int main(int ac, char *av[])
+{
+	int lc;
+
+	tst_parse_opts(ac, av, NULL, NULL);
+
+	setup();
+
+	for (lc = 0; TEST_LOOPING(lc); ++lc) {
+		tst_count = 0;
+		test01();
+	}
+
+	cleanup();
+	tst_exit();
+}
Signature · 5 lines
1.7.1


-- 
Mailing list info: http://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
#7 lore raw

PATCH v2 6/6 kernel/syscalls/open14: openat03: add new test-cases

In reply to Alexey Kodanev

* create multiple directories and related temporary files;

* create multiple directories and link files into them. Check that files permissions correspond to the ones specified with open()/openat().

---
 testcases/kernel/syscalls/open/open14.c     |  163 ++++++++++++++++++++++++---
 testcases/kernel/syscalls/openat/openat03.c |  160 ++++++++++++++++++++++++---
 2 files changed, 294 insertions(+), 29 deletions(-)
testcases/kernel/syscalls/open/open14.c +148 -15
index 3784cb3..37a900a 100644
@@ -21,6 +21,7 @@
 #define _GNU_SOURCE
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
 
@@ -29,7 +30,8 @@
 #include "lapi/fcntl.h"
 
 char *TCID = "open14";
-int TST_TOTAL = 1;
+int TST_TOTAL = 3;
+
 static const char *test_dir = ".";
 static const ssize_t size = 1024;
 static char buf[1024];
@@ -48,27 +50,36 @@ static void setup(void)
 	memset(buf, 1, size);
 }
 
-void test01(void)
+static void create_file(const char *dir_name, int *fd, int mode)
 {
-	int fd, i;
+	*fd = open(dir_name, O_TMPFILE | O_RDWR, mode);
+	if (*fd != -1)
+		return;
 
-	char path[PATH_MAX], tmp[PATH_MAX];
+	if (errno == EISDIR)
+		tst_brkm(TCONF, cleanup, "O_TMPFILE not supported");
 
-	tst_resm(TINFO, "creating a file with O_TMPFILE flag");
+	tst_brkm(TFAIL | TERRNO, cleanup, "open() failed");
+}
 
-	fd = open(test_dir, O_TMPFILE | O_WRONLY | O_SYNC);
-	if (fd == -1) {
-		if (errno == EISDIR) {
-			tst_brkm(TCONF, cleanup,
-				"O_TMPFILE not supported");
-		}
-		tst_resm(TFAIL | TERRNO, "open() failed");
-		return;
-	}
+static void write_file(int fd)
+{
+	int i;
 
-	tst_resm(TINFO, "writing data to the file");
 	for (i = 0; i < blocks_num; ++i)
 		SAFE_WRITE(cleanup, 1, fd, buf, size);
+}
+
+void test01(void)
+{
+	int fd;
+	char path[PATH_MAX], tmp[PATH_MAX];
+
+	tst_resm(TINFO, "creating a file with O_TMPFILE flag");
+	create_file(test_dir, &fd, 0600);
+
+	tst_resm(TINFO, "writing data to the file");
+	write_file(fd);
 
 	SAFE_FSTAT(cleanup, fd, &st);
 	tst_resm(TINFO, "file size is '%zu'", st.st_size);
@@ -102,6 +113,126 @@ void test01(void)
 	tst_resm(TPASS, "test succeeded");
 }
 
+static void read_file(int fd)
+{
+	int i;
+	char tmp[size];
+
+	SAFE_LSEEK(cleanup, fd, 0, SEEK_SET);
+
+	for (i = 0; i < blocks_num; ++i) {
+		SAFE_READ(cleanup, 0, fd, tmp, size);
+		if (strncmp(buf, tmp, size))
+			tst_brkm(TBROK, cleanup, "got unexepected data");
+	}
+}
+
+static void test02(void)
+{
+	const int files_num = 100;
+	int i, fd[files_num];
+	char path[PATH_MAX];
+
+	tst_resm(TINFO, "create files in multiple directories");
+	for (i = 0; i < files_num; ++i) {
+		snprintf(path, PATH_MAX, "tst02_%d", i);
+		SAFE_MKDIR(cleanup, path, 0700);
+		SAFE_CHDIR(cleanup, path);
+		SAFE_GETCWD(cleanup, path, PATH_MAX);
+		create_file(path, fd + i, 0600);
+		write_file(fd[i]);
+	}
+
+	tst_resm(TINFO, "removing test directories");
+	for (i = files_num - 1; i >= 0; --i) {
+		SAFE_CHDIR(cleanup, "../");
+		snprintf(path, PATH_MAX, "tst02_%d", i);
+		SAFE_RMDIR(cleanup, path);
+	}
+
+	tst_resm(TINFO, "writing/reading temporary files");
+	for (i = 0; i < files_num; ++i) {
+		write_file(fd[i]);
+		read_file(fd[i]);
+	}
+
+	tst_resm(TINFO, "closing temporary files");
+	for (i = 0; i < files_num; ++i)
+		SAFE_CLOSE(cleanup, fd[i]);
+
+	tst_resm(TPASS, "test succeeded");
+}
+
+static void link_tmp_file(int fd)
+{
+	char path1[PATH_MAX], path2[PATH_MAX];
+
+	snprintf(path1, PATH_MAX,  "/proc/self/fd/%d", fd);
+	snprintf(path2, PATH_MAX,  "tmpfile_%d", fd);
+
+	SAFE_LINKAT(cleanup, AT_FDCWD, path1, AT_FDCWD, path2,
+		    AT_SYMLINK_FOLLOW);
+}
+
+static const int tst_perm[] = { 0, 07777, 001, 0755, 0644, 0440 };
+
+static void test03(void)
+{
+	const int files_num = 100;
+	int i, fd[files_num];
+	char path[PATH_MAX];
+	struct stat st;
+	unsigned int j;
+	mode_t mask = umask(0);
+
+	umask(mask);
+
+	tst_resm(TINFO, "create multiple directories, link files into them");
+	tst_resm(TINFO, "and check file permissions");
+	for (i = 0, j = 0; i < files_num; ++i) {
+
+		snprintf(path, PATH_MAX, "tst03_%d", i);
+		SAFE_MKDIR(cleanup, path, 0700);
+		SAFE_CHDIR(cleanup, path);
+		SAFE_GETCWD(cleanup, path, PATH_MAX);
+
+		create_file(path, fd + i, tst_perm[j]);
+		write_file(fd[i]);
+		read_file(fd[i]);
+
+		link_tmp_file(fd[i]);
+
+		snprintf(path, PATH_MAX, "tmpfile_%d", fd[i]);
+
+		SAFE_LSTAT(cleanup, path, &st);
+
+		mode_t exp_mode = tst_perm[j] & ~mask;
+
+		if ((st.st_mode & ~S_IFMT) != exp_mode) {
+			tst_brkm(TFAIL, cleanup,
+				"file mode read %o, but expected %o",
+				st.st_mode & ~S_IFMT, exp_mode);
+		}
+
+		if (++j >= ARRAY_SIZE(tst_perm))
+			j = 0;
+	}
+
+	tst_resm(TINFO, "remove files, directories");
+	for (i = files_num - 1; i >= 0; --i) {
+		snprintf(path, PATH_MAX, "tmpfile_%d", fd[i]);
+		SAFE_UNLINK(cleanup, path);
+		SAFE_CLOSE(cleanup, fd[i]);
+
+		SAFE_CHDIR(cleanup, "..");
+
+		snprintf(path, PATH_MAX, "tst03_%d", i);
+		SAFE_RMDIR(cleanup, path);
+	}
+
+	tst_resm(TPASS, "test passed");
+}
+
 int main(int ac, char *av[])
 {
 	int lc;
@@ -113,6 +244,8 @@ int main(int ac, char *av[])
 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
 		tst_count = 0;
 		test01();
+		test02();
+		test03();
 	}
 
 	cleanup();
testcases/kernel/syscalls/openat/openat03.c +146 -14

191 lines not on this page · raw message

Signature · 1 line
1.7.1
#8 lore raw

PATCH v2 6/6 kernel/syscalls/open14: openat03: add new test-cases

Hi!

Quoted 5 lines

* create multiple directories and related temporary files;

* create multiple directories and link files into them. Check that files permissions correspond to the ones specified with open()/openat().

I would say that for newly added code it makes more sense to add new files in one patch. It's kind of strange to add more code to files you have added in previous one.

Quoted 37 lines
---
 testcases/kernel/syscalls/open/open14.c     |  163 ++++++++++++++++++++++++---
 testcases/kernel/syscalls/openat/openat03.c |  160 ++++++++++++++++++++++++---
 2 files changed, 294 insertions(+), 29 deletions(-)
testcases/kernel/syscalls/open/open14.c +5 -3
index 3784cb3..37a900a 100644
@@ -21,6 +21,7 @@
 #define _GNU_SOURCE
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
 
@@ -29,7 +30,8 @@
 #include "lapi/fcntl.h"
 
 char *TCID = "open14";
-int TST_TOTAL = 1;
+int TST_TOTAL = 3;
+
 static const char *test_dir = ".";
 static const ssize_t size = 1024;
 static char buf[1024];
@@ -48,27 +50,36 @@ static void setup(void)
 	memset(buf, 1, size);
 }
 
-void test01(void)
+static void create_file(const char *dir_name, int *fd, int mode)
 {
-	int fd, i;
+	*fd = open(dir_name, O_TMPFILE | O_RDWR, mode);

testcases/kernel/syscalls/open/open14.c static void setup(void) · line 63

Quoted 10 lines
testcases/kernel/syscalls/open/open14.c +6 -2
+	if (*fd != -1)
+		return;
 
-	char path[PATH_MAX], tmp[PATH_MAX];
+	if (errno == EISDIR)
+		tst_brkm(TCONF, cleanup, "O_TMPFILE not supported");
 
-	tst_resm(TINFO, "creating a file with O_TMPFILE flag");
+	tst_brkm(TFAIL | TERRNO, cleanup, "open() failed");
+}

Maybe it would be easier to try to open dummy tmpfile in setup and check for EISDIR there. Then we can just do SAFE_OPEN() in the rest of the code.

Quoted 35 lines
testcases/kernel/syscalls/open/open14.c +15 -10
-	fd = open(test_dir, O_TMPFILE | O_WRONLY | O_SYNC);
-	if (fd == -1) {
-		if (errno == EISDIR) {
-			tst_brkm(TCONF, cleanup,
-				"O_TMPFILE not supported");
-		}
-		tst_resm(TFAIL | TERRNO, "open() failed");
-		return;
-	}
+static void write_file(int fd)
+{
+	int i;
 
-	tst_resm(TINFO, "writing data to the file");
 	for (i = 0; i < blocks_num; ++i)
 		SAFE_WRITE(cleanup, 1, fd, buf, size);
+}
+
+void test01(void)
+{
+	int fd;
+	char path[PATH_MAX], tmp[PATH_MAX];
+
+	tst_resm(TINFO, "creating a file with O_TMPFILE flag");
+	create_file(test_dir, &fd, 0600);
+
+	tst_resm(TINFO, "writing data to the file");
+	write_file(fd);
 
 	SAFE_FSTAT(cleanup, fd, &st);
 	tst_resm(TINFO, "file size is '%zu'", st.st_size);
@@ -102,6 +113,126 @@ void test01(void)
 	tst_resm(TPASS, "test succeeded");
 }
 

testcases/kernel/syscalls/open/open14.c void test01(void) · line 125

Quoted 10 lines
testcases/kernel/syscalls/open/open14.c +10 -0
+static void read_file(int fd)
+{
+	int i;
+	char tmp[size];
+
+	SAFE_LSEEK(cleanup, fd, 0, SEEK_SET);
+
+	for (i = 0; i < blocks_num; ++i) {
+		SAFE_READ(cleanup, 0, fd, tmp, size);
+		if (strncmp(buf, tmp, size))\

Using memcmp() here would be a bit cleaner solution.

testcases/kernel/syscalls/open/open14.c void test01(void) · line 126

Quoted 1 line
testcases/kernel/syscalls/open/open14.c +1 -0
+			tst_brkm(TBROK, cleanup, "got unexepected data");

Here again, I would say that this should rather be TFAIL.

Quoted 6 lines
testcases/kernel/syscalls/open/open14.c +6 -0
+	}
+}
+
+static void test02(void)
+{
+	const int files_num = 100;

testcases/kernel/syscalls/open/open14.c void test01(void) · line 142

Quoted 10 lines
testcases/kernel/syscalls/open/open14.c +10 -0
+	int i, fd[files_num];
+	char path[PATH_MAX];
+
+	tst_resm(TINFO, "create files in multiple directories");
+	for (i = 0; i < files_num; ++i) {
+		snprintf(path, PATH_MAX, "tst02_%d", i);
+		SAFE_MKDIR(cleanup, path, 0700);
+		SAFE_CHDIR(cleanup, path);
+		SAFE_GETCWD(cleanup, path, PATH_MAX);
+		create_file(path, fd + i, 0600);

We can do just create_file(".", fd + 1, 0600) here as well.

Quoted 47 lines
testcases/kernel/syscalls/open/open14.c +46 -0
+		write_file(fd[i]);
+	}
+
+	tst_resm(TINFO, "removing test directories");
+	for (i = files_num - 1; i >= 0; --i) {
+		SAFE_CHDIR(cleanup, "../");
+		snprintf(path, PATH_MAX, "tst02_%d", i);
+		SAFE_RMDIR(cleanup, path);
+	}
+
+	tst_resm(TINFO, "writing/reading temporary files");
+	for (i = 0; i < files_num; ++i) {
+		write_file(fd[i]);
+		read_file(fd[i]);
+	}
+
+	tst_resm(TINFO, "closing temporary files");
+	for (i = 0; i < files_num; ++i)
+		SAFE_CLOSE(cleanup, fd[i]);
+
+	tst_resm(TPASS, "test succeeded");
+}
 
+static void link_tmp_file(int fd)
+{
+	char path1[PATH_MAX], path2[PATH_MAX];
+
+	snprintf(path1, PATH_MAX,  "/proc/self/fd/%d", fd);
+	snprintf(path2, PATH_MAX,  "tmpfile_%d", fd);
+
+	SAFE_LINKAT(cleanup, AT_FDCWD, path1, AT_FDCWD, path2,
+		    AT_SYMLINK_FOLLOW);
+}
+
+static const int tst_perm[] = { 0, 07777, 001, 0755, 0644, 0440 };
+
+static void test03(void)
+{
+	const int files_num = 100;
+	int i, fd[files_num];
+	char path[PATH_MAX];
+	struct stat st;
+	unsigned int j;
+	mode_t mask = umask(0);
+
+	umask(mask);
+

testcases/kernel/syscalls/open/open14.c void test01(void) · line 199

Quoted 10 lines
testcases/kernel/syscalls/open/open14.c +10 -0
+	tst_resm(TINFO, "create multiple directories, link files into them");
+	tst_resm(TINFO, "and check file permissions");
+	for (i = 0, j = 0; i < files_num; ++i) {
+
+		snprintf(path, PATH_MAX, "tst03_%d", i);
+		SAFE_MKDIR(cleanup, path, 0700);
+		SAFE_CHDIR(cleanup, path);
+		SAFE_GETCWD(cleanup, path, PATH_MAX);
+
+		create_file(path, fd + i, tst_perm[j]);

And here as well.

Quoted 24 lines
testcases/kernel/syscalls/open/open14.c +24 -0
+		write_file(fd[i]);
+		read_file(fd[i]);
+
+		link_tmp_file(fd[i]);
+
+		snprintf(path, PATH_MAX, "tmpfile_%d", fd[i]);
+
+		SAFE_LSTAT(cleanup, path, &st);
+
+		mode_t exp_mode = tst_perm[j] & ~mask;
+
+		if ((st.st_mode & ~S_IFMT) != exp_mode) {
+			tst_brkm(TFAIL, cleanup,
+				"file mode read %o, but expected %o",
+				st.st_mode & ~S_IFMT, exp_mode);
+		}
+
+		if (++j >= ARRAY_SIZE(tst_perm))
+			j = 0;
+	}
+
+	tst_resm(TINFO, "remove files, directories");
+	for (i = files_num - 1; i >= 0; --i) {
+		snprintf(path, PATH_MAX, "tmpfile_%d", fd[i]);

testcases/kernel/syscalls/open/open14.c void test01(void) · line 233

Quoted 10 lines
testcases/kernel/syscalls/open/open14.c +10 -0
+		SAFE_UNLINK(cleanup, path);
+		SAFE_CLOSE(cleanup, fd[i]);
+
+		SAFE_CHDIR(cleanup, "..");
+
+		snprintf(path, PATH_MAX, "tst03_%d", i);
+		SAFE_RMDIR(cleanup, path);
+	}
+
+	tst_resm(TPASS, "test passed");

We should be a bit more informative here.

What about "permission tests passed"

And for test02 we should say somethign as: "unlink tests passed"

testcases/kernel/syscalls/openat/openat03.c

Quoted 18 lines
testcases/kernel/syscalls/open/open14.c +4 -0
+}
+
 int main(int ac, char *av[])
 {
 	int lc;
@@ -113,6 +244,8 @@ int main(int ac, char *av[])
 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
 		tst_count = 0;
 		test01();
+		test02();
+		test03();
 	}
 
 	cleanup();
testcases/kernel/syscalls/openat/openat03.c +0 -0
index 3a0c2f1..743442c 100644

The same applies for openat03.

The rest of the patchset looks good.

Signature · 2 lines
Cyril Hrubis
[email protected]
#9 lore raw

PATCH v2 6/6 kernel/syscalls/open14: openat03: add new test-cases

Hi,

Quoted 9 lines · Cyril Hrubis

Hi!

Quoted 5 lines

* create multiple directories and related temporary files;

* create multiple directories and link files into them. Check that files permissions correspond to the ones specified with open()/openat().

I would say that for newly added code it makes more sense to add new files in one patch. It's kind of strange to add more code to files you have added in previous one.

The previous patch was sent in the end of Dec, the last one quite recently. I thought it would be easier to notice that the last one is included here right now. Agree, it looks strange in the one patch set. Will squash it into the previous.

Thanks,
Alexey