command.sh revision 1.1
1#!/bin/sh
2#
3# Copyright (c) 2015 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		./shortsleep $args &
25	else
26		env -i $args ./shortsleep &
27	fi
28	pid=$!
29
30	# Give the forked process some time to set up its process name.
31	ps > /dev/null
32
33	result=`env $ps_vars ps -p $pid $ps_args | tail -n +2`
34	kill $pid
35	if [ "$result" != "$expected" ]; then
36		echo "$args & $ps_vars ps $ps_args"
37		echo "expected: >$expected<"
38		echo "result:   >$result<"
39		exit 1;
40	fi
41}
42
43mypid=`printf %5d $$`
44
45# not in the last column, limited width
46test_ps "" "" "-o command,ppid" "./shortsleep     $mypid"
47test_ps "" "" "-c -o command,ppid" "shortsleep       $mypid"
48test_ps "E=1" "" "-eo command,ppid" "E=1 (shortsleep) $mypid"
49test_ps "E=1" "" "-ceo command,ppid" "E=1 shortsleep   $mypid"
50test_ps "long_argument" "" "-o command,ppid" "./shortsleep lon $mypid"
51test_ps "long_argument" "" "-co command,ppid" "shortsleep       $mypid"
52test_ps "E=long" "" "-eo command,ppid" "E=long (shortsle $mypid"
53test_ps "E=long" "" "-ceo command,ppid" "E=long shortslee $mypid"
54test_ps "E=1 L=very_long_var" "" "-eo command,ppid" "E=1 L=very_long_ $mypid"
55test_ps "E=1 L=very_long_var" "" "-ceo command,ppid" "E=1 L=very_long_ $mypid"
56
57# not in the last column, unlimited width
58test_ps "" "" "-wwo command,ppid" "./shortsleep     $mypid"
59test_ps "" "" "-cwwo command,ppid" "shortsleep       $mypid"
60test_ps "E=1" "" "-ewwo command,ppid" "E=1 (shortsleep) $mypid"
61test_ps "E=1" "" "-cewwo command,ppid" "E=1 shortsleep   $mypid"
62test_ps "long_argument" "" "-wwo command,ppid" "./shortsleep lon $mypid"
63test_ps "long_argument" "" "-cwwo command,ppid" "shortsleep       $mypid"
64test_ps "E=long" "" "-ewwo command,ppid" "E=long (shortsle $mypid"
65test_ps "E=long" "" "-cewwo command,ppid" "E=long shortslee $mypid"
66test_ps "E=1 L=very_long_var" "" "-ewwo command,ppid" \
67	"E=1 L=very_long_ $mypid"
68test_ps "E=1 L=very_long_var" "" "-cewwo command,ppid" \
69	"E=1 L=very_long_ $mypid"
70
71# in the last column, limited width
72test_ps "" "" "-o command" "./shortsleep"
73test_ps "" "" "-co command" "shortsleep"
74test_ps "" "COLUMNS=4" "-o command" "./sh"
75test_ps "" "COLUMNS=4" "-co command" "shor"
76test_ps "" "COLUMNS=10" "-o ppid,command" "$mypid ./sh"
77test_ps "" "COLUMNS=10" "-co ppid,command" "$mypid shor"
78test_ps "" "COLUMNS=4" "-o ppid,command" "$mypid ./shortsleep"
79test_ps "" "COLUMNS=4" "-co ppid,command" "$mypid shortsleep"
80test_ps "long_arg" "COLUMNS=4" "-o ppid,command" "$mypid ./shortsleep lon"
81test_ps "long_arg" "COLUMNS=4" "-co ppid,command" "$mypid shortsleep"
82test_ps "E=1" "" "-eo command" "E=1 (shortsleep)"
83test_ps "E=1" "" "-ceo command" "E=1 shortsleep"
84test_ps "E=1" "COLUMNS=6" "-eo command" "E=1 (s"
85test_ps "E=1" "COLUMNS=5" "-eo command" "E=1 ("
86test_ps "E=1" "COLUMNS=4" "-eo command" "E=1 "
87test_ps "E=1" "COLUMNS=3" "-eo command" "E=1"
88test_ps "E=1" "COLUMNS=2" "-eo command" "E="
89test_ps "E=1" "COLUMNS=5" "-ceo command" "E=1 s"
90test_ps "E=1" "COLUMNS=4" "-ceo command" "E=1 "
91test_ps "E=1" "COLUMNS=3" "-ceo command" "E=1"
92test_ps "E=1" "COLUMNS=2" "-ceo command" "E="
93
94# in the last column, unlimited width
95test_ps "" "" "-wwo command" "./shortsleep"
96test_ps "" "" "-cwwo command" "shortsleep"
97test_ps "long_argument" "" "-wwo command" "./shortsleep long_argument"
98test_ps "long_argument" "" "-cwwo command" "shortsleep"
99test_ps "E=1" "" "-ewwo command" "E=1 (shortsleep)"
100test_ps "E=1" "" "-cewwo command" "E=1 shortsleep"
101test_ps "E=1 L=very_long_var" "" "-ewwo command" \
102	"E=1 L=very_long_var (shortsleep)"
103test_ps "E=1 L=very_long_var" "" "-cewwo command" \
104	"E=1 L=very_long_var shortsleep"
105
106exit 0
107