common/rc: generalize _get_filesize()
[xfstests-dev.git] / tests / generic / 399
index 8f5fcdc6dfba87897fed182bfee1cf3bb9f83960..a9b3997ae59e98058e40b576ac371b55445ee231 100755 (executable)
@@ -1,4 +1,7 @@
 #! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2016 Google, Inc.  All Rights Reserved.
+#
 # FS QA Test generic/399
 #
 # Check for weaknesses in filesystem encryption involving the same ciphertext
 # initialization vector reuse bug fixed in commit 02fc59a0d28f ("f2fs/crypto:
 # fix xts_tweak initialization").
 #
-#-----------------------------------------------------------------------
-# Copyright (c) 2016 Google, Inc.  All Rights Reserved.
-#
-# Author: Eric Biggers <ebiggers@google.com>
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU General Public License as
-# published by the Free Software Foundation.
-#
-# This program is distributed in the hope that it would be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write the Free Software Foundation,
-# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
-#-----------------------------------------------------------------------
-#
-
 seq=`basename $0`
 seqres=$RESULT_DIR/$seq
 echo "QA output created by $seq"
@@ -60,7 +43,6 @@ rm -f $seqres.full
 _supported_fs generic
 _supported_os Linux
 _require_scratch_encryption
-_require_xfs_io_command "set_encpolicy"
 _require_command "$XZ_PROG" xz
 _require_command "$KEYCTL_PROG" keyctl
 
@@ -76,13 +58,12 @@ fs_size_in_mb=64
 fs_size=$((fs_size_in_mb * 1024 * 1024))
 dd if=/dev/zero of=$SCRATCH_DEV bs=$((1024 * 1024)) \
        count=$fs_size_in_mb &>> $seqres.full
-MKFS_OPTIONS="$MKFS_OPTIONS -O encrypt" \
-       _scratch_mkfs_sized $fs_size &>> $seqres.full
+_scratch_mkfs_sized_encrypted $fs_size &>> $seqres.full
 _scratch_mount
 
-keydesc=$(_generate_encryption_key)
+keydesc=$(_generate_session_encryption_key)
 mkdir $SCRATCH_MNT/encrypted_dir
-$XFS_IO_PROG -c "set_encpolicy $keydesc" $SCRATCH_MNT/encrypted_dir
+_set_encpolicy $SCRATCH_MNT/encrypted_dir $keydesc
 
 # Create the "same" symlink in two different directories.
 # Later we'll check both the name and target of the symlink.
@@ -101,28 +82,38 @@ total_file_size=0
 i=1
 while true; do
        file=$SCRATCH_MNT/encrypted_dir/file$i
-       if ! $XFS_IO_PROG -f $file -c 'pwrite 0 1M' &> $tmp.out; then
-               if ! grep -q 'No space left on device' $tmp.out; then
-                       echo "FAIL: unexpected pwrite failure"
-                       cat $tmp.out
-               elif [ -e $file ]; then
-                       total_file_size=$((total_file_size + $(stat -c %s $file)))
-               fi
-               break
+
+       $XFS_IO_PROG -f $file -c 'pwrite 0 1M' &> $tmp.out
+       echo "Writing $file..." >> $seqres.full
+       cat $tmp.out >> $seqres.full
+
+       file_size=0
+       if [ -e $file ]; then
+               file_size=$(_get_filesize $file)
        fi
-       total_file_size=$((total_file_size + $(stat -c %s $file)))
-       i=$((i + 1))
-       if [ $i -gt $fs_size_in_mb ]; then
-               echo "FAIL: filesystem never filled up!"
+
+       # We shouldn't have been able to write more data than we had space for.
+       (( total_file_size += file_size ))
+       if (( total_file_size > fs_size )); then
+               _fail "Wrote $total_file_size bytes but should have only" \
+                     "had space for $fs_size bytes at most!"
+       fi
+
+       # Stop if we hit ENOSPC.
+       if grep -q 'No space left on device' $tmp.out; then
                break
        fi
-done
 
-# We shouldn't have been able to write more data than we had space for.
-if (( $total_file_size > $fs_size )); then
-       echo "FAIL: wrote $total_file_size bytes but should have only" \
-               "had space for $fs_size bytes at most"
-fi
+       # Otherwise the file should have been successfully created.
+       if [ ! -e $file ]; then
+               _fail "$file failed to be created, but the fs isn't out of space yet!"
+       fi
+       if (( file_size != 1024 * 1024 )); then
+               _fail "Size of $file is wrong (possible write error?)." \
+                     "Got $file_size, expected 1 MiB"
+       fi
+       (( i++ ))
+done
 
 #
 # Unmount the filesystem and compute its compressed size.  It must be no smaller
@@ -136,7 +127,7 @@ fi
 # memory than the '-9' preset.  The memory needed with our settings will be
 # 64 * 6.5 = 416 MB; see xz(1).
 #
-_unlink_encryption_key $keydesc
+_unlink_session_encryption_key $keydesc
 _scratch_unmount
 fs_compressed_size=$(head -c $fs_size $SCRATCH_DEV | \
        xz --lzma2=dict=64M,mf=hc4,mode=fast,nice=16 | \