1#
2# Copyright (c) 2017 Enji Cooper <ngie@FreeBSD.org>
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7# 1. Redistributions of source code must retain the above copyright
8#    notice, this list of conditions and the following disclaimer.
9# 2. Redistributions in binary form must reproduce the above copyright
10#    notice, this list of conditions and the following disclaimer in the
11#    documentation and/or other materials provided with the distribution.
12#
13# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
17# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23# POSSIBILITY OF SUCH DAMAGE.
24#
25# $FreeBSD$
26#
27
28MAX_TRIES=20
29PROG_PID=
30PROG_PATH=$(atf_get_srcdir)/while1
31
32SP='[[:space:]]'
33
34start_program()
35{
36	echo "Starting program in background"
37	PROG_COMM=while1
38	PROG_PATH=$(atf_get_srcdir)/$PROG_COMM
39
40	$PROG_PATH $* &
41	PROG_PID=$!
42	try=0
43	while [ $try -lt $MAX_TRIES ] && ! kill -0 $PROG_PID; do
44		sleep 0.5
45		: $(( try += 1 ))
46	done
47	if [ $try -ge $MAX_TRIES ]; then
48		atf_fail "Polled for program start $MAX_TRIES tries and failed"
49	fi
50}
51
52atf_test_case binary_info
53binary_info_head()
54{
55	atf_set "descr" "Checks -b support"
56}
57binary_info_body()
58{
59	start_program bogus-arg
60
61	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP*"
62	header_re=$(printf "$line_format" "PID" "COMM" "OSREL" "PATH")
63	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM "[[:digit:]]+" "$PROG_PATH")
64
65	atf_check -o save:procstat.out procstat binary $PROG_PID
66	atf_check -o match:"$header_re" head -n 1 procstat.out
67	atf_check -o match:"$line_re" tail -n 1 procstat.out
68
69	atf_check -o save:procstat.out procstat -b $PROG_PID
70	atf_check -o match:"$header_re" head -n 1 procstat.out
71	atf_check -o match:"$line_re" tail -n 1 procstat.out
72}
73
74atf_test_case command_line_arguments
75command_line_arguments_head()
76{
77	atf_set "descr" "Checks -c support"
78}
79command_line_arguments_body()
80{
81	atf_skip "https://bugs.freebsd.org/233587"
82
83	arguments="my arguments"
84
85	start_program $arguments
86
87	line_format="$SP*%s$SP+%s$SP+%s$SP*"
88	header_re=$(printf "$line_format" "PID" "COMM" "ARGS")
89	line_re=$(printf "$line_format" $PROG_PID "$PROG_COMM" "$PROG_PATH $arguments")
90
91	atf_check -o save:procstat.out procstat arguments $PROG_PID
92	atf_check -o match:"$header_re" head -n 1 procstat.out
93	atf_check -o match:"$line_re" tail -n 1 procstat.out
94
95	atf_check -o save:procstat.out procstat -c $PROG_PID
96	atf_check -o match:"$header_re" head -n 1 procstat.out
97	atf_check -o match:"$line_re" tail -n 1 procstat.out
98}
99
100atf_test_case environment
101environment_head()
102{
103	atf_set "descr" "Checks -e support"
104}
105environment_body()
106{
107	atf_skip "https://bugs.freebsd.org/233588"
108
109	var="MY_VARIABLE=foo"
110	eval "export $var"
111
112	start_program my arguments
113
114	line_format="$SP*%s$SP+%s$SP+%s$SP*"
115	header_re=$(printf "$line_format" "PID" "COMM" "ENVIRONMENT")
116	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".*$var.*")
117
118	atf_check -o save:procstat.out procstat environment $PROG_PID
119	atf_check -o match:"$header_re" head -n 1 procstat.out
120	atf_check -o match:"$line_re" tail -n 1 procstat.out
121
122	atf_check -o save:procstat.out procstat -e $PROG_PID
123	atf_check -o match:"$header_re" head -n 1 procstat.out
124	atf_check -o match:"$line_re" tail -n 1 procstat.out
125}
126
127atf_test_case file_descriptor
128file_descriptor_head()
129{
130	atf_set "descr" "Checks -f support"
131}
132file_descriptor_body()
133{
134	start_program my arguments
135
136	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP%s$SP*"
137	header_re=$(printf "$line_format" "PID" "COMM" "FD" "T" "V" "FLAGS" "REF" "OFFSET" "PRO" "NAME")
138	# XXX: write a more sensible feature test
139	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".+" ".+" ".+" ".+" ".+" ".+" ".+" ".+")
140
141	atf_check -o save:procstat.out procstat files $PROG_PID
142	atf_check -o match:"$header_re" head -n 1 procstat.out
143	atf_check -o match:"$line_re" awk 'NR > 1' procstat.out
144
145	atf_check -o save:procstat.out procstat -f $PROG_PID
146	atf_check -o match:"$header_re" head -n 1 procstat.out
147	atf_check -o match:"$line_re" awk 'NR > 1' procstat.out
148}
149
150atf_test_case kernel_stacks
151kernel_stacks_head()
152{
153	atf_set "descr" "Captures kernel stacks for all visible threads"
154}
155kernel_stacks_body()
156{
157	atf_check -o save:procstat.out procstat -a kstack
158	atf_check -o not-empty awk '{if ($3 == "procstat") print}' procstat.out
159
160	atf_check -o save:procstat.out procstat -kka
161	atf_check -o not-empty awk '{if ($3 == "procstat") print}' procstat.out
162}
163
164atf_init_test_cases()
165{
166	atf_add_test_case binary_info
167	atf_add_test_case command_line_arguments
168	atf_add_test_case environment
169	atf_add_test_case file_descriptor
170	atf_add_test_case kernel_stacks
171}
172