xref: /freebsd/usr.bin/limits/tests/limits_test.sh (revision 0f63c4af5e4761680550b0aa08cc96f52076c08c)
1dd76c272SEnji Cooper#
2dd76c272SEnji Cooper# Copyright 2015 EMC Corp.
3dd76c272SEnji Cooper# All rights reserved.
4dd76c272SEnji Cooper#
5dd76c272SEnji Cooper# Redistribution and use in source and binary forms, with or without
6dd76c272SEnji Cooper# modification, are permitted provided that the following conditions are
7dd76c272SEnji Cooper# met:
8dd76c272SEnji Cooper#
9dd76c272SEnji Cooper# * Redistributions of source code must retain the above copyright
10dd76c272SEnji Cooper#   notice, this list of conditions and the following disclaimer.
11dd76c272SEnji Cooper# * Redistributions in binary form must reproduce the above copyright
12dd76c272SEnji Cooper#   notice, this list of conditions and the following disclaimer in the
13dd76c272SEnji Cooper#   documentation and/or other materials provided with the distribution.
14dd76c272SEnji Cooper#
15dd76c272SEnji Cooper# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16dd76c272SEnji Cooper# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17dd76c272SEnji Cooper# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18dd76c272SEnji Cooper# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19dd76c272SEnji Cooper# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20dd76c272SEnji Cooper# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21dd76c272SEnji Cooper# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22dd76c272SEnji Cooper# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23dd76c272SEnji Cooper# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24dd76c272SEnji Cooper# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25dd76c272SEnji Cooper# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26dd76c272SEnji Cooper#
27dd76c272SEnji Cooper#
28dd76c272SEnji Cooper
29dd76c272SEnji Cooper# Make sure time(1) is consistent with the FreeBSD time command and not the
30dd76c272SEnji Cooper# shell interpretation of time(1)
31dd76c272SEnji CooperTIME=/usr/bin/time
32dd76c272SEnji Cooper
33485f783fSEric van Gyzenvalidate_time_output()
34485f783fSEric van Gyzen{
35485f783fSEric van Gyzen	local time_output=$1
36485f783fSEric van Gyzen
37485f783fSEric van Gyzen	# RLIMIT_CPU is enforced by a 1-second timer.  Allow 3 + 1 + a little.
38485f783fSEric van Gyzen	atf_check awk '
39485f783fSEric van Gyzen		/^(user|sys) / {
40485f783fSEric van Gyzen		    sum += $2
41485f783fSEric van Gyzen		}
42485f783fSEric van Gyzen		END {
43485f783fSEric van Gyzen		    if (sum < 3 || sum >= 4.5) {
44485f783fSEric van Gyzen			print(sum);
45485f783fSEric van Gyzen			exit(1);
46485f783fSEric van Gyzen		    }
47485f783fSEric van Gyzen		}
48485f783fSEric van Gyzen	' < $time_output
49485f783fSEric van Gyzen}
50485f783fSEric van Gyzen
51485f783fSEric van Gyzenatf_test_case cputime_hard_flag cleanup
52dd76c272SEnji Coopercputime_hard_flag_body()
53dd76c272SEnji Cooper{
54dd76c272SEnji Cooper
55485f783fSEric van Gyzen	atf_check -o match:'cputime[[:space:]]+3 secs' \
56dd76c272SEnji Cooper	    limits -H -t 3 limits -H
57485f783fSEric van Gyzen	atf_check -o match:'cputime[[:space:]]+3 secs' \
58dd76c272SEnji Cooper	    limits -H -t 3 limits -S
59485f783fSEric van Gyzen	atf_check -e save:time_output -s signal:sigkill \
60*0f63c4afSMark Johnston	    $TIME -p limits -H -t 3 sh -c 'while : ; do : ; done'
61485f783fSEric van Gyzen	validate_time_output time_output
62485f783fSEric van Gyzen}
63485f783fSEric van Gyzencputime_hard_flag_cleanup()
64485f783fSEric van Gyzen{
65485f783fSEric van Gyzen	rm -f time_output
66dd76c272SEnji Cooper}
67dd76c272SEnji Cooper
68dd76c272SEnji CooperSIGXCPU=24 # atf_check doesn't know sigxcpu
69dd76c272SEnji Cooper
70485f783fSEric van Gyzenatf_test_case cputime_soft_flag cleanup
71dd76c272SEnji Coopercputime_soft_flag_body()
72dd76c272SEnji Cooper{
73dd76c272SEnji Cooper
74485f783fSEric van Gyzen	atf_check -o match:'cputime-max[[:space:]]+infinity secs' \
75dd76c272SEnji Cooper	    limits -S -t 3 limits -H
76485f783fSEric van Gyzen	atf_check -o match:'cputime-cur[[:space:]]+3 secs' \
77dd76c272SEnji Cooper	    limits -S -t 3 limits -S
78485f783fSEric van Gyzen	atf_check -e save:time_output -s signal:$SIGXCPU \
79*0f63c4afSMark Johnston	    $TIME -p limits -S -t 3 sh -c 'while : ; do : ; done'
80485f783fSEric van Gyzen	validate_time_output time_output
81485f783fSEric van Gyzen}
82485f783fSEric van Gyzencputime_soft_flag_cleanup()
83485f783fSEric van Gyzen{
84485f783fSEric van Gyzen	rm -f time_output
85dd76c272SEnji Cooper}
86dd76c272SEnji Cooper
87dd76c272SEnji Cooperatf_init_test_cases()
88dd76c272SEnji Cooper{
89dd76c272SEnji Cooper
90dd76c272SEnji Cooper	atf_add_test_case cputime_hard_flag
91dd76c272SEnji Cooper	atf_add_test_case cputime_soft_flag
92dd76c272SEnji Cooper}
93