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