xref: /freebsd/usr.bin/limits/tests/limits_test.sh (revision 485f783f882ed026cdbfede89aa7bddad3fffdf3)
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# $FreeBSD$
28dd76c272SEnji Cooper#
29dd76c272SEnji Cooper
30dd76c272SEnji Cooper# Make sure time(1) is consistent with the FreeBSD time command and not the
31dd76c272SEnji Cooper# shell interpretation of time(1)
32dd76c272SEnji CooperTIME=/usr/bin/time
33dd76c272SEnji Cooper
34*485f783fSEric van Gyzenvalidate_time_output()
35*485f783fSEric van Gyzen{
36*485f783fSEric van Gyzen	local time_output=$1
37*485f783fSEric van Gyzen
38*485f783fSEric van Gyzen	# RLIMIT_CPU is enforced by a 1-second timer.  Allow 3 + 1 + a little.
39*485f783fSEric van Gyzen	atf_check awk '
40*485f783fSEric van Gyzen		/^(user|sys) / {
41*485f783fSEric van Gyzen		    sum += $2
42*485f783fSEric van Gyzen		}
43*485f783fSEric van Gyzen		END {
44*485f783fSEric van Gyzen		    if (sum < 3 || sum >= 4.5) {
45*485f783fSEric van Gyzen			print(sum);
46*485f783fSEric van Gyzen			exit(1);
47*485f783fSEric van Gyzen		    }
48*485f783fSEric van Gyzen		}
49*485f783fSEric van Gyzen	' < $time_output
50*485f783fSEric van Gyzen}
51*485f783fSEric van Gyzen
52*485f783fSEric van Gyzenatf_test_case cputime_hard_flag cleanup
53dd76c272SEnji Coopercputime_hard_flag_body()
54dd76c272SEnji Cooper{
55dd76c272SEnji Cooper
56*485f783fSEric van Gyzen	atf_check -o match:'cputime[[:space:]]+3 secs' \
57dd76c272SEnji Cooper	    limits -H -t 3 limits -H
58*485f783fSEric van Gyzen	atf_check -o match:'cputime[[:space:]]+3 secs' \
59dd76c272SEnji Cooper	    limits -H -t 3 limits -S
60*485f783fSEric van Gyzen	atf_check -e save:time_output -s signal:sigkill \
61dd76c272SEnji Cooper	    limits -H -t 3 $TIME -p sh -c 'while : ; do : ; done'
62*485f783fSEric van Gyzen	validate_time_output time_output
63*485f783fSEric van Gyzen}
64*485f783fSEric van Gyzencputime_hard_flag_cleanup()
65*485f783fSEric van Gyzen{
66*485f783fSEric van Gyzen	rm -f time_output
67dd76c272SEnji Cooper}
68dd76c272SEnji Cooper
69dd76c272SEnji CooperSIGXCPU=24 # atf_check doesn't know sigxcpu
70dd76c272SEnji Cooper
71*485f783fSEric van Gyzenatf_test_case cputime_soft_flag cleanup
72dd76c272SEnji Coopercputime_soft_flag_body()
73dd76c272SEnji Cooper{
74dd76c272SEnji Cooper
75*485f783fSEric van Gyzen	atf_check -o match:'cputime-max[[:space:]]+infinity secs' \
76dd76c272SEnji Cooper	    limits -S -t 3 limits -H
77*485f783fSEric van Gyzen	atf_check -o match:'cputime-cur[[:space:]]+3 secs' \
78dd76c272SEnji Cooper	    limits -S -t 3 limits -S
79*485f783fSEric van Gyzen	atf_check -e save:time_output -s signal:$SIGXCPU \
80dd76c272SEnji Cooper	    limits -S -t 3 $TIME -p sh -c 'while : ; do : ; done'
81*485f783fSEric van Gyzen	validate_time_output time_output
82*485f783fSEric van Gyzen}
83*485f783fSEric van Gyzencputime_soft_flag_cleanup()
84*485f783fSEric van Gyzen{
85*485f783fSEric van Gyzen	rm -f time_output
86dd76c272SEnji Cooper}
87dd76c272SEnji Cooper
88dd76c272SEnji Cooperatf_init_test_cases()
89dd76c272SEnji Cooper{
90dd76c272SEnji Cooper
91dd76c272SEnji Cooper	atf_add_test_case cputime_hard_flag
92dd76c272SEnji Cooper	atf_add_test_case cputime_soft_flag
93dd76c272SEnji Cooper}
94