xref: /freebsd/usr.sbin/jail/tests/jail_basic_test.sh (revision 48c738b038ac84f3334b20b9a0f56fa3d9b7f6d1)
1#
2# SPDX-License-Identifier: BSD-2-Clause
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
27atf_test_case "basic" "cleanup"
28basic_head()
29{
30	atf_set descr 'Basic jail test'
31	atf_set require.user root
32}
33
34basic_body()
35{
36	# Create the jail
37	atf_check -s exit:0 -o ignore jail -c name=basejail persist ip4.addr=192.0.1.1
38	# Check output of jls
39	atf_check -s exit:0 -o ignore jls
40	atf_check -s exit:0 -o ignore jls -v
41	atf_check -s exit:0 -o ignore jls -n
42	# Stop jail
43	atf_check -s exit:0 -o ignore jail -r basejail
44	jail -c name=basejail persist ip4.addr=192.0.1.1
45	# Stop jail by jid
46	atf_check -s exit:0 -o ignore jail -r `jls -j basejail jid`
47	# Recreate
48	atf_check -s exit:0 -o ignore jail -cm name=basejail persist ip4.addr=192.0.1.1
49	# Restart
50	atf_check -s exit:0 -o ignore jail -rc name=basejail persist ip4.addr=192.0.1.1
51}
52
53basic_cleanup()
54{
55	jail -r basejail
56}
57
58atf_test_case "list" "cleanup"
59list_head()
60{
61	atf_set descr 'Specify some jail parameters as lists'
62	atf_set require.user root
63}
64
65list_body()
66{
67	if [ "$(sysctl -qn kern.features.vimage)" -ne 1 ]; then
68		atf_skip "cannot create VNET jails"
69	fi
70	atf_check -o save:epair ifconfig epair create
71
72	epair=$(cat epair)
73	atf_check jail -c name=basejail vnet persist vnet.interface=${epair},${epair%a}b
74
75	atf_check -o ignore jexec basejail ifconfig ${epair}
76	atf_check -o ignore jexec basejail ifconfig ${epair%a}b
77}
78
79list_cleanup()
80{
81	jail -r basejail
82	if [ -f epair ]; then
83		ifconfig $(cat epair) destroy
84	fi
85}
86
87atf_test_case "nested" "cleanup"
88nested_head()
89{
90	atf_set descr 'Hierarchical jails test'
91	atf_set require.user root
92}
93
94nested_body()
95{
96	# Create the first jail
97	jail -c name=basejail persist ip4.addr=192.0.1.1 children.max=1
98	atf_check -s exit:0 -o empty \
99		jexec basejail \
100			jail -c name=nestedjail persist ip4.addr=192.0.1.1
101
102	atf_check -s exit:1 -o empty -e inline:"jail: prison limit exceeded\n"\
103		jexec basejail \
104			jail -c name=secondnestedjail persist ip4.addr=192.0.1.1
105	# Check output of jls
106	atf_check -s exit:0 -o ignore \
107		jexec basejail jls
108	atf_check -s exit:0 -o ignore \
109		jexec basejail jls -v
110	atf_check -s exit:0 -o ignore \
111		jexec basejail jls -n
112	# Create jail with no child - children.max should be 0 by default
113	jail -c name=basejail_nochild persist ip4.addr=192.0.1.1
114	atf_check -s exit:1 -o empty \
115		-e inline:"jail: jail_set: Operation not permitted\n" \
116		jexec basejail_nochild \
117			jail -c name=nestedjail persist ip4.addr=192.0.1.1
118}
119
120nested_cleanup()
121{
122	jail -r nestedjail
123	jail -r basejail
124	jail -r basejail_nochild
125}
126
127atf_test_case "commands" "cleanup"
128commands_head()
129{
130	atf_set descr 'Commands jail test'
131	atf_set require.user root
132}
133
134commands_body()
135{
136	# exec.prestart
137	atf_check -s exit:0 -o inline:"START\n" \
138		jail -f $(atf_get_srcdir)/commands.jail.conf -qc basejail
139	# exec.prestop by jailname
140	atf_check -s exit:0 -o inline:"STOP\n" \
141		jail -f $(atf_get_srcdir)/commands.jail.conf -qr basejail
142	# exec.prestop by jid
143	jail -f $(atf_get_srcdir)/commands.jail.conf -qc basejail
144	atf_check -s exit:0 -o inline:"STOP\n" \
145		jail -f $(atf_get_srcdir)/commands.jail.conf -qr `jls -j basejail jid`
146}
147
148commands_cleanup()
149{
150	jls -j basejail > /dev/null 2>&1
151	if [ $? -e 0 ]
152	then
153	    jail -r basejail
154	fi
155}
156
157atf_init_test_cases()
158{
159	atf_add_test_case "basic"
160	atf_add_test_case "list"
161	atf_add_test_case "nested"
162	atf_add_test_case "commands"
163}
164