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