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

Sahil Siddiq icegambit91 at gmail.com
Thu Mar 30 12:28:49 UTC 2023


These tests 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 v5 -> v6:
 - tests/gen_tests.in: Modify rule for "strace--syscall-limit-status"
 - tests/strace--syscall-limit.c: Unify main() and helper() functions.
 - tests/strace--syscall-limit-path.c: Likewise
 - tests/strace--syscall-limit-status.c:
   - Likewise
   - Also, use fchdir instead of getcwd in child process
 - tests/strace--syscall-limit.test: Wait for dummy file creation, and read
   file before comparing logs.

 tests/.gitignore                     |  3 ++
 tests/Makefile.am                    |  4 ++
 tests/gen_tests.in                   |  2 +
 tests/options-syntax.test            |  4 ++
 tests/strace--syscall-limit-path.c   | 65 ++++++++++++++++++++++++++
 tests/strace--syscall-limit-status.c | 68 ++++++++++++++++++++++++++++
 tests/strace--syscall-limit.c        | 56 +++++++++++++++++++++++
 tests/strace--syscall-limit.test     | 57 +++++++++++++++++++++++
 8 files changed, 259 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..4e4b3bd01 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,fchdir --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..f97a40b59
--- /dev/null
+++ b/tests/strace--syscall-limit-path.c
@@ -0,0 +1,65 @@
+/*
+ * 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>
+
+int
+main(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(".");
+
+	status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
+	FILE *f = fopen("dummy.file", "w");
+	fprintf(f, "%d\n", status);
+	fclose(f);
+
+	return status;
+}
diff --git a/tests/strace--syscall-limit-status.c b/tests/strace--syscall-limit-status.c
new file mode 100644
index 000000000..d16604022
--- /dev/null
+++ b/tests/strace--syscall-limit-status.c
@@ -0,0 +1,68 @@
+/*
+ * 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>
+
+int
+main(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);
+		}
+
+		if (fchdir(-1) == -1)
+			printf("%-5u fchdir(-1) = %s\n", pid, 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);
+	}
+
+	if (chdir("invalid.dir") == 0)
+		error_msg_and_fail("chdir: invalid.dir");
+
+	status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
+	FILE *f = fopen("dummy.file", "w");
+	fprintf(f, "%d\n", status);
+	fclose(f);
+
+	return status;
+}
diff --git a/tests/strace--syscall-limit.c b/tests/strace--syscall-limit.c
new file mode 100644
index 000000000..8797f495e
--- /dev/null
+++ b/tests/strace--syscall-limit.c
@@ -0,0 +1,56 @@
+/*
+ * 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>
+
+int
+main(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(".");
+
+	status = WIFEXITED(status) ? WEXITSTATUS(status) : 1;
+	FILE *f = fopen("dummy.file", "w");
+	fprintf(f, "%d\n", status);
+	fclose(f);
+
+	return status;
+}
diff --git a/tests/strace--syscall-limit.test b/tests/strace--syscall-limit.test
new file mode 100755
index 000000000..9da77d344
--- /dev/null
+++ b/tests/strace--syscall-limit.test
@@ -0,0 +1,57 @@
+#!/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
+
+if [ -f "dummy.file" ]
+then
+	rm "dummy.file"
+fi
+
+args=$prog_args
+run_strace --signal='!SIGCHLD,SIGCONT' --quiet=path-resolution -f -a 9 "$@" $args > "$EXP"
+
+# Fail test if file does not exist after 10
+# attempts of checking for existence of file.
+attempt=1
+while [ ! -f "dummy.file" ]; do
+	$SLEEP_A_BIT
+	attempt=$(($attempt + 1))
+	if [ "$attempt" -gt 10 ]
+	then
+		break
+	fi
+done
+
+if [ ! -f "dummy.file" ]
+then
+	fail_ "tracee failed to create dummy file after getting detached"
+else
+	status=$(<dummy.file)
+	if [ $status -eq 0 ]
+	then
+		rm "dummy.file"
+	else
+		fail_ "child process exited with error or was interrupted"
+	fi
+fi
+
+match_diff "$LOG" "$EXP"
-- 
2.40.0



More information about the Strace-devel mailing list