| 1 |
#!/bin/bash |
| 2 |
# |
| 3 |
# run.sh - run the fscrypt command-line interface tests |
| 4 |
# |
| 5 |
# Copyright 2020 Google LLC |
| 6 |
# |
| 7 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 8 |
# use this file except in compliance with the License. You may obtain a copy of |
| 9 |
# the License at |
| 10 |
# |
| 11 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 |
# |
| 13 |
# Unless required by applicable law or agreed to in writing, software |
| 14 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 |
# License for the specific language governing permissions and limitations under |
| 17 |
# the License. |
| 18 |
# |
| 19 |
|
| 20 |
# Use extra-strict mode. |
| 21 |
set -e -u -o pipefail |
| 22 |
|
| 23 |
# Ensure we're in the cli-tests/ directory. |
| 24 |
cd "$(dirname "$0")" |
| 25 |
|
| 26 |
# Names of the test devices. |
| 27 |
# Variables with these names are exported to the tests. |
| 28 |
DEVICES=(DEV DEV_ROOT) |
| 29 |
|
| 30 |
# Names of the mountpoint of each test device. |
| 31 |
# Variables with these names are exported to the tests. |
| 32 |
MOUNTS=(MNT MNT_ROOT) |
| 33 |
|
| 34 |
# Name of the test user. This user will be created and deleted by this script. |
| 35 |
# This variable is exported to the tests. |
| 36 |
TEST_USER=fscrypt-test-user |
| 37 |
|
| 38 |
# The temporary directory to use. |
| 39 |
# This variable is exported to the tests. |
| 40 |
TMPDIR=$(mktemp -d /tmp/fscrypt.XXXXXX) |
| 41 |
|
| 42 |
# The loopback devices that correspond to each test device. |
| 43 |
LOOPS=() |
| 44 |
|
| 45 |
# Update the expected output files to match the actual output? |
| 46 |
UPDATE_OUTPUT=false |
| 47 |
|
| 48 |
LONGOPTS_ARRAY=( |
| 49 |
'help' |
| 50 |
'update-output' |
| 51 |
) |
| 52 |
LONGOPTS=$(echo "${LONGOPTS_ARRAY[*]}" | tr ' ' ,) |
| 53 |
|
| 54 |
cleanup() |
| 55 |
{ |
| 56 |
local mnt loop |
| 57 |
|
| 58 |
# Unmount all the test filesystems. |
| 59 |
for mnt in "${MOUNTS[@]}"; do |
| 60 |
mnt="$TMPDIR/$mnt" |
| 61 |
if mountpoint "$mnt" &> /dev/null; then |
| 62 |
umount "$mnt" |
| 63 |
fi |
| 64 |
done |
| 65 |
|
| 66 |
# Delete the loopback device of each test device. |
| 67 |
for loop in "${LOOPS[@]}"; do |
| 68 |
losetup -d "$loop" |
| 69 |
done |
| 70 |
|
| 71 |
# Delete all temporary files. |
| 72 |
rm -rf "${TMPDIR:?}"/* |
| 73 |
} |
| 74 |
|
| 75 |
cleanup_full() |
| 76 |
{ |
| 77 |
cleanup |
| 78 |
rm -rf "$TMPDIR" |
| 79 |
userdel "$TEST_USER" || true |
| 80 |
} |
| 81 |
|
| 82 |
# Filters the output of the test script to make the output consistent on every |
| 83 |
# run of the test. For example, references to the mountpoint like |
| 84 |
# /tmp/fscrypt.4OTb6y/MNT will be replaced with simply MNT, since the name of |
| 85 |
# the temporary directory is different every time. |
| 86 |
filter_test_output() |
| 87 |
{ |
| 88 |
local sedscript="" |
| 89 |
local raw_output=$TMPDIR/raw-test-output |
| 90 |
local i |
| 91 |
|
| 92 |
cat > "$raw_output" |
| 93 |
|
| 94 |
# Filter mountpoint and device names. |
| 95 |
for i in "${!DEVICES[@]}"; do |
| 96 |
sedscript+="s@$TMPDIR/${MOUNTS[$i]}@${MOUNTS[$i]}@g;" |
| 97 |
sedscript+="s@${LOOPS[$i]}@${DEVICES[$i]}@g;" |
| 98 |
done |
| 99 |
|
| 100 |
# Filter the path to fscrypt.conf. |
| 101 |
sedscript+="s@$FSCRYPT_CONF@FSCRYPT_CONF@g;" |
| 102 |
|
| 103 |
# Filter policy and protector descriptors. |
| 104 |
sedscript+=$(grep -E -o '\<([a-f0-9]{16})|([a-f0-9]{32})\>' \ |
| 105 |
"$raw_output" \ |
| 106 |
| awk '{ printf "s@\\<" $1 "\\>@desc" NR "@g;" }') |
| 107 |
|
| 108 |
# Filter any other paths in TMPDIR. |
| 109 |
sedscript+="s@$TMPDIR@TMPDIR@g;" |
| 110 |
|
| 111 |
# At some point, 'bash -c COMMAND' started showing error messages as |
| 112 |
# "bash: line 1: " instead of just "bash: ". Filter out the "line 1: ". |
| 113 |
sedscript+="s@^bash: line 1: @bash: @;" |
| 114 |
|
| 115 |
# Work around protojson whitespace randomization. |
| 116 |
sedscript+="/^Options: /s@ @ @g;" |
| 117 |
sedscript+="s@^Options: @Options: @;" |
| 118 |
|
| 119 |
sed -e "$sedscript" "$raw_output" |
| 120 |
} |
| 121 |
|
| 122 |
# Prepares to run a test script. |
| 123 |
setup_for_test() |
| 124 |
{ |
| 125 |
local i dev_var mnt_var img mnt loop |
| 126 |
|
| 127 |
# Start with a clean state. |
| 128 |
cleanup |
| 129 |
|
| 130 |
# ../bin/fscrypt might not be accessible to $TEST_USER. Copy it into |
| 131 |
# $TMPDIR so that $TEST_USER is guaranteed to have access to it. |
| 132 |
mkdir "$TMPDIR/bin" |
| 133 |
cp ../bin/fscrypt "$TMPDIR/bin/" |
| 134 |
chmod 755 "$TMPDIR" "$TMPDIR/bin" "$TMPDIR/bin/fscrypt" |
| 135 |
|
| 136 |
# Create the test filesystems and mountpoints. |
| 137 |
LOOPS=() |
| 138 |
for i in "${!DEVICES[@]}"; do |
| 139 |
dev_var=${DEVICES[$i]} |
| 140 |
mnt_var=${MOUNTS[$i]} |
| 141 |
img="$TMPDIR/$dev_var" |
| 142 |
if ! mkfs.ext4 -O encrypt -F -b 4096 -I 256 "$img" $((1<<15)) \ |
| 143 |
&> "$TMPDIR/mkfs.out" |
| 144 |
then |
| 145 |
cat 1>&2 "$TMPDIR/mkfs.out" |
| 146 |
exit 1 |
| 147 |
fi |
| 148 |
loop=$(losetup --find --show "$img") |
| 149 |
LOOPS+=("$loop") |
| 150 |
export "$dev_var=$loop" |
| 151 |
mnt="$TMPDIR/$mnt_var" |
| 152 |
export "$mnt_var=$mnt" |
| 153 |
mkdir -p "$mnt" |
| 154 |
mount "$loop" "$mnt" |
| 155 |
done |
| 156 |
|
| 157 |
# Give the tests their own "root" mount for storing login protectors, so |
| 158 |
# they don't use the real "/". |
| 159 |
export FSCRYPT_ROOT_MNT="$MNT_ROOT" |
| 160 |
|
| 161 |
# Enable consistent output mode. |
| 162 |
export FSCRYPT_CONSISTENT_OUTPUT="1" |
| 163 |
|
| 164 |
# Give the tests their own fscrypt.conf. |
| 165 |
export FSCRYPT_CONF="$TMPDIR/fscrypt.conf" |
| 166 |
fscrypt setup --time=1ms --quiet --all-users > /dev/null |
| 167 |
|
| 168 |
# The tests assume kernel support for v2 policies. |
| 169 |
if ! grep -E -q '"policy_version": +"2"' "$FSCRYPT_CONF"; then |
| 170 |
cat 1>&2 << EOF |
| 171 |
ERROR: Can't run these tests because your kernel doesn't support v2 policies. |
| 172 |
You need kernel v5.4 or later. |
| 173 |
EOF |
| 174 |
exit 1 |
| 175 |
fi |
| 176 |
|
| 177 |
# Set up the test filesystems that aren't already set up. |
| 178 |
fscrypt setup --quiet --all-users "$MNT" > /dev/null |
| 179 |
} |
| 180 |
|
| 181 |
run_test() |
| 182 |
{ |
| 183 |
local t=$1 |
| 184 |
|
| 185 |
# Run the test script. |
| 186 |
set +e |
| 187 |
"./$1.sh" |& filter_test_output > "$t.out.actual" |
| 188 |
status=${PIPESTATUS[0]} |
| 189 |
set -e |
| 190 |
|
| 191 |
# Check for failure status. |
| 192 |
if [ "$status" != 0 ]; then |
| 193 |
echo 1>&2 "FAILED: $t [exited with failure status $status]" |
| 194 |
if [ -s "$t.out.actual" ]; then |
| 195 |
if (( $(wc -l "$t.out.actual" | cut -f1 -d' ') > 10 )); then |
| 196 |
echo 1>&2 "Last 10 lines of test output:" |
| 197 |
tail -n10 "$t.out.actual" | sed 1>&2 's/^/ /' |
| 198 |
echo 1>&2 |
| 199 |
echo 1>&2 "See $t.out.actual for the full output." |
| 200 |
else |
| 201 |
echo 1>&2 "Test output:" |
| 202 |
sed 1>&2 's/^/ /' < "$t.out.actual" |
| 203 |
fi |
| 204 |
fi |
| 205 |
exit 1 |
| 206 |
fi |
| 207 |
|
| 208 |
# Check for output mismatch. |
| 209 |
if ! cmp "$t.out" "$t.out.actual" &> /dev/null; then |
| 210 |
if $UPDATE_OUTPUT; then |
| 211 |
cp "$t.out.actual" "$t.out" |
| 212 |
echo "Updated $t.out" |
| 213 |
else |
| 214 |
echo 1>&2 "FAILED: $t [output mismatch]" |
| 215 |
echo 1>&2 "Differences between $t.out and $t.out.actual:" |
| 216 |
echo 1>&2 |
| 217 |
diff 1>&2 "$t.out" "$t.out.actual" |
| 218 |
exit 1 |
| 219 |
fi |
| 220 |
fi |
| 221 |
rm -f "$t.out.actual" |
| 222 |
} |
| 223 |
|
| 224 |
usage() |
| 225 |
{ |
| 226 |
cat << EOF |
| 227 |
Usage: run.sh [--update-output] [TEST_SCRIPT_NAME]..." |
| 228 |
EOF |
| 229 |
exit 1 |
| 230 |
} |
| 231 |
|
| 232 |
if ! options=$(getopt -o "" -l "$LONGOPTS" -- "$@"); then |
| 233 |
usage |
| 234 |
fi |
| 235 |
eval set -- "$options" |
| 236 |
while (( $# >= 1 )); do |
| 237 |
case "$1" in |
| 238 |
--update-output) |
| 239 |
UPDATE_OUTPUT=true |
| 240 |
;; |
| 241 |
--) |
| 242 |
shift |
| 243 |
break |
| 244 |
;; |
| 245 |
--help|*) |
| 246 |
usage |
| 247 |
;; |
| 248 |
esac |
| 249 |
shift |
| 250 |
done |
| 251 |
|
| 252 |
if [ "$(id -u)" != 0 ]; then |
| 253 |
echo 1>&2 "ERROR: You must be root to run these tests." |
| 254 |
exit 1 |
| 255 |
fi |
| 256 |
|
| 257 |
# Check for prerequisites. |
| 258 |
PREREQ_CMDS=(mkfs.ext4 expect keyctl) |
| 259 |
PREREQ_PKGS=(e2fsprogs expect keyutils) |
| 260 |
for i in ${!PREREQ_CMDS[*]}; do |
| 261 |
if ! type -P "${PREREQ_CMDS[$i]}" > /dev/null; then |
| 262 |
cat 1>&2 << EOF |
| 263 |
ERROR: You must install the '${PREREQ_PKGS[$i]}' package to run these tests. |
| 264 |
Try a command like 'sudo apt-get install ${PREREQ_PKGS[$i]}'. |
| 265 |
EOF |
| 266 |
exit 1 |
| 267 |
fi |
| 268 |
done |
| 269 |
|
| 270 |
# Use a consistent umask. |
| 271 |
umask 022 |
| 272 |
|
| 273 |
# Use a consistent locale, to prevent output mismatches. |
| 274 |
export LANG=C |
| 275 |
export LC_ALL=C |
| 276 |
|
| 277 |
# Always cleanup fully on exit. |
| 278 |
trap cleanup_full EXIT |
| 279 |
|
| 280 |
# Create a test user, so that we can test non-root use of fscrypt. Give them a |
| 281 |
# password, so that we can test creating login passphrase protected directories. |
| 282 |
userdel "$TEST_USER" &> /dev/null || true |
| 283 |
useradd "$TEST_USER" |
| 284 |
echo "$TEST_USER:TEST_USER_PASS" | chpasswd |
| 285 |
export TEST_USER |
| 286 |
|
| 287 |
# Let the tests use $TMPDIR if they need it. |
| 288 |
export TMPDIR |
| 289 |
|
| 290 |
# Make it so that running 'fscrypt' in the tests runs the correct binary. |
| 291 |
export PATH="$TMPDIR/bin:$PATH" |
| 292 |
|
| 293 |
if (( $# >= 1 )); then |
| 294 |
# Tests specified on command line. |
| 295 |
tests=("$@") |
| 296 |
else |
| 297 |
# No tests specified on command line. Just run everything. |
| 298 |
tests=(t_*.sh) |
| 299 |
fi |
| 300 |
for t in "${tests[@]}"; do |
| 301 |
t=${t%.sh} |
| 302 |
echo "Running $t" |
| 303 |
setup_for_test |
| 304 |
run_test "$t" |
| 305 |
done |
| 306 |
|
| 307 |
echo "All tests passed!" |