1#
2# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3#
4# Copyright (c) 2019 Michael Zhilin 
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# $FreeBSD: stable/11/usr.sbin/jail/tests/jail_basic_test.sh 370002 2021-06-16 20:30:40Z git2svn $
28
29atf_test_case "basic" "cleanup"
30atf_test_case "nested" "cleanup"
31atf_test_case "commands" "cleanup"
32
33basic_head()
34{
35	atf_set descr 'Basic jail test'
36	atf_set require.user root
37}
38
39basic_body()
40{
41	# Create the jail
42	atf_check -s exit:0 -o ignore jail -c name=basejail persist ip4.addr=192.0.1.1
43	# Check output of jls
44	atf_check -s exit:0 -o ignore jls
45	atf_check -s exit:0 -o ignore jls -v
46	atf_check -s exit:0 -o ignore jls -n
47	# Stop jail
48	atf_check -s exit:0 -o ignore jail -r basejail
49	jail -c name=basejail persist ip4.addr=192.0.1.1
50	# Stop jail by jid
51	atf_check -s exit:0 -o ignore jail -r `jls -j basejail jid`
52	# Recreate
53	atf_check -s exit:0 -o ignore jail -cm name=basejail persist ip4.addr=192.0.1.1
54	# Restart
55	atf_check -s exit:0 -o ignore jail -rc name=basejail persist ip4.addr=192.0.1.1
56}
57
58basic_cleanup()
59{
60	jail -r basejail
61}
62
63nested_head()
64{
65	atf_set descr 'Hierarchical jails test'
66	atf_set require.user root
67}
68
69nested_body()
70{
71	# Create the first jail
72	jail -c name=basejail persist ip4.addr=192.0.1.1 children.max=1
73	atf_check -s exit:0 -o empty \
74		jexec basejail \
75			jail -c name=nestedjail persist ip4.addr=192.0.1.1
76
77	atf_check -s exit:1 -o empty -e inline:"jail: prison limit exceeded\n"\
78		jexec basejail \
79			jail -c name=secondnestedjail persist ip4.addr=192.0.1.1 
80	# Check output of jls
81	atf_check -s exit:0 -o ignore \
82		jexec basejail jls
83	atf_check -s exit:0 -o ignore \
84		jexec basejail jls -v
85	atf_check -s exit:0 -o ignore \
86		jexec basejail jls -n
87	# Create jail with no child - children.max should be 0 by default
88	jail -c name=basejail_nochild persist ip4.addr=192.0.1.1
89	atf_check -s exit:1 -o empty \
90		-e inline:"jail: jail_set: Operation not permitted\n" \
91		jexec basejail_nochild \
92			jail -c name=nestedjail persist ip4.addr=192.0.1.1 
93}
94
95nested_cleanup()
96{
97	jail -r nestedjail
98	jail -r basejail
99	jail -r basejail_nochild
100}
101
102commands_head()
103{
104	atf_set descr 'Commands jail test'
105	atf_set require.user root
106}
107
108commands_body()
109{
110	# exec.prestart
111	atf_check -s exit:0 -o inline:"START\n" \
112		jail -f $(atf_get_srcdir)/commands.jail.conf -qc basejail
113	# exec.prestop by jailname
114	atf_check -s exit:0 -o inline:"STOP\n" \
115		jail -f $(atf_get_srcdir)/commands.jail.conf -qr basejail 
116	# exec.prestop by jid
117	jail -f $(atf_get_srcdir)/commands.jail.conf -qc basejail
118	atf_check -s exit:0 -o inline:"STOP\n" \
119		jail -f $(atf_get_srcdir)/commands.jail.conf -qr `jls -j basejail jid`
120}
121
122commands_cleanup() 
123{
124	jls -j basejail > /dev/null 2>&1
125	if [ $? -e 0 ] 
126	then
127	    jail -r basejail
128	fi
129}
130
131atf_init_test_cases()
132{
133	atf_add_test_case "basic"
134	atf_add_test_case "nested"
135	atf_add_test_case "commands"
136}
137