Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 20 May 2018 09:10:24 GMT
From:      aniketp@FreeBSD.org
To:        svn-soc-all@FreeBSD.org
Subject:   socsvn commit: r337241 - soc2018/aniketp/head/tests/sys/audit
Message-ID:  <201805200910.w4K9AOfM066447@socsvn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: aniketp
Date: Sun May 20 09:10:23 2018
New Revision: 337241
URL: http://svnweb.FreeBSD.org/socsvn/?view=rev&rev=337241

Log:
  Add tests for chmod(2) family

Modified:
  soc2018/aniketp/head/tests/sys/audit/file-attribute-modify.c

Modified: soc2018/aniketp/head/tests/sys/audit/file-attribute-modify.c
==============================================================================
--- soc2018/aniketp/head/tests/sys/audit/file-attribute-modify.c	Sat May 19 19:24:05 2018	(r337240)
+++ soc2018/aniketp/head/tests/sys/audit/file-attribute-modify.c	Sun May 20 09:10:23 2018	(r337241)
@@ -26,6 +26,7 @@
  * $FreeBSD$
  */
 
+#include <sys/stat.h>
 #include <sys/syscall.h>
 
 #include <atf-c.h>
@@ -36,8 +37,191 @@
 
 static struct pollfd fds[1];
 static mode_t mode = 0777;
+static struct stat statbuff;
 static const char *path = "fileforaudit";
 static const char *errpath = "dirdoesnotexist/fileforaudit";
+static const char *successreg = "fileforaudit.*return,success";
+static const char *failurereg = "fileforaudit.*return,failure";
+
+
+ATF_TC_WITH_CLEANUP(chmod_success);
+ATF_TC_HEAD(chmod_success, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
+					"chmod(2) call");
+}
+
+ATF_TC_BODY(chmod_success, tc)
+{
+	/* File needs to exist to call chmod(2) */
+	ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
+	FILE *pipefd = setup(fds, "fm");
+	ATF_REQUIRE_EQ(0, chmod(path, mode));
+	check_audit(fds, successreg, pipefd);
+}
+
+ATF_TC_CLEANUP(chmod_success, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(chmod_failure);
+ATF_TC_HEAD(chmod_failure, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"chmod(2) call");
+}
+
+ATF_TC_BODY(chmod_failure, tc)
+{
+	FILE *pipefd = setup(fds, "fm");
+	/* Failure reason: file does not exist */
+	ATF_REQUIRE_EQ(-1, chmod(errpath, mode));
+	check_audit(fds, failurereg, pipefd);
+}
+
+ATF_TC_CLEANUP(chmod_failure, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(fchmod_success);
+ATF_TC_HEAD(fchmod_success, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
+					"fchmod(2) call");
+}
+
+ATF_TC_BODY(fchmod_success, tc)
+{
+	int filedesc;
+	char regex[30];
+
+	/* File needs to exist to call fchmod(2) */
+	ATF_REQUIRE((filedesc = open(path, O_CREAT, mode)) != -1);
+	ATF_REQUIRE_EQ(0, fstat(filedesc, &statbuff));
+	/* Prepare the regex to be checked in the audit record */
+	snprintf(regex, 30, "fchmod.*%lu.*return,success", statbuff.st_ino);
+
+	FILE *pipefd = setup(fds, "fm");
+	ATF_REQUIRE_EQ(0, fchmod(filedesc, mode));
+	check_audit(fds, regex, pipefd);
+}
+
+ATF_TC_CLEANUP(fchmod_success, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(fchmod_failure);
+ATF_TC_HEAD(fchmod_failure, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"fchmod(2) call");
+}
+
+ATF_TC_BODY(fchmod_failure, tc)
+{
+	const char *regex = "fchmod.*return,failure : Bad file descriptor";
+	FILE *pipefd = setup(fds, "fm");
+	/* Failure reason: Invalid file descriptor */
+	ATF_REQUIRE_EQ(-1, fchmod(-1, mode));
+	check_audit(fds, regex, pipefd);
+}
+
+ATF_TC_CLEANUP(fchmod_failure, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(lchmod_success);
+ATF_TC_HEAD(lchmod_success, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
+					"lchmod(2) call");
+}
+
+ATF_TC_BODY(lchmod_success, tc)
+{
+	/* Symbolic link needs to exist to call lchmod(2) */
+	ATF_REQUIRE_EQ(0, symlink("symlink", path));
+	FILE *pipefd = setup(fds, "fm");
+	ATF_REQUIRE_EQ(0, lchmod(path, mode));
+	check_audit(fds, successreg, pipefd);
+}
+
+ATF_TC_CLEANUP(lchmod_success, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(lchmod_failure);
+ATF_TC_HEAD(lchmod_failure, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"lchmod(2) call");
+}
+
+ATF_TC_BODY(lchmod_failure, tc)
+{
+	FILE *pipefd = setup(fds, "fm");
+	/* Failure reason: file does not exist */
+	ATF_REQUIRE_EQ(-1, lchmod(errpath, mode));
+	check_audit(fds, failurereg, pipefd);
+}
+
+ATF_TC_CLEANUP(lchmod_failure, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(fchmodat_success);
+ATF_TC_HEAD(fchmodat_success, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of a successful "
+					"fchmodat(2) call");
+}
+
+ATF_TC_BODY(fchmodat_success, tc)
+{
+	/* File needs to exist to call fchmodat(2) */
+	ATF_REQUIRE(open(path, O_CREAT, mode) != -1);
+	FILE *pipefd = setup(fds, "fm");
+	ATF_REQUIRE_EQ(0, fchmodat(AT_FDCWD, path, mode, 0));
+	check_audit(fds, successreg, pipefd);
+}
+
+ATF_TC_CLEANUP(fchmodat_success, tc)
+{
+	cleanup();
+}
+
+
+ATF_TC_WITH_CLEANUP(fchmodat_failure);
+ATF_TC_HEAD(fchmodat_failure, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Tests the audit of an unsuccessful "
+					"fchmodat(2) call");
+}
+
+ATF_TC_BODY(fchmodat_failure, tc)
+{
+	FILE *pipefd = setup(fds, "fm");
+	/* Failure reason: file does not exist */
+	ATF_REQUIRE_EQ(-1, fchmodat(AT_FDCWD, errpath, mode, 0));
+	check_audit(fds, failurereg, pipefd);
+}
+
+ATF_TC_CLEANUP(fchmodat_failure, tc)
+{
+	cleanup();
+}
 
 
 ATF_TC_WITH_CLEANUP(open_read_creat_success);
@@ -810,6 +994,15 @@
 
 ATF_TP_ADD_TCS(tp)
 {
+	ATF_TP_ADD_TC(tp, chmod_success);
+	ATF_TP_ADD_TC(tp, chmod_failure);
+	ATF_TP_ADD_TC(tp, fchmod_success);
+	ATF_TP_ADD_TC(tp, fchmod_failure);
+	ATF_TP_ADD_TC(tp, lchmod_success);
+	ATF_TP_ADD_TC(tp, lchmod_failure);
+	ATF_TP_ADD_TC(tp, fchmodat_success);
+	ATF_TP_ADD_TC(tp, fchmodat_failure);
+
 	ATF_TP_ADD_TC(tp, open_read_creat_success);
 	ATF_TP_ADD_TC(tp, open_read_creat_failure);
 	ATF_TP_ADD_TC(tp, openat_read_creat_success);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201805200910.w4K9AOfM066447>