[PATCH v1 1/2] tests: check decoding of successful quotactl

Zhibin Li 08826794brmt at gmail.com
Fri Jun 15 07:57:52 UTC 2018


* tests/quotactl-success.c: New file.
* tests/quotactl-success-v.c: Likewise.
* tests/quotactl-success.test: New test.
* tests/quotactl-success-v.test: Likewise.
* tests/.gitignore: Add quotactl-success and quotaclt-success-v.
* tests/Makefile.am (check_PROGRAMS): Likewise.
(DECODER_TESTS): Add quotactl-success.test and quotactl-success-v.test
* tests/quotactl.h: [INJECT_RETVAL] (check_quota): Use errstr instead of
sprintrc; check that returned value is equivalent to the injected one;
append "(INJECTED)" string to errstr.
* tests/quotactl.c: Use check_quota function for invalid commands.
(invalid_cmd_str, invalid_id_str): New variables.
(print_dqfmt): New function.
---
 tests/.gitignore              |  2 ++
 tests/Makefile.am             |  4 +++
 tests/quotactl-success-v.c    |  2 ++
 tests/quotactl-success-v.test |  6 ++++
 tests/quotactl-success.c      |  2 ++
 tests/quotactl-success.test   |  6 ++++
 tests/quotactl.c              | 76 +++++++++++++++++++++++++++++++++----------
 tests/quotactl.h              | 18 +++++++++-
 8 files changed, 97 insertions(+), 19 deletions(-)
 create mode 100644 tests/quotactl-success-v.c
 create mode 100755 tests/quotactl-success-v.test
 create mode 100644 tests/quotactl-success.c
 create mode 100755 tests/quotactl-success.test

diff --git a/tests/.gitignore b/tests/.gitignore
index 2285a357..af2506fa 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -388,6 +388,8 @@ quotactl
 quotactl-v
 quotactl-xfs
 quotactl-xfs-v
+quotactl-success
+quotactl-success-v
 read-write
 readahead
 readdir
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 34b08a9f..d1f2d8d7 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -152,6 +152,8 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
 	qual_inject-signal \
 	qual_signal \
 	quotactl-v \
+	quotactl-success \
+	quotactl-success-v \
 	quotactl-xfs-v \
 	redirect-fds \
 	restart_syscall \
@@ -273,6 +275,8 @@ DECODER_TESTS = \
 	prctl-tid_address.test \
 	prctl-tsc.test \
 	qual_fault-exit_group.test \
+	quotactl-success.test \
+	quotactl-success-v.test \
 	readv.test \
 	rt_sigaction.test \
 	scm_rights-fd.test \
diff --git a/tests/quotactl-success-v.c b/tests/quotactl-success-v.c
new file mode 100644
index 00000000..4d1c7b30
--- /dev/null
+++ b/tests/quotactl-success-v.c
@@ -0,0 +1,2 @@
+#define INJECT_RETVAL 42
+#include "quotactl-v.c"
diff --git a/tests/quotactl-success-v.test b/tests/quotactl-success-v.test
new file mode 100755
index 00000000..a9937bd3
--- /dev/null
+++ b/tests/quotactl-success-v.test
@@ -0,0 +1,6 @@
+#!/bin/sh -efu
+
+. "${srcdir=.}/scno_tampering.sh"
+
+run_strace -v -e trace=quotactl -e inject=quotactl:retval=42 ../quotactl-success-v > "$EXP"
+match_diff "$LOG" "$EXP"
diff --git a/tests/quotactl-success.c b/tests/quotactl-success.c
new file mode 100644
index 00000000..aa2b9dc0
--- /dev/null
+++ b/tests/quotactl-success.c
@@ -0,0 +1,2 @@
+#define INJECT_RETVAL 42
+#include "quotactl.c"
diff --git a/tests/quotactl-success.test b/tests/quotactl-success.test
new file mode 100755
index 00000000..6cd95321
--- /dev/null
+++ b/tests/quotactl-success.test
@@ -0,0 +1,6 @@
+#!/bin/sh -efu
+
+. "${srcdir=.}/scno_tampering.sh"
+
+run_strace -e trace=quotactl -e inject=quotactl:retval=42 ../quotactl-success > "$EXP"
+match_diff "$LOG" "$EXP"
diff --git a/tests/quotactl.c b/tests/quotactl.c
index f1648d94..67aa936f 100644
--- a/tests/quotactl.c
+++ b/tests/quotactl.c
@@ -161,6 +161,38 @@ print_dqinfo(long rc, void *ptr, void *arg)
 	printf("}");
 }
 
+void
+print_dqfmt(long rc, void *ptr, void *arg)
+{
+	uint32_t *fmtval = ptr;
+	long out_arg = (long) arg;
+	const char *fmtstr;
+
+	if (((rc != 0) && out_arg) || (out_arg > 1)) {
+		printf("%p", fmtval);
+		return;
+	}
+	printf("[");
+	switch (*fmtval) {
+	case 1:
+		fmtstr = "QFMT_VFS_OLD";
+		break;
+	case 2:
+		fmtstr = "QFMT_VFS_V0";
+		break;
+	case 3:
+		fmtstr = "QFMT_OCFS2";
+		break;
+	case 4:
+		fmtstr = "QFMT_VFS_V1";
+		break;
+	default:
+		printf("%#x /* QFMT_VFS_??? */]", *fmtval);
+		return;
+	}
+	printf("%s]", fmtstr);
+}
+
 
 int
 main(void)
@@ -171,7 +203,6 @@ main(void)
 	char bogus_special_str[sizeof(void *) * 2 + sizeof("0x")];
 	char unterminated_str[sizeof(void *) * 2 + sizeof("0x")];
 
-	long rc;
 	char *unterminated = tail_memdup(unterminated_data,
 					 sizeof(unterminated_data));
 	TAIL_ALLOC_OBJECT_CONST_PTR(struct if_dqblk, dqblk);
@@ -179,6 +210,8 @@ main(void)
 	TAIL_ALLOC_OBJECT_CONST_PTR(uint32_t, fmt);
 	TAIL_ALLOC_OBJECT_CONST_PTR(struct if_nextdqblk, nextdqblk);
 
+	static char invalid_cmd_str[1024];
+	static char invalid_id_str[1024];
 
 	snprintf(bogus_special_str, sizeof(bogus_special_str), "%p",
 		bogus_special);
@@ -188,16 +221,16 @@ main(void)
 
 	/* Invalid commands */
 
-	rc = syscall(__NR_quotactl, bogus_cmd, bogus_special, bogus_id,
-		     bogus_addr);
-	printf("quotactl(QCMD(%#x /* Q_??? */, %#x /* ???QUOTA */)"
-	       ", %p, %u, %p) = %s\n",
-	       QCMD_CMD(bogus_cmd), QCMD_TYPE(bogus_cmd),
-	       bogus_special, bogus_id, bogus_addr, sprintrc(rc));
+	snprintf(invalid_cmd_str, sizeof(invalid_cmd_str),
+		 "QCMD(%#x /* Q_??? */, %#x /* ???QUOTA */)",
+		 QCMD_CMD(bogus_cmd), QCMD_TYPE(bogus_cmd));
+	check_quota(CQF_NONE, bogus_cmd, invalid_cmd_str,
+		    bogus_special, bogus_special_str, bogus_id, bogus_addr);
 
-	rc = syscall(__NR_quotactl, 0, NULL, -1, NULL);
-	printf("quotactl(QCMD(0 /* Q_??? */, USRQUOTA), NULL, -1, NULL) = %s\n",
-	       sprintrc(rc));
+	snprintf(invalid_cmd_str, sizeof(invalid_cmd_str),
+		 "QCMD(0 /* Q_??? */, USRQUOTA)");
+	check_quota(CQF_ADDR_STR, 0, invalid_cmd_str,
+		    ARG_STR(NULL), -1, ARG_STR(NULL));
 
 
 	/* Q_QUOTAON */
@@ -207,12 +240,14 @@ main(void)
 		    ARG_STR("/dev/bogus/"), ARG_STR(QFMT_VFS_OLD),
 		    ARG_STR("/tmp/bogus/"));
 
-	rc = syscall(__NR_quotactl, QCMD(Q_QUOTAON, 0xfacefeed), bogus_dev,
-		     bogus_id, bogus_addr);
-	printf("quotactl(QCMD(Q_QUOTAON, %#x /* ???QUOTA */)"
-	       ", %s, %#x /* QFMT_VFS_??? */, %p) = %s\n",
-	       QCMD_TYPE(QCMD(Q_QUOTAON, 0xfacefeed)),
-	       bogus_dev_str, bogus_id, bogus_addr, sprintrc(rc));
+	snprintf(invalid_cmd_str, sizeof(invalid_cmd_str),
+		 "QCMD(Q_QUOTAON, %#x /* ???QUOTA */)",
+		 QCMD_TYPE(QCMD(Q_QUOTAON, 0xfacefeed)));
+	snprintf(invalid_id_str, sizeof(invalid_id_str),
+		 "%#x /* QFMT_VFS_??? */", bogus_id);
+	check_quota(CQF_ID_STR, QCMD(Q_QUOTAON, 0xfacefeed),
+		    invalid_cmd_str, bogus_dev, bogus_dev_str,
+		    bogus_id, invalid_id_str, bogus_addr);
 
 
 	/* Q_QUOTAOFF */
@@ -294,9 +329,14 @@ main(void)
 	check_quota(CQF_ID_SKIP,
 		    ARG_STR(QCMD(Q_GETFMT, USRQUOTA)),
 		    unterminated, unterminated_str, fmt + 1);
-	check_quota(CQF_ID_SKIP,
+	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
+		    ARG_STR(QCMD(Q_GETFMT, GRPQUOTA)),
+		    ARG_STR("/dev/sda1"), fmt, print_dqfmt, (uintptr_t) 1);
+	/* Try to check valid quota format */
+	*fmt = QFMT_VFS_OLD;
+	check_quota(CQF_ID_SKIP | CQF_ADDR_CB,
 		    ARG_STR(QCMD(Q_GETFMT, GRPQUOTA)),
-		    ARG_STR("/dev/sda1"), fmt);
+		    ARG_STR("/dev/sda1"), fmt, print_dqfmt, (uintptr_t) 1);
 
 
 	/* Q_SYNC */
diff --git a/tests/quotactl.h b/tests/quotactl.h
index 352f9270..087747cc 100644
--- a/tests/quotactl.h
+++ b/tests/quotactl.h
@@ -78,6 +78,7 @@ enum check_quotactl_flags {
 	CQF_ADDR_CB   = 1 << CQF_ADDR_CB_BIT,
 };
 
+static const char *errstr;
 
 static inline void
 check_quota(uint32_t flags, int cmd, const char *cmd_str,
@@ -116,6 +117,21 @@ check_quota(uint32_t flags, int cmd, const char *cmd_str,
 	va_end(ap);
 
 	rc = syscall(__NR_quotactl, cmd, special, id, addr);
+
+	errstr = sprintrc(rc);
+
+#ifdef INJECT_RETVAL
+	if (rc != INJECT_RETVAL)
+		error_msg_and_fail("Got a return value of %ld != %d",
+				   rc, INJECT_RETVAL);
+
+	static char inj_errstr[4096];
+
+	snprintf(inj_errstr, sizeof(inj_errstr), "%s (INJECTED)", errstr);
+	errstr = inj_errstr;
+	addr_cb_arg = 0;
+#endif
+
 	printf("quotactl(%s, %s", cmd_str, special_str);
 
 	if (!(flags & CQF_ID_SKIP)) {
@@ -140,7 +156,7 @@ check_quota(uint32_t flags, int cmd, const char *cmd_str,
 		}
 	}
 
-	printf(") = %s\n", sprintrc(rc));
+	printf(") = %s\n", errstr);
 }
 
 
-- 
2.14.4



More information about the Strace-devel mailing list