#!/bin/bash

#
# Copyright (C) 2016 Stanislav Brabec <sbrabec@suse.cz>
#
# 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.
#

TS_TOPDIR="${0%/*}/../.."
TS_DESC="losetup-loop"

. $TS_TOPDIR/functions.sh
ts_init "$*"

ts_check_test_command "$TS_CMD_LOSETUP"
ts_check_test_command "$TS_CMD_LSBLK"

ts_skip_nonroot
ts_check_losetup

. "$TS_SELF/losetup_functions.sh"

#
# Backing file is a block device
#
ts_scsi_debug_init dev_size_mb=11
BACKFILE="$TS_DEVICE"

# All tests are separated by sleep 3, because loop device exists some time after
# "losetup -d". This device confuses some tests. And find-race-condition, tests,
# whether re-use of this device works.

ts_init_subtest "find-race-condition"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show $BACKFILE )
$TS_CMD_LOSETUP -d $LODEV
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show $BACKFILE )
sleep 3
dd if=/dev/zero of=$LODEV count=1 bs=1 >/dev/null 2>&1
sleep 3
$TS_CMD_LOSETUP --list | grep -q $LODEV
ts_log $?
$TS_CMD_LOSETUP -d $LODEV >/dev/null 2>&1
ts_log "Success"
ts_finalize_subtest

sleep 3

ts_init_subtest "find-re-use"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show $BACKFILE )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d $LODEVR
	fi
fi
$TS_CMD_LOSETUP -d $LODEV
ts_log "Success"
ts_finalize_subtest

sleep 3

ts_init_subtest "find-no-re-use"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=0 --sizelimit=1MiB $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=1MiB --sizelimit=1MiB $BACKFILE )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d $LODEVR
	fi
fi
$TS_CMD_LOSETUP -d $LODEV
ts_log "Success"
ts_finalize_subtest

sleep 3

ts_init_subtest "find-conflict"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=1MiB --sizelimit=2MiB $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=2MiB --sizelimit=2MiB $BACKFILE >/dev/null 2>&1 )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d $LODEVR
	fi
fi
$TS_CMD_LOSETUP -d $LODEV
ts_log "Success"
ts_finalize_subtest

sleep 3

ts_init_subtest "find-conflict-no-sizelimit"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=1MiB $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=2MiB --sizelimit=2MiB $BACKFILE >/dev/null 2>&1 )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d $LODEVR
	fi
fi
$TS_CMD_LOSETUP -d $LODEV
ts_log "Success"
ts_finalize_subtest

sleep 3

ts_init_subtest "find-conflict-readonly"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --read-only $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=2MiB --sizelimit=2MiB $BACKFILE >/dev/null 2>&1 )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d $LODEVR
	fi
fi
$TS_CMD_LOSETUP -d $LODEV
$TS_CMD_LOSETUP -d $LODEVR >/dev/null 2>&1
ts_log "Success"
ts_finalize_subtest

sleep 3

ts_init_subtest "find-ok-no-sizelimit"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=1MiB --sizelimit=1MiB $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find --nooverlap --show --offset=2MiB $BACKFILE )
if [ -z "$LODEVR" ]; then
	ts_log "Failed to create loop device"
fi
if test "$LODEV" = "$LODEVR" ; then
	echo "equal" >>$TS_OUTPUT
else
	echo "different" >>$TS_OUTPUT
	if test -n "$LODEVR" ; then
		$TS_CMD_LOSETUP -d $LODEVR
	fi
fi
$TS_CMD_LOSETUP -d $LODEV
ts_log "Success"
ts_finalize_subtest

sleep 3

ts_init_subtest "plain-conflict"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find )
$TS_CMD_LOSETUP --nooverlap $LODEVR $BACKFILE >/dev/null 2>&1
ts_log $?
$TS_CMD_LOSETUP -d $LODEV
$TS_CMD_LOSETUP -d $LODEVR >/dev/null 2>&1
ts_log "Success"
ts_finalize_subtest

sleep 3

ts_init_subtest "plain-readonly"
LODEV=$( $TS_CMD_LOSETUP --find --nooverlap --show --read-only $BACKFILE )
if [ -z "$LODEV" ]; then
	ts_log "Failed to create loop device"
fi
LODEVR=$( $TS_CMD_LOSETUP --find )
$TS_CMD_LOSETUP --nooverlap $LODEVR $BACKFILE >/dev/null 2>&1
ts_log $?
$TS_CMD_LOSETUP -d $LODEV
$TS_CMD_LOSETUP -d $LODEVR >/dev/null 2>&1
ts_log "Success"
ts_finalize_subtest

sleep 3

ts_log "Success"
ts_finalize
