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