1#!/bin/sh
2#
3# Copyright (c) 2015, 2018 Ingo Schwarze <schwarze@openbsd.org>
4#
5# Permission to use, copy, modify, and distribute this software for any
6# purpose with or without fee is hereby granted, provided that the above
7# copyright notice and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17test_ps()
18{
19	args=$1
20	ps_vars=$2
21	ps_args=$3
22	expected=$4
23	if [ "X$args" = "X${args%=*}" ]; then
24		if [ -n "$args" ]; then
25			./shortsleep "$args" &
26		else
27			./shortsleep &
28		fi
29	else
30		env -i $args ./shortsleep &
31	fi
32	pid=$!
33
34	# Give the forked process some time to set up its process name.
35	until ps -p $pid -o wchan | grep -q nanoslp; do :; done
36
37	result=`env $ps_vars ps -p $pid $ps_args | tail -n +2`
38	kill $pid
39	if [ "$result" != "$expected" ]; then
40		echo "$args & $ps_vars ps $ps_args"
41		echo "expected: >$expected<"
42		echo "result:   >$result<"
43		exit 1;
44	fi
45}
46
47unset LC_ALL
48
49mypid=`printf %5d $$`
50
51# not in the last column, limited width
52test_ps "" "" "-o command,ppid" "./shortsleep     $mypid"
53test_ps "" "" "-c -o command,ppid" "shortsleep       $mypid"
54test_ps "E=1" "" "-eo command,ppid" "E=1 ./shortsleep $mypid"
55test_ps "E=1" "" "-ceo command,ppid" "E=1 shortsleep   $mypid"
56test_ps "long_argument" "" "-o command,ppid" "./shortsleep lon $mypid"
57test_ps "long_argument" "" "-co command,ppid" "shortsleep       $mypid"
58test_ps "E=long" "" "-eo command,ppid" "E=long ./shortsl $mypid"
59test_ps "E=long" "" "-ceo command,ppid" "E=long shortslee $mypid"
60test_ps "E=1 L=very_long_var" "" "-eo command,ppid" "E=1 L=very_long_ $mypid"
61test_ps "E=1 L=very_long_var" "" "-ceo command,ppid" "E=1 L=very_long_ $mypid"
62
63# not in the last column, unlimited width
64test_ps "" "" "-wwo command,ppid" "./shortsleep     $mypid"
65test_ps "" "" "-cwwo command,ppid" "shortsleep       $mypid"
66test_ps "E=1" "" "-ewwo command,ppid" "E=1 ./shortsleep $mypid"
67test_ps "E=1" "" "-cewwo command,ppid" "E=1 shortsleep   $mypid"
68test_ps "long_argument" "" "-wwo command,ppid" "./shortsleep lon $mypid"
69test_ps "long_argument" "" "-cwwo command,ppid" "shortsleep       $mypid"
70test_ps "E=long" "" "-ewwo command,ppid" "E=long ./shortsl $mypid"
71test_ps "E=long" "" "-cewwo command,ppid" "E=long shortslee $mypid"
72test_ps "E=1 L=very_long_var" "" "-ewwo command,ppid" \
73	"E=1 L=very_long_ $mypid"
74test_ps "E=1 L=very_long_var" "" "-cewwo command,ppid" \
75	"E=1 L=very_long_ $mypid"
76
77# UTF-8
78#width 1
79test_ps "���������" \
80	"LC_CTYPE=en_US.UTF-8" "-wwo command,ppid" \
81	"./shortsleep ��������� $mypid"
82# width 0 (combining)
83test_ps "x��" "LC_CTYPE=en_US.UTF-8" "-wwo command,ppid" \
84	"./shortsleep x��   $mypid"
85# width 2 (east asian)
86test_ps "���" "LC_CTYPE=en_US.UTF-8" "-wwo command,ppid" \
87	"./shortsleep ���  $mypid"
88# non-printable
89test_ps "��" "LC_CTYPE=en_US.UTF-8" "-wwo command,ppid" \
90	"./shortsleep ���   $mypid"
91
92# UTF-8 in the C locale
93test_ps "E=��" "LC_CTYPE=C" "-ewwo command,ppid" \
94	"E=\M-C\M-1 ./sho $mypid"
95test_ps "E=���" "LC_CTYPE=C" "-ewwo command,ppid" \
96	"E=\M-o\M->\M^] . $mypid"
97test_ps "E=x��" "LC_CTYPE=C" "-ewwo command,ppid" \
98	"E=x\M-L\M^@ ./sh $mypid"
99test_ps "E=���" "LC_CTYPE=C" "-ewwo command,ppid" \
100	"E=\M-l\M-?\M-? . $mypid"
101
102# invalid 8-bit bytes
103test_ps "E=x�x" "" "-ewwo command,ppid" "E=x\M^?x ./short $mypid"
104test_ps "E=x�x" "" "-ewwo command,ppid" "E=x\M-1x ./short $mypid"
105test_ps "E=x�x" "" "-ewwo command,ppid" "E=x\M-Cx ./short $mypid"
106test_ps "E=x��x" "" "-ewwo command,ppid" "E=x\M-o\M->x ./s $mypid"
107
108# in the last column, limited width
109test_ps "" "" "-o command" "./shortsleep"
110test_ps "" "" "-co command" "shortsleep"
111test_ps "" "COLUMNS=4" "-o command" "./sh"
112test_ps "" "COLUMNS=4" "-co command" "shor"
113test_ps "" "COLUMNS=10" "-o ppid,command" "$mypid ./sh"
114test_ps "" "COLUMNS=10" "-co ppid,command" "$mypid shor"
115test_ps "" "COLUMNS=4" "-o ppid,command" "$mypid ./shortsleep"
116test_ps "" "COLUMNS=4" "-co ppid,command" "$mypid shortsleep"
117test_ps "long_arg" "COLUMNS=4" "-o ppid,command" "$mypid ./shortsleep lon"
118test_ps "long_arg" "COLUMNS=4" "-co ppid,command" "$mypid shortsleep"
119test_ps "E=1" "" "-eo command" "E=1 ./shortsleep"
120test_ps "E=1" "" "-ceo command" "E=1 shortsleep"
121test_ps "E=1" "COLUMNS=7" "-eo command" "E=1 ./s"
122test_ps "E=1" "COLUMNS=6" "-eo command" "E=1 ./"
123test_ps "E=1" "COLUMNS=5" "-eo command" "E=1 ."
124test_ps "E=1" "COLUMNS=4" "-eo command" "E=1 "
125test_ps "E=1" "COLUMNS=3" "-eo command" "E=1"
126test_ps "E=1" "COLUMNS=2" "-eo command" "E="
127test_ps "E=1" "COLUMNS=5" "-ceo command" "E=1 s"
128test_ps "E=1" "COLUMNS=4" "-ceo command" "E=1 "
129test_ps "E=1" "COLUMNS=3" "-ceo command" "E=1"
130test_ps "E=1" "COLUMNS=2" "-ceo command" "E="
131
132# in the last column, unlimited width
133test_ps "" "" "-wwo command" "./shortsleep"
134test_ps "" "" "-cwwo command" "shortsleep"
135test_ps "long_argument" "" "-wwo command" "./shortsleep long_argument"
136test_ps "long_argument" "" "-cwwo command" "shortsleep"
137test_ps "E=1" "" "-ewwo command" "E=1 ./shortsleep"
138test_ps "E=1" "" "-cewwo command" "E=1 shortsleep"
139test_ps "E=1 L=very_long_var" "" "-ewwo command" \
140	"E=1 L=very_long_var ./shortsleep"
141test_ps "E=1 L=very_long_var" "" "-cewwo command" \
142	"E=1 L=very_long_var shortsleep"
143
144# test vis(3)ing
145test_ps "xx" "" "-o command" "./shortsleep xx"
146test_ps "" "" "-o command" "./shortsleep \\^A\\^B\\^C"
147test_ps "" "" "-o command" "./shortsleep \\^D\\^E\\^F\\a"
148test_ps "	x
149" "" "-o command" "./shortsleep \\b\\tx\\n\\v"
150test_ps "
151" "" "-o command" "./shortsleep \\f\\r\\^N\\^O"
152test_ps "" "" "-o command" "./shortsleep \\^P\\^Q\\^R\\^S"
153test_ps "" "" "-o command" "./shortsleep \\^T\\^U\\^V\\^W"
154test_ps "" "" "-o command" "./shortsleep \\^X\\^Y\\^Z\\^["
155test_ps "" "" "-o command" "./shortsleep \\^\\\\^]\\^^\\^_"
156test_ps "x x" "" "-o command" "./shortsleep x x"
157test_ps "" "" "-o command" "./shortsleep \\^?"
158
159exit 0
160