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