xref: /illumos-gate/usr/src/cmd/tail/tests/tailtests.sh (revision a02e855f)
1209e49b2SChris Love#!/bin/bash
2209e49b2SChris Love#
3209e49b2SChris Love#
4209e49b2SChris Love# This file and its contents are supplied under the terms of the
5209e49b2SChris Love# Common Development and Distribution License ("CDDL"), version 1.0.
6209e49b2SChris Love# You may only use this file in accordance with the terms of version
7209e49b2SChris Love# 1.0 of the CDDL.
8209e49b2SChris Love#
9209e49b2SChris Love# A full copy of the text of the CDDL should have accompanied this
10209e49b2SChris Love# source.  A copy is of the CDDL is also available via the Internet
11209e49b2SChris Love# at http://www.illumos.org/license/CDDL.
12209e49b2SChris Love#
13209e49b2SChris Love
14209e49b2SChris Love#
15209e49b2SChris Love# Copyright 2010 Chris Love.  All rights reserved.
16*a02e855fSCody Peter Mello# Copyright 2017, Joyent, Inc.
17209e49b2SChris Love#
18209e49b2SChris Love
19*a02e855fSCody Peter MelloBLOCK=""
20*a02e855fSCody Peter Mellofor i in {1..512}; do
21*a02e855fSCody Peter Mello	BLOCK+="."
22*a02e855fSCody Peter Mellodone
23*a02e855fSCody Peter Mello
24*a02e855fSCody Peter Mello
2572102e74SBryan Cantrillchecktest()
2672102e74SBryan Cantrill{
2772102e74SBryan Cantrill	local actual=$1
2872102e74SBryan Cantrill	local output=$2
2972102e74SBryan Cantrill	local test=$3
3072102e74SBryan Cantrill
3172102e74SBryan Cantrill	if [[ "$actual" != "$output" ]]; then
3272102e74SBryan Cantrill		echo "$CMD: test $test: FAIL"
3372102e74SBryan Cantrill		echo -e "$CMD: test $test: expected output:\n$output"
3472102e74SBryan Cantrill		echo -e "$CMD: test $test: actual output:\n$actual"
3572102e74SBryan Cantrill	else
3672102e74SBryan Cantrill		echo "$CMD: test $test: pass"
3772102e74SBryan Cantrill	fi
3872102e74SBryan Cantrill}
39209e49b2SChris Love
40*a02e855fSCody Peter Mellocheckfail()
41*a02e855fSCody Peter Mello{
42*a02e855fSCody Peter Mello	printf "foobar" | $PROG $* &> /dev/null
43*a02e855fSCody Peter Mello
44*a02e855fSCody Peter Mello	if [[ $? -eq 0 ]]; then
45*a02e855fSCody Peter Mello		printf '%s: test "test %s": was supposed to fail\n' "$CMD" "$*"
46*a02e855fSCody Peter Mello	else
47*a02e855fSCody Peter Mello		printf '%s: test "%s": pass\n' "$CMD" "$*"
48*a02e855fSCody Peter Mello	fi
49*a02e855fSCody Peter Mello}
50*a02e855fSCody Peter Mello
51209e49b2SChris Love#
52209e49b2SChris Love# Test cases for 'tail', some based on CoreUtils test cases (validated
5372102e74SBryan Cantrill# with legacy Solaris 'tail' and/or xpg4 'tail').  Note that this is designed
5472102e74SBryan Cantrill# to be able to run on BSD systems as well to check our behavior against
5572102e74SBryan Cantrill# theirs (some behavior that is known to be idiosyncratic to illumos is
5672102e74SBryan Cantrill# skipped on non-illumos systems).
57209e49b2SChris Love#
58209e49b2SChris LovePROG=/usr/bin/tail
5972102e74SBryan CantrillCMD=`basename $0`
6072102e74SBryan CantrillDIR=""
6172102e74SBryan Cantrill
6272102e74SBryan Cantrillwhile [[ $# -gt 0 ]]; do
6372102e74SBryan Cantrill	case $1 in
6472102e74SBryan Cantrill	    -x)
6572102e74SBryan Cantrill		PROG=/usr/xpg4/bin/tail
6672102e74SBryan Cantrill		shift
6772102e74SBryan Cantrill		;;
6872102e74SBryan Cantrill	    -o)
6972102e74SBryan Cantrill		PROG=$2
7072102e74SBryan Cantrill		shift 2
7172102e74SBryan Cantrill		;;
7272102e74SBryan Cantrill	    -d)
7372102e74SBryan Cantrill		DIR=$2
7472102e74SBryan Cantrill		shift 2
7572102e74SBryan Cantrill		;;
7672102e74SBryan Cantrill	    *)
7772102e74SBryan Cantrill		echo "Usage: tailtests.sh" \
7872102e74SBryan Cantrill		    "[-x][-o <override tail executable>]" \
7972102e74SBryan Cantrill		    "[-d <override output directory>]"
8072102e74SBryan Cantrill		exit 1
8172102e74SBryan Cantrill		;;
8272102e74SBryan Cantrill	esac
8372102e74SBryan Cantrilldone
84209e49b2SChris Love
8572102e74SBryan Cantrill#
8672102e74SBryan Cantrill# Shut bash up upon receiving a term so we can drop it on our children
8772102e74SBryan Cantrill# without disrupting the output.
8872102e74SBryan Cantrill#
8972102e74SBryan Cantrilltrap "exit 0" TERM
9072102e74SBryan Cantrill
9172102e74SBryan Cantrillecho "$CMD: program is $PROG"
9272102e74SBryan Cantrill
9372102e74SBryan Cantrillif [[ $DIR != "" ]]; then
9472102e74SBryan Cantrill	echo "$CMD: directory is $DIR"
9572102e74SBryan Cantrillfi
96209e49b2SChris Love
97209e49b2SChris Loveo=`echo -e "bcd"`
98209e49b2SChris Lovea=`echo -e "abcd" | $PROG +2c`
9972102e74SBryan Cantrillchecktest "$a" "$o" 1
100209e49b2SChris Love
101209e49b2SChris Loveo=`echo -e ""`
102209e49b2SChris Lovea=`echo "abcd" | $PROG +8c`
10372102e74SBryan Cantrillchecktest "$a" "$o" 2
104209e49b2SChris Love
105209e49b2SChris Loveo=`echo -e "abcd"`
106209e49b2SChris Lovea=`echo "abcd" | $PROG -9c`
10772102e74SBryan Cantrillchecktest "$a" "$o" 3
108209e49b2SChris Love
109209e49b2SChris Loveo=`echo -e "x"`
110209e49b2SChris Lovea=`echo -e "x" | $PROG -1l`
11172102e74SBryan Cantrillchecktest "$a" "$o" 4
112209e49b2SChris Love
113209e49b2SChris Loveo=`echo -e "\n"`
114209e49b2SChris Lovea=`echo -e "x\ny\n" | $PROG -1l`
11572102e74SBryan Cantrillchecktest "$a" "$o" 5
116209e49b2SChris Love
117209e49b2SChris Loveo=`echo -e "y\n"`
118209e49b2SChris Lovea=`echo -e "x\ny\n" | $PROG -2l`
11972102e74SBryan Cantrillchecktest "$a" "$o" 6
120209e49b2SChris Love
121209e49b2SChris Loveo=`echo -e "y"`
122209e49b2SChris Lovea=`echo -e "x\ny" | $PROG -1l`
12372102e74SBryan Cantrillchecktest "$a" "$o" 7
124209e49b2SChris Love
125209e49b2SChris Loveo=`echo -e "x\ny\n"`
126209e49b2SChris Lovea=`echo -e "x\ny\n" | $PROG +1l`
12772102e74SBryan Cantrillchecktest "$a" "$o" 8
128209e49b2SChris Love
129209e49b2SChris Loveo=`echo -e "y\n"`
130209e49b2SChris Lovea=`echo -e "x\ny\n" | $PROG +2l`
13172102e74SBryan Cantrillchecktest "$a" "$o" 9
132209e49b2SChris Love
133209e49b2SChris Loveo=`echo -e "x"`
134209e49b2SChris Lovea=`echo -e "x" | $PROG -1`
13572102e74SBryan Cantrillchecktest "$a" "$o" 10
136209e49b2SChris Love
137209e49b2SChris Loveo=`echo -e "\n"`
138209e49b2SChris Lovea=`echo -e "x\ny\n" | $PROG -1`
13972102e74SBryan Cantrillchecktest "$a" "$o" 11
140209e49b2SChris Love
141209e49b2SChris Loveo=`echo -e "y\n"`
142209e49b2SChris Lovea=`echo -e "x\ny\n" | $PROG -2`
14372102e74SBryan Cantrillchecktest "$a" "$o" 12
144209e49b2SChris Love
145209e49b2SChris Loveo=`echo -e "y"`
146209e49b2SChris Lovea=`echo -e "x\ny" | $PROG -1`
14772102e74SBryan Cantrillchecktest "$a" "$o" 13
148209e49b2SChris Love
149209e49b2SChris Loveo=`echo -e "x\ny\n"`
150209e49b2SChris Lovea=`echo -e "x\ny\n" | $PROG +1`
15172102e74SBryan Cantrillchecktest "$a" "$o" 14
152209e49b2SChris Love
153209e49b2SChris Loveo=`echo -e "y\n"`
154209e49b2SChris Lovea=`echo -e "x\ny\n" | $PROG +2`
15572102e74SBryan Cantrillchecktest "$a" "$o" 15
156209e49b2SChris Love
157*a02e855fSCody Peter Melloo=`printf "yyz\n"`
158*a02e855fSCody Peter Melloa=`printf "xyyyyyyyyyyz\n" | $PROG +10c`
15972102e74SBryan Cantrillchecktest "$a" "$o" 16
160209e49b2SChris Love
161*a02e855fSCody Peter Melloo=`printf "y\ny\nz\n"`
162*a02e855fSCody Peter Melloa=`printf "x\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\nz\n" | $PROG +10l`
16372102e74SBryan Cantrillchecktest "$a" "$o" 17
164209e49b2SChris Love
165*a02e855fSCody Peter Melloo=`printf "y\ny\ny\ny\ny\ny\ny\ny\ny\nz\n"`
166*a02e855fSCody Peter Melloa=`printf "x\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\nz\n" | $PROG -10l`
16772102e74SBryan Cantrillchecktest "$a" "$o" 18
168209e49b2SChris Love
169*a02e855fSCody Peter Melloa=`printf "o\nn\nm\nl\nk\nj\ni\nh\ng\n"`
170*a02e855fSCody Peter Melloo=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG +10lr`
171*a02e855fSCody Peter Mellochecktest "$a" "$o" 19
172*a02e855fSCody Peter Mello
173*a02e855fSCody Peter Melloa=`printf "o\nn\nm\nl\nk\nj\ni\nh\ng\nf\n"`
174*a02e855fSCody Peter Melloo=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG -10lr`
175*a02e855fSCody Peter Mellochecktest "$a" "$o" 20
176*a02e855fSCody Peter Mello
177*a02e855fSCody Peter Melloa=`printf "o\nn\nm\nl\n"`
178*a02e855fSCody Peter Melloo=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG +10cr`
179*a02e855fSCody Peter Mellochecktest "$a" "$o" 21
180*a02e855fSCody Peter Mello
181*a02e855fSCody Peter Melloa=`printf "o\nn\nm\nl\nk\n"`
182*a02e855fSCody Peter Melloo=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG -10cr`
183*a02e855fSCody Peter Mellochecktest "$a" "$o" 22
184*a02e855fSCody Peter Mello
18572102e74SBryan Cantrill#
18672102e74SBryan Cantrill# For reasons that are presumably as accidental as they are ancient, legacy
18772102e74SBryan Cantrill# (and closed) Solaris tail(1) allows +c, +l and -l to be aliases for +10c,
18872102e74SBryan Cantrill# +10l and -10l, respectively.  If we are on SunOS, verify that this silly
18972102e74SBryan Cantrill# behavior is functional.
19072102e74SBryan Cantrill#
19172102e74SBryan Cantrillif [[ `uname -s` == "SunOS" ]]; then
192*a02e855fSCody Peter Mello	o=`printf "yyz\n"`
193*a02e855fSCody Peter Mello	a=`printf "xyyyyyyyyyyz\n" | $PROG +c`
19472102e74SBryan Cantrill	checktest "$a" "$o" 16a
19572102e74SBryan Cantrill
196*a02e855fSCody Peter Mello	o=`printf "y\ny\nz\n"`
197*a02e855fSCody Peter Mello	a=`printf "x\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\nz\n" | $PROG +l`
19872102e74SBryan Cantrill	checktest "$a" "$o" 17a
19972102e74SBryan Cantrill
200*a02e855fSCody Peter Mello	o=`printf "y\ny\ny\ny\ny\ny\ny\ny\ny\nz\n"`
201*a02e855fSCody Peter Mello	a=`printf "x\ny\ny\ny\ny\ny\ny\ny\ny\ny\ny\nz\n" | $PROG -l`
20272102e74SBryan Cantrill	checktest "$a" "$o" 18a
203*a02e855fSCody Peter Mello
204*a02e855fSCody Peter Mello	a=`printf "o\nn\nm\nl\nk\nj\ni\nh\ng\n"`
205*a02e855fSCody Peter Mello
206*a02e855fSCody Peter Mello	o=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG +lr`
207*a02e855fSCody Peter Mello	checktest "$a" "$o" 19a
208*a02e855fSCody Peter Mello
209*a02e855fSCody Peter Mello	o=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG +l -r`
210*a02e855fSCody Peter Mello	checktest "$a" "$o" 19a
211*a02e855fSCody Peter Mello
212*a02e855fSCody Peter Mello	a=`printf "o\nn\nm\nl\nk\nj\ni\nh\ng\nf\n"`
213*a02e855fSCody Peter Mello
214*a02e855fSCody Peter Mello	o=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG -lr`
215*a02e855fSCody Peter Mello	checktest "$a" "$o" 20a
216*a02e855fSCody Peter Mello
217*a02e855fSCody Peter Mello	o=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG -l -r`
218*a02e855fSCody Peter Mello	checktest "$a" "$o" 20b
219*a02e855fSCody Peter Mello
220*a02e855fSCody Peter Mello	a=`printf "o\nn\nm\nl\n"`
221*a02e855fSCody Peter Mello
222*a02e855fSCody Peter Mello	o=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG +cr`
223*a02e855fSCody Peter Mello	checktest "$a" "$o" 21a
224*a02e855fSCody Peter Mello
225*a02e855fSCody Peter Mello	o=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG +c -r`
226*a02e855fSCody Peter Mello	checktest "$a" "$o" 21a
227*a02e855fSCody Peter Mello
228*a02e855fSCody Peter Mello	a=`printf "o\nn\nm\nl\nk\n"`
229*a02e855fSCody Peter Mello
230*a02e855fSCody Peter Mello	o=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG -cr`
231*a02e855fSCody Peter Mello	checktest "$a" "$o" 22a
232*a02e855fSCody Peter Mello
233*a02e855fSCody Peter Mello	o=`printf "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\nn\no\n" | $PROG -c -r`
234*a02e855fSCody Peter Mello	checktest "$a" "$o" 22b
23572102e74SBryan Cantrillfi
236209e49b2SChris Love
237209e49b2SChris Loveo=`echo -e "c\nb\na"`
238209e49b2SChris Lovea=`echo -e "a\nb\nc" | $PROG -r`
239*a02e855fSCody Peter Mellochecktest "$a" "$o" 23
24072102e74SBryan Cantrill
24172102e74SBryan Cantrill#
24272102e74SBryan Cantrill# Now we want to do a series of follow tests.
24372102e74SBryan Cantrill#
24472102e74SBryan Cantrillif [[ $DIR == "" ]]; then
24572102e74SBryan Cantrill	export TMPDIR=/var/tmp
24672102e74SBryan Cantrill	tdir=$(mktemp -d -t tailtest.XXXXXXXX || exit 1)
24772102e74SBryan Cantrillelse
24872102e74SBryan Cantrill	tdir=$(mktemp -d $DIR/tailtest.XXXXXXXX || exit 1)
24972102e74SBryan Cantrillfi
25072102e74SBryan Cantrill
25172102e74SBryan Cantrillfollow=$tdir/follow
25272102e74SBryan Cantrillmoved=$tdir/follow.moved
25372102e74SBryan Cantrillout=$tdir/out
25472102e74SBryan Cantrill
25572102e74SBryan Cantrill#
25672102e74SBryan Cantrill# First, verify that following works in its most basic sense.
25772102e74SBryan Cantrill#
25872102e74SBryan Cantrillecho -e "a\nb\nc" > $follow
25972102e74SBryan Cantrill$PROG -f $follow > $out 2> /dev/null &
26072102e74SBryan Cantrillchild=$!
26172102e74SBryan Cantrillsleep 2
26272102e74SBryan Cantrillecho -e "d\ne\nf" >> $follow
26372102e74SBryan Cantrillsleep 1
26472102e74SBryan Cantrillkill $child
26572102e74SBryan Cantrillsleep 1
26672102e74SBryan Cantrill
26772102e74SBryan Cantrillo=`echo -e "a\nb\nc\nd\ne\nf\n"`
26872102e74SBryan Cantrilla=`cat $out`
269*a02e855fSCody Peter Mellochecktest "$a" "$o" 24
27072102e74SBryan Cantrillrm $follow
27172102e74SBryan Cantrill
27272102e74SBryan Cantrill#
27372102e74SBryan Cantrill# Now verify that following correctly follows the file being moved.
27472102e74SBryan Cantrill#
27572102e74SBryan Cantrillecho -e "a\nb\nc" > $follow
27672102e74SBryan Cantrill$PROG -f $follow > $out 2> /dev/null &
27772102e74SBryan Cantrillchild=$!
27872102e74SBryan Cantrillsleep 2
27972102e74SBryan Cantrillmv $follow $moved
28072102e74SBryan Cantrill
28172102e74SBryan Cantrillecho -e "d\ne\nf" >> $moved
28272102e74SBryan Cantrillsleep 1
28372102e74SBryan Cantrillkill $child
28472102e74SBryan Cantrillsleep 1
28572102e74SBryan Cantrill
28672102e74SBryan Cantrillo=`echo -e "a\nb\nc\nd\ne\nf\n"`
28772102e74SBryan Cantrilla=`cat $out`
288*a02e855fSCody Peter Mellochecktest "$a" "$o" 25
28972102e74SBryan Cantrillrm $moved
29072102e74SBryan Cantrill
29172102e74SBryan Cantrill#
29272102e74SBryan Cantrill# And now truncation with the new offset being less than the old offset.
29372102e74SBryan Cantrill#
29472102e74SBryan Cantrillecho -e "a\nb\nc" > $follow
29572102e74SBryan Cantrill$PROG -f $follow > $out 2> /dev/null &
29672102e74SBryan Cantrillchild=$!
29772102e74SBryan Cantrillsleep 2
29872102e74SBryan Cantrillecho -e "d\ne\nf" >> $follow
29972102e74SBryan Cantrillsleep 1
30072102e74SBryan Cantrillecho -e "g\nh\ni" > $follow
30172102e74SBryan Cantrillsleep 1
30272102e74SBryan Cantrillkill $child
30372102e74SBryan Cantrillsleep 1
30472102e74SBryan Cantrill
30572102e74SBryan Cantrillo=`echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\n"`
30672102e74SBryan Cantrilla=`cat $out`
307*a02e855fSCody Peter Mellochecktest "$a" "$o" 26
30872102e74SBryan Cantrillrm $follow
30972102e74SBryan Cantrill
31072102e74SBryan Cantrill#
31172102e74SBryan Cantrill# And truncation with the new offset being greater than the old offset.
31272102e74SBryan Cantrill#
31372102e74SBryan Cantrillecho -e "a\nb\nc" > $follow
31472102e74SBryan Cantrillsleep 1
31572102e74SBryan Cantrill$PROG -f $follow > $out 2> /dev/null &
31672102e74SBryan Cantrillchild=$!
31772102e74SBryan Cantrillsleep 2
31872102e74SBryan Cantrillecho -e "d\ne\nf" >> $follow
31972102e74SBryan Cantrillsleep 1
32072102e74SBryan Cantrillecho -e "g\nh\ni\nj\nk\nl\nm\no\np\nq" > $follow
32172102e74SBryan Cantrillsleep 1
32272102e74SBryan Cantrillkill $child
32372102e74SBryan Cantrillsleep 1
32472102e74SBryan Cantrill
32572102e74SBryan Cantrillo=`echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\no\np\nq"`
32672102e74SBryan Cantrilla=`cat $out`
327*a02e855fSCody Peter Mellochecktest "$a" "$o" 27
32872102e74SBryan Cantrillrm $follow
32972102e74SBryan Cantrill
33072102e74SBryan Cantrill#
33172102e74SBryan Cantrill# Verify that we can follow the moved file and correctly see a truncation.
33272102e74SBryan Cantrill#
33372102e74SBryan Cantrillecho -e "a\nb\nc" > $follow
33472102e74SBryan Cantrill$PROG -f $follow > $out 2> /dev/null &
33572102e74SBryan Cantrillchild=$!
33672102e74SBryan Cantrillsleep 2
33772102e74SBryan Cantrillmv $follow $moved
33872102e74SBryan Cantrill
33972102e74SBryan Cantrillecho -e "d\ne\nf" >> $moved
34072102e74SBryan Cantrillsleep 1
34172102e74SBryan Cantrillecho -e "g\nh\ni\nj\nk\nl\nm\no\np\nq" > $moved
34272102e74SBryan Cantrillsleep 1
34372102e74SBryan Cantrillkill $child
34472102e74SBryan Cantrillsleep 1
34572102e74SBryan Cantrill
34672102e74SBryan Cantrillo=`echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\nk\nl\nm\no\np\nq"`
34772102e74SBryan Cantrilla=`cat $out`
348*a02e855fSCody Peter Mellochecktest "$a" "$o" 28
34972102e74SBryan Cantrillrm $moved
35072102e74SBryan Cantrill
35172102e74SBryan Cantrill#
35272102e74SBryan Cantrill# Verify that capital-F follow properly deals with truncation
35372102e74SBryan Cantrill#
35472102e74SBryan Cantrillecho -e "a\nb\nc" > $follow
35572102e74SBryan Cantrill$PROG -F $follow > $out 2> /dev/null &
35672102e74SBryan Cantrillchild=$!
35772102e74SBryan Cantrillsleep 2
35872102e74SBryan Cantrillecho -e "d\ne\nf" >> $follow
35972102e74SBryan Cantrillsleep 1
36072102e74SBryan Cantrillecho -e "g\nh\ni" > $follow
36172102e74SBryan Cantrillsleep 1
36272102e74SBryan Cantrillkill $child
36372102e74SBryan Cantrillsleep 1
36472102e74SBryan Cantrill
36572102e74SBryan Cantrillo=`echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\n"`
36672102e74SBryan Cantrilla=`cat $out`
367*a02e855fSCody Peter Mellochecktest "$a" "$o" 29
36872102e74SBryan Cantrillrm $follow
36972102e74SBryan Cantrill
37072102e74SBryan Cantrill#
37172102e74SBryan Cantrill# Verify that capital-F follow _won't_ follow the moved file and will
37272102e74SBryan Cantrill# correctly deal with recreation of the original file.
37372102e74SBryan Cantrill#
37472102e74SBryan Cantrillecho -e "a\nb\nc" > $follow
37572102e74SBryan Cantrill$PROG -F $follow > $out 2> /dev/null &
37672102e74SBryan Cantrillchild=$!
37772102e74SBryan Cantrillsleep 2
37872102e74SBryan Cantrillmv $follow $moved
37972102e74SBryan Cantrill
38072102e74SBryan Cantrillecho -e "x\ny\nz" >> $moved
38172102e74SBryan Cantrill
38272102e74SBryan Cantrill#
38372102e74SBryan Cantrill# At this point, tail is polling on stat'ing the missing file; we need to
38472102e74SBryan Cantrill# be sure to sleep long enough after recreating it to know that it will pick
38572102e74SBryan Cantrill# it up.
38672102e74SBryan Cantrill#
38772102e74SBryan Cantrillecho -e "d\ne\nf" > $follow
38872102e74SBryan Cantrillsleep 5
38972102e74SBryan Cantrillkill $child
39072102e74SBryan Cantrillsleep 1
39172102e74SBryan Cantrill
39272102e74SBryan Cantrillo=`echo -e "a\nb\nc\nd\ne\nf\n"`
39372102e74SBryan Cantrilla=`cat $out`
394*a02e855fSCody Peter Mellochecktest "$a" "$o" 30
39572102e74SBryan Cantrillrm $moved
39672102e74SBryan Cantrill
39772102e74SBryan Cantrill#
39872102e74SBryan Cantrill# Verify that following two files works.
39972102e74SBryan Cantrill#
40072102e74SBryan Cantrillecho -e "one" > $follow
40172102e74SBryan Cantrillecho -e "two" > $moved
40272102e74SBryan Cantrill$PROG -f $follow $moved > $out 2> /dev/null &
40372102e74SBryan Cantrillchild=$!
40472102e74SBryan Cantrillsleep 2
40572102e74SBryan Cantrillecho -e "three" >> $follow
40672102e74SBryan Cantrillsleep 1
40772102e74SBryan Cantrillecho -e "four" >> $moved
40872102e74SBryan Cantrillsleep 1
40972102e74SBryan Cantrillecho -e "five" >> $follow
41072102e74SBryan Cantrillsleep 1
41172102e74SBryan Cantrillkill $child
41272102e74SBryan Cantrillsleep 1
41372102e74SBryan Cantrill
41472102e74SBryan Cantrill# There is a bug where the content comes before the header lines,
41572102e74SBryan Cantrill# where rlines/mapprint happens before the header.  A pain to fix.
41672102e74SBryan Cantrill# In this test, just make sure we see both files change.
41772102e74SBryan Cantrillo="one
41872102e74SBryan Cantrill
41972102e74SBryan Cantrill==> $follow <==
42072102e74SBryan Cantrilltwo
42172102e74SBryan Cantrill
42272102e74SBryan Cantrill==> $moved <==
42372102e74SBryan Cantrill
42472102e74SBryan Cantrill==> $follow <==
42572102e74SBryan Cantrillthree
42672102e74SBryan Cantrill
42772102e74SBryan Cantrill==> $moved <==
42872102e74SBryan Cantrillfour
42972102e74SBryan Cantrill
43072102e74SBryan Cantrill==> $follow <==
43172102e74SBryan Cantrillfive"
43272102e74SBryan Cantrilla=`cat $out`
433*a02e855fSCody Peter Mellochecktest "$a" "$o" 31
43472102e74SBryan Cantrillrm $follow $moved
43572102e74SBryan Cantrill
43672102e74SBryan Cantrillif [[ `uname -s` == "SunOS" ]]; then
43772102e74SBryan Cantrill	#
43872102e74SBryan Cantrill	# Use DTrace to truncate the file between the return from port_get()
43972102e74SBryan Cantrill	# and the reassociation of the file object with the port, exposing
44072102e74SBryan Cantrill	# any race conditions whereby FILE_TRUNC events are lost.
44172102e74SBryan Cantrill	#
44272102e74SBryan Cantrill	cat /dev/null > $follow
44372102e74SBryan Cantrill	dtrace -c "$PROG -f $follow" -s /dev/stdin > $out <<EOF
44472102e74SBryan Cantrill		#pragma D option destructive
44572102e74SBryan Cantrill		#pragma D option quiet 
44672102e74SBryan Cantrill
44772102e74SBryan Cantrill		pid\$target::port_get:return
44872102e74SBryan Cantrill		/++i == 5/
44972102e74SBryan Cantrill		{
45072102e74SBryan Cantrill			stop();
45172102e74SBryan Cantrill			system("cat /dev/null > $follow");
45272102e74SBryan Cantrill			system("prun %d", pid);
45372102e74SBryan Cantrill		}
45472102e74SBryan Cantrill
45572102e74SBryan Cantrill		tick-1sec
45672102e74SBryan Cantrill		{
45772102e74SBryan Cantrill			system("echo %d >> $follow", j++);
45872102e74SBryan Cantrill		}
45972102e74SBryan Cantrill
46072102e74SBryan Cantrill		tick-1sec
46172102e74SBryan Cantrill		/j == 10/
46272102e74SBryan Cantrill		{
46372102e74SBryan Cantrill			exit(0);
46472102e74SBryan Cantrill		}
46572102e74SBryan CantrillEOF
46672102e74SBryan Cantrill
46772102e74SBryan Cantrill	o=`echo -e "0\n1\n2\n3\n5\n6\n7\n8\n9\n"`
46872102e74SBryan Cantrill	a=`cat $out`
469*a02e855fSCody Peter Mello	checktest "$a" "$o" 31a
47072102e74SBryan Cantrill	rm $follow
47172102e74SBryan Cantrill
47272102e74SBryan Cantrill	cat /dev/null > $follow
47372102e74SBryan Cantrill	dtrace -c "$PROG -f $follow" -s /dev/stdin > $out <<EOF
47472102e74SBryan Cantrill		#pragma D option destructive
47572102e74SBryan Cantrill		#pragma D option quiet 
47672102e74SBryan Cantrill
47772102e74SBryan Cantrill		pid\$target::port_get:return
47872102e74SBryan Cantrill		/++i == 5/
47972102e74SBryan Cantrill		{
48072102e74SBryan Cantrill			stop();
48172102e74SBryan Cantrill			system("mv $follow $moved");
48272102e74SBryan Cantrill			system("cat /dev/null > $moved");
48372102e74SBryan Cantrill			system("prun %d", pid);
48472102e74SBryan Cantrill		}
48572102e74SBryan Cantrill
48672102e74SBryan Cantrill		tick-1sec
48772102e74SBryan Cantrill		{
48872102e74SBryan Cantrill			system("echo %d >> %s", j++,
48972102e74SBryan Cantrill			    i < 5 ? "$follow" : "$moved");
49072102e74SBryan Cantrill		}
49172102e74SBryan Cantrill
49272102e74SBryan Cantrill		tick-1sec
49372102e74SBryan Cantrill		/j == 10/
49472102e74SBryan Cantrill		{
49572102e74SBryan Cantrill			exit(0);
49672102e74SBryan Cantrill		}
49772102e74SBryan CantrillEOF
49872102e74SBryan Cantrill
49972102e74SBryan Cantrill	o=`echo -e "0\n1\n2\n3\n5\n6\n7\n8\n9\n"`
50072102e74SBryan Cantrill	a=`cat $out`
501*a02e855fSCody Peter Mello	checktest "$a" "$o" 31b
50272102e74SBryan Cantrill	rm $moved
50372102e74SBryan Cantrill
50472102e74SBryan Cantrill	#
50572102e74SBryan Cantrill	# Verify that -F will deal properly with the file being truncated
50672102e74SBryan Cantrill	# not by truncation, but rather via an ftruncate() from logadm.
50772102e74SBryan Cantrill	#
50872102e74SBryan Cantrill	cat /dev/null > $follow
50972102e74SBryan Cantrill	( $PROG -F $follow > $out ) &
51072102e74SBryan Cantrill	child=$!
51172102e74SBryan Cantrill	echo -e "a\nb\nc\nd\ne\nf" >> $follow
51272102e74SBryan Cantrill	logadm -c $follow
51372102e74SBryan Cantrill	sleep 2
51472102e74SBryan Cantrill	echo -e "g\nh\ni" >> $follow
51572102e74SBryan Cantrill	sleep 2
51672102e74SBryan Cantrill	kill $child
51772102e74SBryan Cantrill	sleep 1
51872102e74SBryan Cantrill
51972102e74SBryan Cantrill	o=`echo -e "a\nb\nc\nd\ne\nf\ng\nh\ni\n"`
52072102e74SBryan Cantrill	a=`cat $out`
521*a02e855fSCody Peter Mello	checktest "$a" "$o" 31c
52272102e74SBryan Cantrillfi
52372102e74SBryan Cantrill
52472102e74SBryan Cantrill#
52572102e74SBryan Cantrill# We're now going to test that while we may miss output due to truncations
52672102e74SBryan Cantrill# occurring faster than tail can read, we don't ever repeat output.
52772102e74SBryan Cantrill#
52872102e74SBryan Cantrillcat /dev/null > $follow
52972102e74SBryan Cantrill( $PROG -f $follow > $out ) &
53072102e74SBryan Cantrilltchild=$!
53172102e74SBryan Cantrill( let i=0 ; while true; do echo $i > $follow ; sleep 0.1; let i=i+1 ; done ) &
53272102e74SBryan Cantrillchild=$!
53372102e74SBryan Cantrillsleep 10
53472102e74SBryan Cantrillkill $tchild
53572102e74SBryan Cantrillkill $child
53672102e74SBryan Cantrill
53772102e74SBryan Cantrilla=`sort $out | uniq -c | sort -n | tail -1 | awk '{ print $1 }'`
53872102e74SBryan Cantrillo=1
539209e49b2SChris Love
540*a02e855fSCody Peter Mellochecktest "$a" "$o" 32
541*a02e855fSCody Peter Mello
542*a02e855fSCody Peter Mello# Test different ways of specifying character offsets
543*a02e855fSCody Peter Melloo=`printf "d\n"`
544*a02e855fSCody Peter Mello
545*a02e855fSCody Peter Melloa=`printf "hello\nworld\n" | $PROG -c2`
546*a02e855fSCody Peter Mellochecktest "$a" "$o" 33
547*a02e855fSCody Peter Mello
548*a02e855fSCody Peter Melloa=`printf "hello\nworld\n" | $PROG -c-2`
549*a02e855fSCody Peter Mellochecktest "$a" "$o" 34
550*a02e855fSCody Peter Mello
551*a02e855fSCody Peter Melloa=`printf "hello\nworld\n" | $PROG -c 2`
552*a02e855fSCody Peter Mellochecktest "$a" "$o" 35
553*a02e855fSCody Peter Mello
554*a02e855fSCody Peter Melloa=`printf "hello\nworld\n" | $PROG -c -2`
555*a02e855fSCody Peter Mellochecktest "$a" "$o" 36
556*a02e855fSCody Peter Mello
557*a02e855fSCody Peter Melloa=`printf "hello\nworld\n" | $PROG -2c`
558*a02e855fSCody Peter Mellochecktest "$a" "$o" 37
559*a02e855fSCody Peter Mello
560*a02e855fSCody Peter Melloo=`printf "llo\nworld\n"`
561*a02e855fSCody Peter Mello
562*a02e855fSCody Peter Melloa=`printf "hello\nworld\n" | $PROG -c +3`
563*a02e855fSCody Peter Mellochecktest "$a" "$o" 38
564*a02e855fSCody Peter Mello
565*a02e855fSCody Peter Melloa=`printf "hello\nworld\n" | $PROG -c+3`
566*a02e855fSCody Peter Mellochecktest "$a" "$o" 39
567*a02e855fSCody Peter Mello
568*a02e855fSCody Peter Melloa=`printf "hello\nworld\n" | $PROG +3c`
569*a02e855fSCody Peter Mellochecktest "$a" "$o" 40
570*a02e855fSCody Peter Mello
571*a02e855fSCody Peter Mello# Test various ways of specifying block offsets
572*a02e855fSCody Peter Melloo=`printf "$BLOCK"`
573*a02e855fSCody Peter Mello
574*a02e855fSCody Peter Melloa=`printf "${BLOCK//./x}$BLOCK" | $PROG -b1`
575*a02e855fSCody Peter Mellochecktest "$a" "$o" 41
576*a02e855fSCody Peter Mello
577*a02e855fSCody Peter Melloa=`printf "${BLOCK//./x}$BLOCK" | $PROG -b 1`
578*a02e855fSCody Peter Mellochecktest "$a" "$o" 42
579*a02e855fSCody Peter Mello
580*a02e855fSCody Peter Melloa=`printf "${BLOCK//./x}$BLOCK" | $PROG -b -1`
581*a02e855fSCody Peter Mellochecktest "$a" "$o" 43
582*a02e855fSCody Peter Mello
583*a02e855fSCody Peter Melloa=`printf "${BLOCK//./x}$BLOCK" | $PROG -b +2`
584*a02e855fSCody Peter Mellochecktest "$a" "$o" 44
585*a02e855fSCody Peter Mello
586*a02e855fSCody Peter Mello# Test that illegal arguments aren't allowed
587*a02e855fSCody Peter Mello
588*a02e855fSCody Peter Mellocheckfail +b2
589*a02e855fSCody Peter Mellocheckfail +c3
590*a02e855fSCody Peter Mellocheckfail -l3
591*a02e855fSCody Peter Mellocheckfail -cz
592*a02e855fSCody Peter Mellocheckfail -bz
593*a02e855fSCody Peter Mellocheckfail -nz
594*a02e855fSCody Peter Mellocheckfail -3n
595*a02e855fSCody Peter Mellocheckfail +3n
596*a02e855fSCody Peter Mellocheckfail +n3
597*a02e855fSCody Peter Mellocheckfail -lfoobar
598209e49b2SChris Love
59972102e74SBryan Cantrillecho "$CMD: completed"
600209e49b2SChris Love
60172102e74SBryan Cantrillexit $errs
602209e49b2SChris Love
603