#!/bin/sh
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
#
# Copyright 2019 Olaf Wintermann. All rights reserved.
#
# 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 COPYRIGHT HOLDERS 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 COPYRIGHT HOLDER 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.
#

if [ -z "$DAV_BIN" ];
then
	echo "DAV_BIN variable not set"
	exit 1
fi
if [ -z "$DAV_SYNC_BIN" ];
then
	echo "DAV_BIN variable not set"
	exit 1
fi

XATTR=../../build/xattrtool

# checks if tmp-sync/out.txt contains a specific text
# arg1: pattern
# arg2: errormsg
check_tmpout()
{
	TEST=`cat tmp-sync/out.txt | grep "$1"`
	if [ $? -ne 0 ];
	then
		echo "$2"
		exit 2	
	fi
}

# checks if tmp-sync/out.txt does not contain a specific text
# arg1: pattern
# arg2: errormsg
ncheck_tmpout()
{
	TEST=`cat tmp-sync/out.txt | grep "$1"`
	if [ $? -eq 0 ];
	then
		echo "$2"
		exit 2	
	fi
}

# do dav-sync push and check return value
# arg1: dir
# arg2: errormsg
dav_sync_push()
{
	$DAV_SYNC_BIN push $1 > tmp-sync/out.txt
	if [ $? -ne 0 ];
	then
		echo "$2"
		exit 2
	fi
}
# do dav-sync pull and check return value
# arg1: dir
# arg2: errormsg
dav_sync_pull()
{
	$DAV_SYNC_BIN pull $1 > tmp-sync/out.txt
	if [ $? -ne 0 ];
	then
		echo "$2"
		exit 2
	fi
}

rm -f .dav/dav-sync-tests-test6a-db.xml
rm -f .dav/dav-sync-tests-test6b-db.xml

$DAV_BIN rm dav-test-repo/sync/test6 2> /dev/null

$DAV_BIN mkcol dav-test-repo/sync/test6 2> /dev/null

# tmp sync dir
rm -Rf tmp-sync
mkdir tmp-sync
mkdir tmp-sync/test6a
mkdir tmp-sync/test6b

# ----------------------------------------------------------------------------
# test 1: add some small files and sync
# expected result: everything synced, no split

mkdir tmp-sync/test6a/dir1
mkdir tmp-sync/test6a/dir1/sub1

cp synctest/file1 tmp-sync/test6a
cp synctest/file2 tmp-sync/test6a/dir1
cp synctest/file3 tmp-sync/test6a/dir1/sub1

dav_sync_push test6a "test 1: push failed"
check_tmpout "3 files pushed" "test 1: wrong push counter"
check_tmpout "0 conflicts" "test 1: wrong conflict counter (push)"
check_tmpout "0 errors" "test 1: wrong error counter (push)"

dav_sync_pull test6b "test 1: pull failed"
check_tmpout "3 files pulled" "test 1: wrong pull counter"
check_tmpout "0 conflicts" "test 1: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 1: wrong error counter (pull)"

$DAV_BIN info dav-test-repo/sync/test6/file1 | grep "type: resource" > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "test 1: file1 not a resource"
	exit 2
fi
$DAV_BIN info dav-test-repo/sync/test6/dir1/file2 | grep "type: resource" > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "test 1: file2 not a resource"
	exit 2
fi
$DAV_BIN info dav-test-repo/sync/test6/dir1/sub1/file3 | grep "type: resource" > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "test 1: file3 not a resource"
	exit 2
fi

# ----------------------------------------------------------------------------
# test 2: add a large file
# expected result: split

head -c 1048576 /dev/zero | sed 's/\x0/a/g' > tmp-sync/xa
cat tmp-sync/xa | sed 's/a/b/g' > tmp-sync/xb
cat tmp-sync/xa | sed 's/a/c/g' > tmp-sync/xc
cat tmp-sync/xa | sed 's/a/1/g' > tmp-sync/x1
cat tmp-sync/xa | sed 's/a/2/g' > tmp-sync/x2
cat tmp-sync/xa | sed 's/a/3/g' > tmp-sync/x3

cat tmp-sync/xa tmp-sync/xb tmp-sync/xc tmp-sync/x1 tmp-sync/x2 tmp-sync/x3 > tmp-sync/test6a/big1

dav_sync_push test6a "test 2: push failed"
check_tmpout "1 file pushed" "test 2: wrong push counter"
check_tmpout "0 conflicts" "test 2: wrong conflict counter (push)"
check_tmpout "0 errors" "test 2: wrong error counter (push)"

dav_sync_pull test6b "test 2: pull failed"
check_tmpout "1 file pulled" "test 2: wrong pull counter"
check_tmpout "0 conflicts" "test 2: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 2: wrong error counter (pull)"

$DAV_BIN info dav-test-repo/sync/test6/big1 | grep "type: collection" > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "test 2: big1 not a collection"
	exit 2
fi

HASH1=`cat tmp-sync/test6a/big1 | sha256sum`
HASH2=`cat tmp-sync/test6b/big1 | sha256sum`
if [ "$HASH1" != "$HASH2" ]; then
	echo "test 2: big1 not equal"
	exit 2
fi

$DAV_BIN list -l dav-test-repo/sync/test6/big1 > tmp-sync/list.txt 2> /dev/null
if [ $? -ne 0 ]; then
	echo "test 2: dav list failed"
	exit 2
fi

TEST=`cat tmp-sync/list.txt | wc -l`
if [ $TEST -ne 6 ]; then
	echo "test 2: wrong block count"
	exit 2
fi

LASTMODIFIED_PART0=`$DAV_BIN get-property dav-test-repo/sync/test6/big1/0 D:getlastmodified`
LASTMODIFIED_PART1=`$DAV_BIN get-property dav-test-repo/sync/test6/big1/1 D:getlastmodified`
LASTMODIFIED_PART2=`$DAV_BIN get-property dav-test-repo/sync/test6/big1/2 D:getlastmodified`
LASTMODIFIED_PART3=`$DAV_BIN get-property dav-test-repo/sync/test6/big1/3 D:getlastmodified`
LASTMODIFIED_PART4=`$DAV_BIN get-property dav-test-repo/sync/test6/big1/4 D:getlastmodified`
LASTMODIFIED_PART5=`$DAV_BIN get-property dav-test-repo/sync/test6/big1/5 D:getlastmodified`


# ----------------------------------------------------------------------------
# test 3: modify first block
# expected result: first block synced

sleep 2

dd if=tmp-sync/xc of=tmp-sync/test6a/big1 conv=notrunc > /dev/null 2>&1

dav_sync_push test6a "test 3: push failed"
check_tmpout "1 file pushed" "test 3: wrong push counter"
check_tmpout "0 conflicts" "test 3: wrong conflict counter (push)"
check_tmpout "0 errors" "test 3: wrong error counter (push)"

dav_sync_pull test6b "test 3: pull failed"
check_tmpout "1 file pulled" "test 3: wrong pull counter"
check_tmpout "0 conflicts" "test 3: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 3: wrong error counter (pull)"

HASH1=`cat tmp-sync/test6a/big1 | sha256sum`
HASH2=`cat tmp-sync/test6b/big1 | sha256sum`
if [ "$HASH1" != "$HASH2" ]; then
	echo "test 3: big1 not equal"
	exit 2
fi

LASTMODIFIED_PART0_2=`$DAV_BIN get-property dav-test-repo/sync/test6/big1/0 D:getlastmodified`
LASTMODIFIED_PART1_2=`$DAV_BIN get-property dav-test-repo/sync/test6/big1/1 D:getlastmodified`
LASTMODIFIED_PART2_2=`$DAV_BIN get-property dav-test-repo/sync/test6/big1/2 D:getlastmodified`

if [ "$LASTMODIFIED_PART0" = "$LASTMODIFIED_PART0_2" ]; then
	echo "test 3: part 0 not updated"
	exit 2
fi
if [ "$LASTMODIFIED_PART1" != "$LASTMODIFIED_PART1_2" ]; then
	echo "test 3: part 1 updated"
	exit 2
fi
if [ "$LASTMODIFIED_PART2" != "$LASTMODIFIED_PART2_2" ]; then
	echo "test 3: part 2 updated"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 4: add new block to big1 and sync
# expected result: block synced

sleep 2

cat tmp-sync/test6a/big1 tmp-sync/xb > tmp-sync/test6a/big_tmp
rm tmp-sync/test6a/big1
mv tmp-sync/test6a/big_tmp tmp-sync/test6a/big1

dav_sync_push test6a "test 4: push failed"
check_tmpout "1 file pushed" "test 4: wrong push counter"
check_tmpout "0 conflicts" "test 4: wrong conflict counter (push)"
check_tmpout "0 errors" "test 4: wrong error counter (push)"

dav_sync_pull test6b "test 4: pull failed"
check_tmpout "1 file pulled" "test 4: wrong pull counter"
check_tmpout "0 conflicts" "test 4: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 4: wrong error counter (pull)"

HASH1=`cat tmp-sync/test6a/big1 | sha256sum`
HASH2=`cat tmp-sync/test6b/big1 | sha256sum`
if [ "$HASH1" != "$HASH2" ]; then
	echo "test 4: big1 not equal"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 5: add some extra bytes to big1 and sync
# expected result: block synced

sleep 2

echo "test5-mod1" >> tmp-sync/test6a/big1

dav_sync_push test6a "test 5: push failed"
check_tmpout "1 file pushed" "test 5: wrong push counter"
check_tmpout "0 conflicts" "test 5: wrong conflict counter (push)"
check_tmpout "0 errors" "test 5: wrong error counter (push)"

dav_sync_pull test6b "test 5: pull failed"
check_tmpout "1 file pulled" "test 5: wrong pull counter"
check_tmpout "0 conflicts" "test 5: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 5: wrong error counter (pull)"

HASH1=`cat tmp-sync/test6a/big1 | sha256sum`
HASH2=`cat tmp-sync/test6b/big1 | sha256sum`
if [ "$HASH1" != "$HASH2" ]; then
	echo "test 5: big1 not equal"
	exit 2
fi


# ----------------------------------------------------------------------------
# test 6: decrease size of big1 and sync
# expected result: file truncated

# no sleep
cat tmp-sync/xc tmp-sync/xb tmp-sync/xc > tmp-sync/test6a/big1

dav_sync_push test6a "test 6: push failed"
check_tmpout "1 file pushed" "test 6: wrong push counter"
check_tmpout "0 conflicts" "test 6: wrong conflict counter (push)"
check_tmpout "0 errors" "test 6: wrong error counter (push)"

dav_sync_pull test6b "test 6: pull failed"
check_tmpout "1 file pulled" "test 6: wrong pull counter"
check_tmpout "0 conflicts" "test 6: wrong conflict counter (pull)"
check_tmpout "0 errors" "test 6: wrong error counter (pull)"

HASH1=`cat tmp-sync/test6a/big1 | sha256sum`
HASH2=`cat tmp-sync/test6b/big1 | sha256sum`
if [ "$HASH1" != "$HASH2" ]; then
	echo "test 6: big1 not equal"
	exit 2
fi


