#!/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.
#

#
# Test dav-sync pull conflict detection
# and related commands
#

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

# 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
}

# 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-test1a-db.xml
rm -f .dav/dav-sync-tests-test1b-db.xml

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

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

# prepare
cp synctest/file1 tmp-sync/test1a
cp synctest/file2 tmp-sync/test1a

mkdir tmp-sync/test1a/dir1/
mkdir tmp-sync/test1a/dir2/
mkdir tmp-sync/test1a/dir1/subdir1/
mkdir tmp-sync/test1a/dir2/subdir1/

cp synctest/file1 tmp-sync/test1a/dir1/
cp synctest/file2 tmp-sync/test1a/dir1/
cp synctest/file3 tmp-sync/test1a/dir1/
cp synctest/file4 tmp-sync/test1a/dir1/

cp synctest/file1 tmp-sync/test1a/dir2/
cp synctest/file2 tmp-sync/test1a/dir2/
cp synctest/file3 tmp-sync/test1a/dir2/subdir1/
cp synctest/file4 tmp-sync/test1a/dir2/subdir1/

cp synctest/file1 tmp-sync/test1a/dir1/subdir1/
cp synctest/file2 tmp-sync/test1a/dir1/subdir1/
cp synctest/file3 tmp-sync/test1a/dir1/subdir1/
cp synctest/file4 tmp-sync/test1a/dir1/subdir1/
cp synctest/empty1 tmp-sync/test1a/dir1/subdir1/
cp synctest/empty2 tmp-sync/test1a/dir1/subdir1/

dav_sync_push test1a "prepare: push failed"
dav_sync_pull test1b "prepare: pull failed"
sleep 3

# ----------------------------------------------------------------------------
# 1. test: pull, local file also modified
# expected result: 1 conflict

echo "conflict1-test1" >> tmp-sync/test1a/file1
dav_sync_push test1a "prepare: push failed"
check_tmpout "1 file pushed" "test 1: wrong push counter"

TEST1B_COUNT1=`ls tmp-sync/test1b/ | wc -l`

echo "conflict1-test1-conflict" >> tmp-sync/test1b/file1
dav_sync_pull test1b "prepare: pull failed"
check_tmpout "1 file pulled" "test 1: wrong pull counter"
check_tmpout "1 conflict" "test 1: wrong conflict counter"
check_tmpout "0 errors" "test 1: wrong error counter"

TEST1B_COUNT2=`ls tmp-sync/test1b/ | wc -l`
# check if the conflict file was moved
if [ $TEST1B_COUNT1 = $TEST1B_COUNT2 ]; then
	echo "test 1: conflict file not renamed"
	exit 2
fi

# warning: never check if the moved conflict file has a specific
#          name like "orig.0.file1"

# ----------------------------------------------------------------------------
# 2. test: check list-conflicts command
# expected result: "/file1" output

$DAV_SYNC_BIN list-conflicts test1b > tmp-sync/out.txt
if [ $? -ne 0 ]; then
	echo "test 2: list-conflicts failed"
	exit 2
fi
TEST=`cat tmp-sync/out.txt | grep "/file1"`
if [ $? -ne 0 ];
then
	echo "test 2: wrong list-conflicts output"
	exit 2	
fi

# ----------------------------------------------------------------------------
# 3. test: check delete-conflicts command
# expected result: conflict file ("/orig.0.file1") deleted

# don't check if the conflict file has this specific name

$DAV_SYNC_BIN delete-conflicts test1b > tmp-sync/out.txt
if [ $? -ne 0 ]; then
	echo "test 3: delete-conflicts failed"
	exit 2
fi
TEST=`cat tmp-sync/out.txt | grep "1 conflict file deleted"`
if [ $? -ne 0 ];
then
	echo "test 3: wrong delete-conflicts output"
	exit 2	
fi

TEST1B_COUNT3=`ls tmp-sync/test1b/ | wc -l`

if [ $TEST1B_COUNT1 != $TEST1B_COUNT3 ]; then
	echo "test 3: file not removed"
	exit 2
fi

# ----------------------------------------------------------------------------
# 4. test: pull after deleted conflicts
# expected result: 0 files pulled, 0 conflicts

dav_sync_pull test1b "test 4: pull failed"
check_tmpout "0 files pulled" "test 4: wrong pull counter"
check_tmpout "0 conflicts" "test 4: wrong conflict counter"
check_tmpout "0 errors" "test 4: wrong error counter"

# ----------------------------------------------------------------------------
# 5. test: pull, local file also modified, delete-conflicts
#          this test prepares test 6, to test 'push' after 'delete-conflicts'
# expected result: 0 files pulled, 0 conflicts

sleep 3 # make sure mtime changes

echo "conflict1-test5" >> tmp-sync/test1a/file1
dav_sync_push test1a "prepare: push failed"
check_tmpout "1 file pushed" "test 1: wrong push counter"

echo "conflict1-test5-conflict" >> tmp-sync/test1b/file1
dav_sync_pull test1b "prepare: pull failed"
check_tmpout "1 file pulled" "test 1: wrong pull counter"
check_tmpout "1 conflict" "test 1: wrong conflict counter"
check_tmpout "0 errors" "test 1: wrong error counter"

$DAV_SYNC_BIN delete-conflicts test1b > tmp-sync/out.txt
if [ $? -ne 0 ]; then
	echo "test 3: delete-conflicts failed"
	exit 2
fi
TEST=`cat tmp-sync/out.txt | grep "1 conflict file deleted"`
if [ $? -ne 0 ];
then
	echo "test 3: wrong delete-conflicts output"
	exit 2	
fi


# ----------------------------------------------------------------------------
# 6. test: push after deleted conflicts
# expected result: 0 files pushed, 0 conflicts

dav_sync_push test1b "test 6: push failed"
check_tmpout "0 files pushed" "test 6: wrong push counter"
check_tmpout "0 conflicts" "test 6: wrong conflict counter"
check_tmpout "0 errors" "test 6: wrong error counter"


# ----------------------------------------------------------------------------
# 7. test: resolve-conflicts after pull, followed by push
# expected result: pull with 1 conflict, resolve-conflicts removes conflicts
#                  from db (list-conflicts doesn't show conflicts after that)

sleep 3 # make sure mtime changes

echo "conflict1-test7" >> tmp-sync/test1a/file1
dav_sync_push test1a "prepare: push failed"
check_tmpout "1 file pushed" "test 7: wrong push counter"

echo "conflict1-test7-conflict" >> tmp-sync/test1b/file1
dav_sync_pull test1b "prepare: pull failed"
check_tmpout "1 file pulled" "test 7: wrong pull counter"
check_tmpout "1 conflict" "test 7: wrong conflict counter"
check_tmpout "0 errors" "test 7: wrong error counter"

TEST1B_COUNT_T7_1=`ls tmp-sync/test1b/ | wc -l`

$DAV_SYNC_BIN resolve-conflicts test1b > tmp-sync/out.txt
if [ $? -ne 0 ]; then
	echo "test 7: resolve-conflicts failed"
	exit 2
fi
TEST=`cat tmp-sync/out.txt | grep "1 conflict resolved"`
if [ $? -ne 0 ];
then
	echo "test 7: wrong resolve-conflicts output"
	exit 2	
fi

TEST1B_COUNT_T7_2=`ls tmp-sync/test1b/ | wc -l`

if [ $TEST1B_COUNT_T7_1 != $TEST1B_COUNT_T7_2 ]; then
	echo "test 7: number of files has changed"
	exit 2
fi

TEST7=`dav-sync list-conflicts test1b | wc -l`
if [ $TEST7 != "0" ]; then
	echo "test 7: list-conflicts must not show conflicts"
	return 2
fi

# pull again, should do nothing

dav_sync_pull test1b "test 7: pull(2) failed"
check_tmpout "0 files pulled" "test 7: wrong pull counter (2)"
check_tmpout "0 conflicts" "test 7: wrong conflict counter (2)"
check_tmpout "0 errors" "test 7: wrong error counter (2)"

# ----------------------------------------------------------------------------
# 8. test: resolve-conflicts after pull, followed by push
# expected result: pull with 1 conflict, resolve-conflicts removes conflicts
#                  from db (list-conflicts doesn't show conflicts after that)

# test mostly the same as test 7

sleep 3 # make sure mtime changes

echo "conflict1-test8" >> tmp-sync/test1a/file1
dav_sync_push test1a "prepare: push failed"
check_tmpout "1 file pushed" "test 8: wrong push counter"

echo "conflict1-test8-conflict" >> tmp-sync/test1b/file1
dav_sync_pull test1b "prepare: pull failed"
check_tmpout "1 file pulled" "test 8: wrong pull counter"
check_tmpout "1 conflict" "test 8: wrong conflict counter"
check_tmpout "0 errors" "test 8: wrong error counter"

TEST1B_COUNT_T7_1=`ls tmp-sync/test1b/ | wc -l`

$DAV_SYNC_BIN resolve-conflicts test1b > tmp-sync/out.txt
if [ $? -ne 0 ]; then
	echo "test 8: resolve-conflicts failed"
	exit 2
fi
TEST=`cat tmp-sync/out.txt | grep "1 conflict resolved"`
if [ $? -ne 0 ];
then
	echo "test 8: wrong resolve-conflicts output"
	exit 2	
fi

TEST1B_COUNT_T7_2=`ls tmp-sync/test1b/ | wc -l`

if [ $TEST1B_COUNT_T7_1 != $TEST1B_COUNT_T7_2 ]; then
	echo "test 8: number of files has changed"
	exit 2
fi

TEST7=`dav-sync list-conflicts test1b | wc -l`
if [ $TEST7 != "0" ]; then
	echo "test 8: list-conflicts must not show conflicts"
	return 2
fi

# push, 2 resolved conflict files (test 7 and test 8 'orig' file)

dav_sync_push test1b "test 8: push failed"
check_tmpout "2 files pushed" "test 8: wrong push counter"
check_tmpout "0 conflicts" "test 8: wrong conflicts counter"
check_tmpout "0 errors" "test 8: wrong error counter"

# push again, shoud do nothing
dav_sync_push test1b "test 8: push(2) failed"
check_tmpout "0 files pushed" "test 8: wrong push counter (2)"
check_tmpout "0 conflicts" "test 8: wrong conflicts counter (2)"
check_tmpout "0 errors" "test 8: wrong error counter (2)"


# ----------------------------------------------------------------------------
# 9. test: test1a deleted file, test1b modified file
# expected result: no delete

sleep 3 # make sure mtime changes

rm -f tmp-sync/test1a/file1

dav_sync_push test1a "test 9: push failed"
check_tmpout "1 file deleted" "test 9: wrong delete counter"
check_tmpout "0 conflicts" "test 9: wrong conflict counter"

echo "conflict1-test9-conflict" >> tmp-sync/test1b/file1

dav_sync_pull test1b "test 9: pull failed"
# don't check conflict counter
TEST=`cat tmp-sync/out.txt | grep "1 file deleted"`
if [ $? -eq 0 ];
then
	echo "test 9: file1 deleted"
	exit 2	
fi

cat tmp-sync/test1b/file1 > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "test 9: file1 deleted (2)"
	exit 2
fi

# ----------------------------------------------------------------------------
# 10. test: test1a deleted 2 files in subdir, test1b modified one file
# expected result: 1 delete

# no sleep required, because we are working with other files

rm -f tmp-sync/test1a/dir2/subdir1/file3
rm -f tmp-sync/test1a/dir2/subdir1/file4

dav_sync_push test1a "test 10: push failed"
check_tmpout "2 files deleted" "test 10: wrong delete counter"
check_tmpout "0 conflicts" "test 9: wrong conflict counter"

echo "conflict1-test10-conflict" >> tmp-sync/test1b/dir2/subdir1/file4

dav_sync_pull test1b "test 10: pull failed"
# don't check conflict counter
check_tmpout "1 file deleted" "test 10: wrong delete counter"

cat tmp-sync/test1b/dir2/subdir1/file4 > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "test 10: file4 deleted"
	exit 2
fi


# ----------------------------------------------------------------------------
# 11. test: delete dir in test1a, modify 1 file in test1b
# expected result: 1 remaining file

# no sleep required, because we are working with other files

rm -Rf tmp-sync/test1a/dir1

dav_sync_push test1a "test 11: push failed"
check_tmpout "0 conflicts" "test 11: wrong conflict counter (push)"
check_tmpout "0 errors" "test 11: wrong error counter (push)"

echo "conflict1-test11-conflict" >> tmp-sync/test1b/dir1/file3

dav_sync_pull test1b "test 11: pull failed"
check_tmpout "0 errors" "test 11: wrong error counter (pull)"
check_tmpout "delete: " "test 11: no deletes (pull)"

TEST=`ls tmp-sync/test1b/dir1/ | wc -l`
if [ $TEST != "1" ]; then
	echo "test 11: wrong file count"
	exit 2
fi


# ----------------------------------------------------------------------------
# 12. test: test1a modifies file, test1b deletes file
# expected result: pull test1b restores deleted file

# no sleep required, because we are working with other files

echo "modified" >> tmp-sync/test1a/file2

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

rm tmp-sync/test1b/file2

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

cat tmp-sync/test1b/file2 > /dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "test 12: file2 missing"
	exit 2
fi




