]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: (fallocate) --insert-range test
authorChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Fri, 1 May 2026 22:25:21 +0000 (18:25 -0400)
committerChristian Goeschel Ndjomouo <cgoesc2@wgu.edu>
Tue, 5 May 2026 13:53:27 +0000 (09:53 -0400)
Signed-off-by: Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
tests/expected/fallocate/insert-range [new file with mode: 0644]
tests/ts/fallocate/insert-range [new file with mode: 0755]

diff --git a/tests/expected/fallocate/insert-range b/tests/expected/fallocate/insert-range
new file mode 100644 (file)
index 0000000..15f3824
--- /dev/null
@@ -0,0 +1,7 @@
+aa aa aa aa aa aa aa aa  aa aa aa aa aa aa aa aa  |................|
+*
+00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
+*
+ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  |................|
+*
+
diff --git a/tests/ts/fallocate/insert-range b/tests/ts/fallocate/insert-range
new file mode 100755 (executable)
index 0000000..36a28e0
--- /dev/null
@@ -0,0 +1,69 @@
+#!/usr/bin/env bash
+
+# This file is part of util-linux.
+#
+# This file 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; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This file is distributed in the hope that it will 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.
+#
+# Copyright (C) 2026 Christian Goeschel Ndjomouo <cgoesc2@wgu.edu>
+
+TS_TOPDIR="${0%/*}/../.."
+TS_DESC="insert-range"
+
+. "$TS_TOPDIR"/functions.sh
+ts_init "$*"
+
+ts_check_test_command "$TS_CMD_FALLOCATE"
+ts_check_test_command "$TS_CMD_HEXDUMP"
+ts_check_test_command "$TS_CMD_FINDMNT"
+ts_check_prog "stat"
+
+TEST_FILE="${TS_OUTDIR}/${TS_TESTNAME}.data"
+# We have to properly align the different data blocks
+# to the logical filesystem block size otherwise fallocate
+# will fail with EINVAL.
+BLKSIZE="$(stat --file-system --format=%s "$TS_OUTDIR")"
+
+# The two patterns are used to create distinct data blocks of size $BLKSIZE
+# and will help to verify the data block shift.
+#
+# Our test file's data block patterns will look like this:
+#
+#           bs bytes   bs bytes
+#       |aaaaaaaa...|ffffffff...|
+# insert here ------^
+# 
+# to obtain this
+#   bs bytes    bs bytes    bs bytes
+# |aaaaaaaa...|00000000...|ffffffff...|
+#
+{
+    printf '%*s' "$BLKSIZE" '' | tr ' ' '\252'
+    printf '%*s' "$BLKSIZE" '' | tr ' ' '\377'
+} >"$TEST_FILE"
+
+# After this command the block with 'ff' should be shifted
+# $BLKSIZE 
+"$TS_CMD_FALLOCATE" --insert-range --offset "$BLKSIZE" --length "$BLKSIZE" \
+        "$TEST_FILE" >>"$TS_OUTPUT" 2>>"$TS_ERRLOG"
+
+size_after="$(stat --format=%s "$TEST_FILE")"
+
+if (( size_after != (BLKSIZE * 3) )); then
+    rm -f "$TEST_FILE"
+    ts_failed "file not properly resized (size: $size_after, block size: $BLKSIZE)"
+fi
+
+"$TS_CMD_HEXDUMP" --canonical "$TEST_FILE" | sed -e 's/^[[:alnum:]]*[[:space:]]*//g' \
+        >>"$TS_OUTPUT" 2>>"$TS_ERRLOG"
+
+rm -f "$TEST_FILE"
+
+ts_finalize