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