1318325Sngie#
2346920Sngie# Copyright (c) 2017 Enji Cooper <ngie@FreeBSD.org>
3318325Sngie#
4318325Sngie# Redistribution and use in source and binary forms, with or without
5318325Sngie# modification, are permitted provided that the following conditions
6318325Sngie# are met:
7318325Sngie# 1. Redistributions of source code must retain the above copyright
8318325Sngie#    notice, this list of conditions and the following disclaimer.
9318325Sngie# 2. Redistributions in binary form must reproduce the above copyright
10318325Sngie#    notice, this list of conditions and the following disclaimer in the
11318325Sngie#    documentation and/or other materials provided with the distribution.
12318325Sngie#
13318325Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
14318325Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
15318325Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16318325Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
17318325Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18318325Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19318325Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20318325Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21318325Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22318325Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23318325Sngie# POSSIBILITY OF SUCH DAMAGE.
24318325Sngie#
25318325Sngie# $FreeBSD: stable/11/usr.bin/procstat/tests/procstat_test.sh 352492 2019-09-18 19:28:17Z jilles $
26318325Sngie#
27318325Sngie
28318325SngiePROG_PID=
29318325SngiePROG_PATH=$(atf_get_srcdir)/while1
30318325Sngie
31318325SngieSP='[[:space:]]'
32318325Sngie
33318325Sngiestart_program()
34318325Sngie{
35318325Sngie	echo "Starting program in background"
36318325Sngie	PROG_COMM=while1
37318325Sngie	PROG_PATH=$(atf_get_srcdir)/$PROG_COMM
38318325Sngie
39352492Sjilles	mkfifo wait_for_start || atf_fail "mkfifo"
40352492Sjilles	$PROG_PATH $* >wait_for_start &
41318325Sngie	PROG_PID=$!
42352492Sjilles	if ! read dummy <wait_for_start; then
43352492Sjilles		atf_fail "Program did not start properly"
44318325Sngie	fi
45352492Sjilles	rm wait_for_start
46318325Sngie}
47318325Sngie
48318325Sngieatf_test_case binary_info
49318325Sngiebinary_info_head()
50318325Sngie{
51318325Sngie	atf_set "descr" "Checks -b support"
52318325Sngie}
53318325Sngiebinary_info_body()
54318325Sngie{
55318325Sngie	start_program bogus-arg
56318325Sngie
57318325Sngie	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP*"
58318325Sngie	header_re=$(printf "$line_format" "PID" "COMM" "OSREL" "PATH")
59318325Sngie	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM "[[:digit:]]+" "$PROG_PATH")
60318325Sngie
61318325Sngie	atf_check -o save:procstat.out procstat -b $PROG_PID
62318325Sngie
63318325Sngie	atf_check -o match:"$header_re" head -n 1 procstat.out
64318325Sngie	atf_check -o match:"$line_re" tail -n 1 procstat.out
65318325Sngie}
66318325Sngie
67318325Sngieatf_test_case command_line_arguments
68318325Sngiecommand_line_arguments_head()
69318325Sngie{
70318325Sngie	atf_set "descr" "Checks -c support"
71318325Sngie}
72318325Sngiecommand_line_arguments_body()
73318325Sngie{
74318325Sngie	arguments="my arguments"
75318325Sngie
76318325Sngie	start_program $arguments
77318325Sngie
78318325Sngie	line_format="$SP*%s$SP+%s$SP+%s$SP*"
79318325Sngie	header_re=$(printf "$line_format" "PID" "COMM" "ARGS")
80318325Sngie	line_re=$(printf "$line_format" $PROG_PID "$PROG_COMM" "$PROG_PATH $arguments")
81318325Sngie
82318325Sngie	atf_check -o save:procstat.out procstat -c $PROG_PID
83318325Sngie	atf_check -o match:"$header_re" head -n 1 procstat.out
84318325Sngie	atf_check -o match:"$line_re" tail -n 1 procstat.out
85318325Sngie}
86318325Sngie
87318325Sngieatf_test_case environment
88318325Sngieenvironment_head()
89318325Sngie{
90318325Sngie	atf_set "descr" "Checks -e support"
91318325Sngie}
92318325Sngieenvironment_body()
93318325Sngie{
94318325Sngie	var="MY_VARIABLE=foo"
95318325Sngie	eval "export $var"
96318325Sngie
97318325Sngie	start_program my arguments
98318325Sngie
99318325Sngie	line_format="$SP*%s$SP+%s$SP+%s$SP*"
100318325Sngie	header_re=$(printf "$line_format" "PID" "COMM" "ENVIRONMENT")
101318325Sngie	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".*$var.*")
102318325Sngie
103318325Sngie	atf_check -o save:procstat.out procstat -e $PROG_PID
104318325Sngie
105318325Sngie	atf_check -o match:"$header_re" head -n 1 procstat.out
106318325Sngie	atf_check -o match:"$line_re" tail -n 1 procstat.out
107318325Sngie}
108318325Sngie
109318325Sngieatf_test_case file_descriptor
110318325Sngiefile_descriptor_head()
111318325Sngie{
112318325Sngie	atf_set "descr" "Checks -f support"
113318325Sngie}
114318325Sngiefile_descriptor_body()
115318325Sngie{
116318325Sngie	start_program my arguments
117318325Sngie
118318325Sngie	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP%s$SP*"
119318325Sngie	header_re=$(printf "$line_format" "PID" "COMM" "FD" "T" "V" "FLAGS" "REF" "OFFSET" "PRO" "NAME")
120318325Sngie	# XXX: write a more sensible feature test
121318325Sngie	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".+" ".+" ".+" ".+" ".+" ".+" ".+" ".+")
122318325Sngie
123318325Sngie	atf_check -o save:procstat.out procstat -f $PROG_PID
124318325Sngie
125318325Sngie	atf_check -o match:"$header_re" head -n 1 procstat.out
126318325Sngie	atf_check -o match:"$line_re" awk 'NR > 1' procstat.out
127318325Sngie}
128318325Sngie
129337542Smarkjatf_test_case kernel_stacks
130337542Smarkjkernel_stacks_head()
131337542Smarkj{
132337542Smarkj	atf_set "descr" "Captures kernel stacks for all visible threads"
133337542Smarkj}
134337542Smarkjkernel_stacks_body()
135337542Smarkj{
136346515Sngie	# Bug 237445: checks will fail because of missing MFCs on branch
137346515Sngie	#atf_check -o save:procstat.out procstat -a kstack
138346515Sngie	#atf_check -o not-empty awk '{if ($3 == "procstat") print}' procstat.out
139337542Smarkj
140337542Smarkj	atf_check -o save:procstat.out procstat -kka
141337542Smarkj	atf_check -o not-empty awk '{if ($3 == "procstat") print}' procstat.out
142337542Smarkj}
143337542Smarkj
144318325Sngieatf_init_test_cases()
145318325Sngie{
146318325Sngie	atf_add_test_case binary_info
147318325Sngie	atf_add_test_case command_line_arguments
148318325Sngie	atf_add_test_case environment
149318325Sngie	atf_add_test_case file_descriptor
150337542Smarkj	atf_add_test_case kernel_stacks
151318325Sngie}
152