]> git.saurik.com Git - apt.git/blame - test/integration/framework
tests: allow to disable generation of InRelease/Release.gpg file
[apt.git] / test / integration / framework
CommitLineData
8d876415
DK
1#!/bin/sh -- # no runable script, just for vi
2
5d76cee1 3EXIT_CODE=0
8c1dd12c 4
68042532
DK
5while [ -n "$1" ]; do
6 if [ "$1" = "-q" ]; then
7 export MSGLEVEL=2
8 elif [ "$1" = "-v" ]; then
9 export MSGLEVEL=4
10 elif [ "$1" = '--color=no' ]; then
11 export MSGCOLOR='NO'
61e92778
DK
12 elif [ "$1" = '--color=yes' ]; then
13 export MSGCOLOR='YES'
14 elif [ "$1" = '--color' ]; then
15 export MSGCOLOR="$(echo "$2" | tr 'a-z' 'A-Z')"
16 shift
17 elif [ "$1" = '--level' ]; then
18 export MSGLEVEL=$2
19 shift
68042532
DK
20 else
21 echo >&2 "WARNING: Unknown parameter »$1« will be ignored"
22 fi
23 shift
24done
25export MSGLEVEL="${MSGLEVEL:-3}"
26
8d876415 27# we all like colorful messages
61e92778 28if [ "${MSGCOLOR:-YES}" = 'YES' ]; then
27cb4f6c 29 if [ ! -t 1 ]; then # but check that we output to a terminal
1290422a
DK
30 export MSGCOLOR='NO'
31 fi
32fi
33
34
35if [ "$MSGCOLOR" != 'NO' ]; then
36 CERROR="\033[1;31m" # red
37 CWARNING="\033[1;33m" # yellow
38 CMSG="\033[1;32m" # green
39 CINFO="\033[1;96m" # light blue
40 CDEBUG="\033[1;94m" # blue
41 CNORMAL="\033[0;39m" # default system console color
42 CDONE="\033[1;32m" # green
43 CPASS="\033[1;32m" # green
44 CFAIL="\033[1;31m" # red
45 CCMD="\033[1;35m" # pink
682a3bf7 46fi
8d876415 47
de81b2e2
DK
48msgprintf() {
49 local START="$1"
50 local MIDDLE="$2"
51 local END="$3"
52 shift 3
53 if [ -n "$1" ]; then
54 printf "$START " "$1"
d73840dc 55 shift
de81b2e2 56 while [ -n "$1" ]; do
bc8f83a5 57 printf "$MIDDLE " "$(echo "$1" | sed -e 's#^apt\([cfghks]\)#apt-\1#')"
de81b2e2
DK
58 shift
59 done
60 fi
61 printf "${END}"
2a2a7ef4 62}
de81b2e2
DK
63msgdie() { msgprintf "${CERROR}E: %s" '%s' "${CNORMAL}\n" "$@" >&2; exit 1; }
64msgwarn() { msgprintf "${CWARNING}W: %s" '%s' "${CNORMAL}\n" "$@" >&2; }
65msgmsg() { msgprintf "${CMSG}%s" '%s' "${CNORMAL}\n" "$@"; }
66msginfo() { msgprintf "${CINFO}I: %s" '%s' "${CNORMAL}\n" "$@"; }
67msgdebug() { msgprintf "${CDEBUG}D: %s" '%s' "${CNORMAL}\n" "$@"; }
68msgdone() { msgprintf "${CDONE}DONE" '%s' "${CNORMAL}\n" "$@"; }
69msgnwarn() { msgprintf "${CWARNING}W: %s" '%s' "${CNORMAL}" "$@" >&2; }
70msgnmsg() { msgprintf "${CMSG}%s" '%s' "${CNORMAL}" "$@"; }
71msgninfo() { msgprintf "${CINFO}I: %s" '%s' "${CNORMAL}" "$@"; }
72msgndebug() { msgprintf "${CDEBUG}D: %s" '%s' "${CNORMAL}" "$@"; }
73msgtest() { msgprintf "${CINFO}%s" "${CCMD}%s${CINFO}" "…${CNORMAL} " "$@"; }
3c528b91 74msgpass() { printf "${CPASS}PASS${CNORMAL}\n"; }
1501a2c9 75msgreportheader() {
68042532 76 if [ -n "$MSGTEST_MSG" ]; then
1501a2c9 77 test "$1" != 'msgfailoutput' || echo
ae7fce5a
DK
78 if [ -n "$MSGTEST_MSGMSG" ]; then
79 echo "$MSGTEST_MSGMSG"
80 fi
22ac12b2 81 if [ -n "$MSGTEST_GRP" ] && [ "$MSGTEST_GRP" != 'NEXT' ] && [ "$MSGTEST_GRP" != "$MSGTEST_MSG" ]; then
1501a2c9 82 echo "${CFAIL}Part of the test group: $MSGTEST_GRP"
22ac12b2 83 fi
68042532
DK
84 echo -n "$MSGTEST_MSG"
85 unset MSGTEST_MSG
86 fi
1501a2c9
DK
87}
88msgskip() {
89 msgreportheader 'msgskip'
68ba0b7f
DK
90 if [ $# -gt 0 ]; then printf "${CWARNING}SKIP: $*${CNORMAL}\n" >&2;
91 else printf "${CWARNING}SKIP${CNORMAL}\n" >&2; fi
92}
5229b285 93msgfail() {
1501a2c9 94 msgreportheader 'msgfail'
68042532 95 if [ $# -gt 0 ] && [ -n "$1" ]; then printf "${CFAIL}FAIL: $*${CNORMAL}\n" >&2;
3c528b91 96 else printf "${CFAIL}FAIL${CNORMAL}\n" >&2; fi
03aa0847 97 if [ -n "$APT_DEBUG_TESTS" ]; then
896f0ae8 98 runapt $SHELL
03aa0847 99 fi
5229b285
DK
100 EXIT_CODE=$((EXIT_CODE+1));
101}
9beb11aa
DK
102MSGGROUP_LEVEL=0
103msggroup() {
104 if [ -n "$1" ]; then
105 if [ $MSGGROUP_LEVEL = 0 ]; then
106 MSGTEST_GRP='NEXT'
107 fi
108 MSGGROUP_LEVEL=$((MSGGROUP_LEVEL+1));
109 else
110 MSGGROUP_LEVEL=$((MSGGROUP_LEVEL-1));
111 if [ $MSGGROUP_LEVEL = 0 ]; then
112 unset MSGTEST_GRP
113 fi
114 fi
115}
8d876415
DK
116
117# enable / disable Debugging
fc89263e
DK
118if [ $MSGLEVEL -le 0 ]; then
119 msgdie() { true; }
120fi
121if [ $MSGLEVEL -le 1 ]; then
122 msgwarn() { true; }
123 msgnwarn() { true; }
124fi
125if [ $MSGLEVEL -le 2 ]; then
ae7fce5a
DK
126 msgmsg() {
127 MSGTEST_MSGMSG="$(msgprintf "${CMSG}%s" '%s' "${CNORMAL}" "$@")"
128 }
fc89263e 129 msgnmsg() { true; }
9beb11aa
DK
130 msgtest() {
131 MSGTEST_MSG="$(msgprintf "${CINFO}%s" "${CCMD}%s${CINFO}" "…${CNORMAL} " "$@")"
132 if [ "$MSGTEST_GRP" = 'NEXT' ]; then
133 MSGTEST_GRP="$MSGTEST_MSG"
134 fi
135 }
3c528b91 136 msgpass() { printf " ${CPASS}P${CNORMAL}"; }
fc89263e
DK
137fi
138if [ $MSGLEVEL -le 3 ]; then
139 msginfo() { true; }
140 msgninfo() { true; }
141fi
142if [ $MSGLEVEL -le 4 ]; then
143 msgdebug() { true; }
144 msgndebug() { true; }
145fi
146msgdone() {
147 if [ "$1" = "debug" -a $MSGLEVEL -le 4 ] ||
148 [ "$1" = "info" -a $MSGLEVEL -le 3 ] ||
149 [ "$1" = "msg" -a $MSGLEVEL -le 2 ] ||
150 [ "$1" = "warn" -a $MSGLEVEL -le 1 ] ||
151 [ "$1" = "die" -a $MSGLEVEL -le 0 ]; then
152 true;
153 else
3c528b91 154 printf "${CDONE}DONE${CNORMAL}\n";
fc89263e
DK
155 fi
156}
e43a426e
MV
157getaptconfig() {
158 if [ -f ./aptconfig.conf ]; then
5465192b 159 echo "$(readlink -f ./aptconfig.conf)"
e43a426e 160 elif [ -f ../aptconfig.conf ]; then
5465192b
DK
161 echo "$(readlink -f ../aptconfig.conf)"
162 elif [ -f ../../aptconfig.conf ]; then
163 echo "$(readlink -f ../../aptconfig.conf)"
164 elif [ -f "${TMPWORKINGDIRECTORY}/aptconfig.conf" ]; then
165 echo "$(readlink -f "${TMPWORKINGDIRECTORY}/aptconfig.conf")"
166 fi
e43a426e 167}
8d876415
DK
168runapt() {
169 msgdebug "Executing: ${CCMD}$*${CDEBUG} "
846856f4
DK
170 local CMD="$1"
171 shift
3abb6a6a 172 case "$CMD" in
a311fb96 173 sh|aptitude|*/*|command) ;;
3b8eb3bc
DK
174 *) CMD="${BUILDDIRECTORY}/$CMD";;
175 esac
896f0ae8 176 MALLOC_PERTURB_=21 MALLOC_CHECK_=2 APT_CONFIG="$(getaptconfig)" LD_LIBRARY_PATH="${LIBRARYPATH}:${LD_LIBRARY_PATH}" "$CMD" "$@"
8d876415 177}
0390edd5 178runpython3() { runapt command python3 "$@"; }
846856f4
DK
179aptconfig() { runapt apt-config "$@"; }
180aptcache() { runapt apt-cache "$@"; }
181aptcdrom() { runapt apt-cdrom "$@"; }
182aptget() { runapt apt-get "$@"; }
183aptftparchive() { runapt apt-ftparchive "$@"; }
184aptkey() { runapt apt-key "$@"; }
185aptmark() { runapt apt-mark "$@"; }
4f6d26b4 186aptsortpkgs() { runapt apt-sortpkgs "$@"; }
796673c3 187apt() { runapt apt "$@"; }
3b8eb3bc
DK
188apthelper() { runapt "${APTHELPERBINDIR}/apt-helper" "$@"; }
189aptwebserver() { runapt "${APTWEBSERVERBINDIR}/aptwebserver" "$@"; }
190aptitude() { runapt aptitude "$@"; }
8d50b63f 191aptextracttemplates() { runapt apt-extracttemplates "$@"; }
3082603f 192aptinternalsolver() { runapt "${APTINTERNALSOLVER}" "$@"; }
ad7e0941 193aptdumpsolver() { runapt "${APTDUMPSOLVER}" "$@"; }
3b8eb3bc 194
158fda31 195dpkg() {
b0be0e09 196 "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "$@"
158fda31 197}
ce7f128c 198dpkgcheckbuilddeps() {
63c71412 199 command dpkg-checkbuilddeps --admindir="${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg" "$@"
b6b5a542 200}
3fa950f1 201gdb() {
b07aeb1a
DK
202 local CMD
203 case "$1" in
204 aptget) CMD="apt-get";;
205 aptcache) CMD="apt-cache";;
5465192b
DK
206 aptcdrom) CMD="apt-cdrom";;
207 aptconfig) CMD="apt-config";;
b07aeb1a
DK
208 aptmark) CMD="apt-mark";;
209 apthelper) CMD="apt-helper";;
3d8232bf 210 aptftparchive) CMD="apt-ftparchive";;
63c71412 211 dpkg) shift; runapt "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg" "$@"; return;;
b07aeb1a
DK
212 *) CMD="$1";;
213 esac
ce928105 214 shift
921a9626
DK
215 if [ "${CMD##*/}" = "$CMD" ]; then
216 CMD="${BUILDDIRECTORY}/${CMD}"
217 fi
218 runapt command gdb --quiet -ex run "$CMD" --args "$CMD" "$@"
ae99ce2e 219}
0340069c
DK
220lastmodification() {
221 date -u -d "@$(stat -c '%Y' "${TMPWORKINGDIRECTORY}/$1")" '+%a, %d %b %Y %H:%M:%S GMT'
222}
223releasefiledate() {
224 grep "^${2:-Date}:" "$1" | cut -d' ' -f 2- | sed -e 's#UTC#GMT#'
225}
8d876415 226
8c1dd12c 227exitwithstatus() {
f91bd741
MV
228 # error if we about to overflow, but ...
229 # "255 failures ought to be enough for everybody"
5d76cee1
MV
230 if [ $EXIT_CODE -gt 255 ]; then
231 msgdie "Total failure count $EXIT_CODE too big"
f91bd741 232 fi
5d76cee1 233 exit $((EXIT_CODE <= 255 ? EXIT_CODE : 255));
8c1dd12c
MV
234}
235
804d4a0d
DK
236shellsetedetector() {
237 local exit_status=$?
238 if [ "$exit_status" != '0' ]; then
3c528b91 239 printf >&2 "${CERROR}E: Looks like the testcases ended prematurely with exitcode: ${exit_status}${CNORMAL}\n"
804d4a0d
DK
240 if [ "$EXIT_CODE" = '0' ]; then
241 EXIT_CODE="$exit_status"
242 fi
243 fi
244}
245
b720d0bd 246addtrap() {
8437b7d4
DK
247 if [ "$1" = 'prefix' ]; then
248 CURRENTTRAP="$2 $CURRENTTRAP"
249 else
250 CURRENTTRAP="$CURRENTTRAP $1"
251 fi
804d4a0d 252 trap "shellsetedetector; $CURRENTTRAP exitwithstatus;" 0 HUP INT QUIT ILL ABRT FPE SEGV PIPE TERM
b720d0bd 253}
8d876415 254
bc8f83a5
DK
255escape_shell() {
256 echo "$@" | sed -e "s#'#'\"'\"'#g"
257}
258
8d876415 259setupenvironment() {
0d303f17
DK
260 # privilege dropping and testing doesn't work if /tmp isn't world-writeable (as e.g. with libpam-tmpdir)
261 if [ -n "$TMPDIR" ] && [ "$(id -u)" = '0' ] && [ "$(stat --format '%a' "$TMPDIR")" != '1777' ]; then
262 unset TMPDIR
263 fi
63c71412 264 TMPWORKINGDIRECTORY="$(mktemp -d)"
bc8f83a5
DK
265 addtrap "cd /; rm -rf '$(escape_shell "$TMPWORKINGDIRECTORY")';"
266 if [ -n "$TMPDIR_ADD" ]; then
267 TMPWORKINGDIRECTORY="${TMPWORKINGDIRECTORY}/${TMPDIR_ADD}"
268 mkdir -p "$TMPWORKINGDIRECTORY"
269 unset TMPDIR_ADD
270 export TMPDIR="$TMPWORKINGDIRECTORY"
271 fi
63c71412 272 msgninfo "Preparing environment for ${0##*/} in ${TMPWORKINGDIRECTORY}…"
5c0dd6fc 273
03aa0847 274 mkdir -m 700 "${TMPWORKINGDIRECTORY}/downloaded"
68ba0b7f
DK
275 if [ "$(id -u)" = '0' ]; then
276 # relax permissions so that running as root with user switching works
277 umask 022
03aa0847
DK
278 chmod 711 "$TMPWORKINGDIRECTORY"
279 chown _apt:root "${TMPWORKINGDIRECTORY}/downloaded"
68ba0b7f
DK
280 fi
281
63c71412 282 TESTDIRECTORY="$(readlink -f "$(dirname $0)")"
5c0dd6fc 283 # allow overriding the default BUILDDIR location
63c71412
DK
284 SOURCEDIRECTORY="${APT_INTEGRATION_TESTS_SOURCE_DIR:-"${TESTDIRECTORY}/../../"}"
285 BUILDDIRECTORY="${APT_INTEGRATION_TESTS_BUILD_DIR:-"${TESTDIRECTORY}/../../build/bin"}"
286 LIBRARYPATH="${APT_INTEGRATION_TESTS_LIBRARY_PATH:-"${BUILDDIRECTORY}"}"
287 METHODSDIR="${APT_INTEGRATION_TESTS_METHODS_DIR:-"${BUILDDIRECTORY}/methods"}"
288 APTHELPERBINDIR="${APT_INTEGRATION_TESTS_LIBEXEC_DIR:-"${BUILDDIRECTORY}"}"
289 APTWEBSERVERBINDIR="${APT_INTEGRATION_TESTS_WEBSERVER_BIN_DIR:-"${BUILDDIRECTORY}"}"
290 APTINTERNALSOLVER="${APT_INTEGRATION_TESTS_INTERNAL_SOLVER:-"${BUILDDIRECTORY}/apt-internal-solver"}"
291 APTDUMPSOLVER="${APT_INTEGRATION_TESTS_DUMP_SOLVER:-"${BUILDDIRECTORY}/apt-dump-solver"}"
8d876415 292 test -x "${BUILDDIRECTORY}/apt-get" || msgdie "You need to build tree first"
5c0dd6fc
MV
293 # -----
294
63c71412 295 cd "$TMPWORKINGDIRECTORY"
cd725954 296 mkdir rootdir aptarchive keys
8d876415 297 cd rootdir
b29c3712 298 mkdir -p etc/apt/apt.conf.d etc/apt/sources.list.d etc/apt/trusted.gpg.d etc/apt/preferences.d
8fe964f1 299 mkdir -p usr/bin var/cache var/lib var/log tmp
cffea9af 300 mkdir -p var/lib/dpkg/info var/lib/dpkg/updates var/lib/dpkg/triggers
cd725954 301 touch var/lib/dpkg/available
8d876415 302 mkdir -p usr/lib/apt
63c71412 303 ln -s "${METHODSDIR}" usr/lib/apt/methods
1f6cf9e7
DK
304 if [ "$BUILDDIRECTORY" = "$LIBRARYPATH" ]; then
305 mkdir -p usr/lib/apt/solvers
306 ln -s "${BUILDDIRECTORY}/apt-dump-solver" usr/lib/apt/solvers/dump
307 ln -s "${BUILDDIRECTORY}/apt-internal-solver" usr/lib/apt/solvers/apt
308 echo "Dir::Bin::Solvers \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/solvers\";" > etc/apt/apt.conf.d/externalsolver.conf
309 fi
291a386f
MV
310 # use the autoremove from the BUILDDIRECTORY if its there, otherwise
311 # system
63c71412
DK
312 if [ -e "${BUILDDIRECTORY}/../../debian/apt.conf.autoremove" ]; then
313 ln -s "${BUILDDIRECTORY}/../../debian/apt.conf.autoremove" etc/apt/apt.conf.d/01autoremove
291a386f
MV
314 else
315 ln -s /etc/apt/apt.conf.d/01autoremove etc/apt/apt.conf.d/01autoremove
316 fi
8d876415 317 cd ..
63c71412
DK
318 local BASENAME="${0##*/}"
319 local PACKAGESFILE="Packages-${BASENAME#*-}"
53ea1b56
DK
320 if [ -f "${TESTDIRECTORY}/${PACKAGESFILE}" ]; then
321 cp "${TESTDIRECTORY}/${PACKAGESFILE}" aptarchive/Packages
8f8169ac 322 fi
63c71412 323 local SOURCESSFILE="Sources-${BASENAME#*-}"
53ea1b56
DK
324 if [ -f "${TESTDIRECTORY}/${SOURCESSFILE}" ]; then
325 cp "${TESTDIRECTORY}/${SOURCESSFILE}" aptarchive/Sources
8f8169ac 326 fi
3abb6a6a
DK
327 find "$TESTDIRECTORY" \( -name '*.pub' -o -name '*.sec' \) -exec cp '{}' keys/ \;
328 chmod 644 keys/*
63c71412 329 ln -s "${TMPWORKINGDIRECTORY}/keys/joesixpack.pub" rootdir/etc/apt/trusted.gpg.d/joesixpack.gpg
d6cf2345 330
cffea9af 331 echo "Dir \"${TMPWORKINGDIRECTORY}/rootdir\";" > aptconfig.conf
94eb3bee 332 echo "Dir::state::status \"${TMPWORKINGDIRECTORY}/rootdir/var/lib/dpkg/status\";" >> aptconfig.conf
8d876415 333 echo "APT::Get::Show-User-Simulation-Note \"false\";" >> aptconfig.conf
9d2a8a73 334 echo "Dir::Bin::Methods \"${TMPWORKINGDIRECTORY}/rootdir/usr/lib/apt/methods\";" >> aptconfig.conf
23e64f6d
DK
335 # either store apt-key were we can access it, even if we run it as a different user
336 #cp "${BUILDDIRECTORY}/apt-key" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/"
337 #chmod o+rx "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apt-key"
338 #echo "Dir::Bin::apt-key \"${TMPWORKINGDIRECTORY}/rootdir/usr/bin/apt-key\";" >> aptconfig.conf
339 # destroys coverage reporting though, so we disable changing user for the calling gpgv
340 echo "Dir::Bin::apt-key \"${BUILDDIRECTORY}/apt-key\";" >> aptconfig.conf
68ba0b7f 341 if [ "$(id -u)" = '0' ]; then
23e64f6d 342 echo 'Binary::gpgv::Debug::NoDropPrivs "true";' >>aptconfig.conf
68ba0b7f 343 fi
b0be0e09 344
63c71412 345 cat > "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" <<EOF
b0be0e09
DK
346#!/bin/sh
347set -e
3abb6a6a 348if [ -r '${TMPWORKINGDIRECTORY}/noopchroot.so' ]; then
63c71412 349 if [ -n "\$LD_LIBRARY_PATH" ]; then
3abb6a6a 350 export LD_LIBRARY_PATH='${TMPWORKINGDIRECTORY}:'"\${LD_LIBRARY_PATH}"
b0be0e09 351 else
3abb6a6a 352 export LD_LIBRARY_PATH='${TMPWORKINGDIRECTORY}'
b0be0e09 353 fi
921a9626 354 if [ -n "\$LD_PRELOAD" ]; then
63c71412 355 export LD_PRELOAD="noopchroot.so \${LD_PRELOAD}"
921a9626 356 else
63c71412 357 export LD_PRELOAD="noopchroot.so"
921a9626
DK
358 fi
359fi
63c71412
DK
360EOF
361 cp "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg"
362 cat >> "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" <<EOF
3abb6a6a
DK
363exec fakeroot '${DPKG:-dpkg}' --root='${TMPWORKINGDIRECTORY}/rootdir' \\
364 --log='${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log' \\
63c71412
DK
365 --force-not-root --force-bad-path "\$@"
366EOF
367 cat >> "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg" <<EOF
3abb6a6a
DK
368exec fakeroot gdb --quiet -ex run '${DPKG:-dpkg}' --args '${DPKG:-dpkg}' --root='${TMPWORKINGDIRECTORY}/rootdir' \\
369 --log='${TMPWORKINGDIRECTORY}/rootdir/var/log/dpkg.log' \\
b0be0e09
DK
370 --force-not-root --force-bad-path "\$@"
371EOF
921a9626 372 chmod +x "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg" "${TMPWORKINGDIRECTORY}/rootdir/usr/bin/gdb-dpkg"
b0be0e09
DK
373 echo "Dir::Bin::dpkg \"${TMPWORKINGDIRECTORY}/rootdir/usr/bin/dpkg\";" > rootdir/etc/apt/apt.conf.d/99dpkg
374
533fe3d1
DK
375 {
376 if ! command dpkg --assert-multi-arch >/dev/null 2>&1; then
377 echo "DPKG::options:: \"--force-architecture\";" # Added to test multiarch before dpkg is ready for it…
378 fi
87d6947d 379 echo 'quiet "0";'
533fe3d1
DK
380 echo 'quiet::NoUpdate "true";'
381 echo 'quiet::NoStatistic "true";'
382 # too distracting for users, but helpful to detect changes
383 echo 'Acquire::Progress::Ignore::ShowErrorText "true";'
384 echo 'Acquire::Progress::Diffpercent "true";'
385 # in testcases, it can appear as if localhost has a rotation setup,
386 # hide this as we can't really deal with it properly
387 echo 'Acquire::Failure::ShowIP "false";'
ebca2f25
DK
388 # fakeroot can't fake everything, so disabled in production but good for tests
389 echo 'APT::Sandbox::Verify "true";'
533fe3d1 390 } >> aptconfig.conf
d6cf2345 391
68ba0b7f
DK
392 cp "${TESTDIRECTORY}/apt.pem" "${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem"
393 if [ "$(id -u)" = '0' ]; then
394 chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem"
395 fi
396 echo "Acquire::https::CaInfo \"${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem\";" > rootdir/etc/apt/apt.conf.d/99https
d6cf2345 397 echo "Apt::Cmd::Disable-Script-Warning \"1\";" > rootdir/etc/apt/apt.conf.d/apt-binary
23d35ec1 398 echo 'Acquire::Connect::AddrConfig "false";' > rootdir/etc/apt/apt.conf.d/connect-addrconfig
493a813e 399
276e51dd 400 configcompression '.' 'gz' #'bz2' 'lzma' 'xz'
51c04562 401 confighashes 'SHA256' # these are tests, not security best-practices
77a45beb 402
4bb006d1 403 # create some files in /tmp and look at user/group to get what this means
eab57e08 404 TEST_DEFAULT_USER="$(id -un)"
4bb006d1
DK
405 if [ "$(uname)" = 'GNU/kFreeBSD' ]; then
406 TEST_DEFAULT_GROUP='root'
407 else
eab57e08 408 TEST_DEFAULT_GROUP="$(id -gn)"
4bb006d1
DK
409 fi
410
ce7f128c 411 # cleanup the environment a bit
995a4bf6 412 # prefer our apt binaries over the system apt binaries
8c617819 413 export PATH="${BUILDDIRECTORY}:${PATH}:/usr/local/sbin:/usr/sbin:/sbin"
be233796 414 export LC_ALL=C.UTF-8
3b8eb3bc 415 unset LANGUAGE APT_CONFIG
ce7f128c 416 unset GREP_OPTIONS DEB_BUILD_PROFILES
59f57473 417 unset http_proxy HTTP_PROXY https_proxy HTTPS_PROXY no_proxy
0cbb7e29
JAK
418
419 # If gpgv supports --weak-digest, pass it to make sure we can disable SHA1
420 if aptkey verify --weak-digest SHA1 --help 2>/dev/null >/dev/null; then
421 echo 'Acquire::gpgv::Options { "--weak-digest"; "sha1"; };' > rootdir/etc/apt/apt.conf.d/no-sha1
422 fi
423
8d876415
DK
424 msgdone "info"
425}
426
ea65d079
DK
427getarchitecture() {
428 if [ "$1" = "native" -o -z "$1" ]; then
429 eval `aptconfig shell ARCH APT::Architecture`
430 if [ -n "$ARCH" ]; then
431 echo $ARCH
432 else
5834d7a1 433 dpkg --print-architecture
ea65d079
DK
434 fi
435 else
436 echo $1
437 fi
438}
439
53ea1b56 440getarchitectures() {
081c9d44 441 aptconfig dump --no-empty --format '%v%n' APT::Architecture APT::Architectures | sort -u | tr '\n' ' '
53ea1b56
DK
442}
443
3dcdc1f9
DK
444getarchitecturesfromcommalist() {
445 echo "$1" | sed -e 's#,#\n#g' | sed -e "s/^native\$/$(getarchitecture 'native')/"
446}
447
8d876415 448configarchitecture() {
18908589
DK
449 {
450 echo "APT::Architecture \"$(getarchitecture $1)\";"
451 while [ -n "$1" ]; do
452 echo "APT::Architectures:: \"$(getarchitecture $1)\";"
453 shift
454 done
455 } >rootdir/etc/apt/apt.conf.d/01multiarch.conf
53ea1b56
DK
456 configdpkg
457}
458
459configdpkg() {
460 if [ ! -e rootdir/var/lib/dpkg/status ]; then
63c71412
DK
461 local BASENAME="${0##*/}"
462 local STATUSFILE="status-${BASENAME#*-}"
53ea1b56
DK
463 if [ -f "${TESTDIRECTORY}/${STATUSFILE}" ]; then
464 cp "${TESTDIRECTORY}/${STATUSFILE}" rootdir/var/lib/dpkg/status
465 else
466 echo -n > rootdir/var/lib/dpkg/status
467 fi
468 fi
18908589 469 rm -f rootdir/etc/apt/apt.conf.d/00foreigndpkg
846856f4 470 if command dpkg --assert-multi-arch >/dev/null 2>&1 ; then
53ea1b56
DK
471 local ARCHS="$(getarchitectures)"
472 if echo "$ARCHS" | grep -E -q '[^ ]+ [^ ]+'; then
473 DPKGARCH="$(dpkg --print-architecture)"
474 for ARCH in ${ARCHS}; do
feae193b 475 if [ "${ARCH}" != "${DPKGARCH}" ]; then
18908589 476 if ! dpkg --add-architecture ${ARCH} >/dev/null 2>&1; then
feae193b 477 # old-style used e.g. in Ubuntu-P – and as it seems travis
18908589
DK
478 echo "DPKG::options:: \"--foreign-architecture\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
479 echo "DPKG::options:: \"${ARCH}\";" >> rootdir/etc/apt/apt.conf.d/00foreigndpkg
feae193b
DK
480 fi
481 fi
53ea1b56
DK
482 done
483 if [ "0" = "$(dpkg -l dpkg 2> /dev/null | grep '^i' | wc -l)" ]; then
05343a22
DK
484 # dpkg doesn't really check the version as long as it is fully installed,
485 # but just to be sure we choose one above the required version
486 insertinstalledpackage 'dpkg' "all" '1.16.2+fake'
53ea1b56
DK
487 fi
488 fi
489 fi
8d876415
DK
490}
491
0c787570
DK
492configdpkgnoopchroot() {
493 # create a library to noop chroot() and rewrite maintainer script executions
494 # via execvp() as used by dpkg as we don't want our rootdir to be a fullblown
495 # chroot directory dpkg could chroot into to execute the maintainer scripts
496 msgtest 'Building library to preload to make maintainerscript work in' 'dpkg'
63c71412 497 cat > noopchroot.c << EOF
0c787570
DK
498#define _GNU_SOURCE
499#include <stdio.h>
500#include <stdlib.h>
501#include <string.h>
502#include <dlfcn.h>
503
504static char * chrootdir = NULL;
505
506int chroot(const char *path) {
507 printf("WARNING: CHROOTing to %s was ignored!\n", path);
508 free(chrootdir);
509 chrootdir = strdup(path);
510 return 0;
511}
512int execvp(const char *file, char *const argv[]) {
513 static int (*func_execvp) (const char *, char * const []) = NULL;
514 if (func_execvp == NULL)
515 func_execvp = (int (*) (const char *, char * const [])) dlsym(RTLD_NEXT, "execvp");
516 if (chrootdir == NULL || strncmp(file, "/var/lib/dpkg/", strlen("/var/lib/dpkg/")) != 0)
517 return func_execvp(file, argv);
518 printf("REWRITE execvp call %s into %s\n", file, chrootdir);
950733c9
JAK
519 char *newfile;
520 if (asprintf(&newfile, "%s%s", chrootdir, file) == -1) {
521 perror("asprintf");
522 return -1;
523 }
b0be0e09 524 char const * const baseadmindir = "/var/lib/dpkg";
950733c9
JAK
525 char *admindir;
526 if (asprintf(&admindir, "%s%s", chrootdir, baseadmindir) == -1) {
527 perror("asprintf");
528 return -1;
529 }
b0be0e09 530 setenv("DPKG_ADMINDIR", admindir, 1);
0c787570
DK
531 return func_execvp(newfile, argv);
532}
533EOF
a66e1837 534 testempty --nomsg gcc -Wall -Wextra -fPIC -shared -o noopchroot.so noopchroot.c -ldl
0c787570 535}
276e51dd 536configcompression() {
abec2980 537 local CMD='apthelper cat-file -C'
276e51dd
DK
538 while [ -n "$1" ]; do
539 case "$1" in
3c528b91 540 '.') printf ".\t.\tcat\n";;
abec2980
DK
541 'gz') printf "gzip\tgz\t$CMD $1\n";;
542 'bz2') printf "bzip2\tbz2\t$CMD $1\n";;
543 *) printf "$1\t$1\t$CMD $1\n";;
276e51dd
DK
544 esac
545 shift
63c71412 546 done > "${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf"
276e51dd 547}
c5ede4ca
DK
548confighashes() {
549 {
550 echo 'APT::FTPArchive {'
551 {
552 while [ -n "$1" ]; do
553 printf "$1" | tr 'a-z' 'A-Z'
554 printf "\t\"true\";\n"
555 shift
556 done
557 for h in 'MD5' 'SHA1' 'SHA256' 'SHA512'; do
558 printf "$h\t\"false\";\n"
559 done
560 } | awk '!x[$1]++'
561 echo '};'
562 } >> "${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/ftparchive-hashes.conf"
563}
5f982b9d
DK
564forcecompressor() {
565 COMPRESSOR="$1"
912a6131 566 COMPRESS="$1"
abec2980 567 COMPRESSOR_CMD="apthelper cat-file -C $1"
5f982b9d
DK
568 case $COMPRESSOR in
569 gzip) COMPRESS='gz';;
570 bzip2) COMPRESS='bz2';;
5f982b9d
DK
571 esac
572 local CONFFILE="${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
573 echo "Acquire::CompressionTypes::Order { \"${COMPRESS}\"; };
912a6131 574Dir::Bin::uncompressed \"/does/not/exist\";" > "$CONFFILE"
b2fd8524
DK
575 for COMP in $(aptconfig dump 'APT::Compressor' --format '%f%n' | cut -d':' -f 5 | uniq); do
576 if [ -z "$COMP" -o "$COMP" = '.' -o "$COMP" = "$COMPRESSOR" ]; then continue; fi
577 echo "Dir::Bin::${COMP} \"/does/not/exist\";" >> "$CONFFILE"
578 echo "APT::Compressor::${COMP}::Name \"${COMP}-disabled\";" >> "$CONFFILE"
579 done
5f982b9d
DK
580}
581
75954ae2 582setupsimplenativepackage() {
ce9864a8
DK
583 local NAME="$1"
584 local ARCH="$2"
585 local VERSION="$3"
586 local RELEASE="${4:-unstable}"
587 local DEPENDENCIES="$5"
14109555 588 local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
18908589
DK
589 If you find such a package installed on your system,
590 something went horribly wrong! They are autogenerated
3d284148 591 und used only by testcases and serve no other purpose…"}"
18908589 592
b7899b00
DK
593 local SECTION="${7:-others}"
594 local DISTSECTION
63c71412 595 if [ "$SECTION" = "${SECTION#*/}" ]; then
b7899b00
DK
596 DISTSECTION="main"
597 else
63c71412 598 DISTSECTION="${SECTION%/*}"
b7899b00 599 fi
ce9864a8
DK
600 local BUILDDIR=incoming/${NAME}-${VERSION}
601 mkdir -p ${BUILDDIR}/debian/source
602 cd ${BUILDDIR}
603 echo "* most suckless software product ever" > FEATURES
0340069c 604 test -e debian/copyright || echo "Copyleft by Joe Sixpack $(date -u +%Y)" > debian/copyright
b7899b00 605 test -e debian/changelog || echo "$NAME ($VERSION) $RELEASE; urgency=low
ce9864a8
DK
606
607 * Initial release
608
0340069c 609 -- Joe Sixpack <joe@example.org> $(date -u -R)" > debian/changelog
b7899b00
DK
610 test -e debian/control || echo "Source: $NAME
611Section: $SECTION
ce9864a8
DK
612Priority: optional
613Maintainer: Joe Sixpack <joe@example.org>
614Build-Depends: debhelper (>= 7)
615Standards-Version: 3.9.1
616
875bcb36
DK
617Package: $NAME" > debian/control
618 if [ "$ARCH" = 'all' ]; then
619 echo "Architecture: all" >> debian/control
620 else
621 echo "Architecture: any" >> debian/control
622 fi
ce9864a8 623 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> debian/control
18908589
DK
624 echo "Description: $DESCRIPTION" >> debian/control
625
b7899b00
DK
626 test -e debian/compat || echo "7" > debian/compat
627 test -e debian/source/format || echo "3.0 (native)" > debian/source/format
ce9864a8 628 test -e debian/rules || cp /usr/share/doc/debhelper/examples/rules.tiny debian/rules
75954ae2
DK
629 cd - > /dev/null
630}
631
632buildsimplenativepackage() {
633 local NAME="$1"
d56e2917
DK
634 local NM
635 if [ "$(echo "$NAME" | cut -c 1-3)" = 'lib' ]; then
636 NM="$(echo "$NAME" | cut -c 1-4)"
637 else
638 NM="$(echo "$NAME" | cut -c 1)"
639 fi
75954ae2
DK
640 local ARCH="$2"
641 local VERSION="$3"
642 local RELEASE="${4:-unstable}"
643 local DEPENDENCIES="$5"
14109555 644 local DESCRIPTION="${6:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASE}
18908589
DK
645 If you find such a package installed on your system,
646 something went horribly wrong! They are autogenerated
3d284148 647 und used only by testcases and serve no other purpose…"}"
18908589 648
75954ae2 649 local SECTION="${7:-others}"
d67004e0 650 local PRIORITY="${8:-optional}"
63c71412
DK
651 local FILE_TREE="$9"
652 local COMPRESS_TYPE="${10:-gzip}"
75954ae2 653 local DISTSECTION
63c71412 654 if [ "$SECTION" = "${SECTION#*/}" ]; then
75954ae2
DK
655 DISTSECTION="main"
656 else
63c71412 657 DISTSECTION="${SECTION%/*}"
75954ae2 658 fi
63c71412 659 local BUILDDIR="${TMPWORKINGDIRECTORY}/incoming/${NAME}-${VERSION}"
b761356f 660
0c787570 661 msgtest "Build source package in version ${VERSION} for ${RELEASE} in ${DISTSECTION}" "$NAME"
63c71412
DK
662 mkdir -p "$BUILDDIR/debian/source"
663 echo "* most suckless software product ever" > "${BUILDDIR}/FEATURES"
b761356f 664 echo "#!/bin/sh
63c71412 665echo '$NAME says \"Hello!\"'" > "${BUILDDIR}/${NAME}"
b761356f 666
0340069c 667 echo "Copyleft by Joe Sixpack $(date -u +%Y)" > "${BUILDDIR}/debian/copyright"
b761356f
DK
668 echo "$NAME ($VERSION) $RELEASE; urgency=low
669
670 * Initial release
671
0340069c 672 -- Joe Sixpack <joe@example.org> $(date -u -R)" > "${BUILDDIR}/debian/changelog"
63c71412
DK
673 {
674 echo "Source: $NAME
d67004e0 675Priority: $PRIORITY
b761356f 676Maintainer: Joe Sixpack <joe@example.org>
63c71412
DK
677Standards-Version: 3.9.3"
678 if [ "$SECTION" != '<none>' ]; then
679 echo "Section: $SECTION"
680 fi
681 local BUILDDEPS="$(echo "$DEPENDENCIES" | grep '^Build-')"
682 test -z "$BUILDDEPS" || echo "$BUILDDEPS"
683 echo "
684Package: $NAME"
b761356f 685
63c71412
DK
686 if [ "$ARCH" = 'all' ]; then
687 echo "Architecture: all"
688 else
689 echo "Architecture: any"
690 fi
691 local DEPS="$(echo "$DEPENDENCIES" | grep -v '^Build-')"
692 test -z "$DEPS" || echo "$DEPS"
693 echo "Description: $DESCRIPTION"
694 } > "${BUILDDIR}/debian/control"
01f520ce 695
63c71412
DK
696 echo '3.0 (native)' > "${BUILDDIR}/debian/source/format"
697 cd "${BUILDDIR}/.."
0c787570
DK
698 testsuccess --nomsg dpkg-source -b ${NAME}-${VERSION}
699 cd - >/dev/null
63c71412 700 sed -n 's#^dpkg-source: info: building [^ ]\+ in ##p' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output" \
f1828b69 701 | while read SRC; do
63c71412 702 echo "pool/${SRC}" >> "${BUILDDIR}/../${RELEASE}.${DISTSECTION}.srclist"
f1828b69 703# if expr match "${SRC}" '.*\.dsc' >/dev/null 2>&1; then
33a22672 704# aptkey --keyring ./keys/joesixpack.pub --secret-keyring ./keys/joesixpack.sec --quiet --readonly \
bd7fb5aa 705# adv --yes --default-key 'Joe Sixpack' \
f1828b69
DK
706# --clearsign -o "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
707# mv "${BUILDDIR}/../${SRC}.sign" "${BUILDDIR}/../$SRC"
708# fi
b761356f 709 done
84aa13f4 710
3dcdc1f9 711 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
0c787570 712 msgtest "Build binary package for ${RELEASE} in ${SECTION}" "$NAME"
63c71412
DK
713 rm -rf "${BUILDDIR}/debian/tmp"
714 mkdir -p "${BUILDDIR}/debian/tmp/DEBIAN" "${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}" "${BUILDDIR}/debian/tmp/usr/bin"
715 cp "${BUILDDIR}/debian/copyright" "${BUILDDIR}/debian/changelog" "${BUILDDIR}/FEATURES" "${BUILDDIR}/debian/tmp/usr/share/doc/${NAME}"
716 cp "${BUILDDIR}/${NAME}" "${BUILDDIR}/debian/tmp/usr/bin/${NAME}-${arch}"
42c1513b 717 if [ -n "$FILE_TREE" ]; then
63c71412 718 cp -ar "$FILE_TREE" "${BUILDDIR}/debian/tmp"
42c1513b 719 fi
42c1513b 720
63c71412
DK
721 (cd "${BUILDDIR}"; dpkg-gencontrol -DArchitecture=$arch)
722 (cd "${BUILDDIR}/debian/tmp"; md5sum $(find usr/ -type f) > DEBIAN/md5sums)
846856f4 723 local LOG="${BUILDDIR}/../${NAME}_${VERSION}_${arch}.dpkg-deb.log"
d56e2917 724 # ensure the right permissions as dpkg-deb insists
63c71412
DK
725 chmod 755 "${BUILDDIR}/debian/tmp/DEBIAN"
726 testsuccess --nomsg dpkg-deb -Z${COMPRESS_TYPE} --build "${BUILDDIR}/debian/tmp" "${BUILDDIR}/.."
727 echo "pool/${NAME}_${VERSION}_${arch}.deb" >> "${BUILDDIR}/../${RELEASE}.${DISTSECTION}.pkglist"
84aa13f4
DK
728 done
729
d56e2917 730 local CHANGEPATH="${BUILDDIR}/../${DISTSECTION}/${NM}/${NAME}/${NAME}_${VERSION}"
63c71412
DK
731 mkdir -p "$CHANGEPATH"
732 cp "${BUILDDIR}/debian/changelog" "$CHANGEPATH"
5a635ee4 733 rm -rf "${BUILDDIR}"
b761356f 734 msgdone "info"
75954ae2
DK
735}
736
737buildpackage() {
738 local BUILDDIR=$1
739 local RELEASE=$2
740 local SECTION=$3
ea65d079 741 local ARCH=$(getarchitecture $4)
846856f4
DK
742 local PKGNAME="$(echo "$BUILDDIR" | grep -o '[^/]*$')"
743 local BUILDLOG="$(readlink -f "${BUILDDIR}/../${PKGNAME}_${RELEASE}_${SECTION}.dpkg-bp.log")"
0c787570 744 msgtest "Build package for ${RELEASE} in ${SECTION}" "$PKGNAME"
63c71412 745 cd "$BUILDDIR"
75954ae2
DK
746 if [ "$ARCH" = "all" ]; then
747 ARCH="$(dpkg-architecture -qDEB_HOST_ARCH 2> /dev/null)"
748 fi
0c787570 749 testsuccess --nomsg dpkg-buildpackage -uc -us -a$ARCH
63c71412
DK
750 cp "${TMPWORKINGDIRECTORY}/rootdir/tmp/testsuccess.output" "$BUILDLOG"
751 local PKGS="$(grep '^dpkg-deb: building package' "$BUILDLOG" | cut -d'/' -f 2 | sed -e "s#'\.##")"
752 local SRCS="$(grep '^dpkg-source: info: building' "$BUILDLOG" | grep -o '[a-z0-9._+~-]*$')"
ce9864a8 753 cd - > /dev/null
b7899b00 754 for PKG in $PKGS; do
63c71412 755 echo "pool/${PKG}" >> "${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.pkglist"
b7899b00
DK
756 done
757 for SRC in $SRCS; do
63c71412 758 echo "pool/${SRC}" >> "${TMPWORKINGDIRECTORY}/incoming/${RELEASE}.${SECTION}.srclist"
b7899b00 759 done
ce9864a8
DK
760}
761
762buildaptarchive() {
ce9864a8 763 if [ -d incoming ]; then
846856f4 764 buildaptarchivefromincoming "$@"
ce9864a8 765 else
846856f4 766 buildaptarchivefromfiles "$@"
ce9864a8
DK
767 fi
768}
769
770createaptftparchiveconfig() {
63c71412 771 local COMPRESSORS="$(cut -d' ' -f 1 "${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf" | tr '\n' ' ')"
016bea82
DK
772 local COMPRESSORS="${COMPRESSORS%* }"
773 local ARCHS="$(getarchitectures)"
c5ede4ca
DK
774 cat > ftparchive.conf <<EOF
775Dir {
776 ArchiveDir "$(readlink -f .)";
777 CacheDir "$(readlink -f ..)";
778 FileListDir "$(readlink -f pool/)";
b7899b00
DK
779};
780Default {
c5ede4ca
DK
781 Packages::Compress "$COMPRESSORS";
782 Sources::Compress "$COMPRESSORS";
783 Contents::Compress "$COMPRESSORS";
784 Translation::Compress "$COMPRESSORS";
18331adf 785 LongDescription "false";
ce9864a8
DK
786};
787TreeDefault {
788 Directory "pool/";
789 SrcDirectory "pool/";
790};
c5ede4ca 791EOF
b7899b00 792 for DIST in $(find ./pool/ -maxdepth 1 -name '*.pkglist' -type f | cut -d'/' -f 3 | cut -d'.' -f 1 | sort | uniq); do
63c71412
DK
793 cat <<EOF
794tree "dists/$DIST" {
1dd20368 795 Architectures "$ARCHS all source";
63c71412
DK
796 FileList "${DIST}.\$(SECTION).pkglist";
797 SourceFileList "${DIST}.\$(SECTION).srclist";
798 Sections "$(find ./pool/ -maxdepth 1 -name "${DIST}.*.pkglist" -type f | cut -d'/' -f 3 | cut -d'.' -f 2 | sort | uniq | tr '\n' ' ')";
799};
800EOF
801 done >> ftparchive.conf
ce9864a8
DK
802}
803
804buildaptftparchivedirectorystructure() {
b7899b00
DK
805 local DISTS="$(grep -i '^tree ' ftparchive.conf | cut -d'/' -f 2 | sed -e 's#".*##')"
806 for DIST in $DISTS; do
807 local SECTIONS="$(grep -i -A 5 "dists/$DIST" ftparchive.conf | grep -i 'Sections' | cut -d'"' -f 2)"
808 for SECTION in $SECTIONS; do
809 local ARCHS="$(grep -A 5 "dists/$DIST" ftparchive.conf | grep Architectures | cut -d'"' -f 2 | sed -e 's#source##')"
810 for ARCH in $ARCHS; do
63c71412 811 mkdir -p "dists/${DIST}/${SECTION}/binary-${ARCH}"
b7899b00 812 done
63c71412
DK
813 mkdir -p "dists/${DIST}/${SECTION}/source"
814 mkdir -p "dists/${DIST}/${SECTION}/i18n"
b7899b00 815 done
ce9864a8 816 done
ce9864a8
DK
817}
818
9b78cda6 819insertpackage() {
10e100e5 820 local RELEASES="$1"
9b78cda6
DK
821 local NAME="$2"
822 local ARCH="$3"
823 local VERSION="$4"
824 local DEPENDENCIES="$5"
d67004e0 825 local PRIORITY="${6:-optional}"
10e100e5 826 local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/${RELEASES}
18908589
DK
827 If you find such a package installed on your system,
828 something went horribly wrong! They are autogenerated
3d284148 829 und used only by testcases and serve no other purpose…"}"
d67004e0 830 local ARCHS=""
10e100e5
DK
831 for RELEASE in $(printf '%s' "$RELEASES" | tr ',' '\n'); do
832 if [ "$RELEASE" = 'installed' ]; then
833 insertinstalledpackage "$2" "$3" "$4" "$5" "$6" "$7"
834 continue
d67004e0 835 fi
10e100e5 836 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
1dd20368 837 if [ "$arch" = 'none' ]; then
10e100e5
DK
838 ARCHS="$(getarchitectures)"
839 else
840 ARCHS="$arch"
841 fi
842 for BUILDARCH in $ARCHS; do
843 local PPATH="aptarchive/dists/${RELEASE}/main/binary-${BUILDARCH}"
63c71412
DK
844 mkdir -p "$PPATH"
845 {
846 echo "Package: $NAME
d67004e0 847Priority: $PRIORITY
9b78cda6 848Section: other
2c085486 849Installed-Size: 42
63c71412
DK
850Maintainer: Joe Sixpack <joe@example.org>"
851 test "$arch" = 'none' || echo "Architecture: $arch"
852 echo "Version: $VERSION
853Filename: pool/main/${NAME}/${NAME}_${VERSION}_${arch}.deb"
854 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES"
855 echo "Description: $(printf '%s' "$DESCRIPTION" | head -n 1)"
856 echo "Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)"
857 echo
858 } >> "${PPATH}/Packages"
10e100e5 859 done
d67004e0 860 done
63c71412
DK
861 mkdir -p "aptarchive/dists/${RELEASE}/main/source" "aptarchive/dists/${RELEASE}/main/i18n"
862 touch "aptarchive/dists/${RELEASE}/main/source/Sources"
10e100e5 863 echo "Package: $NAME
0e0d9919
DK
864Description-md5: $(printf '%s' "$DESCRIPTION" | md5sum | cut -d' ' -f 1)
865Description-en: $DESCRIPTION
63c71412 866" >> "aptarchive/dists/${RELEASE}/main/i18n/Translation-en"
10e100e5 867 done
9b78cda6
DK
868}
869
234675b7 870insertsource() {
07aca07a 871 local RELEASES="$1"
234675b7
DK
872 local NAME="$2"
873 local ARCH="$3"
874 local VERSION="$4"
875 local DEPENDENCIES="$5"
07aca07a 876 local BINARY="${6:-$NAME}"
234675b7 877 local ARCHS=""
07aca07a
DK
878 for RELEASE in $(printf '%s' "$RELEASES" | tr ',' '\n'); do
879 local SPATH="aptarchive/dists/${RELEASE}/main/source"
880 mkdir -p $SPATH
881 local FILE="${SPATH}/Sources"
882 local DSCFILE="${NAME}_${VERSION}.dsc"
883 local TARFILE="${NAME}_${VERSION}.tar.gz"
884 echo "Package: $NAME
a8275acf 885Binary: $BINARY
234675b7
DK
886Version: $VERSION
887Maintainer: Joe Sixpack <joe@example.org>
888Architecture: $ARCH" >> $FILE
07aca07a
DK
889 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> "$FILE"
890 echo "Files:
63c71412
DK
891 $(echo -n "$DSCFILE" | md5sum | cut -d' ' -f 1) $(echo -n "$DSCFILE" | wc -c) "$DSCFILE"
892 $(echo -n "$TARFILE" | md5sum | cut -d' ' -f 1) $(echo -n "$TARFILE" | wc -c) "$TARFILE"
55ae7a51 893Checksums-Sha256:
63c71412
DK
894 $(echo -n "$DSCFILE" | sha256sum | cut -d' ' -f 1) $(echo -n "$DSCFILE" | wc -c) "$DSCFILE"
895 $(echo -n "$TARFILE" | sha256sum | cut -d' ' -f 1) $(echo -n "$TARFILE" | wc -c) "$TARFILE"
896" >> "$FILE"
07aca07a 897 done
234675b7
DK
898}
899
dfc2b1be
DK
900insertinstalledpackage() {
901 local NAME="$1"
902 local ARCH="$2"
903 local VERSION="$3"
904 local DEPENDENCIES="$4"
d67004e0 905 local PRIORITY="${5:-optional}"
d4b4e5ea 906 local STATUS="${6:-install ok installed}"
14109555 907 local DESCRIPTION="${7:-"an autogenerated dummy ${NAME}=${VERSION}/installed
18908589
DK
908 If you find such a package installed on your system,
909 something went horribly wrong! They are autogenerated
3d284148 910 und used only by testcases and serve no other purpose…"}"
18908589 911
53ea1b56
DK
912 local FILE='rootdir/var/lib/dpkg/status'
913 local INFO='rootdir/var/lib/dpkg/info'
3dcdc1f9 914 for arch in $(getarchitecturesfromcommalist "$ARCH"); do
d67004e0 915 echo "Package: $NAME
d4b4e5ea 916Status: $STATUS
d67004e0 917Priority: $PRIORITY
dfc2b1be
DK
918Section: other
919Installed-Size: 42
920Maintainer: Joe Sixpack <joe@example.org>
63c71412
DK
921Version: $VERSION" >> "$FILE"
922 test "$arch" = 'none' || echo "Architecture: $arch" >> "$FILE"
923 test -z "$DEPENDENCIES" || echo "$DEPENDENCIES" >> "$FILE"
924 echo "Description: $DESCRIPTION" >> "$FILE"
925 echo >> "$FILE"
53ea1b56 926 if [ "$(dpkg-query -W --showformat='${Multi-Arch}')" = 'same' ]; then
63c71412 927 echo -n > "${INFO}/${NAME}:${arch}.list"
53ea1b56 928 else
63c71412 929 echo -n > "${INFO}/${NAME}.list"
53ea1b56 930 fi
d67004e0 931 done
dfc2b1be
DK
932}
933
934
ce9864a8 935buildaptarchivefromincoming() {
63c71412 936 msginfo "Build APT archive for ${CCMD}${0##*/}${CINFO} based on incoming packages…"
ce9864a8
DK
937 cd aptarchive
938 [ -e pool ] || ln -s ../incoming pool
939 [ -e ftparchive.conf ] || createaptftparchiveconfig
940 [ -e dists ] || buildaptftparchivedirectorystructure
b7899b00 941 msgninfo "\tGenerate Packages, Sources and Contents files… "
31be38d2 942 testsuccess aptftparchive generate ftparchive.conf
ce9864a8
DK
943 cd - > /dev/null
944 msgdone "info"
4dbfe436 945 generatereleasefiles "$@"
ce9864a8
DK
946}
947
948buildaptarchivefromfiles() {
63c71412 949 msginfo "Build APT archive for ${CCMD}${0##*/}${CINFO} based on prebuild files…"
8d041b4f
DK
950 local DIR='aptarchive'
951 if [ -d "${DIR}/dists" ]; then DIR="${DIR}/dists"; fi
952 find "$DIR" -name 'Packages' -o -name 'Sources' -o -name 'Translation-*' | while read line; do
9b78cda6 953 msgninfo "\t${line} file… "
276e51dd 954 compressfile "$line" "$1"
8d876415 955 msgdone "info"
9b78cda6 956 done
e3c62328 957 generatereleasefiles "$@"
9b78cda6
DK
958}
959
276e51dd 960compressfile() {
63c71412 961 cat "${TMPWORKINGDIRECTORY}/rootdir/etc/testcase-compressor.conf" | while read compressor extension command; do
276e51dd
DK
962 if [ "$compressor" = '.' ]; then
963 if [ -n "$2" ]; then
964 touch -d "$2" "$1"
965 fi
966 continue
967 fi
968 cat "$1" | $command > "${1}.${extension}"
969 if [ -n "$2" ]; then
970 touch -d "$2" "${1}.${extension}"
971 fi
972 done
973}
974
a3bbbab7 975# can be overridden by testcases for their pleasure
bf9e7447
DK
976getcodenamefromsuite() {
977 case "$1" in
978 unstable) echo 'sid';;
979 *) echo -n "$1";;
980 esac
981}
a3bbbab7 982getreleaseversionfromsuite() { true; }
718f797c 983getlabelfromsuite() { true; }
d56e2917 984getoriginfromsuite() { true; }
1dd20368 985getarchitecturesfromreleasefile() { echo "all $(getarchitectures)"; }
a3bbbab7 986
c5ede4ca
DK
987aptftparchiverelease() {
988 aptftparchive -qq release "$@" | sed -e '/0 Release$/ d' # remove the self reference
989}
9b78cda6 990generatereleasefiles() {
884a4c0a
DK
991 # $1 is the Date header and $2 is the ValidUntil header to be set
992 # both should be given in notation date/touch can understand
1dd20368
DK
993 local DATE="$1"
994 local VALIDUNTIL="$2"
9b78cda6 995 if [ -e aptarchive/dists ]; then
87d6947d 996 msgninfo "\tGenerate Release files for dists… "
9b78cda6 997 for dir in $(find ./aptarchive/dists -mindepth 1 -maxdepth 1 -type d); do
1dd20368 998 local ARCHITECTURES="$(getarchitecturesfromreleasefile "$dir")"
a3bbbab7
DK
999 local SUITE="$(echo "$dir" | cut -d'/' -f 4)"
1000 local CODENAME="$(getcodenamefromsuite $SUITE)"
1001 local VERSION="$(getreleaseversionfromsuite $SUITE)"
718f797c 1002 local LABEL="$(getlabelfromsuite $SUITE)"
d56e2917 1003 local ORIGIN="$(getoriginfromsuite $SUITE)"
63c71412 1004 aptftparchiverelease "$dir" \
884a4c0a
DK
1005 -o APT::FTPArchive::Release::Suite="${SUITE}" \
1006 -o APT::FTPArchive::Release::Codename="${CODENAME}" \
bc7a59dd
DK
1007 -o APT::FTPArchive::Release::Architectures="${ARCHITECTURES}" \
1008 -o APT::FTPArchive::Release::Label="${LABEL}" \
1009 -o APT::FTPArchive::Release::Origin="${ORIGIN}" \
1010 -o APT::FTPArchive::Release::Version="${VERSION}" \
63c71412 1011 > "$dir/Release"
a3bbbab7 1012 if [ "$SUITE" = "experimental" -o "$SUITE" = "experimental2" ]; then
35faae11 1013 sed -i '/^Date: / a\
63c71412 1014NotAutomatic: yes' "$dir/Release"
35faae11 1015 fi
9b78cda6
DK
1016 done
1017 else
87d6947d 1018 msgninfo "\tGenerate Release files for flat… "
c5ede4ca 1019 aptftparchiverelease ./aptarchive > aptarchive/Release
8d876415 1020 fi
1dd20368 1021 if [ -n "$DATE" -a "$DATE" != "now" ]; then
fe0f7911 1022 for release in $(find ./aptarchive -name 'Release'); do
0340069c 1023 sed -i "s/^Date: .*$/Date: $(date -u -d "$DATE" '+%a, %d %b %Y %H:%M:%S %Z')/" "$release"
1dd20368 1024 touch -d "$DATE" "$release"
fe0f7911 1025 done
8d876415 1026 fi
1dd20368 1027 if [ -n "$VALIDUNTIL" ]; then
c5ede4ca 1028 sed -i "/^Date: / a\
0340069c 1029Valid-Until: $(date -u -d "$VALIDUNTIL" '+%a, %d %b %Y %H:%M:%S %Z')" $(find ./aptarchive -name 'Release')
c5ede4ca 1030 fi
b7899b00 1031 msgdone "info"
8d876415
DK
1032}
1033
b7899b00 1034setupdistsaptarchive() {
63c71412 1035 local APTARCHIVE="$(readlink -f ./aptarchive | sed 's# #%20#g')"
b7899b00
DK
1036 rm -f root/etc/apt/sources.list.d/apt-test-*-deb.list
1037 rm -f root/etc/apt/sources.list.d/apt-test-*-deb-src.list
1038 for DISTS in $(find ./aptarchive/dists/ -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 4); do
63c71412 1039 SECTIONS=$(find "./aptarchive/dists/${DISTS}/" -mindepth 1 -maxdepth 1 -type d | cut -d'/' -f 5 | tr '\n' ' ')
b7899b00 1040 msgninfo "\tadd deb and deb-src sources.list lines for ${CCMD}${DISTS} ${SECTIONS}${CINFO}… "
63c71412
DK
1041 echo "deb file://$APTARCHIVE $DISTS $SECTIONS" > "rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb.list"
1042 echo "deb-src file://$APTARCHIVE $DISTS $SECTIONS" > "rootdir/etc/apt/sources.list.d/apt-test-${DISTS}-deb-src.list"
b7899b00
DK
1043 msgdone "info"
1044 done
1045}
1046
1047setupflataptarchive() {
63c71412
DK
1048 local APTARCHIVE="$(readlink -f ./aptarchive)"
1049 local APTARCHIVEURI="$(readlink -f ./aptarchive | sed 's# #%20#g')"
1050 if [ -f "${APTARCHIVE}/Packages" ]; then
8d876415 1051 msgninfo "\tadd deb sources.list line… "
63c71412
DK
1052 echo "deb file://$APTARCHIVEURI /" > 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list'
1053 msgdone 'info'
8d876415 1054 else
63c71412 1055 rm -f 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb.list'
8d876415 1056 fi
63c71412 1057 if [ -f "${APTARCHIVE}/Sources" ]; then
8d876415 1058 msgninfo "\tadd deb-src sources.list line… "
63c71412
DK
1059 echo "deb-src file://$APTARCHIVEURI /" > 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list'
1060 msgdone 'info'
8d876415 1061 else
63c71412 1062 rm -f 'rootdir/etc/apt/sources.list.d/apt-test-archive-deb-src.list'
8d876415 1063 fi
b7899b00
DK
1064}
1065
1066setupaptarchive() {
4dbfe436
DK
1067 local NOUPDATE=0
1068 if [ "$1" = '--no-update' ]; then
1069 NOUPDATE=1
1070 shift
1071 fi
1072 buildaptarchive "$@"
b7899b00
DK
1073 if [ -e aptarchive/dists ]; then
1074 setupdistsaptarchive
1075 else
1076 setupflataptarchive
1077 fi
9d653a6d 1078 signreleasefiles 'Joe Sixpack'
4dbfe436 1079 if [ "1" != "$NOUPDATE" ]; then
5684f71f 1080 testsuccess aptget update -o Debug::pkgAcquire::Worker=true -o Debug::Acquire::gpgv=true
b2ea1a47 1081 fi
8d876415
DK
1082}
1083
f213b6ea 1084signreleasefiles() {
fb7b11eb 1085 local SIGNERS="${1:-Joe Sixpack}"
07cb47e7 1086 local REPODIR="${2:-aptarchive}"
1af227c2
DK
1087 if [ -n "$1" ]; then shift; fi
1088 if [ -n "$1" ]; then shift; fi
fb7b11eb
DK
1089 local KEY="keys/$(echo "$SIGNERS" | tr 'A-Z' 'a-z' | tr -d ' ,')"
1090 msgninfo "\tSign archive with $SIGNERS key $KEY… "
29a59c46
DK
1091 local REXKEY='keys/rexexpired'
1092 local SECEXPIREBAK="${REXKEY}.sec.bak"
1093 local PUBEXPIREBAK="${REXKEY}.pub.bak"
fb7b11eb
DK
1094 local SIGUSERS=""
1095 while [ -n "${SIGNERS%%,*}" ]; do
1096 local SIGNER="${SIGNERS%%,*}"
1097 if [ "${SIGNERS}" = "${SIGNER}" ]; then
1098 SIGNERS=""
1099 fi
1100 SIGNERS="${SIGNERS#*,}"
1101 # FIXME: This should be the full name, but we can't encode the space properly currently
1102 SIGUSERS="${SIGUSERS} -u ${SIGNER#* }"
1103 if [ "${SIGNER}" = 'Rex Expired' ]; then
1104 # the key is expired, so gpg doesn't allow to sign with and the --faked-system-time
1105 # option doesn't exist anymore (and using faketime would add a new obscure dependency)
1106 # therefore we 'temporary' make the key not expired and restore a backup after signing
1107 cp "${REXKEY}.sec" "$SECEXPIREBAK"
1108 cp "${REXKEY}.pub" "$PUBEXPIREBAK"
1109 local SECUNEXPIRED="${REXKEY}.sec.unexpired"
1110 local PUBUNEXPIRED="${REXKEY}.pub.unexpired"
1111 if [ -f "$SECUNEXPIRED" ] && [ -f "$PUBUNEXPIRED" ]; then
1112 cp "$SECUNEXPIRED" "${REXKEY}.sec"
1113 cp "$PUBUNEXPIRED" "${REXKEY}.pub"
1114 else
1115 if ! printf "expire\n1w\nsave\n" | aptkey --quiet --keyring "${REXKEY}.pub" --secret-keyring "${REXKEY}.sec" \
1116 --readonly adv --batch --yes --digest-algo "${APT_TESTS_DIGEST_ALGO:-SHA512}" \
1117 --default-key "$SIGNER" --command-fd 0 --edit-key "${SIGNER}" >setexpire.gpg 2>&1; then
1118 cat setexpire.gpg
1119 exit 1
1120 fi
1121 cp "${REXKEY}.sec" "$SECUNEXPIRED"
1122 cp "${REXKEY}.pub" "$PUBUNEXPIRED"
f1e1abd8 1123 fi
29a59c46 1124 fi
fb7b11eb
DK
1125 if [ ! -e "${KEY}.pub" ]; then
1126 local K="keys/$(echo "$SIGNER" | tr 'A-Z' 'a-z' | tr -d ' ,')"
1127 cat "${K}.pub" >> "${KEY}.new.pub"
1128 cat "${K}.sec" >> "${KEY}.new.sec"
1129 fi
1130 done
1131 if [ ! -e "${KEY}.pub" ]; then
1132 mv "${KEY}.new.pub" "${KEY}.pub"
1133 mv "${KEY}.new.sec" "${KEY}.sec"
29a59c46 1134 fi
fb7b11eb 1135 local GPG="aptkey --quiet --keyring ${KEY}.pub --secret-keyring ${KEY}.sec --readonly adv --batch --yes --digest-algo ${APT_TESTS_DIGEST_ALGO:-SHA512}"
63c71412 1136 for RELEASE in $(find "${REPODIR}/" -name Release); do
e3c62328 1137 # we might have set a specific date for the Release file, so copy it
761a5ad2
DK
1138 local DATE="$(stat --format "%y" "${RELEASE}")"
1139 if [ "$APT_DONT_SIGN" = 'Release.gpg' ]; then
1140 rm -f "${RELEASE}.gpg"
1141 else
1142 testsuccess $GPG "$@" $SIGUSERS --armor --detach-sign --sign --output "${RELEASE}.gpg" "${RELEASE}"
1143 touch -d "$DATE" "${RELEASE}.gpg"
1144 fi
1145 local INRELEASE="${RELEASE%/*}/InRelease"
1146 if [ "$APT_DONT_SIGN" = 'InRelease' ]; then
1147 rm -f "$INRELEASE"
1148 else
1149 testsuccess $GPG "$@" $SIGUSERS --clearsign --output "$INRELEASE" "$RELEASE"
1150 touch -d "$DATE" "${INRELEASE}"
1151 fi
f213b6ea 1152 done
29a59c46 1153 if [ -f "$SECEXPIREBAK" ] && [ -f "$PUBEXPIREBAK" ]; then
63c71412
DK
1154 mv -f "$SECEXPIREBAK" "${REXKEY}.sec"
1155 mv -f "$PUBEXPIREBAK" "${REXKEY}.pub"
29a59c46 1156 fi
63c71412 1157 msgdone 'info'
f213b6ea
DK
1158}
1159
8eafc759 1160redatereleasefiles() {
0340069c 1161 local DATE="$(date -u -d "$1" '+%a, %d %b %Y %H:%M:%S %Z')"
8eafc759 1162 for release in $(find aptarchive/ -name 'Release'); do
63c71412
DK
1163 sed -i "s/^Date: .*$/Date: ${DATE}/" "$release"
1164 touch -d "$DATE" "$release"
8eafc759
DK
1165 done
1166 signreleasefiles "${2:-Joe Sixpack}"
1167}
1168
f2c0ec8b 1169webserverconfig() {
6c0765c0 1170 local WEBSERVER="${3:-http://localhost:${APTHTTPPORT}}"
b0314abb
DK
1171 local NOCHECK=false
1172 if [ "$1" = '--no-check' ]; then
1173 NOCHECK=true
1174 shift
1175 fi
0d58c26a 1176 local DOWNLOG='rootdir/tmp/download-testfile.log'
b0314abb 1177 local STATUS='downloaded/webserverconfig.status'
0d58c26a 1178 rm -f "$STATUS" "$DOWNLOG"
6c0765c0 1179 # very very basic URI encoding
b0314abb
DK
1180 local URI
1181 if [ -n "$2" ]; then
1182 msgtest "Set webserver config option '${1}' to" "$2"
6c0765c0 1183 URI="${WEBSERVER}/_config/set/$(echo "${1}" | sed -e 's/\//%2f/g')/$(echo "${2}" | sed -e 's/\//%2f/g')"
b0314abb
DK
1184 else
1185 msgtest 'Clear webserver config option' "${1}"
6c0765c0 1186 URI="${WEBSERVER}/_config/clear/$(echo "${1}" | sed -e 's/\//%2f/g')"
b0314abb
DK
1187 fi
1188 if downloadfile "$URI" "$STATUS" > "$DOWNLOG"; then
f2c0ec8b
DK
1189 msgpass
1190 else
68042532
DK
1191 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/webserverconfig.output"
1192 cat "$DOWNLOG" "$STATUS" >"$OUTPUT" 2>&1 || true
1193 msgfailoutput '' "$OUTPUT"
f2c0ec8b 1194 fi
b0314abb 1195 $NOCHECK || testwebserverlaststatuscode '200'
f2c0ec8b
DK
1196}
1197
fd46d305 1198rewritesourceslist() {
63c71412 1199 local APTARCHIVE="file://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive" | sed 's# #%20#g')"
af9e40c9 1200 local APTARCHIVE2="copy://$(readlink -f "${TMPWORKINGDIRECTORY}/aptarchive" | sed 's# #%20#g')"
fd46d305 1201 for LIST in $(find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list'); do
af9e40c9 1202 sed -i $LIST -e "s#$APTARCHIVE#${1}#" -e "s#$APTARCHIVE2#${1}#" \
28b2efcb
DK
1203 -e "s#http://[^@]*@\?localhost:${APTHTTPPORT}/\?#${1}#" \
1204 -e "s#https://[^@]*@\?localhost:${APTHTTPSPORT}/\?#${1}#"
fd46d305
DK
1205 done
1206}
1207
5572f6bd
MV
1208# wait for up to 10s for a pid file to appear to avoid possible race
1209# when a helper is started and dosn't write the PID quick enough
1210waitforpidfile() {
1211 local PIDFILE="$1"
1212 for i in $(seq 10); do
1213 if test -s "$PIDFILE"; then
1214 return 0
1215 fi
1216 sleep 1
1217 done
1218 msgdie "waiting for $PIDFILE failed"
1219 return 1
1220}
1221
f213b6ea 1222changetowebserver() {
6c0765c0 1223 local REWRITE='no'
23af9f40 1224 if [ "$1" != '--no-rewrite' ]; then
6c0765c0 1225 REWRITE='yes'
23af9f40
DK
1226 else
1227 shift
1228 fi
63c71412 1229 if test -x "${APTWEBSERVERBINDIR}/aptwebserver"; then
fbd29dd6 1230 cd aptarchive
a0db467c 1231 local LOG="webserver.log"
6c0765c0 1232 if ! aptwebserver --port 0 -o aptwebserver::fork=1 -o aptwebserver::portfile='aptwebserver.port' "$@" >$LOG 2>&1 ; then
3abb6a6a 1233 cat "$LOG"
bee0670b
DK
1234 false
1235 fi
6c0765c0 1236 waitforpidfile aptwebserver.pid
e3c62328
DK
1237 local PID="$(cat aptwebserver.pid)"
1238 if [ -z "$PID" ]; then
1239 msgdie 'Could not fork aptwebserver successfully'
1240 fi
1241 addtrap "kill $PID;"
6c0765c0
DK
1242 waitforpidfile aptwebserver.port
1243 APTHTTPPORT="$(cat aptwebserver.port)"
1244 if [ -z "$APTHTTPPORT" ]; then
1245 msgdie 'Could not get port for aptwebserver successfully'
1246 fi
fbd29dd6 1247 cd - > /dev/null
c5bcc607 1248 else
3d284148 1249 msgdie 'You have to build apt from source to have test/interactive-helper/aptwebserver available for tests requiring a webserver'
f213b6ea 1250 fi
6c0765c0
DK
1251 if [ "$REWRTE" != 'yes' ]; then
1252 rewritesourceslist "http://localhost:${APTHTTPPORT}/"
1253 fi
fd46d305
DK
1254}
1255
1256changetohttpswebserver() {
e75e5879 1257 if ! command -v stunnel4 >/dev/null 2>&1; then
fd46d305
DK
1258 msgdie 'You need to install stunnel4 for https testcases'
1259 fi
1260 if [ ! -e "${TMPWORKINGDIRECTORY}/aptarchive/aptwebserver.pid" ]; then
dc95fee1 1261 changetowebserver --no-rewrite "$@"
fd46d305
DK
1262 fi
1263 echo "pid = ${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid
68ba0b7f 1264cert = ${TMPWORKINGDIRECTORY}/rootdir/etc/webserver.pem
23af9f40 1265output = /dev/null
fd46d305
DK
1266
1267[https]
6c0765c0
DK
1268accept = 0
1269connect = $APTHTTPPORT
63c71412 1270" > "${TMPWORKINGDIRECTORY}/stunnel.conf"
fd46d305 1271 stunnel4 "${TMPWORKINGDIRECTORY}/stunnel.conf"
5572f6bd 1272 waitforpidfile "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid"
63c71412 1273 local PID="$(cat "${TMPWORKINGDIRECTORY}/aptarchive/stunnel.pid")"
5572f6bd
MV
1274 if [ -z "$PID" ]; then
1275 msgdie 'Could not fork stunnel4 successfully'
1276 fi
fd46d305 1277 addtrap 'prefix' "kill ${PID};"
6970f6e6 1278 APTHTTPSPORT="$(lsof -i -n | awk "/^stunnel4 / && \$2 == \"${PID}\" {print \$9; exit; }" | cut -d':' -f 2)"
6c0765c0
DK
1279 webserverconfig 'aptwebserver::port::https' "$APTHTTPSPORT" "https://localhost:${APTHTTPSPORT}"
1280 rewritesourceslist "https://localhost:${APTHTTPSPORT}/"
f213b6ea
DK
1281}
1282
c45233ea
DK
1283changetocdrom() {
1284 mkdir -p rootdir/media/cdrom/.disk
1285 local CD="$(readlink -f rootdir/media/cdrom)"
63c71412
DK
1286 cat > rootdir/etc/apt/apt.conf.d/00cdrom <<EOF
1287acquire::cdrom::mount "${CD}";
1288acquire::cdrom::"${CD}/"::mount "mv ${CD}-unmounted ${CD}";
1289acquire::cdrom::"${CD}/"::umount "mv ${CD} ${CD}-unmounted";
1290acquire::cdrom::autodetect 0;
1291EOF
1292 echo -n "$1" > "${CD}/.disk/info"
c45233ea
DK
1293 if [ ! -d aptarchive/dists ]; then
1294 msgdie 'Flat file archive cdroms can not be created currently'
1295 return 1
1296 fi
a0975c8d 1297 mv aptarchive/dists "$CD"
63c71412 1298 ln -s "$(readlink -f ./incoming)" "$CD/pool"
c45233ea 1299 find rootdir/etc/apt/sources.list.d/ -name 'apt-test-*.list' -delete
a0975c8d
DK
1300 # start with an unmounted disk
1301 mv "${CD}" "${CD}-unmounted"
1302 # we don't want the disk to be modifiable
bc8f83a5 1303 addtrap 'prefix' "chmod -f -R +w '$(escape_shell "$PWD/rootdir/media/cdrom/dists/")' '$(escape_shell "$PWD/rootdir/media/cdrom-unmounted/dists/")' || true;"
b0314abb 1304 chmod -R 555 rootdir/media/cdrom-unmounted/dists
c45233ea
DK
1305}
1306
fd46d305 1307downloadfile() {
ed793a19 1308 local PROTO="${1%%:*}"
27925d82 1309 if ! apthelper -o Debug::Acquire::${PROTO}=1 -o Debug::pkgAcquire::Worker=1 \
dcbb364f 1310 download-file "$1" "$2" "$3" 2>&1 ; then
27925d82
DK
1311 return 1
1312 fi
fd46d305 1313 # only if the file exists the download was successful
b0314abb 1314 if [ -r "$2" ]; then
fd46d305
DK
1315 return 0
1316 else
1317 return 1
1318 fi
1319}
1320
f213b6ea 1321checkdiff() {
03aa0847 1322 local DIFFTEXT="$(command diff -u "$@" 2>&1 | sed -e '/^---/ d' -e '/^+++/ d' -e '/^@@/ d')"
8d876415 1323 if [ -n "$DIFFTEXT" ]; then
0d58c26a
DK
1324 echo >&2
1325 echo >&2 "$DIFFTEXT"
8d876415
DK
1326 return 1
1327 else
1328 return 0
1329 fi
1330}
1331
22ac12b2
DK
1332testoutputequal() {
1333 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testoutputequal.output"
1334 local COMPAREFILE="$1"
1335 shift
63c71412 1336 if "$@" 2>&1 | checkdiff "$COMPAREFILE" - >"$OUTPUT" 2>&1; then
22ac12b2
DK
1337 msgpass
1338 else
1339 echo "=== content of file we compared with (${COMPAREFILE}) ===" >>"${OUTPUT}"
1340 cat "$COMPAREFILE" >>"${OUTPUT}"
1341 msgfailoutput '' "$OUTPUT" "$@"
1342 fi
1343}
1344
75954ae2 1345testfileequal() {
1501a2c9 1346 msggroup 'testfileequal'
e6a12ff7
DK
1347 local MSG='Test for correctness of file'
1348 if [ "$1" = '--nomsg' ]; then
1349 MSG=''
1350 shift
1351 fi
75954ae2
DK
1352 local FILE="$1"
1353 shift
e6a12ff7
DK
1354 if [ -n "$MSG" ]; then
1355 msgtest "$MSG" "$FILE"
1356 fi
68042532 1357 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfileequal.output"
75954ae2 1358 if [ -z "$*" ]; then
22ac12b2 1359 testoutputequal "$FILE" echo -n ''
75954ae2 1360 else
22ac12b2 1361 testoutputequal "$FILE" echo "$*"
75954ae2 1362 fi
9beb11aa 1363 msggroup
75954ae2
DK
1364}
1365
b855a400 1366testempty() {
1501a2c9 1367 msggroup 'testempty'
a66e1837
DK
1368 if [ "$1" = '--nomsg' ]; then
1369 shift
1370 else
1371 msgtest "Test for no output of" "$*"
1372 fi
859093da 1373 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testempty.comparefile"
a66e1837 1374 if "$@" >"$COMPAREFILE" 2>&1 && test ! -s "$COMPAREFILE"; then
859093da
DK
1375 msgpass
1376 else
22ac12b2 1377 msgfailoutput '' "$COMPAREFILE" "$@"
859093da 1378 fi
ab25bf1f 1379 aptautotest 'testempty' "$@"
9beb11aa 1380 msggroup
b855a400 1381}
63c71412
DK
1382testnotempty() {
1383 msggroup 'testnotempty'
1384 msgtest "Test for some output of" "$*"
1385 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnotempty.comparefile"
1386 if ("$@" >"$COMPAREFILE" 2>&1 || true) && test -s "$COMPAREFILE"; then
1387 msgpass
1388 else
1389 msgfailoutput '' "$COMPAREFILE" "$@"
1390 fi
1391 aptautotest 'testnotempty' "$@"
1392 msggroup
1393}
b855a400 1394
f74a6fa1 1395testequal() {
1501a2c9 1396 msggroup 'testequal'
f74a6fa1
DK
1397 local MSG='Test of equality of'
1398 if [ "$1" = '--nomsg' ]; then
1399 MSG=''
1400 shift
1401 fi
1402
03938280 1403 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.comparefile"
63c71412 1404 echo "$1" > "$COMPAREFILE"
8d876415 1405 shift
d2d68aaf 1406
f74a6fa1
DK
1407 if [ -n "$MSG" ]; then
1408 msgtest "$MSG" "$*"
1409 fi
22ac12b2 1410 testoutputequal "$COMPAREFILE" "$@"
ab25bf1f 1411 aptautotest 'testequal' "$@"
9beb11aa 1412 msggroup
8d876415
DK
1413}
1414
685625bd 1415testequalor2() {
1501a2c9 1416 msggroup 'testequalor2'
03938280
DK
1417 local COMPAREFILE1="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile1"
1418 local COMPAREFILE2="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.comparefile2"
1419 local COMPAREAGAINST="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequalor2.compareagainst"
63c71412
DK
1420 echo "$1" > "$COMPAREFILE1"
1421 echo "$2" > "$COMPAREFILE2"
685625bd
DK
1422 shift 2
1423 msgtest "Test for equality OR of" "$*"
63c71412
DK
1424 "$@" >"$COMPAREAGAINST" 2>&1 || true
1425 if checkdiff "$COMPAREFILE1" "$COMPAREAGAINST" >/dev/null 2>&1 || \
1426 checkdiff "$COMPAREFILE2" "$COMPAREAGAINST" >/dev/null 2>&1
0d58c26a 1427 then
0caa5a4c
DK
1428 msgpass
1429 else
68042532
DK
1430 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testequal.output"
1431 echo -n "\n${CINFO}Diff against OR 1${CNORMAL}" >"$OUTPUT" 2>&1
1432 checkdiff "$COMPAREFILE1" "$COMPAREAGAINST" >"$OUTPUT" 2>&1 || true
1433 echo -n "${CINFO}Diff against OR 2${CNORMAL}" >"$OUTPUT" 2>&1
1434 checkdiff "$COMPAREFILE2" "$COMPAREAGAINST" >"$OUTPUT" 2>&1 || true
1435 msgfailoutput '' "$OUTPUT"
0caa5a4c 1436 fi
ab25bf1f 1437 aptautotest 'testequalor2' "$@"
9beb11aa 1438 msggroup
685625bd
DK
1439}
1440
8d876415 1441testshowvirtual() {
1501a2c9 1442 msggroup 'testshowvirtual'
edc0ef10 1443 local VIRTUAL="N: Can't select versions from package '$1' as it is purely virtual"
8d876415
DK
1444 local PACKAGE="$1"
1445 shift
1446 while [ -n "$1" ]; do
1447 VIRTUAL="${VIRTUAL}
edc0ef10 1448N: Can't select versions from package '$1' as it is purely virtual"
8d876415
DK
1449 PACKAGE="${PACKAGE} $1"
1450 shift
1451 done
1452 msgtest "Test for virtual packages" "apt-cache show $PACKAGE"
1453 VIRTUAL="${VIRTUAL}
4bec02c2 1454N: No packages found"
03938280 1455 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.comparefile"
ea65d079 1456 local ARCH="$(getarchitecture 'native')"
68042532
DK
1457 echo "$VIRTUAL" | sed -e "s/:$ARCH//" -e 's/:all//' >"$COMPAREFILE"
1458 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testshowvirtual.output"
87d6947d 1459 testoutputequal "$COMPAREFILE" aptcache show "$PACKAGE"
9beb11aa 1460 msggroup
8d876415
DK
1461}
1462
1463testnopackage() {
1501a2c9 1464 msggroup 'testnopackage'
8d876415 1465 msgtest "Test for non-existent packages" "apt-cache show $*"
846856f4 1466 local SHOWPKG="$(aptcache show "$@" 2>&1 | grep '^Package: ')"
8d876415 1467 if [ -n "$SHOWPKG" ]; then
68042532
DK
1468 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnopackage.output"
1469 echo "$SHOWPKG" >"$OUTPUT"
1470 msgfailoutput '' "$OUTPUT"
0d58c26a
DK
1471 else
1472 msgpass
8d876415 1473 fi
9beb11aa 1474 msggroup
8d876415 1475}
bd4a8f51
DK
1476testnosrcpackage() {
1477 msggroup 'testnosrcpackage'
1478 msgtest "Test for non-existent source packages" "apt-cache showsrc $*"
1479 local SHOWPKG="$(aptcache showsrc "$@" 2>&1 | grep '^Package: ')"
1480 if [ -n "$SHOWPKG" ]; then
1481 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnosrcpackage.output"
1482 echo "$SHOWPKG" >"$OUTPUT"
1483 msgfailoutput '' "$OUTPUT"
1484 else
1485 msgpass
1486 fi
1487 msggroup
1488}
01a6e24c 1489
ecb777dd 1490testdpkgstatus() {
1501a2c9 1491 msggroup 'testdpkgstatus'
ecb777dd
DK
1492 local STATE="$1"
1493 local NR="$2"
1494 shift 2
1495 msgtest "Test that $NR package(s) are in state $STATE with" "dpkg -l $*"
1496 local PKGS="$(dpkg -l "$@" 2>/dev/null | grep "^${STATE}" | wc -l)"
1497 if [ "$PKGS" != $NR ]; then
68042532
DK
1498 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testnopackage.output"
1499 echo "$PKGS" >"$OUTPUT"
1500 dpkg -l "$@" | grep '^[a-z]' >"$OUTPUT" >&2 || true
1501 msgfailoutput '' "$OUTPUT"
0d58c26a
DK
1502 else
1503 msgpass
01a6e24c 1504 fi
9beb11aa 1505 msggroup
01a6e24c
DK
1506}
1507
ecb777dd 1508testdpkginstalled() {
1501a2c9 1509 msggroup 'testdpkginstalled'
ecb777dd 1510 testdpkgstatus 'ii' "$#" "$@"
9beb11aa 1511 msggroup
ecb777dd
DK
1512}
1513
5cf733e1 1514testdpkgnotinstalled() {
1501a2c9 1515 msggroup 'testdpkgnotinstalled'
ecb777dd 1516 testdpkgstatus 'ii' '0' "$@"
9beb11aa 1517 msggroup
01a6e24c 1518}
ec7f904e
DK
1519
1520testmarkedauto() {
1501a2c9 1521 msggroup 'testmarkedauto'
03938280 1522 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedauto.comparefile"
ec7f904e
DK
1523 if [ -n "$1" ]; then
1524 msgtest 'Test for correctly marked as auto-installed' "$*"
63c71412 1525 while [ -n "$1" ]; do echo "$1"; shift; done | sort > "$COMPAREFILE"
ec7f904e
DK
1526 else
1527 msgtest 'Test for correctly marked as auto-installed' 'no package'
63c71412 1528 echo -n > "$COMPAREFILE"
ec7f904e 1529 fi
22ac12b2 1530 testoutputequal "$COMPAREFILE" aptmark showauto
9beb11aa 1531 msggroup
ec7f904e 1532}
7c2cc4a7 1533testmarkedmanual() {
1501a2c9 1534 msggroup 'testmarkedmanual'
7c2cc4a7
DK
1535 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testmarkedmanual.comparefile"
1536 if [ -n "$1" ]; then
1537 msgtest 'Test for correctly marked as manually installed' "$*"
63c71412 1538 while [ -n "$1" ]; do echo "$1"; shift; done | sort > "$COMPAREFILE"
7c2cc4a7
DK
1539 else
1540 msgtest 'Test for correctly marked as manually installed' 'no package'
63c71412 1541 echo -n > "$COMPAREFILE"
7c2cc4a7 1542 fi
22ac12b2 1543 testoutputequal "$COMPAREFILE" aptmark showmanual
9beb11aa 1544 msggroup
7c2cc4a7 1545}
89a1aa5d 1546
742f67ea
DK
1547catfile() {
1548 if [ "${1##*.}" = 'deb' ]; then
1549 stat >&2 "$1" || true
1550 file >&2 "$1" || true
1551 else
1552 cat >&2 "$1" || true
1553 fi
1554}
e52aad52 1555msgfailoutput() {
1501a2c9 1556 msgreportheader 'msgfailoutput'
e52aad52
DK
1557 local MSG="$1"
1558 local OUTPUT="$2"
1559 shift 2
e52aad52 1560 if [ "$1" = 'grep' ]; then
68042532 1561 echo >&2
e52aad52
DK
1562 while [ -n "$2" ]; do shift; done
1563 echo "#### Complete file: $1 ####"
742f67ea 1564 catfile "$1"
e52aad52
DK
1565 echo '#### grep output ####'
1566 elif [ "$1" = 'test' ]; then
68042532 1567 echo >&2
e52aad52
DK
1568 # doesn't support ! or non-file flags
1569 msgfailoutputstatfile() {
1570 local FILEFLAGS='^-[bcdefgGhkLOprsStuwx]$'
1571 if expr match "$1" "$FILEFLAGS" >/dev/null; then
1572 echo "#### stat(2) of file: $2 ####"
1573 stat "$2" || true
dffc17ba
DK
1574 if test -d "$2"; then
1575 echo "#### The directory contains: $2 ####"
1576 ls >&2 "$2" || true
1577 elif test -e "$2"; then
3196dae8 1578 echo "#### Complete file: $2 ####"
742f67ea 1579 catfile "$2"
3196dae8 1580 fi
e52aad52
DK
1581 fi
1582 }
1583 msgfailoutputstatfile "$2" "$3"
1584 while [ -n "$5" ] && [ "$4" = '-o' -o "$4" = '-a' ]; do
1585 shift 3
1586 msgfailoutputstatfile "$2" "$3"
1587 done
1588 echo '#### test output ####'
6fc2e030
DK
1589 elif [ "$1" = 'cmp' ]; then
1590 echo >&2
1591 while [ -n "$2" ]; do
1592 echo "#### Complete file: $2 ####"
742f67ea 1593 catfile "$2"
6fc2e030
DK
1594 shift
1595 done
1596 echo '#### cmp output ####'
e52aad52 1597 fi
742f67ea 1598 catfile "$OUTPUT"
e52aad52
DK
1599 msgfail "$MSG"
1600}
1601
671a55ba
DK
1602testsuccesswithglobalerror() {
1603 local TYPE="$1"
1604 local ERRORS="$2"
1605 shift 2
1606 msggroup "$TYPE"
0440d936
DK
1607 if [ "$1" = '--nomsg' ]; then
1608 shift
1609 else
1610 msgtest 'Test for successful execution of' "$*"
1611 fi
671a55ba 1612 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/${TYPE}.output"
63c71412 1613 if "$@" >"${OUTPUT}" 2>&1; then
4fa34122 1614 if expr match "$1" '^apt.*' >/dev/null; then
e52aad52
DK
1615 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1616 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
671a55ba 1617 elif grep -E "^[${ERRORS}]: " "$OUTPUT" > "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" 2>&1; then
3261271e
DK
1618 if [ "$IGNORE_PTY_NOT_MOUNTED" = '1' ]; then
1619 if echo 'E: Can not write log (Is /dev/pts mounted?) - posix_openpt (2: No such file or directory)' \
1620 | cmp - "${TMPWORKINGDIRECTORY}/rootdir/tmp/checkforwarnings.output" >/dev/null 2>&1; then
1621 msgpass
1622 else
1623 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1624 fi
1625 else
1626 msgfailoutput 'successful run, but output contains warnings/errors' "$OUTPUT" "$@"
1627 fi
671a55ba
DK
1628 elif [ "$TYPE" = 'testsuccesswithnotice' ]; then
1629 if grep -q -E "^N: " "$OUTPUT"; then
1630 msgpass
1631 else
1632 msgfailoutput 'successful run, but output had no notices' "$OUTPUT" "$@"
1633 fi
4fa34122
DK
1634 else
1635 msgpass
1636 fi
1637 else
1638 msgpass
1639 fi
1640 else
1641 local EXITCODE=$?
e52aad52 1642 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
4fa34122 1643 fi
671a55ba 1644 aptautotest "$TYPE" "$@"
9beb11aa 1645 msggroup
4fa34122 1646}
671a55ba
DK
1647testsuccesswithnotice() {
1648 testsuccesswithglobalerror 'testsuccesswithnotice' 'WE' "$@"
1649}
1650testsuccess() {
1651 testsuccesswithglobalerror 'testsuccess' 'NWE' "$@"
1652}
4fa34122 1653testwarning() {
1501a2c9 1654 msggroup 'testwarning'
4fa34122
DK
1655 if [ "$1" = '--nomsg' ]; then
1656 shift
1657 else
1658 msgtest 'Test for successful execution with warnings of' "$*"
1659 fi
25b86db1 1660 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output"
63c71412 1661 if "$@" >"${OUTPUT}" 2>&1; then
1df24acf 1662 if expr match "$1" '^apt.*' >/dev/null; then
e52aad52
DK
1663 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1664 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1665 elif grep -q -E '^E: ' "$OUTPUT"; then
1666 msgfailoutput 'successful run, but output contains errors' "$OUTPUT" "$@"
4fa34122 1667 elif ! grep -q -E '^W: ' "$OUTPUT"; then
e52aad52 1668 msgfailoutput 'successful run, but output contains no warnings' "$OUTPUT" "$@"
1df24acf
DK
1669 else
1670 msgpass
1671 fi
1672 else
1673 msgpass
1674 fi
0440d936 1675 else
07cb47e7 1676 local EXITCODE=$?
68042532 1677 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
0440d936 1678 fi
4fa34122 1679 aptautotest 'testwarning' "$@"
9beb11aa 1680 msggroup
0440d936 1681}
0440d936 1682testfailure() {
1501a2c9 1683 msggroup 'testfailure'
0440d936
DK
1684 if [ "$1" = '--nomsg' ]; then
1685 shift
1686 else
889b0072 1687 msgtest 'Test for failure in execution of' "$*"
0440d936 1688 fi
03938280 1689 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output"
63c71412 1690 if "$@" >"${OUTPUT}" 2>&1; then
07cb47e7 1691 local EXITCODE=$?
e52aad52 1692 msgfailoutput "exitcode $EXITCODE" "$OUTPUT" "$@"
0440d936 1693 else
1df24acf
DK
1694 local EXITCODE=$?
1695 if expr match "$1" '^apt.*' >/dev/null; then
b0d40854
DK
1696 if [ "$1" = 'aptkey' ]; then
1697 if grep -q -E " Can't check signature: " "$OUTPUT" || \
1698 grep -q -E " BAD signature from " "$OUTPUT"; then
1699 msgpass
1700 else
1701 msgfailoutput "run failed with exitcode ${EXITCODE}, but no signature error" "$OUTPUT" "$@"
1702 fi
1df24acf 1703 else
b0d40854
DK
1704 if grep -q -E ' runtime error: ' "$OUTPUT"; then
1705 msgfailoutput 'compiler detected undefined behavior' "$OUTPUT" "$@"
1706 elif grep -q -E '==ERROR' "$OUTPUT"; then
1707 msgfailoutput 'compiler sanitizers reported errors' "$OUTPUT" "$@"
1708 elif ! grep -q -E '^E: ' "$OUTPUT"; then
1709 msgfailoutput "run failed with exitcode ${EXITCODE}, but with no errors" "$OUTPUT" "$@"
1710 else
1711 msgpass
1712 fi
1df24acf
DK
1713 fi
1714 else
1715 msgpass
1716 fi
0440d936 1717 fi
ab25bf1f 1718 aptautotest 'testfailure' "$@"
9beb11aa 1719 msggroup
0440d936
DK
1720}
1721
7414af7f
DK
1722testreturnstateequal() {
1723 local STATE="$1"
671a55ba
DK
1724 if [ "$STATE" = 'testsuccesswithglobalerror' ]; then
1725 local STATE="$2"
1726 local TYPE="$3"
7414af7f 1727 shift 3
671a55ba
DK
1728 msggroup "${STATE}equal"
1729 if [ "$1" != '--nomsg' ]; then
1730 local CMP="$1"
1731 shift
1732 testsuccesswithglobalerror "$STATE" "$TYPE" "$@"
1733 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1734 else
1735 local CMP="$2"
1736 shift 2
1737 testsuccesswithglobalerror "$STATE" "$TYPE" "$@"
1738 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1739 fi
1740 else
1741 msggroup "${STATE}equal"
1742 if [ "$2" != '--nomsg' ]; then
1743 local CMP="$2"
1744 shift 2
1745 "$STATE" "$@"
1746 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1747 else
1748 local CMP="$3"
1749 shift 3
1750 "$STATE" --nomsg "$@"
1751 testfileequal "${TMPWORKINGDIRECTORY}/rootdir/tmp/${STATE}.output" "$CMP"
1752 fi
7414af7f 1753 fi
9beb11aa 1754 msggroup
25b86db1 1755}
7414af7f 1756testsuccessequal() {
671a55ba
DK
1757 # we compare output, so we know perfectly well about N:
1758 testreturnstateequal 'testsuccesswithglobalerror' 'testsuccess' 'WE' "$@"
7414af7f 1759}
25b86db1 1760testwarningequal() {
7414af7f 1761 testreturnstateequal 'testwarning' "$@"
25b86db1
DK
1762}
1763testfailureequal() {
7414af7f 1764 testreturnstateequal 'testfailure' "$@"
25b86db1
DK
1765}
1766
27925d82 1767testfailuremsg() {
1501a2c9 1768 msggroup 'testfailuremsg'
27925d82
DK
1769 local CMP="$1"
1770 shift
1771 testfailure "$@"
1772 msgtest 'Check that the output of the previous failed command has expected' 'failures and warnings'
68042532 1773 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailuremsg.comparefile"
002b1bc4 1774 grep '^\(W\|E\|N\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testfailure.output" > "$COMPAREFILE" 2>&1 || true
22ac12b2 1775 testoutputequal "$COMPAREFILE" echo "$CMP"
9beb11aa 1776 msggroup
27925d82 1777}
f18f2338
DK
1778testwarningmsg() {
1779 msggroup 'testwarningmsg'
1780 local CMP="$1"
1781 shift
1782 testwarning "$@"
1783 msgtest 'Check that the output of the previous warned command has expected' 'warnings'
1784 local COMPAREFILE="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarningmsg.comparefile"
002b1bc4 1785 grep '^\(W\|E\|N\):' "${TMPWORKINGDIRECTORY}/rootdir/tmp/testwarning.output" > "$COMPAREFILE" 2>&1 || true
f18f2338
DK
1786 testoutputequal "$COMPAREFILE" echo "$CMP"
1787 msggroup
1788}
25b86db1 1789
de81b2e2 1790testfilestats() {
1501a2c9 1791 msggroup 'testfilestats'
de81b2e2
DK
1792 msgtest "Test that file $1 has $2 $3" "$4"
1793 if [ "$4" "$3" "$(stat --format "$2" "$1")" ]; then
5684f71f
DK
1794 msgpass
1795 else
68042532
DK
1796 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testfilestats.output"
1797 {
1798 ls -ld "$1" || true
1799 echo -n "stat(1) reports for $2: "
1800 stat --format "$2" "$1" || true
1801 } >"$OUTPUT" 2>&1
1802 msgfailoutput '' "$OUTPUT"
5684f71f 1803 fi
9beb11aa 1804 msggroup
5684f71f 1805}
de81b2e2 1806testaccessrights() {
1501a2c9 1807 msggroup 'testaccessrights'
de81b2e2 1808 testfilestats "$1" '%a' '=' "$2"
9beb11aa 1809 msggroup
de81b2e2 1810}
5684f71f 1811
0d58c26a 1812testwebserverlaststatuscode() {
1501a2c9 1813 msggroup 'testwebserverlaststatuscode'
0d58c26a 1814 local DOWNLOG='rootdir/tmp/webserverstatus-testfile.log'
03aa0847 1815 local STATUS='downloaded/webserverstatus-statusfile.log'
0d58c26a
DK
1816 rm -f "$DOWNLOG" "$STATUS"
1817 msgtest 'Test last status code from the webserver was' "$1"
6c0765c0 1818 if downloadfile "http://localhost:${APTHTTPPORT}/_config/find/aptwebserver::last-status-code" "$STATUS" > "$DOWNLOG" && [ "$(cat "$STATUS")" = "$1" ]; then
0d58c26a
DK
1819 msgpass
1820 else
68042532
DK
1821 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/testwebserverlaststatuscode.output"
1822 {
1823 if [ -n "$2" ]; then
1824 shift
1825 echo >&2 '#### Additionally provided output files contain:'
1826 cat >&2 "$@"
1827 fi
1828 echo >&2 '#### Download log of the status code:'
1829 cat >&2 "$DOWNLOG"
1830 } >"$OUTPUT" 2>&1
1831 msgfailoutput "Status was $(cat "$STATUS")" "$OUTPUT"
0d58c26a 1832 fi
9beb11aa 1833 msggroup
0d58c26a
DK
1834}
1835
785cb6fc
DK
1836createlistofkeys() {
1837 local OUTPUT="$1"
1838 shift
1839 while [ -n "$1" ]; do
eb5113c4
JAK
1840 # gpg 2.1.something starts printing [SC] at some point
1841 if grep -q ' rsa2048/' "$OUTPUT" && grep -qF '[SC]' "$OUTPUT"; then
1842 case "$1" in
1843 *Joe*|*Sixpack*) echo 'pub rsa2048/DBAC8DAE 2010-08-18 [SC]';;
1844 *Rex*|*Expired*) echo 'pub rsa2048/27CE74F9 2013-07-12 [SC] [expired: 2013-07-13]';;
1845 *Marvin*|*Paranoid*) echo 'pub rsa2048/528144E2 2011-01-16 [SC]';;
1846 oldarchive) echo 'pub rsa1024/F68C85A3 2013-12-19 [SC]';;
1847 newarchive) echo 'pub rsa2048/DBAC8DAE 2010-08-18 [SC]';;
1848 *) echo 'UNKNOWN KEY';;
1849 esac
785cb6fc 1850 # gpg 2.1 has a slightly different output format
eb5113c4 1851 elif grep -q ' rsa2048/' "$OUTPUT"; then
785cb6fc
DK
1852 case "$1" in
1853 *Joe*|*Sixpack*) echo 'pub rsa2048/DBAC8DAE 2010-08-18';;
1854 *Rex*|*Expired*) echo 'pub rsa2048/27CE74F9 2013-07-12 [expired: 2013-07-13]';;
1855 *Marvin*|*Paranoid*) echo 'pub rsa2048/528144E2 2011-01-16';;
1856 oldarchive) echo 'pub rsa1024/F68C85A3 2013-12-19';;
1857 newarchive) echo 'pub rsa2048/DBAC8DAE 2010-08-18';;
1858 *) echo 'UNKNOWN KEY';;
1859 esac
1860 else
1861 case "$1" in
1862 *Joe*|*Sixpack*) echo 'pub 2048R/DBAC8DAE 2010-08-18';;
1863 *Rex*|*Expired*) echo 'pub 2048R/27CE74F9 2013-07-12 [expired: 2013-07-13]';;
1864 *Marvin*|*Paranoid*) echo 'pub 2048R/528144E2 2011-01-16';;
1865 oldarchive) echo 'pub 1024R/F68C85A3 2013-12-19';;
1866 newarchive) echo 'pub 2048R/DBAC8DAE 2010-08-18';;
1867 *) echo 'UNKNOWN KEY';;
1868 esac
1869 fi
1870 shift
1871 done
1872}
1873testaptkeys() {
1874 local OUTPUT="${TMPWORKINGDIRECTORY}/rootdir/tmp/aptkeylist.output"
1875 if ! aptkey list | grep '^pub' > "$OUTPUT"; then
1876 echo -n > "$OUTPUT"
1877 fi
1878 testfileequal "$OUTPUT" "$(createlistofkeys "$OUTPUT" "$@")"
1879}
1880
89a1aa5d
DK
1881pause() {
1882 echo "STOPPED execution. Press enter to continue"
1883 local IGNORE
1884 read IGNORE
1885}
ab25bf1f 1886
846bc058 1887listcurrentlistsdirectory() {
ba6b79bd
DK
1888 {
1889 find rootdir/var/lib/apt/lists -maxdepth 1 -type d | while read line; do
1890 stat --format '%U:%G:%a:%n' "$line"
1891 done
1892 find rootdir/var/lib/apt/lists -maxdepth 1 \! -type d | while read line; do
1893 stat --format '%U:%G:%a:%s:%y:%n' "$line"
1894 done
1895 } | sort
846bc058 1896}
912a6131 1897forallsupportedcompressors() {
b2fd8524 1898 rm -f "${TMPWORKINGDIRECTORY}/rootdir/etc/apt/apt.conf.d/00force-compressor"
912a6131 1899 for COMP in $(aptconfig dump 'APT::Compressor' --format '%f%n' | cut -d':' -f 5 | uniq); do
0179cfa8 1900 if [ -z "$COMP" -o "$COMP" = '.' ]; then continue; fi
912a6131
DK
1901 "$@" "$COMP"
1902 done
1903}
846bc058 1904
3a8776a3 1905### convenience hacks ###
8fe964f1
DK
1906mkdir() {
1907 # creating some directories by hand is a tedious task, so make it look simple
6aef1942
DK
1908 local PARAMS="$*"
1909 if [ "$PARAMS" != "${PARAMS#*rootdir/var/lib/apt/lists}" ]; then
8fe964f1
DK
1910 # only the last directory created by mkdir is effected by the -m !
1911 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt"
1912 command mkdir -m 755 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"
1913 command mkdir -m 700 -p "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1914 touch "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/lock"
1915 if [ "$(id -u)" = '0' ]; then
1916 chown _apt:root "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial"
1917 fi
1918 else
1919 command mkdir "$@"
1920 fi
1921}
1922
ab25bf1f
DK
1923### The following tests are run by most test methods automatically to check
1924### general things about commands executed without writing the test every time.
1925
1926aptautotest() {
1927 local TESTCALL="$1"
1928 local CMD="$2"
1929 local FIRSTOPT="$3"
896f0ae8 1930 local AUTOTEST="aptautotest_$(echo "${CMD##*/}_${FIRSTOPT}" | tr -d -c 'A-za-z0-9')"
ab25bf1f
DK
1931 if command -v $AUTOTEST >/dev/null; then
1932 shift 3
1933 # save and restore the *.output files from other tests
1934 # as we might otherwise override them in these automatic tests
63c71412
DK
1935 rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
1936 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-before"
1937 mkdir "${TMPWORKINGDIRECTORY}/rootdir/tmp"
ab25bf1f 1938 $AUTOTEST "$TESTCALL" "$@"
63c71412
DK
1939 rm -rf "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
1940 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp" "${TMPWORKINGDIRECTORY}/rootdir/tmp-aptautotest"
1941 mv "${TMPWORKINGDIRECTORY}/rootdir/tmp-before" "${TMPWORKINGDIRECTORY}/rootdir/tmp"
ab25bf1f
DK
1942 fi
1943}
1944
1945aptautotest_aptget_update() {
59148d96
DK
1946 local TESTCALL="$1"
1947 while [ -n "$2" ]; do
1948 if [ "$2" = '--print-uris' ]; then return; fi # simulation mode
1949 shift
1950 done
ab25bf1f 1951 if ! test -d "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists"; then return; fi
4bb006d1
DK
1952 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
1953 testfilestats "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:755"
ab25bf1f 1954 # all copied files are properly chmodded
63c71412
DK
1955 local backupIFS="$IFS"
1956 IFS="$(printf "\n\b")"
146f7715 1957 for file in $(find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists" -type f ! -name 'lock'); do
4bb006d1 1958 testfilestats "$file" '%U:%G:%a' '=' "${TEST_DEFAULT_USER}:${TEST_DEFAULT_GROUP}:644"
ab25bf1f 1959 done
63c71412 1960 IFS="$backupIFS"
59148d96 1961 if [ "$TESTCALL" = 'testsuccess' ]; then
146f7715
DK
1962 # failure cases can retain partial files and such
1963 testempty find "${TMPWORKINGDIRECTORY}/rootdir/var/lib/apt/lists/partial" -mindepth 1 ! \( -name 'lock' -o -name '*.FAILED' \)
1964 fi
ab25bf1f
DK
1965}
1966aptautotest_apt_update() { aptautotest_aptget_update "$@"; }
d84da499 1967aptautotest_aptcdrom_add() { aptautotest_aptget_update "$@"; }
016bea82
DK
1968
1969testaptautotestnodpkgwarning() {
1970 local TESTCALL="$1"
1971 while [ -n "$2" ]; do
6bf93605
DK
1972 if expr match "$2" '^-[a-z]*s' >/dev/null 2>&1; then return; fi # simulation mode
1973 if expr match "$2" '^-dy\?' >/dev/null 2>&1; then return; fi # download-only mode
016bea82
DK
1974 shift
1975 done
1976 testfailure grep '^dpkg: warning:.*ignor.*' "${TMPWORKINGDIRECTORY}/rootdir/tmp-before/${TESTCALL}.output"
1977}
1978
1979aptautotest_aptget_install() { testaptautotestnodpkgwarning "$@"; }
1980aptautotest_aptget_remove() { testaptautotestnodpkgwarning "$@"; }
1981aptautotest_aptget_purge() { testaptautotestnodpkgwarning "$@"; }
1982aptautotest_apt_install() { testaptautotestnodpkgwarning "$@"; }
1983aptautotest_apt_remove() { testaptautotestnodpkgwarning "$@"; }
1984aptautotest_apt_purge() { testaptautotestnodpkgwarning "$@"; }