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