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