xref: /freebsd/tests/sys/cam/ctl/prevent.sh (revision 215fd38e2915d142cb4b0245f137329ef4da5a12)
1# SPDX-License-Identifier: BSD-2-Clause
2#
3# Copyright (c) 2024 Axcient
4# All rights reserved.
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 DOCUMENTATION IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26. $(atf_get_srcdir)/ctl.subr
27
28# TODO
29# * multiple initiators may block removal
30
31# Not Tested
32# * persistent removal (not implemented in CTL)
33
34atf_test_case allow cleanup
35allow_head()
36{
37	atf_set "descr" "SCSI PREVENT ALLOW MEDIUM REMOVAL will prevent a CD from being ejected"
38	atf_set "require.user" "root"
39	atf_set "require.progs" "sg_prevent sg_start ctladm"
40}
41allow_body()
42{
43	# -t 5 for CD/DVD device type
44	create_ramdisk -t 5
45
46	atf_check sg_prevent --prevent 1 /dev/$dev
47
48	# Now sg_start --eject should fail
49	atf_check -s exit:5 -e match:"Illegal request" sg_start --eject /dev/$dev
50
51	atf_check sg_prevent --allow /dev/$dev
52
53	# Now sg_start --eject should work again
54	atf_check -s exit:0 sg_start --eject /dev/$dev
55}
56allow_cleanup()
57{
58	cleanup
59}
60
61atf_test_case allow_idempotent cleanup
62allow_idempotent_head()
63{
64	atf_set "descr" "SCSI PREVENT ALLOW MEDIUM REMOVAL is idempotent when run from the same initiator"
65	atf_set "require.user" "root"
66	atf_set "require.progs" "sg_prevent sg_start ctladm"
67}
68allow_idempotent_body()
69{
70	# -t 5 for CD/DVD device type
71	create_ramdisk -t 5
72
73	atf_check sg_prevent --allow /dev/$dev
74	atf_check sg_prevent --allow /dev/$dev
75	atf_check sg_prevent --prevent 1 /dev/$dev
76
77	# Even though we ran --allow twice, a single --prevent command should
78	# suffice to prevent ejecting.  Multiple ALLOW/PREVENT commands from
79	# the same initiator don't have any additional effect.
80	atf_check -s exit:5 -e match:"Illegal request" sg_start --eject /dev/$dev
81}
82allow_idempotent_cleanup()
83{
84	cleanup
85}
86
87atf_test_case nonremovable cleanup
88nonremovable_head()
89{
90	atf_set "descr" "SCSI PREVENT ALLOW MEDIUM REMOVAL may not be used on non-removable media"
91	atf_set "require.user" "root"
92	atf_set "require.progs" "sg_prevent ctladm"
93}
94nonremovable_body()
95{
96	# Create a HDD, not a CD, device
97	create_ramdisk -t 0
98
99	atf_check -s exit:9 -e match:"Invalid opcode" sg_prevent /dev/$dev
100}
101nonremovable_cleanup()
102{
103	cleanup
104}
105
106atf_test_case prevent cleanup
107prevent_head()
108{
109	atf_set "descr" "SCSI PREVENT ALLOW MEDIUM REMOVAL will prevent a CD from being ejected"
110	atf_set "require.user" "root"
111	atf_set "require.progs" "sg_prevent sg_start ctladm"
112}
113prevent_body()
114{
115	# -t 5 for CD/DVD device type
116	create_ramdisk -t 5
117
118	atf_check sg_prevent --prevent 1 /dev/$dev
119
120	# Now sg_start --eject should fail
121	atf_check -s exit:5 -e match:"Illegal request" sg_start --eject /dev/$dev
122}
123prevent_cleanup()
124{
125	cleanup
126}
127
128atf_test_case prevent_idempotent cleanup
129prevent_idempotent_head()
130{
131	atf_set "descr" "SCSI PREVENT ALLOW MEDIUM REMOVAL is idempotent when run from the same initiator"
132	atf_set "require.user" "root"
133	atf_set "require.progs" "sg_prevent sg_start ctladm"
134}
135prevent_idempotent_body()
136{
137	# -t 5 for CD/DVD device type
138	create_ramdisk -t 5
139
140	atf_check sg_prevent --prevent 1 /dev/$dev
141	atf_check sg_prevent --prevent 1 /dev/$dev
142	atf_check sg_prevent --allow /dev/$dev
143
144	# Even though we ran prevent idempotent and allow only once, eject
145	# should be allowed.  Multiple PREVENT commands from the same initiator
146	# don't have any additional effect.
147	atf_check sg_start --eject /dev/$dev
148}
149prevent_idempotent_cleanup()
150{
151	cleanup
152}
153
154atf_init_test_cases()
155{
156	atf_add_test_case allow
157	atf_add_test_case allow_idempotent
158	atf_add_test_case nonremovable
159	atf_add_test_case prevent
160	atf_add_test_case prevent_idempotent
161}
162