2017-06-13 20:55:53 +03:00
#!/bin/bash
#
# Copyright © 2017 Endless Mobile, Inc.
#
2018-01-30 22:26:26 +03:00
# SPDX-License-Identifier: LGPL-2.0+
#
2017-06-13 20:55:53 +03:00
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
2021-12-07 04:20:55 +03:00
# License along with this library. If not, see <https://www.gnu.org/licenses/>.
2017-06-13 20:55:53 +03:00
set -euo pipefail
. $( dirname $0 ) /libtest.sh
2017-11-20 17:47:31 +03:00
echo '1..11'
2017-06-13 20:55:53 +03:00
cd ${ test_tmpdir }
2017-11-20 17:47:31 +03:00
# Create a new repository with one ref with the repository’ s collection ID, and
# one ref with a different collection ID (which should be in refs/mirrors).
set_up_repo_with_collection_id( ) {
rm -rf repo files
mkdir repo
ostree_repo_init repo --collection-id org.example.Collection
mkdir files
pushd files
${ CMD_PREFIX } ostree --repo= ../repo commit -s "Commit 1" -b ref1 > ../ref1-checksum
${ CMD_PREFIX } ostree --repo= ../repo commit -s "Commit 2" --orphan --bind-ref ref2 --add-metadata-string= ostree.collection-binding= org.example.Collection2 > ../ref2-checksum
${ CMD_PREFIX } ostree --repo= ../repo refs --collections --create= org.example.Collection2:ref2 $( cat ../ref2-checksum)
popd
}
# Create a new repository with one ref and no collection IDs.
set_up_repo_without_collection_id( ) {
2017-06-13 20:55:53 +03:00
rm -rf repo files
mkdir repo
ostree_repo_init repo
mkdir files
pushd files
2017-11-20 17:47:31 +03:00
${ CMD_PREFIX } ostree --repo= ../repo commit -s "Commit 3" -b ref3 --bind-ref ref4 > ../ref3-checksum
${ CMD_PREFIX } ostree --repo= ../repo refs --create= ref4 $( cat ../ref3-checksum)
2017-06-13 20:55:53 +03:00
popd
}
2017-11-20 17:47:31 +03:00
set_up_repo_with_collection_id
2017-06-13 20:55:53 +03:00
# fsck at this point should succeed
${ CMD_PREFIX } ostree fsck --repo= repo > fsck
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs in collections\.\.\. $"
2017-06-13 20:55:53 +03:00
# Drop the commit the ref points to, and drop the original ref so that fsck doesn’ t prematurely fail on that.
find repo/objects -name '*.commit' -delete -print | wc -l > commitcount
2017-11-20 17:47:31 +03:00
assert_file_has_content commitcount " ^2 $"
2017-06-13 20:55:53 +03:00
2017-11-20 17:47:31 +03:00
rm repo/refs/heads/ref1
2017-06-13 20:55:53 +03:00
# fsck should now fail
if ${ CMD_PREFIX } ostree fsck --repo= repo > fsck; then
assert_not_reached "fsck unexpectedly succeeded after deleting commit!"
fi
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs\.\.\. $"
assert_file_has_content fsck " ^Validating refs in collections\.\.\. $"
2017-06-13 20:55:53 +03:00
echo "ok 1 fsck-collections"
# Try fsck in an old repository where refs/mirrors doesn’ t exist to begin with.
# It should succeed.
2017-11-20 17:47:31 +03:00
set_up_repo_with_collection_id
2017-06-13 20:55:53 +03:00
rm -rf repo/refs/mirrors
${ CMD_PREFIX } ostree fsck --repo= repo > fsck
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs\.\.\. $"
assert_file_has_content fsck " ^Validating refs in collections\.\.\. $"
2017-06-13 20:55:53 +03:00
echo "ok 2 fsck-collections in old repository"
2017-11-20 17:47:31 +03:00
# Test that fsck detects commits which are pointed to by refs, but which don’ t
# list those refs in their ref-bindings.
set_up_repo_with_collection_id
${ CMD_PREFIX } ostree --repo= repo refs --create= new-ref $( cat ref1-checksum)
2017-12-14 20:36:48 +03:00
# For compatibility we don't check for this by default
${ CMD_PREFIX } ostree fsck --repo= repo
2017-11-20 17:47:31 +03:00
# fsck should now fail
2017-12-14 20:36:48 +03:00
if ${ CMD_PREFIX } ostree fsck --repo= repo --verify-bindings > fsck 2> fsck-error; then
2017-11-20 17:47:31 +03:00
assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
fi
assert_file_has_content fsck-error "Commit has no requested ref ‘ new-ref’ in ref binding metadata (‘ ref1’ )"
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs\.\.\. $"
2017-11-20 17:47:31 +03:00
echo "ok 3 fsck detects missing ref bindings"
# And the same where the ref is a collection– ref.
set_up_repo_with_collection_id
${ CMD_PREFIX } ostree --repo= repo refs --collections --create= org.example.Collection2:new-ref $( cat ref1-checksum)
# fsck should now fail
2017-12-14 20:36:48 +03:00
if ${ CMD_PREFIX } ostree fsck --repo= repo --verify-bindings > fsck 2> fsck-error; then
2017-11-20 17:47:31 +03:00
assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
fi
assert_file_has_content fsck-error "Commit has no requested ref ‘ new-ref’ in ref binding metadata (‘ ref1’ )"
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs\.\.\. $"
assert_file_has_content fsck " ^Validating refs in collections\.\.\. $"
2017-11-20 17:47:31 +03:00
echo "ok 4 fsck detects missing collection– ref bindings"
# Check that a ref with a different collection ID but the same ref name is caught.
set_up_repo_with_collection_id
${ CMD_PREFIX } ostree --repo= repo refs --collections --create= org.example.Collection2:ref1 $( cat ref1-checksum)
# fsck should now fail
2017-12-14 20:36:48 +03:00
if ${ CMD_PREFIX } ostree fsck --repo= repo --verify-bindings > fsck 2> fsck-error; then
2017-11-20 17:47:31 +03:00
assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
fi
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck-error "Commit has collection ID ‘ org\.example\.Collection’ in collection binding metadata, while the remote it came from has collection ID ‘ org\.example\.Collection2’ "
assert_file_has_content fsck " ^Validating refs\.\.\. $"
assert_file_has_content fsck " ^Validating refs in collections\.\.\. $"
2017-11-20 17:47:31 +03:00
echo "ok 5 fsck detects missing collection– ref bindings"
# Check that a commit with ref bindings which aren’ t pointed to by refs is OK.
set_up_repo_with_collection_id
${ CMD_PREFIX } ostree --repo= repo refs --delete ref1
# fsck at this point should succeed
${ CMD_PREFIX } ostree fsck --repo= repo > fsck
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs in collections\.\.\. $"
2017-11-20 17:47:31 +03:00
echo "ok 6 fsck ignores unreferenced ref bindings"
# …but it’ s not OK if we pass --verify-back-refs to fsck.
if ${ CMD_PREFIX } ostree fsck --repo= repo --verify-back-refs > fsck 2> fsck-error; then
assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
fi
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck-error "Collection– ref (org\.example\.Collection, ref1) in bindings for commit .* does not exist"
assert_file_has_content fsck " ^Validating refs\.\.\. $"
assert_file_has_content fsck " ^Validating refs in collections\.\.\. $"
2017-11-20 17:47:31 +03:00
echo "ok 7 fsck ignores unreferenced ref bindings"
#
# Now repeat most of the above tests with a repository without collection IDs.
#
set_up_repo_without_collection_id
# fsck at this point should succeed
2017-12-14 20:36:48 +03:00
${ CMD_PREFIX } ostree fsck --repo= repo --verify-bindings > fsck
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs in collections\.\.\. $"
2017-11-20 17:47:31 +03:00
# Drop the commit the ref points to, and drop the original ref so that fsck doesn’ t prematurely fail on that.
find repo/objects -name '*.commit' -delete -print | wc -l > commitcount
assert_file_has_content commitcount " ^1 $"
rm repo/refs/heads/ref3
# fsck should now fail
2017-12-14 20:36:48 +03:00
if ${ CMD_PREFIX } ostree fsck --repo= repo --verify-bindings > fsck; then
2017-11-20 17:47:31 +03:00
assert_not_reached "fsck unexpectedly succeeded after deleting commit!"
fi
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs\.\.\. $"
2017-11-20 17:47:31 +03:00
echo "ok 8 fsck-collections"
# Test that fsck detects commits which are pointed to by refs, but which don’ t
# list those refs in their ref-bindings.
set_up_repo_without_collection_id
${ CMD_PREFIX } ostree --repo= repo refs --create= new-ref $( cat ref3-checksum)
# fsck should now fail
2017-12-14 20:36:48 +03:00
if ${ CMD_PREFIX } ostree fsck --repo= repo --verify-bindings > fsck 2> fsck-error; then
2017-11-20 17:47:31 +03:00
assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
fi
assert_file_has_content fsck-error "Commit has no requested ref ‘ new-ref’ in ref binding metadata (‘ ref3’ , ‘ ref4’ )"
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs\.\.\. $"
2017-11-20 17:47:31 +03:00
echo "ok 9 fsck detects missing ref bindings"
# Check that a commit with ref bindings which aren’ t pointed to by refs is OK.
set_up_repo_without_collection_id
${ CMD_PREFIX } ostree --repo= repo refs --delete ref3
# fsck at this point should succeed
${ CMD_PREFIX } ostree fsck --repo= repo > fsck
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs\.\.\. $"
2017-11-20 17:47:31 +03:00
echo "ok 10 fsck ignores unreferenced ref bindings"
# …but it’ s not OK if we pass --verify-back-refs to fsck.
if ${ CMD_PREFIX } ostree fsck --repo= repo --verify-back-refs > fsck 2> fsck-error; then
assert_not_reached "fsck unexpectedly succeeded after adding unbound ref!"
fi
assert_file_has_content fsck-error "Ref ‘ ref3’ in bindings for commit .* does not exist"
2019-04-02 05:01:49 +03:00
assert_file_has_content fsck " ^Validating refs\.\.\. $"
2017-11-20 17:47:31 +03:00
echo "ok 11 fsck ignores unreferenced ref bindings"