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