[PATCH v5 3/3] Add tests for the -l/--syscall-limit option

Sahil Siddiq icegambit91 at gmail.com
Mon Mar 20 05:40:58 UTC 2023


This test checks the correctness of the -l/--syscall-limit
option when used with the --trace,--trace-path and --status
options.

* tests/strace--syscall-limit.c: New file
* tests/strace--syscall-limit-path.c: New file
* tests/strace--syscall-limit-status.c: New file
* tests/strace--syscall-limit.test: New file
* tests/options-syntax.test: Check invalid -l arguments.
* tests/gen_tests.in: Add rules to generate test.
* tests/Makefile.am
  (check_PROGRAMS): Add strace--syscall-limit variants.
  (MISC_TESTS): Add strace--syscall-limit.test.
* tests/.gitignore: Add strace--syscall-limit variants.

Signed-off-by: Sahil Siddiq <icegambit91 at gmail.com>
---
Changes since v4:
 - tests/strace--syscall-limit-status.c: Test with "getcwd" instead of "openat"
 - tests/options-syntax.test: Check invalid -l arguments

 tests/.gitignore                     |  3 ++
 tests/Makefile.am                    |  4 ++
 tests/gen_tests.in                   |  2 +
 tests/options-syntax.test            |  4 ++
 tests/strace--syscall-limit-path.c   | 69 ++++++++++++++++++++++++++
 tests/strace--syscall-limit-status.c | 73 ++++++++++++++++++++++++++++
 tests/strace--syscall-limit.c        | 60 +++++++++++++++++++++++
 tests/strace--syscall-limit.test     | 34 +++++++++++++
 8 files changed, 249 insertions(+)
 create mode 100644 tests/strace--syscall-limit-path.c
 create mode 100644 tests/strace--syscall-limit-status.c
 create mode 100644 tests/strace--syscall-limit.c
 create mode 100755 tests/strace--syscall-limit.test

diff --git a/tests/.gitignore b/tests/.gitignore
index 5935c39d9..f5c8e4496 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -1020,6 +1020,9 @@ strace--strings-in-hex-all
 strace--strings-in-hex-non-ascii
 strace--strings-in-hex-non-ascii-chars
 strace--strings-in-hex-none
+strace--syscall-limit
+strace--syscall-limit-path
+strace--syscall-limit-status
 strace-Y-0123456789
 strace-n
 strace-no-x
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 9cbfc33f8..7f0aab6ba 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -379,6 +379,9 @@ check_PROGRAMS = $(PURE_EXECUTABLES) \
 	status-successful-threads \
 	status-unfinished-threads \
 	strace--decode-pids-comm \
+	strace--syscall-limit \
+	strace--syscall-limit-path \
+	strace--syscall-limit-status \
 	strace-Y-0123456789 \
 	strace-p-Y-p2 \
 	strace-p1-Y-p \
@@ -606,6 +609,7 @@ MISC_TESTS = \
 	status-none-threads.test \
 	status-successful-threads.test \
 	status-unfinished-threads.test \
+	strace--syscall-limit.test \
 	strace--tips.test \
 	strace--tips-full.test \
 	strace-C.test \
diff --git a/tests/gen_tests.in b/tests/gen_tests.in
index eceac8d60..29a3fb5e5 100644
--- a/tests/gen_tests.in
+++ b/tests/gen_tests.in
@@ -1006,6 +1006,8 @@ strace--strings-in-hex-all		--trace=chdir --strings-in-hex=all --columns=18
 strace--strings-in-hex-non-ascii	--trace=chdir --strings-in-hex=non-ascii --columns=12
 strace--strings-in-hex-non-ascii-chars	--trace=chdir --strings-in-hex=non-ascii-chars --columns=12
 strace--strings-in-hex-none		--trace=chdir --strings-in-hex=none --columns=12
+strace--syscall-limit-path	+strace--syscall-limit.test --trace=all --trace-path=. --trace-path=invalid.dir
+strace--syscall-limit-status	+strace--syscall-limit.test --trace=chdir,getcwd --status=failed
 strace--syscall-times +strace-T_upper.test --syscall-times
 strace--syscall-times-ms +strace-T_upper.test --syscall-times=ms
 strace--syscall-times-ns +strace-T_upper.test --syscall-times=ns
diff --git a/tests/options-syntax.test b/tests/options-syntax.test
index c400fa689..47fe7cd3f 100755
--- a/tests/options-syntax.test
+++ b/tests/options-syntax.test
@@ -186,6 +186,10 @@ check_h "must have PROG [ARGS] or -p PID" --strings-in-hex=non-ascii
 check_h "invalid --strings-in-hex argument: 'al'" --strings-in-hex=al
 check_h "invalid --strings-in-hex argument: 'alll'" --strings-in-hex=alll
 check_h "invalid --strings-in-hex argument: 'ascii'" --strings-in-hex=ascii
+check_h "invalid -l argument: '-5'" -l -5
+check_h "invalid -l argument: '0'" -l0
+check_h "must have PROG [ARGS] or -p PID" -l2
+check_h "must have PROG [ARGS] or -p PID" -l 5
 check_h "must have PROG [ARGS] or -p PID" --quiet=all
 check_h "must have PROG [ARGS] or -p PID" --quiet=none
 check_h "must have PROG [ARGS] or -p PID" --quiet=\!attach
diff --git a/tests/strace--syscall-limit-path.c b/tests/strace--syscall-limit-path.c
new file mode 100644
index 000000000..9ea913ffe
--- /dev/null
+++ b/tests/strace--syscall-limit-path.c
@@ -0,0 +1,69 @@
+/*
+ * Helper function for testing -l/--syscall-limit
+ * with the --trace-path option
+ *
+ * Copyright (c) 2023 The strace developers.
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "tests.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+static int
+syscall_limit__path(void)
+{
+	int pid = getpid();
+	int ret = chdir(".");
+
+	printf("%-5u chdir(\".\") = %d\n", pid, ret);
+	fflush(stdout);
+
+	pid_t child = fork();
+	if (child < 0)
+		perror_msg_and_fail("fork");
+	else if (child == 0) {
+		int pid = getpid();
+		int ret = chdir(".");
+
+		printf("%-5u chdir(\".\") = %d\n", pid, ret);
+		fflush(stdout);
+
+		ret = chdir("invalid.dir");
+
+		printf("%-5u chdir(\"invalid.dir\") = %s\n",
+		       pid, sprintrc(-1));
+		fflush(stdout);
+
+		struct stat statbuf;
+		stat(".", &statbuf);
+
+		return 0;
+	}
+	int status;
+	while ((waitpid(child, &status, 0)) != child) {
+		if (errno == EINTR)
+			continue;
+		perror_msg_and_fail("waitpid: %d", child);
+	}
+
+	chdir(".");
+
+	FILE *f = fopen("dummy.file", "w");
+	fclose(f);
+
+	return WEXITSTATUS(status);
+}
+
+int
+main(void)
+{
+	return syscall_limit__path();
+}
diff --git a/tests/strace--syscall-limit-status.c b/tests/strace--syscall-limit-status.c
new file mode 100644
index 000000000..74278c94b
--- /dev/null
+++ b/tests/strace--syscall-limit-status.c
@@ -0,0 +1,73 @@
+/*
+ * Helper function for testing -l/--syscall-limit
+ * with the --status option
+ *
+ * Copyright (c) 2023 The strace developers.
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "tests.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+static int
+syscall_limit__status(void)
+{
+	int pid = getpid();
+	long ret = chdir("invalid.dir");
+
+	if (ret == -1) {
+		printf("%-5u chdir(\"invalid.dir\") = %s\n", pid, sprintrc(ret));
+		fflush(stdout);
+	}
+
+	pid_t child = fork();
+	if (child < 0)
+		perror_msg_and_fail("fork");
+	else if (child == 0) {
+		int pid = getpid();
+		long ret = chdir("invalid.dir");
+
+		if (ret == -1) {
+			printf("%-5u chdir(\"invalid.dir\") = %s\n", pid, sprintrc(ret));
+			fflush(stdout);
+		}
+
+		char buf[256];
+
+		if (getcwd(buf, 1) == NULL)
+			printf("%-5u getcwd(%p, 1) = %s\n", pid, buf, sprintrc(-1));
+		fflush(stdout);
+
+		struct stat statbuf;
+		stat("invalid.file", &statbuf);
+
+		return 0;
+	}
+	int status;
+	while ((waitpid(child, &status, 0)) != child) {
+		if (errno == EINTR)
+			continue;
+		perror_msg_and_fail("waitpid: %d", child);
+	}
+
+	chdir("invalid.dir");
+
+	FILE *f = fopen("dummy.file", "w");
+	fclose(f);
+
+	return WEXITSTATUS(status);
+}
+
+int
+main(void)
+{
+	return syscall_limit__status();
+}
diff --git a/tests/strace--syscall-limit.c b/tests/strace--syscall-limit.c
new file mode 100644
index 000000000..03eaa894e
--- /dev/null
+++ b/tests/strace--syscall-limit.c
@@ -0,0 +1,60 @@
+/*
+ * Helper function for testing -l/--syscall-limit
+ * with the --trace option
+ *
+ * Copyright (c) 2023 The strace developers.
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "tests.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+static int
+syscall_limit__trace(void)
+{
+	int pid = getpid();
+	int ret = chdir(".");
+
+	printf("%-5u getpid() = %d\n", pid, pid);
+	printf("%-5u chdir(\".\") = %d\n", pid, ret);
+	fflush(stdout);
+
+	pid_t child = fork();
+	if (child < 0)
+		perror_msg_and_fail("fork");
+	else if (child == 0) {
+		int pid = getpid();
+		chdir(".");
+
+		printf("%-5u getpid() = %d\n", pid, pid);
+		fflush(stdout);
+
+		return 0;
+	}
+	int status;
+	while ((waitpid(child, &status, 0)) != child) {
+		if (errno == EINTR)
+			continue;
+		perror_msg_and_fail("waitpid: %d", child);
+	}
+
+	chdir(".");
+
+	FILE *f = fopen("dummy.file", "w");
+	fclose(f);
+
+	return WEXITSTATUS(status);
+}
+
+int
+main(void)
+{
+	return syscall_limit__trace();
+}
diff --git a/tests/strace--syscall-limit.test b/tests/strace--syscall-limit.test
new file mode 100755
index 000000000..75953ae69
--- /dev/null
+++ b/tests/strace--syscall-limit.test
@@ -0,0 +1,34 @@
+#!/bin/sh
+#
+# Check -l/--syscall-limit option.
+#
+# Copyright (c) 2016-2023 The strace developers.
+# All rights reserved.
+#
+# SPDX-License-Identifier: GPL-2.0-or-later
+
+. "${srcdir=.}/init.sh"
+
+prog_args="../$NAME"
+
+set -- --syscall-limit=3 "$@"
+
+args="$*"
+[ -n "$args" -a \( -z "${args##*-e trace=*}" -o \
+			-z "${args##*-etrace=*}" -o \
+			-z "${args##*--trace=*}" \) ] ||
+	set -- --trace="chdir,getpid" "$@"
+
+run_prog > /dev/null
+rm "dummy.file"
+
+args=$prog_args
+run_strace --signal='!SIGCHLD,SIGCONT' --quiet=path-resolution -f -a 9 "$@" $args > "$EXP"
+match_diff "$LOG" "$EXP"
+
+if [ -f "dummy.file" ]
+then
+	rm "dummy.file"
+else
+	fail_ "tracee failed to create dummy file after getting detached"
+fi
-- 
2.39.2



More information about the Strace-devel mailing list