[RFC,2/3] syscalls/fanotify03: included execve() to generate_events() to increase test coverage

Message ID 2b2a0e71fd63b7536f0cdfbbc24570283ea98c33.1540348505.git.mbobrowski@mbobrowski.org
State Superseded
Headers show
Series
  • syscalls/fanotify03: add support and tests for new FAN_OPEN_EXEC_PERM flag
Related show

Commit Message

Matthew Bobrowski Oct. 24, 2018, 3:27 a.m.
* Created an executable helper program 'fanotify_child' so that can be
  used within fanotify tests

* Defined .resource_files so that additional test resources can be
  copied across to the tmp working directory i.e. fanotify_child

* Updated generate_events() so that it now includes a call to execve()
  on fanotify_child. This is so that we can increase the overall test
  coverage by generating more events on a watched object

* Updated each tcase events[] to accommodate for the additional events
  generated by execve()

Signed-off-by: Matthew Bobrowski <mbobrowski@mbobrowski.org>
---
 testcases/kernel/syscalls/fanotify/.gitignore |  1 +
 .../kernel/syscalls/fanotify/fanotify03.c     | 83 ++++++++++++-------
 .../kernel/syscalls/fanotify/fanotify_child.c | 14 ++++
 3 files changed, 66 insertions(+), 32 deletions(-)
 create mode 100644 testcases/kernel/syscalls/fanotify/fanotify_child.c

Patch

diff --git a/testcases/kernel/syscalls/fanotify/.gitignore b/testcases/kernel/syscalls/fanotify/.gitignore
index c26f2bd27..af420b8b3 100644
--- a/testcases/kernel/syscalls/fanotify/.gitignore
+++ b/testcases/kernel/syscalls/fanotify/.gitignore
@@ -8,3 +8,4 @@ 
 /fanotify08
 /fanotify09
 /fanotify10
+/fanotify_child
diff --git a/testcases/kernel/syscalls/fanotify/fanotify03.c b/testcases/kernel/syscalls/fanotify/fanotify03.c
index cca15aa00..f9418ee6b 100644
--- a/testcases/kernel/syscalls/fanotify/fanotify03.c
+++ b/testcases/kernel/syscalls/fanotify/fanotify03.c
@@ -35,6 +35,7 @@ 
 
 #define BUF_SIZE 256
 #define TST_TOTAL 3
+#define TEST_APP "fanotify_child"
 
 static char fname[BUF_SIZE];
 static char buf[BUF_SIZE];
@@ -60,28 +61,31 @@  static struct tcase {
 	{
 		"inode mark permission events",
 		INIT_FANOTIFY_MARK_TYPE(INODE),
-		FAN_OPEN_PERM | FAN_ACCESS_PERM, 2,
+		FAN_OPEN_PERM | FAN_ACCESS_PERM, 3,
 		{
 			{FAN_OPEN_PERM, FAN_ALLOW},
-			{FAN_ACCESS_PERM, FAN_DENY}
+			{FAN_ACCESS_PERM, FAN_DENY},
+			{FAN_OPEN_PERM, FAN_DENY}
 		}
 	},
 	{
 		"mount mark permission events",
 		INIT_FANOTIFY_MARK_TYPE(MOUNT),
-		FAN_OPEN_PERM | FAN_ACCESS_PERM, 2,
+		FAN_OPEN_PERM | FAN_ACCESS_PERM, 3,
 		{
 			{FAN_OPEN_PERM, FAN_ALLOW},
-			{FAN_ACCESS_PERM, FAN_DENY}
+			{FAN_ACCESS_PERM, FAN_DENY},
+			{FAN_OPEN_PERM, FAN_DENY}
 		}
 	},
 	{
 		"filesystem mark permission events",
 		INIT_FANOTIFY_MARK_TYPE(FILESYSTEM),
-		FAN_OPEN_PERM | FAN_ACCESS_PERM, 2,
+		FAN_OPEN_PERM | FAN_ACCESS_PERM, 3,
 		{
 			{FAN_OPEN_PERM, FAN_ALLOW},
-			{FAN_ACCESS_PERM, FAN_DENY}
+			{FAN_ACCESS_PERM, FAN_DENY},
+			{FAN_OPEN_PERM, FAN_DENY}
 		}
 	}
 };
@@ -89,9 +93,10 @@  static struct tcase {
 static void generate_events(void)
 {
 	int fd;
+	char *const argv[] = {TEST_APP, NULL};
 
 	/*
-	 * generate sequence of events
+	 * Generate sequence of events
 	 */
 	if ((fd = open(fname, O_RDWR | O_CREAT, 0700)) == -1)
 		exit(1);
@@ -104,6 +109,9 @@  static void generate_events(void)
 
 	if (close(fd) == -1)
 		exit(4);
+
+	if (execve(TEST_APP, argv, environ) != -1)
+		exit(5);
 }
 
 static void child_handler(int tmp)
@@ -131,6 +139,7 @@  static void run_child(void)
 	}
 
 	child_pid = SAFE_FORK();
+
 	if (child_pid == 0) {
 		/* Child will generate events now */
 		close(fd_notify);
@@ -161,38 +170,43 @@  static void check_child(void)
 
 static int setup_mark(unsigned int n)
 {
+	unsigned int i = 0;
 	struct tcase *tc = &tcases[n];
 	struct fanotify_mark_type *mark = &tc->mark;
+	char *const files[] = {fname, TEST_APP};
 
+	tst_res(TINFO, "Test #%d: %s", n, tc->tname);
 	fd_notify = SAFE_FANOTIFY_INIT(FAN_CLASS_CONTENT, O_RDONLY);
 
-	if (fanotify_mark(fd_notify, FAN_MARK_ADD | mark->flag, tc->mask,
-			  AT_FDCWD, fname) < 0) {
-		if (errno == EINVAL && support_perm_events &&
-		    mark->flag == FAN_MARK_FILESYSTEM) {
-			tst_res(TCONF,
-				"FAN_MARK_FILESYSTEM not supported in kernel?");
-			return -1;
-		} else if (errno == EINVAL) {
-			tst_brk(TCONF | TERRNO,
-				"CONFIG_FANOTIFY_ACCESS_PERMISSIONS not "
-				"configured in kernel?");
+	for (; i < ARRAY_SIZE(files); i++) {
+		if (fanotify_mark(fd_notify, FAN_MARK_ADD | mark->flag,
+				  tc->mask, AT_FDCWD, files[i]) < 0) {
+			if (errno == EINVAL && support_perm_events &&
+			    mark->flag == FAN_MARK_FILESYSTEM) {
+				tst_res(TCONF,
+					"FAN_MARK_FILESYSTEM not supported in "
+					"kernel?");
+				return -1;
+			} else if (errno == EINVAL) {
+				tst_brk(TCONF | TERRNO,
+					"CONFIG_FANOTIFY_ACCESS_PERMISSIONS "
+					"not configured in kernel?");
+			} else {
+				tst_brk(TBROK | TERRNO,
+					"fanotify_mark (%d, FAN_MARK_ADD | %s, "
+					"FAN_ACCESS_PERM | FAN_OPEN_PERM, "
+					"AT_FDCWD, %s) failed.",
+					fd_notify, mark->name, fname);
+			}
 		} else {
-			tst_brk(TBROK | TERRNO,
-				"fanotify_mark (%d, FAN_MARK_ADD | %s, "
-				"FAN_ACCESS_PERM | FAN_OPEN_PERM, "
-				"AT_FDCWD, %s) failed.",
-				fd_notify, mark->name, fname);
+			/*
+			 * To distinguish between perm event not supported and
+			 * filesystem mark not supported.
+			 */
+			support_perm_events = 1;
 		}
-	} else {
-		/*
-		 * To distinguish between perm event not supported and
-		 * filesystem mark not supported.
-		 */
-		support_perm_events = 1;
 	}
 
-	tst_res(TINFO, "Test #%d: %s", n, tc->tname);
 	return 0;
 }
 
@@ -294,14 +308,19 @@  static void cleanup(void)
 		SAFE_CLOSE(fd_notify);
 }
 
+static const char *const resource_files[] = {
+	TEST_APP,
+	NULL
+};
+
 static struct tst_test test = {
 	.test = test_fanotify,
 	.tcnt = ARRAY_SIZE(tcases),
 	.setup = setup,
 	.cleanup = cleanup,
-	.needs_tmpdir = 1,
 	.forks_child = 1,
-	.needs_root = 1
+	.needs_root = 1,
+	.resource_files = resource_files
 };
 
 #else
diff --git a/testcases/kernel/syscalls/fanotify/fanotify_child.c b/testcases/kernel/syscalls/fanotify/fanotify_child.c
new file mode 100644
index 000000000..f43068264
--- /dev/null
+++ b/testcases/kernel/syscalls/fanotify/fanotify_child.c
@@ -0,0 +1,14 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2018 Matthew Bobrowski. All Rights Reserved.
+ *
+ * Started by Matthew Bobrowski <mbobrowski@mbobrowski.org>
+ *
+ * DESCRIPTION
+ *    Simple helper program that can be simply invoked from fanotify tests
+ */
+
+int main(void)
+{
+	return 0;
+}