osi-tools/test/osi-mount.bats

116 lines
3.5 KiB
Bash

# Copyright (C) 2024 Umorpha Systems
# SPDX-License-Identifier: AGPL-3.0-or-later
bats_require_minimum_version 1.7.0
bats_load_library bats-support
bats_load_library bats-assert
bats_load_library bats-file
teardown() {
if [ -n "${tmpdir:-}" ]; then
temp_del "$tmpdir"
fi
}
_sudo() {
sudo ${TMPDIR:+TMPDIR="$TMPDIR"} "$@"
}
@test "osi-mount needs arguments" {
LC_ALL=C run --separate-stderr osi-mount
assert_failure 2
assert_equal "$output" ''
assert [ ${#stderr_lines[@]} -gt 1 ]
assert_regex "${stderr_lines[-1]}" "^Try .*osi-mount --help' for more information\$"
}
@test "osi-mount shows help" {
LC_ALL=C run --separate-stderr osi-mount --help
assert_success
assert_equal "$stderr" ''
assert_regex "${lines[0]}" '^Usage: sudo osi-mount '
LC_ALL=C run --separate-stderr osi-mount -h
assert_success
assert_equal "$stderr" ''
assert_regex "${lines[0]}" '^Usage: sudo osi-mount'
}
@test "osi-mount shows version" {
LC_ALL=C run --separate-stderr osi-mount --version
assert_success
assert_equal "$stderr" ''
assert_regex "${lines[0]}" '^osi-mount \(osi-tools\) '
LC_ALL=C run --separate-stderr osi-mount -V
assert_success
assert_equal "$stderr" ''
assert_regex "${lines[0]}" '^osi-mount \(osi-tools\) '
}
@test "osi-mount --user" {
tmpdir="$(temp_make)"
truncate --size=100m "$tmpdir/tst.img"
mkfs.ext4 "$tmpdir/tst.img"
mkdir "$tmpdir/mnt"
echo 'Checking that it uses the current directory...'
run --separate-stderr _sudo osi-mount --user "$tmpdir/tst.img" "$tmpdir/mnt" pwd
assert_success
assert_equal "${output%$'\r'}" "$PWD"
assert_equal "$stderr" ''
echo 'Checking that it respects TMPDIR...'
mkdir "$tmpdir/tmp"
TMPDIR="$tmpdir/tmp" _sudo osi-mount --user "$tmpdir/tst.img" "$tmpdir/mnt" env >"$tmpdir/env.txt"
act=$(grep ^TMPDIR= "$tmpdir/env.txt")
assert_equal "${act%$'\r'}" "TMPDIR=$tmpdir/tmp"
echo 'Checking that it can write outside of the mountpoint...'
run --separate-stderr _sudo osi-mount --user "$tmpdir/tst.img" "$tmpdir/mnt" -- sh -c "echo foo>$tmpdir/x"
assert_success
assert_file_contains "$tmpdir/x" foo
assert_file_owner "$USER" "$tmpdir/x"
echo "Checking that it can't write inside of the mountpoint..."
run --separate-stderr _sudo osi-mount --user "$tmpdir/tst.img" "$tmpdir/mnt" -- sh -c "echo bar>$tmpdir/mnt/y"
assert_failure
}
@test "osi-mount --root" {
tmpdir="$(temp_make)"
truncate --size=100m "$tmpdir/tst.img"
mkfs.ext4 "$tmpdir/tst.img"
mkdir "$tmpdir/mnt"
echo 'Checking that it uses the current directory...'
run --separate-stderr _sudo osi-mount --root "$tmpdir/tst.img" "$tmpdir/mnt" pwd
assert_success
assert_equal "${output%$'\r'}" "$PWD"
assert_equal "$stderr" ''
echo 'Checking that it respects TMPDIR...'
mkdir "$tmpdir/tmp"
TMPDIR="$tmpdir/tmp" _sudo osi-mount --root "$tmpdir/tst.img" "$tmpdir/mnt" env >"$tmpdir/env.txt"
act=$(grep ^TMPDIR= "$tmpdir/env.txt")
assert_equal "${act%$'\r'}" "TMPDIR=$tmpdir/tmp"
echo "Checking that it can't write outside of the mountpoint..."
run --separate-stderr _sudo osi-mount --root "$tmpdir/tst.img" "$tmpdir/mnt" -- sh -c "echo foo>$tmpdir/x"
if [ -f "$tmpdir/x" ]; then
sudo rm -f -- "$tmpdir/x"
fi
assert_failure
assert_file_not_exists "$tmpdir/x"
echo 'Checking that it can write inside of the mountpoint...'
run --separate-stderr _sudo osi-mount --root "$tmpdir/tst.img" "$tmpdir/mnt" -- sh -c "echo bar>$tmpdir/mnt/y"
assert_success
assert_file_not_exists "$tmpdir/mnt/y"
run --separate-stderr _sudo osi-mount --user "$tmpdir/tst.img" "$tmpdir/mnt" -- cat "$tmpdir/mnt/y"
assert_success
assert_equal "${output%$'\r'}" 'bar'
}