From 771b61db30e1bfac73f528274b7116c33fb830b8 Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Sun, 5 Oct 2025 21:33:32 +0100 Subject: [PATCH] Add test for over-read when using stop-at-size (#166). --- Makefile.am | 1 + tests/Transfer_-_--stop-at-size_reads.test | 54 ++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100755 tests/Transfer_-_--stop-at-size_reads.test diff --git a/Makefile.am b/Makefile.am index 4a2ac7b..a5a0c35 100644 --- a/Makefile.am +++ b/Makefile.am @@ -99,6 +99,7 @@ tests/Terminal_-_Detect_width.test \ tests/Transfer_-_--rate-limit.test \ tests/Transfer_-_--remote.test \ tests/Transfer_-_--stop-at-size.test \ +tests/Transfer_-_--stop-at-size_reads.test \ tests/Watchfd_-_Multiple_descriptors.test \ tests/Watchfd_-_Single_descriptor.test diff --git a/tests/Transfer_-_--stop-at-size_reads.test b/tests/Transfer_-_--stop-at-size_reads.test new file mode 100755 index 0000000..82b8625 --- /dev/null +++ b/tests/Transfer_-_--stop-at-size_reads.test @@ -0,0 +1,54 @@ +#!/bin/sh +# +# Make sure -S stops reading at the given size. +# +# See https://codeberg.org/ivarch/pv/issues/166 + +# Allow all tests to be skipped, e.g. during a release build +test "${SKIP_ALL_TESTS}" = "1" && exit 77 + +true "${testSubject:?not set - call this from 'make check'}" +true "${workFile1:?not set - call this from 'make check'}" +true "${workFile2:?not set - call this from 'make check'}" + +printf '%s\n' 'abcdefghi' | ( +"${testSubject}" -qSs5 > "${workFile1}" +cat > "${workFile2}" +) + +# pv should have read 5 bytes and put them into workFile1. +pvOutput="$(cat "${workFile1}")" +if ! test "${pvOutput}" = 'abcde'; then + echo "expected pv to write [abcde], got [${pvOutput}]" + exit 1 +fi + +# cat should have read the remaining bytes and put them into workFile2. +catOutput="$(cat "${workFile2}")" +if ! test "${catOutput}" = 'fghi'; then + echo "expected cat to write [fghi], got [${catOutput}]" + exit 1 +fi + +# Now repeat the test with -X to make sure there's no over-read there either. + +printf '%s\n' 'abcdefghi' | ( +"${testSubject}" -qXSs5 > "${workFile1}" +cat > "${workFile2}" +) + +# pv should have read 5 bytes and put nothing into workFile1. +pvOutput="$(cat "${workFile1}")" +if test -s "${workFile1}"; then + echo "with -X, expected pv to write nothing, got [${pvOutput}]" + exit 1 +fi + +# cat should have read the remaining bytes and put them into workFile2. +catOutput="$(cat "${workFile2}")" +if ! test "${catOutput}" = 'fghi'; then + echo "with pv -X, expected cat to write [fghi], got [${catOutput}]" + exit 1 +fi + +exit 0