summaryrefslogtreecommitdiff
path: root/tests/test-pull-metalink.sh
blob: 3cd425b3c106997fe59bea10b996a88a6301721d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/bin/bash
#
# Copyright (C) 2014 Colin Walters <walters@verbum.org>
#
# 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
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

set -e

. $(dirname $0)/libtest.sh

setup_fake_remote_repo1 "archive-z2"

# And another web server acting as the metalink server
cd ${test_tmpdir}
mkdir metalink-data
cd metalink-data
ostree trivial-httpd --autoexit --daemonize -p ${test_tmpdir}/metalink-httpd-port
metalink_port=$(cat ${test_tmpdir}/metalink-httpd-port)
echo "http://127.0.0.1:${metalink_port}" > ${test_tmpdir}/metalink-httpd-address

ostree --repo=${test_tmpdir}/ostree-srv/gnomerepo summary -u

summary_path=${test_tmpdir}/ostree-srv/gnomerepo/summary

echo -n broken > ${summary_path}.bad

echo '1..1'
cd ${test_tmpdir}

cat > ${test_tmpdir}/metalink-data/metalink.xml <<EOF
<?xml version="1.0" encoding="utf-8"?>
<metalink version="3.0" xmlns="http://www.metalinker.org/">
  <files>
    <file name="summary">
      <size>$(stat -c '%s' ${summary_path})</size>
      <verification>
        <hash type="md5">$(md5sum ${summary_path} | cut -f 1 -d ' ')</hash>
        <hash type="sha256">$(sha256sum ${summary_path} | cut -f 1 -d ' ')</hash>
        <hash type="sha512">$(sha512sum ${summary_path} | cut -f 1 -d ' ')</hash>
      </verification>
      <resources maxconnections="1">
        <url protocol="http" type="http" location="US" preference="100" >$(cat httpd-address)/ostree/gnomerepo/summary.bad</url>
        <url protocol="http" type="http" location="US" preference="99" >$(cat httpd-address)/ostree/gnomerepo/nosuchfile</url>
        <url protocol="http" type="http" location="US" preference="98" >$(cat httpd-address)/ostree/gnomerepo/summary</url>
      </resources>
    </file>
  </files>
</metalink>
EOF

cd ${test_tmpdir}
mkdir repo
${CMD_PREFIX} ostree --repo=repo init
${CMD_PREFIX} ostree --repo=repo remote add --set=gpg-verify=false origin metalink=$(cat metalink-httpd-address)/metalink.xml
${CMD_PREFIX} ostree --repo=repo pull origin:main
${CMD_PREFIX} ostree --repo=repo rev-parse origin:main
${CMD_PREFIX} ostree --repo=repo fsck
echo "ok pull via metalink"

cp metalink-data/metalink.xml{,.orig}
cp ostree-srv/gnomerepo/summary{,.orig}

test_metalink_pull_error() {
    msg=$1
    rm repo -rf
    mkdir repo
    ${CMD_PREFIX} ostree --repo=repo init
    ${CMD_PREFIX} ostree --repo=repo remote add --set=gpg-verify=false origin metalink=$(cat metalink-httpd-address)/metalink.xml
    if ${CMD_PREFIX} ostree --repo=repo pull origin:main 2>err.txt; then
	assert_not_reached "pull unexpectedly succeeded"
    fi
    cat err.txt
    assert_file_has_content err.txt "${msg}"
}

cd ${test_tmpdir}
sed -e 's,<hash type="sha512">.*</hash>,<hash type="sha512">bacon</hash>,' < metalink-data/metalink.xml.orig > metalink-data/metalink.xml
test_metalink_pull_error "Invalid hash digest for sha512"
echo "ok metalink err hash format"

cd ${test_tmpdir}
sed -e 's,<hash type="sha512">.*</hash>,<hash type="sha512">'$( (echo -n dummy; cat ${summary_path}) | sha512sum | cut -f 1 -d ' ')'</hash>,' < metalink-data/metalink.xml.orig > metalink-data/metalink.xml
test_metalink_pull_error "Expected checksum is .* but actual is"
echo "ok metalink err hash sha512"

cd ${test_tmpdir}
cp metalink-data/metalink.xml.orig metalink-data/metalink.xml
echo -n moo > ostree-srv/gnomerepo/summary
test_metalink_pull_error "Expected size is .* bytes but content is 3 bytes"
echo "ok metalink err size"
cp ostree-srv/gnomerepo/summary{.orig,}

cd ${test_tmpdir}
grep -v sha256 < metalink-data/metalink.xml.orig |grep -v sha512 > metalink-data/metalink.xml
test_metalink_pull_error "No.*verification.*with known.*hash"
echo "ok metalink err no verify"

cd ${test_tmpdir}
grep -v '<url protocol' < metalink-data/metalink.xml.orig > metalink-data/metalink.xml
test_metalink_pull_error "No.*url.*method.*elements"
echo "ok metalink err no url"

cd ${test_tmpdir}
echo bacon > metalink-data/metalink.xml
test_metalink_pull_error "Document must begin with an element"
echo "ok metalink err malformed"