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# Not tested 27# * modes other than "Data" and "Desc". We don't support those. 28# * Buffer ID other than 0. We don't support those. 29# * The Mode Specific field. We don't support it. 30 31. $(atf_get_srcdir)/ctl.subr 32 33atf_test_case basic cleanup 34basic_head() 35{ 36 atf_set "descr" "READ BUFFER can retrieve data previously written by WRITE BUFFER" 37 atf_set "require.user" "root" 38 atf_set "require.progs" sg_read_buffer sg_write_buffer 39} 40basic_body() 41{ 42 create_ramdisk 43 44 # Write to its buffer 45 cp /etc/passwd input 46 len=`wc -c input | cut -wf 2` 47 atf_check -o ignore sg_write_buffer --mode data --in=input /dev/$dev 48 49 # Read it back 50 atf_check -o save:output sg_read_buffer --mode data -l $len --raw /dev/$dev 51 52 # And verify 53 if ! diff -q input output; then 54 atf_fail "Miscompare!" 55 fi 56} 57basic_cleanup() 58{ 59 cleanup 60} 61 62# Read from the Descriptor mode. Along with Data, these are the only two modes 63# we support. 64atf_test_case desc cleanup 65desc_head() 66{ 67 atf_set "descr" "READ BUFFER can retrieve the buffer size via the DESCRIPTOR mode" 68 atf_set "require.user" "root" 69 atf_set "require.progs" sg_read_buffer 70} 71desc_body() 72{ 73 create_ramdisk 74 75 atf_check -o inline:" 00 00 04 00 00\n" sg_read_buffer --hex --mode desc /dev/$dev 76} 77desc_cleanup() 78{ 79 cleanup 80} 81 82atf_test_case length cleanup 83length_head() 84{ 85 atf_set "descr" "READ BUFFER can limit its length with the LENGTH field" 86 atf_set "require.user" "root" 87 atf_set "require.progs" sg_read_buffer sg_write_buffer 88} 89length_body() 90{ 91 create_ramdisk 92 93 # Write to its buffer 94 atf_check -o ignore -e ignore dd if=/dev/random of=input bs=4096 count=1 95 atf_check -o ignore -e ignore dd if=input bs=2048 count=1 of=expected 96 atf_check -o ignore sg_write_buffer --mode data --in=input /dev/$dev 97 98 # Read it back 99 atf_check -o save:output sg_read_buffer --mode data -l 2048 --raw /dev/$dev 100 101 # And verify 102 if ! diff -q expected output; then 103 atf_fail "Miscompare!" 104 fi 105} 106length_cleanup() 107{ 108 cleanup 109} 110 111atf_test_case offset cleanup 112offset_head() 113{ 114 atf_set "descr" "READ BUFFER accepts the BUFFER OFFSET field" 115 atf_set "require.user" "root" 116 atf_set "require.progs" sg_read_buffer sg_write_buffer 117} 118offset_body() 119{ 120 create_ramdisk 121 122 # Write to its buffer 123 atf_check -o ignore -e ignore dd if=/dev/random of=input bs=4096 count=1 124 atf_check -o ignore -e ignore dd if=input iseek=2 bs=512 count=1 of=expected 125 atf_check -o ignore sg_write_buffer --mode data --in=input /dev/$dev 126 127 # Read it back 128 atf_check -o save:output sg_read_buffer --mode data -l 512 -o 1024 --raw /dev/$dev 129 130 # And verify 131 if ! diff -q expected output; then 132 atf_fail "Miscompare!" 133 fi 134} 135offset_cleanup() 136{ 137 cleanup 138} 139 140atf_test_case uninitialized cleanup 141uninitialized_head() 142{ 143 atf_set "descr" "READ BUFFER buffers are zero-initialized" 144 atf_set "require.user" "root" 145 atf_set "require.progs" sg_read_buffer 146} 147uninitialized_body() 148{ 149 create_ramdisk 150 151 # Read an uninitialized buffer 152 atf_check -o save:output sg_read_buffer --mode data -l 262144 --raw /dev/$dev 153 154 # And verify 155 atf_check -o ignore -e ignore dd if=/dev/zero bs=262144 count=1 of=expected 156 if ! diff -q expected output; then 157 atf_fail "Miscompare!" 158 fi 159} 160uninitialized_cleanup() 161{ 162 cleanup 163} 164 165atf_init_test_cases() 166{ 167 atf_add_test_case basic 168 atf_add_test_case desc 169 atf_add_test_case length 170 atf_add_test_case offset 171 atf_add_test_case uninitialized 172} 173