2012-04-10 08:31:53 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# Tests for Serval DNA rhizome operations.
|
|
|
|
#
|
|
|
|
# Copyright 2012 Paul Gardner-Stephen
|
|
|
|
#
|
|
|
|
# This program 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 program 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.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
source "${0%/*}/../testframework.sh"
|
|
|
|
source "${0%/*}/../testdefs.sh"
|
|
|
|
|
2012-04-12 08:58:34 +00:00
|
|
|
setup_dna_rhizome() {
|
|
|
|
setup_dna "$@"
|
|
|
|
executeOk $dna config set debug rhizome
|
|
|
|
}
|
|
|
|
|
2012-04-13 08:17:20 +00:00
|
|
|
assert_rhizome_list() {
|
2012-04-12 07:34:37 +00:00
|
|
|
executeOk $dna rhizome list
|
2012-04-13 08:17:20 +00:00
|
|
|
assertStdoutLineCount '==' $#
|
|
|
|
assertStdoutGrep --matches=$# '^fileid=.*:name='
|
|
|
|
local filename
|
|
|
|
for filename; do
|
|
|
|
local filehash='[^:]\+'
|
|
|
|
# If there is a manifest file that looks like it matches this payload
|
|
|
|
# file, then use its file hash to check the rhizome list output.
|
|
|
|
if [ -r "$filename.manifest" ]; then
|
|
|
|
local name=$(sed -n -e '/^name=/s///p' "$filename.manifest")
|
|
|
|
if [ "$name" == "$filename" ]; then
|
|
|
|
filehash=$(sed -n -e '/^filehash=/s///p' "$filename.manifest")
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
assertStdoutGrep --matches=1 "^\(.*:\)\?fileid=$filehash:.*:name=$filename\$"
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2012-04-16 02:16:58 +00:00
|
|
|
assert_manifest_newer() {
|
|
|
|
local manifest1="$1"
|
|
|
|
local manifest2="$2"
|
|
|
|
# The new manifest must have a higher version than the original.
|
|
|
|
extract_manifest_version oldversion "$manifest1"
|
|
|
|
extract_manifest_version newversion "$manifest2"
|
|
|
|
assert [ $newversion -gt $oldversion ]
|
|
|
|
# The new manifest must have a different filehash from the original.
|
|
|
|
extract_manifest_filehash oldfilehash "$manifest1"
|
|
|
|
extract_manifest_filehash newfilehash "$manifest2"
|
|
|
|
assert [ $oldfilehash != $newfilehash ]
|
|
|
|
}
|
|
|
|
|
2012-04-13 08:17:20 +00:00
|
|
|
strip_signatures() {
|
|
|
|
for file; do
|
|
|
|
cat -v "$file" | sed -e '/^^@/,$d' >"tmp.$file" && mv -f "tmp.$file" "$file"
|
|
|
|
done
|
2012-04-10 08:31:53 +00:00
|
|
|
}
|
|
|
|
|
2012-04-16 02:16:58 +00:00
|
|
|
extract_manifest_version() {
|
|
|
|
local _var="$1"
|
|
|
|
local _manifestfile="$2"
|
2012-04-17 07:24:39 +00:00
|
|
|
local _version=$(sed -n -e '/^version=[0-9]\{1,\}$/s/^version=//p' "$_manifestfile")
|
2012-04-16 02:16:58 +00:00
|
|
|
assert --message="$_manifestfile contains valid 'version=' line" [ -n "$_version" ]
|
|
|
|
[ -n "$_var" ] && eval $_var=$_version
|
|
|
|
}
|
|
|
|
|
|
|
|
extract_manifest_filehash() {
|
|
|
|
local _var="$1"
|
|
|
|
local _manifestfile="$2"
|
2012-04-17 07:24:39 +00:00
|
|
|
local _filehash=$(sed -n -e '/^filehash=[0-9a-fA-F]\{1,\}$/s/^filehash=//p' "$_manifestfile")
|
2012-04-16 02:16:58 +00:00
|
|
|
assert --message="$_manifestfile contains valid 'filehash=' line" [ -n "$_filehash" ]
|
|
|
|
eval $_var=$_filehash
|
|
|
|
}
|
|
|
|
|
2012-04-13 08:29:40 +00:00
|
|
|
doc_InitialEmptyList="Initial list is empty"
|
2012-04-10 08:31:53 +00:00
|
|
|
setup_InitialEmptyList() {
|
2012-04-12 08:58:34 +00:00
|
|
|
setup_dna_rhizome
|
2012-04-10 08:31:53 +00:00
|
|
|
}
|
|
|
|
test_InitialEmptyList() {
|
2012-04-13 08:17:20 +00:00
|
|
|
assert_rhizome_list
|
|
|
|
}
|
|
|
|
|
2012-04-13 08:29:40 +00:00
|
|
|
doc_AddNoManifest="Add with no manifest file"
|
2012-04-13 08:17:20 +00:00
|
|
|
setup_AddNoManifest() {
|
|
|
|
setup_dna_rhizome
|
|
|
|
assert_rhizome_list
|
|
|
|
echo "A test file" >file1
|
|
|
|
echo "Another test file" >file2
|
|
|
|
}
|
|
|
|
test_AddNoManifest() {
|
|
|
|
executeOk $dna rhizome add file file1
|
|
|
|
}
|
|
|
|
|
2012-04-13 08:29:40 +00:00
|
|
|
doc_AddNonExistManifest="Add with non-existent manifest file"
|
2012-04-13 08:17:20 +00:00
|
|
|
setup_AddNonExistManifest() {
|
|
|
|
setup_dna_rhizome
|
|
|
|
assert_rhizome_list
|
|
|
|
echo "A test file" >file1
|
|
|
|
echo "Another test file" >file2
|
|
|
|
}
|
|
|
|
test_AddNonExistManifest() {
|
|
|
|
assert --error-on-fail [ ! -e file1.manifest ]
|
|
|
|
executeOk $dna rhizome add file file1 file1.manifest
|
|
|
|
assert [ -r file1.manifest ]
|
|
|
|
tfw_cat -v file1.manifest
|
2012-04-17 07:24:39 +00:00
|
|
|
local size=$(( $(cat file1 | wc -c) + 0 ))
|
2012-04-13 08:17:20 +00:00
|
|
|
assertGrep file1.manifest '^name=file1$'
|
|
|
|
assertGrep file1.manifest '^date=[0-9]\+$'
|
|
|
|
assertGrep file1.manifest '^version=[0-9]\+$'
|
|
|
|
assertGrep file1.manifest '^id=[0-9a-fA-F]\+$'
|
2012-04-17 07:24:39 +00:00
|
|
|
assertGrep file1.manifest "^filesize=$size\$"
|
2012-04-13 08:17:20 +00:00
|
|
|
assertGrep file1.manifest "^first_byte=0$"
|
2012-04-17 07:24:39 +00:00
|
|
|
assertGrep file1.manifest "^last_byte=$size\$"
|
2012-04-10 08:31:53 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 08:29:40 +00:00
|
|
|
doc_AddManifest="Add with minimal manifest file"
|
2012-04-10 08:31:53 +00:00
|
|
|
setup_AddManifest() {
|
2012-04-12 08:58:34 +00:00
|
|
|
setup_dna_rhizome
|
2012-04-13 08:17:20 +00:00
|
|
|
assert_rhizome_list
|
2012-04-10 08:31:53 +00:00
|
|
|
echo "A test file" >file1
|
2012-04-13 08:17:20 +00:00
|
|
|
echo -e 'name=wah\ndate=12345' >file1.manifest
|
2012-04-10 08:31:53 +00:00
|
|
|
echo "Another test file" >file2
|
|
|
|
}
|
|
|
|
test_AddManifest() {
|
2012-04-12 07:34:37 +00:00
|
|
|
executeOk $dna rhizome add file file1 file1.manifest
|
2012-04-10 08:31:53 +00:00
|
|
|
tfw_cat --stdout
|
|
|
|
tfw_cat --stderr
|
|
|
|
tfw_cat -v file1.manifest
|
2012-04-17 07:24:39 +00:00
|
|
|
local size=$(( $(cat file1 | wc -c) + 0 ))
|
2012-04-10 08:39:52 +00:00
|
|
|
assertGrep file1.manifest '^name=wah$'
|
2012-04-13 08:17:20 +00:00
|
|
|
assertGrep file1.manifest '^date=12345$'
|
2012-04-10 08:31:53 +00:00
|
|
|
assertGrep file1.manifest '^version=[0-9]\+$'
|
|
|
|
assertGrep file1.manifest '^id=[0-9a-fA-F]\+$'
|
2012-04-17 07:24:39 +00:00
|
|
|
assertGrep file1.manifest "^filesize=$size\$"
|
2012-04-10 08:31:53 +00:00
|
|
|
assertGrep file1.manifest "^first_byte=0$"
|
2012-04-17 07:24:39 +00:00
|
|
|
assertGrep file1.manifest "^last_byte=$size\$"
|
2012-04-10 08:31:53 +00:00
|
|
|
}
|
|
|
|
|
2012-04-13 08:29:40 +00:00
|
|
|
doc_AddThenList="List contains one file after one add"
|
2012-04-10 08:31:53 +00:00
|
|
|
setup_AddThenList() {
|
2012-04-12 08:58:34 +00:00
|
|
|
setup_dna_rhizome
|
2012-04-13 08:17:20 +00:00
|
|
|
assert_rhizome_list
|
2012-04-10 08:31:53 +00:00
|
|
|
echo "A test file" >file1
|
|
|
|
echo "Another test file" >file2
|
|
|
|
}
|
|
|
|
test_AddThenList() {
|
|
|
|
# Add first file
|
2012-04-13 08:17:20 +00:00
|
|
|
executeOk $dna rhizome add file file1 file1.manifest
|
|
|
|
assert_rhizome_list file1
|
2012-04-10 08:31:53 +00:00
|
|
|
# Add second file
|
2012-04-13 08:17:20 +00:00
|
|
|
executeOk $dna rhizome add file file2 file2.manifest
|
|
|
|
assert_rhizome_list file1 file2
|
2012-04-10 08:31:53 +00:00
|
|
|
}
|
|
|
|
|
2012-04-16 02:16:58 +00:00
|
|
|
doc_AddDuplicate="Add same manifest detects duplicate"
|
2012-04-10 08:31:53 +00:00
|
|
|
setup_AddDuplicate() {
|
2012-04-12 08:58:34 +00:00
|
|
|
setup_dna_rhizome
|
2012-04-13 08:17:20 +00:00
|
|
|
assert_rhizome_list
|
2012-04-10 08:31:53 +00:00
|
|
|
echo "A test file" >file1
|
|
|
|
echo "Another test file" >file2
|
2012-04-13 08:17:20 +00:00
|
|
|
echo "A test file, second version" >file1_2
|
2012-04-10 08:31:53 +00:00
|
|
|
# Add first file
|
2012-04-13 08:17:20 +00:00
|
|
|
executeOk $dna rhizome add file file1 file1.manifest
|
2012-04-10 08:31:53 +00:00
|
|
|
# Add second file
|
2012-04-13 08:17:20 +00:00
|
|
|
executeOk $dna rhizome add file file2 file2.manifest
|
2012-04-10 08:31:53 +00:00
|
|
|
# Make sure they are both in the list.
|
2012-04-13 08:17:20 +00:00
|
|
|
assert_rhizome_list file1 file2
|
2012-04-10 08:31:53 +00:00
|
|
|
}
|
|
|
|
test_AddDuplicate() {
|
2012-04-13 08:17:20 +00:00
|
|
|
# Add first file again - nothing should change in its manifests, and it
|
|
|
|
# should appear that the add command succeeded (with perhaps some grumbling
|
|
|
|
# on stderr).
|
2012-04-16 02:16:58 +00:00
|
|
|
execute --exit-status=2 $dna rhizome add file file1 file1.manifestA
|
2012-04-13 08:17:20 +00:00
|
|
|
assert [ -s file1.manifestA ]
|
|
|
|
assert_rhizome_list file1 file2
|
|
|
|
strip_signatures file1.manifest file1.manifestA
|
|
|
|
assert diff file1.manifest file1.manifestA
|
|
|
|
# Repeat for second file.
|
2012-04-16 02:16:58 +00:00
|
|
|
execute --exit-status=2 $dna rhizome add file file2 file2.manifestA
|
2012-04-13 08:17:20 +00:00
|
|
|
assert [ -s file2.manifestA ]
|
|
|
|
assert_rhizome_list file1 file2
|
|
|
|
strip_signatures file2.manifest file2.manifestA
|
|
|
|
assert diff file2.manifest file2.manifestA
|
|
|
|
}
|
|
|
|
|
2012-04-13 08:29:40 +00:00
|
|
|
doc_AddMismatched="Add mismatched manifest/payload fails"
|
2012-04-13 08:17:20 +00:00
|
|
|
setup_AddMismatched() {
|
|
|
|
setup_AddDuplicate
|
|
|
|
}
|
|
|
|
test_AddMismatched() {
|
|
|
|
# Try to add another file using an existing manifest, should fail and leave
|
|
|
|
# the manifest file unchanged.
|
|
|
|
cp file1.manifest file1_2.manifest
|
|
|
|
execute $dna rhizome add file file1_2 file1_2.manifest
|
|
|
|
assertExitStatus '!=' 0
|
|
|
|
assert cmp file1.manifest file1_2.manifest
|
|
|
|
# And rhizome store should be unchanged.
|
|
|
|
assert_rhizome_list file1 file2
|
|
|
|
}
|
|
|
|
|
2012-04-16 02:16:58 +00:00
|
|
|
doc_AddUpdateSameVersion="Add new payload to existing manifest with same version"
|
|
|
|
setup_AddUpdateSameVersion() {
|
2012-04-13 08:17:20 +00:00
|
|
|
setup_AddDuplicate
|
|
|
|
cp file1.manifest file1_2.manifest
|
|
|
|
strip_signatures file1_2.manifest
|
2012-04-17 07:24:39 +00:00
|
|
|
sed -i -e '/^date=/d;/^filehash=/d;/^filesize=/d' file1_2.manifest
|
|
|
|
tfw_cat -v file1_2.manifest
|
2012-04-13 08:29:40 +00:00
|
|
|
assertGrep --matches=0 file1_2.manifest '^filehash='
|
2012-04-16 02:16:58 +00:00
|
|
|
extract_manifest_version '' file1_2.manifest # asserts has version= line
|
|
|
|
assertGrep file1_2.manifest '^id='
|
2012-04-13 08:29:40 +00:00
|
|
|
cp file1_2.manifest file1_2.manifest.orig
|
|
|
|
}
|
2012-04-16 02:16:58 +00:00
|
|
|
test_AddUpdateSameVersion() {
|
|
|
|
tfw_cat -v file1_2.manifest
|
2012-04-13 08:29:40 +00:00
|
|
|
execute $dna rhizome add file file1_2 file1_2.manifest
|
2012-04-16 02:16:58 +00:00
|
|
|
assertExitStatus --stderr '!=' 0
|
|
|
|
tfw_cat -v file1_2.manifest
|
2012-04-13 08:29:40 +00:00
|
|
|
assert cmp file1_2.manifest file1_2.manifest.orig
|
|
|
|
# And rhizome store should be unchanged.
|
|
|
|
assert_rhizome_list file1 file2
|
|
|
|
}
|
|
|
|
|
2012-04-16 02:16:58 +00:00
|
|
|
doc_AddUpdateNewVersion="Add new payload to existing manifest with new version"
|
|
|
|
setup_AddUpdateNewVersion() {
|
|
|
|
setup_AddUpdateSameVersion
|
|
|
|
extract_manifest_version version file1_2.manifest
|
2012-04-13 08:29:40 +00:00
|
|
|
let version=version+1
|
|
|
|
sed -i -e "/^version=/s/=.*/=$version/" file1_2.manifest
|
|
|
|
assertGrep --matches=1 file1_2.manifest "^version=$version$"
|
|
|
|
}
|
2012-04-16 02:16:58 +00:00
|
|
|
test_AddUpdateNewVersion() {
|
2012-04-17 07:24:39 +00:00
|
|
|
tfw_cat -v file1_2.manifest
|
2012-04-16 02:16:58 +00:00
|
|
|
executeOk $dna rhizome add file file1_2 file1_2.manifest
|
|
|
|
assert_manifest_newer file1.manifest file1_2.manifest
|
|
|
|
# Rhizome store contents reflect new payload.
|
|
|
|
mv -f file1_2.manifest file1.manifest
|
|
|
|
assert_rhizome_list file1 file2
|
|
|
|
}
|
|
|
|
|
|
|
|
doc_AddUpdateAutoVersion="Add new payload to existing manifest with automatic version"
|
|
|
|
setup_AddUpdateAutoVersion() {
|
|
|
|
setup_AddUpdateSameVersion
|
|
|
|
sed -i -e '/^version=/d' file1_2.manifest
|
|
|
|
assertGrep --matches=0 file1_2.manifest '^version='
|
|
|
|
}
|
|
|
|
test_AddUpdateAutoVersion() {
|
2012-04-17 07:24:39 +00:00
|
|
|
tfw_cat -v file1_2.manifest
|
2012-04-16 02:16:58 +00:00
|
|
|
sleep 0.001 # Ensure that at least one millisecond has elapsed
|
2012-04-13 08:17:20 +00:00
|
|
|
executeOk $dna rhizome add file file1_2 file1_2.manifest
|
2012-04-16 02:16:58 +00:00
|
|
|
assert_manifest_newer file1.manifest file1_2.manifest
|
2012-04-13 08:17:20 +00:00
|
|
|
# Rhizome store contents reflect new payload.
|
|
|
|
mv -f file1_2.manifest file1.manifest
|
|
|
|
assert_rhizome_list file1 file2
|
2012-04-10 08:31:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
runTests "$@"
|