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

Sahil Siddiq icegambit91 at gmail.com
Sat Mar 4 13:32:17 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/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 v3:
 - m4/st_warn_cflags.m4: Remove Wno-error=old-style-definition
 - tests/Makefile.am: Add strace--syscall-limit.test in MISC_TESTS
 - tests/gen_tests.in: Change rules for generating strace--syscall-limit tests
 - tests/strace--syscall-limit.test: New file
 - tests/strace--syscall-limit*.c: Improvements and code reorganization

 tests/.gitignore                     |  3 ++
 tests/Makefile.am                    |  4 ++
 tests/gen_tests.in                   |  2 +
 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 +++++++++++++
 7 files changed, 245 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 00d70fa46..4d5445f81 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..698b291fc 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,openat --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/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..6d7b98f77
--- /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);
+		}
+
+		FILE *f = fopen("invalid.file", "r");
+
+		if (f == NULL)
+			printf("%-5u openat(AT_FDCWD, \"invalid.file\", O_RDONLY) = %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);
+	}
+
+	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..b23d82a3f
--- /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