From fa745f2cfe61c708fe53d82afcbdded767dcb651 Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Sat, 30 Nov 2024 22:55:00 +0000 Subject: [PATCH] Test "--watchfd PID". --- Makefile.am | 1 + tests/Watchfd_-_Multiple_descriptors.test | 73 +++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100755 tests/Watchfd_-_Multiple_descriptors.test diff --git a/Makefile.am b/Makefile.am index a3a3584..3672857 100644 --- a/Makefile.am +++ b/Makefile.am @@ -83,6 +83,7 @@ tests/Terminal_-_Detect_width.test \ tests/Transfer_-_--rate-limit.test \ tests/Transfer_-_--remote.test \ tests/Transfer_-_--stop-at-size.test \ +tests/Watchfd_-_Multiple_descriptors.test \ tests/Watchfd_-_Single_descriptor.test EXTRA_DIST += $(TESTS) tests/run-valgrind.sh tests/test-env.sh diff --git a/tests/Watchfd_-_Multiple_descriptors.test b/tests/Watchfd_-_Multiple_descriptors.test new file mode 100755 index 0000000..5894cf0 --- /dev/null +++ b/tests/Watchfd_-_Multiple_descriptors.test @@ -0,0 +1,73 @@ +#!/bin/sh +# +# Check that watching a process's file descriptors works as expected. + +# Dummy assignments for "shellcheck". +testSubject="${testSubject:-false}"; workFile1="${workFile1:-.tmp1}"; workFile2="${workFile2:-.tmp2}"; workFile3="${workFile3:-.tmp3}" + +# Skip the test if "-d" is not available. +if ! "${testSubject}" -h | grep -Fq ' -d'; then + echo "no \`--watchfd' / \`-d' option on this platform" + exit 77 +fi + +seq 1 100 > "${workFile1}" +seq 1 300 > "${workFile2}" + +# Run a background process to perform a few reads from a file, opening a +# second file after a moment. +# NB we just export line to make shellcheck think it's used. +( +export line +sleep 1 +read -r line +exec 9<"${workFile2}" +sleep 1 +read -r line +sleep 1 +) <"${workFile1}" & +pid=$! + +sleep 0.1 +"${testSubject}" -d "${pid}" -f -i 0.5 >/dev/null 2>"${workFile3}" + +# Skip the test if "-d" does not work. +if grep -Fq ' -d: not available' "${workFile3}"; then + echo "no \`--watchfd' / \`-d' option on this platform" + exit 77 +fi + +# Process the output to make it easier to handle. +# NB "ESC [ A" is "cursor up", we turn that into "-" and a newline. +tr '\r' '\n' < "${workFile3}" | sed 's/.\[A/-!/g' | tr '!' '\n' > "${workFile1}" + +# We should see at least 2 file positions for fd 0. +positionsReported="$(awk '/^ *0:/ {print $2}' < "${workFile1}" | sort -n | uniq)" +differentNumbers="$(printf "%s\n" "${positionsReported}" | grep -Ec .)" +if ! test "${differentNumbers}" -gt 1; then + printf "%s\n" "Expected at least 2 different numbers to be output for fd 0" + printf "%s:\n%s\n" "Positions reported" "${positionsReported}" + printf "%s: %s\n" "Number of different values seen" "${differentNumbers}" + printf "%s\n" "Raw output:" + cat "${workFile1}" + exit 1 +fi + +# We should see file descriptor 9 appear. +if ! grep -Eq '^ *9:' < "${workFile1}"; then + printf "%s\n" "Expected to see information for fd 9" + printf "%s\n" "Raw output:" + cat "${workFile1}" + exit 1 +fi + +# We should see the last 6 characters of fd 9's filename appear. +lastChars="$(printf "%s\n" "${workFile2}" | rev | cut -b1-6 | rev)" +if ! grep -Fq "${lastChars}:" < "${workFile1}"; then + printf "%s\n" "Expected to see a filename for fd 9" + printf "%s\n" "Raw output:" + cat "${workFile1}" + exit 1 +fi + +exit 0