1#
2# SPDX-License-Identifier: BSD-2-Clause
3#
4# Copyright (c) 2024 Igor Ostapenko <pm@igoro.pro>
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26
27#
28# Even being is_exclusive="true" this test does not expect a host to spawn
29# other jails during the test execution.
30#
31atf_test_case "max_cur" "cleanup"
32max_cur_head()
33{
34	atf_set descr 'Test maximum and current number of child jails'
35	atf_set require.user root
36}
37max_cur_body()
38{
39	if ! which -s jail; then
40		atf_skip "This test requires jail"
41	fi
42	origin_max=$(sysctl -n security.jail.children.max)
43	origin_cur=$(sysctl -n security.jail.children.cur)
44
45	# Magic numbers reasoning:
46	# 3 stands for:
47	#   - the test creates three jails: childfree, maxallowed, maxallowed.family
48	# 6 stands for:
49	#   - maxallowed.family wants to set children.max=4
50	#   - it means that its parent (maxallowed) should have at least children.max=5
51	#   - it makes the origin (parent of maxallowed) provide children.max=6 minimum
52	#
53	test $origin_cur -le $origin_max || atf_fail "Abnormal cur=$origin_cur > max=$origin_max."
54	test $((origin_max - origin_cur)) -ge 3 || atf_skip "Not enough child jails are allowed for the test."
55	test $origin_max -ge 6 || atf_skip "Not high enough children.max limit for the test."
56
57	jail -c name=childfree persist
58	atf_check_equal "$((origin_cur + 1))" "$(sysctl -n security.jail.children.cur)"
59	atf_check_equal "0" "$(jexec childfree sysctl -n security.jail.children.max)"
60	atf_check_equal "0" "$(jexec childfree sysctl -n security.jail.children.cur)"
61
62	jail -c name=maxallowed children.max=$((origin_max - 1)) persist
63	atf_check_equal "$((origin_cur + 2))" "$(sysctl -n security.jail.children.cur)"
64	atf_check_equal "$((origin_max - 1))" "$(jexec maxallowed sysctl -n security.jail.children.max)"
65	atf_check_equal "0" "$(jexec maxallowed sysctl -n security.jail.children.cur)"
66
67	jexec maxallowed jail -c name=family children.max=4 persist
68	atf_check_equal "$((origin_cur + 3))" "$(sysctl -n security.jail.children.cur)"
69	atf_check_equal "1" "$(jexec maxallowed sysctl -n security.jail.children.cur)"
70	atf_check_equal "4" "$(jexec maxallowed.family sysctl -n security.jail.children.max)"
71	atf_check_equal "0" "$(jexec maxallowed.family sysctl -n security.jail.children.cur)"
72}
73max_cur_cleanup()
74{
75	jail -r maxallowed
76	jail -r childfree
77	return 0
78}
79
80atf_init_test_cases()
81{
82	atf_add_test_case "max_cur"
83}
84