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#
26
27PROG_PID=
28PROG_PATH=$(atf_get_srcdir)/while1
29
30SP='[[:space:]]'
31
32start_program()
33{
34	echo "Starting program in background"
35	PROG_COMM=while1
36	PROG_PATH=$(atf_get_srcdir)/$PROG_COMM
37
38	mkfifo wait_for_start || atf_fail "mkfifo"
39	$PROG_PATH $* >wait_for_start &
40	PROG_PID=$!
41	if ! read dummy <wait_for_start; then
42		atf_fail "Program did not start properly"
43	fi
44	rm wait_for_start
45}
46
47atf_test_case binary_info
48binary_info_head()
49{
50	atf_set "descr" "Checks -b support"
51}
52binary_info_body()
53{
54	start_program bogus-arg
55
56	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP*"
57	header_re=$(printf "$line_format" "PID" "COMM" "OSREL" "PATH")
58	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM "[[:digit:]]+" "$PROG_PATH")
59
60	atf_check -o save:procstat.out procstat binary $PROG_PID
61	atf_check -o match:"$header_re" head -n 1 procstat.out
62	atf_check -o match:"$line_re" tail -n 1 procstat.out
63
64	atf_check -o save:procstat.out procstat -b $PROG_PID
65	atf_check -o match:"$header_re" head -n 1 procstat.out
66	atf_check -o match:"$line_re" tail -n 1 procstat.out
67}
68
69atf_test_case command_line_arguments
70command_line_arguments_head()
71{
72	atf_set "descr" "Checks -c support"
73}
74command_line_arguments_body()
75{
76	arguments="my arguments"
77
78	start_program $arguments
79
80	line_format="$SP*%s$SP+%s$SP+%s$SP*"
81	header_re=$(printf "$line_format" "PID" "COMM" "ARGS")
82	line_re=$(printf "$line_format" $PROG_PID "$PROG_COMM" "$PROG_PATH $arguments")
83
84	atf_check -o save:procstat.out procstat arguments $PROG_PID
85	atf_check -o match:"$header_re" head -n 1 procstat.out
86	atf_check -o match:"$line_re" tail -n 1 procstat.out
87
88	atf_check -o save:procstat.out procstat -c $PROG_PID
89	atf_check -o match:"$header_re" head -n 1 procstat.out
90	atf_check -o match:"$line_re" tail -n 1 procstat.out
91}
92
93atf_test_case environment
94environment_head()
95{
96	atf_set "descr" "Checks -e support"
97}
98environment_body()
99{
100	var="MY_VARIABLE=foo"
101	eval "export $var"
102
103	start_program my arguments
104
105	line_format="$SP*%s$SP+%s$SP+%s$SP*"
106	header_re=$(printf "$line_format" "PID" "COMM" "ENVIRONMENT")
107	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".*$var.*")
108
109	atf_check -o save:procstat.out procstat environment $PROG_PID
110	atf_check -o match:"$header_re" head -n 1 procstat.out
111	atf_check -o match:"$line_re" tail -n 1 procstat.out
112
113	atf_check -o save:procstat.out procstat -e $PROG_PID
114	atf_check -o match:"$header_re" head -n 1 procstat.out
115	atf_check -o match:"$line_re" tail -n 1 procstat.out
116}
117
118atf_test_case file_descriptor
119file_descriptor_head()
120{
121	atf_set "descr" "Checks -f support"
122}
123file_descriptor_body()
124{
125	start_program my arguments
126
127	line_format="$SP*%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP+%s$SP%s$SP*"
128	header_re=$(printf "$line_format" "PID" "COMM" "FD" "T" "V" "FLAGS" "REF" "OFFSET" "PRO" "NAME")
129	# XXX: write a more sensible feature test
130	line_re=$(printf "$line_format" $PROG_PID $PROG_COMM ".+" ".+" ".+" ".+" ".+" ".+" ".+" ".+")
131
132	atf_check -o save:procstat.out procstat files $PROG_PID
133	atf_check -o match:"$header_re" head -n 1 procstat.out
134	atf_check -o match:"$line_re" awk 'NR > 1' procstat.out
135
136	atf_check -o save:procstat.out procstat -f $PROG_PID
137	atf_check -o match:"$header_re" head -n 1 procstat.out
138	atf_check -o match:"$line_re" awk 'NR > 1' procstat.out
139}
140
141atf_test_case kernel_stacks
142kernel_stacks_head()
143{
144	atf_set "descr" "Captures kernel stacks for all visible threads"
145}
146kernel_stacks_body()
147{
148	atf_check -o save:procstat.out procstat -a kstack
149	atf_check -o not-empty awk '{if ($3 == "procstat") print}' procstat.out
150
151	atf_check -o save:procstat.out procstat -kka
152	atf_check -o not-empty awk '{if ($3 == "procstat") print}' procstat.out
153}
154
155atf_init_test_cases()
156{
157	atf_add_test_case binary_info
158	atf_add_test_case command_line_arguments
159	atf_add_test_case environment
160	atf_add_test_case file_descriptor
161	atf_add_test_case kernel_stacks
162}
163