diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -794,6 +794,8 @@ .. stripe .. + union + .. uzip etalon .. diff --git a/tests/sys/geom/class/Makefile b/tests/sys/geom/class/Makefile --- a/tests/sys/geom/class/Makefile +++ b/tests/sys/geom/class/Makefile @@ -18,6 +18,7 @@ TESTS_SUBDIRS+= raid3 TESTS_SUBDIRS+= shsec TESTS_SUBDIRS+= stripe +TESTS_SUBDIRS+= union TESTS_SUBDIRS+= uzip ${PACKAGE}FILES+= geom_subr.sh diff --git a/tests/sys/geom/class/union/Makefile b/tests/sys/geom/class/union/Makefile new file mode 100644 --- /dev/null +++ b/tests/sys/geom/class/union/Makefile @@ -0,0 +1,9 @@ +PACKAGE= tests + +TESTSDIR= ${TESTSBASE}/sys/geom/class/${.CURDIR:T} + +ATF_TESTS_SH+= union_test + +${PACKAGE}FILES+= conf.sh + +.include diff --git a/tests/sys/geom/class/union/conf.sh b/tests/sys/geom/class/union/conf.sh new file mode 100644 --- /dev/null +++ b/tests/sys/geom/class/union/conf.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +class="union" +base=$(atf_get ident) + +attach_md() +{ + local test_md + + test_md=$(mdconfig -a "$@") || atf_fail "failed to allocate md(4)" + echo $test_md >> $TEST_MDS_FILE || exit + echo $test_md +} + +gunion_test_cleanup() +{ + if mount | grep -q "/gunion"; then + umount gunion + fi + if mount | grep -q "/writable_device"; then + umount writable_device + fi + if mount | grep -q "/readable_device"; then + umount readable_device + fi + + if [ -e "gunion_device" ]; then + gunion destroy "$(cat gunion_device)" + fi + + geom_test_cleanup + + gunion unload +} + +gunion_test_setup() +{ + geom_atf_test_setup +} + +ATF_TEST=true +. `dirname $0`/../geom_subr.sh diff --git a/tests/sys/geom/class/union/union_test.sh b/tests/sys/geom/class/union/union_test.sh new file mode 100644 --- /dev/null +++ b/tests/sys/geom/class/union/union_test.sh @@ -0,0 +1,287 @@ +# +# SPDX-License-Identifier: BSD-2-Clause +# +# Copyright (c) 2023 The FreeBSD Foundation +# +# This software was developed1 by Yan-Hao Wang +# under sponsorship from the FreeBSD Foundation. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND +# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +# SUCH DAMAGE +# + +. $(atf_get_srcdir)/conf.sh + +atf_test_case create cleanup +create_head() +{ + atf_set "descr" "Test gunion create and destroy" +} +create_body() +{ + gunion_test_setup + + writable_device_name="$(attach_md -s 1m)" + readable_device_name="$(attach_md -s 1m)" + newfs -U "/dev/${readable_device_name}" + + atf_check gunion create "$writable_device_name" "$readable_device_name" + gunion_device="${writable_device_name}-${readable_device_name}.union" + atf_check -o inline:"/dev/${gunion_device}\n" ls "/dev/${gunion_device}" + + atf_check -o ignore fsck -p -f "/dev/${gunion_device}" + atf_check gunion destroy "${gunion_device}" + atf_check -s not-exit:0 -o ignore -e ignore ls "/dev/${gunion_device}" +} +create_cleanup() +{ + gunion_test_cleanup +} + +atf_test_case commit cleanup +commit_head() +{ + atf_set "descr" "Test basic gunion commit with no argument" +} +commit_body() +{ + gunion_test_setup + + writable_device_name="$(attach_md -s 1m)" + readable_device_name="$(attach_md -s 1m)" + newfs -U "/dev/${readable_device_name}" + mkdir readable_device + mkdir writable_device + mkdir gunion + + mount "/dev/${readable_device_name}" readable_device + echo "I am readable file" > readable_file + cp readable_file readable_device/readable_file + sync + umount readable_device + + gunion create "$writable_device_name" "$readable_device_name" + gunion_device="${writable_device_name}-${readable_device_name}.union" + mount "/dev/${gunion_device}" gunion + checksum gunion/readable_file readable_file + + echo "I am gunion file" > gunion_file + cp gunion_file gunion/gunion_file + sync + umount gunion + atf_check gunion commit "${gunion_device}" + gunion destroy "${gunion_device}" + + atf_check -o ignore fsck -p -f "/dev/${readable_device_name}" + mount "/dev/${writable_device_name}" writable_device + mount "/dev/${readable_device_name}" readable_device + checksum readable_device/readable_file readable_file + checksum writable_device/gunion_file gunion_file + checksum readable_device/gunion_file gunion_file +} +commit_cleanup() +{ + gunion_test_cleanup +} + +atf_test_case offset cleanup +offset_head() +{ + atf_set "descr" "Test gunion command with -o offset argument" + +} +offset_body() +{ + gunion_test_setup + + writable_device_name="$(attach_md -s 1m)" + readable_device_name="$(attach_md -s 1m)" + gpart create -s GPT "/dev/${readable_device_name}" + gpart add -t freebsd-ufs "${readable_device_name}" + newfs "/dev/${readable_device_name}p1" + gpt_entry_1=$(gpart show "/dev/${readable_device_name}") + mkdir gunion + + secsize="$(diskinfo "/dev/${readable_device_name}" | awk '{print $2}')" + gpt_sector_count="$(gpart show /dev/"${readable_device_name}" | awk 'NR==1 {print $2}')" + gpt_size="$((secsize * gpt_sector_count))" + + gunion create -o "$gpt_size" "$writable_device_name" "$readable_device_name" + gunion_device="${writable_device_name}-${readable_device_name}.union" + echo "$gunion_device" > gunion_device + + atf_check -o ignore fsck -p -f "/dev/${gunion_device}" + atf_check mount "/dev/${gunion_device}" gunion + umount gunion + gunion destroy "${gunion_device}" + + gpt_entry_2=$(gpart show "/dev/${readable_device_name}") + atf_check_equal "${gpt_entry_1}" "${gpt_entry_2}" +} +offset_cleanup() +{ + gunion_test_cleanup +} + +atf_test_case size cleanup +size_head() +{ + atf_set "descr" "Test gunion command with -s size argument" +} +size_body() +{ + gunion_test_setup + + writable_device_name="$(attach_md -s 2m)" + readable_device_name="$(attach_md -s 1m)" + newfs -U "/dev/${readable_device_name}" + + gunion create -s 2m "$writable_device_name" "$readable_device_name" + gunion_device="${writable_device_name}-${readable_device_name}.union" + echo "$gunion_device" > gunion_device + + size="$(diskinfo "/dev/$gunion_device" | awk '{print $3}')" + atf_check_equal "2097152" "$size" +} +size_cleanup() +{ + gunion_test_cleanup +} + +atf_test_case secsize cleanup +secsize_head() +{ + atf_set "descr" "Test gunion command with -S secsize argument" +} +secsize_body() +{ + gunion_test_setup + + writable_device_name="$(attach_md -s 1m)" + readable_device_name="$(attach_md -s 1m)" + newfs -S 512 -U "/dev/${readable_device_name}" + + gunion create -S 1024 "$writable_device_name" "$readable_device_name" + gunion_device="${writable_device_name}-${readable_device_name}.union" + echo "$gunion_device" > gunion_device + + secsize="$(diskinfo "/dev/$gunion_device" | awk '{print $2}')" + atf_check_equal "1024" "$secsize" +} +secsize_cleanup() +{ + gunion_test_cleanup +} + +atf_test_case gunionname cleanup +gunionname_head() +{ + atf_set "descr" "Test gunion command with -Z gunionname argument" +} +gunionname_body() +{ + gunion_test_setup + + writable_device_name="$(attach_md -s 1m)" + readable_device_name="$(attach_md -s 1m)" + newfs -U "/dev/${readable_device_name}" + + gunion create -Z gunion1 "$writable_device_name" "$readable_device_name" + echo "gunion1.union" > gunion_device + atf_check -o inline:"/dev/gunion1.union\n" ls /dev/gunion1.union +} +gunionname_cleanup() +{ + gunion_test_cleanup +} + +atf_test_case revert cleanup +revert_head() +{ + atf_set "descr" "Test gunion revert" +} +revert_body() +{ + gunion_test_setup + + writable_device_name="$(attach_md -s 1m)" + readable_device_name="$(attach_md -s 1m)" + newfs -U "/dev/${readable_device_name}" + mkdir readable_device + mkdir writable_device + mkdir gunion + + mount "/dev/${readable_device_name}" readable_device + echo "I am readable file" > readable_file + cp readable_file readable_device/readable_file + sync + umount readable_device + + atf_check gunion create "$writable_device_name" "$readable_device_name" + gunion_device="${writable_device_name}-${readable_device_name}.union" + mount "/dev/${gunion_device}" gunion + + echo "I am gunion file" > gunion_file + cp gunion_file gunion/gunion_file + sync + umount gunion + atf_check gunion revert "${gunion_device}" + gunion destroy "${gunion_device}" + + mount "/dev/${readable_device_name}" readable_device + checksum readable_device/readable_file readable_file + atf_check -s not-exit:0 -o ignore -e ignore ls readable_device/gunion_file +} +revert_cleanup() +{ + gunion_test_cleanup +} + +atf_init_test_cases() +{ + atf_add_test_case create + atf_add_test_case commit + atf_add_test_case offset + atf_add_test_case size + atf_add_test_case secsize + atf_add_test_case gunionname + atf_add_test_case revert +} + +checksum() +{ + src=$1 + work=$2 + + if [ ! -e "$src" ]; then + atf_fail "file not exist" + fi + if [ ! -e "$work" ]; then + atf_fail "file not exist" + fi + + src_checksum=$(md5 -q "$src") + work_checksum=$(md5 -q "$work") + + if [ "$work_checksum" != "$src_checksum" ]; then + atf_fail "md5 checksum didn't match with ${src} and ${work}" + fi +}