[PATCH v6 3/3] tests: check -l/--syscall-limit options
Dmitry V. Levin
ldv at strace.io
Thu Apr 20 21:33:08 UTC 2023
These tests check the correctness of -l/--syscall-limit options
when used with --trace, --trace-path and --status options.
* tests/options-syntax.test: Check for invalid -l/--syscall-limit
arguments.
* tests/strace--syscall-limit.c: New file.
* tests/strace--syscall-limit-path.c: Likewise.
* tests/strace--syscall-limit-status.c: Likewise.
* tests/strace--syscall-limit.test: New test.
* tests/.gitignore: Add strace--syscall-limit,
strace--syscall-limit-path, and strace--syscall-limit-status.
* tests/Makefile.am (check_PROGRAMS): Likewise.
(MISC_TESTS): Add strace--syscall-limit.test.
* tests/gen_tests.in (strace--syscall-limit-path,
strace--syscall-limit-status): New entries.
Signed-off-by: Sahil Siddiq <icegambit91 at gmail.com>
Co-authored-by: Dmitry V. Levin <ldv at strace.io>
---
I've reworked the test a bit, please have a look.
tests/.gitignore | 3 +
tests/Makefile.am | 4 +
tests/gen_tests.in | 2 +
tests/options-syntax.test | 4 +
tests/strace--syscall-limit-path.c | 11 +++
tests/strace--syscall-limit-status.c | 11 +++
tests/strace--syscall-limit.c | 107 +++++++++++++++++++++++++++
tests/strace--syscall-limit.test | 33 +++++++++
8 files changed, 175 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 70153a5b1..2f1c90795 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..74d9984c9 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=invalid.dir
+strace--syscall-limit-status +strace--syscall-limit.test --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 c76c161d9..f6fcd95f5 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 --syscall-limit argument: '0'" --syscall-limit=0
+check_h "must have PROG [ARGS] or -p PID" -l2
+check_h "must have PROG [ARGS] or -p PID" --syscall-limit 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..8c4c3a7b9
--- /dev/null
+++ b/tests/strace--syscall-limit-path.c
@@ -0,0 +1,11 @@
+/*
+ * Test -l/--syscall-limit with --trace-path option.
+ *
+ * Copyright (c) 2023 The strace developers.
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define PRINT_VALID 0
+#include "strace--syscall-limit.c"
diff --git a/tests/strace--syscall-limit-status.c b/tests/strace--syscall-limit-status.c
new file mode 100644
index 000000000..9cd77a3be
--- /dev/null
+++ b/tests/strace--syscall-limit-status.c
@@ -0,0 +1,11 @@
+/*
+ * Test -l/--syscall-limit with --status option.
+ *
+ * Copyright (c) 2023 The strace developers.
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#define PRINT_VALID 0
+#include "strace--syscall-limit.c"
diff --git a/tests/strace--syscall-limit.c b/tests/strace--syscall-limit.c
new file mode 100644
index 000000000..590ba6623
--- /dev/null
+++ b/tests/strace--syscall-limit.c
@@ -0,0 +1,107 @@
+/*
+ * Test -l/--syscall-limit options.
+ *
+ * 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 <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#ifndef PRINT_VALID
+# define PRINT_VALID 1
+#endif
+
+static int
+write_status(const char *name, int value)
+{
+ FILE *fp = fopen(name, "w");
+ if (!fp)
+ perror_msg_and_fail("fopen: %s", name);
+ if (fprintf(fp, "%d\n", value) <= 0)
+ perror_msg_and_fail("fprintf: %s", name);
+ if (fclose(fp))
+ perror_msg_and_fail("fclose: %s", name);
+ fflush(stdout);
+ return value;
+}
+
+static const char valid_path[] = ".";
+static const char invalid_path[] = "invalid.dir";
+
+static void
+test_chdir(int pid, bool print)
+{
+ if (chdir(valid_path))
+ perror_msg_and_fail("chdir: %s", valid_path);
+
+ if (print) {
+#if PRINT_VALID
+ printf("%-5u chdir(\"%s\") = 0\n", pid, valid_path);
+#else
+ if (chdir(invalid_path) == 0)
+ error_msg_and_fail("chdir: %s", invalid_path);
+ printf("%-5u chdir(\"%s\") = %s\n", pid, invalid_path, sprintrc(-1));
+#endif
+ } else {
+ if (chdir(invalid_path) == 0)
+ error_msg_and_fail("chdir: %s", invalid_path);
+ }
+}
+
+static void
+test_rmdir(int pid, bool print)
+{
+ if (rmdir(invalid_path) == 0)
+ error_msg_and_fail("rmdir: %s", invalid_path);
+ if (print)
+ printf("%-5u rmdir(\"%s\") = %s\n", pid, invalid_path, sprintrc(-1));
+}
+
+int
+main(void)
+{
+ int pid = getpid();
+
+ test_chdir(pid, /* print */ true);
+
+ fflush(stdout);
+ pid_t child = fork();
+ if (child < 0)
+ perror_msg_and_fail("fork");
+
+ if (child == 0) {
+ int pid = getpid();
+
+ test_rmdir(pid, /* print */ true);
+ test_chdir(pid, /* print */ true);
+
+ /* the tracer is expected to detach at this point */
+
+ test_rmdir(pid, /* print */ false);
+
+ exit(0);
+ }
+
+ int status;
+ while ((waitpid(child, &status, 0)) != child) {
+ if (errno == EINTR)
+ continue;
+ perror_msg_and_fail("waitpid: %d", child);
+ }
+
+ /* the tracer is expected to detach at this point */
+
+ test_chdir(pid, /* print */ false);
+
+ return write_status("parent_status",
+ WIFEXITED(status) ? WEXITSTATUS(status) : 1);
+}
diff --git a/tests/strace--syscall-limit.test b/tests/strace--syscall-limit.test
new file mode 100755
index 000000000..410c24a3d
--- /dev/null
+++ b/tests/strace--syscall-limit.test
@@ -0,0 +1,33 @@
+#!/bin/sh -e
+#
+# 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 --trace='chdir,rmdir' "$@"
+
+run_prog > /dev/null
+echo 0 > exp_status
+match_diff exp_status parent_status 'parent_status does not match expectations'
+rm parent_status
+
+run_strace --signal='!SIGCHLD,SIGCONT' --quiet=path-resolution,personality \
+ -f -a9 "$@" $prog_args > "$EXP"
+
+# Do not wait forever, stop waiting
+# if the file won't appear after 10 iterations.
+attempt=1
+while [ "$attempt" -le 10 ] && [ ! -s 'parent_status' ]; do
+ $SLEEP_A_BIT
+ attempt=$((attempt + 1))
+done
+
+match_diff "$LOG" "$EXP"
+match_diff exp_status parent_status 'parent_status does not match expectations'
+rm parent_status exp_status
--
ldv
More information about the Strace-devel
mailing list