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