"Fossies" - the Fresh Open Source Software Archive

Member "cryptsetup-2.4.3/tests/blockwise-compat" (13 Jan 2022, 11995 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Bash source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "blockwise-compat": 2.3.6_vs_2.4.0.

    1 #!/bin/bash
    2 
    3 # set _FORCE_LOCAL environment variable to run blockwise unit tests even on local
    4 # nfs. Some tests will fail because nfs is eager to write for example 4095 bytes
    5 # in O_DIRECT mode.
    6 
    7 BW_UNIT=./unit-utils-io
    8 STRACE=strace
    9 MNT_DIR=./mnt_bwunit
   10 LOCAL_FILE=./blockwise_localfile
   11 
   12 # $1 path to scsi debug bdev
   13 scsi_debug_teardown() {
   14     local _tries=15;
   15 
   16     while [ -b "$1" -a $_tries -gt 0 ]; do
   17         rmmod scsi_debug >/dev/null 2>&1
   18         if [ -b "$1" ]; then
   19             sleep .1
   20             _tries=$((_tries-1))
   21         fi
   22     done
   23 
   24     test ! -b "$1" || rmmod scsi_debug >/dev/null 2>&1
   25 }
   26 
   27 cleanup() {
   28     if [ -d "$MNT_DIR" ] ; then
   29         umount -f $MNT_DIR 2>/dev/null
   30         rmdir $MNT_DIR 2>/dev/null
   31     fi
   32     rm -f $LOCAL_FILE 2> /dev/null
   33     scsi_debug_teardown "$DEV" || exit 100
   34 }
   35 
   36 fail()
   37 {
   38     if [ -n "$1" ] ; then echo "FAIL $1" ; else echo "FAIL" ; fi
   39     cleanup
   40     exit 100
   41 }
   42 
   43 fail_count()
   44 {
   45     echo "$MSG[FAIL]"
   46     FAILS=$((FAILS+1))
   47 }
   48 
   49 warn_count()
   50 {
   51     echo "$MSG[WARNING]"
   52     WARNS=$((WARNS+1))
   53 }
   54 
   55 skip()
   56 {
   57     echo "TEST SKIPPED: $1"
   58     cleanup
   59     exit 0
   60 }
   61 
   62 add_device() {
   63     rmmod scsi_debug >/dev/null 2>&1
   64     if [ -d /sys/module/scsi_debug ] ; then
   65         echo "Cannot use scsi_debug module (in use or compiled-in), test skipped."
   66         exit 77
   67     fi
   68     modprobe scsi_debug $@ delay=0 >/dev/null 2>&1
   69     if [ $? -ne 0 ] ; then
   70         echo "This kernel seems to not support proper scsi_debug module, test skipped."
   71         exit 77
   72     fi
   73     DEV=$(grep -l -e scsi_debug /sys/block/*/device/model | cut -f4 -d /)
   74     DEV="/dev/$DEV"
   75     [ -b $DEV ] || fail "Cannot find $DEV."
   76 }
   77 
   78 falloc() {
   79     dd if=/dev/zero of=$2 bs=1M count=$1 2> /dev/null
   80 }
   81 
   82 run_all_in_fs() {
   83     for file in $(ls img_fs_*.img.xz) ; do
   84         echo "Run tests in $file put on top block device."
   85         xz -d -c $file | dd of=$DEV bs=1M 2>/dev/null || fail "bad image"
   86         [ ! -d $MNT_DIR ] && mkdir $MNT_DIR
   87         mount $DEV $MNT_DIR
   88         if [ $? -ne 0 ]; then
   89             echo "Mounting image $file failed, skipped."
   90             continue;
   91         fi
   92         rm -rf $MNT_DIR/* 2>/dev/null
   93         local tfile=$MNT_DIR/bwunit_tstfile
   94         falloc $DEVSIZEMB $tfile || fail "enospc?"
   95         local iobsize=$(stat -c "%o" $tfile)
   96         test -n "$iobsize" -a $iobsize -gt 0 || fail
   97         local oldbsize=$BSIZE
   98         BSIZE=$iobsize
   99         run_all $tfile
  100         BSIZE=$oldbsize
  101         umount $MNT_DIR
  102     done
  103 }
  104 
  105 trunc_file() {
  106     test $1 -eq 0 || truncate -c -s $1 $2 2>/dev/null || dd if=/dev/zero of=$2 bs=$1 count=1 2>/dev/null || fail "Failed to truncate test file $2."
  107 }
  108 
  109 RUN() {
  110     local _res=$1
  111     shift
  112     local _dev=$1
  113     shift
  114     local _fn=$1
  115     shift
  116     local _type="bdev"
  117     local _fsize=0
  118 
  119     test -b $_dev || {
  120         _type="file"
  121         _fsize=$(stat -c "%s" $_dev)
  122     }
  123 
  124     case "$_res" in
  125     P)
  126         MSG="Testing $_fn on $_type with params $@ [expecting TRUE]..."
  127         $BW_UNIT $_dev $_fn $@
  128         if [ $? -ne 0 ]; then
  129             if [ $_type = "file" ]; then
  130                 warn_count
  131             else
  132                 fail_count
  133             fi
  134             trunc_file $_fsize $_dev
  135             test -z "$STRACE" || $STRACE -o ./$BW_UNIT-fail-$FAILS-should-pass.log $BW_UNIT $_dev $_fn $@ 2> /dev/null
  136         else
  137             MSG="$MSG[OK]"
  138         fi
  139         ;;
  140     F)
  141         MSG="Testing $_fn on $_type with params $@ [expecting FALSE]..."
  142         $BW_UNIT $_dev $_fn $@ 2> /dev/null
  143         if [ $? -eq 0 ]; then
  144             if [ $_type = "file" ]; then
  145                 warn_count
  146             else
  147                 fail_count
  148             fi
  149             trunc_file $_fsize $_dev
  150             test -z "$STRACE" || $STRACE -o ./$BW_UNIT-fail-$FAILS-should-fail.log $BW_UNIT $_dev $_fn $@ 2> /dev/null
  151         else
  152             MSG="$MSG[OK]"
  153         fi
  154         ;;
  155     *)
  156         fail "Internal test error"
  157         ;;
  158     esac
  159 
  160     trunc_file $_fsize $_dev
  161 }
  162 
  163 run_all() {
  164     if [ -b "$1" ]; then
  165         BD_FAIL="F"
  166     else
  167         BD_FAIL="P"
  168     fi
  169 
  170     # buffer io support only blocksize aligned ios
  171     # device/file fn_name length
  172     RUN "P" $1 read_buffer $BSIZE
  173     RUN "P" $1 read_buffer $((2*BSIZE))
  174     RUN "F" $1 read_buffer $((BSIZE-1))
  175     RUN "F" $1 read_buffer $((BSIZE+1))
  176     RUN "P" $1 read_buffer 0
  177 
  178     RUN "P" $1 write_buffer $BSIZE
  179     RUN "P" $1 write_buffer $((2*BSIZE))
  180 
  181     RUN "F" $1 write_buffer $((BSIZE-1))
  182     RUN "F" $1 write_buffer $((BSIZE+1))
  183     RUN "F" $1 write_buffer 0
  184 
  185     # basic blockwise functions
  186     # device/file fn_name length bsize
  187     RUN "P" $1 read_blockwise 0 $BSIZE
  188     RUN "P" $1 read_blockwise $((BSIZE)) $BSIZE
  189     RUN "P" $1 read_blockwise $((BSIZE-1)) $BSIZE
  190     RUN "P" $1 read_blockwise $((BSIZE+1)) $BSIZE
  191     RUN "P" $1 read_blockwise $((DEVSIZE)) $BSIZE
  192     RUN "P" $1 read_blockwise $((DEVSIZE-1)) $BSIZE
  193     RUN "F" $1 read_blockwise $((DEVSIZE+1)) $BSIZE
  194 
  195     RUN "P" $1 write_blockwise 0 $BSIZE
  196     RUN "P" $1 write_blockwise $((BSIZE)) $BSIZE
  197     RUN "P" $1 write_blockwise $((BSIZE-1)) $BSIZE
  198     RUN "P" $1 write_blockwise $((BSIZE+1)) $BSIZE
  199     RUN "P" $1 write_blockwise $((DEVSIZE)) $BSIZE
  200     RUN "P" $1 write_blockwise $((DEVSIZE-1)) $BSIZE
  201     RUN "$BD_FAIL" $1 write_blockwise $((DEVSIZE+1)) $BSIZE
  202 
  203     # seek variant blockwise functions
  204     # device/file fn_name length bsize offset
  205     RUN "P" $1 read_lseek_blockwise 0 $BSIZE 0
  206     RUN "P" $1 read_lseek_blockwise 0 $BSIZE 1
  207     RUN "P" $1 read_lseek_blockwise 0 $BSIZE $((DEVSIZE))
  208     # length = 0 is significant here
  209     RUN "P" $1 read_lseek_blockwise 0 $BSIZE $((DEVSIZE+1))
  210 
  211     # beginning of device
  212     RUN "P" $1 read_lseek_blockwise 1 $BSIZE 0
  213     RUN "P" $1 read_lseek_blockwise 1 $BSIZE 1
  214     RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((BSIZE-1))
  215     RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((BSIZE/2))
  216 
  217     # somewhere in the 'middle'
  218     RUN "P" $1 read_lseek_blockwise 1 $BSIZE $BSIZE
  219     RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((BSIZE+1))
  220     RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((2*BSIZE-1))
  221     RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((BSIZE+BSIZE/2-1))
  222 
  223     # cross-sector tests
  224     RUN "P" $1 read_lseek_blockwise 2 $BSIZE $((BSIZE-1))
  225     RUN "P" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((BSIZE-1))
  226     RUN "P" $1 read_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE-1))
  227     RUN "P" $1 read_lseek_blockwise 2 $BSIZE $((2*BSIZE-1))
  228     RUN "P" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((2*BSIZE-1))
  229     RUN "P" $1 read_lseek_blockwise $((BSIZE+2)) $BSIZE $((2*BSIZE-1))
  230 
  231     # including one whole sector
  232     RUN "P" $1 read_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE))
  233     RUN "P" $1 read_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE+1))
  234     RUN "P" $1 read_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE-1))
  235     RUN "P" $1 read_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE-1))
  236     RUN "P" $1 read_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE+1))
  237     RUN "P" $1 read_lseek_blockwise $((3*BSIZE-2)) $BSIZE $((BSIZE+1))
  238 
  239     # hiting exactly the sector boundary
  240     RUN "P" $1 read_lseek_blockwise $((BSIZE-1)) $BSIZE 1
  241     RUN "P" $1 read_lseek_blockwise $((BSIZE-1)) $BSIZE $((BSIZE+1))
  242     RUN "P" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((BSIZE-1))
  243     RUN "P" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((2*BSIZE-1))
  244 
  245     # device end
  246     RUN "P" $1 read_lseek_blockwise 1 $BSIZE $((DEVSIZE-1))
  247     RUN "P" $1 read_lseek_blockwise $((BSIZE-1)) $BSIZE $((DEVSIZE-BSIZE+1))
  248     RUN "P" $1 read_lseek_blockwise $((BSIZE)) $BSIZE $((DEVSIZE-BSIZE))
  249     RUN "P" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((DEVSIZE-BSIZE-1))
  250 
  251     # this must fail on both device and file
  252     RUN "F" $1 read_lseek_blockwise 1 $BSIZE $((DEVSIZE))
  253     RUN "F" $1 read_lseek_blockwise $((BSIZE-1)) $BSIZE $((DEVSIZE-BSIZE+2))
  254     RUN "F" $1 read_lseek_blockwise $((BSIZE)) $BSIZE $((DEVSIZE-BSIZE+1))
  255     RUN "F" $1 read_lseek_blockwise $((BSIZE+1)) $BSIZE $((DEVSIZE-BSIZE))
  256 
  257     RUN "P" $1 write_lseek_blockwise 0 $BSIZE 0
  258     # TODO: this may pass but must not write a byte (write(0) is undefined).
  259     #   Test it with underlying dm-error or phony read/write syscalls.
  260     #   Skipping read is optimization.
  261     # HINT: currently it performs useless write and read as well
  262     RUN "P" $1 write_lseek_blockwise 0 $BSIZE 1
  263     RUN "P" $1 write_lseek_blockwise 0 $BSIZE $BSIZE
  264 
  265     # beginning of device
  266     RUN "P" $1 write_lseek_blockwise 1 $BSIZE 0
  267     RUN "P" $1 write_lseek_blockwise 1 $BSIZE 1
  268     RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((BSIZE-1))
  269     RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((BSIZE/2))
  270 
  271     # somewhere in the 'middle'
  272     RUN "P" $1 write_lseek_blockwise 1 $BSIZE $BSIZE
  273     RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((BSIZE+1))
  274     RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((2*BSIZE-1))
  275     RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((BSIZE+BSIZE/2-1))
  276 
  277     # cross-sector tests
  278     RUN "P" $1 write_lseek_blockwise 2 $BSIZE $((BSIZE-1))
  279     RUN "P" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((BSIZE-1))
  280     RUN "P" $1 write_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE-1))
  281     RUN "P" $1 write_lseek_blockwise 2 $BSIZE $((2*BSIZE-1))
  282     RUN "P" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((2*BSIZE-1))
  283     RUN "P" $1 write_lseek_blockwise $((BSIZE+2)) $BSIZE $((2*BSIZE-1))
  284 
  285     # including one whole sector
  286     RUN "P" $1 write_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE))
  287     RUN "P" $1 write_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE+1))
  288     RUN "P" $1 write_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE-1))
  289     RUN "P" $1 write_lseek_blockwise $((BSIZE+2)) $BSIZE $((BSIZE-1))
  290     RUN "P" $1 write_lseek_blockwise $((2*BSIZE)) $BSIZE $((BSIZE+1))
  291     RUN "P" $1 write_lseek_blockwise $((3*BSIZE-2)) $BSIZE $((BSIZE+1))
  292 
  293     # hiting exactly the sector boundary
  294     RUN "P" $1 write_lseek_blockwise $((BSIZE-1)) $BSIZE 1
  295     RUN "P" $1 write_lseek_blockwise $((BSIZE-1)) $BSIZE $((BSIZE+1))
  296     RUN "P" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((BSIZE-1))
  297     RUN "P" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((2*BSIZE-1))
  298 
  299     # device end
  300     RUN "P" $1 write_lseek_blockwise 1 $BSIZE $((DEVSIZE-1))
  301     RUN "P" $1 write_lseek_blockwise $((BSIZE-1)) $BSIZE $((DEVSIZE-BSIZE+1))
  302     RUN "P" $1 write_lseek_blockwise $((BSIZE)) $BSIZE $((DEVSIZE-BSIZE))
  303     RUN "P" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((DEVSIZE-BSIZE-1))
  304 
  305     # this must fail on device, but pass on file (which is unfortunate and maybe design mistake)
  306     RUN "$BD_FAIL" $1 write_lseek_blockwise 1 $BSIZE $((DEVSIZE))
  307     RUN "$BD_FAIL" $1 write_lseek_blockwise $((BSIZE-1)) $BSIZE $((DEVSIZE-BSIZE+2))
  308     RUN "$BD_FAIL" $1 write_lseek_blockwise $((BSIZE)) $BSIZE $((DEVSIZE-BSIZE+1))
  309     RUN "$BD_FAIL" $1 write_lseek_blockwise $((BSIZE+1)) $BSIZE $((DEVSIZE-BSIZE))
  310 }
  311 
  312 [ -n "$CRYPTSETUP_PATH" ] && skip "Cannot run this test with CRYPTSETUP_PATH set."
  313 
  314 which $STRACE > /dev/null 2>&1 || unset STRACE
  315 test -x $BW_UNIT || skip "Run \"make `basename $BW_UNIT`\" first"
  316 
  317 FAILS=0
  318 WARNS=0
  319 DEVSIZEMB=2
  320 DEVSIZE=$((DEVSIZEMB*1024*1024))
  321 
  322 PAGE_SIZE=$(getconf PAGE_SIZE)
  323 echo "System PAGE_SIZE=$PAGE_SIZE"
  324 
  325 echo "Run tests in local filesystem"
  326 falloc $DEVSIZEMB $LOCAL_FILE || fail "Failed to create file in local filesystem."
  327 BSIZE=$(stat -c "%o" $LOCAL_FILE)
  328 if [ $BSIZE -gt $((512*1024)) ]; then
  329     echo "Detected file block size: $BSIZE bytes"
  330     echo "Tuning it down to system page size ($PAGE_SIZE bytes)"
  331     BSIZE=$PAGE_SIZE
  332 fi
  333 run_all $LOCAL_FILE
  334 
  335 [ $(id -u) -eq 0 ] || {
  336     echo "WARNING: You must be root to run remaining tests."
  337     test $FAILS -eq 0 || fail "($FAILS wrong result(s) in total)"
  338     cleanup
  339     exit 0
  340 }
  341 
  342 DEVBSIZE=512
  343 BSIZE=$DEVBSIZE
  344 EXP=0
  345 DEVSIZEMBIMG=32
  346 
  347 echo "# Create classic 512B drive"
  348 echo "# (logical_block_size=$DEVBSIZE, physical_block_size=$((DEVBSIZE*(1<<EXP))))"
  349 add_device dev_size_mb=$DEVSIZEMB sector_size=$DEVBSIZE physblk_exp=$EXP num_tgts=1
  350 run_all $DEV
  351 cleanup
  352 add_device dev_size_mb=$DEVSIZEMBIMG sector_size=$DEVBSIZE physblk_exp=$EXP num_tgts=1
  353 run_all_in_fs
  354 cleanup
  355 
  356 EXP=3
  357 echo "# Create desktop-class 4K drive"
  358 echo "# (logical_block_size=$DEVBSIZE, physical_block_size=$((DEVBSIZE*(1<<EXP))))"
  359 add_device dev_size_mb=$DEVSIZEMB physblk_exp=$EXP sector_size=$DEVBSIZE num_tgts=1
  360 run_all $DEV
  361 BSIZE=$((DEVBSIZE*(1<<EXP)))
  362 run_all $DEV
  363 cleanup
  364 
  365 add_device dev_size_mb=$DEVSIZEMBIMG physblk_exp=$EXP sector_size=$DEVBSIZE num_tgts=1
  366 run_all_in_fs
  367 cleanup
  368 
  369 DEVBSIZE=4096
  370 BSIZE=$DEVBSIZE
  371 EXP=0
  372 echo "# Create enterprise-class 4K drive"
  373 echo "# (logical_block_size=$DEVBSIZE, physical_block_size=$((DEVBSIZE*(1<<EXP))))"
  374 add_device dev_size_mb=$DEVSIZEMB physblk_exp=$EXP sector_size=$DEVBSIZE num_tgts=1
  375 run_all $DEV
  376 cleanup
  377 add_device dev_size_mb=$DEVSIZEMBIMG sector_size=$DEVBSIZE physblk_exp=$EXP num_tgts=1
  378 run_all_in_fs
  379 cleanup
  380 
  381 test $WARNS -eq 0 || echo "(WARNING: $WARNS suspicious result(s) in total)"
  382 test $FAILS -eq 0 || fail "($FAILS wrong result(s) in total)"