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