# SPDX-License-Identifier: BSD-2-Clause1#2# Copyright (c) 2024 Axcient3# All rights reserved.4#5# Redistribution and use in source and binary forms, with or without6# modification, are permitted provided that the following conditions7# are met:8# 1. Redistributions of source code must retain the above copyright9# notice, this list of conditions and the following disclaimer.10# 2. Redistributions in binary form must reproduce the above copyright11# notice, this list of conditions and the following disclaimer in the12# documentation and/or other materials provided with the distribution.13#14# THIS DOCUMENTATION IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR15# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,18# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2425. $(atf_get_srcdir)/ctl.subr2627# TODO28# * multiple initiators may block removal2930# Not Tested31# * persistent removal (not implemented in CTL)3233atf_test_case allow cleanup34allow_head()35{36atf_set "descr" "SCSI PREVENT ALLOW MEDIUM REMOVAL will prevent a CD from being ejected"37atf_set "require.user" "root"38atf_set "require.progs" "sg_prevent sg_start ctladm"39}40allow_body()41{42# -t 5 for CD/DVD device type43create_ramdisk -t 54445atf_check sg_prevent --prevent 1 /dev/$dev4647# Now sg_start --eject should fail48atf_check -s exit:5 -e match:"Illegal request" sg_start --eject /dev/$dev4950atf_check sg_prevent --allow /dev/$dev5152# Now sg_start --eject should work again53atf_check -s exit:0 sg_start --eject /dev/$dev54}55allow_cleanup()56{57cleanup58}5960atf_test_case allow_idempotent cleanup61allow_idempotent_head()62{63atf_set "descr" "SCSI PREVENT ALLOW MEDIUM REMOVAL is idempotent when run from the same initiator"64atf_set "require.user" "root"65atf_set "require.progs" "sg_prevent sg_start ctladm"66}67allow_idempotent_body()68{69# -t 5 for CD/DVD device type70create_ramdisk -t 57172atf_check sg_prevent --allow /dev/$dev73atf_check sg_prevent --allow /dev/$dev74atf_check sg_prevent --prevent 1 /dev/$dev7576# Even though we ran --allow twice, a single --prevent command should77# suffice to prevent ejecting. Multiple ALLOW/PREVENT commands from78# the same initiator don't have any additional effect.79atf_check -s exit:5 -e match:"Illegal request" sg_start --eject /dev/$dev80}81allow_idempotent_cleanup()82{83cleanup84}8586atf_test_case nonremovable cleanup87nonremovable_head()88{89atf_set "descr" "SCSI PREVENT ALLOW MEDIUM REMOVAL may not be used on non-removable media"90atf_set "require.user" "root"91atf_set "require.progs" "sg_prevent ctladm"92}93nonremovable_body()94{95# Create a HDD, not a CD, device96create_ramdisk -t 09798atf_check -s exit:9 -e match:"Invalid opcode" sg_prevent /dev/$dev99}100nonremovable_cleanup()101{102cleanup103}104105atf_test_case prevent cleanup106prevent_head()107{108atf_set "descr" "SCSI PREVENT ALLOW MEDIUM REMOVAL will prevent a CD from being ejected"109atf_set "require.user" "root"110atf_set "require.progs" "sg_prevent sg_start ctladm"111}112prevent_body()113{114# -t 5 for CD/DVD device type115create_ramdisk -t 5116117atf_check sg_prevent --prevent 1 /dev/$dev118119# Now sg_start --eject should fail120atf_check -s exit:5 -e match:"Illegal request" sg_start --eject /dev/$dev121}122prevent_cleanup()123{124cleanup125}126127atf_test_case prevent_idempotent cleanup128prevent_idempotent_head()129{130atf_set "descr" "SCSI PREVENT ALLOW MEDIUM REMOVAL is idempotent when run from the same initiator"131atf_set "require.user" "root"132atf_set "require.progs" "sg_prevent sg_start ctladm"133}134prevent_idempotent_body()135{136# -t 5 for CD/DVD device type137create_ramdisk -t 5138139atf_check sg_prevent --prevent 1 /dev/$dev140atf_check sg_prevent --prevent 1 /dev/$dev141atf_check sg_prevent --allow /dev/$dev142143# Even though we ran prevent idempotent and allow only once, eject144# should be allowed. Multiple PREVENT commands from the same initiator145# don't have any additional effect.146atf_check sg_start --eject /dev/$dev147}148prevent_idempotent_cleanup()149{150cleanup151}152153atf_init_test_cases()154{155atf_add_test_case allow156atf_add_test_case allow_idempotent157atf_add_test_case nonremovable158atf_add_test_case prevent159atf_add_test_case prevent_idempotent160}161162163