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