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