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